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