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