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