Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
50656eec | 2 | /* |
0e60836b | 3 | * probe-event.c : perf-probe definition to probe_events format converter |
50656eec MH |
4 | * |
5 | * Written by Masami Hiramatsu <mhiramat@redhat.com> | |
50656eec MH |
6 | */ |
7 | ||
fd20e811 | 8 | #include <inttypes.h> |
50656eec MH |
9 | #include <sys/utsname.h> |
10 | #include <sys/types.h> | |
11 | #include <sys/stat.h> | |
12 | #include <fcntl.h> | |
13 | #include <errno.h> | |
58103715 | 14 | #include <libgen.h> |
50656eec MH |
15 | #include <stdio.h> |
16 | #include <unistd.h> | |
17 | #include <stdlib.h> | |
18 | #include <string.h> | |
4de189fe MH |
19 | #include <stdarg.h> |
20 | #include <limits.h> | |
e80711ca | 21 | #include <elf.h> |
50656eec | 22 | |
4a3cec84 | 23 | #include "build-id.h" |
50656eec | 24 | #include "event.h" |
40f3b2d2 | 25 | #include "namespaces.h" |
4de189fe | 26 | #include "strlist.h" |
8ec20b17 | 27 | #include "strfilter.h" |
50656eec | 28 | #include "debug.h" |
4a3cec84 | 29 | #include "dso.h" |
631c9def | 30 | #include "color.h" |
1101f69a | 31 | #include "map.h" |
c54d241b | 32 | #include "maps.h" |
e54dea69 | 33 | #include "mutex.h" |
e0faa8d3 | 34 | #include "symbol.h" |
4605eab3 | 35 | #include <api/fs/fs.h> |
1d037ca1 | 36 | #include "trace-event.h" /* For __maybe_unused */ |
50656eec | 37 | #include "probe-event.h" |
4235b045 | 38 | #include "probe-finder.h" |
92f6c72e | 39 | #include "probe-file.h" |
225466f1 | 40 | #include "session.h" |
a067558e | 41 | #include "string2.h" |
fa0d9846 | 42 | #include "strbuf.h" |
50656eec | 43 | |
fa0d9846 | 44 | #include <subcmd/pager.h> |
3052ba56 | 45 | #include <linux/ctype.h> |
7f7c536f | 46 | #include <linux/zalloc.h> |
3d689ed6 | 47 | |
7cd5738d MH |
48 | #ifdef HAVE_DEBUGINFOD_SUPPORT |
49 | #include <elfutils/debuginfod.h> | |
50 | #endif | |
51 | ||
50656eec MH |
52 | #define PERFPROBE_GROUP "probe" |
53 | ||
f4d7da49 | 54 | bool probe_event_dry_run; /* Dry run flag */ |
cb402730 | 55 | struct probe_conf probe_conf = { .magic_num = DEFAULT_PROBE_MAGIC_NUM }; |
f4d7da49 | 56 | |
aeb50d3f ACM |
57 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp); |
58 | ||
146a1439 | 59 | #define semantic_error(msg ...) pr_err("Semantic error :" msg) |
50656eec | 60 | |
92f6c72e | 61 | int e_snprintf(char *str, size_t size, const char *format, ...) |
4de189fe MH |
62 | { |
63 | int ret; | |
64 | va_list ap; | |
65 | va_start(ap, format); | |
66 | ret = vsnprintf(str, size, format, ap); | |
67 | va_end(ap); | |
68 | if (ret >= (int)size) | |
69 | ret = -E2BIG; | |
70 | return ret; | |
71 | } | |
72 | ||
ee45b6c2 | 73 | static struct machine *host_machine; |
e0faa8d3 | 74 | |
469b9b88 | 75 | /* Initialize symbol maps and path of vmlinux/modules */ |
9bae1e8c | 76 | int init_probe_symbol_maps(bool user_only) |
e0faa8d3 | 77 | { |
146a1439 MH |
78 | int ret; |
79 | ||
680d926a | 80 | symbol_conf.allow_aliases = true; |
0a7e6d1b | 81 | ret = symbol__init(NULL); |
146a1439 MH |
82 | if (ret < 0) { |
83 | pr_debug("Failed to init symbol map.\n"); | |
84 | goto out; | |
85 | } | |
e0faa8d3 | 86 | |
ee45b6c2 MH |
87 | if (host_machine || user_only) /* already initialized */ |
88 | return 0; | |
d28c6223 | 89 | |
ee45b6c2 MH |
90 | if (symbol_conf.vmlinux_name) |
91 | pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); | |
92 | ||
93 | host_machine = machine__new_host(); | |
94 | if (!host_machine) { | |
95 | pr_debug("machine__new_host() failed.\n"); | |
96 | symbol__exit(); | |
97 | ret = -1; | |
469b9b88 | 98 | } |
146a1439 MH |
99 | out: |
100 | if (ret < 0) | |
101 | pr_warning("Failed to init vmlinux path.\n"); | |
102 | return ret; | |
e0faa8d3 MH |
103 | } |
104 | ||
9bae1e8c | 105 | void exit_probe_symbol_maps(void) |
ee45b6c2 | 106 | { |
32ca678d ACM |
107 | machine__delete(host_machine); |
108 | host_machine = NULL; | |
ee45b6c2 MH |
109 | symbol__exit(); |
110 | } | |
111 | ||
80526491 | 112 | static struct ref_reloc_sym *kernel_get_ref_reloc_sym(struct map **pmap) |
8f33f7de | 113 | { |
8f33f7de | 114 | struct kmap *kmap; |
a5e813c6 | 115 | struct map *map = machine__kernel_map(host_machine); |
8f33f7de | 116 | |
be39db9f | 117 | if (map__load(map) < 0) |
8f33f7de MH |
118 | return NULL; |
119 | ||
77e65977 | 120 | kmap = map__kmap(map); |
ba92732e WN |
121 | if (!kmap) |
122 | return NULL; | |
80526491 MH |
123 | |
124 | if (pmap) | |
125 | *pmap = map; | |
126 | ||
8f33f7de MH |
127 | return kmap->ref_reloc_sym; |
128 | } | |
129 | ||
9b239a12 MH |
130 | static int kernel_get_symbol_address_by_name(const char *name, u64 *addr, |
131 | bool reloc, bool reladdr) | |
8f33f7de MH |
132 | { |
133 | struct ref_reloc_sym *reloc_sym; | |
134 | struct symbol *sym; | |
135 | struct map *map; | |
136 | ||
137 | /* ref_reloc_sym is just a label. Need a special fix*/ | |
ac7a75d1 | 138 | reloc_sym = kernel_get_ref_reloc_sym(&map); |
8f33f7de | 139 | if (reloc_sym && strcmp(name, reloc_sym->name) == 0) |
2a6e5e8a | 140 | *addr = (!map__reloc(map) || reloc) ? reloc_sym->addr : |
ac7a75d1 | 141 | reloc_sym->unrelocated_addr; |
8f33f7de | 142 | else { |
107cad95 | 143 | sym = machine__find_kernel_symbol_by_name(host_machine, name, &map); |
9b239a12 MH |
144 | if (!sym) |
145 | return -ENOENT; | |
78a1f7cd | 146 | *addr = map__unmap_ip(map, sym->start) - |
2a6e5e8a | 147 | ((reloc) ? 0 : map__reloc(map)) - |
e5116f46 | 148 | ((reladdr) ? map__start(map) : 0); |
8f33f7de MH |
149 | } |
150 | return 0; | |
151 | } | |
152 | ||
300b53d5 IR |
153 | struct kernel_get_module_map_cb_args { |
154 | const char *module; | |
155 | struct map *result; | |
156 | }; | |
157 | ||
158 | static int kernel_get_module_map_cb(struct map *map, void *data) | |
159 | { | |
160 | struct kernel_get_module_map_cb_args *args = data; | |
161 | struct dso *dso = map__dso(map); | |
ee756ef7 IR |
162 | const char *short_name = dso__short_name(dso); |
163 | u16 short_name_len = dso__short_name_len(dso); | |
300b53d5 IR |
164 | |
165 | if (strncmp(short_name + 1, args->module, short_name_len - 2) == 0 && | |
166 | args->module[short_name_len - 2] == '\0') { | |
167 | args->result = map__get(map); | |
168 | return 1; | |
169 | } | |
170 | return 0; | |
171 | } | |
172 | ||
e80711ca MH |
173 | static struct map *kernel_get_module_map(const char *module) |
174 | { | |
300b53d5 IR |
175 | struct kernel_get_module_map_cb_args args = { |
176 | .module = module, | |
177 | .result = NULL, | |
178 | }; | |
e80711ca | 179 | |
14a8fd7c MH |
180 | /* A file path -- this is an offline module */ |
181 | if (module && strchr(module, '/')) | |
eebc509b | 182 | return dso__new_map(module); |
14a8fd7c | 183 | |
eaeffeb9 | 184 | if (!module) { |
ff583dc4 IR |
185 | struct map *map = machine__kernel_map(host_machine); |
186 | ||
187 | return map__get(map); | |
eaeffeb9 | 188 | } |
e80711ca | 189 | |
300b53d5 IR |
190 | maps__for_each_map(machine__kernel_maps(host_machine), kernel_get_module_map_cb, &args); |
191 | ||
192 | return args.result; | |
e80711ca MH |
193 | } |
194 | ||
544abd44 | 195 | struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user) |
9b118aca MH |
196 | { |
197 | /* Init maps of given executable or kernel */ | |
544abd44 KJ |
198 | if (user) { |
199 | struct map *map; | |
63df0e4b | 200 | struct dso *dso; |
544abd44 KJ |
201 | |
202 | map = dso__new_map(target); | |
63df0e4b IR |
203 | dso = map ? map__dso(map) : NULL; |
204 | if (dso) { | |
ee756ef7 IR |
205 | mutex_lock(dso__lock(dso)); |
206 | dso__set_nsinfo(dso, nsinfo__get(nsi)); | |
207 | mutex_unlock(dso__lock(dso)); | |
dedeb4be | 208 | } |
544abd44 KJ |
209 | return map; |
210 | } else { | |
9b118aca | 211 | return kernel_get_module_map(target); |
544abd44 | 212 | } |
9b118aca MH |
213 | } |
214 | ||
fb7345bb MH |
215 | static int convert_exec_to_group(const char *exec, char **result) |
216 | { | |
217 | char *ptr1, *ptr2, *exec_copy; | |
218 | char buf[64]; | |
219 | int ret; | |
220 | ||
221 | exec_copy = strdup(exec); | |
222 | if (!exec_copy) | |
223 | return -ENOMEM; | |
224 | ||
225 | ptr1 = basename(exec_copy); | |
226 | if (!ptr1) { | |
227 | ret = -EINVAL; | |
228 | goto out; | |
229 | } | |
230 | ||
ead1a574 | 231 | for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) { |
35726d3a MH |
232 | if (!isalnum(*ptr2) && *ptr2 != '_') { |
233 | *ptr2 = '\0'; | |
234 | break; | |
235 | } | |
236 | } | |
237 | ||
a529bec0 | 238 | ret = e_snprintf(buf, sizeof(buf), "%s_%s", PERFPROBE_GROUP, ptr1); |
fb7345bb MH |
239 | if (ret < 0) |
240 | goto out; | |
241 | ||
242 | *result = strdup(buf); | |
243 | ret = *result ? 0 : -ENOMEM; | |
244 | ||
245 | out: | |
246 | free(exec_copy); | |
247 | return ret; | |
248 | } | |
249 | ||
9b118aca MH |
250 | static void clear_perf_probe_point(struct perf_probe_point *pp) |
251 | { | |
d8f9da24 ACM |
252 | zfree(&pp->file); |
253 | zfree(&pp->function); | |
254 | zfree(&pp->lazy_line); | |
9b118aca MH |
255 | } |
256 | ||
eb948e50 MH |
257 | static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs) |
258 | { | |
259 | int i; | |
260 | ||
261 | for (i = 0; i < ntevs; i++) | |
262 | clear_probe_trace_event(tevs + i); | |
263 | } | |
264 | ||
22a66551 YJ |
265 | static bool kprobe_blacklist__listed(u64 address); |
266 | static bool kprobe_warn_out_range(const char *symbol, u64 address) | |
b031220d | 267 | { |
2ae5d0d7 MH |
268 | struct map *map; |
269 | bool ret = false; | |
270 | ||
271 | map = kernel_get_module_map(NULL); | |
272 | if (map) { | |
e5116f46 | 273 | ret = address <= map__start(map) || map__end(map) < address; |
2ae5d0d7 MH |
274 | if (ret) |
275 | pr_warning("%s is out of .text, skip it.\n", symbol); | |
276 | map__put(map); | |
277 | } | |
278 | if (!ret && kprobe_blacklist__listed(address)) { | |
b031220d | 279 | pr_warning("%s is blacklisted function, skip it.\n", symbol); |
2ae5d0d7 MH |
280 | ret = true; |
281 | } | |
b031220d | 282 | |
2ae5d0d7 | 283 | return ret; |
b031220d MH |
284 | } |
285 | ||
c61fb959 RB |
286 | /* |
287 | * @module can be module name of module file path. In case of path, | |
288 | * inspect elf and find out what is actual module name. | |
289 | * Caller has to free mod_name after using it. | |
290 | */ | |
291 | static char *find_module_name(const char *module) | |
292 | { | |
293 | int fd; | |
294 | Elf *elf; | |
295 | GElf_Ehdr ehdr; | |
296 | GElf_Shdr shdr; | |
297 | Elf_Data *data; | |
298 | Elf_Scn *sec; | |
299 | char *mod_name = NULL; | |
1f2ed153 | 300 | int name_offset; |
c61fb959 RB |
301 | |
302 | fd = open(module, O_RDONLY); | |
303 | if (fd < 0) | |
304 | return NULL; | |
305 | ||
306 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); | |
307 | if (elf == NULL) | |
308 | goto elf_err; | |
309 | ||
310 | if (gelf_getehdr(elf, &ehdr) == NULL) | |
311 | goto ret_err; | |
312 | ||
313 | sec = elf_section_by_name(elf, &ehdr, &shdr, | |
314 | ".gnu.linkonce.this_module", NULL); | |
315 | if (!sec) | |
316 | goto ret_err; | |
317 | ||
318 | data = elf_getdata(sec, NULL); | |
319 | if (!data || !data->d_buf) | |
320 | goto ret_err; | |
321 | ||
1f2ed153 MH |
322 | /* |
323 | * NOTE: | |
324 | * '.gnu.linkonce.this_module' section of kernel module elf directly | |
325 | * maps to 'struct module' from linux/module.h. This section contains | |
326 | * actual module name which will be used by kernel after loading it. | |
327 | * But, we cannot use 'struct module' here since linux/module.h is not | |
328 | * exposed to user-space. Offset of 'name' has remained same from long | |
329 | * time, so hardcoding it here. | |
330 | */ | |
331 | if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) | |
332 | name_offset = 12; | |
333 | else /* expect ELFCLASS64 by default */ | |
334 | name_offset = 24; | |
335 | ||
336 | mod_name = strdup((char *)data->d_buf + name_offset); | |
c61fb959 RB |
337 | |
338 | ret_err: | |
339 | elf_end(elf); | |
340 | elf_err: | |
341 | close(fd); | |
342 | return mod_name; | |
343 | } | |
344 | ||
89fe808a | 345 | #ifdef HAVE_DWARF_SUPPORT |
60fb7742 WN |
346 | |
347 | static int kernel_get_module_dso(const char *module, struct dso **pdso) | |
348 | { | |
349 | struct dso *dso; | |
350 | struct map *map; | |
351 | const char *vmlinux_name; | |
352 | int ret = 0; | |
353 | ||
354 | if (module) { | |
266fa2b2 ACM |
355 | char module_name[128]; |
356 | ||
357 | snprintf(module_name, sizeof(module_name), "[%s]", module); | |
1a97cee6 | 358 | map = maps__find_by_name(machine__kernel_maps(host_machine), module_name); |
266fa2b2 | 359 | if (map) { |
63df0e4b | 360 | dso = map__dso(map); |
107ef66c | 361 | map__put(map); |
266fa2b2 | 362 | goto found; |
60fb7742 WN |
363 | } |
364 | pr_debug("Failed to find module %s.\n", module); | |
365 | return -ENOENT; | |
366 | } | |
367 | ||
a5e813c6 | 368 | map = machine__kernel_map(host_machine); |
63df0e4b | 369 | dso = map__dso(map); |
ee756ef7 | 370 | if (!dso__has_build_id(dso)) |
7cd5738d | 371 | dso__read_running_kernel_build_id(dso, host_machine); |
60fb7742 WN |
372 | |
373 | vmlinux_name = symbol_conf.vmlinux_name; | |
ee756ef7 | 374 | *dso__load_errno(dso) = 0; |
60fb7742 | 375 | if (vmlinux_name) |
be39db9f | 376 | ret = dso__load_vmlinux(dso, map, vmlinux_name, false); |
60fb7742 | 377 | else |
be39db9f | 378 | ret = dso__load_vmlinux_path(dso, map); |
60fb7742 WN |
379 | found: |
380 | *pdso = dso; | |
381 | return ret; | |
382 | } | |
383 | ||
9b118aca MH |
384 | /* |
385 | * Some binaries like glibc have special symbols which are on the symbol | |
386 | * table, but not in the debuginfo. If we can find the address of the | |
387 | * symbol from map, we can translate the address back to the probe point. | |
388 | */ | |
389 | static int find_alternative_probe_point(struct debuginfo *dinfo, | |
390 | struct perf_probe_point *pp, | |
391 | struct perf_probe_point *result, | |
544abd44 KJ |
392 | const char *target, struct nsinfo *nsi, |
393 | bool uprobes) | |
9b118aca MH |
394 | { |
395 | struct map *map = NULL; | |
396 | struct symbol *sym; | |
397 | u64 address = 0; | |
398 | int ret = -ENOENT; | |
259dce91 | 399 | size_t idx; |
9b118aca MH |
400 | |
401 | /* This can work only for function-name based one */ | |
402 | if (!pp->function || pp->file) | |
403 | return -ENOTSUP; | |
404 | ||
544abd44 | 405 | map = get_target_map(target, nsi, uprobes); |
9b118aca MH |
406 | if (!map) |
407 | return -EINVAL; | |
408 | ||
409 | /* Find the address of given function */ | |
259dce91 | 410 | map__for_each_symbol_by_name(map, pp->function, sym, idx) { |
3de2bf9d | 411 | if (uprobes) { |
e6d7c91c | 412 | address = sym->start; |
3de2bf9d MH |
413 | if (sym->type == STT_GNU_IFUNC) |
414 | pr_warning("Warning: The probe function (%s) is a GNU indirect function.\n" | |
415 | "Consider identifying the final function used at run time and set the probe directly on that.\n", | |
416 | pp->function); | |
417 | } else | |
2a6e5e8a | 418 | address = map__unmap_ip(map, sym->start) - map__reloc(map); |
e578da3b | 419 | break; |
9b118aca MH |
420 | } |
421 | if (!address) { | |
422 | ret = -ENOENT; | |
423 | goto out; | |
424 | } | |
f6c15621 WN |
425 | pr_debug("Symbol %s address found : %" PRIx64 "\n", |
426 | pp->function, address); | |
9b118aca | 427 | |
22a66551 | 428 | ret = debuginfo__find_probe_point(dinfo, address, result); |
9b118aca MH |
429 | if (ret <= 0) |
430 | ret = (!ret) ? -ENOENT : ret; | |
431 | else { | |
432 | result->offset += pp->offset; | |
433 | result->line += pp->line; | |
9d7b45c5 | 434 | result->retprobe = pp->retprobe; |
9b118aca MH |
435 | ret = 0; |
436 | } | |
437 | ||
438 | out: | |
eebc509b | 439 | map__put(map); |
9b118aca MH |
440 | return ret; |
441 | ||
442 | } | |
443 | ||
444 | static int get_alternative_probe_event(struct debuginfo *dinfo, | |
445 | struct perf_probe_event *pev, | |
44225521 | 446 | struct perf_probe_point *tmp) |
9b118aca MH |
447 | { |
448 | int ret; | |
449 | ||
450 | memcpy(tmp, &pev->point, sizeof(*tmp)); | |
451 | memset(&pev->point, 0, sizeof(pev->point)); | |
544abd44 KJ |
452 | ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target, |
453 | pev->nsi, pev->uprobes); | |
9b118aca MH |
454 | if (ret < 0) |
455 | memcpy(&pev->point, tmp, sizeof(*tmp)); | |
456 | ||
457 | return ret; | |
458 | } | |
a15ad2f5 | 459 | |
811dd2ae MH |
460 | static int get_alternative_line_range(struct debuginfo *dinfo, |
461 | struct line_range *lr, | |
462 | const char *target, bool user) | |
463 | { | |
6d4a4896 DA |
464 | struct perf_probe_point pp = { .function = lr->function, |
465 | .file = lr->file, | |
466 | .line = lr->start }; | |
467 | struct perf_probe_point result; | |
811dd2ae MH |
468 | int ret, len = 0; |
469 | ||
6d4a4896 DA |
470 | memset(&result, 0, sizeof(result)); |
471 | ||
811dd2ae MH |
472 | if (lr->end != INT_MAX) |
473 | len = lr->end - lr->start; | |
474 | ret = find_alternative_probe_point(dinfo, &pp, &result, | |
544abd44 | 475 | target, NULL, user); |
811dd2ae MH |
476 | if (!ret) { |
477 | lr->function = result.function; | |
478 | lr->file = result.file; | |
479 | lr->start = result.line; | |
480 | if (lr->end != INT_MAX) | |
481 | lr->end = lr->start + len; | |
482 | clear_perf_probe_point(&pp); | |
483 | } | |
484 | return ret; | |
485 | } | |
486 | ||
7cd5738d MH |
487 | #ifdef HAVE_DEBUGINFOD_SUPPORT |
488 | static struct debuginfo *open_from_debuginfod(struct dso *dso, struct nsinfo *nsi, | |
489 | bool silent) | |
490 | { | |
491 | debuginfod_client *c = debuginfod_begin(); | |
492 | char sbuild_id[SBUILD_ID_SIZE + 1]; | |
493 | struct debuginfo *ret = NULL; | |
494 | struct nscookie nsc; | |
495 | char *path; | |
496 | int fd; | |
497 | ||
498 | if (!c) | |
499 | return NULL; | |
500 | ||
ee756ef7 | 501 | build_id__sprintf(dso__bid(dso), sbuild_id); |
7cd5738d MH |
502 | fd = debuginfod_find_debuginfo(c, (const unsigned char *)sbuild_id, |
503 | 0, &path); | |
504 | if (fd >= 0) | |
505 | close(fd); | |
506 | debuginfod_end(c); | |
507 | if (fd < 0) { | |
508 | if (!silent) | |
509 | pr_debug("Failed to find debuginfo in debuginfod.\n"); | |
510 | return NULL; | |
511 | } | |
512 | if (!silent) | |
513 | pr_debug("Load debuginfo from debuginfod (%s)\n", path); | |
514 | ||
515 | nsinfo__mountns_enter(nsi, &nsc); | |
516 | ret = debuginfo__new((const char *)path); | |
517 | nsinfo__mountns_exit(&nsc); | |
518 | return ret; | |
519 | } | |
520 | #else | |
521 | static inline | |
522 | struct debuginfo *open_from_debuginfod(struct dso *dso __maybe_unused, | |
523 | struct nsinfo *nsi __maybe_unused, | |
524 | bool silent __maybe_unused) | |
525 | { | |
526 | return NULL; | |
527 | } | |
528 | #endif | |
529 | ||
ff741783 | 530 | /* Open new debuginfo of given module */ |
544abd44 KJ |
531 | static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi, |
532 | bool silent) | |
e0faa8d3 | 533 | { |
a15ad2f5 | 534 | const char *path = module; |
419e8738 MH |
535 | char reason[STRERR_BUFSIZE]; |
536 | struct debuginfo *ret = NULL; | |
537 | struct dso *dso = NULL; | |
544abd44 | 538 | struct nscookie nsc; |
419e8738 | 539 | int err; |
ff741783 | 540 | |
a15ad2f5 | 541 | if (!module || !strchr(module, '/')) { |
419e8738 MH |
542 | err = kernel_get_module_dso(module, &dso); |
543 | if (err < 0) { | |
ee756ef7 | 544 | if (!dso || *dso__load_errno(dso) == 0) { |
c8b5f2c9 | 545 | if (!str_error_r(-err, reason, STRERR_BUFSIZE)) |
419e8738 MH |
546 | strcpy(reason, "(unknown)"); |
547 | } else | |
548 | dso__strerror_load(dso, reason, STRERR_BUFSIZE); | |
7cd5738d MH |
549 | if (dso) |
550 | ret = open_from_debuginfod(dso, nsi, silent); | |
551 | if (ret) | |
552 | return ret; | |
4d6101f5 ACM |
553 | if (!silent) { |
554 | if (module) | |
555 | pr_err("Module %s is not loaded, please specify its full path name.\n", module); | |
556 | else | |
557 | pr_err("Failed to find the path for the kernel: %s\n", reason); | |
558 | } | |
14a8fd7c MH |
559 | return NULL; |
560 | } | |
ee756ef7 | 561 | path = dso__long_name(dso); |
e0faa8d3 | 562 | } |
544abd44 | 563 | nsinfo__mountns_enter(nsi, &nsc); |
92561cb7 MH |
564 | ret = debuginfo__new(path); |
565 | if (!ret && !silent) { | |
566 | pr_warning("The %s file has no debug information.\n", path); | |
567 | if (!module || !strtailcmp(path, ".ko")) | |
568 | pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, "); | |
569 | else | |
570 | pr_warning("Rebuild with -g, "); | |
571 | pr_warning("or install an appropriate debuginfo package.\n"); | |
572 | } | |
544abd44 | 573 | nsinfo__mountns_exit(&nsc); |
92561cb7 | 574 | return ret; |
e0faa8d3 | 575 | } |
4b4da7f7 | 576 | |
7737af01 MH |
577 | /* For caching the last debuginfo */ |
578 | static struct debuginfo *debuginfo_cache; | |
579 | static char *debuginfo_cache_path; | |
580 | ||
581 | static struct debuginfo *debuginfo_cache__open(const char *module, bool silent) | |
582 | { | |
20f49859 MH |
583 | const char *path = module; |
584 | ||
585 | /* If the module is NULL, it should be the kernel. */ | |
586 | if (!module) | |
587 | path = "kernel"; | |
588 | ||
589 | if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path)) | |
7737af01 MH |
590 | goto out; |
591 | ||
592 | /* Copy module path */ | |
593 | free(debuginfo_cache_path); | |
20f49859 MH |
594 | debuginfo_cache_path = strdup(path); |
595 | if (!debuginfo_cache_path) { | |
596 | debuginfo__delete(debuginfo_cache); | |
597 | debuginfo_cache = NULL; | |
598 | goto out; | |
7737af01 MH |
599 | } |
600 | ||
544abd44 | 601 | debuginfo_cache = open_debuginfo(module, NULL, silent); |
7737af01 MH |
602 | if (!debuginfo_cache) |
603 | zfree(&debuginfo_cache_path); | |
604 | out: | |
605 | return debuginfo_cache; | |
606 | } | |
607 | ||
608 | static void debuginfo_cache__exit(void) | |
609 | { | |
610 | debuginfo__delete(debuginfo_cache); | |
611 | debuginfo_cache = NULL; | |
612 | zfree(&debuginfo_cache_path); | |
613 | } | |
614 | ||
92561cb7 | 615 | |
22a66551 | 616 | static int get_text_start_address(const char *exec, u64 *address, |
544abd44 | 617 | struct nsinfo *nsi) |
99ca4233 MH |
618 | { |
619 | Elf *elf; | |
620 | GElf_Ehdr ehdr; | |
621 | GElf_Shdr shdr; | |
622 | int fd, ret = -ENOENT; | |
544abd44 | 623 | struct nscookie nsc; |
99ca4233 | 624 | |
544abd44 | 625 | nsinfo__mountns_enter(nsi, &nsc); |
99ca4233 | 626 | fd = open(exec, O_RDONLY); |
544abd44 | 627 | nsinfo__mountns_exit(&nsc); |
99ca4233 MH |
628 | if (fd < 0) |
629 | return -errno; | |
630 | ||
631 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); | |
062d6c2a MH |
632 | if (elf == NULL) { |
633 | ret = -EINVAL; | |
634 | goto out_close; | |
635 | } | |
99ca4233 MH |
636 | |
637 | if (gelf_getehdr(elf, &ehdr) == NULL) | |
638 | goto out; | |
639 | ||
640 | if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL)) | |
641 | goto out; | |
642 | ||
643 | *address = shdr.sh_addr - shdr.sh_offset; | |
644 | ret = 0; | |
645 | out: | |
646 | elf_end(elf); | |
062d6c2a MH |
647 | out_close: |
648 | close(fd); | |
649 | ||
99ca4233 MH |
650 | return ret; |
651 | } | |
652 | ||
5a6f6314 MH |
653 | /* |
654 | * Convert trace point to probe point with debuginfo | |
655 | */ | |
656 | static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp, | |
657 | struct perf_probe_point *pp, | |
658 | bool is_kprobe) | |
659 | { | |
660 | struct debuginfo *dinfo = NULL; | |
22a66551 | 661 | u64 stext = 0; |
5a6f6314 MH |
662 | u64 addr = tp->address; |
663 | int ret = -ENOENT; | |
664 | ||
665 | /* convert the address to dwarf address */ | |
666 | if (!is_kprobe) { | |
667 | if (!addr) { | |
668 | ret = -EINVAL; | |
669 | goto error; | |
670 | } | |
544abd44 | 671 | ret = get_text_start_address(tp->module, &stext, NULL); |
5a6f6314 MH |
672 | if (ret < 0) |
673 | goto error; | |
674 | addr += stext; | |
e486367f | 675 | } else if (tp->symbol) { |
9b239a12 MH |
676 | /* If the module is given, this returns relative address */ |
677 | ret = kernel_get_symbol_address_by_name(tp->symbol, &addr, | |
678 | false, !!tp->module); | |
679 | if (ret != 0) | |
5a6f6314 MH |
680 | goto error; |
681 | addr += tp->offset; | |
682 | } | |
683 | ||
684 | pr_debug("try to find information at %" PRIx64 " in %s\n", addr, | |
685 | tp->module ? : "kernel"); | |
686 | ||
bb963e16 | 687 | dinfo = debuginfo_cache__open(tp->module, verbose <= 0); |
7737af01 | 688 | if (dinfo) |
22a66551 | 689 | ret = debuginfo__find_probe_point(dinfo, addr, pp); |
7737af01 | 690 | else |
5a6f6314 | 691 | ret = -ENOENT; |
5a6f6314 MH |
692 | |
693 | if (ret > 0) { | |
694 | pp->retprobe = tp->retprobe; | |
695 | return 0; | |
696 | } | |
697 | error: | |
698 | pr_debug("Failed to find corresponding probes from debuginfo.\n"); | |
699 | return ret ? : -ENOENT; | |
700 | } | |
701 | ||
3e96dac7 MH |
702 | /* Adjust symbol name and address */ |
703 | static int post_process_probe_trace_point(struct probe_trace_point *tp, | |
22a66551 | 704 | struct map *map, u64 offs) |
3e96dac7 MH |
705 | { |
706 | struct symbol *sym; | |
7598f8bc | 707 | u64 addr = tp->address - offs; |
3e96dac7 MH |
708 | |
709 | sym = map__find_symbol(map, addr); | |
f338de22 MH |
710 | if (!sym) { |
711 | /* | |
712 | * If the address is in the inittext section, map can not | |
713 | * find it. Ignore it if we are probing offline kernel. | |
714 | */ | |
715 | return (symbol_conf.ignore_vmlinux_buildid) ? 0 : -ENOENT; | |
716 | } | |
3e96dac7 MH |
717 | |
718 | if (strcmp(sym->name, tp->symbol)) { | |
719 | /* If we have no realname, use symbol for it */ | |
720 | if (!tp->realname) | |
721 | tp->realname = tp->symbol; | |
722 | else | |
723 | free(tp->symbol); | |
724 | tp->symbol = strdup(sym->name); | |
725 | if (!tp->symbol) | |
726 | return -ENOMEM; | |
727 | } | |
728 | tp->offset = addr - sym->start; | |
729 | tp->address -= offs; | |
730 | ||
731 | return 0; | |
732 | } | |
733 | ||
8a937a25 MH |
734 | /* |
735 | * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions | |
736 | * and generate new symbols with suffixes such as .constprop.N or .isra.N | |
737 | * etc. Since those symbols are not recorded in DWARF, we have to find | |
738 | * correct generated symbols from offline ELF binary. | |
739 | * For online kernel or uprobes we don't need this because those are | |
740 | * rebased on _text, or already a section relative address. | |
741 | */ | |
742 | static int | |
743 | post_process_offline_probe_trace_events(struct probe_trace_event *tevs, | |
744 | int ntevs, const char *pathname) | |
745 | { | |
8a937a25 | 746 | struct map *map; |
22a66551 | 747 | u64 stext = 0; |
3e96dac7 | 748 | int i, ret = 0; |
8a937a25 MH |
749 | |
750 | /* Prepare a map for offline binary */ | |
751 | map = dso__new_map(pathname); | |
544abd44 | 752 | if (!map || get_text_start_address(pathname, &stext, NULL) < 0) { |
8a937a25 MH |
753 | pr_warning("Failed to get ELF symbols for %s\n", pathname); |
754 | return -EINVAL; | |
755 | } | |
756 | ||
757 | for (i = 0; i < ntevs; i++) { | |
3e96dac7 MH |
758 | ret = post_process_probe_trace_point(&tevs[i].point, |
759 | map, stext); | |
760 | if (ret < 0) | |
761 | break; | |
8a937a25 MH |
762 | } |
763 | map__put(map); | |
764 | ||
3e96dac7 | 765 | return ret; |
8a937a25 MH |
766 | } |
767 | ||
fb7345bb | 768 | static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs, |
544abd44 KJ |
769 | int ntevs, const char *exec, |
770 | struct nsinfo *nsi) | |
fb7345bb MH |
771 | { |
772 | int i, ret = 0; | |
22a66551 | 773 | u64 stext = 0; |
fb7345bb MH |
774 | |
775 | if (!exec) | |
776 | return 0; | |
777 | ||
544abd44 | 778 | ret = get_text_start_address(exec, &stext, nsi); |
fb7345bb MH |
779 | if (ret < 0) |
780 | return ret; | |
781 | ||
782 | for (i = 0; i < ntevs && ret >= 0; i++) { | |
adba1634 | 783 | /* point.address is the address of point.symbol + point.offset */ |
eb948e50 | 784 | tevs[i].point.address -= stext; |
fb7345bb | 785 | tevs[i].point.module = strdup(exec); |
eb948e50 | 786 | if (!tevs[i].point.module) { |
fb7345bb MH |
787 | ret = -ENOMEM; |
788 | break; | |
789 | } | |
790 | tevs[i].uprobes = true; | |
791 | } | |
792 | ||
793 | return ret; | |
794 | } | |
795 | ||
613f050d MH |
796 | static int |
797 | post_process_module_probe_trace_events(struct probe_trace_event *tevs, | |
798 | int ntevs, const char *module, | |
799 | struct debuginfo *dinfo) | |
190b57fc | 800 | { |
613f050d | 801 | Dwarf_Addr text_offs = 0; |
14a8fd7c | 802 | int i, ret = 0; |
63a29613 | 803 | char *mod_name = NULL; |
613f050d | 804 | struct map *map; |
14a8fd7c MH |
805 | |
806 | if (!module) | |
807 | return 0; | |
808 | ||
544abd44 | 809 | map = get_target_map(module, NULL, false); |
613f050d MH |
810 | if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) { |
811 | pr_warning("Failed to get ELF symbols for %s\n", module); | |
812 | return -EINVAL; | |
813 | } | |
14a8fd7c | 814 | |
613f050d | 815 | mod_name = find_module_name(module); |
190b57fc | 816 | for (i = 0; i < ntevs; i++) { |
613f050d | 817 | ret = post_process_probe_trace_point(&tevs[i].point, |
22a66551 | 818 | map, text_offs); |
613f050d MH |
819 | if (ret < 0) |
820 | break; | |
63a29613 RB |
821 | tevs[i].point.module = |
822 | strdup(mod_name ? mod_name : module); | |
14a8fd7c MH |
823 | if (!tevs[i].point.module) { |
824 | ret = -ENOMEM; | |
825 | break; | |
826 | } | |
190b57fc | 827 | } |
14a8fd7c | 828 | |
63a29613 | 829 | free(mod_name); |
613f050d MH |
830 | map__put(map); |
831 | ||
14a8fd7c | 832 | return ret; |
190b57fc MH |
833 | } |
834 | ||
d820456d RB |
835 | static int |
836 | post_process_kernel_probe_trace_events(struct probe_trace_event *tevs, | |
837 | int ntevs) | |
dfef99cd MH |
838 | { |
839 | struct ref_reloc_sym *reloc_sym; | |
80526491 | 840 | struct map *map; |
dfef99cd | 841 | char *tmp; |
5a51fcd1 | 842 | int i, skipped = 0; |
dfef99cd | 843 | |
428aff82 MH |
844 | /* Skip post process if the target is an offline kernel */ |
845 | if (symbol_conf.ignore_vmlinux_buildid) | |
8a937a25 MH |
846 | return post_process_offline_probe_trace_events(tevs, ntevs, |
847 | symbol_conf.vmlinux_name); | |
428aff82 | 848 | |
80526491 | 849 | reloc_sym = kernel_get_ref_reloc_sym(&map); |
dfef99cd | 850 | if (!reloc_sym) { |
41ca1d1e RB |
851 | pr_warning("Relocated base symbol is not found! " |
852 | "Check /proc/sys/kernel/kptr_restrict\n" | |
853 | "and /proc/sys/kernel/perf_event_paranoid. " | |
854 | "Or run as privileged perf user.\n\n"); | |
dfef99cd MH |
855 | return -EINVAL; |
856 | } | |
857 | ||
858 | for (i = 0; i < ntevs; i++) { | |
7ab31d94 NR |
859 | if (!tevs[i].point.address) |
860 | continue; | |
861 | if (tevs[i].point.retprobe && !kretprobe_offset_is_supported()) | |
b031220d | 862 | continue; |
80526491 MH |
863 | /* |
864 | * If we found a wrong one, mark it by NULL symbol. | |
865 | * Since addresses in debuginfo is same as objdump, we need | |
866 | * to convert it to addresses on memory. | |
867 | */ | |
b031220d | 868 | if (kprobe_warn_out_range(tevs[i].point.symbol, |
80526491 | 869 | map__objdump_2mem(map, tevs[i].point.address))) { |
b031220d MH |
870 | tmp = NULL; |
871 | skipped++; | |
872 | } else { | |
873 | tmp = strdup(reloc_sym->name); | |
874 | if (!tmp) | |
875 | return -ENOMEM; | |
dfef99cd | 876 | } |
b031220d MH |
877 | /* If we have no realname, use symbol for it */ |
878 | if (!tevs[i].point.realname) | |
879 | tevs[i].point.realname = tevs[i].point.symbol; | |
880 | else | |
881 | free(tevs[i].point.symbol); | |
882 | tevs[i].point.symbol = tmp; | |
883 | tevs[i].point.offset = tevs[i].point.address - | |
2a6e5e8a | 884 | (map__reloc(map) ? reloc_sym->unrelocated_addr : |
ac7a75d1 | 885 | reloc_sym->addr); |
dfef99cd | 886 | } |
5a51fcd1 | 887 | return skipped; |
dfef99cd MH |
888 | } |
889 | ||
99e608b5 RB |
890 | void __weak |
891 | arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused, | |
892 | int ntevs __maybe_unused) | |
893 | { | |
894 | } | |
895 | ||
d820456d | 896 | /* Post processing the probe events */ |
99e608b5 RB |
897 | static int post_process_probe_trace_events(struct perf_probe_event *pev, |
898 | struct probe_trace_event *tevs, | |
d820456d | 899 | int ntevs, const char *module, |
613f050d | 900 | bool uprobe, struct debuginfo *dinfo) |
d820456d | 901 | { |
99e608b5 | 902 | int ret; |
d820456d | 903 | |
99e608b5 | 904 | if (uprobe) |
544abd44 KJ |
905 | ret = add_exec_to_probe_trace_events(tevs, ntevs, module, |
906 | pev->nsi); | |
99e608b5 | 907 | else if (module) |
d820456d | 908 | /* Currently ref_reloc_sym based probe is not for drivers */ |
613f050d MH |
909 | ret = post_process_module_probe_trace_events(tevs, ntevs, |
910 | module, dinfo); | |
99e608b5 RB |
911 | else |
912 | ret = post_process_kernel_probe_trace_events(tevs, ntevs); | |
d820456d | 913 | |
99e608b5 RB |
914 | if (ret >= 0) |
915 | arch__post_process_probe_trace_events(pev, ntevs); | |
916 | ||
917 | return ret; | |
d820456d RB |
918 | } |
919 | ||
4b4da7f7 | 920 | /* Try to find perf_probe_event with debuginfo */ |
0e60836b | 921 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
ddb2f58f | 922 | struct probe_trace_event **tevs) |
4b4da7f7 MH |
923 | { |
924 | bool need_dwarf = perf_probe_event_need_dwarf(pev); | |
9b118aca | 925 | struct perf_probe_point tmp; |
225466f1 | 926 | struct debuginfo *dinfo; |
190b57fc | 927 | int ntevs, ret = 0; |
4b4da7f7 | 928 | |
105f75eb JL |
929 | /* Workaround for gcc #98776 issue. |
930 | * Perf failed to add kretprobe event with debuginfo of vmlinux which is | |
931 | * compiled by gcc with -fpatchable-function-entry option enabled. The | |
932 | * same issue with kernel module. The retprobe doesn`t need debuginfo. | |
933 | * This workaround solution use map to query the probe function address | |
934 | * for retprobe event. | |
935 | */ | |
936 | if (pev->point.retprobe) | |
937 | return 0; | |
938 | ||
544abd44 | 939 | dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf); |
ff741783 | 940 | if (!dinfo) { |
92561cb7 | 941 | if (need_dwarf) |
67ef66ba | 942 | return -ENODATA; |
ff741783 | 943 | pr_debug("Could not open debuginfo. Try to use symbols.\n"); |
4b4da7f7 MH |
944 | return 0; |
945 | } | |
946 | ||
dfef99cd | 947 | pr_debug("Try to find probe point from debuginfo.\n"); |
ff741783 | 948 | /* Searching trace events corresponding to a probe event */ |
ddb2f58f | 949 | ntevs = debuginfo__find_trace_events(dinfo, pev, tevs); |
ff741783 | 950 | |
9b118aca | 951 | if (ntevs == 0) { /* Not found, retry with an alternative */ |
44225521 | 952 | ret = get_alternative_probe_event(dinfo, pev, &tmp); |
9b118aca | 953 | if (!ret) { |
ddb2f58f | 954 | ntevs = debuginfo__find_trace_events(dinfo, pev, tevs); |
9b118aca MH |
955 | /* |
956 | * Write back to the original probe_event for | |
957 | * setting appropriate (user given) event name | |
958 | */ | |
959 | clear_perf_probe_point(&pev->point); | |
960 | memcpy(&pev->point, &tmp, sizeof(tmp)); | |
961 | } | |
962 | } | |
963 | ||
146a1439 | 964 | if (ntevs > 0) { /* Succeeded to find trace events */ |
dfef99cd | 965 | pr_debug("Found %d probe_trace_events.\n", ntevs); |
99e608b5 | 966 | ret = post_process_probe_trace_events(pev, *tevs, ntevs, |
613f050d | 967 | pev->target, pev->uprobes, dinfo); |
5a51fcd1 | 968 | if (ret < 0 || ret == ntevs) { |
613f050d | 969 | pr_debug("Post processing failed or all events are skipped. (%d)\n", ret); |
981d05ad MH |
970 | clear_probe_trace_events(*tevs, ntevs); |
971 | zfree(tevs); | |
613f050d | 972 | ntevs = 0; |
981d05ad | 973 | } |
146a1439 | 974 | } |
4b4da7f7 | 975 | |
613f050d MH |
976 | debuginfo__delete(dinfo); |
977 | ||
146a1439 | 978 | if (ntevs == 0) { /* No error but failed to find probe point. */ |
7bc0153c ACM |
979 | char *probe_point = synthesize_perf_probe_point(&pev->point); |
980 | pr_warning("Probe point '%s' not found.\n", probe_point); | |
981 | free(probe_point); | |
67ef66ba | 982 | return -ENODEV; |
613f050d MH |
983 | } else if (ntevs < 0) { |
984 | /* Error path : ntevs < 0 */ | |
985 | pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); | |
1c0bd0e8 WN |
986 | if (ntevs == -EBADF) |
987 | pr_warning("Warning: No dwarf info found in the vmlinux - " | |
988 | "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n"); | |
15eca306 | 989 | if (!need_dwarf) { |
0e43e5d2 | 990 | pr_debug("Trying to use symbols.\n"); |
15eca306 MH |
991 | return 0; |
992 | } | |
4b4da7f7 | 993 | } |
15eca306 | 994 | return ntevs; |
4b4da7f7 MH |
995 | } |
996 | ||
997 | #define LINEBUF_SIZE 256 | |
998 | #define NR_ADDITIONAL_LINES 2 | |
999 | ||
fde52dbd | 1000 | static int __show_one_line(FILE *fp, int l, bool skip, bool show_num) |
4b4da7f7 | 1001 | { |
5f03cba4 | 1002 | char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE]; |
befe3414 FBH |
1003 | const char *color = show_num ? "" : PERF_COLOR_BLUE; |
1004 | const char *prefix = NULL; | |
4b4da7f7 | 1005 | |
befe3414 | 1006 | do { |
4b4da7f7 MH |
1007 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
1008 | goto error; | |
befe3414 FBH |
1009 | if (skip) |
1010 | continue; | |
1011 | if (!prefix) { | |
1012 | prefix = show_num ? "%7d " : " "; | |
1013 | color_fprintf(stdout, color, prefix, l); | |
4b4da7f7 | 1014 | } |
befe3414 FBH |
1015 | color_fprintf(stdout, color, "%s", buf); |
1016 | ||
1017 | } while (strchr(buf, '\n') == NULL); | |
146a1439 | 1018 | |
fde52dbd | 1019 | return 1; |
4b4da7f7 | 1020 | error: |
fde52dbd | 1021 | if (ferror(fp)) { |
5f03cba4 | 1022 | pr_warning("File read error: %s\n", |
c8b5f2c9 | 1023 | str_error_r(errno, sbuf, sizeof(sbuf))); |
fde52dbd FBH |
1024 | return -1; |
1025 | } | |
1026 | return 0; | |
1027 | } | |
146a1439 | 1028 | |
fde52dbd FBH |
1029 | static int _show_one_line(FILE *fp, int l, bool skip, bool show_num) |
1030 | { | |
1031 | int rv = __show_one_line(fp, l, skip, show_num); | |
1032 | if (rv == 0) { | |
1033 | pr_warning("Source file is shorter than expected.\n"); | |
1034 | rv = -1; | |
1035 | } | |
1036 | return rv; | |
4b4da7f7 MH |
1037 | } |
1038 | ||
fde52dbd FBH |
1039 | #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true) |
1040 | #define show_one_line(f,l) _show_one_line(f,l,false,false) | |
1041 | #define skip_one_line(f,l) _show_one_line(f,l,true,false) | |
1042 | #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false) | |
1043 | ||
4b4da7f7 MH |
1044 | /* |
1045 | * Show line-range always requires debuginfo to find source file and | |
1046 | * line number. | |
1047 | */ | |
811dd2ae MH |
1048 | static int __show_line_range(struct line_range *lr, const char *module, |
1049 | bool user) | |
4b4da7f7 | 1050 | { |
bf541169 | 1051 | struct build_id bid; |
d3b63d7a | 1052 | int l = 1; |
5a62257a | 1053 | struct int_node *ln; |
ff741783 | 1054 | struct debuginfo *dinfo; |
4b4da7f7 | 1055 | FILE *fp; |
ff741783 | 1056 | int ret; |
7cf0b79e | 1057 | char *tmp; |
5f03cba4 | 1058 | char sbuf[STRERR_BUFSIZE]; |
7cd5738d | 1059 | char sbuild_id[SBUILD_ID_SIZE] = ""; |
4b4da7f7 MH |
1060 | |
1061 | /* Search a line range */ | |
544abd44 | 1062 | dinfo = open_debuginfo(module, NULL, false); |
92561cb7 | 1063 | if (!dinfo) |
ff741783 | 1064 | return -ENOENT; |
146a1439 | 1065 | |
ff741783 | 1066 | ret = debuginfo__find_line_range(dinfo, lr); |
811dd2ae MH |
1067 | if (!ret) { /* Not found, retry with an alternative */ |
1068 | ret = get_alternative_line_range(dinfo, lr, module, user); | |
1069 | if (!ret) | |
1070 | ret = debuginfo__find_line_range(dinfo, lr); | |
1071 | } | |
bf541169 JO |
1072 | if (dinfo->build_id) { |
1073 | build_id__init(&bid, dinfo->build_id, BUILD_ID_SIZE); | |
1074 | build_id__sprintf(&bid, sbuild_id); | |
1075 | } | |
ff741783 | 1076 | debuginfo__delete(dinfo); |
5ee05b88 | 1077 | if (ret == 0 || ret == -ENOENT) { |
146a1439 MH |
1078 | pr_warning("Specified source line is not found.\n"); |
1079 | return -ENOENT; | |
1080 | } else if (ret < 0) { | |
5ee05b88 | 1081 | pr_warning("Debuginfo analysis failed.\n"); |
146a1439 MH |
1082 | return ret; |
1083 | } | |
4b4da7f7 | 1084 | |
7cf0b79e MH |
1085 | /* Convert source file path */ |
1086 | tmp = lr->path; | |
7cd5738d | 1087 | ret = find_source_path(tmp, sbuild_id, lr->comp_dir, &lr->path); |
a78604de HK |
1088 | |
1089 | /* Free old path when new path is assigned */ | |
1090 | if (tmp != lr->path) | |
1091 | free(tmp); | |
1092 | ||
7cf0b79e | 1093 | if (ret < 0) { |
5ee05b88 | 1094 | pr_warning("Failed to find source file path.\n"); |
7cf0b79e MH |
1095 | return ret; |
1096 | } | |
1097 | ||
4b4da7f7 MH |
1098 | setup_pager(); |
1099 | ||
1100 | if (lr->function) | |
8737ebde | 1101 | fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path, |
4b4da7f7 MH |
1102 | lr->start - lr->offset); |
1103 | else | |
62c15fc4 | 1104 | fprintf(stdout, "<%s:%d>\n", lr->path, lr->start); |
4b4da7f7 MH |
1105 | |
1106 | fp = fopen(lr->path, "r"); | |
146a1439 MH |
1107 | if (fp == NULL) { |
1108 | pr_warning("Failed to open %s: %s\n", lr->path, | |
c8b5f2c9 | 1109 | str_error_r(errno, sbuf, sizeof(sbuf))); |
146a1439 MH |
1110 | return -errno; |
1111 | } | |
4b4da7f7 | 1112 | /* Skip to starting line number */ |
44b81e92 | 1113 | while (l < lr->start) { |
fde52dbd | 1114 | ret = skip_one_line(fp, l++); |
44b81e92 FBH |
1115 | if (ret < 0) |
1116 | goto end; | |
1117 | } | |
4b4da7f7 | 1118 | |
10daf4d0 | 1119 | intlist__for_each_entry(ln, lr->line_list) { |
94253393 | 1120 | for (; ln->i > (unsigned long)l; l++) { |
fde52dbd | 1121 | ret = show_one_line(fp, l - lr->offset); |
44b81e92 FBH |
1122 | if (ret < 0) |
1123 | goto end; | |
1124 | } | |
fde52dbd | 1125 | ret = show_one_line_with_num(fp, l++ - lr->offset); |
146a1439 MH |
1126 | if (ret < 0) |
1127 | goto end; | |
4b4da7f7 MH |
1128 | } |
1129 | ||
1130 | if (lr->end == INT_MAX) | |
1131 | lr->end = l + NR_ADDITIONAL_LINES; | |
fde52dbd FBH |
1132 | while (l <= lr->end) { |
1133 | ret = show_one_line_or_eof(fp, l++ - lr->offset); | |
1134 | if (ret <= 0) | |
44b81e92 FBH |
1135 | break; |
1136 | } | |
146a1439 | 1137 | end: |
4b4da7f7 | 1138 | fclose(fp); |
146a1439 | 1139 | return ret; |
4b4da7f7 MH |
1140 | } |
1141 | ||
544abd44 KJ |
1142 | int show_line_range(struct line_range *lr, const char *module, |
1143 | struct nsinfo *nsi, bool user) | |
ee45b6c2 MH |
1144 | { |
1145 | int ret; | |
544abd44 | 1146 | struct nscookie nsc; |
ee45b6c2 | 1147 | |
9bae1e8c | 1148 | ret = init_probe_symbol_maps(user); |
ee45b6c2 MH |
1149 | if (ret < 0) |
1150 | return ret; | |
544abd44 | 1151 | nsinfo__mountns_enter(nsi, &nsc); |
811dd2ae | 1152 | ret = __show_line_range(lr, module, user); |
544abd44 | 1153 | nsinfo__mountns_exit(&nsc); |
9bae1e8c | 1154 | exit_probe_symbol_maps(); |
ee45b6c2 MH |
1155 | |
1156 | return ret; | |
1157 | } | |
1158 | ||
ff741783 MH |
1159 | static int show_available_vars_at(struct debuginfo *dinfo, |
1160 | struct perf_probe_event *pev, | |
ddb2f58f | 1161 | struct strfilter *_filter) |
cf6eb489 MH |
1162 | { |
1163 | char *buf; | |
bd09d7b5 | 1164 | int ret, i, nvars; |
cf6eb489 MH |
1165 | struct str_node *node; |
1166 | struct variable_list *vls = NULL, *vl; | |
9b118aca | 1167 | struct perf_probe_point tmp; |
bd09d7b5 | 1168 | const char *var; |
cf6eb489 MH |
1169 | |
1170 | buf = synthesize_perf_probe_point(&pev->point); | |
1171 | if (!buf) | |
1172 | return -EINVAL; | |
1173 | pr_debug("Searching variables at %s\n", buf); | |
1174 | ||
ddb2f58f | 1175 | ret = debuginfo__find_available_vars_at(dinfo, pev, &vls); |
9b118aca | 1176 | if (!ret) { /* Not found, retry with an alternative */ |
44225521 | 1177 | ret = get_alternative_probe_event(dinfo, pev, &tmp); |
9b118aca MH |
1178 | if (!ret) { |
1179 | ret = debuginfo__find_available_vars_at(dinfo, pev, | |
ddb2f58f | 1180 | &vls); |
9b118aca MH |
1181 | /* Release the old probe_point */ |
1182 | clear_perf_probe_point(&tmp); | |
1183 | } | |
1184 | } | |
bd09d7b5 | 1185 | if (ret <= 0) { |
69e96eaa MH |
1186 | if (ret == 0 || ret == -ENOENT) { |
1187 | pr_err("Failed to find the address of %s\n", buf); | |
1188 | ret = -ENOENT; | |
1189 | } else | |
1190 | pr_warning("Debuginfo analysis failed.\n"); | |
bd09d7b5 MH |
1191 | goto end; |
1192 | } | |
69e96eaa | 1193 | |
bd09d7b5 MH |
1194 | /* Some variables are found */ |
1195 | fprintf(stdout, "Available variables at %s\n", buf); | |
1196 | for (i = 0; i < ret; i++) { | |
1197 | vl = &vls[i]; | |
1198 | /* | |
1199 | * A probe point might be converted to | |
1200 | * several trace points. | |
1201 | */ | |
1202 | fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol, | |
1203 | vl->point.offset); | |
74cf249d | 1204 | zfree(&vl->point.symbol); |
bd09d7b5 MH |
1205 | nvars = 0; |
1206 | if (vl->vars) { | |
602a1f4d | 1207 | strlist__for_each_entry(node, vl->vars) { |
bd09d7b5 MH |
1208 | var = strchr(node->s, '\t') + 1; |
1209 | if (strfilter__compare(_filter, var)) { | |
cf6eb489 | 1210 | fprintf(stdout, "\t\t%s\n", node->s); |
bd09d7b5 MH |
1211 | nvars++; |
1212 | } | |
1213 | } | |
1214 | strlist__delete(vl->vars); | |
cf6eb489 | 1215 | } |
bd09d7b5 MH |
1216 | if (nvars == 0) |
1217 | fprintf(stdout, "\t\t(No matched variables)\n"); | |
1218 | } | |
1219 | free(vls); | |
1220 | end: | |
cf6eb489 MH |
1221 | free(buf); |
1222 | return ret; | |
1223 | } | |
1224 | ||
1225 | /* Show available variables on given probe point */ | |
1226 | int show_available_vars(struct perf_probe_event *pevs, int npevs, | |
ddb2f58f | 1227 | struct strfilter *_filter) |
cf6eb489 | 1228 | { |
ff741783 MH |
1229 | int i, ret = 0; |
1230 | struct debuginfo *dinfo; | |
cf6eb489 | 1231 | |
9bae1e8c | 1232 | ret = init_probe_symbol_maps(pevs->uprobes); |
cf6eb489 MH |
1233 | if (ret < 0) |
1234 | return ret; | |
1235 | ||
544abd44 | 1236 | dinfo = open_debuginfo(pevs->target, pevs->nsi, false); |
ff741783 | 1237 | if (!dinfo) { |
ee45b6c2 MH |
1238 | ret = -ENOENT; |
1239 | goto out; | |
ff741783 MH |
1240 | } |
1241 | ||
cf6eb489 MH |
1242 | setup_pager(); |
1243 | ||
ff741783 | 1244 | for (i = 0; i < npevs && ret >= 0; i++) |
ddb2f58f | 1245 | ret = show_available_vars_at(dinfo, &pevs[i], _filter); |
ff741783 MH |
1246 | |
1247 | debuginfo__delete(dinfo); | |
ee45b6c2 | 1248 | out: |
9bae1e8c | 1249 | exit_probe_symbol_maps(); |
cf6eb489 MH |
1250 | return ret; |
1251 | } | |
1252 | ||
89fe808a | 1253 | #else /* !HAVE_DWARF_SUPPORT */ |
4b4da7f7 | 1254 | |
7737af01 MH |
1255 | static void debuginfo_cache__exit(void) |
1256 | { | |
1257 | } | |
1258 | ||
5a6f6314 MH |
1259 | static int |
1260 | find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused, | |
1261 | struct perf_probe_point *pp __maybe_unused, | |
1262 | bool is_kprobe __maybe_unused) | |
4b4da7f7 | 1263 | { |
5a6f6314 | 1264 | return -ENOSYS; |
4b4da7f7 MH |
1265 | } |
1266 | ||
0e60836b | 1267 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
ddb2f58f | 1268 | struct probe_trace_event **tevs __maybe_unused) |
4b4da7f7 | 1269 | { |
146a1439 MH |
1270 | if (perf_probe_event_need_dwarf(pev)) { |
1271 | pr_warning("Debuginfo-analysis is not supported.\n"); | |
1272 | return -ENOSYS; | |
1273 | } | |
225466f1 | 1274 | |
4b4da7f7 MH |
1275 | return 0; |
1276 | } | |
1277 | ||
1d037ca1 | 1278 | int show_line_range(struct line_range *lr __maybe_unused, |
2b394bc4 | 1279 | const char *module __maybe_unused, |
544abd44 | 1280 | struct nsinfo *nsi __maybe_unused, |
2b394bc4 | 1281 | bool user __maybe_unused) |
4b4da7f7 | 1282 | { |
146a1439 MH |
1283 | pr_warning("Debuginfo-analysis is not supported.\n"); |
1284 | return -ENOSYS; | |
4b4da7f7 MH |
1285 | } |
1286 | ||
1d037ca1 | 1287 | int show_available_vars(struct perf_probe_event *pevs __maybe_unused, |
ddb2f58f MH |
1288 | int npevs __maybe_unused, |
1289 | struct strfilter *filter __maybe_unused) | |
cf6eb489 MH |
1290 | { |
1291 | pr_warning("Debuginfo-analysis is not supported.\n"); | |
1292 | return -ENOSYS; | |
1293 | } | |
e0faa8d3 MH |
1294 | #endif |
1295 | ||
e53b00d3 MH |
1296 | void line_range__clear(struct line_range *lr) |
1297 | { | |
d8f9da24 ACM |
1298 | zfree(&lr->function); |
1299 | zfree(&lr->file); | |
1300 | zfree(&lr->path); | |
1301 | zfree(&lr->comp_dir); | |
5a62257a | 1302 | intlist__delete(lr->line_list); |
e53b00d3 MH |
1303 | } |
1304 | ||
5a62257a | 1305 | int line_range__init(struct line_range *lr) |
e53b00d3 MH |
1306 | { |
1307 | memset(lr, 0, sizeof(*lr)); | |
5a62257a MH |
1308 | lr->line_list = intlist__new(NULL); |
1309 | if (!lr->line_list) | |
1310 | return -ENOMEM; | |
1311 | else | |
1312 | return 0; | |
e53b00d3 MH |
1313 | } |
1314 | ||
21dd9ae5 FBH |
1315 | static int parse_line_num(char **ptr, int *val, const char *what) |
1316 | { | |
1317 | const char *start = *ptr; | |
1318 | ||
1319 | errno = 0; | |
1320 | *val = strtol(*ptr, ptr, 0); | |
1321 | if (errno || *ptr == start) { | |
1322 | semantic_error("'%s' is not a valid number.\n", what); | |
1323 | return -EINVAL; | |
1324 | } | |
1325 | return 0; | |
1326 | } | |
1327 | ||
573709fd MH |
1328 | /* Check the name is good for event, group or function */ |
1329 | static bool is_c_func_name(const char *name) | |
1330 | { | |
1331 | if (!isalpha(*name) && *name != '_') | |
1332 | return false; | |
1333 | while (*++name != '\0') { | |
1334 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') | |
1335 | return false; | |
1336 | } | |
1337 | return true; | |
1338 | } | |
1339 | ||
9d95b580 FBH |
1340 | /* |
1341 | * Stuff 'lr' according to the line range described by 'arg'. | |
1342 | * The line range syntax is described by: | |
1343 | * | |
1344 | * SRC[:SLN[+NUM|-ELN]] | |
e116dfa1 | 1345 | * FNC[@SRC][:SLN[+NUM|-ELN]] |
9d95b580 | 1346 | */ |
146a1439 | 1347 | int parse_line_range_desc(const char *arg, struct line_range *lr) |
631c9def | 1348 | { |
e116dfa1 | 1349 | char *range, *file, *name = strdup(arg); |
21dd9ae5 FBH |
1350 | int err; |
1351 | ||
1352 | if (!name) | |
1353 | return -ENOMEM; | |
1354 | ||
1355 | lr->start = 0; | |
1356 | lr->end = INT_MAX; | |
1357 | ||
1358 | range = strchr(name, ':'); | |
1359 | if (range) { | |
1360 | *range++ = '\0'; | |
1361 | ||
1362 | err = parse_line_num(&range, &lr->start, "start line"); | |
1363 | if (err) | |
1364 | goto err; | |
1365 | ||
1366 | if (*range == '+' || *range == '-') { | |
1367 | const char c = *range++; | |
1368 | ||
1369 | err = parse_line_num(&range, &lr->end, "end line"); | |
1370 | if (err) | |
1371 | goto err; | |
1372 | ||
1373 | if (c == '+') { | |
1374 | lr->end += lr->start; | |
1375 | /* | |
1376 | * Adjust the number of lines here. | |
1377 | * If the number of lines == 1, the | |
c69d33eb | 1378 | * end of line should be equal to |
21dd9ae5 FBH |
1379 | * the start of line. |
1380 | */ | |
1381 | lr->end--; | |
1382 | } | |
1383 | } | |
9d95b580 | 1384 | |
d3b63d7a | 1385 | pr_debug("Line range is %d to %d\n", lr->start, lr->end); |
21dd9ae5 FBH |
1386 | |
1387 | err = -EINVAL; | |
d3b63d7a | 1388 | if (lr->start > lr->end) { |
631c9def | 1389 | semantic_error("Start line must be smaller" |
146a1439 | 1390 | " than end line.\n"); |
21dd9ae5 | 1391 | goto err; |
146a1439 | 1392 | } |
21dd9ae5 FBH |
1393 | if (*range != '\0') { |
1394 | semantic_error("Tailing with invalid str '%s'.\n", range); | |
1395 | goto err; | |
146a1439 | 1396 | } |
d3b63d7a | 1397 | } |
02b95dad | 1398 | |
e116dfa1 MH |
1399 | file = strchr(name, '@'); |
1400 | if (file) { | |
1401 | *file = '\0'; | |
1402 | lr->file = strdup(++file); | |
1403 | if (lr->file == NULL) { | |
1404 | err = -ENOMEM; | |
1405 | goto err; | |
1406 | } | |
1407 | lr->function = name; | |
573709fd | 1408 | } else if (strchr(name, '/') || strchr(name, '.')) |
21dd9ae5 | 1409 | lr->file = name; |
573709fd | 1410 | else if (is_c_func_name(name))/* We reuse it for checking funcname */ |
21dd9ae5 | 1411 | lr->function = name; |
573709fd MH |
1412 | else { /* Invalid name */ |
1413 | semantic_error("'%s' is not a valid function name.\n", name); | |
1414 | err = -EINVAL; | |
1415 | goto err; | |
1416 | } | |
146a1439 MH |
1417 | |
1418 | return 0; | |
21dd9ae5 FBH |
1419 | err: |
1420 | free(name); | |
1421 | return err; | |
631c9def MH |
1422 | } |
1423 | ||
36a009fe MH |
1424 | static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev) |
1425 | { | |
1426 | char *ptr; | |
1427 | ||
c588d158 | 1428 | ptr = strpbrk_esc(*arg, ":"); |
36a009fe MH |
1429 | if (ptr) { |
1430 | *ptr = '\0'; | |
42bba263 | 1431 | if (!pev->sdt && !is_c_func_name(*arg)) |
36a009fe | 1432 | goto ng_name; |
c588d158 | 1433 | pev->group = strdup_esc(*arg); |
36a009fe MH |
1434 | if (!pev->group) |
1435 | return -ENOMEM; | |
1436 | *arg = ptr + 1; | |
1437 | } else | |
1438 | pev->group = NULL; | |
c588d158 MH |
1439 | |
1440 | pev->event = strdup_esc(*arg); | |
1441 | if (pev->event == NULL) | |
1442 | return -ENOMEM; | |
1443 | ||
1444 | if (!pev->sdt && !is_c_func_name(pev->event)) { | |
1445 | zfree(&pev->event); | |
36a009fe | 1446 | ng_name: |
c588d158 | 1447 | zfree(&pev->group); |
36a009fe MH |
1448 | semantic_error("%s is bad for event name -it must " |
1449 | "follow C symbol-naming rule.\n", *arg); | |
1450 | return -EINVAL; | |
1451 | } | |
36a009fe MH |
1452 | return 0; |
1453 | } | |
1454 | ||
50656eec | 1455 | /* Parse probepoint definition. */ |
146a1439 | 1456 | static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev) |
50656eec | 1457 | { |
4235b045 | 1458 | struct perf_probe_point *pp = &pev->point; |
50656eec MH |
1459 | char *ptr, *tmp; |
1460 | char c, nc = 0; | |
3099c026 | 1461 | bool file_spec = false; |
36a009fe MH |
1462 | int ret; |
1463 | ||
50656eec MH |
1464 | /* |
1465 | * <Syntax> | |
8d993d96 MH |
1466 | * perf probe [GRP:][EVENT=]SRC[:LN|;PTN] |
1467 | * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] | |
36a009fe | 1468 | * perf probe %[GRP:]SDT_EVENT |
50656eec | 1469 | */ |
e59d29e8 WN |
1470 | if (!arg) |
1471 | return -EINVAL; | |
50656eec | 1472 | |
af9100ad | 1473 | if (is_sdt_event(arg)) { |
36a009fe | 1474 | pev->sdt = true; |
7e9fca51 MH |
1475 | if (arg[0] == '%') |
1476 | arg++; | |
36a009fe MH |
1477 | } |
1478 | ||
c588d158 | 1479 | ptr = strpbrk_esc(arg, ";=@+%"); |
36a009fe | 1480 | if (pev->sdt) { |
8d993d96 | 1481 | if (ptr) { |
a598180a MH |
1482 | if (*ptr != '@') { |
1483 | semantic_error("%s must be an SDT name.\n", | |
1484 | arg); | |
1485 | return -EINVAL; | |
1486 | } | |
1487 | /* This must be a target file name or build id */ | |
1488 | tmp = build_id_cache__complement(ptr + 1); | |
1489 | if (tmp) { | |
1490 | pev->target = build_id_cache__origname(tmp); | |
1491 | free(tmp); | |
1492 | } else | |
c588d158 | 1493 | pev->target = strdup_esc(ptr + 1); |
a598180a MH |
1494 | if (!pev->target) |
1495 | return -ENOMEM; | |
1496 | *ptr = '\0'; | |
146a1439 | 1497 | } |
36a009fe MH |
1498 | ret = parse_perf_probe_event_name(&arg, pev); |
1499 | if (ret == 0) { | |
1500 | if (asprintf(&pev->point.function, "%%%s", pev->event) < 0) | |
1501 | ret = -errno; | |
1502 | } | |
1503 | return ret; | |
1504 | } | |
1505 | ||
1506 | if (ptr && *ptr == '=') { /* Event name */ | |
1507 | *ptr = '\0'; | |
1508 | tmp = ptr + 1; | |
1509 | ret = parse_perf_probe_event_name(&arg, pev); | |
1510 | if (ret < 0) | |
1511 | return ret; | |
1512 | ||
af663d75 MH |
1513 | arg = tmp; |
1514 | } | |
1515 | ||
3099c026 NR |
1516 | /* |
1517 | * Check arg is function or file name and copy it. | |
1518 | * | |
1519 | * We consider arg to be a file spec if and only if it satisfies | |
1520 | * all of the below criteria:: | |
1521 | * - it does not include any of "+@%", | |
1522 | * - it includes one of ":;", and | |
1523 | * - it has a period '.' in the name. | |
1524 | * | |
1525 | * Otherwise, we consider arg to be a function specification. | |
1526 | */ | |
c588d158 MH |
1527 | if (!strpbrk_esc(arg, "+@%")) { |
1528 | ptr = strpbrk_esc(arg, ";:"); | |
3099c026 | 1529 | /* This is a file spec if it includes a '.' before ; or : */ |
c588d158 | 1530 | if (ptr && memchr(arg, '.', ptr - arg)) |
3099c026 NR |
1531 | file_spec = true; |
1532 | } | |
1533 | ||
c588d158 | 1534 | ptr = strpbrk_esc(arg, ";:+@%"); |
50656eec MH |
1535 | if (ptr) { |
1536 | nc = *ptr; | |
1537 | *ptr++ = '\0'; | |
1538 | } | |
1539 | ||
6c6e024f WN |
1540 | if (arg[0] == '\0') |
1541 | tmp = NULL; | |
1542 | else { | |
c588d158 | 1543 | tmp = strdup_esc(arg); |
6c6e024f WN |
1544 | if (tmp == NULL) |
1545 | return -ENOMEM; | |
1546 | } | |
02b95dad | 1547 | |
3099c026 | 1548 | if (file_spec) |
02b95dad | 1549 | pp->file = tmp; |
da15bd9d | 1550 | else { |
02b95dad | 1551 | pp->function = tmp; |
50656eec | 1552 | |
da15bd9d WN |
1553 | /* |
1554 | * Keep pp->function even if this is absolute address, | |
1555 | * so it can mark whether abs_address is valid. | |
1556 | * Which make 'perf probe lib.bin 0x0' possible. | |
1557 | * | |
1558 | * Note that checking length of tmp is not needed | |
1559 | * because when we access tmp[1] we know tmp[0] is '0', | |
1560 | * so tmp[1] should always valid (but could be '\0'). | |
1561 | */ | |
1562 | if (tmp && !strncmp(tmp, "0x", 2)) { | |
22a66551 | 1563 | pp->abs_address = strtoull(pp->function, &tmp, 0); |
da15bd9d WN |
1564 | if (*tmp != '\0') { |
1565 | semantic_error("Invalid absolute address.\n"); | |
1566 | return -EINVAL; | |
1567 | } | |
1568 | } | |
1569 | } | |
1570 | ||
50656eec MH |
1571 | /* Parse other options */ |
1572 | while (ptr) { | |
1573 | arg = ptr; | |
1574 | c = nc; | |
2a9c8c36 | 1575 | if (c == ';') { /* Lazy pattern must be the last part */ |
c588d158 | 1576 | pp->lazy_line = strdup(arg); /* let leave escapes */ |
02b95dad MH |
1577 | if (pp->lazy_line == NULL) |
1578 | return -ENOMEM; | |
2a9c8c36 MH |
1579 | break; |
1580 | } | |
c588d158 | 1581 | ptr = strpbrk_esc(arg, ";:+@%"); |
50656eec MH |
1582 | if (ptr) { |
1583 | nc = *ptr; | |
1584 | *ptr++ = '\0'; | |
1585 | } | |
1586 | switch (c) { | |
1587 | case ':': /* Line number */ | |
1588 | pp->line = strtoul(arg, &tmp, 0); | |
146a1439 | 1589 | if (*tmp != '\0') { |
2a9c8c36 | 1590 | semantic_error("There is non-digit char" |
146a1439 MH |
1591 | " in line number.\n"); |
1592 | return -EINVAL; | |
1593 | } | |
50656eec MH |
1594 | break; |
1595 | case '+': /* Byte offset from a symbol */ | |
1596 | pp->offset = strtoul(arg, &tmp, 0); | |
146a1439 | 1597 | if (*tmp != '\0') { |
2a9c8c36 | 1598 | semantic_error("There is non-digit character" |
146a1439 MH |
1599 | " in offset.\n"); |
1600 | return -EINVAL; | |
1601 | } | |
50656eec MH |
1602 | break; |
1603 | case '@': /* File name */ | |
146a1439 MH |
1604 | if (pp->file) { |
1605 | semantic_error("SRC@SRC is not allowed.\n"); | |
1606 | return -EINVAL; | |
1607 | } | |
c588d158 | 1608 | pp->file = strdup_esc(arg); |
02b95dad MH |
1609 | if (pp->file == NULL) |
1610 | return -ENOMEM; | |
50656eec MH |
1611 | break; |
1612 | case '%': /* Probe places */ | |
1613 | if (strcmp(arg, "return") == 0) { | |
1614 | pp->retprobe = 1; | |
146a1439 MH |
1615 | } else { /* Others not supported yet */ |
1616 | semantic_error("%%%s is not supported.\n", arg); | |
1617 | return -ENOTSUP; | |
1618 | } | |
50656eec | 1619 | break; |
146a1439 MH |
1620 | default: /* Buggy case */ |
1621 | pr_err("This program has a bug at %s:%d.\n", | |
1622 | __FILE__, __LINE__); | |
1623 | return -ENOTSUP; | |
50656eec MH |
1624 | break; |
1625 | } | |
1626 | } | |
1627 | ||
1628 | /* Exclusion check */ | |
146a1439 | 1629 | if (pp->lazy_line && pp->line) { |
0e43e5d2 MH |
1630 | semantic_error("Lazy pattern can't be used with" |
1631 | " line number.\n"); | |
146a1439 MH |
1632 | return -EINVAL; |
1633 | } | |
2a9c8c36 | 1634 | |
146a1439 | 1635 | if (pp->lazy_line && pp->offset) { |
0e43e5d2 | 1636 | semantic_error("Lazy pattern can't be used with offset.\n"); |
146a1439 MH |
1637 | return -EINVAL; |
1638 | } | |
2a9c8c36 | 1639 | |
146a1439 | 1640 | if (pp->line && pp->offset) { |
0e43e5d2 | 1641 | semantic_error("Offset can't be used with line number.\n"); |
146a1439 MH |
1642 | return -EINVAL; |
1643 | } | |
50656eec | 1644 | |
146a1439 | 1645 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { |
2a9c8c36 | 1646 | semantic_error("File always requires line number or " |
0e43e5d2 | 1647 | "lazy pattern.\n"); |
146a1439 MH |
1648 | return -EINVAL; |
1649 | } | |
50656eec | 1650 | |
146a1439 | 1651 | if (pp->offset && !pp->function) { |
0e43e5d2 | 1652 | semantic_error("Offset requires an entry function.\n"); |
146a1439 MH |
1653 | return -EINVAL; |
1654 | } | |
50656eec | 1655 | |
146a1439 | 1656 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { |
2a9c8c36 | 1657 | semantic_error("Offset/Line/Lazy pattern can't be used with " |
0e43e5d2 | 1658 | "return probe.\n"); |
146a1439 MH |
1659 | return -EINVAL; |
1660 | } | |
50656eec | 1661 | |
4235b045 | 1662 | pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", |
2a9c8c36 MH |
1663 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, |
1664 | pp->lazy_line); | |
146a1439 | 1665 | return 0; |
50656eec MH |
1666 | } |
1667 | ||
7df2f329 | 1668 | /* Parse perf-probe event argument */ |
146a1439 | 1669 | static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) |
7df2f329 | 1670 | { |
b2a3c12b | 1671 | char *tmp, *goodname; |
7df2f329 MH |
1672 | struct perf_probe_arg_field **fieldp; |
1673 | ||
1674 | pr_debug("parsing arg: %s into ", str); | |
1675 | ||
48481938 MH |
1676 | tmp = strchr(str, '='); |
1677 | if (tmp) { | |
02b95dad MH |
1678 | arg->name = strndup(str, tmp - str); |
1679 | if (arg->name == NULL) | |
1680 | return -ENOMEM; | |
11a1ca35 | 1681 | pr_debug("name:%s ", arg->name); |
48481938 MH |
1682 | str = tmp + 1; |
1683 | } | |
1684 | ||
1e032f7c | 1685 | tmp = strchr(str, '@'); |
9256c303 | 1686 | if (tmp && tmp != str && !strcmp(tmp + 1, "user")) { /* user attr */ |
1e032f7c MH |
1687 | if (!user_access_is_supported()) { |
1688 | semantic_error("ftrace does not support user access\n"); | |
1689 | return -EINVAL; | |
1690 | } | |
1691 | *tmp = '\0'; | |
1692 | arg->user_access = true; | |
1693 | pr_debug("user_access "); | |
1694 | } | |
1695 | ||
11a1ca35 MH |
1696 | tmp = strchr(str, ':'); |
1697 | if (tmp) { /* Type setting */ | |
1698 | *tmp = '\0'; | |
02b95dad MH |
1699 | arg->type = strdup(tmp + 1); |
1700 | if (arg->type == NULL) | |
1701 | return -ENOMEM; | |
11a1ca35 MH |
1702 | pr_debug("type:%s ", arg->type); |
1703 | } | |
1704 | ||
b2a3c12b | 1705 | tmp = strpbrk(str, "-.["); |
7df2f329 MH |
1706 | if (!is_c_varname(str) || !tmp) { |
1707 | /* A variable, register, symbol or special value */ | |
02b95dad MH |
1708 | arg->var = strdup(str); |
1709 | if (arg->var == NULL) | |
1710 | return -ENOMEM; | |
48481938 | 1711 | pr_debug("%s\n", arg->var); |
146a1439 | 1712 | return 0; |
7df2f329 MH |
1713 | } |
1714 | ||
b2a3c12b | 1715 | /* Structure fields or array element */ |
02b95dad MH |
1716 | arg->var = strndup(str, tmp - str); |
1717 | if (arg->var == NULL) | |
1718 | return -ENOMEM; | |
b2a3c12b | 1719 | goodname = arg->var; |
48481938 | 1720 | pr_debug("%s, ", arg->var); |
7df2f329 MH |
1721 | fieldp = &arg->field; |
1722 | ||
1723 | do { | |
e334016f MH |
1724 | *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); |
1725 | if (*fieldp == NULL) | |
1726 | return -ENOMEM; | |
b2a3c12b MH |
1727 | if (*tmp == '[') { /* Array */ |
1728 | str = tmp; | |
1729 | (*fieldp)->index = strtol(str + 1, &tmp, 0); | |
7df2f329 | 1730 | (*fieldp)->ref = true; |
b2a3c12b MH |
1731 | if (*tmp != ']' || tmp == str + 1) { |
1732 | semantic_error("Array index must be a" | |
1733 | " number.\n"); | |
1734 | return -EINVAL; | |
1735 | } | |
1736 | tmp++; | |
1737 | if (*tmp == '\0') | |
1738 | tmp = NULL; | |
1739 | } else { /* Structure */ | |
1740 | if (*tmp == '.') { | |
1741 | str = tmp + 1; | |
1742 | (*fieldp)->ref = false; | |
1743 | } else if (tmp[1] == '>') { | |
1744 | str = tmp + 2; | |
1745 | (*fieldp)->ref = true; | |
1746 | } else { | |
1747 | semantic_error("Argument parse error: %s\n", | |
1748 | str); | |
1749 | return -EINVAL; | |
1750 | } | |
1751 | tmp = strpbrk(str, "-.["); | |
146a1439 | 1752 | } |
7df2f329 | 1753 | if (tmp) { |
02b95dad MH |
1754 | (*fieldp)->name = strndup(str, tmp - str); |
1755 | if ((*fieldp)->name == NULL) | |
1756 | return -ENOMEM; | |
b2a3c12b MH |
1757 | if (*str != '[') |
1758 | goodname = (*fieldp)->name; | |
7df2f329 MH |
1759 | pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); |
1760 | fieldp = &(*fieldp)->next; | |
1761 | } | |
1762 | } while (tmp); | |
02b95dad MH |
1763 | (*fieldp)->name = strdup(str); |
1764 | if ((*fieldp)->name == NULL) | |
1765 | return -ENOMEM; | |
b2a3c12b MH |
1766 | if (*str != '[') |
1767 | goodname = (*fieldp)->name; | |
7df2f329 | 1768 | pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); |
df0faf4b | 1769 | |
b2a3c12b | 1770 | /* If no name is specified, set the last field name (not array index)*/ |
02b95dad | 1771 | if (!arg->name) { |
b2a3c12b | 1772 | arg->name = strdup(goodname); |
02b95dad MH |
1773 | if (arg->name == NULL) |
1774 | return -ENOMEM; | |
1775 | } | |
146a1439 | 1776 | return 0; |
7df2f329 MH |
1777 | } |
1778 | ||
4235b045 | 1779 | /* Parse perf-probe event command */ |
146a1439 | 1780 | int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) |
50656eec | 1781 | { |
e1c01d61 | 1782 | char **argv; |
146a1439 | 1783 | int argc, i, ret = 0; |
fac13fd5 | 1784 | |
4235b045 | 1785 | argv = argv_split(cmd, &argc); |
146a1439 MH |
1786 | if (!argv) { |
1787 | pr_debug("Failed to split arguments.\n"); | |
1788 | return -ENOMEM; | |
1789 | } | |
1790 | if (argc - 1 > MAX_PROBE_ARGS) { | |
1791 | semantic_error("Too many probe arguments (%d).\n", argc - 1); | |
1792 | ret = -ERANGE; | |
1793 | goto out; | |
1794 | } | |
50656eec | 1795 | /* Parse probe point */ |
146a1439 MH |
1796 | ret = parse_perf_probe_point(argv[0], pev); |
1797 | if (ret < 0) | |
1798 | goto out; | |
50656eec | 1799 | |
15354d54 MH |
1800 | /* Generate event name if needed */ |
1801 | if (!pev->event && pev->point.function && pev->point.line | |
1802 | && !pev->point.lazy_line && !pev->point.offset) { | |
1803 | if (asprintf(&pev->event, "%s_L%d", pev->point.function, | |
4bf6dcaa CJ |
1804 | pev->point.line) < 0) { |
1805 | ret = -ENOMEM; | |
1806 | goto out; | |
1807 | } | |
15354d54 MH |
1808 | } |
1809 | ||
e1c01d61 | 1810 | /* Copy arguments and ensure return probe has no C argument */ |
4235b045 | 1811 | pev->nargs = argc - 1; |
e334016f MH |
1812 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
1813 | if (pev->args == NULL) { | |
1814 | ret = -ENOMEM; | |
1815 | goto out; | |
1816 | } | |
146a1439 MH |
1817 | for (i = 0; i < pev->nargs && ret >= 0; i++) { |
1818 | ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); | |
1819 | if (ret >= 0 && | |
1820 | is_c_varname(pev->args[i].var) && pev->point.retprobe) { | |
4235b045 | 1821 | semantic_error("You can't specify local variable for" |
146a1439 MH |
1822 | " kretprobe.\n"); |
1823 | ret = -EINVAL; | |
1824 | } | |
e1c01d61 | 1825 | } |
146a1439 | 1826 | out: |
e1c01d61 | 1827 | argv_free(argv); |
146a1439 MH |
1828 | |
1829 | return ret; | |
50656eec MH |
1830 | } |
1831 | ||
b3f33f93 RB |
1832 | /* Returns true if *any* ARG is either C variable, $params or $vars. */ |
1833 | bool perf_probe_with_var(struct perf_probe_event *pev) | |
1834 | { | |
1835 | int i = 0; | |
1836 | ||
1837 | for (i = 0; i < pev->nargs; i++) | |
1838 | if (is_c_varname(pev->args[i].var) || | |
1839 | !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) || | |
1840 | !strcmp(pev->args[i].var, PROBE_ARG_VARS)) | |
1841 | return true; | |
1842 | return false; | |
1843 | } | |
1844 | ||
4235b045 MH |
1845 | /* Return true if this perf_probe_event requires debuginfo */ |
1846 | bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) | |
1847 | { | |
4235b045 MH |
1848 | if (pev->point.file || pev->point.line || pev->point.lazy_line) |
1849 | return true; | |
1850 | ||
b3f33f93 RB |
1851 | if (perf_probe_with_var(pev)) |
1852 | return true; | |
4235b045 MH |
1853 | |
1854 | return false; | |
1855 | } | |
1856 | ||
0e60836b | 1857 | /* Parse probe_events event into struct probe_point */ |
92f6c72e | 1858 | int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev) |
4de189fe | 1859 | { |
0e60836b | 1860 | struct probe_trace_point *tp = &tev->point; |
4de189fe MH |
1861 | char pr; |
1862 | char *p; | |
bcbd0040 | 1863 | char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str; |
4de189fe MH |
1864 | int ret, i, argc; |
1865 | char **argv; | |
1866 | ||
0e60836b | 1867 | pr_debug("Parsing probe_events: %s\n", cmd); |
4235b045 | 1868 | argv = argv_split(cmd, &argc); |
146a1439 MH |
1869 | if (!argv) { |
1870 | pr_debug("Failed to split arguments.\n"); | |
1871 | return -ENOMEM; | |
1872 | } | |
1873 | if (argc < 2) { | |
1874 | semantic_error("Too few probe arguments.\n"); | |
1875 | ret = -ERANGE; | |
1876 | goto out; | |
1877 | } | |
4de189fe MH |
1878 | |
1879 | /* Scan event and group name. */ | |
bcbd0040 IT |
1880 | argv0_str = strdup(argv[0]); |
1881 | if (argv0_str == NULL) { | |
1882 | ret = -ENOMEM; | |
1883 | goto out; | |
1884 | } | |
1885 | fmt1_str = strtok_r(argv0_str, ":", &fmt); | |
1886 | fmt2_str = strtok_r(NULL, "/", &fmt); | |
1887 | fmt3_str = strtok_r(NULL, " \t", &fmt); | |
c6aab66a | 1888 | if (fmt1_str == NULL || fmt2_str == NULL || fmt3_str == NULL) { |
146a1439 MH |
1889 | semantic_error("Failed to parse event name: %s\n", argv[0]); |
1890 | ret = -EINVAL; | |
1891 | goto out; | |
1892 | } | |
bcbd0040 IT |
1893 | pr = fmt1_str[0]; |
1894 | tev->group = strdup(fmt2_str); | |
1895 | tev->event = strdup(fmt3_str); | |
1896 | if (tev->group == NULL || tev->event == NULL) { | |
1897 | ret = -ENOMEM; | |
1898 | goto out; | |
1899 | } | |
4235b045 | 1900 | pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); |
4de189fe | 1901 | |
4235b045 | 1902 | tp->retprobe = (pr == 'r'); |
4de189fe | 1903 | |
190b57fc MH |
1904 | /* Scan module name(if there), function name and offset */ |
1905 | p = strchr(argv[1], ':'); | |
1906 | if (p) { | |
1907 | tp->module = strndup(argv[1], p - argv[1]); | |
844faa4b MH |
1908 | if (!tp->module) { |
1909 | ret = -ENOMEM; | |
1910 | goto out; | |
1911 | } | |
42bba263 | 1912 | tev->uprobes = (tp->module[0] == '/'); |
190b57fc MH |
1913 | p++; |
1914 | } else | |
1915 | p = argv[1]; | |
bcbd0040 | 1916 | fmt1_str = strtok_r(p, "+", &fmt); |
be07afe9 WN |
1917 | /* only the address started with 0x */ |
1918 | if (fmt1_str[0] == '0') { | |
1919 | /* | |
1920 | * Fix a special case: | |
1921 | * if address == 0, kernel reports something like: | |
1922 | * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax | |
1923 | * Newer kernel may fix that, but we want to | |
1924 | * support old kernel also. | |
1925 | */ | |
1926 | if (strcmp(fmt1_str, "0x") == 0) { | |
1927 | if (!argv[2] || strcmp(argv[2], "(null)")) { | |
1928 | ret = -EINVAL; | |
1929 | goto out; | |
1930 | } | |
1931 | tp->address = 0; | |
1932 | ||
1933 | free(argv[2]); | |
1934 | for (i = 2; argv[i + 1] != NULL; i++) | |
1935 | argv[i] = argv[i + 1]; | |
1936 | ||
1937 | argv[i] = NULL; | |
1938 | argc -= 1; | |
1939 | } else | |
22a66551 | 1940 | tp->address = strtoull(fmt1_str, NULL, 0); |
be07afe9 | 1941 | } else { |
5a6f6314 MH |
1942 | /* Only the symbol-based probe has offset */ |
1943 | tp->symbol = strdup(fmt1_str); | |
1944 | if (tp->symbol == NULL) { | |
1945 | ret = -ENOMEM; | |
1946 | goto out; | |
1947 | } | |
1948 | fmt2_str = strtok_r(NULL, "", &fmt); | |
1949 | if (fmt2_str == NULL) | |
1950 | tp->offset = 0; | |
1951 | else | |
1952 | tp->offset = strtoul(fmt2_str, NULL, 10); | |
bcbd0040 | 1953 | } |
4de189fe | 1954 | |
5a5e3d3c RB |
1955 | if (tev->uprobes) { |
1956 | fmt2_str = strchr(p, '('); | |
1957 | if (fmt2_str) | |
1958 | tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0); | |
1959 | } | |
1960 | ||
4235b045 | 1961 | tev->nargs = argc - 2; |
0e60836b | 1962 | tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); |
e334016f MH |
1963 | if (tev->args == NULL) { |
1964 | ret = -ENOMEM; | |
1965 | goto out; | |
1966 | } | |
4235b045 | 1967 | for (i = 0; i < tev->nargs; i++) { |
4de189fe MH |
1968 | p = strchr(argv[i + 2], '='); |
1969 | if (p) /* We don't need which register is assigned. */ | |
4235b045 MH |
1970 | *p++ = '\0'; |
1971 | else | |
1972 | p = argv[i + 2]; | |
02b95dad | 1973 | tev->args[i].name = strdup(argv[i + 2]); |
4235b045 | 1974 | /* TODO: parse regs and offset */ |
02b95dad MH |
1975 | tev->args[i].value = strdup(p); |
1976 | if (tev->args[i].name == NULL || tev->args[i].value == NULL) { | |
1977 | ret = -ENOMEM; | |
1978 | goto out; | |
1979 | } | |
4de189fe | 1980 | } |
146a1439 MH |
1981 | ret = 0; |
1982 | out: | |
bcbd0040 | 1983 | free(argv0_str); |
4de189fe | 1984 | argv_free(argv); |
146a1439 | 1985 | return ret; |
4de189fe MH |
1986 | } |
1987 | ||
7df2f329 | 1988 | /* Compose only probe arg */ |
909b0360 | 1989 | char *synthesize_perf_probe_arg(struct perf_probe_arg *pa) |
7df2f329 MH |
1990 | { |
1991 | struct perf_probe_arg_field *field = pa->field; | |
909b0360 | 1992 | struct strbuf buf; |
bf4d5f25 MH |
1993 | char *ret = NULL; |
1994 | int err; | |
1995 | ||
1996 | if (strbuf_init(&buf, 64) < 0) | |
1997 | return NULL; | |
7df2f329 | 1998 | |
48481938 | 1999 | if (pa->name && pa->var) |
bf4d5f25 | 2000 | err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var); |
48481938 | 2001 | else |
bf4d5f25 MH |
2002 | err = strbuf_addstr(&buf, pa->name ?: pa->var); |
2003 | if (err) | |
2004 | goto out; | |
7df2f329 MH |
2005 | |
2006 | while (field) { | |
b2a3c12b | 2007 | if (field->name[0] == '[') |
bf4d5f25 | 2008 | err = strbuf_addstr(&buf, field->name); |
b2a3c12b | 2009 | else |
bf4d5f25 MH |
2010 | err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".", |
2011 | field->name); | |
7df2f329 | 2012 | field = field->next; |
bf4d5f25 MH |
2013 | if (err) |
2014 | goto out; | |
7df2f329 | 2015 | } |
11a1ca35 | 2016 | |
909b0360 | 2017 | if (pa->type) |
bf4d5f25 MH |
2018 | if (strbuf_addf(&buf, ":%s", pa->type) < 0) |
2019 | goto out; | |
909b0360 MH |
2020 | |
2021 | ret = strbuf_detach(&buf, NULL); | |
bf4d5f25 MH |
2022 | out: |
2023 | strbuf_release(&buf); | |
146a1439 | 2024 | return ret; |
7df2f329 MH |
2025 | } |
2026 | ||
4235b045 | 2027 | /* Compose only probe point (not argument) */ |
aeb50d3f | 2028 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp) |
4de189fe | 2029 | { |
909b0360 | 2030 | struct strbuf buf; |
bf4d5f25 MH |
2031 | char *tmp, *ret = NULL; |
2032 | int len, err = 0; | |
2033 | ||
2034 | if (strbuf_init(&buf, 64) < 0) | |
2035 | return NULL; | |
909b0360 | 2036 | |
909b0360 | 2037 | if (pp->function) { |
bf4d5f25 MH |
2038 | if (strbuf_addstr(&buf, pp->function) < 0) |
2039 | goto out; | |
909b0360 | 2040 | if (pp->offset) |
bf4d5f25 | 2041 | err = strbuf_addf(&buf, "+%lu", pp->offset); |
909b0360 | 2042 | else if (pp->line) |
bf4d5f25 | 2043 | err = strbuf_addf(&buf, ":%d", pp->line); |
909b0360 | 2044 | else if (pp->retprobe) |
bf4d5f25 MH |
2045 | err = strbuf_addstr(&buf, "%return"); |
2046 | if (err) | |
2047 | goto out; | |
fb1587d8 MH |
2048 | } |
2049 | if (pp->file) { | |
32ae2ade FBH |
2050 | tmp = pp->file; |
2051 | len = strlen(tmp); | |
2052 | if (len > 30) { | |
2053 | tmp = strchr(pp->file + len - 30, '/'); | |
2054 | tmp = tmp ? tmp + 1 : pp->file + len - 30; | |
2055 | } | |
bf4d5f25 MH |
2056 | err = strbuf_addf(&buf, "@%s", tmp); |
2057 | if (!err && !pp->function && pp->line) | |
2058 | err = strbuf_addf(&buf, ":%d", pp->line); | |
4de189fe | 2059 | } |
bf4d5f25 MH |
2060 | if (!err) |
2061 | ret = strbuf_detach(&buf, NULL); | |
2062 | out: | |
2063 | strbuf_release(&buf); | |
2064 | return ret; | |
7ef17aaf MH |
2065 | } |
2066 | ||
4235b045 | 2067 | char *synthesize_perf_probe_command(struct perf_probe_event *pev) |
7ef17aaf | 2068 | { |
c4ff4920 MH |
2069 | struct strbuf buf; |
2070 | char *tmp, *ret = NULL; | |
2071 | int i; | |
7ef17aaf | 2072 | |
c4ff4920 | 2073 | if (strbuf_init(&buf, 64)) |
4235b045 | 2074 | return NULL; |
c4ff4920 MH |
2075 | if (pev->event) |
2076 | if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP, | |
2077 | pev->event) < 0) | |
2078 | goto out; | |
2079 | ||
2080 | tmp = synthesize_perf_probe_point(&pev->point); | |
a612bbf8 ACM |
2081 | if (!tmp || strbuf_addstr(&buf, tmp) < 0) { |
2082 | free(tmp); | |
c4ff4920 | 2083 | goto out; |
a612bbf8 | 2084 | } |
c4ff4920 | 2085 | free(tmp); |
4de189fe | 2086 | |
4235b045 | 2087 | for (i = 0; i < pev->nargs; i++) { |
c4ff4920 | 2088 | tmp = synthesize_perf_probe_arg(pev->args + i); |
a612bbf8 ACM |
2089 | if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0) { |
2090 | free(tmp); | |
c4ff4920 | 2091 | goto out; |
a612bbf8 | 2092 | } |
c4ff4920 | 2093 | free(tmp); |
4de189fe | 2094 | } |
4de189fe | 2095 | |
c4ff4920 MH |
2096 | ret = strbuf_detach(&buf, NULL); |
2097 | out: | |
2098 | strbuf_release(&buf); | |
2099 | return ret; | |
4235b045 | 2100 | } |
4235b045 | 2101 | |
0e60836b | 2102 | static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref, |
909b0360 | 2103 | struct strbuf *buf, int depth) |
4235b045 | 2104 | { |
bf4d5f25 | 2105 | int err; |
4235b045 | 2106 | if (ref->next) { |
0e60836b | 2107 | depth = __synthesize_probe_trace_arg_ref(ref->next, buf, |
909b0360 | 2108 | depth + 1); |
4235b045 | 2109 | if (depth < 0) |
bf4d5f25 | 2110 | return depth; |
4235b045 | 2111 | } |
9256c303 SK |
2112 | if (ref->user_access) |
2113 | err = strbuf_addf(buf, "%s%ld(", "+u", ref->offset); | |
2114 | else | |
2115 | err = strbuf_addf(buf, "%+ld(", ref->offset); | |
bf4d5f25 | 2116 | return (err < 0) ? err : depth; |
4de189fe MH |
2117 | } |
2118 | ||
0e60836b | 2119 | static int synthesize_probe_trace_arg(struct probe_trace_arg *arg, |
909b0360 | 2120 | struct strbuf *buf) |
50656eec | 2121 | { |
0e60836b | 2122 | struct probe_trace_arg_ref *ref = arg->ref; |
bf4d5f25 | 2123 | int depth = 0, err; |
4235b045 MH |
2124 | |
2125 | /* Argument name or separator */ | |
2126 | if (arg->name) | |
bf4d5f25 | 2127 | err = strbuf_addf(buf, " %s=", arg->name); |
4235b045 | 2128 | else |
bf4d5f25 MH |
2129 | err = strbuf_addch(buf, ' '); |
2130 | if (err) | |
2131 | return err; | |
4235b045 | 2132 | |
b7dcb857 MH |
2133 | /* Special case: @XXX */ |
2134 | if (arg->value[0] == '@' && arg->ref) | |
2135 | ref = ref->next; | |
2136 | ||
4235b045 | 2137 | /* Dereferencing arguments */ |
b7dcb857 | 2138 | if (ref) { |
909b0360 | 2139 | depth = __synthesize_probe_trace_arg_ref(ref, buf, 1); |
4235b045 MH |
2140 | if (depth < 0) |
2141 | return depth; | |
2142 | } | |
2143 | ||
2144 | /* Print argument value */ | |
b7dcb857 | 2145 | if (arg->value[0] == '@' && arg->ref) |
bf4d5f25 | 2146 | err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset); |
b7dcb857 | 2147 | else |
bf4d5f25 | 2148 | err = strbuf_addstr(buf, arg->value); |
4235b045 MH |
2149 | |
2150 | /* Closing */ | |
bf4d5f25 MH |
2151 | while (!err && depth--) |
2152 | err = strbuf_addch(buf, ')'); | |
2153 | ||
4984912e | 2154 | /* Print argument type */ |
bf4d5f25 MH |
2155 | if (!err && arg->type) |
2156 | err = strbuf_addf(buf, ":%s", arg->type); | |
4235b045 | 2157 | |
bf4d5f25 | 2158 | return err; |
4235b045 MH |
2159 | } |
2160 | ||
5a5e3d3c | 2161 | static int |
d26ea481 MH |
2162 | synthesize_probe_trace_args(struct probe_trace_event *tev, struct strbuf *buf) |
2163 | { | |
2164 | int i, ret = 0; | |
2165 | ||
2166 | for (i = 0; i < tev->nargs && ret >= 0; i++) | |
2167 | ret = synthesize_probe_trace_arg(&tev->args[i], buf); | |
2168 | ||
2169 | return ret; | |
2170 | } | |
2171 | ||
2172 | static int | |
2173 | synthesize_uprobe_trace_def(struct probe_trace_point *tp, struct strbuf *buf) | |
5a5e3d3c | 2174 | { |
5a5e3d3c RB |
2175 | int err; |
2176 | ||
d26ea481 MH |
2177 | /* Uprobes must have tp->module */ |
2178 | if (!tp->module) | |
2179 | return -EINVAL; | |
2180 | /* | |
2181 | * If tp->address == 0, then this point must be a | |
2182 | * absolute address uprobe. | |
2183 | * try_to_find_absolute_address() should have made | |
2184 | * tp->symbol to "0x0". | |
2185 | */ | |
2186 | if (!tp->address && (!tp->symbol || strcmp(tp->symbol, "0x0"))) | |
2187 | return -EINVAL; | |
2188 | ||
2189 | /* Use the tp->address for uprobes */ | |
22a66551 | 2190 | err = strbuf_addf(buf, "%s:0x%" PRIx64, tp->module, tp->address); |
5a5e3d3c RB |
2191 | |
2192 | if (err >= 0 && tp->ref_ctr_offset) { | |
2193 | if (!uprobe_ref_ctr_is_supported()) | |
d26ea481 | 2194 | return -EINVAL; |
5a5e3d3c RB |
2195 | err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset); |
2196 | } | |
d26ea481 MH |
2197 | return err >= 0 ? 0 : err; |
2198 | } | |
2199 | ||
2200 | static int | |
2201 | synthesize_kprobe_trace_def(struct probe_trace_point *tp, struct strbuf *buf) | |
2202 | { | |
2203 | if (!strncmp(tp->symbol, "0x", 2)) { | |
2204 | /* Absolute address. See try_to_find_absolute_address() */ | |
22a66551 | 2205 | return strbuf_addf(buf, "%s%s0x%" PRIx64, tp->module ?: "", |
d26ea481 MH |
2206 | tp->module ? ":" : "", tp->address); |
2207 | } else { | |
2208 | return strbuf_addf(buf, "%s%s%s+%lu", tp->module ?: "", | |
2209 | tp->module ? ":" : "", tp->symbol, tp->offset); | |
2210 | } | |
5a5e3d3c RB |
2211 | } |
2212 | ||
0e60836b | 2213 | char *synthesize_probe_trace_command(struct probe_trace_event *tev) |
4235b045 | 2214 | { |
0e60836b | 2215 | struct probe_trace_point *tp = &tev->point; |
909b0360 MH |
2216 | struct strbuf buf; |
2217 | char *ret = NULL; | |
d26ea481 | 2218 | int err; |
909b0360 | 2219 | |
bf4d5f25 MH |
2220 | if (strbuf_init(&buf, 32) < 0) |
2221 | return NULL; | |
2222 | ||
2223 | if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p', | |
2224 | tev->group, tev->event) < 0) | |
2225 | goto error; | |
eb948e50 | 2226 | |
d26ea481 MH |
2227 | if (tev->uprobes) |
2228 | err = synthesize_uprobe_trace_def(tp, &buf); | |
2229 | else | |
2230 | err = synthesize_kprobe_trace_def(tp, &buf); | |
5a5e3d3c | 2231 | |
d26ea481 MH |
2232 | if (err >= 0) |
2233 | err = synthesize_probe_trace_args(tev, &buf); | |
50656eec | 2234 | |
d26ea481 MH |
2235 | if (err >= 0) |
2236 | ret = strbuf_detach(&buf, NULL); | |
50656eec | 2237 | error: |
909b0360 MH |
2238 | strbuf_release(&buf); |
2239 | return ret; | |
4235b045 | 2240 | } |
50656eec | 2241 | |
5a6f6314 MH |
2242 | static int find_perf_probe_point_from_map(struct probe_trace_point *tp, |
2243 | struct perf_probe_point *pp, | |
2244 | bool is_kprobe) | |
2245 | { | |
2246 | struct symbol *sym = NULL; | |
8a2efd6d | 2247 | struct map *map = NULL; |
e486367f | 2248 | u64 addr = tp->address; |
5a6f6314 MH |
2249 | int ret = -ENOENT; |
2250 | ||
2251 | if (!is_kprobe) { | |
2252 | map = dso__new_map(tp->module); | |
2253 | if (!map) | |
2254 | goto out; | |
be39db9f | 2255 | sym = map__find_symbol(map, addr); |
5a6f6314 | 2256 | } else { |
9b239a12 | 2257 | if (tp->symbol && !addr) { |
0a62f686 MH |
2258 | if (kernel_get_symbol_address_by_name(tp->symbol, |
2259 | &addr, true, false) < 0) | |
9b239a12 MH |
2260 | goto out; |
2261 | } | |
5a6f6314 MH |
2262 | if (addr) { |
2263 | addr += tp->offset; | |
107cad95 | 2264 | sym = machine__find_kernel_symbol(host_machine, addr, &map); |
5a6f6314 MH |
2265 | } |
2266 | } | |
98d3b258 | 2267 | |
5a6f6314 MH |
2268 | if (!sym) |
2269 | goto out; | |
2270 | ||
2271 | pp->retprobe = tp->retprobe; | |
78a1f7cd | 2272 | pp->offset = addr - map__unmap_ip(map, sym->start); |
5a6f6314 MH |
2273 | pp->function = strdup(sym->name); |
2274 | ret = pp->function ? 0 : -ENOMEM; | |
2275 | ||
2276 | out: | |
ec42d3d5 | 2277 | map__put(map); |
5a6f6314 MH |
2278 | |
2279 | return ret; | |
2280 | } | |
2281 | ||
2282 | static int convert_to_perf_probe_point(struct probe_trace_point *tp, | |
da15bd9d WN |
2283 | struct perf_probe_point *pp, |
2284 | bool is_kprobe) | |
5a6f6314 MH |
2285 | { |
2286 | char buf[128]; | |
2287 | int ret; | |
2288 | ||
2289 | ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe); | |
2290 | if (!ret) | |
2291 | return 0; | |
2292 | ret = find_perf_probe_point_from_map(tp, pp, is_kprobe); | |
2293 | if (!ret) | |
2294 | return 0; | |
2295 | ||
2296 | pr_debug("Failed to find probe point from both of dwarf and map.\n"); | |
2297 | ||
2298 | if (tp->symbol) { | |
2299 | pp->function = strdup(tp->symbol); | |
2300 | pp->offset = tp->offset; | |
614e2fdb | 2301 | } else { |
22a66551 | 2302 | ret = e_snprintf(buf, 128, "0x%" PRIx64, tp->address); |
5a6f6314 MH |
2303 | if (ret < 0) |
2304 | return ret; | |
2305 | pp->function = strdup(buf); | |
2306 | pp->offset = 0; | |
2307 | } | |
2308 | if (pp->function == NULL) | |
2309 | return -ENOMEM; | |
2310 | ||
2311 | pp->retprobe = tp->retprobe; | |
2312 | ||
2313 | return 0; | |
2314 | } | |
2315 | ||
0e60836b | 2316 | static int convert_to_perf_probe_event(struct probe_trace_event *tev, |
225466f1 | 2317 | struct perf_probe_event *pev, bool is_kprobe) |
4235b045 | 2318 | { |
909b0360 | 2319 | struct strbuf buf = STRBUF_INIT; |
146a1439 | 2320 | int i, ret; |
4235b045 | 2321 | |
4b4da7f7 | 2322 | /* Convert event/group name */ |
02b95dad MH |
2323 | pev->event = strdup(tev->event); |
2324 | pev->group = strdup(tev->group); | |
2325 | if (pev->event == NULL || pev->group == NULL) | |
2326 | return -ENOMEM; | |
fb1587d8 | 2327 | |
4b4da7f7 | 2328 | /* Convert trace_point to probe_point */ |
5a6f6314 | 2329 | ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe); |
146a1439 MH |
2330 | if (ret < 0) |
2331 | return ret; | |
4b4da7f7 | 2332 | |
4235b045 MH |
2333 | /* Convert trace_arg to probe_arg */ |
2334 | pev->nargs = tev->nargs; | |
e334016f MH |
2335 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
2336 | if (pev->args == NULL) | |
2337 | return -ENOMEM; | |
02b95dad | 2338 | for (i = 0; i < tev->nargs && ret >= 0; i++) { |
4235b045 | 2339 | if (tev->args[i].name) |
02b95dad | 2340 | pev->args[i].name = strdup(tev->args[i].name); |
4235b045 | 2341 | else { |
bf4d5f25 MH |
2342 | if ((ret = strbuf_init(&buf, 32)) < 0) |
2343 | goto error; | |
909b0360 MH |
2344 | ret = synthesize_probe_trace_arg(&tev->args[i], &buf); |
2345 | pev->args[i].name = strbuf_detach(&buf, NULL); | |
4235b045 | 2346 | } |
02b95dad MH |
2347 | if (pev->args[i].name == NULL && ret >= 0) |
2348 | ret = -ENOMEM; | |
2349 | } | |
bf4d5f25 | 2350 | error: |
146a1439 MH |
2351 | if (ret < 0) |
2352 | clear_perf_probe_event(pev); | |
2353 | ||
2354 | return ret; | |
4235b045 MH |
2355 | } |
2356 | ||
2357 | void clear_perf_probe_event(struct perf_probe_event *pev) | |
2358 | { | |
7df2f329 | 2359 | struct perf_probe_arg_field *field, *next; |
4235b045 MH |
2360 | int i; |
2361 | ||
d8f9da24 ACM |
2362 | zfree(&pev->event); |
2363 | zfree(&pev->group); | |
2364 | zfree(&pev->target); | |
9b118aca | 2365 | clear_perf_probe_point(&pev->point); |
f5385650 | 2366 | |
7df2f329 | 2367 | for (i = 0; i < pev->nargs; i++) { |
d8f9da24 ACM |
2368 | zfree(&pev->args[i].name); |
2369 | zfree(&pev->args[i].var); | |
2370 | zfree(&pev->args[i].type); | |
7df2f329 MH |
2371 | field = pev->args[i].field; |
2372 | while (field) { | |
2373 | next = field->next; | |
74cf249d | 2374 | zfree(&field->name); |
7df2f329 MH |
2375 | free(field); |
2376 | field = next; | |
2377 | } | |
2378 | } | |
df8350ed | 2379 | pev->nargs = 0; |
d8f9da24 | 2380 | zfree(&pev->args); |
4235b045 MH |
2381 | } |
2382 | ||
0542bb9c MH |
2383 | #define strdup_or_goto(str, label) \ |
2384 | ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; }) | |
2385 | ||
2386 | static int perf_probe_point__copy(struct perf_probe_point *dst, | |
2387 | struct perf_probe_point *src) | |
2388 | { | |
2389 | dst->file = strdup_or_goto(src->file, out_err); | |
2390 | dst->function = strdup_or_goto(src->function, out_err); | |
2391 | dst->lazy_line = strdup_or_goto(src->lazy_line, out_err); | |
2392 | dst->line = src->line; | |
2393 | dst->retprobe = src->retprobe; | |
2394 | dst->offset = src->offset; | |
2395 | return 0; | |
2396 | ||
2397 | out_err: | |
2398 | clear_perf_probe_point(dst); | |
2399 | return -ENOMEM; | |
2400 | } | |
2401 | ||
2402 | static int perf_probe_arg__copy(struct perf_probe_arg *dst, | |
2403 | struct perf_probe_arg *src) | |
2404 | { | |
2405 | struct perf_probe_arg_field *field, **ppfield; | |
2406 | ||
2407 | dst->name = strdup_or_goto(src->name, out_err); | |
2408 | dst->var = strdup_or_goto(src->var, out_err); | |
2409 | dst->type = strdup_or_goto(src->type, out_err); | |
2410 | ||
2411 | field = src->field; | |
2412 | ppfield = &(dst->field); | |
2413 | while (field) { | |
2414 | *ppfield = zalloc(sizeof(*field)); | |
2415 | if (!*ppfield) | |
2416 | goto out_err; | |
2417 | (*ppfield)->name = strdup_or_goto(field->name, out_err); | |
2418 | (*ppfield)->index = field->index; | |
2419 | (*ppfield)->ref = field->ref; | |
2420 | field = field->next; | |
2421 | ppfield = &((*ppfield)->next); | |
2422 | } | |
2423 | return 0; | |
2424 | out_err: | |
2425 | return -ENOMEM; | |
2426 | } | |
2427 | ||
2428 | int perf_probe_event__copy(struct perf_probe_event *dst, | |
2429 | struct perf_probe_event *src) | |
2430 | { | |
2431 | int i; | |
2432 | ||
2433 | dst->event = strdup_or_goto(src->event, out_err); | |
2434 | dst->group = strdup_or_goto(src->group, out_err); | |
2435 | dst->target = strdup_or_goto(src->target, out_err); | |
2436 | dst->uprobes = src->uprobes; | |
2437 | ||
2438 | if (perf_probe_point__copy(&dst->point, &src->point) < 0) | |
2439 | goto out_err; | |
2440 | ||
2441 | dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs); | |
2442 | if (!dst->args) | |
2443 | goto out_err; | |
2444 | dst->nargs = src->nargs; | |
2445 | ||
2446 | for (i = 0; i < src->nargs; i++) | |
2447 | if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0) | |
2448 | goto out_err; | |
2449 | return 0; | |
2450 | ||
2451 | out_err: | |
2452 | clear_perf_probe_event(dst); | |
2453 | return -ENOMEM; | |
2454 | } | |
2455 | ||
92f6c72e | 2456 | void clear_probe_trace_event(struct probe_trace_event *tev) |
4235b045 | 2457 | { |
0e60836b | 2458 | struct probe_trace_arg_ref *ref, *next; |
4235b045 MH |
2459 | int i; |
2460 | ||
d8f9da24 ACM |
2461 | zfree(&tev->event); |
2462 | zfree(&tev->group); | |
2463 | zfree(&tev->point.symbol); | |
2464 | zfree(&tev->point.realname); | |
2465 | zfree(&tev->point.module); | |
4235b045 | 2466 | for (i = 0; i < tev->nargs; i++) { |
d8f9da24 ACM |
2467 | zfree(&tev->args[i].name); |
2468 | zfree(&tev->args[i].value); | |
2469 | zfree(&tev->args[i].type); | |
4235b045 MH |
2470 | ref = tev->args[i].ref; |
2471 | while (ref) { | |
2472 | next = ref->next; | |
2473 | free(ref); | |
2474 | ref = next; | |
2475 | } | |
2476 | } | |
d8f9da24 | 2477 | zfree(&tev->args); |
9e6124d9 | 2478 | tev->nargs = 0; |
50656eec MH |
2479 | } |
2480 | ||
9aaf5a5f MH |
2481 | struct kprobe_blacklist_node { |
2482 | struct list_head list; | |
22a66551 YJ |
2483 | u64 start; |
2484 | u64 end; | |
9aaf5a5f MH |
2485 | char *symbol; |
2486 | }; | |
2487 | ||
2488 | static void kprobe_blacklist__delete(struct list_head *blacklist) | |
2489 | { | |
2490 | struct kprobe_blacklist_node *node; | |
2491 | ||
2492 | while (!list_empty(blacklist)) { | |
2493 | node = list_first_entry(blacklist, | |
2494 | struct kprobe_blacklist_node, list); | |
e56fbc9d | 2495 | list_del_init(&node->list); |
d8f9da24 | 2496 | zfree(&node->symbol); |
9aaf5a5f MH |
2497 | free(node); |
2498 | } | |
2499 | } | |
2500 | ||
2501 | static int kprobe_blacklist__load(struct list_head *blacklist) | |
2502 | { | |
2503 | struct kprobe_blacklist_node *node; | |
4605eab3 | 2504 | const char *__debugfs = debugfs__mountpoint(); |
9aaf5a5f MH |
2505 | char buf[PATH_MAX], *p; |
2506 | FILE *fp; | |
2507 | int ret; | |
2508 | ||
2509 | if (__debugfs == NULL) | |
2510 | return -ENOTSUP; | |
2511 | ||
2512 | ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs); | |
2513 | if (ret < 0) | |
2514 | return ret; | |
2515 | ||
2516 | fp = fopen(buf, "r"); | |
2517 | if (!fp) | |
2518 | return -errno; | |
2519 | ||
2520 | ret = 0; | |
2521 | while (fgets(buf, PATH_MAX, fp)) { | |
2522 | node = zalloc(sizeof(*node)); | |
2523 | if (!node) { | |
2524 | ret = -ENOMEM; | |
2525 | break; | |
2526 | } | |
2527 | INIT_LIST_HEAD(&node->list); | |
2528 | list_add_tail(&node->list, blacklist); | |
22a66551 | 2529 | if (sscanf(buf, "0x%" PRIx64 "-0x%" PRIx64, &node->start, &node->end) != 2) { |
9aaf5a5f MH |
2530 | ret = -EINVAL; |
2531 | break; | |
2532 | } | |
2533 | p = strchr(buf, '\t'); | |
2534 | if (p) { | |
2535 | p++; | |
2536 | if (p[strlen(p) - 1] == '\n') | |
2537 | p[strlen(p) - 1] = '\0'; | |
2538 | } else | |
2539 | p = (char *)"unknown"; | |
2540 | node->symbol = strdup(p); | |
2541 | if (!node->symbol) { | |
2542 | ret = -ENOMEM; | |
2543 | break; | |
2544 | } | |
22a66551 | 2545 | pr_debug2("Blacklist: 0x%" PRIx64 "-0x%" PRIx64 ", %s\n", |
9aaf5a5f MH |
2546 | node->start, node->end, node->symbol); |
2547 | ret++; | |
2548 | } | |
2549 | if (ret < 0) | |
2550 | kprobe_blacklist__delete(blacklist); | |
2551 | fclose(fp); | |
2552 | ||
2553 | return ret; | |
2554 | } | |
2555 | ||
2556 | static struct kprobe_blacklist_node * | |
22a66551 | 2557 | kprobe_blacklist__find_by_address(struct list_head *blacklist, u64 address) |
9aaf5a5f MH |
2558 | { |
2559 | struct kprobe_blacklist_node *node; | |
2560 | ||
2561 | list_for_each_entry(node, blacklist, list) { | |
2c29461e | 2562 | if (node->start <= address && address < node->end) |
9aaf5a5f MH |
2563 | return node; |
2564 | } | |
2565 | ||
2566 | return NULL; | |
2567 | } | |
2568 | ||
b031220d MH |
2569 | static LIST_HEAD(kprobe_blacklist); |
2570 | ||
2571 | static void kprobe_blacklist__init(void) | |
2572 | { | |
2573 | if (!list_empty(&kprobe_blacklist)) | |
2574 | return; | |
2575 | ||
2576 | if (kprobe_blacklist__load(&kprobe_blacklist) < 0) | |
2577 | pr_debug("No kprobe blacklist support, ignored\n"); | |
2578 | } | |
2579 | ||
2580 | static void kprobe_blacklist__release(void) | |
2581 | { | |
2582 | kprobe_blacklist__delete(&kprobe_blacklist); | |
2583 | } | |
2584 | ||
22a66551 | 2585 | static bool kprobe_blacklist__listed(u64 address) |
b031220d MH |
2586 | { |
2587 | return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address); | |
2588 | } | |
2589 | ||
d350bd57 MH |
2590 | static int perf_probe_event__sprintf(const char *group, const char *event, |
2591 | struct perf_probe_event *pev, | |
ba7ecb02 MH |
2592 | const char *module, |
2593 | struct strbuf *result) | |
278498d4 | 2594 | { |
bf4d5f25 | 2595 | int i, ret; |
909b0360 | 2596 | char *buf; |
278498d4 | 2597 | |
909b0360 MH |
2598 | if (asprintf(&buf, "%s:%s", group, event) < 0) |
2599 | return -errno; | |
bf4d5f25 | 2600 | ret = strbuf_addf(result, " %-20s (on ", buf); |
909b0360 | 2601 | free(buf); |
bf4d5f25 MH |
2602 | if (ret) |
2603 | return ret; | |
4235b045 | 2604 | |
909b0360 MH |
2605 | /* Synthesize only event probe point */ |
2606 | buf = synthesize_perf_probe_point(&pev->point); | |
2607 | if (!buf) | |
2608 | return -ENOMEM; | |
bf4d5f25 | 2609 | ret = strbuf_addstr(result, buf); |
909b0360 | 2610 | free(buf); |
146a1439 | 2611 | |
bf4d5f25 MH |
2612 | if (!ret && module) |
2613 | ret = strbuf_addf(result, " in %s", module); | |
278498d4 | 2614 | |
bf4d5f25 MH |
2615 | if (!ret && pev->nargs > 0) { |
2616 | ret = strbuf_add(result, " with", 5); | |
2617 | for (i = 0; !ret && i < pev->nargs; i++) { | |
909b0360 MH |
2618 | buf = synthesize_perf_probe_arg(&pev->args[i]); |
2619 | if (!buf) | |
2620 | return -ENOMEM; | |
bf4d5f25 | 2621 | ret = strbuf_addf(result, " %s", buf); |
909b0360 | 2622 | free(buf); |
7df2f329 | 2623 | } |
278498d4 | 2624 | } |
bf4d5f25 MH |
2625 | if (!ret) |
2626 | ret = strbuf_addch(result, ')'); | |
909b0360 | 2627 | |
bf4d5f25 | 2628 | return ret; |
278498d4 MH |
2629 | } |
2630 | ||
ba7ecb02 | 2631 | /* Show an event */ |
b02137cc NK |
2632 | int show_perf_probe_event(const char *group, const char *event, |
2633 | struct perf_probe_event *pev, | |
2634 | const char *module, bool use_stdout) | |
ba7ecb02 MH |
2635 | { |
2636 | struct strbuf buf = STRBUF_INIT; | |
2637 | int ret; | |
2638 | ||
d350bd57 | 2639 | ret = perf_probe_event__sprintf(group, event, pev, module, &buf); |
ba7ecb02 MH |
2640 | if (ret >= 0) { |
2641 | if (use_stdout) | |
2642 | printf("%s\n", buf.buf); | |
2643 | else | |
2644 | pr_info("%s\n", buf.buf); | |
2645 | } | |
2646 | strbuf_release(&buf); | |
2647 | ||
2648 | return ret; | |
2649 | } | |
2650 | ||
b6a89643 MH |
2651 | static bool filter_probe_trace_event(struct probe_trace_event *tev, |
2652 | struct strfilter *filter) | |
2653 | { | |
2654 | char tmp[128]; | |
2655 | ||
2656 | /* At first, check the event name itself */ | |
2657 | if (strfilter__compare(filter, tev->event)) | |
2658 | return true; | |
2659 | ||
2660 | /* Next, check the combination of name and group */ | |
2661 | if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0) | |
2662 | return false; | |
2663 | return strfilter__compare(filter, tmp); | |
2664 | } | |
2665 | ||
2666 | static int __show_perf_probe_events(int fd, bool is_kprobe, | |
2667 | struct strfilter *filter) | |
4de189fe | 2668 | { |
225466f1 | 2669 | int ret = 0; |
0e60836b | 2670 | struct probe_trace_event tev; |
4235b045 | 2671 | struct perf_probe_event pev; |
4de189fe MH |
2672 | struct strlist *rawlist; |
2673 | struct str_node *ent; | |
2674 | ||
4235b045 MH |
2675 | memset(&tev, 0, sizeof(tev)); |
2676 | memset(&pev, 0, sizeof(pev)); | |
72041334 | 2677 | |
92f6c72e | 2678 | rawlist = probe_file__get_rawlist(fd); |
146a1439 | 2679 | if (!rawlist) |
6eb08660 | 2680 | return -ENOMEM; |
4de189fe | 2681 | |
602a1f4d | 2682 | strlist__for_each_entry(ent, rawlist) { |
0e60836b | 2683 | ret = parse_probe_trace_command(ent->s, &tev); |
146a1439 | 2684 | if (ret >= 0) { |
b6a89643 MH |
2685 | if (!filter_probe_trace_event(&tev, filter)) |
2686 | goto next; | |
225466f1 SD |
2687 | ret = convert_to_perf_probe_event(&tev, &pev, |
2688 | is_kprobe); | |
ba7ecb02 MH |
2689 | if (ret < 0) |
2690 | goto next; | |
d350bd57 MH |
2691 | ret = show_perf_probe_event(pev.group, pev.event, |
2692 | &pev, tev.point.module, | |
ba7ecb02 | 2693 | true); |
146a1439 | 2694 | } |
b6a89643 | 2695 | next: |
4235b045 | 2696 | clear_perf_probe_event(&pev); |
0e60836b | 2697 | clear_probe_trace_event(&tev); |
146a1439 MH |
2698 | if (ret < 0) |
2699 | break; | |
4de189fe | 2700 | } |
4de189fe | 2701 | strlist__delete(rawlist); |
7737af01 MH |
2702 | /* Cleanup cached debuginfo if needed */ |
2703 | debuginfo_cache__exit(); | |
146a1439 MH |
2704 | |
2705 | return ret; | |
4de189fe MH |
2706 | } |
2707 | ||
225466f1 | 2708 | /* List up current perf-probe events */ |
b6a89643 | 2709 | int show_perf_probe_events(struct strfilter *filter) |
225466f1 | 2710 | { |
5e45187c | 2711 | int kp_fd, up_fd, ret; |
225466f1 SD |
2712 | |
2713 | setup_pager(); | |
225466f1 | 2714 | |
1f3736c9 MH |
2715 | if (probe_conf.cache) |
2716 | return probe_cache__show_all_caches(filter); | |
2717 | ||
9bae1e8c | 2718 | ret = init_probe_symbol_maps(false); |
225466f1 SD |
2719 | if (ret < 0) |
2720 | return ret; | |
2721 | ||
92f6c72e MH |
2722 | ret = probe_file__open_both(&kp_fd, &up_fd, 0); |
2723 | if (ret < 0) | |
2724 | return ret; | |
225466f1 | 2725 | |
92f6c72e MH |
2726 | if (kp_fd >= 0) |
2727 | ret = __show_perf_probe_events(kp_fd, true, filter); | |
2728 | if (up_fd >= 0 && ret >= 0) | |
b6a89643 | 2729 | ret = __show_perf_probe_events(up_fd, false, filter); |
92f6c72e MH |
2730 | if (kp_fd > 0) |
2731 | close(kp_fd); | |
2732 | if (up_fd > 0) | |
5e45187c | 2733 | close(up_fd); |
9bae1e8c | 2734 | exit_probe_symbol_maps(); |
225466f1 | 2735 | |
146a1439 | 2736 | return ret; |
50656eec MH |
2737 | } |
2738 | ||
146a1439 | 2739 | static int get_new_event_name(char *buf, size_t len, const char *base, |
e63c625a MH |
2740 | struct strlist *namelist, bool ret_event, |
2741 | bool allow_suffix) | |
b498ce1f MH |
2742 | { |
2743 | int i, ret; | |
663b1151 | 2744 | char *p, *nbase; |
17f88fcd | 2745 | |
3099c026 NR |
2746 | if (*base == '.') |
2747 | base++; | |
663b1151 MH |
2748 | nbase = strdup(base); |
2749 | if (!nbase) | |
2750 | return -ENOMEM; | |
2751 | ||
a3110cd9 MH |
2752 | /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */ |
2753 | p = strpbrk(nbase, ".@"); | |
663b1151 MH |
2754 | if (p && p != nbase) |
2755 | *p = '\0'; | |
3099c026 | 2756 | |
663b1151 | 2757 | /* Try no suffix number */ |
e63c625a | 2758 | ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : ""); |
146a1439 | 2759 | if (ret < 0) { |
c15ed444 | 2760 | pr_warning("snprintf() failed: %d; the event name nbase='%s' is too long\n", ret, nbase); |
663b1151 | 2761 | goto out; |
146a1439 | 2762 | } |
17f88fcd | 2763 | if (!strlist__has_entry(namelist, buf)) |
663b1151 | 2764 | goto out; |
17f88fcd | 2765 | |
d761b08b | 2766 | if (!allow_suffix) { |
03e01f56 WN |
2767 | pr_warning("Error: event \"%s\" already exists.\n" |
2768 | " Hint: Remove existing event by 'perf probe -d'\n" | |
2769 | " or force duplicates by 'perf probe -f'\n" | |
2770 | " or set 'force=yes' in BPF source.\n", | |
2771 | buf); | |
663b1151 MH |
2772 | ret = -EEXIST; |
2773 | goto out; | |
d761b08b MH |
2774 | } |
2775 | ||
17f88fcd MH |
2776 | /* Try to add suffix */ |
2777 | for (i = 1; i < MAX_EVENT_INDEX; i++) { | |
663b1151 | 2778 | ret = e_snprintf(buf, len, "%s_%d", nbase, i); |
146a1439 | 2779 | if (ret < 0) { |
5f03cba4 | 2780 | pr_debug("snprintf() failed: %d\n", ret); |
663b1151 | 2781 | goto out; |
146a1439 | 2782 | } |
b498ce1f MH |
2783 | if (!strlist__has_entry(namelist, buf)) |
2784 | break; | |
2785 | } | |
146a1439 MH |
2786 | if (i == MAX_EVENT_INDEX) { |
2787 | pr_warning("Too many events are on the same function.\n"); | |
2788 | ret = -ERANGE; | |
2789 | } | |
2790 | ||
663b1151 MH |
2791 | out: |
2792 | free(nbase); | |
9f5c6d87 MH |
2793 | |
2794 | /* Final validation */ | |
2795 | if (ret >= 0 && !is_c_func_name(buf)) { | |
2796 | pr_warning("Internal error: \"%s\" is an invalid event name.\n", | |
2797 | buf); | |
2798 | ret = -EINVAL; | |
2799 | } | |
2800 | ||
146a1439 | 2801 | return ret; |
b498ce1f MH |
2802 | } |
2803 | ||
79702f61 MH |
2804 | /* Warn if the current kernel's uprobe implementation is old */ |
2805 | static void warn_uprobe_event_compat(struct probe_trace_event *tev) | |
2806 | { | |
2807 | int i; | |
2808 | char *buf = synthesize_probe_trace_command(tev); | |
5a5e3d3c RB |
2809 | struct probe_trace_point *tp = &tev->point; |
2810 | ||
2811 | if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) { | |
2812 | pr_warning("A semaphore is associated with %s:%s and " | |
2813 | "seems your kernel doesn't support it.\n", | |
2814 | tev->group, tev->event); | |
2815 | } | |
79702f61 MH |
2816 | |
2817 | /* Old uprobe event doesn't support memory dereference */ | |
2818 | if (!tev->uprobes || tev->nargs == 0 || !buf) | |
2819 | goto out; | |
2820 | ||
e8ca4f0f MH |
2821 | for (i = 0; i < tev->nargs; i++) { |
2822 | if (strchr(tev->args[i].value, '@')) { | |
2823 | pr_warning("%s accesses a variable by symbol name, but that is not supported for user application probe.\n", | |
79702f61 MH |
2824 | tev->args[i].value); |
2825 | break; | |
2826 | } | |
e8ca4f0f MH |
2827 | if (strglobmatch(tev->args[i].value, "[$+-]*")) { |
2828 | pr_warning("Please upgrade your kernel to at least 3.14 to have access to feature %s\n", | |
2829 | tev->args[i].value); | |
2830 | break; | |
2831 | } | |
2832 | } | |
79702f61 MH |
2833 | out: |
2834 | free(buf); | |
2835 | } | |
2836 | ||
a3c9de62 MH |
2837 | /* Set new name from original perf_probe_event and namelist */ |
2838 | static int probe_trace_event__set_name(struct probe_trace_event *tev, | |
2839 | struct perf_probe_event *pev, | |
2840 | struct strlist *namelist, | |
2841 | bool allow_suffix) | |
2842 | { | |
2843 | const char *event, *group; | |
2844 | char buf[64]; | |
2845 | int ret; | |
2846 | ||
bc062230 | 2847 | /* If probe_event or trace_event already have the name, reuse it */ |
42bba263 | 2848 | if (pev->event && !pev->sdt) |
a3c9de62 | 2849 | event = pev->event; |
bc062230 MH |
2850 | else if (tev->event) |
2851 | event = tev->event; | |
2852 | else { | |
2853 | /* Or generate new one from probe point */ | |
da15bd9d WN |
2854 | if (pev->point.function && |
2855 | (strncmp(pev->point.function, "0x", 2) != 0) && | |
2856 | !strisglob(pev->point.function)) | |
a3c9de62 MH |
2857 | event = pev->point.function; |
2858 | else | |
2859 | event = tev->point.realname; | |
bc062230 | 2860 | } |
42bba263 | 2861 | if (pev->group && !pev->sdt) |
a3c9de62 | 2862 | group = pev->group; |
bc062230 MH |
2863 | else if (tev->group) |
2864 | group = tev->group; | |
a3c9de62 MH |
2865 | else |
2866 | group = PERFPROBE_GROUP; | |
2867 | ||
2868 | /* Get an unused new event name */ | |
a529bec0 | 2869 | ret = get_new_event_name(buf, sizeof(buf), event, namelist, |
e63c625a | 2870 | tev->point.retprobe, allow_suffix); |
a3c9de62 MH |
2871 | if (ret < 0) |
2872 | return ret; | |
2873 | ||
2874 | event = buf; | |
2875 | ||
2876 | tev->event = strdup(event); | |
2877 | tev->group = strdup(group); | |
2878 | if (tev->event == NULL || tev->group == NULL) | |
2879 | return -ENOMEM; | |
2880 | ||
72363540 MH |
2881 | /* |
2882 | * Add new event name to namelist if multiprobe event is NOT | |
2883 | * supported, since we have to use new event name for following | |
2884 | * probes in that case. | |
2885 | */ | |
2886 | if (!multiprobe_event_is_supported()) | |
2887 | strlist__add(namelist, event); | |
a3c9de62 MH |
2888 | return 0; |
2889 | } | |
2890 | ||
1de7b8bf MH |
2891 | static int __open_probe_file_and_namelist(bool uprobe, |
2892 | struct strlist **namelist) | |
50656eec | 2893 | { |
1de7b8bf | 2894 | int fd; |
50656eec | 2895 | |
1de7b8bf | 2896 | fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0)); |
92f6c72e | 2897 | if (fd < 0) |
146a1439 | 2898 | return fd; |
5e45187c | 2899 | |
b498ce1f | 2900 | /* Get current event names */ |
1de7b8bf MH |
2901 | *namelist = probe_file__get_namelist(fd); |
2902 | if (!(*namelist)) { | |
146a1439 | 2903 | pr_debug("Failed to get current event list.\n"); |
1de7b8bf MH |
2904 | close(fd); |
2905 | return -ENOMEM; | |
146a1439 | 2906 | } |
1de7b8bf MH |
2907 | return fd; |
2908 | } | |
2909 | ||
2910 | static int __add_probe_trace_events(struct perf_probe_event *pev, | |
2911 | struct probe_trace_event *tevs, | |
2912 | int ntevs, bool allow_suffix) | |
2913 | { | |
2914 | int i, fd[2] = {-1, -1}, up, ret; | |
2915 | struct probe_trace_event *tev = NULL; | |
2916 | struct probe_cache *cache = NULL; | |
2917 | struct strlist *namelist[2] = {NULL, NULL}; | |
544abd44 | 2918 | struct nscookie nsc; |
1de7b8bf MH |
2919 | |
2920 | up = pev->uprobes ? 1 : 0; | |
2921 | fd[up] = __open_probe_file_and_namelist(up, &namelist[up]); | |
2922 | if (fd[up] < 0) | |
2923 | return fd[up]; | |
4235b045 | 2924 | |
146a1439 | 2925 | ret = 0; |
02b95dad | 2926 | for (i = 0; i < ntevs; i++) { |
4235b045 | 2927 | tev = &tevs[i]; |
1de7b8bf MH |
2928 | up = tev->uprobes ? 1 : 0; |
2929 | if (fd[up] == -1) { /* Open the kprobe/uprobe_events */ | |
2930 | fd[up] = __open_probe_file_and_namelist(up, | |
2931 | &namelist[up]); | |
2932 | if (fd[up] < 0) | |
2933 | goto close_out; | |
2934 | } | |
b031220d | 2935 | /* Skip if the symbol is out of .text or blacklisted */ |
bc062230 | 2936 | if (!tev->point.symbol && !pev->uprobes) |
5a51fcd1 | 2937 | continue; |
9aaf5a5f | 2938 | |
a3c9de62 | 2939 | /* Set new name for tev (and update namelist) */ |
1de7b8bf | 2940 | ret = probe_trace_event__set_name(tev, pev, namelist[up], |
a3c9de62 | 2941 | allow_suffix); |
146a1439 MH |
2942 | if (ret < 0) |
2943 | break; | |
4235b045 | 2944 | |
544abd44 | 2945 | nsinfo__mountns_enter(pev->nsi, &nsc); |
1de7b8bf | 2946 | ret = probe_file__add_event(fd[up], tev); |
544abd44 | 2947 | nsinfo__mountns_exit(&nsc); |
146a1439 MH |
2948 | if (ret < 0) |
2949 | break; | |
4235b045 | 2950 | |
4235b045 MH |
2951 | /* |
2952 | * Probes after the first probe which comes from same | |
2953 | * user input are always allowed to add suffix, because | |
2954 | * there might be several addresses corresponding to | |
2955 | * one code line. | |
2956 | */ | |
2957 | allow_suffix = true; | |
50656eec | 2958 | } |
79702f61 MH |
2959 | if (ret == -EINVAL && pev->uprobes) |
2960 | warn_uprobe_event_compat(tev); | |
2fd457a3 | 2961 | if (ret == 0 && probe_conf.cache) { |
f045b8c4 | 2962 | cache = probe_cache__new(pev->target, pev->nsi); |
2fd457a3 MH |
2963 | if (!cache || |
2964 | probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 || | |
2965 | probe_cache__commit(cache) < 0) | |
2966 | pr_warning("Failed to add event to probe cache\n"); | |
2967 | probe_cache__delete(cache); | |
2968 | } | |
146a1439 | 2969 | |
ae2cb1ac | 2970 | close_out: |
1de7b8bf MH |
2971 | for (up = 0; up < 2; up++) { |
2972 | strlist__delete(namelist[up]); | |
2973 | if (fd[up] >= 0) | |
2974 | close(fd[up]); | |
2975 | } | |
146a1439 | 2976 | return ret; |
50656eec | 2977 | } |
fa28244d | 2978 | |
6bb536cc WN |
2979 | static int find_probe_functions(struct map *map, char *name, |
2980 | struct symbol **syms) | |
eb948e50 | 2981 | { |
564c62a4 | 2982 | int found = 0; |
0a3873a8 | 2983 | struct symbol *sym; |
4c859351 | 2984 | struct rb_node *tmp; |
4b3a2716 MH |
2985 | const char *norm, *ver; |
2986 | char *buf = NULL; | |
c588d158 | 2987 | bool cut_version = true; |
564c62a4 | 2988 | |
be39db9f | 2989 | if (map__load(map) < 0) |
f4f1c429 | 2990 | return -EACCES; /* Possible permission error to load symbols */ |
75e4a2a6 | 2991 | |
c588d158 MH |
2992 | /* If user gives a version, don't cut off the version from symbols */ |
2993 | if (strchr(name, '@')) | |
2994 | cut_version = false; | |
2995 | ||
4c859351 | 2996 | map__for_each_symbol(map, sym, tmp) { |
4b3a2716 MH |
2997 | norm = arch__normalize_symbol_name(sym->name); |
2998 | if (!norm) | |
2999 | continue; | |
3000 | ||
c588d158 MH |
3001 | if (cut_version) { |
3002 | /* We don't care about default symbol or not */ | |
3003 | ver = strchr(norm, '@'); | |
3004 | if (ver) { | |
3005 | buf = strndup(norm, ver - norm); | |
3006 | if (!buf) | |
3007 | return -ENOMEM; | |
3008 | norm = buf; | |
3009 | } | |
4b3a2716 | 3010 | } |
c588d158 | 3011 | |
4b3a2716 | 3012 | if (strglobmatch(norm, name)) { |
4c859351 | 3013 | found++; |
6bb536cc WN |
3014 | if (syms && found < probe_conf.max_probes) |
3015 | syms[found - 1] = sym; | |
3016 | } | |
4b3a2716 MH |
3017 | if (buf) |
3018 | zfree(&buf); | |
eb948e50 | 3019 | } |
564c62a4 NK |
3020 | |
3021 | return found; | |
eb948e50 MH |
3022 | } |
3023 | ||
7b6ff0bd NR |
3024 | void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused, |
3025 | struct probe_trace_event *tev __maybe_unused, | |
0b3c2264 NR |
3026 | struct map *map __maybe_unused, |
3027 | struct symbol *sym __maybe_unused) { } | |
7b6ff0bd | 3028 | |
f4f1c429 MH |
3029 | |
3030 | static void pr_kallsyms_access_error(void) | |
3031 | { | |
3032 | pr_err("Please ensure you can read the /proc/kallsyms symbol addresses.\n" | |
3033 | "If /proc/sys/kernel/kptr_restrict is '2', you can not read\n" | |
3034 | "kernel symbol addresses even if you are a superuser. Please change\n" | |
3035 | "it to '1'. If kptr_restrict is '1', the superuser can read the\n" | |
3036 | "symbol addresses.\n" | |
3037 | "In that case, please run this command again with sudo.\n"); | |
3038 | } | |
3039 | ||
eb948e50 MH |
3040 | /* |
3041 | * Find probe function addresses from map. | |
3042 | * Return an error or the number of found probe_trace_event | |
3043 | */ | |
3044 | static int find_probe_trace_events_from_map(struct perf_probe_event *pev, | |
ddb2f58f | 3045 | struct probe_trace_event **tevs) |
e0faa8d3 | 3046 | { |
eb948e50 | 3047 | struct map *map = NULL; |
eb948e50 | 3048 | struct ref_reloc_sym *reloc_sym = NULL; |
e0faa8d3 | 3049 | struct symbol *sym; |
6bb536cc | 3050 | struct symbol **syms = NULL; |
0e60836b | 3051 | struct probe_trace_event *tev; |
eb948e50 MH |
3052 | struct perf_probe_point *pp = &pev->point; |
3053 | struct probe_trace_point *tp; | |
564c62a4 | 3054 | int num_matched_functions; |
b031220d | 3055 | int ret, i, j, skipped = 0; |
c61fb959 | 3056 | char *mod_name; |
4235b045 | 3057 | |
544abd44 | 3058 | map = get_target_map(pev->target, pev->nsi, pev->uprobes); |
eb948e50 MH |
3059 | if (!map) { |
3060 | ret = -EINVAL; | |
3061 | goto out; | |
fb7345bb MH |
3062 | } |
3063 | ||
6bb536cc WN |
3064 | syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes); |
3065 | if (!syms) { | |
3066 | ret = -ENOMEM; | |
3067 | goto out; | |
3068 | } | |
3069 | ||
eb948e50 MH |
3070 | /* |
3071 | * Load matched symbols: Since the different local symbols may have | |
3072 | * same name but different addresses, this lists all the symbols. | |
3073 | */ | |
6bb536cc | 3074 | num_matched_functions = find_probe_functions(map, pp->function, syms); |
4b3a2716 | 3075 | if (num_matched_functions <= 0) { |
f4f1c429 MH |
3076 | if (num_matched_functions == -EACCES) { |
3077 | pr_err("Failed to load symbols from %s\n", | |
3078 | pev->target ?: "/proc/kallsyms"); | |
3079 | if (pev->target) | |
3080 | pr_err("Please ensure the file is not stripped.\n"); | |
3081 | else | |
3082 | pr_kallsyms_access_error(); | |
3083 | } else | |
3084 | pr_err("Failed to find symbol %s in %s\n", pp->function, | |
3085 | pev->target ? : "kernel"); | |
eb948e50 MH |
3086 | ret = -ENOENT; |
3087 | goto out; | |
ddb2f58f | 3088 | } else if (num_matched_functions > probe_conf.max_probes) { |
eb948e50 | 3089 | pr_err("Too many functions matched in %s\n", |
44225521 | 3090 | pev->target ? : "kernel"); |
eb948e50 MH |
3091 | ret = -E2BIG; |
3092 | goto out; | |
fb7345bb MH |
3093 | } |
3094 | ||
1a8ac29c | 3095 | /* Note that the symbols in the kmodule are not relocated */ |
7ab31d94 NR |
3096 | if (!pev->uprobes && !pev->target && |
3097 | (!pp->retprobe || kretprobe_offset_is_supported())) { | |
80526491 | 3098 | reloc_sym = kernel_get_ref_reloc_sym(NULL); |
eb948e50 | 3099 | if (!reloc_sym) { |
41ca1d1e RB |
3100 | pr_warning("Relocated base symbol is not found! " |
3101 | "Check /proc/sys/kernel/kptr_restrict\n" | |
3102 | "and /proc/sys/kernel/perf_event_paranoid. " | |
3103 | "Or run as privileged perf user.\n\n"); | |
eb948e50 MH |
3104 | ret = -EINVAL; |
3105 | goto out; | |
3106 | } | |
3107 | } | |
4235b045 | 3108 | |
eb948e50 MH |
3109 | /* Setup result trace-probe-events */ |
3110 | *tevs = zalloc(sizeof(*tev) * num_matched_functions); | |
3111 | if (!*tevs) { | |
02b95dad | 3112 | ret = -ENOMEM; |
eb948e50 | 3113 | goto out; |
02b95dad | 3114 | } |
ce27a443 | 3115 | |
eb948e50 | 3116 | ret = 0; |
564c62a4 | 3117 | |
6bb536cc WN |
3118 | for (j = 0; j < num_matched_functions; j++) { |
3119 | sym = syms[j]; | |
3120 | ||
4624f199 ZC |
3121 | if (sym->type != STT_FUNC) |
3122 | continue; | |
3123 | ||
26bbf45f MH |
3124 | /* There can be duplicated symbols in the map */ |
3125 | for (i = 0; i < j; i++) | |
3126 | if (sym->start == syms[i]->start) { | |
3127 | pr_debug("Found duplicated symbol %s @ %" PRIx64 "\n", | |
3128 | sym->name, sym->start); | |
3129 | break; | |
3130 | } | |
3131 | if (i != j) | |
3132 | continue; | |
3133 | ||
eb948e50 MH |
3134 | tev = (*tevs) + ret; |
3135 | tp = &tev->point; | |
3136 | if (ret == num_matched_functions) { | |
3137 | pr_warning("Too many symbols are listed. Skip it.\n"); | |
3138 | break; | |
ce27a443 | 3139 | } |
eb948e50 | 3140 | ret++; |
ce27a443 | 3141 | |
eb948e50 MH |
3142 | if (pp->offset > sym->end - sym->start) { |
3143 | pr_warning("Offset %ld is bigger than the size of %s\n", | |
3144 | pp->offset, sym->name); | |
3145 | ret = -ENOENT; | |
3146 | goto err_out; | |
3147 | } | |
3148 | /* Add one probe point */ | |
78a1f7cd | 3149 | tp->address = map__unmap_ip(map, sym->start) + pp->offset; |
1a8ac29c MH |
3150 | |
3151 | /* Check the kprobe (not in module) is within .text */ | |
3152 | if (!pev->uprobes && !pev->target && | |
b031220d MH |
3153 | kprobe_warn_out_range(sym->name, tp->address)) { |
3154 | tp->symbol = NULL; /* Skip it */ | |
3155 | skipped++; | |
3156 | } else if (reloc_sym) { | |
eb948e50 MH |
3157 | tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out); |
3158 | tp->offset = tp->address - reloc_sym->addr; | |
3159 | } else { | |
3160 | tp->symbol = strdup_or_goto(sym->name, nomem_out); | |
3161 | tp->offset = pp->offset; | |
3162 | } | |
6bb536cc WN |
3163 | tp->realname = strdup_or_goto(sym->name, nomem_out); |
3164 | ||
eb948e50 | 3165 | tp->retprobe = pp->retprobe; |
c61fb959 RB |
3166 | if (pev->target) { |
3167 | if (pev->uprobes) { | |
3168 | tev->point.module = strdup_or_goto(pev->target, | |
3169 | nomem_out); | |
3170 | } else { | |
3171 | mod_name = find_module_name(pev->target); | |
3172 | tev->point.module = | |
3173 | strdup(mod_name ? mod_name : pev->target); | |
3174 | free(mod_name); | |
3175 | if (!tev->point.module) | |
3176 | goto nomem_out; | |
3177 | } | |
3178 | } | |
eb948e50 MH |
3179 | tev->uprobes = pev->uprobes; |
3180 | tev->nargs = pev->nargs; | |
3181 | if (tev->nargs) { | |
3182 | tev->args = zalloc(sizeof(struct probe_trace_arg) * | |
3183 | tev->nargs); | |
3184 | if (tev->args == NULL) | |
3185 | goto nomem_out; | |
e334016f | 3186 | } |
48481938 | 3187 | for (i = 0; i < tev->nargs; i++) { |
eb948e50 MH |
3188 | if (pev->args[i].name) |
3189 | tev->args[i].name = | |
3190 | strdup_or_goto(pev->args[i].name, | |
3191 | nomem_out); | |
3192 | ||
3193 | tev->args[i].value = strdup_or_goto(pev->args[i].var, | |
3194 | nomem_out); | |
3195 | if (pev->args[i].type) | |
3196 | tev->args[i].type = | |
3197 | strdup_or_goto(pev->args[i].type, | |
3198 | nomem_out); | |
48481938 | 3199 | } |
0b3c2264 | 3200 | arch__fix_tev_from_maps(pev, tev, map, sym); |
4235b045 | 3201 | } |
b031220d MH |
3202 | if (ret == skipped) { |
3203 | ret = -ENOENT; | |
3204 | goto err_out; | |
3205 | } | |
4235b045 | 3206 | |
eb948e50 | 3207 | out: |
eebc509b | 3208 | map__put(map); |
6bb536cc | 3209 | free(syms); |
eb948e50 | 3210 | return ret; |
225466f1 | 3211 | |
eb948e50 MH |
3212 | nomem_out: |
3213 | ret = -ENOMEM; | |
3214 | err_out: | |
3215 | clear_probe_trace_events(*tevs, num_matched_functions); | |
3216 | zfree(tevs); | |
3217 | goto out; | |
3218 | } | |
1c1bc922 | 3219 | |
da15bd9d WN |
3220 | static int try_to_find_absolute_address(struct perf_probe_event *pev, |
3221 | struct probe_trace_event **tevs) | |
3222 | { | |
3223 | struct perf_probe_point *pp = &pev->point; | |
3224 | struct probe_trace_event *tev; | |
3225 | struct probe_trace_point *tp; | |
3226 | int i, err; | |
3227 | ||
3228 | if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2))) | |
3229 | return -EINVAL; | |
3230 | if (perf_probe_event_need_dwarf(pev)) | |
3231 | return -EINVAL; | |
3232 | ||
3233 | /* | |
3234 | * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at | |
3235 | * absolute address. | |
3236 | * | |
3237 | * Only one tev can be generated by this. | |
3238 | */ | |
3239 | *tevs = zalloc(sizeof(*tev)); | |
3240 | if (!*tevs) | |
3241 | return -ENOMEM; | |
3242 | ||
3243 | tev = *tevs; | |
3244 | tp = &tev->point; | |
3245 | ||
3246 | /* | |
3247 | * Don't use tp->offset, use address directly, because | |
3248 | * in synthesize_probe_trace_command() address cannot be | |
3249 | * zero. | |
3250 | */ | |
3251 | tp->address = pev->point.abs_address; | |
3252 | tp->retprobe = pp->retprobe; | |
3253 | tev->uprobes = pev->uprobes; | |
3254 | ||
3255 | err = -ENOMEM; | |
3256 | /* | |
3257 | * Give it a '0x' leading symbol name. | |
3258 | * In __add_probe_trace_events, a NULL symbol is interpreted as | |
adba1634 | 3259 | * invalid. |
da15bd9d | 3260 | */ |
22a66551 | 3261 | if (asprintf(&tp->symbol, "0x%" PRIx64, tp->address) < 0) |
da15bd9d WN |
3262 | goto errout; |
3263 | ||
3264 | /* For kprobe, check range */ | |
3265 | if ((!tev->uprobes) && | |
3266 | (kprobe_warn_out_range(tev->point.symbol, | |
3267 | tev->point.address))) { | |
3268 | err = -EACCES; | |
3269 | goto errout; | |
3270 | } | |
3271 | ||
22a66551 | 3272 | if (asprintf(&tp->realname, "abs_%" PRIx64, tp->address) < 0) |
da15bd9d WN |
3273 | goto errout; |
3274 | ||
3275 | if (pev->target) { | |
3276 | tp->module = strdup(pev->target); | |
3277 | if (!tp->module) | |
3278 | goto errout; | |
3279 | } | |
3280 | ||
3281 | if (tev->group) { | |
3282 | tev->group = strdup(pev->group); | |
3283 | if (!tev->group) | |
3284 | goto errout; | |
3285 | } | |
3286 | ||
3287 | if (pev->event) { | |
3288 | tev->event = strdup(pev->event); | |
3289 | if (!tev->event) | |
3290 | goto errout; | |
3291 | } | |
3292 | ||
3293 | tev->nargs = pev->nargs; | |
3294 | tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); | |
d1d0e29c | 3295 | if (!tev->args) |
da15bd9d | 3296 | goto errout; |
d1d0e29c | 3297 | |
da15bd9d WN |
3298 | for (i = 0; i < tev->nargs; i++) |
3299 | copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]); | |
3300 | ||
3301 | return 1; | |
3302 | ||
3303 | errout: | |
42e233ca ME |
3304 | clear_probe_trace_events(*tevs, 1); |
3305 | *tevs = NULL; | |
da15bd9d WN |
3306 | return err; |
3307 | } | |
3308 | ||
4d39c89f | 3309 | /* Concatenate two arrays */ |
42bba263 MH |
3310 | static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b) |
3311 | { | |
3312 | void *ret; | |
3313 | ||
3314 | ret = malloc(sz_a + sz_b); | |
3315 | if (ret) { | |
3316 | memcpy(ret, a, sz_a); | |
3317 | memcpy(ret + sz_a, b, sz_b); | |
3318 | } | |
3319 | return ret; | |
3320 | } | |
3321 | ||
3322 | static int | |
3323 | concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs, | |
3324 | struct probe_trace_event **tevs2, int ntevs2) | |
3325 | { | |
3326 | struct probe_trace_event *new_tevs; | |
3327 | int ret = 0; | |
3328 | ||
f0a30dca | 3329 | if (*ntevs == 0) { |
42bba263 MH |
3330 | *tevs = *tevs2; |
3331 | *ntevs = ntevs2; | |
3332 | *tevs2 = NULL; | |
3333 | return 0; | |
3334 | } | |
3335 | ||
3336 | if (*ntevs + ntevs2 > probe_conf.max_probes) | |
3337 | ret = -E2BIG; | |
3338 | else { | |
4d39c89f | 3339 | /* Concatenate the array of probe_trace_event */ |
42bba263 MH |
3340 | new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs), |
3341 | *tevs2, ntevs2 * sizeof(**tevs2)); | |
3342 | if (!new_tevs) | |
3343 | ret = -ENOMEM; | |
3344 | else { | |
3345 | free(*tevs); | |
3346 | *tevs = new_tevs; | |
3347 | *ntevs += ntevs2; | |
3348 | } | |
3349 | } | |
3350 | if (ret < 0) | |
3351 | clear_probe_trace_events(*tevs2, ntevs2); | |
3352 | zfree(tevs2); | |
3353 | ||
3354 | return ret; | |
3355 | } | |
3356 | ||
3357 | /* | |
3358 | * Try to find probe_trace_event from given probe caches. Return the number | |
3359 | * of cached events found, if an error occurs return the error. | |
3360 | */ | |
3361 | static int find_cached_events(struct perf_probe_event *pev, | |
3362 | struct probe_trace_event **tevs, | |
3363 | const char *target) | |
3364 | { | |
3365 | struct probe_cache *cache; | |
3366 | struct probe_cache_entry *entry; | |
3367 | struct probe_trace_event *tmp_tevs = NULL; | |
3368 | int ntevs = 0; | |
3369 | int ret = 0; | |
3370 | ||
f045b8c4 | 3371 | cache = probe_cache__new(target, pev->nsi); |
42bba263 MH |
3372 | /* Return 0 ("not found") if the target has no probe cache. */ |
3373 | if (!cache) | |
3374 | return 0; | |
3375 | ||
3376 | for_each_probe_cache_entry(entry, cache) { | |
3377 | /* Skip the cache entry which has no name */ | |
3378 | if (!entry->pev.event || !entry->pev.group) | |
3379 | continue; | |
3380 | if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) && | |
3381 | strglobmatch(entry->pev.event, pev->event)) { | |
3382 | ret = probe_cache_entry__get_event(entry, &tmp_tevs); | |
3383 | if (ret > 0) | |
3384 | ret = concat_probe_trace_events(tevs, &ntevs, | |
3385 | &tmp_tevs, ret); | |
3386 | if (ret < 0) | |
3387 | break; | |
3388 | } | |
3389 | } | |
3390 | probe_cache__delete(cache); | |
3391 | if (ret < 0) { | |
3392 | clear_probe_trace_events(*tevs, ntevs); | |
3393 | zfree(tevs); | |
3394 | } else { | |
3395 | ret = ntevs; | |
3396 | if (ntevs > 0 && target && target[0] == '/') | |
3397 | pev->uprobes = true; | |
3398 | } | |
3399 | ||
3400 | return ret; | |
3401 | } | |
3402 | ||
1de7b8bf MH |
3403 | /* Try to find probe_trace_event from all probe caches */ |
3404 | static int find_cached_events_all(struct perf_probe_event *pev, | |
3405 | struct probe_trace_event **tevs) | |
3406 | { | |
3407 | struct probe_trace_event *tmp_tevs = NULL; | |
3408 | struct strlist *bidlist; | |
3409 | struct str_node *nd; | |
3410 | char *pathname; | |
3411 | int ntevs = 0; | |
3412 | int ret; | |
3413 | ||
3414 | /* Get the buildid list of all valid caches */ | |
3415 | bidlist = build_id_cache__list_all(true); | |
3416 | if (!bidlist) { | |
3417 | ret = -errno; | |
3418 | pr_debug("Failed to get buildids: %d\n", ret); | |
3419 | return ret; | |
3420 | } | |
3421 | ||
3422 | ret = 0; | |
3423 | strlist__for_each_entry(nd, bidlist) { | |
3424 | pathname = build_id_cache__origname(nd->s); | |
3425 | ret = find_cached_events(pev, &tmp_tevs, pathname); | |
3426 | /* In the case of cnt == 0, we just skip it */ | |
3427 | if (ret > 0) | |
3428 | ret = concat_probe_trace_events(tevs, &ntevs, | |
3429 | &tmp_tevs, ret); | |
3430 | free(pathname); | |
3431 | if (ret < 0) | |
3432 | break; | |
3433 | } | |
3434 | strlist__delete(bidlist); | |
3435 | ||
3436 | if (ret < 0) { | |
3437 | clear_probe_trace_events(*tevs, ntevs); | |
3438 | zfree(tevs); | |
3439 | } else | |
3440 | ret = ntevs; | |
3441 | ||
3442 | return ret; | |
3443 | } | |
3444 | ||
bc062230 MH |
3445 | static int find_probe_trace_events_from_cache(struct perf_probe_event *pev, |
3446 | struct probe_trace_event **tevs) | |
3447 | { | |
3448 | struct probe_cache *cache; | |
3449 | struct probe_cache_entry *entry; | |
3450 | struct probe_trace_event *tev; | |
3451 | struct str_node *node; | |
3452 | int ret, i; | |
3453 | ||
1de7b8bf | 3454 | if (pev->sdt) { |
42bba263 | 3455 | /* For SDT/cached events, we use special search functions */ |
1de7b8bf MH |
3456 | if (!pev->target) |
3457 | return find_cached_events_all(pev, tevs); | |
3458 | else | |
3459 | return find_cached_events(pev, tevs, pev->target); | |
3460 | } | |
f045b8c4 | 3461 | cache = probe_cache__new(pev->target, pev->nsi); |
bc062230 MH |
3462 | if (!cache) |
3463 | return 0; | |
3464 | ||
3465 | entry = probe_cache__find(cache, pev); | |
3466 | if (!entry) { | |
36a009fe MH |
3467 | /* SDT must be in the cache */ |
3468 | ret = pev->sdt ? -ENOENT : 0; | |
bc062230 MH |
3469 | goto out; |
3470 | } | |
3471 | ||
3472 | ret = strlist__nr_entries(entry->tevlist); | |
3473 | if (ret > probe_conf.max_probes) { | |
3474 | pr_debug("Too many entries matched in the cache of %s\n", | |
3475 | pev->target ? : "kernel"); | |
3476 | ret = -E2BIG; | |
3477 | goto out; | |
3478 | } | |
3479 | ||
3480 | *tevs = zalloc(ret * sizeof(*tev)); | |
3481 | if (!*tevs) { | |
3482 | ret = -ENOMEM; | |
3483 | goto out; | |
3484 | } | |
3485 | ||
3486 | i = 0; | |
3487 | strlist__for_each_entry(node, entry->tevlist) { | |
3488 | tev = &(*tevs)[i++]; | |
3489 | ret = parse_probe_trace_command(node->s, tev); | |
3490 | if (ret < 0) | |
3491 | goto out; | |
3492 | /* Set the uprobes attribute as same as original */ | |
3493 | tev->uprobes = pev->uprobes; | |
3494 | } | |
3495 | ret = i; | |
3496 | ||
3497 | out: | |
3498 | probe_cache__delete(cache); | |
3499 | return ret; | |
3500 | } | |
3501 | ||
eb948e50 | 3502 | static int convert_to_probe_trace_events(struct perf_probe_event *pev, |
ddb2f58f | 3503 | struct probe_trace_event **tevs) |
eb948e50 MH |
3504 | { |
3505 | int ret; | |
3506 | ||
36a009fe | 3507 | if (!pev->group && !pev->sdt) { |
2a12ec13 MH |
3508 | /* Set group name if not given */ |
3509 | if (!pev->uprobes) { | |
3510 | pev->group = strdup(PERFPROBE_GROUP); | |
3511 | ret = pev->group ? 0 : -ENOMEM; | |
3512 | } else | |
3513 | ret = convert_exec_to_group(pev->target, &pev->group); | |
eb948e50 MH |
3514 | if (ret != 0) { |
3515 | pr_warning("Failed to make a group name.\n"); | |
3516 | return ret; | |
3517 | } | |
02b95dad | 3518 | } |
e334016f | 3519 | |
da15bd9d WN |
3520 | ret = try_to_find_absolute_address(pev, tevs); |
3521 | if (ret > 0) | |
3522 | return ret; | |
3523 | ||
bc062230 MH |
3524 | /* At first, we need to lookup cache entry */ |
3525 | ret = find_probe_trace_events_from_cache(pev, tevs); | |
36a009fe MH |
3526 | if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */ |
3527 | return ret == 0 ? -ENOENT : ret; /* Found in probe cache */ | |
bc062230 | 3528 | |
eb948e50 | 3529 | /* Convert perf_probe_event with debuginfo */ |
ddb2f58f | 3530 | ret = try_to_find_probe_trace_events(pev, tevs); |
eb948e50 MH |
3531 | if (ret != 0) |
3532 | return ret; /* Found in debuginfo or got an error */ | |
3533 | ||
ddb2f58f | 3534 | return find_probe_trace_events_from_map(pev, tevs); |
4235b045 MH |
3535 | } |
3536 | ||
12fae5ef | 3537 | int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs) |
4235b045 | 3538 | { |
844dffa5 | 3539 | int i, ret; |
4235b045 | 3540 | |
4235b045 MH |
3541 | /* Loop 1: convert all events */ |
3542 | for (i = 0; i < npevs; i++) { | |
b031220d | 3543 | /* Init kprobe blacklist if needed */ |
12fae5ef | 3544 | if (!pevs[i].uprobes) |
b031220d | 3545 | kprobe_blacklist__init(); |
4235b045 | 3546 | /* Convert with or without debuginfo */ |
12fae5ef | 3547 | ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs); |
146a1439 | 3548 | if (ret < 0) |
844dffa5 | 3549 | return ret; |
12fae5ef | 3550 | pevs[i].ntevs = ret; |
e0faa8d3 | 3551 | } |
b031220d MH |
3552 | /* This just release blacklist only if allocated */ |
3553 | kprobe_blacklist__release(); | |
e0faa8d3 | 3554 | |
844dffa5 NK |
3555 | return 0; |
3556 | } | |
3557 | ||
1c20b1d1 MH |
3558 | static int show_probe_trace_event(struct probe_trace_event *tev) |
3559 | { | |
3560 | char *buf = synthesize_probe_trace_command(tev); | |
3561 | ||
3562 | if (!buf) { | |
3563 | pr_debug("Failed to synthesize probe trace event.\n"); | |
3564 | return -EINVAL; | |
3565 | } | |
3566 | ||
3567 | /* Showing definition always go stdout */ | |
3568 | printf("%s\n", buf); | |
3569 | free(buf); | |
3570 | ||
3571 | return 0; | |
3572 | } | |
3573 | ||
3574 | int show_probe_trace_events(struct perf_probe_event *pevs, int npevs) | |
3575 | { | |
3576 | struct strlist *namelist = strlist__new(NULL, NULL); | |
3577 | struct probe_trace_event *tev; | |
3578 | struct perf_probe_event *pev; | |
3579 | int i, j, ret = 0; | |
3580 | ||
3581 | if (!namelist) | |
3582 | return -ENOMEM; | |
3583 | ||
3584 | for (j = 0; j < npevs && !ret; j++) { | |
3585 | pev = &pevs[j]; | |
3586 | for (i = 0; i < pev->ntevs && !ret; i++) { | |
3587 | tev = &pev->tevs[i]; | |
3588 | /* Skip if the symbol is out of .text or blacklisted */ | |
3589 | if (!tev->point.symbol && !pev->uprobes) | |
3590 | continue; | |
3591 | ||
3592 | /* Set new name for tev (and update namelist) */ | |
3593 | ret = probe_trace_event__set_name(tev, pev, | |
3594 | namelist, true); | |
3595 | if (!ret) | |
3596 | ret = show_probe_trace_event(tev); | |
3597 | } | |
3598 | } | |
3599 | strlist__delete(namelist); | |
3600 | ||
3601 | return ret; | |
3602 | } | |
3603 | ||
45237f98 MH |
3604 | static int show_bootconfig_event(struct probe_trace_event *tev) |
3605 | { | |
3606 | struct probe_trace_point *tp = &tev->point; | |
3607 | struct strbuf buf; | |
3608 | char *ret = NULL; | |
3609 | int err; | |
3610 | ||
3611 | if (strbuf_init(&buf, 32) < 0) | |
3612 | return -ENOMEM; | |
3613 | ||
3614 | err = synthesize_kprobe_trace_def(tp, &buf); | |
3615 | if (err >= 0) | |
3616 | err = synthesize_probe_trace_args(tev, &buf); | |
3617 | if (err >= 0) | |
3618 | ret = strbuf_detach(&buf, NULL); | |
3619 | strbuf_release(&buf); | |
3620 | ||
3621 | if (ret) { | |
3622 | printf("'%s'", ret); | |
3623 | free(ret); | |
3624 | } | |
3625 | ||
3626 | return err; | |
3627 | } | |
3628 | ||
3629 | int show_bootconfig_events(struct perf_probe_event *pevs, int npevs) | |
3630 | { | |
3631 | struct strlist *namelist = strlist__new(NULL, NULL); | |
3632 | struct probe_trace_event *tev; | |
3633 | struct perf_probe_event *pev; | |
3634 | char *cur_name = NULL; | |
3635 | int i, j, ret = 0; | |
3636 | ||
3637 | if (!namelist) | |
3638 | return -ENOMEM; | |
3639 | ||
3640 | for (j = 0; j < npevs && !ret; j++) { | |
3641 | pev = &pevs[j]; | |
3642 | if (pev->group && strcmp(pev->group, "probe")) | |
3643 | pr_warning("WARN: Group name %s is ignored\n", pev->group); | |
3644 | if (pev->uprobes) { | |
3645 | pr_warning("ERROR: Bootconfig doesn't support uprobes\n"); | |
3646 | ret = -EINVAL; | |
3647 | break; | |
3648 | } | |
3649 | for (i = 0; i < pev->ntevs && !ret; i++) { | |
3650 | tev = &pev->tevs[i]; | |
3651 | /* Skip if the symbol is out of .text or blacklisted */ | |
3652 | if (!tev->point.symbol && !pev->uprobes) | |
3653 | continue; | |
3654 | ||
3655 | /* Set new name for tev (and update namelist) */ | |
3656 | ret = probe_trace_event__set_name(tev, pev, | |
3657 | namelist, true); | |
3658 | if (ret) | |
3659 | break; | |
3660 | ||
3661 | if (!cur_name || strcmp(cur_name, tev->event)) { | |
3662 | printf("%sftrace.event.kprobes.%s.probe = ", | |
3663 | cur_name ? "\n" : "", tev->event); | |
3664 | cur_name = tev->event; | |
3665 | } else | |
3666 | printf(", "); | |
3667 | ret = show_bootconfig_event(tev); | |
3668 | } | |
3669 | } | |
3670 | printf("\n"); | |
3671 | strlist__delete(namelist); | |
3672 | ||
3673 | return ret; | |
3674 | } | |
3675 | ||
12fae5ef | 3676 | int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs) |
844dffa5 NK |
3677 | { |
3678 | int i, ret = 0; | |
3679 | ||
4235b045 | 3680 | /* Loop 2: add all events */ |
8635bf6e | 3681 | for (i = 0; i < npevs; i++) { |
12fae5ef WN |
3682 | ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs, |
3683 | pevs[i].ntevs, | |
ddb2f58f | 3684 | probe_conf.force_add); |
fbee632d ACM |
3685 | if (ret < 0) |
3686 | break; | |
3687 | } | |
844dffa5 NK |
3688 | return ret; |
3689 | } | |
3690 | ||
12fae5ef | 3691 | void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs) |
844dffa5 NK |
3692 | { |
3693 | int i, j; | |
544abd44 | 3694 | struct perf_probe_event *pev; |
844dffa5 | 3695 | |
449e5b24 MH |
3696 | /* Loop 3: cleanup and free trace events */ |
3697 | for (i = 0; i < npevs; i++) { | |
544abd44 | 3698 | pev = &pevs[i]; |
12fae5ef WN |
3699 | for (j = 0; j < pevs[i].ntevs; j++) |
3700 | clear_probe_trace_event(&pevs[i].tevs[j]); | |
3701 | zfree(&pevs[i].tevs); | |
3702 | pevs[i].ntevs = 0; | |
544abd44 | 3703 | nsinfo__zput(pev->nsi); |
a43aac29 | 3704 | clear_perf_probe_event(&pevs[i]); |
449e5b24 | 3705 | } |
844dffa5 NK |
3706 | } |
3707 | ||
3708 | int add_perf_probe_events(struct perf_probe_event *pevs, int npevs) | |
3709 | { | |
3710 | int ret; | |
844dffa5 | 3711 | |
9bae1e8c NK |
3712 | ret = init_probe_symbol_maps(pevs->uprobes); |
3713 | if (ret < 0) | |
3714 | return ret; | |
3715 | ||
12fae5ef | 3716 | ret = convert_perf_probe_events(pevs, npevs); |
844dffa5 | 3717 | if (ret == 0) |
12fae5ef | 3718 | ret = apply_perf_probe_events(pevs, npevs); |
844dffa5 | 3719 | |
12fae5ef | 3720 | cleanup_perf_probe_events(pevs, npevs); |
146a1439 | 3721 | |
9bae1e8c | 3722 | exit_probe_symbol_maps(); |
146a1439 | 3723 | return ret; |
e0faa8d3 MH |
3724 | } |
3725 | ||
307a464b | 3726 | int del_perf_probe_events(struct strfilter *filter) |
fa28244d | 3727 | { |
307a464b | 3728 | int ret, ret2, ufd = -1, kfd = -1; |
307a464b MH |
3729 | char *str = strfilter__string(filter); |
3730 | ||
3731 | if (!str) | |
3732 | return -EINVAL; | |
3733 | ||
fa28244d | 3734 | /* Get current event names */ |
92f6c72e MH |
3735 | ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW); |
3736 | if (ret < 0) | |
3737 | goto out; | |
467ec085 | 3738 | |
92f6c72e | 3739 | ret = probe_file__del_events(kfd, filter); |
307a464b | 3740 | if (ret < 0 && ret != -ENOENT) |
225466f1 | 3741 | goto error; |
225466f1 | 3742 | |
92f6c72e | 3743 | ret2 = probe_file__del_events(ufd, filter); |
dddc7ee3 | 3744 | if (ret2 < 0 && ret2 != -ENOENT) { |
307a464b | 3745 | ret = ret2; |
dddc7ee3 MH |
3746 | goto error; |
3747 | } | |
dddc7ee3 | 3748 | ret = 0; |
225466f1 SD |
3749 | |
3750 | error: | |
92f6c72e | 3751 | if (kfd >= 0) |
225466f1 | 3752 | close(kfd); |
92f6c72e | 3753 | if (ufd >= 0) |
225466f1 | 3754 | close(ufd); |
92f6c72e | 3755 | out: |
307a464b | 3756 | free(str); |
146a1439 MH |
3757 | |
3758 | return ret; | |
fa28244d | 3759 | } |
225466f1 | 3760 | |
544abd44 KJ |
3761 | int show_available_funcs(const char *target, struct nsinfo *nsi, |
3762 | struct strfilter *_filter, bool user) | |
e80711ca MH |
3763 | { |
3764 | struct map *map; | |
63df0e4b | 3765 | struct dso *dso; |
e80711ca MH |
3766 | int ret; |
3767 | ||
9bae1e8c | 3768 | ret = init_probe_symbol_maps(user); |
e80711ca MH |
3769 | if (ret < 0) |
3770 | return ret; | |
3771 | ||
2df58634 | 3772 | /* Get a symbol map */ |
544abd44 | 3773 | map = get_target_map(target, nsi, user); |
e80711ca | 3774 | if (!map) { |
2df58634 | 3775 | pr_err("Failed to get a map for %s\n", (target) ? : "kernel"); |
e80711ca MH |
3776 | return -EINVAL; |
3777 | } | |
225466f1 | 3778 | |
be39db9f | 3779 | ret = map__load(map); |
e7049342 MH |
3780 | if (ret) { |
3781 | if (ret == -2) { | |
3782 | char *str = strfilter__string(_filter); | |
3783 | pr_err("Failed to find symbols matched to \"%s\"\n", | |
3784 | str); | |
3785 | free(str); | |
3786 | } else | |
3787 | pr_err("Failed to load symbols in %s\n", | |
3788 | (target) ? : "kernel"); | |
2df58634 MH |
3789 | goto end; |
3790 | } | |
63df0e4b | 3791 | dso = map__dso(map); |
ce5b2934 | 3792 | dso__sort_by_name(dso); |
225466f1 | 3793 | |
2df58634 MH |
3794 | /* Show all (filtered) symbols */ |
3795 | setup_pager(); | |
fd227598 | 3796 | |
ee756ef7 IR |
3797 | for (size_t i = 0; i < dso__symbol_names_len(dso); i++) { |
3798 | struct symbol *pos = dso__symbol_names(dso)[i]; | |
fd227598 | 3799 | |
259dce91 IR |
3800 | if (strfilter__compare(_filter, pos->name)) |
3801 | printf("%s\n", pos->name); | |
3183f8ca | 3802 | } |
2df58634 | 3803 | end: |
eebc509b | 3804 | map__put(map); |
9bae1e8c | 3805 | exit_probe_symbol_maps(); |
225466f1 | 3806 | |
2df58634 | 3807 | return ret; |
225466f1 SD |
3808 | } |
3809 | ||
da15bd9d WN |
3810 | int copy_to_probe_trace_arg(struct probe_trace_arg *tvar, |
3811 | struct perf_probe_arg *pvar) | |
3812 | { | |
3813 | tvar->value = strdup(pvar->var); | |
3814 | if (tvar->value == NULL) | |
3815 | return -ENOMEM; | |
3816 | if (pvar->type) { | |
3817 | tvar->type = strdup(pvar->type); | |
3818 | if (tvar->type == NULL) | |
3819 | return -ENOMEM; | |
3820 | } | |
3821 | if (pvar->name) { | |
3822 | tvar->name = strdup(pvar->name); | |
3823 | if (tvar->name == NULL) | |
3824 | return -ENOMEM; | |
3825 | } else | |
3826 | tvar->name = NULL; | |
3827 | return 0; | |
3828 | } |