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