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