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