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