Commit | Line | Data |
---|---|---|
50656eec | 1 | /* |
0e60836b | 2 | * probe-event.c : perf-probe definition to probe_events format converter |
50656eec MH |
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 | ||
50656eec MH |
22 | #include <sys/utsname.h> |
23 | #include <sys/types.h> | |
24 | #include <sys/stat.h> | |
25 | #include <fcntl.h> | |
26 | #include <errno.h> | |
27 | #include <stdio.h> | |
28 | #include <unistd.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
4de189fe MH |
31 | #include <stdarg.h> |
32 | #include <limits.h> | |
e80711ca | 33 | #include <elf.h> |
50656eec | 34 | |
31facc5f | 35 | #include "util.h" |
50656eec | 36 | #include "event.h" |
4de189fe | 37 | #include "strlist.h" |
50656eec | 38 | #include "debug.h" |
72041334 | 39 | #include "cache.h" |
631c9def | 40 | #include "color.h" |
e0faa8d3 MH |
41 | #include "symbol.h" |
42 | #include "thread.h" | |
553873e1 | 43 | #include <api/fs/debugfs.h> |
1d037ca1 | 44 | #include "trace-event.h" /* For __maybe_unused */ |
50656eec | 45 | #include "probe-event.h" |
4235b045 | 46 | #include "probe-finder.h" |
225466f1 | 47 | #include "session.h" |
50656eec MH |
48 | |
49 | #define MAX_CMDLEN 256 | |
50656eec MH |
50 | #define PERFPROBE_GROUP "probe" |
51 | ||
f4d7da49 MH |
52 | bool probe_event_dry_run; /* Dry run flag */ |
53 | ||
146a1439 | 54 | #define semantic_error(msg ...) pr_err("Semantic error :" msg) |
50656eec | 55 | |
4de189fe | 56 | /* If there is no space to write, returns -E2BIG. */ |
84988450 MH |
57 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
58 | __attribute__((format(printf, 3, 4))); | |
59 | ||
4de189fe MH |
60 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
61 | { | |
62 | int ret; | |
63 | va_list ap; | |
64 | va_start(ap, format); | |
65 | ret = vsnprintf(str, size, format, ap); | |
66 | va_end(ap); | |
67 | if (ret >= (int)size) | |
68 | ret = -E2BIG; | |
69 | return ret; | |
70 | } | |
71 | ||
4b4da7f7 | 72 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp); |
225466f1 SD |
73 | static int convert_name_to_addr(struct perf_probe_event *pev, |
74 | const char *exec); | |
d28c6223 | 75 | static struct machine machine; |
e0faa8d3 | 76 | |
469b9b88 | 77 | /* Initialize symbol maps and path of vmlinux/modules */ |
146a1439 | 78 | static int init_vmlinux(void) |
e0faa8d3 | 79 | { |
146a1439 MH |
80 | int ret; |
81 | ||
e0faa8d3 MH |
82 | symbol_conf.sort_by_name = true; |
83 | if (symbol_conf.vmlinux_name == NULL) | |
84 | symbol_conf.try_vmlinux_path = true; | |
85 | else | |
86 | pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); | |
146a1439 MH |
87 | ret = symbol__init(); |
88 | if (ret < 0) { | |
89 | pr_debug("Failed to init symbol map.\n"); | |
90 | goto out; | |
91 | } | |
e0faa8d3 | 92 | |
469b9b88 | 93 | ret = machine__init(&machine, "", HOST_KERNEL_ID); |
d28c6223 ACM |
94 | if (ret < 0) |
95 | goto out; | |
96 | ||
469b9b88 | 97 | if (machine__create_kernel_maps(&machine) < 0) { |
0e43e5d2 | 98 | pr_debug("machine__create_kernel_maps() failed.\n"); |
469b9b88 MH |
99 | goto out; |
100 | } | |
146a1439 MH |
101 | out: |
102 | if (ret < 0) | |
103 | pr_warning("Failed to init vmlinux path.\n"); | |
104 | return ret; | |
e0faa8d3 MH |
105 | } |
106 | ||
469b9b88 MH |
107 | static struct symbol *__find_kernel_function_by_name(const char *name, |
108 | struct map **mapp) | |
109 | { | |
110 | return machine__find_kernel_function_by_name(&machine, name, mapp, | |
111 | NULL); | |
112 | } | |
113 | ||
e80711ca MH |
114 | static struct map *kernel_get_module_map(const char *module) |
115 | { | |
116 | struct rb_node *nd; | |
117 | struct map_groups *grp = &machine.kmaps; | |
118 | ||
14a8fd7c MH |
119 | /* A file path -- this is an offline module */ |
120 | if (module && strchr(module, '/')) | |
121 | return machine__new_module(&machine, 0, module); | |
122 | ||
e80711ca MH |
123 | if (!module) |
124 | module = "kernel"; | |
125 | ||
126 | for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) { | |
127 | struct map *pos = rb_entry(nd, struct map, rb_node); | |
128 | if (strncmp(pos->dso->short_name + 1, module, | |
129 | pos->dso->short_name_len - 2) == 0) { | |
130 | return pos; | |
131 | } | |
132 | } | |
133 | return NULL; | |
134 | } | |
135 | ||
136 | static struct dso *kernel_get_module_dso(const char *module) | |
469b9b88 MH |
137 | { |
138 | struct dso *dso; | |
fd930ff9 FBH |
139 | struct map *map; |
140 | const char *vmlinux_name; | |
469b9b88 MH |
141 | |
142 | if (module) { | |
143 | list_for_each_entry(dso, &machine.kernel_dsos, node) { | |
144 | if (strncmp(dso->short_name + 1, module, | |
145 | dso->short_name_len - 2) == 0) | |
146 | goto found; | |
147 | } | |
148 | pr_debug("Failed to find module %s.\n", module); | |
149 | return NULL; | |
fd930ff9 FBH |
150 | } |
151 | ||
152 | map = machine.vmlinux_maps[MAP__FUNCTION]; | |
153 | dso = map->dso; | |
154 | ||
155 | vmlinux_name = symbol_conf.vmlinux_name; | |
156 | if (vmlinux_name) { | |
5230fb7d | 157 | if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0) |
fd930ff9 | 158 | return NULL; |
469b9b88 | 159 | } else { |
c3a34e06 | 160 | if (dso__load_vmlinux_path(dso, map, NULL) <= 0) { |
469b9b88 MH |
161 | pr_debug("Failed to load kernel map.\n"); |
162 | return NULL; | |
163 | } | |
164 | } | |
165 | found: | |
e80711ca MH |
166 | return dso; |
167 | } | |
168 | ||
169 | const char *kernel_get_module_path(const char *module) | |
170 | { | |
171 | struct dso *dso = kernel_get_module_dso(module); | |
172 | return (dso) ? dso->long_name : NULL; | |
469b9b88 MH |
173 | } |
174 | ||
fb7345bb MH |
175 | /* Copied from unwind.c */ |
176 | static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, | |
177 | GElf_Shdr *shp, const char *name) | |
178 | { | |
179 | Elf_Scn *sec = NULL; | |
180 | ||
181 | while ((sec = elf_nextscn(elf, sec)) != NULL) { | |
182 | char *str; | |
183 | ||
184 | gelf_getshdr(sec, shp); | |
185 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); | |
186 | if (!strcmp(name, str)) | |
187 | break; | |
188 | } | |
189 | ||
190 | return sec; | |
191 | } | |
192 | ||
193 | static int get_text_start_address(const char *exec, unsigned long *address) | |
194 | { | |
195 | Elf *elf; | |
196 | GElf_Ehdr ehdr; | |
197 | GElf_Shdr shdr; | |
198 | int fd, ret = -ENOENT; | |
199 | ||
200 | fd = open(exec, O_RDONLY); | |
201 | if (fd < 0) | |
202 | return -errno; | |
203 | ||
204 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); | |
205 | if (elf == NULL) | |
206 | return -EINVAL; | |
207 | ||
208 | if (gelf_getehdr(elf, &ehdr) == NULL) | |
209 | goto out; | |
210 | ||
211 | if (!elf_section_by_name(elf, &ehdr, &shdr, ".text")) | |
212 | goto out; | |
213 | ||
214 | *address = shdr.sh_addr - shdr.sh_offset; | |
215 | ret = 0; | |
216 | out: | |
217 | elf_end(elf); | |
218 | return ret; | |
219 | } | |
220 | ||
225466f1 SD |
221 | static int init_user_exec(void) |
222 | { | |
223 | int ret = 0; | |
224 | ||
225 | symbol_conf.try_vmlinux_path = false; | |
226 | symbol_conf.sort_by_name = true; | |
227 | ret = symbol__init(); | |
228 | ||
229 | if (ret < 0) | |
230 | pr_debug("Failed to init symbol map.\n"); | |
231 | ||
232 | return ret; | |
233 | } | |
234 | ||
fb7345bb MH |
235 | static int convert_exec_to_group(const char *exec, char **result) |
236 | { | |
237 | char *ptr1, *ptr2, *exec_copy; | |
238 | char buf[64]; | |
239 | int ret; | |
240 | ||
241 | exec_copy = strdup(exec); | |
242 | if (!exec_copy) | |
243 | return -ENOMEM; | |
244 | ||
245 | ptr1 = basename(exec_copy); | |
246 | if (!ptr1) { | |
247 | ret = -EINVAL; | |
248 | goto out; | |
249 | } | |
250 | ||
251 | ptr2 = strpbrk(ptr1, "-._"); | |
252 | if (ptr2) | |
253 | *ptr2 = '\0'; | |
254 | ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1); | |
255 | if (ret < 0) | |
256 | goto out; | |
257 | ||
258 | *result = strdup(buf); | |
259 | ret = *result ? 0 : -ENOMEM; | |
260 | ||
261 | out: | |
262 | free(exec_copy); | |
263 | return ret; | |
264 | } | |
265 | ||
225466f1 SD |
266 | static int convert_to_perf_probe_point(struct probe_trace_point *tp, |
267 | struct perf_probe_point *pp) | |
268 | { | |
269 | pp->function = strdup(tp->symbol); | |
270 | ||
271 | if (pp->function == NULL) | |
272 | return -ENOMEM; | |
273 | ||
274 | pp->offset = tp->offset; | |
275 | pp->retprobe = tp->retprobe; | |
276 | ||
277 | return 0; | |
278 | } | |
279 | ||
89fe808a | 280 | #ifdef HAVE_DWARF_SUPPORT |
ff741783 MH |
281 | /* Open new debuginfo of given module */ |
282 | static struct debuginfo *open_debuginfo(const char *module) | |
e0faa8d3 | 283 | { |
14a8fd7c | 284 | const char *path; |
ff741783 | 285 | |
14a8fd7c MH |
286 | /* A file path -- this is an offline module */ |
287 | if (module && strchr(module, '/')) | |
288 | path = module; | |
289 | else { | |
290 | path = kernel_get_module_path(module); | |
291 | ||
292 | if (!path) { | |
293 | pr_err("Failed to find path of %s module.\n", | |
294 | module ?: "kernel"); | |
295 | return NULL; | |
296 | } | |
e0faa8d3 | 297 | } |
ff741783 | 298 | return debuginfo__new(path); |
e0faa8d3 | 299 | } |
4b4da7f7 | 300 | |
0e60836b SD |
301 | /* |
302 | * Convert trace point to probe point with debuginfo | |
303 | * Currently only handles kprobes. | |
304 | */ | |
305 | static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp, | |
469b9b88 | 306 | struct perf_probe_point *pp) |
4b4da7f7 MH |
307 | { |
308 | struct symbol *sym; | |
469b9b88 MH |
309 | struct map *map; |
310 | u64 addr; | |
311 | int ret = -ENOENT; | |
ff741783 | 312 | struct debuginfo *dinfo; |
4b4da7f7 | 313 | |
469b9b88 | 314 | sym = __find_kernel_function_by_name(tp->symbol, &map); |
4b4da7f7 | 315 | if (sym) { |
469b9b88 | 316 | addr = map->unmap_ip(map, sym->start + tp->offset); |
9486aa38 | 317 | pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol, |
469b9b88 | 318 | tp->offset, addr); |
ff741783 MH |
319 | |
320 | dinfo = debuginfo__new_online_kernel(addr); | |
321 | if (dinfo) { | |
322 | ret = debuginfo__find_probe_point(dinfo, | |
323 | (unsigned long)addr, pp); | |
324 | debuginfo__delete(dinfo); | |
325 | } else { | |
326 | pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n", | |
327 | addr); | |
328 | ret = -ENOENT; | |
329 | } | |
4b4da7f7 MH |
330 | } |
331 | if (ret <= 0) { | |
146a1439 MH |
332 | pr_debug("Failed to find corresponding probes from " |
333 | "debuginfo. Use kprobe event information.\n"); | |
225466f1 | 334 | return convert_to_perf_probe_point(tp, pp); |
4b4da7f7 MH |
335 | } |
336 | pp->retprobe = tp->retprobe; | |
146a1439 MH |
337 | |
338 | return 0; | |
4b4da7f7 MH |
339 | } |
340 | ||
fb7345bb MH |
341 | static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs, |
342 | int ntevs, const char *exec) | |
343 | { | |
344 | int i, ret = 0; | |
345 | unsigned long offset, stext = 0; | |
346 | char buf[32]; | |
347 | ||
348 | if (!exec) | |
349 | return 0; | |
350 | ||
351 | ret = get_text_start_address(exec, &stext); | |
352 | if (ret < 0) | |
353 | return ret; | |
354 | ||
355 | for (i = 0; i < ntevs && ret >= 0; i++) { | |
356 | offset = tevs[i].point.address - stext; | |
357 | offset += tevs[i].point.offset; | |
358 | tevs[i].point.offset = 0; | |
359 | free(tevs[i].point.symbol); | |
360 | ret = e_snprintf(buf, 32, "0x%lx", offset); | |
361 | if (ret < 0) | |
362 | break; | |
363 | tevs[i].point.module = strdup(exec); | |
364 | tevs[i].point.symbol = strdup(buf); | |
365 | if (!tevs[i].point.symbol || !tevs[i].point.module) { | |
366 | ret = -ENOMEM; | |
367 | break; | |
368 | } | |
369 | tevs[i].uprobes = true; | |
370 | } | |
371 | ||
372 | return ret; | |
373 | } | |
374 | ||
190b57fc MH |
375 | static int add_module_to_probe_trace_events(struct probe_trace_event *tevs, |
376 | int ntevs, const char *module) | |
377 | { | |
14a8fd7c MH |
378 | int i, ret = 0; |
379 | char *tmp; | |
380 | ||
381 | if (!module) | |
382 | return 0; | |
383 | ||
384 | tmp = strrchr(module, '/'); | |
385 | if (tmp) { | |
386 | /* This is a module path -- get the module name */ | |
387 | module = strdup(tmp + 1); | |
388 | if (!module) | |
389 | return -ENOMEM; | |
390 | tmp = strchr(module, '.'); | |
391 | if (tmp) | |
392 | *tmp = '\0'; | |
393 | tmp = (char *)module; /* For free() */ | |
394 | } | |
395 | ||
190b57fc MH |
396 | for (i = 0; i < ntevs; i++) { |
397 | tevs[i].point.module = strdup(module); | |
14a8fd7c MH |
398 | if (!tevs[i].point.module) { |
399 | ret = -ENOMEM; | |
400 | break; | |
401 | } | |
190b57fc | 402 | } |
14a8fd7c MH |
403 | |
404 | if (tmp) | |
405 | free(tmp); | |
406 | ||
407 | return ret; | |
190b57fc MH |
408 | } |
409 | ||
4b4da7f7 | 410 | /* Try to find perf_probe_event with debuginfo */ |
0e60836b | 411 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
190b57fc | 412 | struct probe_trace_event **tevs, |
4eced234 | 413 | int max_tevs, const char *target) |
4b4da7f7 MH |
414 | { |
415 | bool need_dwarf = perf_probe_event_need_dwarf(pev); | |
225466f1 | 416 | struct debuginfo *dinfo; |
190b57fc | 417 | int ntevs, ret = 0; |
4b4da7f7 | 418 | |
225466f1 SD |
419 | dinfo = open_debuginfo(target); |
420 | ||
ff741783 | 421 | if (!dinfo) { |
146a1439 MH |
422 | if (need_dwarf) { |
423 | pr_warning("Failed to open debuginfo file.\n"); | |
ff741783 | 424 | return -ENOENT; |
146a1439 | 425 | } |
ff741783 | 426 | pr_debug("Could not open debuginfo. Try to use symbols.\n"); |
4b4da7f7 MH |
427 | return 0; |
428 | } | |
429 | ||
ff741783 MH |
430 | /* Searching trace events corresponding to a probe event */ |
431 | ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs); | |
432 | ||
433 | debuginfo__delete(dinfo); | |
4b4da7f7 | 434 | |
146a1439 | 435 | if (ntevs > 0) { /* Succeeded to find trace events */ |
0e60836b | 436 | pr_debug("find %d probe_trace_events.\n", ntevs); |
fb7345bb MH |
437 | if (target) { |
438 | if (pev->uprobes) | |
439 | ret = add_exec_to_probe_trace_events(*tevs, | |
440 | ntevs, target); | |
441 | else | |
442 | ret = add_module_to_probe_trace_events(*tevs, | |
443 | ntevs, target); | |
444 | } | |
190b57fc | 445 | return ret < 0 ? ret : ntevs; |
146a1439 | 446 | } |
4b4da7f7 | 447 | |
146a1439 MH |
448 | if (ntevs == 0) { /* No error but failed to find probe point. */ |
449 | pr_warning("Probe point '%s' not found.\n", | |
450 | synthesize_perf_probe_point(&pev->point)); | |
451 | return -ENOENT; | |
452 | } | |
453 | /* Error path : ntevs < 0 */ | |
15eca306 MH |
454 | pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); |
455 | if (ntevs == -EBADF) { | |
456 | pr_warning("Warning: No dwarf info found in the vmlinux - " | |
457 | "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n"); | |
458 | if (!need_dwarf) { | |
0e43e5d2 | 459 | pr_debug("Trying to use symbols.\n"); |
15eca306 MH |
460 | return 0; |
461 | } | |
4b4da7f7 | 462 | } |
15eca306 | 463 | return ntevs; |
4b4da7f7 MH |
464 | } |
465 | ||
7cf0b79e MH |
466 | /* |
467 | * Find a src file from a DWARF tag path. Prepend optional source path prefix | |
468 | * and chop off leading directories that do not exist. Result is passed back as | |
469 | * a newly allocated path on success. | |
470 | * Return 0 if file was found and readable, -errno otherwise. | |
471 | */ | |
6a330a3c MH |
472 | static int get_real_path(const char *raw_path, const char *comp_dir, |
473 | char **new_path) | |
7cf0b79e | 474 | { |
6a330a3c MH |
475 | const char *prefix = symbol_conf.source_prefix; |
476 | ||
477 | if (!prefix) { | |
478 | if (raw_path[0] != '/' && comp_dir) | |
479 | /* If not an absolute path, try to use comp_dir */ | |
480 | prefix = comp_dir; | |
481 | else { | |
482 | if (access(raw_path, R_OK) == 0) { | |
483 | *new_path = strdup(raw_path); | |
484 | return 0; | |
485 | } else | |
486 | return -errno; | |
487 | } | |
7cf0b79e MH |
488 | } |
489 | ||
6a330a3c | 490 | *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2)); |
7cf0b79e MH |
491 | if (!*new_path) |
492 | return -ENOMEM; | |
493 | ||
494 | for (;;) { | |
6a330a3c | 495 | sprintf(*new_path, "%s/%s", prefix, raw_path); |
7cf0b79e MH |
496 | |
497 | if (access(*new_path, R_OK) == 0) | |
498 | return 0; | |
499 | ||
6a330a3c MH |
500 | if (!symbol_conf.source_prefix) |
501 | /* In case of searching comp_dir, don't retry */ | |
502 | return -errno; | |
503 | ||
7cf0b79e MH |
504 | switch (errno) { |
505 | case ENAMETOOLONG: | |
506 | case ENOENT: | |
507 | case EROFS: | |
508 | case EFAULT: | |
509 | raw_path = strchr(++raw_path, '/'); | |
510 | if (!raw_path) { | |
511 | free(*new_path); | |
512 | *new_path = NULL; | |
513 | return -ENOENT; | |
514 | } | |
515 | continue; | |
516 | ||
517 | default: | |
518 | free(*new_path); | |
519 | *new_path = NULL; | |
520 | return -errno; | |
521 | } | |
522 | } | |
523 | } | |
524 | ||
4b4da7f7 MH |
525 | #define LINEBUF_SIZE 256 |
526 | #define NR_ADDITIONAL_LINES 2 | |
527 | ||
fde52dbd | 528 | static int __show_one_line(FILE *fp, int l, bool skip, bool show_num) |
4b4da7f7 MH |
529 | { |
530 | char buf[LINEBUF_SIZE]; | |
befe3414 FBH |
531 | const char *color = show_num ? "" : PERF_COLOR_BLUE; |
532 | const char *prefix = NULL; | |
4b4da7f7 | 533 | |
befe3414 | 534 | do { |
4b4da7f7 MH |
535 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
536 | goto error; | |
befe3414 FBH |
537 | if (skip) |
538 | continue; | |
539 | if (!prefix) { | |
540 | prefix = show_num ? "%7d " : " "; | |
541 | color_fprintf(stdout, color, prefix, l); | |
4b4da7f7 | 542 | } |
befe3414 FBH |
543 | color_fprintf(stdout, color, "%s", buf); |
544 | ||
545 | } while (strchr(buf, '\n') == NULL); | |
146a1439 | 546 | |
fde52dbd | 547 | return 1; |
4b4da7f7 | 548 | error: |
fde52dbd | 549 | if (ferror(fp)) { |
32b2b6ec | 550 | pr_warning("File read error: %s\n", strerror(errno)); |
fde52dbd FBH |
551 | return -1; |
552 | } | |
553 | return 0; | |
554 | } | |
146a1439 | 555 | |
fde52dbd FBH |
556 | static int _show_one_line(FILE *fp, int l, bool skip, bool show_num) |
557 | { | |
558 | int rv = __show_one_line(fp, l, skip, show_num); | |
559 | if (rv == 0) { | |
560 | pr_warning("Source file is shorter than expected.\n"); | |
561 | rv = -1; | |
562 | } | |
563 | return rv; | |
4b4da7f7 MH |
564 | } |
565 | ||
fde52dbd FBH |
566 | #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true) |
567 | #define show_one_line(f,l) _show_one_line(f,l,false,false) | |
568 | #define skip_one_line(f,l) _show_one_line(f,l,true,false) | |
569 | #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false) | |
570 | ||
4b4da7f7 MH |
571 | /* |
572 | * Show line-range always requires debuginfo to find source file and | |
573 | * line number. | |
574 | */ | |
469b9b88 | 575 | int show_line_range(struct line_range *lr, const char *module) |
4b4da7f7 | 576 | { |
d3b63d7a | 577 | int l = 1; |
4b4da7f7 | 578 | struct line_node *ln; |
ff741783 | 579 | struct debuginfo *dinfo; |
4b4da7f7 | 580 | FILE *fp; |
ff741783 | 581 | int ret; |
7cf0b79e | 582 | char *tmp; |
4b4da7f7 MH |
583 | |
584 | /* Search a line range */ | |
146a1439 MH |
585 | ret = init_vmlinux(); |
586 | if (ret < 0) | |
587 | return ret; | |
588 | ||
ff741783 MH |
589 | dinfo = open_debuginfo(module); |
590 | if (!dinfo) { | |
146a1439 | 591 | pr_warning("Failed to open debuginfo file.\n"); |
ff741783 | 592 | return -ENOENT; |
146a1439 MH |
593 | } |
594 | ||
ff741783 MH |
595 | ret = debuginfo__find_line_range(dinfo, lr); |
596 | debuginfo__delete(dinfo); | |
146a1439 MH |
597 | if (ret == 0) { |
598 | pr_warning("Specified source line is not found.\n"); | |
599 | return -ENOENT; | |
600 | } else if (ret < 0) { | |
601 | pr_warning("Debuginfo analysis failed. (%d)\n", ret); | |
602 | return ret; | |
603 | } | |
4b4da7f7 | 604 | |
7cf0b79e MH |
605 | /* Convert source file path */ |
606 | tmp = lr->path; | |
6a330a3c | 607 | ret = get_real_path(tmp, lr->comp_dir, &lr->path); |
7cf0b79e MH |
608 | free(tmp); /* Free old path */ |
609 | if (ret < 0) { | |
610 | pr_warning("Failed to find source file. (%d)\n", ret); | |
611 | return ret; | |
612 | } | |
613 | ||
4b4da7f7 MH |
614 | setup_pager(); |
615 | ||
616 | if (lr->function) | |
8737ebde | 617 | fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path, |
4b4da7f7 MH |
618 | lr->start - lr->offset); |
619 | else | |
62c15fc4 | 620 | fprintf(stdout, "<%s:%d>\n", lr->path, lr->start); |
4b4da7f7 MH |
621 | |
622 | fp = fopen(lr->path, "r"); | |
146a1439 MH |
623 | if (fp == NULL) { |
624 | pr_warning("Failed to open %s: %s\n", lr->path, | |
625 | strerror(errno)); | |
626 | return -errno; | |
627 | } | |
4b4da7f7 | 628 | /* Skip to starting line number */ |
44b81e92 | 629 | while (l < lr->start) { |
fde52dbd | 630 | ret = skip_one_line(fp, l++); |
44b81e92 FBH |
631 | if (ret < 0) |
632 | goto end; | |
633 | } | |
4b4da7f7 MH |
634 | |
635 | list_for_each_entry(ln, &lr->line_list, list) { | |
44b81e92 | 636 | for (; ln->line > l; l++) { |
fde52dbd | 637 | ret = show_one_line(fp, l - lr->offset); |
44b81e92 FBH |
638 | if (ret < 0) |
639 | goto end; | |
640 | } | |
fde52dbd | 641 | ret = show_one_line_with_num(fp, l++ - lr->offset); |
146a1439 MH |
642 | if (ret < 0) |
643 | goto end; | |
4b4da7f7 MH |
644 | } |
645 | ||
646 | if (lr->end == INT_MAX) | |
647 | lr->end = l + NR_ADDITIONAL_LINES; | |
fde52dbd FBH |
648 | while (l <= lr->end) { |
649 | ret = show_one_line_or_eof(fp, l++ - lr->offset); | |
650 | if (ret <= 0) | |
44b81e92 FBH |
651 | break; |
652 | } | |
146a1439 | 653 | end: |
4b4da7f7 | 654 | fclose(fp); |
146a1439 | 655 | return ret; |
4b4da7f7 MH |
656 | } |
657 | ||
ff741783 MH |
658 | static int show_available_vars_at(struct debuginfo *dinfo, |
659 | struct perf_probe_event *pev, | |
bd09d7b5 MH |
660 | int max_vls, struct strfilter *_filter, |
661 | bool externs) | |
cf6eb489 MH |
662 | { |
663 | char *buf; | |
bd09d7b5 | 664 | int ret, i, nvars; |
cf6eb489 MH |
665 | struct str_node *node; |
666 | struct variable_list *vls = NULL, *vl; | |
bd09d7b5 | 667 | const char *var; |
cf6eb489 MH |
668 | |
669 | buf = synthesize_perf_probe_point(&pev->point); | |
670 | if (!buf) | |
671 | return -EINVAL; | |
672 | pr_debug("Searching variables at %s\n", buf); | |
673 | ||
ff741783 MH |
674 | ret = debuginfo__find_available_vars_at(dinfo, pev, &vls, |
675 | max_vls, externs); | |
bd09d7b5 MH |
676 | if (ret <= 0) { |
677 | pr_err("Failed to find variables at %s (%d)\n", buf, ret); | |
678 | goto end; | |
679 | } | |
680 | /* Some variables are found */ | |
681 | fprintf(stdout, "Available variables at %s\n", buf); | |
682 | for (i = 0; i < ret; i++) { | |
683 | vl = &vls[i]; | |
684 | /* | |
685 | * A probe point might be converted to | |
686 | * several trace points. | |
687 | */ | |
688 | fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol, | |
689 | vl->point.offset); | |
690 | free(vl->point.symbol); | |
691 | nvars = 0; | |
692 | if (vl->vars) { | |
693 | strlist__for_each(node, vl->vars) { | |
694 | var = strchr(node->s, '\t') + 1; | |
695 | if (strfilter__compare(_filter, var)) { | |
cf6eb489 | 696 | fprintf(stdout, "\t\t%s\n", node->s); |
bd09d7b5 MH |
697 | nvars++; |
698 | } | |
699 | } | |
700 | strlist__delete(vl->vars); | |
cf6eb489 | 701 | } |
bd09d7b5 MH |
702 | if (nvars == 0) |
703 | fprintf(stdout, "\t\t(No matched variables)\n"); | |
704 | } | |
705 | free(vls); | |
706 | end: | |
cf6eb489 MH |
707 | free(buf); |
708 | return ret; | |
709 | } | |
710 | ||
711 | /* Show available variables on given probe point */ | |
712 | int show_available_vars(struct perf_probe_event *pevs, int npevs, | |
bd09d7b5 MH |
713 | int max_vls, const char *module, |
714 | struct strfilter *_filter, bool externs) | |
cf6eb489 | 715 | { |
ff741783 MH |
716 | int i, ret = 0; |
717 | struct debuginfo *dinfo; | |
cf6eb489 MH |
718 | |
719 | ret = init_vmlinux(); | |
720 | if (ret < 0) | |
721 | return ret; | |
722 | ||
ff741783 MH |
723 | dinfo = open_debuginfo(module); |
724 | if (!dinfo) { | |
725 | pr_warning("Failed to open debuginfo file.\n"); | |
726 | return -ENOENT; | |
727 | } | |
728 | ||
cf6eb489 MH |
729 | setup_pager(); |
730 | ||
ff741783 MH |
731 | for (i = 0; i < npevs && ret >= 0; i++) |
732 | ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter, | |
bd09d7b5 | 733 | externs); |
ff741783 MH |
734 | |
735 | debuginfo__delete(dinfo); | |
cf6eb489 MH |
736 | return ret; |
737 | } | |
738 | ||
89fe808a | 739 | #else /* !HAVE_DWARF_SUPPORT */ |
4b4da7f7 | 740 | |
0e60836b | 741 | static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp, |
469b9b88 | 742 | struct perf_probe_point *pp) |
4b4da7f7 | 743 | { |
469b9b88 MH |
744 | struct symbol *sym; |
745 | ||
746 | sym = __find_kernel_function_by_name(tp->symbol, NULL); | |
747 | if (!sym) { | |
748 | pr_err("Failed to find symbol %s in kernel.\n", tp->symbol); | |
749 | return -ENOENT; | |
750 | } | |
146a1439 | 751 | |
225466f1 | 752 | return convert_to_perf_probe_point(tp, pp); |
4b4da7f7 MH |
753 | } |
754 | ||
0e60836b | 755 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
1d037ca1 IT |
756 | struct probe_trace_event **tevs __maybe_unused, |
757 | int max_tevs __maybe_unused, const char *target) | |
4b4da7f7 | 758 | { |
146a1439 MH |
759 | if (perf_probe_event_need_dwarf(pev)) { |
760 | pr_warning("Debuginfo-analysis is not supported.\n"); | |
761 | return -ENOSYS; | |
762 | } | |
225466f1 | 763 | |
4b4da7f7 MH |
764 | return 0; |
765 | } | |
766 | ||
1d037ca1 IT |
767 | int show_line_range(struct line_range *lr __maybe_unused, |
768 | const char *module __maybe_unused) | |
4b4da7f7 | 769 | { |
146a1439 MH |
770 | pr_warning("Debuginfo-analysis is not supported.\n"); |
771 | return -ENOSYS; | |
4b4da7f7 MH |
772 | } |
773 | ||
1d037ca1 IT |
774 | int show_available_vars(struct perf_probe_event *pevs __maybe_unused, |
775 | int npevs __maybe_unused, int max_vls __maybe_unused, | |
776 | const char *module __maybe_unused, | |
777 | struct strfilter *filter __maybe_unused, | |
778 | bool externs __maybe_unused) | |
cf6eb489 MH |
779 | { |
780 | pr_warning("Debuginfo-analysis is not supported.\n"); | |
781 | return -ENOSYS; | |
782 | } | |
e0faa8d3 MH |
783 | #endif |
784 | ||
21dd9ae5 FBH |
785 | static int parse_line_num(char **ptr, int *val, const char *what) |
786 | { | |
787 | const char *start = *ptr; | |
788 | ||
789 | errno = 0; | |
790 | *val = strtol(*ptr, ptr, 0); | |
791 | if (errno || *ptr == start) { | |
792 | semantic_error("'%s' is not a valid number.\n", what); | |
793 | return -EINVAL; | |
794 | } | |
795 | return 0; | |
796 | } | |
797 | ||
9d95b580 FBH |
798 | /* |
799 | * Stuff 'lr' according to the line range described by 'arg'. | |
800 | * The line range syntax is described by: | |
801 | * | |
802 | * SRC[:SLN[+NUM|-ELN]] | |
e116dfa1 | 803 | * FNC[@SRC][:SLN[+NUM|-ELN]] |
9d95b580 | 804 | */ |
146a1439 | 805 | int parse_line_range_desc(const char *arg, struct line_range *lr) |
631c9def | 806 | { |
e116dfa1 | 807 | char *range, *file, *name = strdup(arg); |
21dd9ae5 FBH |
808 | int err; |
809 | ||
810 | if (!name) | |
811 | return -ENOMEM; | |
812 | ||
813 | lr->start = 0; | |
814 | lr->end = INT_MAX; | |
815 | ||
816 | range = strchr(name, ':'); | |
817 | if (range) { | |
818 | *range++ = '\0'; | |
819 | ||
820 | err = parse_line_num(&range, &lr->start, "start line"); | |
821 | if (err) | |
822 | goto err; | |
823 | ||
824 | if (*range == '+' || *range == '-') { | |
825 | const char c = *range++; | |
826 | ||
827 | err = parse_line_num(&range, &lr->end, "end line"); | |
828 | if (err) | |
829 | goto err; | |
830 | ||
831 | if (c == '+') { | |
832 | lr->end += lr->start; | |
833 | /* | |
834 | * Adjust the number of lines here. | |
835 | * If the number of lines == 1, the | |
836 | * the end of line should be equal to | |
837 | * the start of line. | |
838 | */ | |
839 | lr->end--; | |
840 | } | |
841 | } | |
9d95b580 | 842 | |
d3b63d7a | 843 | pr_debug("Line range is %d to %d\n", lr->start, lr->end); |
21dd9ae5 FBH |
844 | |
845 | err = -EINVAL; | |
d3b63d7a | 846 | if (lr->start > lr->end) { |
631c9def | 847 | semantic_error("Start line must be smaller" |
146a1439 | 848 | " than end line.\n"); |
21dd9ae5 | 849 | goto err; |
146a1439 | 850 | } |
21dd9ae5 FBH |
851 | if (*range != '\0') { |
852 | semantic_error("Tailing with invalid str '%s'.\n", range); | |
853 | goto err; | |
146a1439 | 854 | } |
d3b63d7a | 855 | } |
02b95dad | 856 | |
e116dfa1 MH |
857 | file = strchr(name, '@'); |
858 | if (file) { | |
859 | *file = '\0'; | |
860 | lr->file = strdup(++file); | |
861 | if (lr->file == NULL) { | |
862 | err = -ENOMEM; | |
863 | goto err; | |
864 | } | |
865 | lr->function = name; | |
866 | } else if (strchr(name, '.')) | |
21dd9ae5 | 867 | lr->file = name; |
631c9def | 868 | else |
21dd9ae5 | 869 | lr->function = name; |
146a1439 MH |
870 | |
871 | return 0; | |
21dd9ae5 FBH |
872 | err: |
873 | free(name); | |
874 | return err; | |
631c9def MH |
875 | } |
876 | ||
b7702a21 MH |
877 | /* Check the name is good for event/group */ |
878 | static bool check_event_name(const char *name) | |
879 | { | |
880 | if (!isalpha(*name) && *name != '_') | |
881 | return false; | |
882 | while (*++name != '\0') { | |
883 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') | |
884 | return false; | |
885 | } | |
886 | return true; | |
887 | } | |
888 | ||
50656eec | 889 | /* Parse probepoint definition. */ |
146a1439 | 890 | static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev) |
50656eec | 891 | { |
4235b045 | 892 | struct perf_probe_point *pp = &pev->point; |
50656eec MH |
893 | char *ptr, *tmp; |
894 | char c, nc = 0; | |
895 | /* | |
896 | * <Syntax> | |
2a9c8c36 MH |
897 | * perf probe [EVENT=]SRC[:LN|;PTN] |
898 | * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] | |
af663d75 MH |
899 | * |
900 | * TODO:Group name support | |
50656eec MH |
901 | */ |
902 | ||
2a9c8c36 MH |
903 | ptr = strpbrk(arg, ";=@+%"); |
904 | if (ptr && *ptr == '=') { /* Event name */ | |
af663d75 MH |
905 | *ptr = '\0'; |
906 | tmp = ptr + 1; | |
146a1439 MH |
907 | if (strchr(arg, ':')) { |
908 | semantic_error("Group name is not supported yet.\n"); | |
909 | return -ENOTSUP; | |
910 | } | |
911 | if (!check_event_name(arg)) { | |
b7702a21 | 912 | semantic_error("%s is bad for event name -it must " |
146a1439 MH |
913 | "follow C symbol-naming rule.\n", arg); |
914 | return -EINVAL; | |
915 | } | |
02b95dad MH |
916 | pev->event = strdup(arg); |
917 | if (pev->event == NULL) | |
918 | return -ENOMEM; | |
4235b045 | 919 | pev->group = NULL; |
af663d75 MH |
920 | arg = tmp; |
921 | } | |
922 | ||
2a9c8c36 | 923 | ptr = strpbrk(arg, ";:+@%"); |
50656eec MH |
924 | if (ptr) { |
925 | nc = *ptr; | |
926 | *ptr++ = '\0'; | |
927 | } | |
928 | ||
02b95dad MH |
929 | tmp = strdup(arg); |
930 | if (tmp == NULL) | |
931 | return -ENOMEM; | |
932 | ||
50656eec | 933 | /* Check arg is function or file and copy it */ |
02b95dad MH |
934 | if (strchr(tmp, '.')) /* File */ |
935 | pp->file = tmp; | |
50656eec | 936 | else /* Function */ |
02b95dad | 937 | pp->function = tmp; |
50656eec MH |
938 | |
939 | /* Parse other options */ | |
940 | while (ptr) { | |
941 | arg = ptr; | |
942 | c = nc; | |
2a9c8c36 | 943 | if (c == ';') { /* Lazy pattern must be the last part */ |
02b95dad MH |
944 | pp->lazy_line = strdup(arg); |
945 | if (pp->lazy_line == NULL) | |
946 | return -ENOMEM; | |
2a9c8c36 MH |
947 | break; |
948 | } | |
949 | ptr = strpbrk(arg, ";:+@%"); | |
50656eec MH |
950 | if (ptr) { |
951 | nc = *ptr; | |
952 | *ptr++ = '\0'; | |
953 | } | |
954 | switch (c) { | |
955 | case ':': /* Line number */ | |
956 | pp->line = strtoul(arg, &tmp, 0); | |
146a1439 | 957 | if (*tmp != '\0') { |
2a9c8c36 | 958 | semantic_error("There is non-digit char" |
146a1439 MH |
959 | " in line number.\n"); |
960 | return -EINVAL; | |
961 | } | |
50656eec MH |
962 | break; |
963 | case '+': /* Byte offset from a symbol */ | |
964 | pp->offset = strtoul(arg, &tmp, 0); | |
146a1439 | 965 | if (*tmp != '\0') { |
2a9c8c36 | 966 | semantic_error("There is non-digit character" |
146a1439 MH |
967 | " in offset.\n"); |
968 | return -EINVAL; | |
969 | } | |
50656eec MH |
970 | break; |
971 | case '@': /* File name */ | |
146a1439 MH |
972 | if (pp->file) { |
973 | semantic_error("SRC@SRC is not allowed.\n"); | |
974 | return -EINVAL; | |
975 | } | |
02b95dad MH |
976 | pp->file = strdup(arg); |
977 | if (pp->file == NULL) | |
978 | return -ENOMEM; | |
50656eec MH |
979 | break; |
980 | case '%': /* Probe places */ | |
981 | if (strcmp(arg, "return") == 0) { | |
982 | pp->retprobe = 1; | |
146a1439 MH |
983 | } else { /* Others not supported yet */ |
984 | semantic_error("%%%s is not supported.\n", arg); | |
985 | return -ENOTSUP; | |
986 | } | |
50656eec | 987 | break; |
146a1439 MH |
988 | default: /* Buggy case */ |
989 | pr_err("This program has a bug at %s:%d.\n", | |
990 | __FILE__, __LINE__); | |
991 | return -ENOTSUP; | |
50656eec MH |
992 | break; |
993 | } | |
994 | } | |
995 | ||
996 | /* Exclusion check */ | |
146a1439 | 997 | if (pp->lazy_line && pp->line) { |
0e43e5d2 MH |
998 | semantic_error("Lazy pattern can't be used with" |
999 | " line number.\n"); | |
146a1439 MH |
1000 | return -EINVAL; |
1001 | } | |
2a9c8c36 | 1002 | |
146a1439 | 1003 | if (pp->lazy_line && pp->offset) { |
0e43e5d2 | 1004 | semantic_error("Lazy pattern can't be used with offset.\n"); |
146a1439 MH |
1005 | return -EINVAL; |
1006 | } | |
2a9c8c36 | 1007 | |
146a1439 | 1008 | if (pp->line && pp->offset) { |
0e43e5d2 | 1009 | semantic_error("Offset can't be used with line number.\n"); |
146a1439 MH |
1010 | return -EINVAL; |
1011 | } | |
50656eec | 1012 | |
146a1439 | 1013 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { |
2a9c8c36 | 1014 | semantic_error("File always requires line number or " |
0e43e5d2 | 1015 | "lazy pattern.\n"); |
146a1439 MH |
1016 | return -EINVAL; |
1017 | } | |
50656eec | 1018 | |
146a1439 | 1019 | if (pp->offset && !pp->function) { |
0e43e5d2 | 1020 | semantic_error("Offset requires an entry function.\n"); |
146a1439 MH |
1021 | return -EINVAL; |
1022 | } | |
50656eec | 1023 | |
146a1439 | 1024 | if (pp->retprobe && !pp->function) { |
0e43e5d2 | 1025 | semantic_error("Return probe requires an entry function.\n"); |
146a1439 MH |
1026 | return -EINVAL; |
1027 | } | |
50656eec | 1028 | |
146a1439 | 1029 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { |
2a9c8c36 | 1030 | semantic_error("Offset/Line/Lazy pattern can't be used with " |
0e43e5d2 | 1031 | "return probe.\n"); |
146a1439 MH |
1032 | return -EINVAL; |
1033 | } | |
50656eec | 1034 | |
4235b045 | 1035 | pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", |
2a9c8c36 MH |
1036 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, |
1037 | pp->lazy_line); | |
146a1439 | 1038 | return 0; |
50656eec MH |
1039 | } |
1040 | ||
7df2f329 | 1041 | /* Parse perf-probe event argument */ |
146a1439 | 1042 | static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) |
7df2f329 | 1043 | { |
b2a3c12b | 1044 | char *tmp, *goodname; |
7df2f329 MH |
1045 | struct perf_probe_arg_field **fieldp; |
1046 | ||
1047 | pr_debug("parsing arg: %s into ", str); | |
1048 | ||
48481938 MH |
1049 | tmp = strchr(str, '='); |
1050 | if (tmp) { | |
02b95dad MH |
1051 | arg->name = strndup(str, tmp - str); |
1052 | if (arg->name == NULL) | |
1053 | return -ENOMEM; | |
11a1ca35 | 1054 | pr_debug("name:%s ", arg->name); |
48481938 MH |
1055 | str = tmp + 1; |
1056 | } | |
1057 | ||
11a1ca35 MH |
1058 | tmp = strchr(str, ':'); |
1059 | if (tmp) { /* Type setting */ | |
1060 | *tmp = '\0'; | |
02b95dad MH |
1061 | arg->type = strdup(tmp + 1); |
1062 | if (arg->type == NULL) | |
1063 | return -ENOMEM; | |
11a1ca35 MH |
1064 | pr_debug("type:%s ", arg->type); |
1065 | } | |
1066 | ||
b2a3c12b | 1067 | tmp = strpbrk(str, "-.["); |
7df2f329 MH |
1068 | if (!is_c_varname(str) || !tmp) { |
1069 | /* A variable, register, symbol or special value */ | |
02b95dad MH |
1070 | arg->var = strdup(str); |
1071 | if (arg->var == NULL) | |
1072 | return -ENOMEM; | |
48481938 | 1073 | pr_debug("%s\n", arg->var); |
146a1439 | 1074 | return 0; |
7df2f329 MH |
1075 | } |
1076 | ||
b2a3c12b | 1077 | /* Structure fields or array element */ |
02b95dad MH |
1078 | arg->var = strndup(str, tmp - str); |
1079 | if (arg->var == NULL) | |
1080 | return -ENOMEM; | |
b2a3c12b | 1081 | goodname = arg->var; |
48481938 | 1082 | pr_debug("%s, ", arg->var); |
7df2f329 MH |
1083 | fieldp = &arg->field; |
1084 | ||
1085 | do { | |
e334016f MH |
1086 | *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); |
1087 | if (*fieldp == NULL) | |
1088 | return -ENOMEM; | |
b2a3c12b MH |
1089 | if (*tmp == '[') { /* Array */ |
1090 | str = tmp; | |
1091 | (*fieldp)->index = strtol(str + 1, &tmp, 0); | |
7df2f329 | 1092 | (*fieldp)->ref = true; |
b2a3c12b MH |
1093 | if (*tmp != ']' || tmp == str + 1) { |
1094 | semantic_error("Array index must be a" | |
1095 | " number.\n"); | |
1096 | return -EINVAL; | |
1097 | } | |
1098 | tmp++; | |
1099 | if (*tmp == '\0') | |
1100 | tmp = NULL; | |
1101 | } else { /* Structure */ | |
1102 | if (*tmp == '.') { | |
1103 | str = tmp + 1; | |
1104 | (*fieldp)->ref = false; | |
1105 | } else if (tmp[1] == '>') { | |
1106 | str = tmp + 2; | |
1107 | (*fieldp)->ref = true; | |
1108 | } else { | |
1109 | semantic_error("Argument parse error: %s\n", | |
1110 | str); | |
1111 | return -EINVAL; | |
1112 | } | |
1113 | tmp = strpbrk(str, "-.["); | |
146a1439 | 1114 | } |
7df2f329 | 1115 | if (tmp) { |
02b95dad MH |
1116 | (*fieldp)->name = strndup(str, tmp - str); |
1117 | if ((*fieldp)->name == NULL) | |
1118 | return -ENOMEM; | |
b2a3c12b MH |
1119 | if (*str != '[') |
1120 | goodname = (*fieldp)->name; | |
7df2f329 MH |
1121 | pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); |
1122 | fieldp = &(*fieldp)->next; | |
1123 | } | |
1124 | } while (tmp); | |
02b95dad MH |
1125 | (*fieldp)->name = strdup(str); |
1126 | if ((*fieldp)->name == NULL) | |
1127 | return -ENOMEM; | |
b2a3c12b MH |
1128 | if (*str != '[') |
1129 | goodname = (*fieldp)->name; | |
7df2f329 | 1130 | pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); |
df0faf4b | 1131 | |
b2a3c12b | 1132 | /* If no name is specified, set the last field name (not array index)*/ |
02b95dad | 1133 | if (!arg->name) { |
b2a3c12b | 1134 | arg->name = strdup(goodname); |
02b95dad MH |
1135 | if (arg->name == NULL) |
1136 | return -ENOMEM; | |
1137 | } | |
146a1439 | 1138 | return 0; |
7df2f329 MH |
1139 | } |
1140 | ||
4235b045 | 1141 | /* Parse perf-probe event command */ |
146a1439 | 1142 | int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) |
50656eec | 1143 | { |
e1c01d61 | 1144 | char **argv; |
146a1439 | 1145 | int argc, i, ret = 0; |
fac13fd5 | 1146 | |
4235b045 | 1147 | argv = argv_split(cmd, &argc); |
146a1439 MH |
1148 | if (!argv) { |
1149 | pr_debug("Failed to split arguments.\n"); | |
1150 | return -ENOMEM; | |
1151 | } | |
1152 | if (argc - 1 > MAX_PROBE_ARGS) { | |
1153 | semantic_error("Too many probe arguments (%d).\n", argc - 1); | |
1154 | ret = -ERANGE; | |
1155 | goto out; | |
1156 | } | |
50656eec | 1157 | /* Parse probe point */ |
146a1439 MH |
1158 | ret = parse_perf_probe_point(argv[0], pev); |
1159 | if (ret < 0) | |
1160 | goto out; | |
50656eec | 1161 | |
e1c01d61 | 1162 | /* Copy arguments and ensure return probe has no C argument */ |
4235b045 | 1163 | pev->nargs = argc - 1; |
e334016f MH |
1164 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
1165 | if (pev->args == NULL) { | |
1166 | ret = -ENOMEM; | |
1167 | goto out; | |
1168 | } | |
146a1439 MH |
1169 | for (i = 0; i < pev->nargs && ret >= 0; i++) { |
1170 | ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); | |
1171 | if (ret >= 0 && | |
1172 | is_c_varname(pev->args[i].var) && pev->point.retprobe) { | |
4235b045 | 1173 | semantic_error("You can't specify local variable for" |
146a1439 MH |
1174 | " kretprobe.\n"); |
1175 | ret = -EINVAL; | |
1176 | } | |
e1c01d61 | 1177 | } |
146a1439 | 1178 | out: |
e1c01d61 | 1179 | argv_free(argv); |
146a1439 MH |
1180 | |
1181 | return ret; | |
50656eec MH |
1182 | } |
1183 | ||
4235b045 MH |
1184 | /* Return true if this perf_probe_event requires debuginfo */ |
1185 | bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) | |
1186 | { | |
1187 | int i; | |
1188 | ||
1189 | if (pev->point.file || pev->point.line || pev->point.lazy_line) | |
1190 | return true; | |
1191 | ||
1192 | for (i = 0; i < pev->nargs; i++) | |
48481938 | 1193 | if (is_c_varname(pev->args[i].var)) |
4235b045 MH |
1194 | return true; |
1195 | ||
1196 | return false; | |
1197 | } | |
1198 | ||
0e60836b SD |
1199 | /* Parse probe_events event into struct probe_point */ |
1200 | static int parse_probe_trace_command(const char *cmd, | |
190b57fc | 1201 | struct probe_trace_event *tev) |
4de189fe | 1202 | { |
0e60836b | 1203 | struct probe_trace_point *tp = &tev->point; |
4de189fe MH |
1204 | char pr; |
1205 | char *p; | |
bcbd0040 | 1206 | char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str; |
4de189fe MH |
1207 | int ret, i, argc; |
1208 | char **argv; | |
1209 | ||
0e60836b | 1210 | pr_debug("Parsing probe_events: %s\n", cmd); |
4235b045 | 1211 | argv = argv_split(cmd, &argc); |
146a1439 MH |
1212 | if (!argv) { |
1213 | pr_debug("Failed to split arguments.\n"); | |
1214 | return -ENOMEM; | |
1215 | } | |
1216 | if (argc < 2) { | |
1217 | semantic_error("Too few probe arguments.\n"); | |
1218 | ret = -ERANGE; | |
1219 | goto out; | |
1220 | } | |
4de189fe MH |
1221 | |
1222 | /* Scan event and group name. */ | |
bcbd0040 IT |
1223 | argv0_str = strdup(argv[0]); |
1224 | if (argv0_str == NULL) { | |
1225 | ret = -ENOMEM; | |
1226 | goto out; | |
1227 | } | |
1228 | fmt1_str = strtok_r(argv0_str, ":", &fmt); | |
1229 | fmt2_str = strtok_r(NULL, "/", &fmt); | |
1230 | fmt3_str = strtok_r(NULL, " \t", &fmt); | |
1231 | if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL | |
1232 | || fmt3_str == NULL) { | |
146a1439 MH |
1233 | semantic_error("Failed to parse event name: %s\n", argv[0]); |
1234 | ret = -EINVAL; | |
1235 | goto out; | |
1236 | } | |
bcbd0040 IT |
1237 | pr = fmt1_str[0]; |
1238 | tev->group = strdup(fmt2_str); | |
1239 | tev->event = strdup(fmt3_str); | |
1240 | if (tev->group == NULL || tev->event == NULL) { | |
1241 | ret = -ENOMEM; | |
1242 | goto out; | |
1243 | } | |
4235b045 | 1244 | pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); |
4de189fe | 1245 | |
4235b045 | 1246 | tp->retprobe = (pr == 'r'); |
4de189fe | 1247 | |
190b57fc MH |
1248 | /* Scan module name(if there), function name and offset */ |
1249 | p = strchr(argv[1], ':'); | |
1250 | if (p) { | |
1251 | tp->module = strndup(argv[1], p - argv[1]); | |
1252 | p++; | |
1253 | } else | |
1254 | p = argv[1]; | |
bcbd0040 IT |
1255 | fmt1_str = strtok_r(p, "+", &fmt); |
1256 | tp->symbol = strdup(fmt1_str); | |
1257 | if (tp->symbol == NULL) { | |
1258 | ret = -ENOMEM; | |
1259 | goto out; | |
1260 | } | |
1261 | fmt2_str = strtok_r(NULL, "", &fmt); | |
1262 | if (fmt2_str == NULL) | |
4235b045 | 1263 | tp->offset = 0; |
bcbd0040 IT |
1264 | else |
1265 | tp->offset = strtoul(fmt2_str, NULL, 10); | |
4de189fe | 1266 | |
4235b045 | 1267 | tev->nargs = argc - 2; |
0e60836b | 1268 | tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); |
e334016f MH |
1269 | if (tev->args == NULL) { |
1270 | ret = -ENOMEM; | |
1271 | goto out; | |
1272 | } | |
4235b045 | 1273 | for (i = 0; i < tev->nargs; i++) { |
4de189fe MH |
1274 | p = strchr(argv[i + 2], '='); |
1275 | if (p) /* We don't need which register is assigned. */ | |
4235b045 MH |
1276 | *p++ = '\0'; |
1277 | else | |
1278 | p = argv[i + 2]; | |
02b95dad | 1279 | tev->args[i].name = strdup(argv[i + 2]); |
4235b045 | 1280 | /* TODO: parse regs and offset */ |
02b95dad MH |
1281 | tev->args[i].value = strdup(p); |
1282 | if (tev->args[i].name == NULL || tev->args[i].value == NULL) { | |
1283 | ret = -ENOMEM; | |
1284 | goto out; | |
1285 | } | |
4de189fe | 1286 | } |
146a1439 MH |
1287 | ret = 0; |
1288 | out: | |
bcbd0040 | 1289 | free(argv0_str); |
4de189fe | 1290 | argv_free(argv); |
146a1439 | 1291 | return ret; |
4de189fe MH |
1292 | } |
1293 | ||
7df2f329 MH |
1294 | /* Compose only probe arg */ |
1295 | int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len) | |
1296 | { | |
1297 | struct perf_probe_arg_field *field = pa->field; | |
1298 | int ret; | |
1299 | char *tmp = buf; | |
1300 | ||
48481938 MH |
1301 | if (pa->name && pa->var) |
1302 | ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var); | |
1303 | else | |
1304 | ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var); | |
7df2f329 MH |
1305 | if (ret <= 0) |
1306 | goto error; | |
1307 | tmp += ret; | |
1308 | len -= ret; | |
1309 | ||
1310 | while (field) { | |
b2a3c12b MH |
1311 | if (field->name[0] == '[') |
1312 | ret = e_snprintf(tmp, len, "%s", field->name); | |
1313 | else | |
1314 | ret = e_snprintf(tmp, len, "%s%s", | |
1315 | field->ref ? "->" : ".", field->name); | |
7df2f329 MH |
1316 | if (ret <= 0) |
1317 | goto error; | |
1318 | tmp += ret; | |
1319 | len -= ret; | |
1320 | field = field->next; | |
1321 | } | |
11a1ca35 MH |
1322 | |
1323 | if (pa->type) { | |
1324 | ret = e_snprintf(tmp, len, ":%s", pa->type); | |
1325 | if (ret <= 0) | |
1326 | goto error; | |
1327 | tmp += ret; | |
1328 | len -= ret; | |
1329 | } | |
1330 | ||
7df2f329 MH |
1331 | return tmp - buf; |
1332 | error: | |
0e43e5d2 | 1333 | pr_debug("Failed to synthesize perf probe argument: %s\n", |
146a1439 MH |
1334 | strerror(-ret)); |
1335 | return ret; | |
7df2f329 MH |
1336 | } |
1337 | ||
4235b045 MH |
1338 | /* Compose only probe point (not argument) */ |
1339 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp) | |
4de189fe | 1340 | { |
fb1587d8 MH |
1341 | char *buf, *tmp; |
1342 | char offs[32] = "", line[32] = "", file[32] = ""; | |
1343 | int ret, len; | |
4de189fe | 1344 | |
e334016f MH |
1345 | buf = zalloc(MAX_CMDLEN); |
1346 | if (buf == NULL) { | |
1347 | ret = -ENOMEM; | |
1348 | goto error; | |
1349 | } | |
4de189fe | 1350 | if (pp->offset) { |
fb1587d8 | 1351 | ret = e_snprintf(offs, 32, "+%lu", pp->offset); |
4de189fe MH |
1352 | if (ret <= 0) |
1353 | goto error; | |
1354 | } | |
1355 | if (pp->line) { | |
fb1587d8 MH |
1356 | ret = e_snprintf(line, 32, ":%d", pp->line); |
1357 | if (ret <= 0) | |
1358 | goto error; | |
1359 | } | |
1360 | if (pp->file) { | |
32ae2ade FBH |
1361 | tmp = pp->file; |
1362 | len = strlen(tmp); | |
1363 | if (len > 30) { | |
1364 | tmp = strchr(pp->file + len - 30, '/'); | |
1365 | tmp = tmp ? tmp + 1 : pp->file + len - 30; | |
1366 | } | |
1367 | ret = e_snprintf(file, 32, "@%s", tmp); | |
4de189fe MH |
1368 | if (ret <= 0) |
1369 | goto error; | |
1370 | } | |
1371 | ||
1372 | if (pp->function) | |
fb1587d8 MH |
1373 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function, |
1374 | offs, pp->retprobe ? "%return" : "", line, | |
1375 | file); | |
4de189fe | 1376 | else |
fb1587d8 | 1377 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line); |
4235b045 MH |
1378 | if (ret <= 0) |
1379 | goto error; | |
1380 | ||
1381 | return buf; | |
7ef17aaf | 1382 | error: |
0e43e5d2 | 1383 | pr_debug("Failed to synthesize perf probe point: %s\n", |
146a1439 | 1384 | strerror(-ret)); |
e334016f MH |
1385 | if (buf) |
1386 | free(buf); | |
146a1439 | 1387 | return NULL; |
7ef17aaf MH |
1388 | } |
1389 | ||
4235b045 MH |
1390 | #if 0 |
1391 | char *synthesize_perf_probe_command(struct perf_probe_event *pev) | |
7ef17aaf MH |
1392 | { |
1393 | char *buf; | |
1394 | int i, len, ret; | |
1395 | ||
4235b045 MH |
1396 | buf = synthesize_perf_probe_point(&pev->point); |
1397 | if (!buf) | |
1398 | return NULL; | |
4de189fe | 1399 | |
4235b045 MH |
1400 | len = strlen(buf); |
1401 | for (i = 0; i < pev->nargs; i++) { | |
4de189fe | 1402 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
4235b045 MH |
1403 | pev->args[i].name); |
1404 | if (ret <= 0) { | |
1405 | free(buf); | |
1406 | return NULL; | |
1407 | } | |
4de189fe MH |
1408 | len += ret; |
1409 | } | |
4de189fe | 1410 | |
4235b045 MH |
1411 | return buf; |
1412 | } | |
1413 | #endif | |
1414 | ||
0e60836b | 1415 | static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref, |
4235b045 MH |
1416 | char **buf, size_t *buflen, |
1417 | int depth) | |
1418 | { | |
1419 | int ret; | |
1420 | if (ref->next) { | |
0e60836b | 1421 | depth = __synthesize_probe_trace_arg_ref(ref->next, buf, |
4235b045 MH |
1422 | buflen, depth + 1); |
1423 | if (depth < 0) | |
1424 | goto out; | |
1425 | } | |
1426 | ||
1427 | ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset); | |
1428 | if (ret < 0) | |
1429 | depth = ret; | |
1430 | else { | |
1431 | *buf += ret; | |
1432 | *buflen -= ret; | |
1433 | } | |
1434 | out: | |
1435 | return depth; | |
4de189fe | 1436 | |
4de189fe MH |
1437 | } |
1438 | ||
0e60836b | 1439 | static int synthesize_probe_trace_arg(struct probe_trace_arg *arg, |
4235b045 | 1440 | char *buf, size_t buflen) |
50656eec | 1441 | { |
0e60836b | 1442 | struct probe_trace_arg_ref *ref = arg->ref; |
4235b045 MH |
1443 | int ret, depth = 0; |
1444 | char *tmp = buf; | |
1445 | ||
1446 | /* Argument name or separator */ | |
1447 | if (arg->name) | |
1448 | ret = e_snprintf(buf, buflen, " %s=", arg->name); | |
1449 | else | |
1450 | ret = e_snprintf(buf, buflen, " "); | |
1451 | if (ret < 0) | |
1452 | return ret; | |
1453 | buf += ret; | |
1454 | buflen -= ret; | |
1455 | ||
b7dcb857 MH |
1456 | /* Special case: @XXX */ |
1457 | if (arg->value[0] == '@' && arg->ref) | |
1458 | ref = ref->next; | |
1459 | ||
4235b045 | 1460 | /* Dereferencing arguments */ |
b7dcb857 | 1461 | if (ref) { |
0e60836b | 1462 | depth = __synthesize_probe_trace_arg_ref(ref, &buf, |
4235b045 MH |
1463 | &buflen, 1); |
1464 | if (depth < 0) | |
1465 | return depth; | |
1466 | } | |
1467 | ||
1468 | /* Print argument value */ | |
b7dcb857 MH |
1469 | if (arg->value[0] == '@' && arg->ref) |
1470 | ret = e_snprintf(buf, buflen, "%s%+ld", arg->value, | |
1471 | arg->ref->offset); | |
1472 | else | |
1473 | ret = e_snprintf(buf, buflen, "%s", arg->value); | |
4235b045 MH |
1474 | if (ret < 0) |
1475 | return ret; | |
1476 | buf += ret; | |
1477 | buflen -= ret; | |
1478 | ||
1479 | /* Closing */ | |
1480 | while (depth--) { | |
1481 | ret = e_snprintf(buf, buflen, ")"); | |
1482 | if (ret < 0) | |
1483 | return ret; | |
1484 | buf += ret; | |
1485 | buflen -= ret; | |
1486 | } | |
4984912e MH |
1487 | /* Print argument type */ |
1488 | if (arg->type) { | |
1489 | ret = e_snprintf(buf, buflen, ":%s", arg->type); | |
1490 | if (ret <= 0) | |
1491 | return ret; | |
1492 | buf += ret; | |
1493 | } | |
4235b045 MH |
1494 | |
1495 | return buf - tmp; | |
1496 | } | |
1497 | ||
0e60836b | 1498 | char *synthesize_probe_trace_command(struct probe_trace_event *tev) |
4235b045 | 1499 | { |
0e60836b | 1500 | struct probe_trace_point *tp = &tev->point; |
50656eec MH |
1501 | char *buf; |
1502 | int i, len, ret; | |
1503 | ||
e334016f MH |
1504 | buf = zalloc(MAX_CMDLEN); |
1505 | if (buf == NULL) | |
1506 | return NULL; | |
1507 | ||
225466f1 SD |
1508 | if (tev->uprobes) |
1509 | len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s:%s", | |
1510 | tp->retprobe ? 'r' : 'p', | |
1511 | tev->group, tev->event, | |
1512 | tp->module, tp->symbol); | |
1513 | else | |
1514 | len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu", | |
1515 | tp->retprobe ? 'r' : 'p', | |
1516 | tev->group, tev->event, | |
1517 | tp->module ?: "", tp->module ? ":" : "", | |
1518 | tp->symbol, tp->offset); | |
1519 | ||
4235b045 | 1520 | if (len <= 0) |
50656eec | 1521 | goto error; |
50656eec | 1522 | |
4235b045 | 1523 | for (i = 0; i < tev->nargs; i++) { |
0e60836b | 1524 | ret = synthesize_probe_trace_arg(&tev->args[i], buf + len, |
4235b045 | 1525 | MAX_CMDLEN - len); |
4de189fe | 1526 | if (ret <= 0) |
50656eec MH |
1527 | goto error; |
1528 | len += ret; | |
1529 | } | |
50656eec | 1530 | |
4235b045 | 1531 | return buf; |
50656eec | 1532 | error: |
4235b045 MH |
1533 | free(buf); |
1534 | return NULL; | |
1535 | } | |
50656eec | 1536 | |
0e60836b | 1537 | static int convert_to_perf_probe_event(struct probe_trace_event *tev, |
225466f1 | 1538 | struct perf_probe_event *pev, bool is_kprobe) |
4235b045 | 1539 | { |
02b95dad | 1540 | char buf[64] = ""; |
146a1439 | 1541 | int i, ret; |
4235b045 | 1542 | |
4b4da7f7 | 1543 | /* Convert event/group name */ |
02b95dad MH |
1544 | pev->event = strdup(tev->event); |
1545 | pev->group = strdup(tev->group); | |
1546 | if (pev->event == NULL || pev->group == NULL) | |
1547 | return -ENOMEM; | |
fb1587d8 | 1548 | |
4b4da7f7 | 1549 | /* Convert trace_point to probe_point */ |
225466f1 SD |
1550 | if (is_kprobe) |
1551 | ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point); | |
1552 | else | |
1553 | ret = convert_to_perf_probe_point(&tev->point, &pev->point); | |
1554 | ||
146a1439 MH |
1555 | if (ret < 0) |
1556 | return ret; | |
4b4da7f7 | 1557 | |
4235b045 MH |
1558 | /* Convert trace_arg to probe_arg */ |
1559 | pev->nargs = tev->nargs; | |
e334016f MH |
1560 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
1561 | if (pev->args == NULL) | |
1562 | return -ENOMEM; | |
02b95dad | 1563 | for (i = 0; i < tev->nargs && ret >= 0; i++) { |
4235b045 | 1564 | if (tev->args[i].name) |
02b95dad | 1565 | pev->args[i].name = strdup(tev->args[i].name); |
4235b045 | 1566 | else { |
0e60836b | 1567 | ret = synthesize_probe_trace_arg(&tev->args[i], |
146a1439 | 1568 | buf, 64); |
02b95dad | 1569 | pev->args[i].name = strdup(buf); |
4235b045 | 1570 | } |
02b95dad MH |
1571 | if (pev->args[i].name == NULL && ret >= 0) |
1572 | ret = -ENOMEM; | |
1573 | } | |
146a1439 MH |
1574 | |
1575 | if (ret < 0) | |
1576 | clear_perf_probe_event(pev); | |
1577 | ||
1578 | return ret; | |
4235b045 MH |
1579 | } |
1580 | ||
1581 | void clear_perf_probe_event(struct perf_probe_event *pev) | |
1582 | { | |
1583 | struct perf_probe_point *pp = &pev->point; | |
7df2f329 | 1584 | struct perf_probe_arg_field *field, *next; |
4235b045 MH |
1585 | int i; |
1586 | ||
1587 | if (pev->event) | |
1588 | free(pev->event); | |
1589 | if (pev->group) | |
1590 | free(pev->group); | |
1591 | if (pp->file) | |
1592 | free(pp->file); | |
1593 | if (pp->function) | |
1594 | free(pp->function); | |
1595 | if (pp->lazy_line) | |
1596 | free(pp->lazy_line); | |
7df2f329 | 1597 | for (i = 0; i < pev->nargs; i++) { |
4235b045 MH |
1598 | if (pev->args[i].name) |
1599 | free(pev->args[i].name); | |
48481938 MH |
1600 | if (pev->args[i].var) |
1601 | free(pev->args[i].var); | |
11a1ca35 MH |
1602 | if (pev->args[i].type) |
1603 | free(pev->args[i].type); | |
7df2f329 MH |
1604 | field = pev->args[i].field; |
1605 | while (field) { | |
1606 | next = field->next; | |
1607 | if (field->name) | |
1608 | free(field->name); | |
1609 | free(field); | |
1610 | field = next; | |
1611 | } | |
1612 | } | |
4235b045 MH |
1613 | if (pev->args) |
1614 | free(pev->args); | |
1615 | memset(pev, 0, sizeof(*pev)); | |
1616 | } | |
1617 | ||
0e60836b | 1618 | static void clear_probe_trace_event(struct probe_trace_event *tev) |
4235b045 | 1619 | { |
0e60836b | 1620 | struct probe_trace_arg_ref *ref, *next; |
4235b045 MH |
1621 | int i; |
1622 | ||
1623 | if (tev->event) | |
1624 | free(tev->event); | |
1625 | if (tev->group) | |
1626 | free(tev->group); | |
1627 | if (tev->point.symbol) | |
1628 | free(tev->point.symbol); | |
190b57fc MH |
1629 | if (tev->point.module) |
1630 | free(tev->point.module); | |
4235b045 MH |
1631 | for (i = 0; i < tev->nargs; i++) { |
1632 | if (tev->args[i].name) | |
1633 | free(tev->args[i].name); | |
1634 | if (tev->args[i].value) | |
1635 | free(tev->args[i].value); | |
4984912e MH |
1636 | if (tev->args[i].type) |
1637 | free(tev->args[i].type); | |
4235b045 MH |
1638 | ref = tev->args[i].ref; |
1639 | while (ref) { | |
1640 | next = ref->next; | |
1641 | free(ref); | |
1642 | ref = next; | |
1643 | } | |
1644 | } | |
1645 | if (tev->args) | |
1646 | free(tev->args); | |
1647 | memset(tev, 0, sizeof(*tev)); | |
50656eec MH |
1648 | } |
1649 | ||
225466f1 SD |
1650 | static void print_warn_msg(const char *file, bool is_kprobe) |
1651 | { | |
1652 | ||
1653 | if (errno == ENOENT) { | |
1654 | const char *config; | |
1655 | ||
1656 | if (!is_kprobe) | |
1657 | config = "CONFIG_UPROBE_EVENTS"; | |
1658 | else | |
1659 | config = "CONFIG_KPROBE_EVENTS"; | |
1660 | ||
1661 | pr_warning("%s file does not exist - please rebuild kernel" | |
1662 | " with %s.\n", file, config); | |
1663 | } else | |
1664 | pr_warning("Failed to open %s file: %s\n", file, | |
1665 | strerror(errno)); | |
1666 | } | |
1667 | ||
1668 | static int open_probe_events(const char *trace_file, bool readwrite, | |
1669 | bool is_kprobe) | |
4de189fe MH |
1670 | { |
1671 | char buf[PATH_MAX]; | |
7ca5989d | 1672 | const char *__debugfs; |
4de189fe MH |
1673 | int ret; |
1674 | ||
7ca5989d MH |
1675 | __debugfs = debugfs_find_mountpoint(); |
1676 | if (__debugfs == NULL) { | |
1677 | pr_warning("Debugfs is not mounted.\n"); | |
1678 | return -ENOENT; | |
1679 | } | |
1680 | ||
225466f1 | 1681 | ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file); |
146a1439 | 1682 | if (ret >= 0) { |
7ca5989d | 1683 | pr_debug("Opening %s write=%d\n", buf, readwrite); |
146a1439 MH |
1684 | if (readwrite && !probe_event_dry_run) |
1685 | ret = open(buf, O_RDWR, O_APPEND); | |
1686 | else | |
1687 | ret = open(buf, O_RDONLY, 0); | |
f4d7da49 | 1688 | |
225466f1 SD |
1689 | if (ret < 0) |
1690 | print_warn_msg(buf, is_kprobe); | |
4de189fe MH |
1691 | } |
1692 | return ret; | |
1693 | } | |
1694 | ||
225466f1 SD |
1695 | static int open_kprobe_events(bool readwrite) |
1696 | { | |
1697 | return open_probe_events("tracing/kprobe_events", readwrite, true); | |
1698 | } | |
1699 | ||
1700 | static int open_uprobe_events(bool readwrite) | |
1701 | { | |
1702 | return open_probe_events("tracing/uprobe_events", readwrite, false); | |
1703 | } | |
1704 | ||
1705 | /* Get raw string list of current kprobe_events or uprobe_events */ | |
0e60836b | 1706 | static struct strlist *get_probe_trace_command_rawlist(int fd) |
4de189fe MH |
1707 | { |
1708 | int ret, idx; | |
1709 | FILE *fp; | |
1710 | char buf[MAX_CMDLEN]; | |
1711 | char *p; | |
1712 | struct strlist *sl; | |
1713 | ||
1714 | sl = strlist__new(true, NULL); | |
1715 | ||
1716 | fp = fdopen(dup(fd), "r"); | |
1717 | while (!feof(fp)) { | |
1718 | p = fgets(buf, MAX_CMDLEN, fp); | |
1719 | if (!p) | |
1720 | break; | |
1721 | ||
1722 | idx = strlen(p) - 1; | |
1723 | if (p[idx] == '\n') | |
1724 | p[idx] = '\0'; | |
1725 | ret = strlist__add(sl, buf); | |
146a1439 MH |
1726 | if (ret < 0) { |
1727 | pr_debug("strlist__add failed: %s\n", strerror(-ret)); | |
1728 | strlist__delete(sl); | |
1729 | return NULL; | |
1730 | } | |
4de189fe MH |
1731 | } |
1732 | fclose(fp); | |
1733 | ||
1734 | return sl; | |
1735 | } | |
1736 | ||
278498d4 | 1737 | /* Show an event */ |
146a1439 | 1738 | static int show_perf_probe_event(struct perf_probe_event *pev) |
278498d4 | 1739 | { |
7e990a51 | 1740 | int i, ret; |
278498d4 | 1741 | char buf[128]; |
4235b045 | 1742 | char *place; |
278498d4 | 1743 | |
4235b045 MH |
1744 | /* Synthesize only event probe point */ |
1745 | place = synthesize_perf_probe_point(&pev->point); | |
146a1439 MH |
1746 | if (!place) |
1747 | return -EINVAL; | |
4235b045 MH |
1748 | |
1749 | ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event); | |
7e990a51 | 1750 | if (ret < 0) |
146a1439 MH |
1751 | return ret; |
1752 | ||
fb1587d8 | 1753 | printf(" %-20s (on %s", buf, place); |
278498d4 | 1754 | |
4235b045 | 1755 | if (pev->nargs > 0) { |
278498d4 | 1756 | printf(" with"); |
7df2f329 | 1757 | for (i = 0; i < pev->nargs; i++) { |
146a1439 MH |
1758 | ret = synthesize_perf_probe_arg(&pev->args[i], |
1759 | buf, 128); | |
1760 | if (ret < 0) | |
1761 | break; | |
7df2f329 MH |
1762 | printf(" %s", buf); |
1763 | } | |
278498d4 MH |
1764 | } |
1765 | printf(")\n"); | |
4235b045 | 1766 | free(place); |
146a1439 | 1767 | return ret; |
278498d4 MH |
1768 | } |
1769 | ||
225466f1 | 1770 | static int __show_perf_probe_events(int fd, bool is_kprobe) |
4de189fe | 1771 | { |
225466f1 | 1772 | int ret = 0; |
0e60836b | 1773 | struct probe_trace_event tev; |
4235b045 | 1774 | struct perf_probe_event pev; |
4de189fe MH |
1775 | struct strlist *rawlist; |
1776 | struct str_node *ent; | |
1777 | ||
4235b045 MH |
1778 | memset(&tev, 0, sizeof(tev)); |
1779 | memset(&pev, 0, sizeof(pev)); | |
72041334 | 1780 | |
0e60836b | 1781 | rawlist = get_probe_trace_command_rawlist(fd); |
146a1439 MH |
1782 | if (!rawlist) |
1783 | return -ENOENT; | |
4de189fe | 1784 | |
adf365f4 | 1785 | strlist__for_each(ent, rawlist) { |
0e60836b | 1786 | ret = parse_probe_trace_command(ent->s, &tev); |
146a1439 | 1787 | if (ret >= 0) { |
225466f1 SD |
1788 | ret = convert_to_perf_probe_event(&tev, &pev, |
1789 | is_kprobe); | |
146a1439 MH |
1790 | if (ret >= 0) |
1791 | ret = show_perf_probe_event(&pev); | |
1792 | } | |
4235b045 | 1793 | clear_perf_probe_event(&pev); |
0e60836b | 1794 | clear_probe_trace_event(&tev); |
146a1439 MH |
1795 | if (ret < 0) |
1796 | break; | |
4de189fe | 1797 | } |
4de189fe | 1798 | strlist__delete(rawlist); |
146a1439 MH |
1799 | |
1800 | return ret; | |
4de189fe MH |
1801 | } |
1802 | ||
225466f1 SD |
1803 | /* List up current perf-probe events */ |
1804 | int show_perf_probe_events(void) | |
1805 | { | |
1806 | int fd, ret; | |
1807 | ||
1808 | setup_pager(); | |
1809 | fd = open_kprobe_events(false); | |
1810 | ||
1811 | if (fd < 0) | |
1812 | return fd; | |
1813 | ||
1814 | ret = init_vmlinux(); | |
1815 | if (ret < 0) | |
1816 | return ret; | |
1817 | ||
1818 | ret = __show_perf_probe_events(fd, true); | |
1819 | close(fd); | |
1820 | ||
1821 | fd = open_uprobe_events(false); | |
1822 | if (fd >= 0) { | |
1823 | ret = __show_perf_probe_events(fd, false); | |
1824 | close(fd); | |
1825 | } | |
1826 | ||
1827 | return ret; | |
1828 | } | |
1829 | ||
b498ce1f | 1830 | /* Get current perf-probe event names */ |
0e60836b | 1831 | static struct strlist *get_probe_trace_event_names(int fd, bool include_group) |
b498ce1f | 1832 | { |
fa28244d | 1833 | char buf[128]; |
b498ce1f MH |
1834 | struct strlist *sl, *rawlist; |
1835 | struct str_node *ent; | |
0e60836b | 1836 | struct probe_trace_event tev; |
146a1439 | 1837 | int ret = 0; |
b498ce1f | 1838 | |
4235b045 | 1839 | memset(&tev, 0, sizeof(tev)); |
0e60836b | 1840 | rawlist = get_probe_trace_command_rawlist(fd); |
e1d2017b | 1841 | sl = strlist__new(true, NULL); |
adf365f4 | 1842 | strlist__for_each(ent, rawlist) { |
0e60836b | 1843 | ret = parse_probe_trace_command(ent->s, &tev); |
146a1439 MH |
1844 | if (ret < 0) |
1845 | break; | |
fa28244d | 1846 | if (include_group) { |
146a1439 MH |
1847 | ret = e_snprintf(buf, 128, "%s:%s", tev.group, |
1848 | tev.event); | |
1849 | if (ret >= 0) | |
1850 | ret = strlist__add(sl, buf); | |
fa28244d | 1851 | } else |
146a1439 | 1852 | ret = strlist__add(sl, tev.event); |
0e60836b | 1853 | clear_probe_trace_event(&tev); |
146a1439 MH |
1854 | if (ret < 0) |
1855 | break; | |
b498ce1f | 1856 | } |
b498ce1f MH |
1857 | strlist__delete(rawlist); |
1858 | ||
146a1439 MH |
1859 | if (ret < 0) { |
1860 | strlist__delete(sl); | |
1861 | return NULL; | |
1862 | } | |
b498ce1f MH |
1863 | return sl; |
1864 | } | |
1865 | ||
0e60836b | 1866 | static int write_probe_trace_event(int fd, struct probe_trace_event *tev) |
50656eec | 1867 | { |
6eca8cc3 | 1868 | int ret = 0; |
0e60836b | 1869 | char *buf = synthesize_probe_trace_command(tev); |
50656eec | 1870 | |
146a1439 | 1871 | if (!buf) { |
0e60836b | 1872 | pr_debug("Failed to synthesize probe trace event.\n"); |
146a1439 MH |
1873 | return -EINVAL; |
1874 | } | |
1875 | ||
fa28244d | 1876 | pr_debug("Writing event: %s\n", buf); |
f4d7da49 MH |
1877 | if (!probe_event_dry_run) { |
1878 | ret = write(fd, buf, strlen(buf)); | |
1879 | if (ret <= 0) | |
146a1439 MH |
1880 | pr_warning("Failed to write event: %s\n", |
1881 | strerror(errno)); | |
f4d7da49 | 1882 | } |
4235b045 | 1883 | free(buf); |
146a1439 | 1884 | return ret; |
50656eec MH |
1885 | } |
1886 | ||
146a1439 MH |
1887 | static int get_new_event_name(char *buf, size_t len, const char *base, |
1888 | struct strlist *namelist, bool allow_suffix) | |
b498ce1f MH |
1889 | { |
1890 | int i, ret; | |
17f88fcd MH |
1891 | |
1892 | /* Try no suffix */ | |
1893 | ret = e_snprintf(buf, len, "%s", base); | |
146a1439 MH |
1894 | if (ret < 0) { |
1895 | pr_debug("snprintf() failed: %s\n", strerror(-ret)); | |
1896 | return ret; | |
1897 | } | |
17f88fcd | 1898 | if (!strlist__has_entry(namelist, buf)) |
146a1439 | 1899 | return 0; |
17f88fcd | 1900 | |
d761b08b MH |
1901 | if (!allow_suffix) { |
1902 | pr_warning("Error: event \"%s\" already exists. " | |
1903 | "(Use -f to force duplicates.)\n", base); | |
146a1439 | 1904 | return -EEXIST; |
d761b08b MH |
1905 | } |
1906 | ||
17f88fcd MH |
1907 | /* Try to add suffix */ |
1908 | for (i = 1; i < MAX_EVENT_INDEX; i++) { | |
b498ce1f | 1909 | ret = e_snprintf(buf, len, "%s_%d", base, i); |
146a1439 MH |
1910 | if (ret < 0) { |
1911 | pr_debug("snprintf() failed: %s\n", strerror(-ret)); | |
1912 | return ret; | |
1913 | } | |
b498ce1f MH |
1914 | if (!strlist__has_entry(namelist, buf)) |
1915 | break; | |
1916 | } | |
146a1439 MH |
1917 | if (i == MAX_EVENT_INDEX) { |
1918 | pr_warning("Too many events are on the same function.\n"); | |
1919 | ret = -ERANGE; | |
1920 | } | |
1921 | ||
1922 | return ret; | |
b498ce1f MH |
1923 | } |
1924 | ||
0e60836b SD |
1925 | static int __add_probe_trace_events(struct perf_probe_event *pev, |
1926 | struct probe_trace_event *tevs, | |
146a1439 | 1927 | int ntevs, bool allow_suffix) |
50656eec | 1928 | { |
146a1439 | 1929 | int i, fd, ret; |
0e60836b | 1930 | struct probe_trace_event *tev = NULL; |
4235b045 MH |
1931 | char buf[64]; |
1932 | const char *event, *group; | |
b498ce1f | 1933 | struct strlist *namelist; |
50656eec | 1934 | |
225466f1 SD |
1935 | if (pev->uprobes) |
1936 | fd = open_uprobe_events(true); | |
1937 | else | |
1938 | fd = open_kprobe_events(true); | |
1939 | ||
146a1439 MH |
1940 | if (fd < 0) |
1941 | return fd; | |
b498ce1f | 1942 | /* Get current event names */ |
0e60836b | 1943 | namelist = get_probe_trace_event_names(fd, false); |
146a1439 MH |
1944 | if (!namelist) { |
1945 | pr_debug("Failed to get current event list.\n"); | |
1946 | return -EIO; | |
1947 | } | |
4235b045 | 1948 | |
146a1439 | 1949 | ret = 0; |
a844d1ef | 1950 | printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":"); |
02b95dad | 1951 | for (i = 0; i < ntevs; i++) { |
4235b045 MH |
1952 | tev = &tevs[i]; |
1953 | if (pev->event) | |
1954 | event = pev->event; | |
1955 | else | |
1956 | if (pev->point.function) | |
1957 | event = pev->point.function; | |
1958 | else | |
1959 | event = tev->point.symbol; | |
1960 | if (pev->group) | |
1961 | group = pev->group; | |
1962 | else | |
1963 | group = PERFPROBE_GROUP; | |
1964 | ||
1965 | /* Get an unused new event name */ | |
146a1439 MH |
1966 | ret = get_new_event_name(buf, 64, event, |
1967 | namelist, allow_suffix); | |
1968 | if (ret < 0) | |
1969 | break; | |
4235b045 MH |
1970 | event = buf; |
1971 | ||
02b95dad MH |
1972 | tev->event = strdup(event); |
1973 | tev->group = strdup(group); | |
1974 | if (tev->event == NULL || tev->group == NULL) { | |
1975 | ret = -ENOMEM; | |
1976 | break; | |
1977 | } | |
0e60836b | 1978 | ret = write_probe_trace_event(fd, tev); |
146a1439 MH |
1979 | if (ret < 0) |
1980 | break; | |
4235b045 MH |
1981 | /* Add added event name to namelist */ |
1982 | strlist__add(namelist, event); | |
1983 | ||
1984 | /* Trick here - save current event/group */ | |
1985 | event = pev->event; | |
1986 | group = pev->group; | |
1987 | pev->event = tev->event; | |
1988 | pev->group = tev->group; | |
1989 | show_perf_probe_event(pev); | |
1990 | /* Trick here - restore current event/group */ | |
1991 | pev->event = (char *)event; | |
1992 | pev->group = (char *)group; | |
1993 | ||
1994 | /* | |
1995 | * Probes after the first probe which comes from same | |
1996 | * user input are always allowed to add suffix, because | |
1997 | * there might be several addresses corresponding to | |
1998 | * one code line. | |
1999 | */ | |
2000 | allow_suffix = true; | |
50656eec | 2001 | } |
146a1439 MH |
2002 | |
2003 | if (ret >= 0) { | |
2004 | /* Show how to use the event. */ | |
a844d1ef | 2005 | printf("\nYou can now use it in all perf tools, such as:\n\n"); |
146a1439 MH |
2006 | printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group, |
2007 | tev->event); | |
2008 | } | |
a9b495b0 | 2009 | |
e1d2017b | 2010 | strlist__delete(namelist); |
50656eec | 2011 | close(fd); |
146a1439 | 2012 | return ret; |
50656eec | 2013 | } |
fa28244d | 2014 | |
0e60836b SD |
2015 | static int convert_to_probe_trace_events(struct perf_probe_event *pev, |
2016 | struct probe_trace_event **tevs, | |
4eced234 | 2017 | int max_tevs, const char *target) |
e0faa8d3 MH |
2018 | { |
2019 | struct symbol *sym; | |
fb7345bb | 2020 | int ret, i; |
0e60836b | 2021 | struct probe_trace_event *tev; |
4235b045 | 2022 | |
fb7345bb MH |
2023 | if (pev->uprobes && !pev->group) { |
2024 | /* Replace group name if not given */ | |
2025 | ret = convert_exec_to_group(target, &pev->group); | |
2026 | if (ret != 0) { | |
2027 | pr_warning("Failed to make a group name.\n"); | |
2028 | return ret; | |
2029 | } | |
2030 | } | |
2031 | ||
4b4da7f7 | 2032 | /* Convert perf_probe_event with debuginfo */ |
4eced234 | 2033 | ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target); |
e334016f | 2034 | if (ret != 0) |
190b57fc | 2035 | return ret; /* Found in debuginfo or got an error */ |
e0faa8d3 | 2036 | |
fb7345bb MH |
2037 | if (pev->uprobes) { |
2038 | ret = convert_name_to_addr(pev, target); | |
2039 | if (ret < 0) | |
2040 | return ret; | |
2041 | } | |
2042 | ||
4235b045 | 2043 | /* Allocate trace event buffer */ |
0e60836b | 2044 | tev = *tevs = zalloc(sizeof(struct probe_trace_event)); |
e334016f MH |
2045 | if (tev == NULL) |
2046 | return -ENOMEM; | |
4235b045 MH |
2047 | |
2048 | /* Copy parameters */ | |
02b95dad MH |
2049 | tev->point.symbol = strdup(pev->point.function); |
2050 | if (tev->point.symbol == NULL) { | |
2051 | ret = -ENOMEM; | |
2052 | goto error; | |
2053 | } | |
ce27a443 | 2054 | |
4eced234 SD |
2055 | if (target) { |
2056 | tev->point.module = strdup(target); | |
ce27a443 JZ |
2057 | if (tev->point.module == NULL) { |
2058 | ret = -ENOMEM; | |
2059 | goto error; | |
2060 | } | |
190b57fc | 2061 | } |
ce27a443 | 2062 | |
4235b045 | 2063 | tev->point.offset = pev->point.offset; |
04ddd04b | 2064 | tev->point.retprobe = pev->point.retprobe; |
4235b045 | 2065 | tev->nargs = pev->nargs; |
225466f1 SD |
2066 | tev->uprobes = pev->uprobes; |
2067 | ||
4235b045 | 2068 | if (tev->nargs) { |
0e60836b | 2069 | tev->args = zalloc(sizeof(struct probe_trace_arg) |
e334016f MH |
2070 | * tev->nargs); |
2071 | if (tev->args == NULL) { | |
02b95dad MH |
2072 | ret = -ENOMEM; |
2073 | goto error; | |
e334016f | 2074 | } |
48481938 | 2075 | for (i = 0; i < tev->nargs; i++) { |
02b95dad MH |
2076 | if (pev->args[i].name) { |
2077 | tev->args[i].name = strdup(pev->args[i].name); | |
2078 | if (tev->args[i].name == NULL) { | |
2079 | ret = -ENOMEM; | |
2080 | goto error; | |
2081 | } | |
2082 | } | |
2083 | tev->args[i].value = strdup(pev->args[i].var); | |
2084 | if (tev->args[i].value == NULL) { | |
2085 | ret = -ENOMEM; | |
2086 | goto error; | |
2087 | } | |
2088 | if (pev->args[i].type) { | |
2089 | tev->args[i].type = strdup(pev->args[i].type); | |
2090 | if (tev->args[i].type == NULL) { | |
2091 | ret = -ENOMEM; | |
2092 | goto error; | |
2093 | } | |
2094 | } | |
48481938 | 2095 | } |
4235b045 MH |
2096 | } |
2097 | ||
225466f1 SD |
2098 | if (pev->uprobes) |
2099 | return 1; | |
2100 | ||
4235b045 | 2101 | /* Currently just checking function name from symbol map */ |
469b9b88 | 2102 | sym = __find_kernel_function_by_name(tev->point.symbol, NULL); |
146a1439 MH |
2103 | if (!sym) { |
2104 | pr_warning("Kernel symbol \'%s\' not found.\n", | |
2105 | tev->point.symbol); | |
02b95dad MH |
2106 | ret = -ENOENT; |
2107 | goto error; | |
1c1bc922 PN |
2108 | } else if (tev->point.offset > sym->end - sym->start) { |
2109 | pr_warning("Offset specified is greater than size of %s\n", | |
2110 | tev->point.symbol); | |
2111 | ret = -ENOENT; | |
2112 | goto error; | |
2113 | ||
02b95dad | 2114 | } |
e334016f | 2115 | |
02b95dad MH |
2116 | return 1; |
2117 | error: | |
0e60836b | 2118 | clear_probe_trace_event(tev); |
02b95dad MH |
2119 | free(tev); |
2120 | *tevs = NULL; | |
e334016f | 2121 | return ret; |
4235b045 MH |
2122 | } |
2123 | ||
2124 | struct __event_package { | |
2125 | struct perf_probe_event *pev; | |
0e60836b | 2126 | struct probe_trace_event *tevs; |
4235b045 MH |
2127 | int ntevs; |
2128 | }; | |
2129 | ||
146a1439 | 2130 | int add_perf_probe_events(struct perf_probe_event *pevs, int npevs, |
4eced234 | 2131 | int max_tevs, const char *target, bool force_add) |
4235b045 | 2132 | { |
146a1439 | 2133 | int i, j, ret; |
4235b045 MH |
2134 | struct __event_package *pkgs; |
2135 | ||
225466f1 | 2136 | ret = 0; |
e334016f | 2137 | pkgs = zalloc(sizeof(struct __event_package) * npevs); |
225466f1 | 2138 | |
e334016f MH |
2139 | if (pkgs == NULL) |
2140 | return -ENOMEM; | |
4235b045 | 2141 | |
225466f1 SD |
2142 | if (!pevs->uprobes) |
2143 | /* Init vmlinux path */ | |
2144 | ret = init_vmlinux(); | |
2145 | else | |
2146 | ret = init_user_exec(); | |
2147 | ||
449e5b24 MH |
2148 | if (ret < 0) { |
2149 | free(pkgs); | |
146a1439 | 2150 | return ret; |
449e5b24 | 2151 | } |
4235b045 MH |
2152 | |
2153 | /* Loop 1: convert all events */ | |
2154 | for (i = 0; i < npevs; i++) { | |
2155 | pkgs[i].pev = &pevs[i]; | |
2156 | /* Convert with or without debuginfo */ | |
0e60836b | 2157 | ret = convert_to_probe_trace_events(pkgs[i].pev, |
469b9b88 MH |
2158 | &pkgs[i].tevs, |
2159 | max_tevs, | |
4eced234 | 2160 | target); |
146a1439 MH |
2161 | if (ret < 0) |
2162 | goto end; | |
2163 | pkgs[i].ntevs = ret; | |
e0faa8d3 MH |
2164 | } |
2165 | ||
4235b045 | 2166 | /* Loop 2: add all events */ |
8635bf6e | 2167 | for (i = 0; i < npevs; i++) { |
0e60836b | 2168 | ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs, |
146a1439 | 2169 | pkgs[i].ntevs, force_add); |
fbee632d ACM |
2170 | if (ret < 0) |
2171 | break; | |
2172 | } | |
146a1439 | 2173 | end: |
449e5b24 MH |
2174 | /* Loop 3: cleanup and free trace events */ |
2175 | for (i = 0; i < npevs; i++) { | |
146a1439 | 2176 | for (j = 0; j < pkgs[i].ntevs; j++) |
0e60836b | 2177 | clear_probe_trace_event(&pkgs[i].tevs[j]); |
449e5b24 MH |
2178 | free(pkgs[i].tevs); |
2179 | } | |
2180 | free(pkgs); | |
146a1439 MH |
2181 | |
2182 | return ret; | |
e0faa8d3 MH |
2183 | } |
2184 | ||
0e60836b | 2185 | static int __del_trace_probe_event(int fd, struct str_node *ent) |
bbbb521b MH |
2186 | { |
2187 | char *p; | |
2188 | char buf[128]; | |
4235b045 | 2189 | int ret; |
bbbb521b | 2190 | |
0e60836b | 2191 | /* Convert from perf-probe event to trace-probe event */ |
146a1439 MH |
2192 | ret = e_snprintf(buf, 128, "-:%s", ent->s); |
2193 | if (ret < 0) | |
2194 | goto error; | |
2195 | ||
bbbb521b | 2196 | p = strchr(buf + 2, ':'); |
146a1439 MH |
2197 | if (!p) { |
2198 | pr_debug("Internal error: %s should have ':' but not.\n", | |
2199 | ent->s); | |
2200 | ret = -ENOTSUP; | |
2201 | goto error; | |
2202 | } | |
bbbb521b MH |
2203 | *p = '/'; |
2204 | ||
4235b045 MH |
2205 | pr_debug("Writing event: %s\n", buf); |
2206 | ret = write(fd, buf, strlen(buf)); | |
44a56040 MH |
2207 | if (ret < 0) { |
2208 | ret = -errno; | |
146a1439 | 2209 | goto error; |
44a56040 | 2210 | } |
146a1439 | 2211 | |
a844d1ef | 2212 | printf("Removed event: %s\n", ent->s); |
146a1439 MH |
2213 | return 0; |
2214 | error: | |
2215 | pr_warning("Failed to delete event: %s\n", strerror(-ret)); | |
2216 | return ret; | |
bbbb521b MH |
2217 | } |
2218 | ||
225466f1 SD |
2219 | static int del_trace_probe_event(int fd, const char *buf, |
2220 | struct strlist *namelist) | |
fa28244d | 2221 | { |
bbbb521b | 2222 | struct str_node *ent, *n; |
225466f1 | 2223 | int ret = -1; |
fa28244d | 2224 | |
bbbb521b MH |
2225 | if (strpbrk(buf, "*?")) { /* Glob-exp */ |
2226 | strlist__for_each_safe(ent, n, namelist) | |
2227 | if (strglobmatch(ent->s, buf)) { | |
0e60836b | 2228 | ret = __del_trace_probe_event(fd, ent); |
146a1439 MH |
2229 | if (ret < 0) |
2230 | break; | |
bbbb521b MH |
2231 | strlist__remove(namelist, ent); |
2232 | } | |
2233 | } else { | |
2234 | ent = strlist__find(namelist, buf); | |
2235 | if (ent) { | |
0e60836b | 2236 | ret = __del_trace_probe_event(fd, ent); |
146a1439 MH |
2237 | if (ret >= 0) |
2238 | strlist__remove(namelist, ent); | |
bbbb521b MH |
2239 | } |
2240 | } | |
146a1439 MH |
2241 | |
2242 | return ret; | |
fa28244d MH |
2243 | } |
2244 | ||
146a1439 | 2245 | int del_perf_probe_events(struct strlist *dellist) |
fa28244d | 2246 | { |
225466f1 SD |
2247 | int ret = -1, ufd = -1, kfd = -1; |
2248 | char buf[128]; | |
fa28244d MH |
2249 | const char *group, *event; |
2250 | char *p, *str; | |
2251 | struct str_node *ent; | |
225466f1 | 2252 | struct strlist *namelist = NULL, *unamelist = NULL; |
146a1439 | 2253 | |
fa28244d | 2254 | /* Get current event names */ |
225466f1 SD |
2255 | kfd = open_kprobe_events(true); |
2256 | if (kfd < 0) | |
2257 | return kfd; | |
2258 | ||
2259 | namelist = get_probe_trace_event_names(kfd, true); | |
2260 | ufd = open_uprobe_events(true); | |
2261 | ||
2262 | if (ufd >= 0) | |
2263 | unamelist = get_probe_trace_event_names(ufd, true); | |
2264 | ||
2265 | if (namelist == NULL && unamelist == NULL) | |
2266 | goto error; | |
fa28244d | 2267 | |
adf365f4 | 2268 | strlist__for_each(ent, dellist) { |
02b95dad MH |
2269 | str = strdup(ent->s); |
2270 | if (str == NULL) { | |
2271 | ret = -ENOMEM; | |
225466f1 | 2272 | goto error; |
02b95dad | 2273 | } |
bbbb521b | 2274 | pr_debug("Parsing: %s\n", str); |
fa28244d MH |
2275 | p = strchr(str, ':'); |
2276 | if (p) { | |
2277 | group = str; | |
2278 | *p = '\0'; | |
2279 | event = p + 1; | |
2280 | } else { | |
bbbb521b | 2281 | group = "*"; |
fa28244d MH |
2282 | event = str; |
2283 | } | |
225466f1 SD |
2284 | |
2285 | ret = e_snprintf(buf, 128, "%s:%s", group, event); | |
2286 | if (ret < 0) { | |
2287 | pr_err("Failed to copy event."); | |
2288 | free(str); | |
2289 | goto error; | |
2290 | } | |
2291 | ||
bbbb521b | 2292 | pr_debug("Group: %s, Event: %s\n", group, event); |
225466f1 SD |
2293 | |
2294 | if (namelist) | |
2295 | ret = del_trace_probe_event(kfd, buf, namelist); | |
2296 | ||
2297 | if (unamelist && ret != 0) | |
2298 | ret = del_trace_probe_event(ufd, buf, unamelist); | |
2299 | ||
2300 | if (ret != 0) | |
2301 | pr_info("Info: Event \"%s\" does not exist.\n", buf); | |
2302 | ||
fa28244d MH |
2303 | free(str); |
2304 | } | |
225466f1 SD |
2305 | |
2306 | error: | |
2307 | if (kfd >= 0) { | |
a23c4dc4 | 2308 | strlist__delete(namelist); |
225466f1 SD |
2309 | close(kfd); |
2310 | } | |
2311 | ||
2312 | if (ufd >= 0) { | |
a23c4dc4 | 2313 | strlist__delete(unamelist); |
225466f1 SD |
2314 | close(ufd); |
2315 | } | |
146a1439 MH |
2316 | |
2317 | return ret; | |
fa28244d | 2318 | } |
225466f1 | 2319 | |
3c42258c MH |
2320 | /* TODO: don't use a global variable for filter ... */ |
2321 | static struct strfilter *available_func_filter; | |
fa28244d | 2322 | |
e80711ca | 2323 | /* |
3c42258c MH |
2324 | * If a symbol corresponds to a function with global binding and |
2325 | * matches filter return 0. For all others return 1. | |
e80711ca | 2326 | */ |
1d037ca1 | 2327 | static int filter_available_functions(struct map *map __maybe_unused, |
3c42258c | 2328 | struct symbol *sym) |
e80711ca | 2329 | { |
3c42258c MH |
2330 | if (sym->binding == STB_GLOBAL && |
2331 | strfilter__compare(available_func_filter, sym->name)) | |
2332 | return 0; | |
2333 | return 1; | |
e80711ca MH |
2334 | } |
2335 | ||
225466f1 SD |
2336 | static int __show_available_funcs(struct map *map) |
2337 | { | |
2338 | if (map__load(map, filter_available_functions)) { | |
2339 | pr_err("Failed to load map.\n"); | |
2340 | return -EINVAL; | |
2341 | } | |
2342 | if (!dso__sorted_by_name(map->dso, map->type)) | |
2343 | dso__sort_by_name(map->dso, map->type); | |
2344 | ||
2345 | dso__fprintf_symbols_by_name(map->dso, map->type, stdout); | |
2346 | return 0; | |
2347 | } | |
2348 | ||
2349 | static int available_kernel_funcs(const char *module) | |
e80711ca MH |
2350 | { |
2351 | struct map *map; | |
2352 | int ret; | |
2353 | ||
e80711ca MH |
2354 | ret = init_vmlinux(); |
2355 | if (ret < 0) | |
2356 | return ret; | |
2357 | ||
225466f1 | 2358 | map = kernel_get_module_map(module); |
e80711ca | 2359 | if (!map) { |
225466f1 | 2360 | pr_err("Failed to find %s map.\n", (module) ? : "kernel"); |
e80711ca MH |
2361 | return -EINVAL; |
2362 | } | |
225466f1 SD |
2363 | return __show_available_funcs(map); |
2364 | } | |
2365 | ||
2366 | static int available_user_funcs(const char *target) | |
2367 | { | |
2368 | struct map *map; | |
2369 | int ret; | |
2370 | ||
2371 | ret = init_user_exec(); | |
2372 | if (ret < 0) | |
2373 | return ret; | |
2374 | ||
2375 | map = dso__new_map(target); | |
2376 | ret = __show_available_funcs(map); | |
2377 | dso__delete(map->dso); | |
2378 | map__delete(map); | |
2379 | return ret; | |
2380 | } | |
2381 | ||
2382 | int show_available_funcs(const char *target, struct strfilter *_filter, | |
2383 | bool user) | |
2384 | { | |
2385 | setup_pager(); | |
3c42258c | 2386 | available_func_filter = _filter; |
225466f1 SD |
2387 | |
2388 | if (!user) | |
2389 | return available_kernel_funcs(target); | |
2390 | ||
2391 | return available_user_funcs(target); | |
2392 | } | |
2393 | ||
2394 | /* | |
2395 | * uprobe_events only accepts address: | |
2396 | * Convert function and any offset to address | |
2397 | */ | |
2398 | static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec) | |
2399 | { | |
2400 | struct perf_probe_point *pp = &pev->point; | |
2401 | struct symbol *sym; | |
2402 | struct map *map = NULL; | |
8a613d40 | 2403 | char *function = NULL; |
225466f1 SD |
2404 | int ret = -EINVAL; |
2405 | unsigned long long vaddr = 0; | |
2406 | ||
2407 | if (!pp->function) { | |
2408 | pr_warning("No function specified for uprobes"); | |
2409 | goto out; | |
2410 | } | |
2411 | ||
2412 | function = strdup(pp->function); | |
2413 | if (!function) { | |
2414 | pr_warning("Failed to allocate memory by strdup.\n"); | |
2415 | ret = -ENOMEM; | |
2416 | goto out; | |
2417 | } | |
2418 | ||
8a613d40 | 2419 | map = dso__new_map(exec); |
225466f1 SD |
2420 | if (!map) { |
2421 | pr_warning("Cannot find appropriate DSO for %s.\n", exec); | |
2422 | goto out; | |
2423 | } | |
2424 | available_func_filter = strfilter__new(function, NULL); | |
3c42258c | 2425 | if (map__load(map, filter_available_functions)) { |
e80711ca | 2426 | pr_err("Failed to load map.\n"); |
225466f1 | 2427 | goto out; |
e80711ca | 2428 | } |
e80711ca | 2429 | |
225466f1 SD |
2430 | sym = map__find_symbol_by_name(map, function, NULL); |
2431 | if (!sym) { | |
2432 | pr_warning("Cannot find %s in DSO %s\n", function, exec); | |
2433 | goto out; | |
2434 | } | |
2435 | ||
2436 | if (map->start > sym->start) | |
2437 | vaddr = map->start; | |
2438 | vaddr += sym->start + pp->offset + map->pgoff; | |
2439 | pp->offset = 0; | |
2440 | ||
2441 | if (!pev->event) { | |
2442 | pev->event = function; | |
2443 | function = NULL; | |
2444 | } | |
2445 | if (!pev->group) { | |
1fb89448 | 2446 | char *ptr1, *ptr2, *exec_copy; |
225466f1 SD |
2447 | |
2448 | pev->group = zalloc(sizeof(char *) * 64); | |
1fb89448 DA |
2449 | exec_copy = strdup(exec); |
2450 | if (!exec_copy) { | |
2451 | ret = -ENOMEM; | |
2452 | pr_warning("Failed to copy exec string.\n"); | |
2453 | goto out; | |
2454 | } | |
2455 | ||
2456 | ptr1 = strdup(basename(exec_copy)); | |
225466f1 SD |
2457 | if (ptr1) { |
2458 | ptr2 = strpbrk(ptr1, "-._"); | |
2459 | if (ptr2) | |
2460 | *ptr2 = '\0'; | |
2461 | e_snprintf(pev->group, 64, "%s_%s", PERFPROBE_GROUP, | |
2462 | ptr1); | |
2463 | free(ptr1); | |
2464 | } | |
1fb89448 | 2465 | free(exec_copy); |
225466f1 SD |
2466 | } |
2467 | free(pp->function); | |
2468 | pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS); | |
2469 | if (!pp->function) { | |
2470 | ret = -ENOMEM; | |
2471 | pr_warning("Failed to allocate memory by zalloc.\n"); | |
2472 | goto out; | |
2473 | } | |
2474 | e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr); | |
2475 | ret = 0; | |
2476 | ||
2477 | out: | |
2478 | if (map) { | |
2479 | dso__delete(map->dso); | |
2480 | map__delete(map); | |
2481 | } | |
2482 | if (function) | |
2483 | free(function); | |
225466f1 | 2484 | return ret; |
e80711ca | 2485 | } |