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