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