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