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