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