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