xref: /linux/tools/perf/util/probe-event.c (revision d1d0e29cb7d03a6019caa125e4c0288366a4f359)
150656eecSMasami Hiramatsu /*
20e60836bSSrikar Dronamraju  * probe-event.c : perf-probe definition to probe_events format converter
350656eecSMasami Hiramatsu  *
450656eecSMasami Hiramatsu  * Written by Masami Hiramatsu <mhiramat@redhat.com>
550656eecSMasami Hiramatsu  *
650656eecSMasami Hiramatsu  * This program is free software; you can redistribute it and/or modify
750656eecSMasami Hiramatsu  * it under the terms of the GNU General Public License as published by
850656eecSMasami Hiramatsu  * the Free Software Foundation; either version 2 of the License, or
950656eecSMasami Hiramatsu  * (at your option) any later version.
1050656eecSMasami Hiramatsu  *
1150656eecSMasami Hiramatsu  * This program is distributed in the hope that it will be useful,
1250656eecSMasami Hiramatsu  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1350656eecSMasami Hiramatsu  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1450656eecSMasami Hiramatsu  * GNU General Public License for more details.
1550656eecSMasami Hiramatsu  *
1650656eecSMasami Hiramatsu  * You should have received a copy of the GNU General Public License
1750656eecSMasami Hiramatsu  * along with this program; if not, write to the Free Software
1850656eecSMasami Hiramatsu  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1950656eecSMasami Hiramatsu  *
2050656eecSMasami Hiramatsu  */
2150656eecSMasami Hiramatsu 
2250656eecSMasami Hiramatsu #include <sys/utsname.h>
2350656eecSMasami Hiramatsu #include <sys/types.h>
2450656eecSMasami Hiramatsu #include <sys/stat.h>
2550656eecSMasami Hiramatsu #include <fcntl.h>
2650656eecSMasami Hiramatsu #include <errno.h>
2750656eecSMasami Hiramatsu #include <stdio.h>
2850656eecSMasami Hiramatsu #include <unistd.h>
2950656eecSMasami Hiramatsu #include <stdlib.h>
3050656eecSMasami Hiramatsu #include <string.h>
314de189feSMasami Hiramatsu #include <stdarg.h>
324de189feSMasami Hiramatsu #include <limits.h>
33e80711caSMasami Hiramatsu #include <elf.h>
3450656eecSMasami Hiramatsu 
3531facc5fSMasami Hiramatsu #include "util.h"
3650656eecSMasami Hiramatsu #include "event.h"
374de189feSMasami Hiramatsu #include "strlist.h"
3850656eecSMasami Hiramatsu #include "debug.h"
3972041334SMasami Hiramatsu #include "cache.h"
40631c9defSMasami Hiramatsu #include "color.h"
41e0faa8d3SMasami Hiramatsu #include "symbol.h"
42e0faa8d3SMasami Hiramatsu #include "thread.h"
434605eab3SJiri Olsa #include <api/fs/fs.h>
441d037ca1SIrina Tirdea #include "trace-event.h"	/* For __maybe_unused */
4550656eecSMasami Hiramatsu #include "probe-event.h"
464235b045SMasami Hiramatsu #include "probe-finder.h"
4792f6c72eSMasami Hiramatsu #include "probe-file.h"
48225466f1SSrikar Dronamraju #include "session.h"
4950656eecSMasami Hiramatsu 
5050656eecSMasami Hiramatsu #define MAX_CMDLEN 256
5150656eecSMasami Hiramatsu #define PERFPROBE_GROUP "probe"
5250656eecSMasami Hiramatsu 
53f4d7da49SMasami Hiramatsu bool probe_event_dry_run;	/* Dry run flag */
54ddb2f58fSMasami Hiramatsu struct probe_conf probe_conf;
55f4d7da49SMasami Hiramatsu 
56146a1439SMasami Hiramatsu #define semantic_error(msg ...) pr_err("Semantic error :" msg)
5750656eecSMasami Hiramatsu 
5892f6c72eSMasami Hiramatsu int e_snprintf(char *str, size_t size, const char *format, ...)
594de189feSMasami Hiramatsu {
604de189feSMasami Hiramatsu 	int ret;
614de189feSMasami Hiramatsu 	va_list ap;
624de189feSMasami Hiramatsu 	va_start(ap, format);
634de189feSMasami Hiramatsu 	ret = vsnprintf(str, size, format, ap);
644de189feSMasami Hiramatsu 	va_end(ap);
654de189feSMasami Hiramatsu 	if (ret >= (int)size)
664de189feSMasami Hiramatsu 		ret = -E2BIG;
674de189feSMasami Hiramatsu 	return ret;
684de189feSMasami Hiramatsu }
694de189feSMasami Hiramatsu 
70ee45b6c2SMasami Hiramatsu static struct machine *host_machine;
71e0faa8d3SMasami Hiramatsu 
72469b9b88SMasami Hiramatsu /* Initialize symbol maps and path of vmlinux/modules */
739bae1e8cSNamhyung Kim int init_probe_symbol_maps(bool user_only)
74e0faa8d3SMasami Hiramatsu {
75146a1439SMasami Hiramatsu 	int ret;
76146a1439SMasami Hiramatsu 
77e0faa8d3SMasami Hiramatsu 	symbol_conf.sort_by_name = true;
78680d926aSNamhyung Kim 	symbol_conf.allow_aliases = true;
790a7e6d1bSNamhyung Kim 	ret = symbol__init(NULL);
80146a1439SMasami Hiramatsu 	if (ret < 0) {
81146a1439SMasami Hiramatsu 		pr_debug("Failed to init symbol map.\n");
82146a1439SMasami Hiramatsu 		goto out;
83146a1439SMasami Hiramatsu 	}
84e0faa8d3SMasami Hiramatsu 
85ee45b6c2SMasami Hiramatsu 	if (host_machine || user_only)	/* already initialized */
86ee45b6c2SMasami Hiramatsu 		return 0;
87d28c6223SArnaldo Carvalho de Melo 
88ee45b6c2SMasami Hiramatsu 	if (symbol_conf.vmlinux_name)
89ee45b6c2SMasami Hiramatsu 		pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
90ee45b6c2SMasami Hiramatsu 
91ee45b6c2SMasami Hiramatsu 	host_machine = machine__new_host();
92ee45b6c2SMasami Hiramatsu 	if (!host_machine) {
93ee45b6c2SMasami Hiramatsu 		pr_debug("machine__new_host() failed.\n");
94ee45b6c2SMasami Hiramatsu 		symbol__exit();
95ee45b6c2SMasami Hiramatsu 		ret = -1;
96469b9b88SMasami Hiramatsu 	}
97146a1439SMasami Hiramatsu out:
98146a1439SMasami Hiramatsu 	if (ret < 0)
99146a1439SMasami Hiramatsu 		pr_warning("Failed to init vmlinux path.\n");
100146a1439SMasami Hiramatsu 	return ret;
101e0faa8d3SMasami Hiramatsu }
102e0faa8d3SMasami Hiramatsu 
1039bae1e8cSNamhyung Kim void exit_probe_symbol_maps(void)
104ee45b6c2SMasami Hiramatsu {
105ee45b6c2SMasami Hiramatsu 	machine__delete(host_machine);
106ee45b6c2SMasami Hiramatsu 	host_machine = NULL;
107ee45b6c2SMasami Hiramatsu 	symbol__exit();
108ee45b6c2SMasami Hiramatsu }
109ee45b6c2SMasami Hiramatsu 
110469b9b88SMasami Hiramatsu static struct symbol *__find_kernel_function_by_name(const char *name,
111469b9b88SMasami Hiramatsu 						     struct map **mapp)
112e0faa8d3SMasami Hiramatsu {
113be39db9fSArnaldo Carvalho de Melo 	return machine__find_kernel_function_by_name(host_machine, name, mapp);
114e0faa8d3SMasami Hiramatsu }
115469b9b88SMasami Hiramatsu 
1168f33f7deSMasami Hiramatsu static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
1178f33f7deSMasami Hiramatsu {
118be39db9fSArnaldo Carvalho de Melo 	return machine__find_kernel_function(host_machine, addr, mapp);
1198f33f7deSMasami Hiramatsu }
1208f33f7deSMasami Hiramatsu 
1218f33f7deSMasami Hiramatsu static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
1228f33f7deSMasami Hiramatsu {
1238f33f7deSMasami Hiramatsu 	/* kmap->ref_reloc_sym should be set if host_machine is initialized */
1248f33f7deSMasami Hiramatsu 	struct kmap *kmap;
125a5e813c6SArnaldo Carvalho de Melo 	struct map *map = machine__kernel_map(host_machine);
1268f33f7deSMasami Hiramatsu 
127be39db9fSArnaldo Carvalho de Melo 	if (map__load(map) < 0)
1288f33f7deSMasami Hiramatsu 		return NULL;
1298f33f7deSMasami Hiramatsu 
13077e65977SArnaldo Carvalho de Melo 	kmap = map__kmap(map);
131ba92732eSWang Nan 	if (!kmap)
132ba92732eSWang Nan 		return NULL;
1338f33f7deSMasami Hiramatsu 	return kmap->ref_reloc_sym;
1348f33f7deSMasami Hiramatsu }
1358f33f7deSMasami Hiramatsu 
1369b239a12SMasami Hiramatsu static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
1379b239a12SMasami Hiramatsu 					     bool reloc, bool reladdr)
1388f33f7deSMasami Hiramatsu {
1398f33f7deSMasami Hiramatsu 	struct ref_reloc_sym *reloc_sym;
1408f33f7deSMasami Hiramatsu 	struct symbol *sym;
1418f33f7deSMasami Hiramatsu 	struct map *map;
1428f33f7deSMasami Hiramatsu 
1438f33f7deSMasami Hiramatsu 	/* ref_reloc_sym is just a label. Need a special fix*/
1448f33f7deSMasami Hiramatsu 	reloc_sym = kernel_get_ref_reloc_sym();
1458f33f7deSMasami Hiramatsu 	if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
1469b239a12SMasami Hiramatsu 		*addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
1478f33f7deSMasami Hiramatsu 	else {
1488f33f7deSMasami Hiramatsu 		sym = __find_kernel_function_by_name(name, &map);
1499b239a12SMasami Hiramatsu 		if (!sym)
1509b239a12SMasami Hiramatsu 			return -ENOENT;
1519b239a12SMasami Hiramatsu 		*addr = map->unmap_ip(map, sym->start) -
1529b239a12SMasami Hiramatsu 			((reloc) ? 0 : map->reloc) -
1539b239a12SMasami Hiramatsu 			((reladdr) ? map->start : 0);
1548f33f7deSMasami Hiramatsu 	}
1558f33f7deSMasami Hiramatsu 	return 0;
1568f33f7deSMasami Hiramatsu }
1578f33f7deSMasami Hiramatsu 
158e80711caSMasami Hiramatsu static struct map *kernel_get_module_map(const char *module)
159e80711caSMasami Hiramatsu {
160ee45b6c2SMasami Hiramatsu 	struct map_groups *grp = &host_machine->kmaps;
1611eee78aeSArnaldo Carvalho de Melo 	struct maps *maps = &grp->maps[MAP__FUNCTION];
1624bb7123dSArnaldo Carvalho de Melo 	struct map *pos;
163e80711caSMasami Hiramatsu 
16414a8fd7cSMasami Hiramatsu 	/* A file path -- this is an offline module */
16514a8fd7cSMasami Hiramatsu 	if (module && strchr(module, '/'))
166eebc509bSMasami Hiramatsu 		return dso__new_map(module);
16714a8fd7cSMasami Hiramatsu 
168e80711caSMasami Hiramatsu 	if (!module)
169e80711caSMasami Hiramatsu 		module = "kernel";
170e80711caSMasami Hiramatsu 
1714bb7123dSArnaldo Carvalho de Melo 	for (pos = maps__first(maps); pos; pos = map__next(pos)) {
172cb3f3378SKonstantin Khlebnikov 		/* short_name is "[module]" */
173e80711caSMasami Hiramatsu 		if (strncmp(pos->dso->short_name + 1, module,
174cb3f3378SKonstantin Khlebnikov 			    pos->dso->short_name_len - 2) == 0 &&
175cb3f3378SKonstantin Khlebnikov 		    module[pos->dso->short_name_len - 2] == '\0') {
176eebc509bSMasami Hiramatsu 			map__get(pos);
177e80711caSMasami Hiramatsu 			return pos;
178e80711caSMasami Hiramatsu 		}
179e80711caSMasami Hiramatsu 	}
180e80711caSMasami Hiramatsu 	return NULL;
181e80711caSMasami Hiramatsu }
182e80711caSMasami Hiramatsu 
18399e608b5SRavi Bangoria struct map *get_target_map(const char *target, bool user)
1849b118acaSMasami Hiramatsu {
1859b118acaSMasami Hiramatsu 	/* Init maps of given executable or kernel */
1869b118acaSMasami Hiramatsu 	if (user)
1879b118acaSMasami Hiramatsu 		return dso__new_map(target);
1889b118acaSMasami Hiramatsu 	else
1899b118acaSMasami Hiramatsu 		return kernel_get_module_map(target);
1909b118acaSMasami Hiramatsu }
1919b118acaSMasami Hiramatsu 
192fb7345bbSMasami Hiramatsu static int convert_exec_to_group(const char *exec, char **result)
193fb7345bbSMasami Hiramatsu {
194fb7345bbSMasami Hiramatsu 	char *ptr1, *ptr2, *exec_copy;
195fb7345bbSMasami Hiramatsu 	char buf[64];
196fb7345bbSMasami Hiramatsu 	int ret;
197fb7345bbSMasami Hiramatsu 
198fb7345bbSMasami Hiramatsu 	exec_copy = strdup(exec);
199fb7345bbSMasami Hiramatsu 	if (!exec_copy)
200fb7345bbSMasami Hiramatsu 		return -ENOMEM;
201fb7345bbSMasami Hiramatsu 
202fb7345bbSMasami Hiramatsu 	ptr1 = basename(exec_copy);
203fb7345bbSMasami Hiramatsu 	if (!ptr1) {
204fb7345bbSMasami Hiramatsu 		ret = -EINVAL;
205fb7345bbSMasami Hiramatsu 		goto out;
206fb7345bbSMasami Hiramatsu 	}
207fb7345bbSMasami Hiramatsu 
208ead1a574SColin Ian King 	for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
20935726d3aSMasami Hiramatsu 		if (!isalnum(*ptr2) && *ptr2 != '_') {
210fb7345bbSMasami Hiramatsu 			*ptr2 = '\0';
21135726d3aSMasami Hiramatsu 			break;
21235726d3aSMasami Hiramatsu 		}
21335726d3aSMasami Hiramatsu 	}
21435726d3aSMasami Hiramatsu 
215fb7345bbSMasami Hiramatsu 	ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
216fb7345bbSMasami Hiramatsu 	if (ret < 0)
217fb7345bbSMasami Hiramatsu 		goto out;
218fb7345bbSMasami Hiramatsu 
219fb7345bbSMasami Hiramatsu 	*result = strdup(buf);
220fb7345bbSMasami Hiramatsu 	ret = *result ? 0 : -ENOMEM;
221fb7345bbSMasami Hiramatsu 
222fb7345bbSMasami Hiramatsu out:
223fb7345bbSMasami Hiramatsu 	free(exec_copy);
224fb7345bbSMasami Hiramatsu 	return ret;
225fb7345bbSMasami Hiramatsu }
226fb7345bbSMasami Hiramatsu 
2279b118acaSMasami Hiramatsu static void clear_perf_probe_point(struct perf_probe_point *pp)
2289b118acaSMasami Hiramatsu {
2299b118acaSMasami Hiramatsu 	free(pp->file);
2309b118acaSMasami Hiramatsu 	free(pp->function);
2319b118acaSMasami Hiramatsu 	free(pp->lazy_line);
2329b118acaSMasami Hiramatsu }
2339b118acaSMasami Hiramatsu 
234eb948e50SMasami Hiramatsu static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
235eb948e50SMasami Hiramatsu {
236eb948e50SMasami Hiramatsu 	int i;
237eb948e50SMasami Hiramatsu 
238eb948e50SMasami Hiramatsu 	for (i = 0; i < ntevs; i++)
239eb948e50SMasami Hiramatsu 		clear_probe_trace_event(tevs + i);
240eb948e50SMasami Hiramatsu }
241eb948e50SMasami Hiramatsu 
242b031220dSMasami Hiramatsu static bool kprobe_blacklist__listed(unsigned long address);
243b031220dSMasami Hiramatsu static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
244b031220dSMasami Hiramatsu {
2459b239a12SMasami Hiramatsu 	u64 etext_addr = 0;
2469b239a12SMasami Hiramatsu 	int ret;
2477c31bb8cSHe Kuang 
248b031220dSMasami Hiramatsu 	/* Get the address of _etext for checking non-probable text symbol */
2499b239a12SMasami Hiramatsu 	ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
2509b239a12SMasami Hiramatsu 						false, false);
2517c31bb8cSHe Kuang 
2529b239a12SMasami Hiramatsu 	if (ret == 0 && etext_addr < address)
253b031220dSMasami Hiramatsu 		pr_warning("%s is out of .text, skip it.\n", symbol);
254b031220dSMasami Hiramatsu 	else if (kprobe_blacklist__listed(address))
255b031220dSMasami Hiramatsu 		pr_warning("%s is blacklisted function, skip it.\n", symbol);
256b031220dSMasami Hiramatsu 	else
257b031220dSMasami Hiramatsu 		return false;
258b031220dSMasami Hiramatsu 
259b031220dSMasami Hiramatsu 	return true;
260b031220dSMasami Hiramatsu }
261b031220dSMasami Hiramatsu 
262c61fb959SRavi Bangoria /*
263c61fb959SRavi Bangoria  * @module can be module name of module file path. In case of path,
264c61fb959SRavi Bangoria  * inspect elf and find out what is actual module name.
265c61fb959SRavi Bangoria  * Caller has to free mod_name after using it.
266c61fb959SRavi Bangoria  */
267c61fb959SRavi Bangoria static char *find_module_name(const char *module)
268c61fb959SRavi Bangoria {
269c61fb959SRavi Bangoria 	int fd;
270c61fb959SRavi Bangoria 	Elf *elf;
271c61fb959SRavi Bangoria 	GElf_Ehdr ehdr;
272c61fb959SRavi Bangoria 	GElf_Shdr shdr;
273c61fb959SRavi Bangoria 	Elf_Data *data;
274c61fb959SRavi Bangoria 	Elf_Scn *sec;
275c61fb959SRavi Bangoria 	char *mod_name = NULL;
2761f2ed153SMasami Hiramatsu 	int name_offset;
277c61fb959SRavi Bangoria 
278c61fb959SRavi Bangoria 	fd = open(module, O_RDONLY);
279c61fb959SRavi Bangoria 	if (fd < 0)
280c61fb959SRavi Bangoria 		return NULL;
281c61fb959SRavi Bangoria 
282c61fb959SRavi Bangoria 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
283c61fb959SRavi Bangoria 	if (elf == NULL)
284c61fb959SRavi Bangoria 		goto elf_err;
285c61fb959SRavi Bangoria 
286c61fb959SRavi Bangoria 	if (gelf_getehdr(elf, &ehdr) == NULL)
287c61fb959SRavi Bangoria 		goto ret_err;
288c61fb959SRavi Bangoria 
289c61fb959SRavi Bangoria 	sec = elf_section_by_name(elf, &ehdr, &shdr,
290c61fb959SRavi Bangoria 			".gnu.linkonce.this_module", NULL);
291c61fb959SRavi Bangoria 	if (!sec)
292c61fb959SRavi Bangoria 		goto ret_err;
293c61fb959SRavi Bangoria 
294c61fb959SRavi Bangoria 	data = elf_getdata(sec, NULL);
295c61fb959SRavi Bangoria 	if (!data || !data->d_buf)
296c61fb959SRavi Bangoria 		goto ret_err;
297c61fb959SRavi Bangoria 
2981f2ed153SMasami Hiramatsu 	/*
2991f2ed153SMasami Hiramatsu 	 * NOTE:
3001f2ed153SMasami Hiramatsu 	 * '.gnu.linkonce.this_module' section of kernel module elf directly
3011f2ed153SMasami Hiramatsu 	 * maps to 'struct module' from linux/module.h. This section contains
3021f2ed153SMasami Hiramatsu 	 * actual module name which will be used by kernel after loading it.
3031f2ed153SMasami Hiramatsu 	 * But, we cannot use 'struct module' here since linux/module.h is not
3041f2ed153SMasami Hiramatsu 	 * exposed to user-space. Offset of 'name' has remained same from long
3051f2ed153SMasami Hiramatsu 	 * time, so hardcoding it here.
3061f2ed153SMasami Hiramatsu 	 */
3071f2ed153SMasami Hiramatsu 	if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
3081f2ed153SMasami Hiramatsu 		name_offset = 12;
3091f2ed153SMasami Hiramatsu 	else	/* expect ELFCLASS64 by default */
3101f2ed153SMasami Hiramatsu 		name_offset = 24;
3111f2ed153SMasami Hiramatsu 
3121f2ed153SMasami Hiramatsu 	mod_name = strdup((char *)data->d_buf + name_offset);
313c61fb959SRavi Bangoria 
314c61fb959SRavi Bangoria ret_err:
315c61fb959SRavi Bangoria 	elf_end(elf);
316c61fb959SRavi Bangoria elf_err:
317c61fb959SRavi Bangoria 	close(fd);
318c61fb959SRavi Bangoria 	return mod_name;
319c61fb959SRavi Bangoria }
320c61fb959SRavi Bangoria 
32189fe808aSIngo Molnar #ifdef HAVE_DWARF_SUPPORT
32260fb7742SWang Nan 
32360fb7742SWang Nan static int kernel_get_module_dso(const char *module, struct dso **pdso)
32460fb7742SWang Nan {
32560fb7742SWang Nan 	struct dso *dso;
32660fb7742SWang Nan 	struct map *map;
32760fb7742SWang Nan 	const char *vmlinux_name;
32860fb7742SWang Nan 	int ret = 0;
32960fb7742SWang Nan 
33060fb7742SWang Nan 	if (module) {
331266fa2b2SArnaldo Carvalho de Melo 		char module_name[128];
332266fa2b2SArnaldo Carvalho de Melo 
333266fa2b2SArnaldo Carvalho de Melo 		snprintf(module_name, sizeof(module_name), "[%s]", module);
334266fa2b2SArnaldo Carvalho de Melo 		map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
335266fa2b2SArnaldo Carvalho de Melo 		if (map) {
336266fa2b2SArnaldo Carvalho de Melo 			dso = map->dso;
33760fb7742SWang Nan 			goto found;
33860fb7742SWang Nan 		}
33960fb7742SWang Nan 		pr_debug("Failed to find module %s.\n", module);
34060fb7742SWang Nan 		return -ENOENT;
34160fb7742SWang Nan 	}
34260fb7742SWang Nan 
343a5e813c6SArnaldo Carvalho de Melo 	map = machine__kernel_map(host_machine);
34460fb7742SWang Nan 	dso = map->dso;
34560fb7742SWang Nan 
34660fb7742SWang Nan 	vmlinux_name = symbol_conf.vmlinux_name;
34760fb7742SWang Nan 	dso->load_errno = 0;
34860fb7742SWang Nan 	if (vmlinux_name)
349be39db9fSArnaldo Carvalho de Melo 		ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
35060fb7742SWang Nan 	else
351be39db9fSArnaldo Carvalho de Melo 		ret = dso__load_vmlinux_path(dso, map);
35260fb7742SWang Nan found:
35360fb7742SWang Nan 	*pdso = dso;
35460fb7742SWang Nan 	return ret;
35560fb7742SWang Nan }
35660fb7742SWang Nan 
3579b118acaSMasami Hiramatsu /*
3589b118acaSMasami Hiramatsu  * Some binaries like glibc have special symbols which are on the symbol
3599b118acaSMasami Hiramatsu  * table, but not in the debuginfo. If we can find the address of the
3609b118acaSMasami Hiramatsu  * symbol from map, we can translate the address back to the probe point.
3619b118acaSMasami Hiramatsu  */
3629b118acaSMasami Hiramatsu static int find_alternative_probe_point(struct debuginfo *dinfo,
3639b118acaSMasami Hiramatsu 					struct perf_probe_point *pp,
3649b118acaSMasami Hiramatsu 					struct perf_probe_point *result,
3659b118acaSMasami Hiramatsu 					const char *target, bool uprobes)
3669b118acaSMasami Hiramatsu {
3679b118acaSMasami Hiramatsu 	struct map *map = NULL;
3689b118acaSMasami Hiramatsu 	struct symbol *sym;
3699b118acaSMasami Hiramatsu 	u64 address = 0;
3709b118acaSMasami Hiramatsu 	int ret = -ENOENT;
3719b118acaSMasami Hiramatsu 
3729b118acaSMasami Hiramatsu 	/* This can work only for function-name based one */
3739b118acaSMasami Hiramatsu 	if (!pp->function || pp->file)
3749b118acaSMasami Hiramatsu 		return -ENOTSUP;
3759b118acaSMasami Hiramatsu 
3769b118acaSMasami Hiramatsu 	map = get_target_map(target, uprobes);
3779b118acaSMasami Hiramatsu 	if (!map)
3789b118acaSMasami Hiramatsu 		return -EINVAL;
3799b118acaSMasami Hiramatsu 
3809b118acaSMasami Hiramatsu 	/* Find the address of given function */
3819b118acaSMasami Hiramatsu 	map__for_each_symbol_by_name(map, pp->function, sym) {
382e6d7c91cSMasami Hiramatsu 		if (uprobes)
3839b118acaSMasami Hiramatsu 			address = sym->start;
384e6d7c91cSMasami Hiramatsu 		else
3858e34189bSMasami Hiramatsu 			address = map->unmap_ip(map, sym->start) - map->reloc;
3869b118acaSMasami Hiramatsu 		break;
3879b118acaSMasami Hiramatsu 	}
3889b118acaSMasami Hiramatsu 	if (!address) {
3899b118acaSMasami Hiramatsu 		ret = -ENOENT;
3909b118acaSMasami Hiramatsu 		goto out;
3919b118acaSMasami Hiramatsu 	}
392f6c15621SWang Nan 	pr_debug("Symbol %s address found : %" PRIx64 "\n",
393f6c15621SWang Nan 			pp->function, address);
3949b118acaSMasami Hiramatsu 
3959b118acaSMasami Hiramatsu 	ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
3969b118acaSMasami Hiramatsu 					  result);
3979b118acaSMasami Hiramatsu 	if (ret <= 0)
3989b118acaSMasami Hiramatsu 		ret = (!ret) ? -ENOENT : ret;
3999b118acaSMasami Hiramatsu 	else {
4009b118acaSMasami Hiramatsu 		result->offset += pp->offset;
4019b118acaSMasami Hiramatsu 		result->line += pp->line;
4029d7b45c5SHe Kuang 		result->retprobe = pp->retprobe;
4039b118acaSMasami Hiramatsu 		ret = 0;
4049b118acaSMasami Hiramatsu 	}
4059b118acaSMasami Hiramatsu 
4069b118acaSMasami Hiramatsu out:
407eebc509bSMasami Hiramatsu 	map__put(map);
4089b118acaSMasami Hiramatsu 	return ret;
4099b118acaSMasami Hiramatsu 
4109b118acaSMasami Hiramatsu }
4119b118acaSMasami Hiramatsu 
4129b118acaSMasami Hiramatsu static int get_alternative_probe_event(struct debuginfo *dinfo,
4139b118acaSMasami Hiramatsu 				       struct perf_probe_event *pev,
41444225521SMasami Hiramatsu 				       struct perf_probe_point *tmp)
4159b118acaSMasami Hiramatsu {
4169b118acaSMasami Hiramatsu 	int ret;
4179b118acaSMasami Hiramatsu 
4189b118acaSMasami Hiramatsu 	memcpy(tmp, &pev->point, sizeof(*tmp));
4199b118acaSMasami Hiramatsu 	memset(&pev->point, 0, sizeof(pev->point));
4209b118acaSMasami Hiramatsu 	ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
42144225521SMasami Hiramatsu 					   pev->target, pev->uprobes);
4229b118acaSMasami Hiramatsu 	if (ret < 0)
4239b118acaSMasami Hiramatsu 		memcpy(&pev->point, tmp, sizeof(*tmp));
4249b118acaSMasami Hiramatsu 
4259b118acaSMasami Hiramatsu 	return ret;
4269b118acaSMasami Hiramatsu }
427a15ad2f5SMasami Hiramatsu 
428811dd2aeSMasami Hiramatsu static int get_alternative_line_range(struct debuginfo *dinfo,
429811dd2aeSMasami Hiramatsu 				      struct line_range *lr,
430811dd2aeSMasami Hiramatsu 				      const char *target, bool user)
431811dd2aeSMasami Hiramatsu {
4326d4a4896SDavid Ahern 	struct perf_probe_point pp = { .function = lr->function,
4336d4a4896SDavid Ahern 				       .file = lr->file,
4346d4a4896SDavid Ahern 				       .line = lr->start };
4356d4a4896SDavid Ahern 	struct perf_probe_point result;
436811dd2aeSMasami Hiramatsu 	int ret, len = 0;
437811dd2aeSMasami Hiramatsu 
4386d4a4896SDavid Ahern 	memset(&result, 0, sizeof(result));
4396d4a4896SDavid Ahern 
440811dd2aeSMasami Hiramatsu 	if (lr->end != INT_MAX)
441811dd2aeSMasami Hiramatsu 		len = lr->end - lr->start;
442811dd2aeSMasami Hiramatsu 	ret = find_alternative_probe_point(dinfo, &pp, &result,
443811dd2aeSMasami Hiramatsu 					   target, user);
444811dd2aeSMasami Hiramatsu 	if (!ret) {
445811dd2aeSMasami Hiramatsu 		lr->function = result.function;
446811dd2aeSMasami Hiramatsu 		lr->file = result.file;
447811dd2aeSMasami Hiramatsu 		lr->start = result.line;
448811dd2aeSMasami Hiramatsu 		if (lr->end != INT_MAX)
449811dd2aeSMasami Hiramatsu 			lr->end = lr->start + len;
450811dd2aeSMasami Hiramatsu 		clear_perf_probe_point(&pp);
451811dd2aeSMasami Hiramatsu 	}
452811dd2aeSMasami Hiramatsu 	return ret;
453811dd2aeSMasami Hiramatsu }
454811dd2aeSMasami Hiramatsu 
455ff741783SMasami Hiramatsu /* Open new debuginfo of given module */
45692561cb7SMasami Hiramatsu static struct debuginfo *open_debuginfo(const char *module, bool silent)
457469b9b88SMasami Hiramatsu {
458a15ad2f5SMasami Hiramatsu 	const char *path = module;
459419e8738SMasami Hiramatsu 	char reason[STRERR_BUFSIZE];
460419e8738SMasami Hiramatsu 	struct debuginfo *ret = NULL;
461419e8738SMasami Hiramatsu 	struct dso *dso = NULL;
462419e8738SMasami Hiramatsu 	int err;
46314a8fd7cSMasami Hiramatsu 
464a15ad2f5SMasami Hiramatsu 	if (!module || !strchr(module, '/')) {
465419e8738SMasami Hiramatsu 		err = kernel_get_module_dso(module, &dso);
466419e8738SMasami Hiramatsu 		if (err < 0) {
467419e8738SMasami Hiramatsu 			if (!dso || dso->load_errno == 0) {
468c8b5f2c9SArnaldo Carvalho de Melo 				if (!str_error_r(-err, reason, STRERR_BUFSIZE))
469419e8738SMasami Hiramatsu 					strcpy(reason, "(unknown)");
470419e8738SMasami Hiramatsu 			} else
471419e8738SMasami Hiramatsu 				dso__strerror_load(dso, reason, STRERR_BUFSIZE);
47292561cb7SMasami Hiramatsu 			if (!silent)
473419e8738SMasami Hiramatsu 				pr_err("Failed to find the path for %s: %s\n",
474419e8738SMasami Hiramatsu 					module ?: "kernel", reason);
475ff741783SMasami Hiramatsu 			return NULL;
476469b9b88SMasami Hiramatsu 		}
477419e8738SMasami Hiramatsu 		path = dso->long_name;
47814a8fd7cSMasami Hiramatsu 	}
47992561cb7SMasami Hiramatsu 	ret = debuginfo__new(path);
48092561cb7SMasami Hiramatsu 	if (!ret && !silent) {
48192561cb7SMasami Hiramatsu 		pr_warning("The %s file has no debug information.\n", path);
48292561cb7SMasami Hiramatsu 		if (!module || !strtailcmp(path, ".ko"))
48392561cb7SMasami Hiramatsu 			pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
48492561cb7SMasami Hiramatsu 		else
48592561cb7SMasami Hiramatsu 			pr_warning("Rebuild with -g, ");
48692561cb7SMasami Hiramatsu 		pr_warning("or install an appropriate debuginfo package.\n");
487e0faa8d3SMasami Hiramatsu 	}
48892561cb7SMasami Hiramatsu 	return ret;
48992561cb7SMasami Hiramatsu }
49092561cb7SMasami Hiramatsu 
4917737af01SMasami Hiramatsu /* For caching the last debuginfo */
4927737af01SMasami Hiramatsu static struct debuginfo *debuginfo_cache;
4937737af01SMasami Hiramatsu static char *debuginfo_cache_path;
4947737af01SMasami Hiramatsu 
4957737af01SMasami Hiramatsu static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
4967737af01SMasami Hiramatsu {
49720f49859SMasami Hiramatsu 	const char *path = module;
49820f49859SMasami Hiramatsu 
49920f49859SMasami Hiramatsu 	/* If the module is NULL, it should be the kernel. */
50020f49859SMasami Hiramatsu 	if (!module)
50120f49859SMasami Hiramatsu 		path = "kernel";
50220f49859SMasami Hiramatsu 
50320f49859SMasami Hiramatsu 	if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
5047737af01SMasami Hiramatsu 		goto out;
5057737af01SMasami Hiramatsu 
5067737af01SMasami Hiramatsu 	/* Copy module path */
5077737af01SMasami Hiramatsu 	free(debuginfo_cache_path);
50820f49859SMasami Hiramatsu 	debuginfo_cache_path = strdup(path);
5097737af01SMasami Hiramatsu 	if (!debuginfo_cache_path) {
5107737af01SMasami Hiramatsu 		debuginfo__delete(debuginfo_cache);
5117737af01SMasami Hiramatsu 		debuginfo_cache = NULL;
5127737af01SMasami Hiramatsu 		goto out;
5137737af01SMasami Hiramatsu 	}
5147737af01SMasami Hiramatsu 
5157737af01SMasami Hiramatsu 	debuginfo_cache = open_debuginfo(module, silent);
5167737af01SMasami Hiramatsu 	if (!debuginfo_cache)
5177737af01SMasami Hiramatsu 		zfree(&debuginfo_cache_path);
5187737af01SMasami Hiramatsu out:
5197737af01SMasami Hiramatsu 	return debuginfo_cache;
5207737af01SMasami Hiramatsu }
5217737af01SMasami Hiramatsu 
5227737af01SMasami Hiramatsu static void debuginfo_cache__exit(void)
5237737af01SMasami Hiramatsu {
5247737af01SMasami Hiramatsu 	debuginfo__delete(debuginfo_cache);
5257737af01SMasami Hiramatsu 	debuginfo_cache = NULL;
5267737af01SMasami Hiramatsu 	zfree(&debuginfo_cache_path);
5277737af01SMasami Hiramatsu }
5287737af01SMasami Hiramatsu 
5294b4da7f7SMasami Hiramatsu 
53099ca4233SMasami Hiramatsu static int get_text_start_address(const char *exec, unsigned long *address)
53199ca4233SMasami Hiramatsu {
53299ca4233SMasami Hiramatsu 	Elf *elf;
53399ca4233SMasami Hiramatsu 	GElf_Ehdr ehdr;
53499ca4233SMasami Hiramatsu 	GElf_Shdr shdr;
53599ca4233SMasami Hiramatsu 	int fd, ret = -ENOENT;
53699ca4233SMasami Hiramatsu 
53799ca4233SMasami Hiramatsu 	fd = open(exec, O_RDONLY);
53899ca4233SMasami Hiramatsu 	if (fd < 0)
53999ca4233SMasami Hiramatsu 		return -errno;
54099ca4233SMasami Hiramatsu 
54199ca4233SMasami Hiramatsu 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
542062d6c2aSMasami Hiramatsu 	if (elf == NULL) {
543062d6c2aSMasami Hiramatsu 		ret = -EINVAL;
544062d6c2aSMasami Hiramatsu 		goto out_close;
545062d6c2aSMasami Hiramatsu 	}
54699ca4233SMasami Hiramatsu 
54799ca4233SMasami Hiramatsu 	if (gelf_getehdr(elf, &ehdr) == NULL)
54899ca4233SMasami Hiramatsu 		goto out;
54999ca4233SMasami Hiramatsu 
55099ca4233SMasami Hiramatsu 	if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
55199ca4233SMasami Hiramatsu 		goto out;
55299ca4233SMasami Hiramatsu 
55399ca4233SMasami Hiramatsu 	*address = shdr.sh_addr - shdr.sh_offset;
55499ca4233SMasami Hiramatsu 	ret = 0;
55599ca4233SMasami Hiramatsu out:
55699ca4233SMasami Hiramatsu 	elf_end(elf);
557062d6c2aSMasami Hiramatsu out_close:
558062d6c2aSMasami Hiramatsu 	close(fd);
559062d6c2aSMasami Hiramatsu 
56099ca4233SMasami Hiramatsu 	return ret;
56199ca4233SMasami Hiramatsu }
56299ca4233SMasami Hiramatsu 
5635a6f6314SMasami Hiramatsu /*
5645a6f6314SMasami Hiramatsu  * Convert trace point to probe point with debuginfo
5655a6f6314SMasami Hiramatsu  */
5665a6f6314SMasami Hiramatsu static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
5675a6f6314SMasami Hiramatsu 					    struct perf_probe_point *pp,
5685a6f6314SMasami Hiramatsu 					    bool is_kprobe)
5695a6f6314SMasami Hiramatsu {
5705a6f6314SMasami Hiramatsu 	struct debuginfo *dinfo = NULL;
5715a6f6314SMasami Hiramatsu 	unsigned long stext = 0;
5725a6f6314SMasami Hiramatsu 	u64 addr = tp->address;
5735a6f6314SMasami Hiramatsu 	int ret = -ENOENT;
5745a6f6314SMasami Hiramatsu 
5755a6f6314SMasami Hiramatsu 	/* convert the address to dwarf address */
5765a6f6314SMasami Hiramatsu 	if (!is_kprobe) {
5775a6f6314SMasami Hiramatsu 		if (!addr) {
5785a6f6314SMasami Hiramatsu 			ret = -EINVAL;
5795a6f6314SMasami Hiramatsu 			goto error;
5805a6f6314SMasami Hiramatsu 		}
5815a6f6314SMasami Hiramatsu 		ret = get_text_start_address(tp->module, &stext);
5825a6f6314SMasami Hiramatsu 		if (ret < 0)
5835a6f6314SMasami Hiramatsu 			goto error;
5845a6f6314SMasami Hiramatsu 		addr += stext;
585e486367fSWang Nan 	} else if (tp->symbol) {
5869b239a12SMasami Hiramatsu 		/* If the module is given, this returns relative address */
5879b239a12SMasami Hiramatsu 		ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
5889b239a12SMasami Hiramatsu 							false, !!tp->module);
5899b239a12SMasami Hiramatsu 		if (ret != 0)
5905a6f6314SMasami Hiramatsu 			goto error;
5915a6f6314SMasami Hiramatsu 		addr += tp->offset;
5925a6f6314SMasami Hiramatsu 	}
5935a6f6314SMasami Hiramatsu 
5945a6f6314SMasami Hiramatsu 	pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
5955a6f6314SMasami Hiramatsu 		 tp->module ? : "kernel");
5965a6f6314SMasami Hiramatsu 
5977737af01SMasami Hiramatsu 	dinfo = debuginfo_cache__open(tp->module, verbose == 0);
5987737af01SMasami Hiramatsu 	if (dinfo)
5995a6f6314SMasami Hiramatsu 		ret = debuginfo__find_probe_point(dinfo,
6005a6f6314SMasami Hiramatsu 						 (unsigned long)addr, pp);
6017737af01SMasami Hiramatsu 	else
6025a6f6314SMasami Hiramatsu 		ret = -ENOENT;
6035a6f6314SMasami Hiramatsu 
6045a6f6314SMasami Hiramatsu 	if (ret > 0) {
6055a6f6314SMasami Hiramatsu 		pp->retprobe = tp->retprobe;
6065a6f6314SMasami Hiramatsu 		return 0;
6075a6f6314SMasami Hiramatsu 	}
6085a6f6314SMasami Hiramatsu error:
6095a6f6314SMasami Hiramatsu 	pr_debug("Failed to find corresponding probes from debuginfo.\n");
6105a6f6314SMasami Hiramatsu 	return ret ? : -ENOENT;
6115a6f6314SMasami Hiramatsu }
6125a6f6314SMasami Hiramatsu 
6138a937a25SMasami Hiramatsu /*
6148a937a25SMasami Hiramatsu  * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
6158a937a25SMasami Hiramatsu  * and generate new symbols with suffixes such as .constprop.N or .isra.N
6168a937a25SMasami Hiramatsu  * etc. Since those symbols are not recorded in DWARF, we have to find
6178a937a25SMasami Hiramatsu  * correct generated symbols from offline ELF binary.
6188a937a25SMasami Hiramatsu  * For online kernel or uprobes we don't need this because those are
6198a937a25SMasami Hiramatsu  * rebased on _text, or already a section relative address.
6208a937a25SMasami Hiramatsu  */
6218a937a25SMasami Hiramatsu static int
6228a937a25SMasami Hiramatsu post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
6238a937a25SMasami Hiramatsu 					int ntevs, const char *pathname)
6248a937a25SMasami Hiramatsu {
6258a937a25SMasami Hiramatsu 	struct symbol *sym;
6268a937a25SMasami Hiramatsu 	struct map *map;
6278a937a25SMasami Hiramatsu 	unsigned long stext = 0;
6288a937a25SMasami Hiramatsu 	u64 addr;
6298a937a25SMasami Hiramatsu 	int i;
6308a937a25SMasami Hiramatsu 
6318a937a25SMasami Hiramatsu 	/* Prepare a map for offline binary */
6328a937a25SMasami Hiramatsu 	map = dso__new_map(pathname);
6338a937a25SMasami Hiramatsu 	if (!map || get_text_start_address(pathname, &stext) < 0) {
6348a937a25SMasami Hiramatsu 		pr_warning("Failed to get ELF symbols for %s\n", pathname);
6358a937a25SMasami Hiramatsu 		return -EINVAL;
6368a937a25SMasami Hiramatsu 	}
6378a937a25SMasami Hiramatsu 
6388a937a25SMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
6398a937a25SMasami Hiramatsu 		addr = tevs[i].point.address + tevs[i].point.offset - stext;
6408a937a25SMasami Hiramatsu 		sym = map__find_symbol(map, addr);
6418a937a25SMasami Hiramatsu 		if (!sym)
6428a937a25SMasami Hiramatsu 			continue;
6438a937a25SMasami Hiramatsu 		if (!strcmp(sym->name, tevs[i].point.symbol))
6448a937a25SMasami Hiramatsu 			continue;
6458a937a25SMasami Hiramatsu 		/* If we have no realname, use symbol for it */
6468a937a25SMasami Hiramatsu 		if (!tevs[i].point.realname)
6478a937a25SMasami Hiramatsu 			tevs[i].point.realname = tevs[i].point.symbol;
6488a937a25SMasami Hiramatsu 		else
6498a937a25SMasami Hiramatsu 			free(tevs[i].point.symbol);
6508a937a25SMasami Hiramatsu 		tevs[i].point.symbol = strdup(sym->name);
6518a937a25SMasami Hiramatsu 		tevs[i].point.offset = addr - sym->start;
6528a937a25SMasami Hiramatsu 	}
6538a937a25SMasami Hiramatsu 	map__put(map);
6548a937a25SMasami Hiramatsu 
6558a937a25SMasami Hiramatsu 	return 0;
6568a937a25SMasami Hiramatsu }
6578a937a25SMasami Hiramatsu 
658fb7345bbSMasami Hiramatsu static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
659fb7345bbSMasami Hiramatsu 					  int ntevs, const char *exec)
660fb7345bbSMasami Hiramatsu {
661fb7345bbSMasami Hiramatsu 	int i, ret = 0;
662eb948e50SMasami Hiramatsu 	unsigned long stext = 0;
663fb7345bbSMasami Hiramatsu 
664fb7345bbSMasami Hiramatsu 	if (!exec)
665fb7345bbSMasami Hiramatsu 		return 0;
666fb7345bbSMasami Hiramatsu 
667fb7345bbSMasami Hiramatsu 	ret = get_text_start_address(exec, &stext);
668fb7345bbSMasami Hiramatsu 	if (ret < 0)
669fb7345bbSMasami Hiramatsu 		return ret;
670fb7345bbSMasami Hiramatsu 
671fb7345bbSMasami Hiramatsu 	for (i = 0; i < ntevs && ret >= 0; i++) {
672981a2379SMasami Hiramatsu 		/* point.address is the addres of point.symbol + point.offset */
673eb948e50SMasami Hiramatsu 		tevs[i].point.address -= stext;
674fb7345bbSMasami Hiramatsu 		tevs[i].point.module = strdup(exec);
675eb948e50SMasami Hiramatsu 		if (!tevs[i].point.module) {
676fb7345bbSMasami Hiramatsu 			ret = -ENOMEM;
677fb7345bbSMasami Hiramatsu 			break;
678fb7345bbSMasami Hiramatsu 		}
679fb7345bbSMasami Hiramatsu 		tevs[i].uprobes = true;
680fb7345bbSMasami Hiramatsu 	}
681fb7345bbSMasami Hiramatsu 
682fb7345bbSMasami Hiramatsu 	return ret;
683fb7345bbSMasami Hiramatsu }
684fb7345bbSMasami Hiramatsu 
685190b57fcSMasami Hiramatsu static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
686190b57fcSMasami Hiramatsu 					    int ntevs, const char *module)
687190b57fcSMasami Hiramatsu {
68814a8fd7cSMasami Hiramatsu 	int i, ret = 0;
68963a29613SRavi Bangoria 	char *mod_name = NULL;
69014a8fd7cSMasami Hiramatsu 
69114a8fd7cSMasami Hiramatsu 	if (!module)
69214a8fd7cSMasami Hiramatsu 		return 0;
69314a8fd7cSMasami Hiramatsu 
69463a29613SRavi Bangoria 	mod_name = find_module_name(module);
69514a8fd7cSMasami Hiramatsu 
696190b57fcSMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
69763a29613SRavi Bangoria 		tevs[i].point.module =
69863a29613SRavi Bangoria 			strdup(mod_name ? mod_name : module);
69914a8fd7cSMasami Hiramatsu 		if (!tevs[i].point.module) {
70014a8fd7cSMasami Hiramatsu 			ret = -ENOMEM;
70114a8fd7cSMasami Hiramatsu 			break;
702190b57fcSMasami Hiramatsu 		}
70314a8fd7cSMasami Hiramatsu 	}
70414a8fd7cSMasami Hiramatsu 
70563a29613SRavi Bangoria 	free(mod_name);
70614a8fd7cSMasami Hiramatsu 	return ret;
707190b57fcSMasami Hiramatsu }
708190b57fcSMasami Hiramatsu 
709d820456dSRavi Bangoria static int
710d820456dSRavi Bangoria post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
711d820456dSRavi Bangoria 				       int ntevs)
712dfef99cdSMasami Hiramatsu {
713dfef99cdSMasami Hiramatsu 	struct ref_reloc_sym *reloc_sym;
714dfef99cdSMasami Hiramatsu 	char *tmp;
7155a51fcd1SMasami Hiramatsu 	int i, skipped = 0;
716dfef99cdSMasami Hiramatsu 
717428aff82SMasami Hiramatsu 	/* Skip post process if the target is an offline kernel */
718428aff82SMasami Hiramatsu 	if (symbol_conf.ignore_vmlinux_buildid)
7198a937a25SMasami Hiramatsu 		return post_process_offline_probe_trace_events(tevs, ntevs,
7208a937a25SMasami Hiramatsu 						symbol_conf.vmlinux_name);
721428aff82SMasami Hiramatsu 
7228f33f7deSMasami Hiramatsu 	reloc_sym = kernel_get_ref_reloc_sym();
723dfef99cdSMasami Hiramatsu 	if (!reloc_sym) {
724dfef99cdSMasami Hiramatsu 		pr_warning("Relocated base symbol is not found!\n");
725dfef99cdSMasami Hiramatsu 		return -EINVAL;
726dfef99cdSMasami Hiramatsu 	}
727dfef99cdSMasami Hiramatsu 
728dfef99cdSMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
729b031220dSMasami Hiramatsu 		if (!tevs[i].point.address || tevs[i].point.retprobe)
730b031220dSMasami Hiramatsu 			continue;
7315a51fcd1SMasami Hiramatsu 		/* If we found a wrong one, mark it by NULL symbol */
732b031220dSMasami Hiramatsu 		if (kprobe_warn_out_range(tevs[i].point.symbol,
733b031220dSMasami Hiramatsu 					  tevs[i].point.address)) {
7345a51fcd1SMasami Hiramatsu 			tmp = NULL;
7355a51fcd1SMasami Hiramatsu 			skipped++;
7365a51fcd1SMasami Hiramatsu 		} else {
737dfef99cdSMasami Hiramatsu 			tmp = strdup(reloc_sym->name);
738dfef99cdSMasami Hiramatsu 			if (!tmp)
739dfef99cdSMasami Hiramatsu 				return -ENOMEM;
7405a51fcd1SMasami Hiramatsu 		}
7414c859351SMasami Hiramatsu 		/* If we have no realname, use symbol for it */
7424c859351SMasami Hiramatsu 		if (!tevs[i].point.realname)
7434c859351SMasami Hiramatsu 			tevs[i].point.realname = tevs[i].point.symbol;
7444c859351SMasami Hiramatsu 		else
745dfef99cdSMasami Hiramatsu 			free(tevs[i].point.symbol);
746dfef99cdSMasami Hiramatsu 		tevs[i].point.symbol = tmp;
747dfef99cdSMasami Hiramatsu 		tevs[i].point.offset = tevs[i].point.address -
748dfef99cdSMasami Hiramatsu 				       reloc_sym->unrelocated_addr;
749dfef99cdSMasami Hiramatsu 	}
7505a51fcd1SMasami Hiramatsu 	return skipped;
751dfef99cdSMasami Hiramatsu }
752dfef99cdSMasami Hiramatsu 
75399e608b5SRavi Bangoria void __weak
75499e608b5SRavi Bangoria arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
75599e608b5SRavi Bangoria 				      int ntevs __maybe_unused)
75699e608b5SRavi Bangoria {
75799e608b5SRavi Bangoria }
75899e608b5SRavi Bangoria 
759d820456dSRavi Bangoria /* Post processing the probe events */
76099e608b5SRavi Bangoria static int post_process_probe_trace_events(struct perf_probe_event *pev,
76199e608b5SRavi Bangoria 					   struct probe_trace_event *tevs,
762d820456dSRavi Bangoria 					   int ntevs, const char *module,
763d820456dSRavi Bangoria 					   bool uprobe)
764d820456dSRavi Bangoria {
76599e608b5SRavi Bangoria 	int ret;
76699e608b5SRavi Bangoria 
767d820456dSRavi Bangoria 	if (uprobe)
76899e608b5SRavi Bangoria 		ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
76999e608b5SRavi Bangoria 	else if (module)
770d820456dSRavi Bangoria 		/* Currently ref_reloc_sym based probe is not for drivers */
77199e608b5SRavi Bangoria 		ret = add_module_to_probe_trace_events(tevs, ntevs, module);
77299e608b5SRavi Bangoria 	else
77399e608b5SRavi Bangoria 		ret = post_process_kernel_probe_trace_events(tevs, ntevs);
774d820456dSRavi Bangoria 
77599e608b5SRavi Bangoria 	if (ret >= 0)
77699e608b5SRavi Bangoria 		arch__post_process_probe_trace_events(pev, ntevs);
77799e608b5SRavi Bangoria 
77899e608b5SRavi Bangoria 	return ret;
779d820456dSRavi Bangoria }
780d820456dSRavi Bangoria 
7814b4da7f7SMasami Hiramatsu /* Try to find perf_probe_event with debuginfo */
7820e60836bSSrikar Dronamraju static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
783ddb2f58fSMasami Hiramatsu 					  struct probe_trace_event **tevs)
7844b4da7f7SMasami Hiramatsu {
7854b4da7f7SMasami Hiramatsu 	bool need_dwarf = perf_probe_event_need_dwarf(pev);
7869b118acaSMasami Hiramatsu 	struct perf_probe_point tmp;
787225466f1SSrikar Dronamraju 	struct debuginfo *dinfo;
788190b57fcSMasami Hiramatsu 	int ntevs, ret = 0;
7894b4da7f7SMasami Hiramatsu 
79044225521SMasami Hiramatsu 	dinfo = open_debuginfo(pev->target, !need_dwarf);
791ff741783SMasami Hiramatsu 	if (!dinfo) {
79292561cb7SMasami Hiramatsu 		if (need_dwarf)
793ff741783SMasami Hiramatsu 			return -ENOENT;
794ff741783SMasami Hiramatsu 		pr_debug("Could not open debuginfo. Try to use symbols.\n");
7954b4da7f7SMasami Hiramatsu 		return 0;
7964b4da7f7SMasami Hiramatsu 	}
7974b4da7f7SMasami Hiramatsu 
798dfef99cdSMasami Hiramatsu 	pr_debug("Try to find probe point from debuginfo.\n");
799ff741783SMasami Hiramatsu 	/* Searching trace events corresponding to a probe event */
800ddb2f58fSMasami Hiramatsu 	ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
801ff741783SMasami Hiramatsu 
8029b118acaSMasami Hiramatsu 	if (ntevs == 0)	{  /* Not found, retry with an alternative */
80344225521SMasami Hiramatsu 		ret = get_alternative_probe_event(dinfo, pev, &tmp);
8049b118acaSMasami Hiramatsu 		if (!ret) {
805ddb2f58fSMasami Hiramatsu 			ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
8069b118acaSMasami Hiramatsu 			/*
8079b118acaSMasami Hiramatsu 			 * Write back to the original probe_event for
8089b118acaSMasami Hiramatsu 			 * setting appropriate (user given) event name
8099b118acaSMasami Hiramatsu 			 */
8109b118acaSMasami Hiramatsu 			clear_perf_probe_point(&pev->point);
8119b118acaSMasami Hiramatsu 			memcpy(&pev->point, &tmp, sizeof(tmp));
8129b118acaSMasami Hiramatsu 		}
8139b118acaSMasami Hiramatsu 	}
8149b118acaSMasami Hiramatsu 
815ff741783SMasami Hiramatsu 	debuginfo__delete(dinfo);
8164b4da7f7SMasami Hiramatsu 
817146a1439SMasami Hiramatsu 	if (ntevs > 0) {	/* Succeeded to find trace events */
818dfef99cdSMasami Hiramatsu 		pr_debug("Found %d probe_trace_events.\n", ntevs);
81999e608b5SRavi Bangoria 		ret = post_process_probe_trace_events(pev, *tevs, ntevs,
82044225521SMasami Hiramatsu 						pev->target, pev->uprobes);
8215a51fcd1SMasami Hiramatsu 		if (ret < 0 || ret == ntevs) {
822981d05adSMasami Hiramatsu 			clear_probe_trace_events(*tevs, ntevs);
823981d05adSMasami Hiramatsu 			zfree(tevs);
824981d05adSMasami Hiramatsu 		}
8255a51fcd1SMasami Hiramatsu 		if (ret != ntevs)
826190b57fcSMasami Hiramatsu 			return ret < 0 ? ret : ntevs;
8275a51fcd1SMasami Hiramatsu 		ntevs = 0;
8285a51fcd1SMasami Hiramatsu 		/* Fall through */
829146a1439SMasami Hiramatsu 	}
8304b4da7f7SMasami Hiramatsu 
831146a1439SMasami Hiramatsu 	if (ntevs == 0)	{	/* No error but failed to find probe point. */
8320687eba7SMasami Hiramatsu 		pr_warning("Probe point '%s' not found.\n",
8334b4da7f7SMasami Hiramatsu 			   synthesize_perf_probe_point(&pev->point));
834146a1439SMasami Hiramatsu 		return -ENOENT;
835146a1439SMasami Hiramatsu 	}
836146a1439SMasami Hiramatsu 	/* Error path : ntevs < 0 */
83715eca306SMasami Hiramatsu 	pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
8381c0bd0e8SWang Nan 	if (ntevs < 0) {
8391c0bd0e8SWang Nan 		if (ntevs == -EBADF)
84015eca306SMasami Hiramatsu 			pr_warning("Warning: No dwarf info found in the vmlinux - "
84115eca306SMasami Hiramatsu 				"please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
84215eca306SMasami Hiramatsu 		if (!need_dwarf) {
8430e43e5d2SMasami Hiramatsu 			pr_debug("Trying to use symbols.\n");
8444b4da7f7SMasami Hiramatsu 			return 0;
8454b4da7f7SMasami Hiramatsu 		}
84615eca306SMasami Hiramatsu 	}
84715eca306SMasami Hiramatsu 	return ntevs;
84815eca306SMasami Hiramatsu }
8494b4da7f7SMasami Hiramatsu 
8504b4da7f7SMasami Hiramatsu #define LINEBUF_SIZE 256
8514b4da7f7SMasami Hiramatsu #define NR_ADDITIONAL_LINES 2
8524b4da7f7SMasami Hiramatsu 
853fde52dbdSFranck Bui-Huu static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
8544b4da7f7SMasami Hiramatsu {
8555f03cba4SMasami Hiramatsu 	char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
856befe3414SFranck Bui-Huu 	const char *color = show_num ? "" : PERF_COLOR_BLUE;
857befe3414SFranck Bui-Huu 	const char *prefix = NULL;
8584b4da7f7SMasami Hiramatsu 
859befe3414SFranck Bui-Huu 	do {
8604b4da7f7SMasami Hiramatsu 		if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
8614b4da7f7SMasami Hiramatsu 			goto error;
862befe3414SFranck Bui-Huu 		if (skip)
863befe3414SFranck Bui-Huu 			continue;
864befe3414SFranck Bui-Huu 		if (!prefix) {
865befe3414SFranck Bui-Huu 			prefix = show_num ? "%7d  " : "         ";
866befe3414SFranck Bui-Huu 			color_fprintf(stdout, color, prefix, l);
8674b4da7f7SMasami Hiramatsu 		}
868befe3414SFranck Bui-Huu 		color_fprintf(stdout, color, "%s", buf);
8694b4da7f7SMasami Hiramatsu 
870befe3414SFranck Bui-Huu 	} while (strchr(buf, '\n') == NULL);
871146a1439SMasami Hiramatsu 
872fde52dbdSFranck Bui-Huu 	return 1;
8734b4da7f7SMasami Hiramatsu error:
874fde52dbdSFranck Bui-Huu 	if (ferror(fp)) {
8755f03cba4SMasami Hiramatsu 		pr_warning("File read error: %s\n",
876c8b5f2c9SArnaldo Carvalho de Melo 			   str_error_r(errno, sbuf, sizeof(sbuf)));
877146a1439SMasami Hiramatsu 		return -1;
8784b4da7f7SMasami Hiramatsu 	}
879fde52dbdSFranck Bui-Huu 	return 0;
880fde52dbdSFranck Bui-Huu }
881fde52dbdSFranck Bui-Huu 
882fde52dbdSFranck Bui-Huu static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
883fde52dbdSFranck Bui-Huu {
884fde52dbdSFranck Bui-Huu 	int rv = __show_one_line(fp, l, skip, show_num);
885fde52dbdSFranck Bui-Huu 	if (rv == 0) {
886fde52dbdSFranck Bui-Huu 		pr_warning("Source file is shorter than expected.\n");
887fde52dbdSFranck Bui-Huu 		rv = -1;
888fde52dbdSFranck Bui-Huu 	}
889fde52dbdSFranck Bui-Huu 	return rv;
890fde52dbdSFranck Bui-Huu }
891fde52dbdSFranck Bui-Huu 
892fde52dbdSFranck Bui-Huu #define show_one_line_with_num(f,l)	_show_one_line(f,l,false,true)
893fde52dbdSFranck Bui-Huu #define show_one_line(f,l)		_show_one_line(f,l,false,false)
894fde52dbdSFranck Bui-Huu #define skip_one_line(f,l)		_show_one_line(f,l,true,false)
895fde52dbdSFranck Bui-Huu #define show_one_line_or_eof(f,l)	__show_one_line(f,l,false,false)
8964b4da7f7SMasami Hiramatsu 
8974b4da7f7SMasami Hiramatsu /*
8984b4da7f7SMasami Hiramatsu  * Show line-range always requires debuginfo to find source file and
8994b4da7f7SMasami Hiramatsu  * line number.
9004b4da7f7SMasami Hiramatsu  */
901811dd2aeSMasami Hiramatsu static int __show_line_range(struct line_range *lr, const char *module,
902811dd2aeSMasami Hiramatsu 			     bool user)
9034b4da7f7SMasami Hiramatsu {
904d3b63d7aSMasami Hiramatsu 	int l = 1;
9055a62257aSMasami Hiramatsu 	struct int_node *ln;
906ff741783SMasami Hiramatsu 	struct debuginfo *dinfo;
9074b4da7f7SMasami Hiramatsu 	FILE *fp;
908ff741783SMasami Hiramatsu 	int ret;
9097cf0b79eSMasami Hiramatsu 	char *tmp;
9105f03cba4SMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE];
9114b4da7f7SMasami Hiramatsu 
9124b4da7f7SMasami Hiramatsu 	/* Search a line range */
91392561cb7SMasami Hiramatsu 	dinfo = open_debuginfo(module, false);
91492561cb7SMasami Hiramatsu 	if (!dinfo)
915ff741783SMasami Hiramatsu 		return -ENOENT;
916146a1439SMasami Hiramatsu 
917ff741783SMasami Hiramatsu 	ret = debuginfo__find_line_range(dinfo, lr);
918811dd2aeSMasami Hiramatsu 	if (!ret) {	/* Not found, retry with an alternative */
919811dd2aeSMasami Hiramatsu 		ret = get_alternative_line_range(dinfo, lr, module, user);
920811dd2aeSMasami Hiramatsu 		if (!ret)
921811dd2aeSMasami Hiramatsu 			ret = debuginfo__find_line_range(dinfo, lr);
922811dd2aeSMasami Hiramatsu 	}
923ff741783SMasami Hiramatsu 	debuginfo__delete(dinfo);
9245ee05b88SMasami Hiramatsu 	if (ret == 0 || ret == -ENOENT) {
925146a1439SMasami Hiramatsu 		pr_warning("Specified source line is not found.\n");
926146a1439SMasami Hiramatsu 		return -ENOENT;
927146a1439SMasami Hiramatsu 	} else if (ret < 0) {
9285ee05b88SMasami Hiramatsu 		pr_warning("Debuginfo analysis failed.\n");
929146a1439SMasami Hiramatsu 		return ret;
930146a1439SMasami Hiramatsu 	}
9314b4da7f7SMasami Hiramatsu 
9327cf0b79eSMasami Hiramatsu 	/* Convert source file path */
9337cf0b79eSMasami Hiramatsu 	tmp = lr->path;
9346a330a3cSMasami Hiramatsu 	ret = get_real_path(tmp, lr->comp_dir, &lr->path);
935a78604deSHe Kuang 
936a78604deSHe Kuang 	/* Free old path when new path is assigned */
937a78604deSHe Kuang 	if (tmp != lr->path)
938a78604deSHe Kuang 		free(tmp);
939a78604deSHe Kuang 
9407cf0b79eSMasami Hiramatsu 	if (ret < 0) {
9415ee05b88SMasami Hiramatsu 		pr_warning("Failed to find source file path.\n");
9427cf0b79eSMasami Hiramatsu 		return ret;
9437cf0b79eSMasami Hiramatsu 	}
9447cf0b79eSMasami Hiramatsu 
9454b4da7f7SMasami Hiramatsu 	setup_pager();
9464b4da7f7SMasami Hiramatsu 
9474b4da7f7SMasami Hiramatsu 	if (lr->function)
9488737ebdeSMasami Hiramatsu 		fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
9494b4da7f7SMasami Hiramatsu 			lr->start - lr->offset);
9504b4da7f7SMasami Hiramatsu 	else
95162c15fc4SFranck Bui-Huu 		fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
9524b4da7f7SMasami Hiramatsu 
9534b4da7f7SMasami Hiramatsu 	fp = fopen(lr->path, "r");
954146a1439SMasami Hiramatsu 	if (fp == NULL) {
955146a1439SMasami Hiramatsu 		pr_warning("Failed to open %s: %s\n", lr->path,
956c8b5f2c9SArnaldo Carvalho de Melo 			   str_error_r(errno, sbuf, sizeof(sbuf)));
957146a1439SMasami Hiramatsu 		return -errno;
958146a1439SMasami Hiramatsu 	}
9594b4da7f7SMasami Hiramatsu 	/* Skip to starting line number */
96044b81e92SFranck Bui-Huu 	while (l < lr->start) {
961fde52dbdSFranck Bui-Huu 		ret = skip_one_line(fp, l++);
962146a1439SMasami Hiramatsu 		if (ret < 0)
963146a1439SMasami Hiramatsu 			goto end;
96444b81e92SFranck Bui-Huu 	}
9654b4da7f7SMasami Hiramatsu 
96610daf4d0SArnaldo Carvalho de Melo 	intlist__for_each_entry(ln, lr->line_list) {
9675a62257aSMasami Hiramatsu 		for (; ln->i > l; l++) {
968fde52dbdSFranck Bui-Huu 			ret = show_one_line(fp, l - lr->offset);
96944b81e92SFranck Bui-Huu 			if (ret < 0)
97044b81e92SFranck Bui-Huu 				goto end;
97144b81e92SFranck Bui-Huu 		}
972fde52dbdSFranck Bui-Huu 		ret = show_one_line_with_num(fp, l++ - lr->offset);
973146a1439SMasami Hiramatsu 		if (ret < 0)
974146a1439SMasami Hiramatsu 			goto end;
9754b4da7f7SMasami Hiramatsu 	}
9764b4da7f7SMasami Hiramatsu 
9774b4da7f7SMasami Hiramatsu 	if (lr->end == INT_MAX)
9784b4da7f7SMasami Hiramatsu 		lr->end = l + NR_ADDITIONAL_LINES;
979fde52dbdSFranck Bui-Huu 	while (l <= lr->end) {
980fde52dbdSFranck Bui-Huu 		ret = show_one_line_or_eof(fp, l++ - lr->offset);
981fde52dbdSFranck Bui-Huu 		if (ret <= 0)
98244b81e92SFranck Bui-Huu 			break;
98344b81e92SFranck Bui-Huu 	}
984146a1439SMasami Hiramatsu end:
9854b4da7f7SMasami Hiramatsu 	fclose(fp);
986146a1439SMasami Hiramatsu 	return ret;
9874b4da7f7SMasami Hiramatsu }
9884b4da7f7SMasami Hiramatsu 
9892b394bc4SMasami Hiramatsu int show_line_range(struct line_range *lr, const char *module, bool user)
990ee45b6c2SMasami Hiramatsu {
991ee45b6c2SMasami Hiramatsu 	int ret;
992ee45b6c2SMasami Hiramatsu 
9939bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(user);
994ee45b6c2SMasami Hiramatsu 	if (ret < 0)
995ee45b6c2SMasami Hiramatsu 		return ret;
996811dd2aeSMasami Hiramatsu 	ret = __show_line_range(lr, module, user);
9979bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
998ee45b6c2SMasami Hiramatsu 
999ee45b6c2SMasami Hiramatsu 	return ret;
1000ee45b6c2SMasami Hiramatsu }
1001ee45b6c2SMasami Hiramatsu 
1002ff741783SMasami Hiramatsu static int show_available_vars_at(struct debuginfo *dinfo,
1003ff741783SMasami Hiramatsu 				  struct perf_probe_event *pev,
1004ddb2f58fSMasami Hiramatsu 				  struct strfilter *_filter)
1005cf6eb489SMasami Hiramatsu {
1006cf6eb489SMasami Hiramatsu 	char *buf;
1007bd09d7b5SMasami Hiramatsu 	int ret, i, nvars;
1008cf6eb489SMasami Hiramatsu 	struct str_node *node;
1009cf6eb489SMasami Hiramatsu 	struct variable_list *vls = NULL, *vl;
10109b118acaSMasami Hiramatsu 	struct perf_probe_point tmp;
1011bd09d7b5SMasami Hiramatsu 	const char *var;
1012cf6eb489SMasami Hiramatsu 
1013cf6eb489SMasami Hiramatsu 	buf = synthesize_perf_probe_point(&pev->point);
1014cf6eb489SMasami Hiramatsu 	if (!buf)
1015cf6eb489SMasami Hiramatsu 		return -EINVAL;
1016cf6eb489SMasami Hiramatsu 	pr_debug("Searching variables at %s\n", buf);
1017cf6eb489SMasami Hiramatsu 
1018ddb2f58fSMasami Hiramatsu 	ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
10199b118acaSMasami Hiramatsu 	if (!ret) {  /* Not found, retry with an alternative */
102044225521SMasami Hiramatsu 		ret = get_alternative_probe_event(dinfo, pev, &tmp);
10219b118acaSMasami Hiramatsu 		if (!ret) {
10229b118acaSMasami Hiramatsu 			ret = debuginfo__find_available_vars_at(dinfo, pev,
1023ddb2f58fSMasami Hiramatsu 								&vls);
10249b118acaSMasami Hiramatsu 			/* Release the old probe_point */
10259b118acaSMasami Hiramatsu 			clear_perf_probe_point(&tmp);
10269b118acaSMasami Hiramatsu 		}
10279b118acaSMasami Hiramatsu 	}
1028bd09d7b5SMasami Hiramatsu 	if (ret <= 0) {
102969e96eaaSMasami Hiramatsu 		if (ret == 0 || ret == -ENOENT) {
103069e96eaaSMasami Hiramatsu 			pr_err("Failed to find the address of %s\n", buf);
103169e96eaaSMasami Hiramatsu 			ret = -ENOENT;
103269e96eaaSMasami Hiramatsu 		} else
103369e96eaaSMasami Hiramatsu 			pr_warning("Debuginfo analysis failed.\n");
1034bd09d7b5SMasami Hiramatsu 		goto end;
1035bd09d7b5SMasami Hiramatsu 	}
103669e96eaaSMasami Hiramatsu 
1037bd09d7b5SMasami Hiramatsu 	/* Some variables are found */
1038cf6eb489SMasami Hiramatsu 	fprintf(stdout, "Available variables at %s\n", buf);
1039cf6eb489SMasami Hiramatsu 	for (i = 0; i < ret; i++) {
1040cf6eb489SMasami Hiramatsu 		vl = &vls[i];
1041cf6eb489SMasami Hiramatsu 		/*
1042cf6eb489SMasami Hiramatsu 		 * A probe point might be converted to
1043cf6eb489SMasami Hiramatsu 		 * several trace points.
1044cf6eb489SMasami Hiramatsu 		 */
1045cf6eb489SMasami Hiramatsu 		fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1046cf6eb489SMasami Hiramatsu 			vl->point.offset);
104774cf249dSArnaldo Carvalho de Melo 		zfree(&vl->point.symbol);
1048bd09d7b5SMasami Hiramatsu 		nvars = 0;
1049cf6eb489SMasami Hiramatsu 		if (vl->vars) {
1050602a1f4dSArnaldo Carvalho de Melo 			strlist__for_each_entry(node, vl->vars) {
1051bd09d7b5SMasami Hiramatsu 				var = strchr(node->s, '\t') + 1;
1052bd09d7b5SMasami Hiramatsu 				if (strfilter__compare(_filter, var)) {
1053cf6eb489SMasami Hiramatsu 					fprintf(stdout, "\t\t%s\n", node->s);
1054bd09d7b5SMasami Hiramatsu 					nvars++;
1055bd09d7b5SMasami Hiramatsu 				}
1056bd09d7b5SMasami Hiramatsu 			}
1057cf6eb489SMasami Hiramatsu 			strlist__delete(vl->vars);
1058bd09d7b5SMasami Hiramatsu 		}
1059bd09d7b5SMasami Hiramatsu 		if (nvars == 0)
1060bd09d7b5SMasami Hiramatsu 			fprintf(stdout, "\t\t(No matched variables)\n");
1061cf6eb489SMasami Hiramatsu 	}
1062cf6eb489SMasami Hiramatsu 	free(vls);
1063bd09d7b5SMasami Hiramatsu end:
1064cf6eb489SMasami Hiramatsu 	free(buf);
1065cf6eb489SMasami Hiramatsu 	return ret;
1066cf6eb489SMasami Hiramatsu }
1067cf6eb489SMasami Hiramatsu 
1068cf6eb489SMasami Hiramatsu /* Show available variables on given probe point */
1069cf6eb489SMasami Hiramatsu int show_available_vars(struct perf_probe_event *pevs, int npevs,
1070ddb2f58fSMasami Hiramatsu 			struct strfilter *_filter)
1071cf6eb489SMasami Hiramatsu {
1072ff741783SMasami Hiramatsu 	int i, ret = 0;
1073ff741783SMasami Hiramatsu 	struct debuginfo *dinfo;
1074cf6eb489SMasami Hiramatsu 
10759bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(pevs->uprobes);
1076cf6eb489SMasami Hiramatsu 	if (ret < 0)
1077cf6eb489SMasami Hiramatsu 		return ret;
1078cf6eb489SMasami Hiramatsu 
107944225521SMasami Hiramatsu 	dinfo = open_debuginfo(pevs->target, false);
1080ff741783SMasami Hiramatsu 	if (!dinfo) {
1081ee45b6c2SMasami Hiramatsu 		ret = -ENOENT;
1082ee45b6c2SMasami Hiramatsu 		goto out;
1083ff741783SMasami Hiramatsu 	}
1084ff741783SMasami Hiramatsu 
1085cc446446SMasami Hiramatsu 	setup_pager();
1086cc446446SMasami Hiramatsu 
1087ff741783SMasami Hiramatsu 	for (i = 0; i < npevs && ret >= 0; i++)
1088ddb2f58fSMasami Hiramatsu 		ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1089ff741783SMasami Hiramatsu 
1090ff741783SMasami Hiramatsu 	debuginfo__delete(dinfo);
1091ee45b6c2SMasami Hiramatsu out:
10929bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
1093cf6eb489SMasami Hiramatsu 	return ret;
1094cf6eb489SMasami Hiramatsu }
1095cf6eb489SMasami Hiramatsu 
109689fe808aSIngo Molnar #else	/* !HAVE_DWARF_SUPPORT */
10974b4da7f7SMasami Hiramatsu 
10987737af01SMasami Hiramatsu static void debuginfo_cache__exit(void)
10997737af01SMasami Hiramatsu {
11007737af01SMasami Hiramatsu }
11017737af01SMasami Hiramatsu 
11025a6f6314SMasami Hiramatsu static int
11035a6f6314SMasami Hiramatsu find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
11045a6f6314SMasami Hiramatsu 				 struct perf_probe_point *pp __maybe_unused,
11055a6f6314SMasami Hiramatsu 				 bool is_kprobe __maybe_unused)
11064b4da7f7SMasami Hiramatsu {
11075a6f6314SMasami Hiramatsu 	return -ENOSYS;
11084b4da7f7SMasami Hiramatsu }
11094b4da7f7SMasami Hiramatsu 
11100e60836bSSrikar Dronamraju static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1111ddb2f58fSMasami Hiramatsu 				struct probe_trace_event **tevs __maybe_unused)
11124b4da7f7SMasami Hiramatsu {
1113146a1439SMasami Hiramatsu 	if (perf_probe_event_need_dwarf(pev)) {
1114146a1439SMasami Hiramatsu 		pr_warning("Debuginfo-analysis is not supported.\n");
1115146a1439SMasami Hiramatsu 		return -ENOSYS;
1116146a1439SMasami Hiramatsu 	}
1117225466f1SSrikar Dronamraju 
11184b4da7f7SMasami Hiramatsu 	return 0;
11194b4da7f7SMasami Hiramatsu }
11204b4da7f7SMasami Hiramatsu 
11211d037ca1SIrina Tirdea int show_line_range(struct line_range *lr __maybe_unused,
11222b394bc4SMasami Hiramatsu 		    const char *module __maybe_unused,
11232b394bc4SMasami Hiramatsu 		    bool user __maybe_unused)
11244b4da7f7SMasami Hiramatsu {
1125146a1439SMasami Hiramatsu 	pr_warning("Debuginfo-analysis is not supported.\n");
1126146a1439SMasami Hiramatsu 	return -ENOSYS;
11274b4da7f7SMasami Hiramatsu }
11284b4da7f7SMasami Hiramatsu 
11291d037ca1SIrina Tirdea int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1130ddb2f58fSMasami Hiramatsu 			int npevs __maybe_unused,
1131ddb2f58fSMasami Hiramatsu 			struct strfilter *filter __maybe_unused)
1132cf6eb489SMasami Hiramatsu {
1133cf6eb489SMasami Hiramatsu 	pr_warning("Debuginfo-analysis is not supported.\n");
1134cf6eb489SMasami Hiramatsu 	return -ENOSYS;
1135cf6eb489SMasami Hiramatsu }
1136e0faa8d3SMasami Hiramatsu #endif
1137e0faa8d3SMasami Hiramatsu 
1138e53b00d3SMasami Hiramatsu void line_range__clear(struct line_range *lr)
1139e53b00d3SMasami Hiramatsu {
1140e53b00d3SMasami Hiramatsu 	free(lr->function);
1141e53b00d3SMasami Hiramatsu 	free(lr->file);
1142e53b00d3SMasami Hiramatsu 	free(lr->path);
1143e53b00d3SMasami Hiramatsu 	free(lr->comp_dir);
11445a62257aSMasami Hiramatsu 	intlist__delete(lr->line_list);
1145e53b00d3SMasami Hiramatsu 	memset(lr, 0, sizeof(*lr));
1146e53b00d3SMasami Hiramatsu }
1147e53b00d3SMasami Hiramatsu 
11485a62257aSMasami Hiramatsu int line_range__init(struct line_range *lr)
1149e53b00d3SMasami Hiramatsu {
1150e53b00d3SMasami Hiramatsu 	memset(lr, 0, sizeof(*lr));
11515a62257aSMasami Hiramatsu 	lr->line_list = intlist__new(NULL);
11525a62257aSMasami Hiramatsu 	if (!lr->line_list)
11535a62257aSMasami Hiramatsu 		return -ENOMEM;
11545a62257aSMasami Hiramatsu 	else
11555a62257aSMasami Hiramatsu 		return 0;
1156e53b00d3SMasami Hiramatsu }
1157e53b00d3SMasami Hiramatsu 
115821dd9ae5SFranck Bui-Huu static int parse_line_num(char **ptr, int *val, const char *what)
115921dd9ae5SFranck Bui-Huu {
116021dd9ae5SFranck Bui-Huu 	const char *start = *ptr;
116121dd9ae5SFranck Bui-Huu 
116221dd9ae5SFranck Bui-Huu 	errno = 0;
116321dd9ae5SFranck Bui-Huu 	*val = strtol(*ptr, ptr, 0);
116421dd9ae5SFranck Bui-Huu 	if (errno || *ptr == start) {
116521dd9ae5SFranck Bui-Huu 		semantic_error("'%s' is not a valid number.\n", what);
116621dd9ae5SFranck Bui-Huu 		return -EINVAL;
116721dd9ae5SFranck Bui-Huu 	}
116821dd9ae5SFranck Bui-Huu 	return 0;
116921dd9ae5SFranck Bui-Huu }
117021dd9ae5SFranck Bui-Huu 
1171573709fdSMasami Hiramatsu /* Check the name is good for event, group or function */
1172573709fdSMasami Hiramatsu static bool is_c_func_name(const char *name)
1173573709fdSMasami Hiramatsu {
1174573709fdSMasami Hiramatsu 	if (!isalpha(*name) && *name != '_')
1175573709fdSMasami Hiramatsu 		return false;
1176573709fdSMasami Hiramatsu 	while (*++name != '\0') {
1177573709fdSMasami Hiramatsu 		if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1178573709fdSMasami Hiramatsu 			return false;
1179573709fdSMasami Hiramatsu 	}
1180573709fdSMasami Hiramatsu 	return true;
1181573709fdSMasami Hiramatsu }
1182573709fdSMasami Hiramatsu 
11839d95b580SFranck Bui-Huu /*
11849d95b580SFranck Bui-Huu  * Stuff 'lr' according to the line range described by 'arg'.
11859d95b580SFranck Bui-Huu  * The line range syntax is described by:
11869d95b580SFranck Bui-Huu  *
11879d95b580SFranck Bui-Huu  *         SRC[:SLN[+NUM|-ELN]]
1188e116dfa1SMasami Hiramatsu  *         FNC[@SRC][:SLN[+NUM|-ELN]]
11899d95b580SFranck Bui-Huu  */
1190146a1439SMasami Hiramatsu int parse_line_range_desc(const char *arg, struct line_range *lr)
1191631c9defSMasami Hiramatsu {
1192e116dfa1SMasami Hiramatsu 	char *range, *file, *name = strdup(arg);
119321dd9ae5SFranck Bui-Huu 	int err;
11949d95b580SFranck Bui-Huu 
119521dd9ae5SFranck Bui-Huu 	if (!name)
119621dd9ae5SFranck Bui-Huu 		return -ENOMEM;
119721dd9ae5SFranck Bui-Huu 
119821dd9ae5SFranck Bui-Huu 	lr->start = 0;
119921dd9ae5SFranck Bui-Huu 	lr->end = INT_MAX;
120021dd9ae5SFranck Bui-Huu 
120121dd9ae5SFranck Bui-Huu 	range = strchr(name, ':');
120221dd9ae5SFranck Bui-Huu 	if (range) {
120321dd9ae5SFranck Bui-Huu 		*range++ = '\0';
120421dd9ae5SFranck Bui-Huu 
120521dd9ae5SFranck Bui-Huu 		err = parse_line_num(&range, &lr->start, "start line");
120621dd9ae5SFranck Bui-Huu 		if (err)
120721dd9ae5SFranck Bui-Huu 			goto err;
120821dd9ae5SFranck Bui-Huu 
120921dd9ae5SFranck Bui-Huu 		if (*range == '+' || *range == '-') {
121021dd9ae5SFranck Bui-Huu 			const char c = *range++;
121121dd9ae5SFranck Bui-Huu 
121221dd9ae5SFranck Bui-Huu 			err = parse_line_num(&range, &lr->end, "end line");
121321dd9ae5SFranck Bui-Huu 			if (err)
121421dd9ae5SFranck Bui-Huu 				goto err;
121521dd9ae5SFranck Bui-Huu 
121621dd9ae5SFranck Bui-Huu 			if (c == '+') {
121721dd9ae5SFranck Bui-Huu 				lr->end += lr->start;
121821dd9ae5SFranck Bui-Huu 				/*
1219dda4ab34SMasami Hiramatsu 				 * Adjust the number of lines here.
1220dda4ab34SMasami Hiramatsu 				 * If the number of lines == 1, the
1221dda4ab34SMasami Hiramatsu 				 * the end of line should be equal to
1222dda4ab34SMasami Hiramatsu 				 * the start of line.
1223dda4ab34SMasami Hiramatsu 				 */
122421dd9ae5SFranck Bui-Huu 				lr->end--;
122521dd9ae5SFranck Bui-Huu 			}
122621dd9ae5SFranck Bui-Huu 		}
122721dd9ae5SFranck Bui-Huu 
1228d3b63d7aSMasami Hiramatsu 		pr_debug("Line range is %d to %d\n", lr->start, lr->end);
122921dd9ae5SFranck Bui-Huu 
123021dd9ae5SFranck Bui-Huu 		err = -EINVAL;
1231d3b63d7aSMasami Hiramatsu 		if (lr->start > lr->end) {
1232631c9defSMasami Hiramatsu 			semantic_error("Start line must be smaller"
1233146a1439SMasami Hiramatsu 				       " than end line.\n");
123421dd9ae5SFranck Bui-Huu 			goto err;
1235146a1439SMasami Hiramatsu 		}
123621dd9ae5SFranck Bui-Huu 		if (*range != '\0') {
123721dd9ae5SFranck Bui-Huu 			semantic_error("Tailing with invalid str '%s'.\n", range);
123821dd9ae5SFranck Bui-Huu 			goto err;
1239146a1439SMasami Hiramatsu 		}
1240d3b63d7aSMasami Hiramatsu 	}
124102b95dadSMasami Hiramatsu 
1242e116dfa1SMasami Hiramatsu 	file = strchr(name, '@');
1243e116dfa1SMasami Hiramatsu 	if (file) {
1244e116dfa1SMasami Hiramatsu 		*file = '\0';
1245e116dfa1SMasami Hiramatsu 		lr->file = strdup(++file);
1246e116dfa1SMasami Hiramatsu 		if (lr->file == NULL) {
1247e116dfa1SMasami Hiramatsu 			err = -ENOMEM;
1248e116dfa1SMasami Hiramatsu 			goto err;
1249e116dfa1SMasami Hiramatsu 		}
1250e116dfa1SMasami Hiramatsu 		lr->function = name;
1251573709fdSMasami Hiramatsu 	} else if (strchr(name, '/') || strchr(name, '.'))
125221dd9ae5SFranck Bui-Huu 		lr->file = name;
1253573709fdSMasami Hiramatsu 	else if (is_c_func_name(name))/* We reuse it for checking funcname */
125421dd9ae5SFranck Bui-Huu 		lr->function = name;
1255573709fdSMasami Hiramatsu 	else {	/* Invalid name */
1256573709fdSMasami Hiramatsu 		semantic_error("'%s' is not a valid function name.\n", name);
1257573709fdSMasami Hiramatsu 		err = -EINVAL;
1258573709fdSMasami Hiramatsu 		goto err;
1259573709fdSMasami Hiramatsu 	}
1260146a1439SMasami Hiramatsu 
1261146a1439SMasami Hiramatsu 	return 0;
126221dd9ae5SFranck Bui-Huu err:
126321dd9ae5SFranck Bui-Huu 	free(name);
126421dd9ae5SFranck Bui-Huu 	return err;
1265631c9defSMasami Hiramatsu }
1266631c9defSMasami Hiramatsu 
126736a009feSMasami Hiramatsu static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
126836a009feSMasami Hiramatsu {
126936a009feSMasami Hiramatsu 	char *ptr;
127036a009feSMasami Hiramatsu 
127136a009feSMasami Hiramatsu 	ptr = strchr(*arg, ':');
127236a009feSMasami Hiramatsu 	if (ptr) {
127336a009feSMasami Hiramatsu 		*ptr = '\0';
127442bba263SMasami Hiramatsu 		if (!pev->sdt && !is_c_func_name(*arg))
127536a009feSMasami Hiramatsu 			goto ng_name;
127636a009feSMasami Hiramatsu 		pev->group = strdup(*arg);
127736a009feSMasami Hiramatsu 		if (!pev->group)
127836a009feSMasami Hiramatsu 			return -ENOMEM;
127936a009feSMasami Hiramatsu 		*arg = ptr + 1;
128036a009feSMasami Hiramatsu 	} else
128136a009feSMasami Hiramatsu 		pev->group = NULL;
128242bba263SMasami Hiramatsu 	if (!pev->sdt && !is_c_func_name(*arg)) {
128336a009feSMasami Hiramatsu ng_name:
128436a009feSMasami Hiramatsu 		semantic_error("%s is bad for event name -it must "
128536a009feSMasami Hiramatsu 			       "follow C symbol-naming rule.\n", *arg);
128636a009feSMasami Hiramatsu 		return -EINVAL;
128736a009feSMasami Hiramatsu 	}
128836a009feSMasami Hiramatsu 	pev->event = strdup(*arg);
128936a009feSMasami Hiramatsu 	if (pev->event == NULL)
129036a009feSMasami Hiramatsu 		return -ENOMEM;
129136a009feSMasami Hiramatsu 
129236a009feSMasami Hiramatsu 	return 0;
129336a009feSMasami Hiramatsu }
129436a009feSMasami Hiramatsu 
129550656eecSMasami Hiramatsu /* Parse probepoint definition. */
1296146a1439SMasami Hiramatsu static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
129750656eecSMasami Hiramatsu {
12984235b045SMasami Hiramatsu 	struct perf_probe_point *pp = &pev->point;
129950656eecSMasami Hiramatsu 	char *ptr, *tmp;
130050656eecSMasami Hiramatsu 	char c, nc = 0;
13013099c026SNaveen N. Rao 	bool file_spec = false;
130236a009feSMasami Hiramatsu 	int ret;
130336a009feSMasami Hiramatsu 
130450656eecSMasami Hiramatsu 	/*
130550656eecSMasami Hiramatsu 	 * <Syntax>
13068d993d96SMasami Hiramatsu 	 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
13078d993d96SMasami Hiramatsu 	 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
130836a009feSMasami Hiramatsu 	 * perf probe %[GRP:]SDT_EVENT
130950656eecSMasami Hiramatsu 	 */
1310e59d29e8SWang Nan 	if (!arg)
1311e59d29e8SWang Nan 		return -EINVAL;
131250656eecSMasami Hiramatsu 
13137e9fca51SMasami Hiramatsu 	/*
13147e9fca51SMasami Hiramatsu 	 * If the probe point starts with '%',
13157e9fca51SMasami Hiramatsu 	 * or starts with "sdt_" and has a ':' but no '=',
13167e9fca51SMasami Hiramatsu 	 * then it should be a SDT/cached probe point.
13177e9fca51SMasami Hiramatsu 	 */
13187e9fca51SMasami Hiramatsu 	if (arg[0] == '%' ||
13197e9fca51SMasami Hiramatsu 	    (!strncmp(arg, "sdt_", 4) &&
13207e9fca51SMasami Hiramatsu 	     !!strchr(arg, ':') && !strchr(arg, '='))) {
132136a009feSMasami Hiramatsu 		pev->sdt = true;
13227e9fca51SMasami Hiramatsu 		if (arg[0] == '%')
132336a009feSMasami Hiramatsu 			arg++;
132436a009feSMasami Hiramatsu 	}
132536a009feSMasami Hiramatsu 
13262a9c8c36SMasami Hiramatsu 	ptr = strpbrk(arg, ";=@+%");
132736a009feSMasami Hiramatsu 	if (pev->sdt) {
132836a009feSMasami Hiramatsu 		if (ptr) {
1329a598180aSMasami Hiramatsu 			if (*ptr != '@') {
1330a598180aSMasami Hiramatsu 				semantic_error("%s must be an SDT name.\n",
1331a598180aSMasami Hiramatsu 					       arg);
133236a009feSMasami Hiramatsu 				return -EINVAL;
133336a009feSMasami Hiramatsu 			}
1334a598180aSMasami Hiramatsu 			/* This must be a target file name or build id */
1335a598180aSMasami Hiramatsu 			tmp = build_id_cache__complement(ptr + 1);
1336a598180aSMasami Hiramatsu 			if (tmp) {
1337a598180aSMasami Hiramatsu 				pev->target = build_id_cache__origname(tmp);
1338a598180aSMasami Hiramatsu 				free(tmp);
1339a598180aSMasami Hiramatsu 			} else
1340a598180aSMasami Hiramatsu 				pev->target = strdup(ptr + 1);
1341a598180aSMasami Hiramatsu 			if (!pev->target)
1342a598180aSMasami Hiramatsu 				return -ENOMEM;
1343a598180aSMasami Hiramatsu 			*ptr = '\0';
1344a598180aSMasami Hiramatsu 		}
134536a009feSMasami Hiramatsu 		ret = parse_perf_probe_event_name(&arg, pev);
134636a009feSMasami Hiramatsu 		if (ret == 0) {
134736a009feSMasami Hiramatsu 			if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
134836a009feSMasami Hiramatsu 				ret = -errno;
134936a009feSMasami Hiramatsu 		}
135036a009feSMasami Hiramatsu 		return ret;
135136a009feSMasami Hiramatsu 	}
135236a009feSMasami Hiramatsu 
13532a9c8c36SMasami Hiramatsu 	if (ptr && *ptr == '=') {	/* Event name */
1354af663d75SMasami Hiramatsu 		*ptr = '\0';
1355af663d75SMasami Hiramatsu 		tmp = ptr + 1;
135636a009feSMasami Hiramatsu 		ret = parse_perf_probe_event_name(&arg, pev);
135736a009feSMasami Hiramatsu 		if (ret < 0)
135836a009feSMasami Hiramatsu 			return ret;
135936a009feSMasami Hiramatsu 
1360af663d75SMasami Hiramatsu 		arg = tmp;
1361af663d75SMasami Hiramatsu 	}
1362af663d75SMasami Hiramatsu 
13633099c026SNaveen N. Rao 	/*
13643099c026SNaveen N. Rao 	 * Check arg is function or file name and copy it.
13653099c026SNaveen N. Rao 	 *
13663099c026SNaveen N. Rao 	 * We consider arg to be a file spec if and only if it satisfies
13673099c026SNaveen N. Rao 	 * all of the below criteria::
13683099c026SNaveen N. Rao 	 * - it does not include any of "+@%",
13693099c026SNaveen N. Rao 	 * - it includes one of ":;", and
13703099c026SNaveen N. Rao 	 * - it has a period '.' in the name.
13713099c026SNaveen N. Rao 	 *
13723099c026SNaveen N. Rao 	 * Otherwise, we consider arg to be a function specification.
13733099c026SNaveen N. Rao 	 */
13743099c026SNaveen N. Rao 	if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
13753099c026SNaveen N. Rao 		/* This is a file spec if it includes a '.' before ; or : */
13763099c026SNaveen N. Rao 		if (memchr(arg, '.', ptr - arg))
13773099c026SNaveen N. Rao 			file_spec = true;
13783099c026SNaveen N. Rao 	}
13793099c026SNaveen N. Rao 
13802a9c8c36SMasami Hiramatsu 	ptr = strpbrk(arg, ";:+@%");
138150656eecSMasami Hiramatsu 	if (ptr) {
138250656eecSMasami Hiramatsu 		nc = *ptr;
138350656eecSMasami Hiramatsu 		*ptr++ = '\0';
138450656eecSMasami Hiramatsu 	}
138550656eecSMasami Hiramatsu 
13866c6e024fSWang Nan 	if (arg[0] == '\0')
13876c6e024fSWang Nan 		tmp = NULL;
13886c6e024fSWang Nan 	else {
138902b95dadSMasami Hiramatsu 		tmp = strdup(arg);
139002b95dadSMasami Hiramatsu 		if (tmp == NULL)
139102b95dadSMasami Hiramatsu 			return -ENOMEM;
13926c6e024fSWang Nan 	}
139302b95dadSMasami Hiramatsu 
13943099c026SNaveen N. Rao 	if (file_spec)
139502b95dadSMasami Hiramatsu 		pp->file = tmp;
1396da15bd9dSWang Nan 	else {
139702b95dadSMasami Hiramatsu 		pp->function = tmp;
139850656eecSMasami Hiramatsu 
1399da15bd9dSWang Nan 		/*
1400da15bd9dSWang Nan 		 * Keep pp->function even if this is absolute address,
1401da15bd9dSWang Nan 		 * so it can mark whether abs_address is valid.
1402da15bd9dSWang Nan 		 * Which make 'perf probe lib.bin 0x0' possible.
1403da15bd9dSWang Nan 		 *
1404da15bd9dSWang Nan 		 * Note that checking length of tmp is not needed
1405da15bd9dSWang Nan 		 * because when we access tmp[1] we know tmp[0] is '0',
1406da15bd9dSWang Nan 		 * so tmp[1] should always valid (but could be '\0').
1407da15bd9dSWang Nan 		 */
1408da15bd9dSWang Nan 		if (tmp && !strncmp(tmp, "0x", 2)) {
1409da15bd9dSWang Nan 			pp->abs_address = strtoul(pp->function, &tmp, 0);
1410da15bd9dSWang Nan 			if (*tmp != '\0') {
1411da15bd9dSWang Nan 				semantic_error("Invalid absolute address.\n");
1412da15bd9dSWang Nan 				return -EINVAL;
1413da15bd9dSWang Nan 			}
1414da15bd9dSWang Nan 		}
1415da15bd9dSWang Nan 	}
1416da15bd9dSWang Nan 
141750656eecSMasami Hiramatsu 	/* Parse other options */
141850656eecSMasami Hiramatsu 	while (ptr) {
141950656eecSMasami Hiramatsu 		arg = ptr;
142050656eecSMasami Hiramatsu 		c = nc;
14212a9c8c36SMasami Hiramatsu 		if (c == ';') {	/* Lazy pattern must be the last part */
142202b95dadSMasami Hiramatsu 			pp->lazy_line = strdup(arg);
142302b95dadSMasami Hiramatsu 			if (pp->lazy_line == NULL)
142402b95dadSMasami Hiramatsu 				return -ENOMEM;
14252a9c8c36SMasami Hiramatsu 			break;
14262a9c8c36SMasami Hiramatsu 		}
14272a9c8c36SMasami Hiramatsu 		ptr = strpbrk(arg, ";:+@%");
142850656eecSMasami Hiramatsu 		if (ptr) {
142950656eecSMasami Hiramatsu 			nc = *ptr;
143050656eecSMasami Hiramatsu 			*ptr++ = '\0';
143150656eecSMasami Hiramatsu 		}
143250656eecSMasami Hiramatsu 		switch (c) {
143350656eecSMasami Hiramatsu 		case ':':	/* Line number */
143450656eecSMasami Hiramatsu 			pp->line = strtoul(arg, &tmp, 0);
1435146a1439SMasami Hiramatsu 			if (*tmp != '\0') {
14362a9c8c36SMasami Hiramatsu 				semantic_error("There is non-digit char"
1437146a1439SMasami Hiramatsu 					       " in line number.\n");
1438146a1439SMasami Hiramatsu 				return -EINVAL;
1439146a1439SMasami Hiramatsu 			}
144050656eecSMasami Hiramatsu 			break;
144150656eecSMasami Hiramatsu 		case '+':	/* Byte offset from a symbol */
144250656eecSMasami Hiramatsu 			pp->offset = strtoul(arg, &tmp, 0);
1443146a1439SMasami Hiramatsu 			if (*tmp != '\0') {
14442a9c8c36SMasami Hiramatsu 				semantic_error("There is non-digit character"
1445146a1439SMasami Hiramatsu 						" in offset.\n");
1446146a1439SMasami Hiramatsu 				return -EINVAL;
1447146a1439SMasami Hiramatsu 			}
144850656eecSMasami Hiramatsu 			break;
144950656eecSMasami Hiramatsu 		case '@':	/* File name */
1450146a1439SMasami Hiramatsu 			if (pp->file) {
1451146a1439SMasami Hiramatsu 				semantic_error("SRC@SRC is not allowed.\n");
1452146a1439SMasami Hiramatsu 				return -EINVAL;
1453146a1439SMasami Hiramatsu 			}
145402b95dadSMasami Hiramatsu 			pp->file = strdup(arg);
145502b95dadSMasami Hiramatsu 			if (pp->file == NULL)
145602b95dadSMasami Hiramatsu 				return -ENOMEM;
145750656eecSMasami Hiramatsu 			break;
145850656eecSMasami Hiramatsu 		case '%':	/* Probe places */
145950656eecSMasami Hiramatsu 			if (strcmp(arg, "return") == 0) {
146050656eecSMasami Hiramatsu 				pp->retprobe = 1;
1461146a1439SMasami Hiramatsu 			} else {	/* Others not supported yet */
1462146a1439SMasami Hiramatsu 				semantic_error("%%%s is not supported.\n", arg);
1463146a1439SMasami Hiramatsu 				return -ENOTSUP;
1464146a1439SMasami Hiramatsu 			}
146550656eecSMasami Hiramatsu 			break;
1466146a1439SMasami Hiramatsu 		default:	/* Buggy case */
1467146a1439SMasami Hiramatsu 			pr_err("This program has a bug at %s:%d.\n",
1468146a1439SMasami Hiramatsu 				__FILE__, __LINE__);
1469146a1439SMasami Hiramatsu 			return -ENOTSUP;
147050656eecSMasami Hiramatsu 			break;
147150656eecSMasami Hiramatsu 		}
147250656eecSMasami Hiramatsu 	}
147350656eecSMasami Hiramatsu 
147450656eecSMasami Hiramatsu 	/* Exclusion check */
1475146a1439SMasami Hiramatsu 	if (pp->lazy_line && pp->line) {
14760e43e5d2SMasami Hiramatsu 		semantic_error("Lazy pattern can't be used with"
14770e43e5d2SMasami Hiramatsu 			       " line number.\n");
1478146a1439SMasami Hiramatsu 		return -EINVAL;
1479146a1439SMasami Hiramatsu 	}
14802a9c8c36SMasami Hiramatsu 
1481146a1439SMasami Hiramatsu 	if (pp->lazy_line && pp->offset) {
14820e43e5d2SMasami Hiramatsu 		semantic_error("Lazy pattern can't be used with offset.\n");
1483146a1439SMasami Hiramatsu 		return -EINVAL;
1484146a1439SMasami Hiramatsu 	}
14852a9c8c36SMasami Hiramatsu 
1486146a1439SMasami Hiramatsu 	if (pp->line && pp->offset) {
14870e43e5d2SMasami Hiramatsu 		semantic_error("Offset can't be used with line number.\n");
1488146a1439SMasami Hiramatsu 		return -EINVAL;
1489146a1439SMasami Hiramatsu 	}
149050656eecSMasami Hiramatsu 
1491146a1439SMasami Hiramatsu 	if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
14922a9c8c36SMasami Hiramatsu 		semantic_error("File always requires line number or "
14930e43e5d2SMasami Hiramatsu 			       "lazy pattern.\n");
1494146a1439SMasami Hiramatsu 		return -EINVAL;
1495146a1439SMasami Hiramatsu 	}
149650656eecSMasami Hiramatsu 
1497146a1439SMasami Hiramatsu 	if (pp->offset && !pp->function) {
14980e43e5d2SMasami Hiramatsu 		semantic_error("Offset requires an entry function.\n");
1499146a1439SMasami Hiramatsu 		return -EINVAL;
1500146a1439SMasami Hiramatsu 	}
150150656eecSMasami Hiramatsu 
1502146a1439SMasami Hiramatsu 	if (pp->retprobe && !pp->function) {
15030e43e5d2SMasami Hiramatsu 		semantic_error("Return probe requires an entry function.\n");
1504146a1439SMasami Hiramatsu 		return -EINVAL;
1505146a1439SMasami Hiramatsu 	}
150650656eecSMasami Hiramatsu 
1507146a1439SMasami Hiramatsu 	if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
15082a9c8c36SMasami Hiramatsu 		semantic_error("Offset/Line/Lazy pattern can't be used with "
15090e43e5d2SMasami Hiramatsu 			       "return probe.\n");
1510146a1439SMasami Hiramatsu 		return -EINVAL;
1511146a1439SMasami Hiramatsu 	}
151250656eecSMasami Hiramatsu 
15134235b045SMasami Hiramatsu 	pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
15142a9c8c36SMasami Hiramatsu 		 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
15152a9c8c36SMasami Hiramatsu 		 pp->lazy_line);
1516146a1439SMasami Hiramatsu 	return 0;
151750656eecSMasami Hiramatsu }
151850656eecSMasami Hiramatsu 
15197df2f329SMasami Hiramatsu /* Parse perf-probe event argument */
1520146a1439SMasami Hiramatsu static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
15217df2f329SMasami Hiramatsu {
1522b2a3c12bSMasami Hiramatsu 	char *tmp, *goodname;
15237df2f329SMasami Hiramatsu 	struct perf_probe_arg_field **fieldp;
15247df2f329SMasami Hiramatsu 
15257df2f329SMasami Hiramatsu 	pr_debug("parsing arg: %s into ", str);
15267df2f329SMasami Hiramatsu 
152748481938SMasami Hiramatsu 	tmp = strchr(str, '=');
152848481938SMasami Hiramatsu 	if (tmp) {
152902b95dadSMasami Hiramatsu 		arg->name = strndup(str, tmp - str);
153002b95dadSMasami Hiramatsu 		if (arg->name == NULL)
153102b95dadSMasami Hiramatsu 			return -ENOMEM;
153211a1ca35SMasami Hiramatsu 		pr_debug("name:%s ", arg->name);
153348481938SMasami Hiramatsu 		str = tmp + 1;
153448481938SMasami Hiramatsu 	}
153548481938SMasami Hiramatsu 
153611a1ca35SMasami Hiramatsu 	tmp = strchr(str, ':');
153711a1ca35SMasami Hiramatsu 	if (tmp) {	/* Type setting */
153811a1ca35SMasami Hiramatsu 		*tmp = '\0';
153902b95dadSMasami Hiramatsu 		arg->type = strdup(tmp + 1);
154002b95dadSMasami Hiramatsu 		if (arg->type == NULL)
154102b95dadSMasami Hiramatsu 			return -ENOMEM;
154211a1ca35SMasami Hiramatsu 		pr_debug("type:%s ", arg->type);
154311a1ca35SMasami Hiramatsu 	}
154411a1ca35SMasami Hiramatsu 
1545b2a3c12bSMasami Hiramatsu 	tmp = strpbrk(str, "-.[");
15467df2f329SMasami Hiramatsu 	if (!is_c_varname(str) || !tmp) {
15477df2f329SMasami Hiramatsu 		/* A variable, register, symbol or special value */
154802b95dadSMasami Hiramatsu 		arg->var = strdup(str);
154902b95dadSMasami Hiramatsu 		if (arg->var == NULL)
155002b95dadSMasami Hiramatsu 			return -ENOMEM;
155148481938SMasami Hiramatsu 		pr_debug("%s\n", arg->var);
1552146a1439SMasami Hiramatsu 		return 0;
15537df2f329SMasami Hiramatsu 	}
15547df2f329SMasami Hiramatsu 
1555b2a3c12bSMasami Hiramatsu 	/* Structure fields or array element */
155602b95dadSMasami Hiramatsu 	arg->var = strndup(str, tmp - str);
155702b95dadSMasami Hiramatsu 	if (arg->var == NULL)
155802b95dadSMasami Hiramatsu 		return -ENOMEM;
1559b2a3c12bSMasami Hiramatsu 	goodname = arg->var;
156048481938SMasami Hiramatsu 	pr_debug("%s, ", arg->var);
15617df2f329SMasami Hiramatsu 	fieldp = &arg->field;
15627df2f329SMasami Hiramatsu 
15637df2f329SMasami Hiramatsu 	do {
1564e334016fSMasami Hiramatsu 		*fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1565e334016fSMasami Hiramatsu 		if (*fieldp == NULL)
1566e334016fSMasami Hiramatsu 			return -ENOMEM;
1567b2a3c12bSMasami Hiramatsu 		if (*tmp == '[') {	/* Array */
1568b2a3c12bSMasami Hiramatsu 			str = tmp;
1569b2a3c12bSMasami Hiramatsu 			(*fieldp)->index = strtol(str + 1, &tmp, 0);
1570b2a3c12bSMasami Hiramatsu 			(*fieldp)->ref = true;
1571b2a3c12bSMasami Hiramatsu 			if (*tmp != ']' || tmp == str + 1) {
1572b2a3c12bSMasami Hiramatsu 				semantic_error("Array index must be a"
1573b2a3c12bSMasami Hiramatsu 						" number.\n");
1574b2a3c12bSMasami Hiramatsu 				return -EINVAL;
1575b2a3c12bSMasami Hiramatsu 			}
1576b2a3c12bSMasami Hiramatsu 			tmp++;
1577b2a3c12bSMasami Hiramatsu 			if (*tmp == '\0')
1578b2a3c12bSMasami Hiramatsu 				tmp = NULL;
1579b2a3c12bSMasami Hiramatsu 		} else {		/* Structure */
15807df2f329SMasami Hiramatsu 			if (*tmp == '.') {
15817df2f329SMasami Hiramatsu 				str = tmp + 1;
15827df2f329SMasami Hiramatsu 				(*fieldp)->ref = false;
15837df2f329SMasami Hiramatsu 			} else if (tmp[1] == '>') {
15847df2f329SMasami Hiramatsu 				str = tmp + 2;
15857df2f329SMasami Hiramatsu 				(*fieldp)->ref = true;
1586146a1439SMasami Hiramatsu 			} else {
1587b2a3c12bSMasami Hiramatsu 				semantic_error("Argument parse error: %s\n",
1588b2a3c12bSMasami Hiramatsu 					       str);
1589146a1439SMasami Hiramatsu 				return -EINVAL;
1590146a1439SMasami Hiramatsu 			}
1591b2a3c12bSMasami Hiramatsu 			tmp = strpbrk(str, "-.[");
1592b2a3c12bSMasami Hiramatsu 		}
15937df2f329SMasami Hiramatsu 		if (tmp) {
159402b95dadSMasami Hiramatsu 			(*fieldp)->name = strndup(str, tmp - str);
159502b95dadSMasami Hiramatsu 			if ((*fieldp)->name == NULL)
159602b95dadSMasami Hiramatsu 				return -ENOMEM;
1597b2a3c12bSMasami Hiramatsu 			if (*str != '[')
1598b2a3c12bSMasami Hiramatsu 				goodname = (*fieldp)->name;
15997df2f329SMasami Hiramatsu 			pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
16007df2f329SMasami Hiramatsu 			fieldp = &(*fieldp)->next;
16017df2f329SMasami Hiramatsu 		}
16027df2f329SMasami Hiramatsu 	} while (tmp);
160302b95dadSMasami Hiramatsu 	(*fieldp)->name = strdup(str);
160402b95dadSMasami Hiramatsu 	if ((*fieldp)->name == NULL)
160502b95dadSMasami Hiramatsu 		return -ENOMEM;
1606b2a3c12bSMasami Hiramatsu 	if (*str != '[')
1607b2a3c12bSMasami Hiramatsu 		goodname = (*fieldp)->name;
16087df2f329SMasami Hiramatsu 	pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1609df0faf4bSMasami Hiramatsu 
1610b2a3c12bSMasami Hiramatsu 	/* If no name is specified, set the last field name (not array index)*/
161102b95dadSMasami Hiramatsu 	if (!arg->name) {
1612b2a3c12bSMasami Hiramatsu 		arg->name = strdup(goodname);
161302b95dadSMasami Hiramatsu 		if (arg->name == NULL)
161402b95dadSMasami Hiramatsu 			return -ENOMEM;
161502b95dadSMasami Hiramatsu 	}
1616146a1439SMasami Hiramatsu 	return 0;
16177df2f329SMasami Hiramatsu }
16187df2f329SMasami Hiramatsu 
16194235b045SMasami Hiramatsu /* Parse perf-probe event command */
1620146a1439SMasami Hiramatsu int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
162150656eecSMasami Hiramatsu {
1622e1c01d61SMasami Hiramatsu 	char **argv;
1623146a1439SMasami Hiramatsu 	int argc, i, ret = 0;
1624fac13fd5SMasami Hiramatsu 
16254235b045SMasami Hiramatsu 	argv = argv_split(cmd, &argc);
1626146a1439SMasami Hiramatsu 	if (!argv) {
1627146a1439SMasami Hiramatsu 		pr_debug("Failed to split arguments.\n");
1628146a1439SMasami Hiramatsu 		return -ENOMEM;
1629146a1439SMasami Hiramatsu 	}
1630146a1439SMasami Hiramatsu 	if (argc - 1 > MAX_PROBE_ARGS) {
1631146a1439SMasami Hiramatsu 		semantic_error("Too many probe arguments (%d).\n", argc - 1);
1632146a1439SMasami Hiramatsu 		ret = -ERANGE;
1633146a1439SMasami Hiramatsu 		goto out;
1634146a1439SMasami Hiramatsu 	}
163550656eecSMasami Hiramatsu 	/* Parse probe point */
1636146a1439SMasami Hiramatsu 	ret = parse_perf_probe_point(argv[0], pev);
1637146a1439SMasami Hiramatsu 	if (ret < 0)
1638146a1439SMasami Hiramatsu 		goto out;
163950656eecSMasami Hiramatsu 
1640e1c01d61SMasami Hiramatsu 	/* Copy arguments and ensure return probe has no C argument */
16414235b045SMasami Hiramatsu 	pev->nargs = argc - 1;
1642e334016fSMasami Hiramatsu 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1643e334016fSMasami Hiramatsu 	if (pev->args == NULL) {
1644e334016fSMasami Hiramatsu 		ret = -ENOMEM;
1645e334016fSMasami Hiramatsu 		goto out;
1646e334016fSMasami Hiramatsu 	}
1647146a1439SMasami Hiramatsu 	for (i = 0; i < pev->nargs && ret >= 0; i++) {
1648146a1439SMasami Hiramatsu 		ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1649146a1439SMasami Hiramatsu 		if (ret >= 0 &&
1650146a1439SMasami Hiramatsu 		    is_c_varname(pev->args[i].var) && pev->point.retprobe) {
16514235b045SMasami Hiramatsu 			semantic_error("You can't specify local variable for"
1652146a1439SMasami Hiramatsu 				       " kretprobe.\n");
1653146a1439SMasami Hiramatsu 			ret = -EINVAL;
1654e1c01d61SMasami Hiramatsu 		}
1655146a1439SMasami Hiramatsu 	}
1656146a1439SMasami Hiramatsu out:
1657e1c01d61SMasami Hiramatsu 	argv_free(argv);
1658146a1439SMasami Hiramatsu 
1659146a1439SMasami Hiramatsu 	return ret;
166050656eecSMasami Hiramatsu }
166150656eecSMasami Hiramatsu 
1662b3f33f93SRavi Bangoria /* Returns true if *any* ARG is either C variable, $params or $vars. */
1663b3f33f93SRavi Bangoria bool perf_probe_with_var(struct perf_probe_event *pev)
16644de189feSMasami Hiramatsu {
1665b3f33f93SRavi Bangoria 	int i = 0;
16664235b045SMasami Hiramatsu 
16674235b045SMasami Hiramatsu 	for (i = 0; i < pev->nargs; i++)
1668f6eb0518SMasami Hiramatsu 		if (is_c_varname(pev->args[i].var)              ||
1669b3f33f93SRavi Bangoria 		    !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1670b3f33f93SRavi Bangoria 		    !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1671b3f33f93SRavi Bangoria 			return true;
1672b3f33f93SRavi Bangoria 	return false;
1673b3f33f93SRavi Bangoria }
1674b3f33f93SRavi Bangoria 
1675b3f33f93SRavi Bangoria /* Return true if this perf_probe_event requires debuginfo */
1676b3f33f93SRavi Bangoria bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1677b3f33f93SRavi Bangoria {
1678b3f33f93SRavi Bangoria 	if (pev->point.file || pev->point.line || pev->point.lazy_line)
1679b3f33f93SRavi Bangoria 		return true;
1680b3f33f93SRavi Bangoria 
1681b3f33f93SRavi Bangoria 	if (perf_probe_with_var(pev))
16824235b045SMasami Hiramatsu 		return true;
16834235b045SMasami Hiramatsu 
16844235b045SMasami Hiramatsu 	return false;
16854235b045SMasami Hiramatsu }
16864235b045SMasami Hiramatsu 
16870e60836bSSrikar Dronamraju /* Parse probe_events event into struct probe_point */
168892f6c72eSMasami Hiramatsu int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
16894235b045SMasami Hiramatsu {
16900e60836bSSrikar Dronamraju 	struct probe_trace_point *tp = &tev->point;
16914de189feSMasami Hiramatsu 	char pr;
16924de189feSMasami Hiramatsu 	char *p;
1693bcbd0040SIrina Tirdea 	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
16944de189feSMasami Hiramatsu 	int ret, i, argc;
16954de189feSMasami Hiramatsu 	char **argv;
16964de189feSMasami Hiramatsu 
16970e60836bSSrikar Dronamraju 	pr_debug("Parsing probe_events: %s\n", cmd);
16984235b045SMasami Hiramatsu 	argv = argv_split(cmd, &argc);
1699146a1439SMasami Hiramatsu 	if (!argv) {
1700146a1439SMasami Hiramatsu 		pr_debug("Failed to split arguments.\n");
1701146a1439SMasami Hiramatsu 		return -ENOMEM;
1702146a1439SMasami Hiramatsu 	}
1703146a1439SMasami Hiramatsu 	if (argc < 2) {
1704146a1439SMasami Hiramatsu 		semantic_error("Too few probe arguments.\n");
1705146a1439SMasami Hiramatsu 		ret = -ERANGE;
1706146a1439SMasami Hiramatsu 		goto out;
1707146a1439SMasami Hiramatsu 	}
17084de189feSMasami Hiramatsu 
17094de189feSMasami Hiramatsu 	/* Scan event and group name. */
1710bcbd0040SIrina Tirdea 	argv0_str = strdup(argv[0]);
1711bcbd0040SIrina Tirdea 	if (argv0_str == NULL) {
1712bcbd0040SIrina Tirdea 		ret = -ENOMEM;
1713bcbd0040SIrina Tirdea 		goto out;
1714bcbd0040SIrina Tirdea 	}
1715bcbd0040SIrina Tirdea 	fmt1_str = strtok_r(argv0_str, ":", &fmt);
1716bcbd0040SIrina Tirdea 	fmt2_str = strtok_r(NULL, "/", &fmt);
1717bcbd0040SIrina Tirdea 	fmt3_str = strtok_r(NULL, " \t", &fmt);
1718bcbd0040SIrina Tirdea 	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1719bcbd0040SIrina Tirdea 	    || fmt3_str == NULL) {
1720146a1439SMasami Hiramatsu 		semantic_error("Failed to parse event name: %s\n", argv[0]);
1721146a1439SMasami Hiramatsu 		ret = -EINVAL;
1722146a1439SMasami Hiramatsu 		goto out;
1723146a1439SMasami Hiramatsu 	}
1724bcbd0040SIrina Tirdea 	pr = fmt1_str[0];
1725bcbd0040SIrina Tirdea 	tev->group = strdup(fmt2_str);
1726bcbd0040SIrina Tirdea 	tev->event = strdup(fmt3_str);
1727bcbd0040SIrina Tirdea 	if (tev->group == NULL || tev->event == NULL) {
1728bcbd0040SIrina Tirdea 		ret = -ENOMEM;
1729bcbd0040SIrina Tirdea 		goto out;
1730bcbd0040SIrina Tirdea 	}
17314235b045SMasami Hiramatsu 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
17324de189feSMasami Hiramatsu 
17334235b045SMasami Hiramatsu 	tp->retprobe = (pr == 'r');
17344de189feSMasami Hiramatsu 
1735190b57fcSMasami Hiramatsu 	/* Scan module name(if there), function name and offset */
1736190b57fcSMasami Hiramatsu 	p = strchr(argv[1], ':');
1737190b57fcSMasami Hiramatsu 	if (p) {
1738190b57fcSMasami Hiramatsu 		tp->module = strndup(argv[1], p - argv[1]);
1739844faa4bSMasami Hiramatsu 		if (!tp->module) {
1740844faa4bSMasami Hiramatsu 			ret = -ENOMEM;
1741844faa4bSMasami Hiramatsu 			goto out;
1742844faa4bSMasami Hiramatsu 		}
174342bba263SMasami Hiramatsu 		tev->uprobes = (tp->module[0] == '/');
1744190b57fcSMasami Hiramatsu 		p++;
1745190b57fcSMasami Hiramatsu 	} else
1746190b57fcSMasami Hiramatsu 		p = argv[1];
1747bcbd0040SIrina Tirdea 	fmt1_str = strtok_r(p, "+", &fmt);
1748be07afe9SWang Nan 	/* only the address started with 0x */
1749be07afe9SWang Nan 	if (fmt1_str[0] == '0')	{
1750be07afe9SWang Nan 		/*
1751be07afe9SWang Nan 		 * Fix a special case:
1752be07afe9SWang Nan 		 * if address == 0, kernel reports something like:
1753be07afe9SWang Nan 		 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x          (null) arg1=%ax
1754be07afe9SWang Nan 		 * Newer kernel may fix that, but we want to
1755be07afe9SWang Nan 		 * support old kernel also.
1756be07afe9SWang Nan 		 */
1757be07afe9SWang Nan 		if (strcmp(fmt1_str, "0x") == 0) {
1758be07afe9SWang Nan 			if (!argv[2] || strcmp(argv[2], "(null)")) {
1759be07afe9SWang Nan 				ret = -EINVAL;
1760be07afe9SWang Nan 				goto out;
1761be07afe9SWang Nan 			}
1762be07afe9SWang Nan 			tp->address = 0;
1763be07afe9SWang Nan 
1764be07afe9SWang Nan 			free(argv[2]);
1765be07afe9SWang Nan 			for (i = 2; argv[i + 1] != NULL; i++)
1766be07afe9SWang Nan 				argv[i] = argv[i + 1];
1767be07afe9SWang Nan 
1768be07afe9SWang Nan 			argv[i] = NULL;
1769be07afe9SWang Nan 			argc -= 1;
1770be07afe9SWang Nan 		} else
17715a6f6314SMasami Hiramatsu 			tp->address = strtoul(fmt1_str, NULL, 0);
1772be07afe9SWang Nan 	} else {
17735a6f6314SMasami Hiramatsu 		/* Only the symbol-based probe has offset */
1774bcbd0040SIrina Tirdea 		tp->symbol = strdup(fmt1_str);
1775bcbd0040SIrina Tirdea 		if (tp->symbol == NULL) {
1776bcbd0040SIrina Tirdea 			ret = -ENOMEM;
1777bcbd0040SIrina Tirdea 			goto out;
1778bcbd0040SIrina Tirdea 		}
1779bcbd0040SIrina Tirdea 		fmt2_str = strtok_r(NULL, "", &fmt);
1780bcbd0040SIrina Tirdea 		if (fmt2_str == NULL)
17814235b045SMasami Hiramatsu 			tp->offset = 0;
1782bcbd0040SIrina Tirdea 		else
1783bcbd0040SIrina Tirdea 			tp->offset = strtoul(fmt2_str, NULL, 10);
17845a6f6314SMasami Hiramatsu 	}
17854de189feSMasami Hiramatsu 
17864235b045SMasami Hiramatsu 	tev->nargs = argc - 2;
17870e60836bSSrikar Dronamraju 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1788e334016fSMasami Hiramatsu 	if (tev->args == NULL) {
1789e334016fSMasami Hiramatsu 		ret = -ENOMEM;
1790e334016fSMasami Hiramatsu 		goto out;
1791e334016fSMasami Hiramatsu 	}
17924235b045SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++) {
17934de189feSMasami Hiramatsu 		p = strchr(argv[i + 2], '=');
17944de189feSMasami Hiramatsu 		if (p)	/* We don't need which register is assigned. */
17954235b045SMasami Hiramatsu 			*p++ = '\0';
17964235b045SMasami Hiramatsu 		else
17974235b045SMasami Hiramatsu 			p = argv[i + 2];
179802b95dadSMasami Hiramatsu 		tev->args[i].name = strdup(argv[i + 2]);
17994235b045SMasami Hiramatsu 		/* TODO: parse regs and offset */
180002b95dadSMasami Hiramatsu 		tev->args[i].value = strdup(p);
180102b95dadSMasami Hiramatsu 		if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
180202b95dadSMasami Hiramatsu 			ret = -ENOMEM;
180302b95dadSMasami Hiramatsu 			goto out;
180402b95dadSMasami Hiramatsu 		}
18054de189feSMasami Hiramatsu 	}
1806146a1439SMasami Hiramatsu 	ret = 0;
1807146a1439SMasami Hiramatsu out:
1808bcbd0040SIrina Tirdea 	free(argv0_str);
18094de189feSMasami Hiramatsu 	argv_free(argv);
1810146a1439SMasami Hiramatsu 	return ret;
18114de189feSMasami Hiramatsu }
18124de189feSMasami Hiramatsu 
18137df2f329SMasami Hiramatsu /* Compose only probe arg */
1814909b0360SMasami Hiramatsu char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
18157df2f329SMasami Hiramatsu {
18167df2f329SMasami Hiramatsu 	struct perf_probe_arg_field *field = pa->field;
1817909b0360SMasami Hiramatsu 	struct strbuf buf;
1818bf4d5f25SMasami Hiramatsu 	char *ret = NULL;
1819bf4d5f25SMasami Hiramatsu 	int err;
18207df2f329SMasami Hiramatsu 
1821bf4d5f25SMasami Hiramatsu 	if (strbuf_init(&buf, 64) < 0)
1822bf4d5f25SMasami Hiramatsu 		return NULL;
1823bf4d5f25SMasami Hiramatsu 
182448481938SMasami Hiramatsu 	if (pa->name && pa->var)
1825bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
182648481938SMasami Hiramatsu 	else
1827bf4d5f25SMasami Hiramatsu 		err = strbuf_addstr(&buf, pa->name ?: pa->var);
1828bf4d5f25SMasami Hiramatsu 	if (err)
1829bf4d5f25SMasami Hiramatsu 		goto out;
18307df2f329SMasami Hiramatsu 
18317df2f329SMasami Hiramatsu 	while (field) {
1832b2a3c12bSMasami Hiramatsu 		if (field->name[0] == '[')
1833bf4d5f25SMasami Hiramatsu 			err = strbuf_addstr(&buf, field->name);
1834b2a3c12bSMasami Hiramatsu 		else
1835bf4d5f25SMasami Hiramatsu 			err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1836909b0360SMasami Hiramatsu 					  field->name);
18377df2f329SMasami Hiramatsu 		field = field->next;
1838bf4d5f25SMasami Hiramatsu 		if (err)
1839bf4d5f25SMasami Hiramatsu 			goto out;
18407df2f329SMasami Hiramatsu 	}
184111a1ca35SMasami Hiramatsu 
1842909b0360SMasami Hiramatsu 	if (pa->type)
1843bf4d5f25SMasami Hiramatsu 		if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1844bf4d5f25SMasami Hiramatsu 			goto out;
184511a1ca35SMasami Hiramatsu 
1846909b0360SMasami Hiramatsu 	ret = strbuf_detach(&buf, NULL);
1847bf4d5f25SMasami Hiramatsu out:
1848bf4d5f25SMasami Hiramatsu 	strbuf_release(&buf);
1849146a1439SMasami Hiramatsu 	return ret;
18507df2f329SMasami Hiramatsu }
18517df2f329SMasami Hiramatsu 
18524235b045SMasami Hiramatsu /* Compose only probe point (not argument) */
1853c4ff4920SMasami Hiramatsu char *synthesize_perf_probe_point(struct perf_probe_point *pp)
185450656eecSMasami Hiramatsu {
1855909b0360SMasami Hiramatsu 	struct strbuf buf;
1856bf4d5f25SMasami Hiramatsu 	char *tmp, *ret = NULL;
1857bf4d5f25SMasami Hiramatsu 	int len, err = 0;
185850656eecSMasami Hiramatsu 
1859bf4d5f25SMasami Hiramatsu 	if (strbuf_init(&buf, 64) < 0)
1860bf4d5f25SMasami Hiramatsu 		return NULL;
1861bf4d5f25SMasami Hiramatsu 
1862909b0360SMasami Hiramatsu 	if (pp->function) {
1863bf4d5f25SMasami Hiramatsu 		if (strbuf_addstr(&buf, pp->function) < 0)
1864bf4d5f25SMasami Hiramatsu 			goto out;
1865909b0360SMasami Hiramatsu 		if (pp->offset)
1866bf4d5f25SMasami Hiramatsu 			err = strbuf_addf(&buf, "+%lu", pp->offset);
1867909b0360SMasami Hiramatsu 		else if (pp->line)
1868bf4d5f25SMasami Hiramatsu 			err = strbuf_addf(&buf, ":%d", pp->line);
1869909b0360SMasami Hiramatsu 		else if (pp->retprobe)
1870bf4d5f25SMasami Hiramatsu 			err = strbuf_addstr(&buf, "%return");
1871bf4d5f25SMasami Hiramatsu 		if (err)
1872bf4d5f25SMasami Hiramatsu 			goto out;
1873fb1587d8SMasami Hiramatsu 	}
1874fb1587d8SMasami Hiramatsu 	if (pp->file) {
187532ae2adeSFranck Bui-Huu 		tmp = pp->file;
187632ae2adeSFranck Bui-Huu 		len = strlen(tmp);
187732ae2adeSFranck Bui-Huu 		if (len > 30) {
187832ae2adeSFranck Bui-Huu 			tmp = strchr(pp->file + len - 30, '/');
187932ae2adeSFranck Bui-Huu 			tmp = tmp ? tmp + 1 : pp->file + len - 30;
188032ae2adeSFranck Bui-Huu 		}
1881bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(&buf, "@%s", tmp);
1882bf4d5f25SMasami Hiramatsu 		if (!err && !pp->function && pp->line)
1883bf4d5f25SMasami Hiramatsu 			err = strbuf_addf(&buf, ":%d", pp->line);
18844de189feSMasami Hiramatsu 	}
1885bf4d5f25SMasami Hiramatsu 	if (!err)
1886bf4d5f25SMasami Hiramatsu 		ret = strbuf_detach(&buf, NULL);
1887bf4d5f25SMasami Hiramatsu out:
1888bf4d5f25SMasami Hiramatsu 	strbuf_release(&buf);
1889bf4d5f25SMasami Hiramatsu 	return ret;
18904235b045SMasami Hiramatsu }
18914235b045SMasami Hiramatsu 
18924235b045SMasami Hiramatsu char *synthesize_perf_probe_command(struct perf_probe_event *pev)
18934235b045SMasami Hiramatsu {
1894c4ff4920SMasami Hiramatsu 	struct strbuf buf;
1895c4ff4920SMasami Hiramatsu 	char *tmp, *ret = NULL;
1896c4ff4920SMasami Hiramatsu 	int i;
18974235b045SMasami Hiramatsu 
1898c4ff4920SMasami Hiramatsu 	if (strbuf_init(&buf, 64))
18994235b045SMasami Hiramatsu 		return NULL;
1900c4ff4920SMasami Hiramatsu 	if (pev->event)
1901c4ff4920SMasami Hiramatsu 		if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1902c4ff4920SMasami Hiramatsu 				pev->event) < 0)
1903c4ff4920SMasami Hiramatsu 			goto out;
19044235b045SMasami Hiramatsu 
1905c4ff4920SMasami Hiramatsu 	tmp = synthesize_perf_probe_point(&pev->point);
1906c4ff4920SMasami Hiramatsu 	if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1907c4ff4920SMasami Hiramatsu 		goto out;
1908c4ff4920SMasami Hiramatsu 	free(tmp);
1909c4ff4920SMasami Hiramatsu 
19104235b045SMasami Hiramatsu 	for (i = 0; i < pev->nargs; i++) {
1911c4ff4920SMasami Hiramatsu 		tmp = synthesize_perf_probe_arg(pev->args + i);
1912c4ff4920SMasami Hiramatsu 		if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1913c4ff4920SMasami Hiramatsu 			goto out;
1914c4ff4920SMasami Hiramatsu 		free(tmp);
19157ef17aafSMasami Hiramatsu 	}
191650656eecSMasami Hiramatsu 
1917c4ff4920SMasami Hiramatsu 	ret = strbuf_detach(&buf, NULL);
1918c4ff4920SMasami Hiramatsu out:
1919c4ff4920SMasami Hiramatsu 	strbuf_release(&buf);
1920c4ff4920SMasami Hiramatsu 	return ret;
19214235b045SMasami Hiramatsu }
19224235b045SMasami Hiramatsu 
19230e60836bSSrikar Dronamraju static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1924909b0360SMasami Hiramatsu 					    struct strbuf *buf, int depth)
19257ef17aafSMasami Hiramatsu {
1926bf4d5f25SMasami Hiramatsu 	int err;
19274235b045SMasami Hiramatsu 	if (ref->next) {
19280e60836bSSrikar Dronamraju 		depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1929909b0360SMasami Hiramatsu 							 depth + 1);
19304235b045SMasami Hiramatsu 		if (depth < 0)
19314235b045SMasami Hiramatsu 			return depth;
19324235b045SMasami Hiramatsu 	}
1933bf4d5f25SMasami Hiramatsu 	err = strbuf_addf(buf, "%+ld(", ref->offset);
1934bf4d5f25SMasami Hiramatsu 	return (err < 0) ? err : depth;
1935bf4d5f25SMasami Hiramatsu }
19364235b045SMasami Hiramatsu 
19370e60836bSSrikar Dronamraju static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1938909b0360SMasami Hiramatsu 				      struct strbuf *buf)
19394235b045SMasami Hiramatsu {
19400e60836bSSrikar Dronamraju 	struct probe_trace_arg_ref *ref = arg->ref;
1941bf4d5f25SMasami Hiramatsu 	int depth = 0, err;
19424235b045SMasami Hiramatsu 
19434235b045SMasami Hiramatsu 	/* Argument name or separator */
19444235b045SMasami Hiramatsu 	if (arg->name)
1945bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(buf, " %s=", arg->name);
19464235b045SMasami Hiramatsu 	else
1947bf4d5f25SMasami Hiramatsu 		err = strbuf_addch(buf, ' ');
1948bf4d5f25SMasami Hiramatsu 	if (err)
1949bf4d5f25SMasami Hiramatsu 		return err;
19504235b045SMasami Hiramatsu 
1951b7dcb857SMasami Hiramatsu 	/* Special case: @XXX */
1952b7dcb857SMasami Hiramatsu 	if (arg->value[0] == '@' && arg->ref)
1953b7dcb857SMasami Hiramatsu 			ref = ref->next;
1954b7dcb857SMasami Hiramatsu 
19554235b045SMasami Hiramatsu 	/* Dereferencing arguments */
1956b7dcb857SMasami Hiramatsu 	if (ref) {
1957909b0360SMasami Hiramatsu 		depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
19584235b045SMasami Hiramatsu 		if (depth < 0)
19594235b045SMasami Hiramatsu 			return depth;
19604235b045SMasami Hiramatsu 	}
19614235b045SMasami Hiramatsu 
19624235b045SMasami Hiramatsu 	/* Print argument value */
1963b7dcb857SMasami Hiramatsu 	if (arg->value[0] == '@' && arg->ref)
1964bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
1965b7dcb857SMasami Hiramatsu 	else
1966bf4d5f25SMasami Hiramatsu 		err = strbuf_addstr(buf, arg->value);
19674235b045SMasami Hiramatsu 
19684235b045SMasami Hiramatsu 	/* Closing */
1969bf4d5f25SMasami Hiramatsu 	while (!err && depth--)
1970bf4d5f25SMasami Hiramatsu 		err = strbuf_addch(buf, ')');
19714235b045SMasami Hiramatsu 
1972bf4d5f25SMasami Hiramatsu 	/* Print argument type */
1973bf4d5f25SMasami Hiramatsu 	if (!err && arg->type)
1974bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(buf, ":%s", arg->type);
1975bf4d5f25SMasami Hiramatsu 
1976bf4d5f25SMasami Hiramatsu 	return err;
19774235b045SMasami Hiramatsu }
19784235b045SMasami Hiramatsu 
19790e60836bSSrikar Dronamraju char *synthesize_probe_trace_command(struct probe_trace_event *tev)
19804235b045SMasami Hiramatsu {
19810e60836bSSrikar Dronamraju 	struct probe_trace_point *tp = &tev->point;
1982909b0360SMasami Hiramatsu 	struct strbuf buf;
1983909b0360SMasami Hiramatsu 	char *ret = NULL;
1984bf4d5f25SMasami Hiramatsu 	int i, err;
1985eb948e50SMasami Hiramatsu 
1986da15bd9dSWang Nan 	/* Uprobes must have tp->module */
1987da15bd9dSWang Nan 	if (tev->uprobes && !tp->module)
1988909b0360SMasami Hiramatsu 		return NULL;
1989909b0360SMasami Hiramatsu 
1990bf4d5f25SMasami Hiramatsu 	if (strbuf_init(&buf, 32) < 0)
1991bf4d5f25SMasami Hiramatsu 		return NULL;
1992bf4d5f25SMasami Hiramatsu 
1993bf4d5f25SMasami Hiramatsu 	if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1994bf4d5f25SMasami Hiramatsu 			tev->group, tev->event) < 0)
1995bf4d5f25SMasami Hiramatsu 		goto error;
1996da15bd9dSWang Nan 	/*
1997da15bd9dSWang Nan 	 * If tp->address == 0, then this point must be a
1998da15bd9dSWang Nan 	 * absolute address uprobe.
1999da15bd9dSWang Nan 	 * try_to_find_absolute_address() should have made
2000da15bd9dSWang Nan 	 * tp->symbol to "0x0".
2001da15bd9dSWang Nan 	 */
2002da15bd9dSWang Nan 	if (tev->uprobes && !tp->address) {
2003da15bd9dSWang Nan 		if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2004da15bd9dSWang Nan 			goto error;
2005da15bd9dSWang Nan 	}
2006eb948e50SMasami Hiramatsu 
2007eb948e50SMasami Hiramatsu 	/* Use the tp->address for uprobes */
2008225466f1SSrikar Dronamraju 	if (tev->uprobes)
2009bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
2010da15bd9dSWang Nan 	else if (!strncmp(tp->symbol, "0x", 2))
2011da15bd9dSWang Nan 		/* Absolute address. See try_to_find_absolute_address() */
2012bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2013909b0360SMasami Hiramatsu 				  tp->module ? ":" : "", tp->address);
2014225466f1SSrikar Dronamraju 	else
2015bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2016909b0360SMasami Hiramatsu 				tp->module ? ":" : "", tp->symbol, tp->offset);
2017bf4d5f25SMasami Hiramatsu 	if (err)
2018bf4d5f25SMasami Hiramatsu 		goto error;
2019225466f1SSrikar Dronamraju 
2020909b0360SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++)
2021909b0360SMasami Hiramatsu 		if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
20224235b045SMasami Hiramatsu 			goto error;
20237ef17aafSMasami Hiramatsu 
2024909b0360SMasami Hiramatsu 	ret = strbuf_detach(&buf, NULL);
202550656eecSMasami Hiramatsu error:
2026909b0360SMasami Hiramatsu 	strbuf_release(&buf);
2027909b0360SMasami Hiramatsu 	return ret;
202850656eecSMasami Hiramatsu }
202950656eecSMasami Hiramatsu 
20305a6f6314SMasami Hiramatsu static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
20315a6f6314SMasami Hiramatsu 					  struct perf_probe_point *pp,
20325a6f6314SMasami Hiramatsu 					  bool is_kprobe)
20335a6f6314SMasami Hiramatsu {
20345a6f6314SMasami Hiramatsu 	struct symbol *sym = NULL;
20355a6f6314SMasami Hiramatsu 	struct map *map;
2036e486367fSWang Nan 	u64 addr = tp->address;
20375a6f6314SMasami Hiramatsu 	int ret = -ENOENT;
20385a6f6314SMasami Hiramatsu 
20395a6f6314SMasami Hiramatsu 	if (!is_kprobe) {
20405a6f6314SMasami Hiramatsu 		map = dso__new_map(tp->module);
20415a6f6314SMasami Hiramatsu 		if (!map)
20425a6f6314SMasami Hiramatsu 			goto out;
2043be39db9fSArnaldo Carvalho de Melo 		sym = map__find_symbol(map, addr);
20445a6f6314SMasami Hiramatsu 	} else {
20459b239a12SMasami Hiramatsu 		if (tp->symbol && !addr) {
20460a62f686SMasami Hiramatsu 			if (kernel_get_symbol_address_by_name(tp->symbol,
20470a62f686SMasami Hiramatsu 						&addr, true, false) < 0)
20489b239a12SMasami Hiramatsu 				goto out;
20499b239a12SMasami Hiramatsu 		}
20505a6f6314SMasami Hiramatsu 		if (addr) {
20515a6f6314SMasami Hiramatsu 			addr += tp->offset;
20525a6f6314SMasami Hiramatsu 			sym = __find_kernel_function(addr, &map);
20535a6f6314SMasami Hiramatsu 		}
20545a6f6314SMasami Hiramatsu 	}
205598d3b258SWang Nan 
20565a6f6314SMasami Hiramatsu 	if (!sym)
20575a6f6314SMasami Hiramatsu 		goto out;
20585a6f6314SMasami Hiramatsu 
20595a6f6314SMasami Hiramatsu 	pp->retprobe = tp->retprobe;
20605a6f6314SMasami Hiramatsu 	pp->offset = addr - map->unmap_ip(map, sym->start);
20615a6f6314SMasami Hiramatsu 	pp->function = strdup(sym->name);
20625a6f6314SMasami Hiramatsu 	ret = pp->function ? 0 : -ENOMEM;
20635a6f6314SMasami Hiramatsu 
20645a6f6314SMasami Hiramatsu out:
20655a6f6314SMasami Hiramatsu 	if (map && !is_kprobe) {
206684c2cafaSArnaldo Carvalho de Melo 		map__put(map);
20675a6f6314SMasami Hiramatsu 	}
20685a6f6314SMasami Hiramatsu 
20695a6f6314SMasami Hiramatsu 	return ret;
20705a6f6314SMasami Hiramatsu }
20715a6f6314SMasami Hiramatsu 
20725a6f6314SMasami Hiramatsu static int convert_to_perf_probe_point(struct probe_trace_point *tp,
20735a6f6314SMasami Hiramatsu 				       struct perf_probe_point *pp,
20745a6f6314SMasami Hiramatsu 				       bool is_kprobe)
20755a6f6314SMasami Hiramatsu {
20765a6f6314SMasami Hiramatsu 	char buf[128];
20775a6f6314SMasami Hiramatsu 	int ret;
20785a6f6314SMasami Hiramatsu 
20795a6f6314SMasami Hiramatsu 	ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
20805a6f6314SMasami Hiramatsu 	if (!ret)
20815a6f6314SMasami Hiramatsu 		return 0;
20825a6f6314SMasami Hiramatsu 	ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
20835a6f6314SMasami Hiramatsu 	if (!ret)
20845a6f6314SMasami Hiramatsu 		return 0;
20855a6f6314SMasami Hiramatsu 
20865a6f6314SMasami Hiramatsu 	pr_debug("Failed to find probe point from both of dwarf and map.\n");
20875a6f6314SMasami Hiramatsu 
20885a6f6314SMasami Hiramatsu 	if (tp->symbol) {
20895a6f6314SMasami Hiramatsu 		pp->function = strdup(tp->symbol);
20905a6f6314SMasami Hiramatsu 		pp->offset = tp->offset;
2091614e2fdbSWang Nan 	} else {
20925a6f6314SMasami Hiramatsu 		ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
20935a6f6314SMasami Hiramatsu 		if (ret < 0)
20945a6f6314SMasami Hiramatsu 			return ret;
20955a6f6314SMasami Hiramatsu 		pp->function = strdup(buf);
20965a6f6314SMasami Hiramatsu 		pp->offset = 0;
20975a6f6314SMasami Hiramatsu 	}
20985a6f6314SMasami Hiramatsu 	if (pp->function == NULL)
20995a6f6314SMasami Hiramatsu 		return -ENOMEM;
21005a6f6314SMasami Hiramatsu 
21015a6f6314SMasami Hiramatsu 	pp->retprobe = tp->retprobe;
21025a6f6314SMasami Hiramatsu 
21035a6f6314SMasami Hiramatsu 	return 0;
21045a6f6314SMasami Hiramatsu }
21055a6f6314SMasami Hiramatsu 
21060e60836bSSrikar Dronamraju static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2107225466f1SSrikar Dronamraju 			       struct perf_probe_event *pev, bool is_kprobe)
21084de189feSMasami Hiramatsu {
2109909b0360SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
2110146a1439SMasami Hiramatsu 	int i, ret;
21114de189feSMasami Hiramatsu 
21124b4da7f7SMasami Hiramatsu 	/* Convert event/group name */
211302b95dadSMasami Hiramatsu 	pev->event = strdup(tev->event);
211402b95dadSMasami Hiramatsu 	pev->group = strdup(tev->group);
211502b95dadSMasami Hiramatsu 	if (pev->event == NULL || pev->group == NULL)
211602b95dadSMasami Hiramatsu 		return -ENOMEM;
2117fb1587d8SMasami Hiramatsu 
21184b4da7f7SMasami Hiramatsu 	/* Convert trace_point to probe_point */
21195a6f6314SMasami Hiramatsu 	ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2120146a1439SMasami Hiramatsu 	if (ret < 0)
2121146a1439SMasami Hiramatsu 		return ret;
21224b4da7f7SMasami Hiramatsu 
21234235b045SMasami Hiramatsu 	/* Convert trace_arg to probe_arg */
21244235b045SMasami Hiramatsu 	pev->nargs = tev->nargs;
2125e334016fSMasami Hiramatsu 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2126e334016fSMasami Hiramatsu 	if (pev->args == NULL)
2127e334016fSMasami Hiramatsu 		return -ENOMEM;
212802b95dadSMasami Hiramatsu 	for (i = 0; i < tev->nargs && ret >= 0; i++) {
21294235b045SMasami Hiramatsu 		if (tev->args[i].name)
213002b95dadSMasami Hiramatsu 			pev->args[i].name = strdup(tev->args[i].name);
21314235b045SMasami Hiramatsu 		else {
2132bf4d5f25SMasami Hiramatsu 			if ((ret = strbuf_init(&buf, 32)) < 0)
2133bf4d5f25SMasami Hiramatsu 				goto error;
2134909b0360SMasami Hiramatsu 			ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2135909b0360SMasami Hiramatsu 			pev->args[i].name = strbuf_detach(&buf, NULL);
213602b95dadSMasami Hiramatsu 		}
213702b95dadSMasami Hiramatsu 		if (pev->args[i].name == NULL && ret >= 0)
213802b95dadSMasami Hiramatsu 			ret = -ENOMEM;
21394de189feSMasami Hiramatsu 	}
2140bf4d5f25SMasami Hiramatsu error:
2141146a1439SMasami Hiramatsu 	if (ret < 0)
2142146a1439SMasami Hiramatsu 		clear_perf_probe_event(pev);
2143146a1439SMasami Hiramatsu 
2144146a1439SMasami Hiramatsu 	return ret;
21454235b045SMasami Hiramatsu }
21464de189feSMasami Hiramatsu 
21474235b045SMasami Hiramatsu void clear_perf_probe_event(struct perf_probe_event *pev)
21484235b045SMasami Hiramatsu {
21497df2f329SMasami Hiramatsu 	struct perf_probe_arg_field *field, *next;
21504235b045SMasami Hiramatsu 	int i;
21514de189feSMasami Hiramatsu 
21524235b045SMasami Hiramatsu 	free(pev->event);
21534235b045SMasami Hiramatsu 	free(pev->group);
21547afb3fabSMasami Hiramatsu 	free(pev->target);
21559b118acaSMasami Hiramatsu 	clear_perf_probe_point(&pev->point);
2156f5385650SArnaldo Carvalho de Melo 
21577df2f329SMasami Hiramatsu 	for (i = 0; i < pev->nargs; i++) {
21584235b045SMasami Hiramatsu 		free(pev->args[i].name);
215948481938SMasami Hiramatsu 		free(pev->args[i].var);
216011a1ca35SMasami Hiramatsu 		free(pev->args[i].type);
21617df2f329SMasami Hiramatsu 		field = pev->args[i].field;
21627df2f329SMasami Hiramatsu 		while (field) {
21637df2f329SMasami Hiramatsu 			next = field->next;
216474cf249dSArnaldo Carvalho de Melo 			zfree(&field->name);
21657df2f329SMasami Hiramatsu 			free(field);
21667df2f329SMasami Hiramatsu 			field = next;
21677df2f329SMasami Hiramatsu 		}
21687df2f329SMasami Hiramatsu 	}
21694235b045SMasami Hiramatsu 	free(pev->args);
21704235b045SMasami Hiramatsu 	memset(pev, 0, sizeof(*pev));
21714235b045SMasami Hiramatsu }
21724235b045SMasami Hiramatsu 
21730542bb9cSMasami Hiramatsu #define strdup_or_goto(str, label)	\
21740542bb9cSMasami Hiramatsu ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
21750542bb9cSMasami Hiramatsu 
21760542bb9cSMasami Hiramatsu static int perf_probe_point__copy(struct perf_probe_point *dst,
21770542bb9cSMasami Hiramatsu 				  struct perf_probe_point *src)
21780542bb9cSMasami Hiramatsu {
21790542bb9cSMasami Hiramatsu 	dst->file = strdup_or_goto(src->file, out_err);
21800542bb9cSMasami Hiramatsu 	dst->function = strdup_or_goto(src->function, out_err);
21810542bb9cSMasami Hiramatsu 	dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
21820542bb9cSMasami Hiramatsu 	dst->line = src->line;
21830542bb9cSMasami Hiramatsu 	dst->retprobe = src->retprobe;
21840542bb9cSMasami Hiramatsu 	dst->offset = src->offset;
21850542bb9cSMasami Hiramatsu 	return 0;
21860542bb9cSMasami Hiramatsu 
21870542bb9cSMasami Hiramatsu out_err:
21880542bb9cSMasami Hiramatsu 	clear_perf_probe_point(dst);
21890542bb9cSMasami Hiramatsu 	return -ENOMEM;
21900542bb9cSMasami Hiramatsu }
21910542bb9cSMasami Hiramatsu 
21920542bb9cSMasami Hiramatsu static int perf_probe_arg__copy(struct perf_probe_arg *dst,
21930542bb9cSMasami Hiramatsu 				struct perf_probe_arg *src)
21940542bb9cSMasami Hiramatsu {
21950542bb9cSMasami Hiramatsu 	struct perf_probe_arg_field *field, **ppfield;
21960542bb9cSMasami Hiramatsu 
21970542bb9cSMasami Hiramatsu 	dst->name = strdup_or_goto(src->name, out_err);
21980542bb9cSMasami Hiramatsu 	dst->var = strdup_or_goto(src->var, out_err);
21990542bb9cSMasami Hiramatsu 	dst->type = strdup_or_goto(src->type, out_err);
22000542bb9cSMasami Hiramatsu 
22010542bb9cSMasami Hiramatsu 	field = src->field;
22020542bb9cSMasami Hiramatsu 	ppfield = &(dst->field);
22030542bb9cSMasami Hiramatsu 	while (field) {
22040542bb9cSMasami Hiramatsu 		*ppfield = zalloc(sizeof(*field));
22050542bb9cSMasami Hiramatsu 		if (!*ppfield)
22060542bb9cSMasami Hiramatsu 			goto out_err;
22070542bb9cSMasami Hiramatsu 		(*ppfield)->name = strdup_or_goto(field->name, out_err);
22080542bb9cSMasami Hiramatsu 		(*ppfield)->index = field->index;
22090542bb9cSMasami Hiramatsu 		(*ppfield)->ref = field->ref;
22100542bb9cSMasami Hiramatsu 		field = field->next;
22110542bb9cSMasami Hiramatsu 		ppfield = &((*ppfield)->next);
22120542bb9cSMasami Hiramatsu 	}
22130542bb9cSMasami Hiramatsu 	return 0;
22140542bb9cSMasami Hiramatsu out_err:
22150542bb9cSMasami Hiramatsu 	return -ENOMEM;
22160542bb9cSMasami Hiramatsu }
22170542bb9cSMasami Hiramatsu 
22180542bb9cSMasami Hiramatsu int perf_probe_event__copy(struct perf_probe_event *dst,
22190542bb9cSMasami Hiramatsu 			   struct perf_probe_event *src)
22200542bb9cSMasami Hiramatsu {
22210542bb9cSMasami Hiramatsu 	int i;
22220542bb9cSMasami Hiramatsu 
22230542bb9cSMasami Hiramatsu 	dst->event = strdup_or_goto(src->event, out_err);
22240542bb9cSMasami Hiramatsu 	dst->group = strdup_or_goto(src->group, out_err);
22250542bb9cSMasami Hiramatsu 	dst->target = strdup_or_goto(src->target, out_err);
22260542bb9cSMasami Hiramatsu 	dst->uprobes = src->uprobes;
22270542bb9cSMasami Hiramatsu 
22280542bb9cSMasami Hiramatsu 	if (perf_probe_point__copy(&dst->point, &src->point) < 0)
22290542bb9cSMasami Hiramatsu 		goto out_err;
22300542bb9cSMasami Hiramatsu 
22310542bb9cSMasami Hiramatsu 	dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
22320542bb9cSMasami Hiramatsu 	if (!dst->args)
22330542bb9cSMasami Hiramatsu 		goto out_err;
22340542bb9cSMasami Hiramatsu 	dst->nargs = src->nargs;
22350542bb9cSMasami Hiramatsu 
22360542bb9cSMasami Hiramatsu 	for (i = 0; i < src->nargs; i++)
22370542bb9cSMasami Hiramatsu 		if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
22380542bb9cSMasami Hiramatsu 			goto out_err;
22390542bb9cSMasami Hiramatsu 	return 0;
22400542bb9cSMasami Hiramatsu 
22410542bb9cSMasami Hiramatsu out_err:
22420542bb9cSMasami Hiramatsu 	clear_perf_probe_event(dst);
22430542bb9cSMasami Hiramatsu 	return -ENOMEM;
22440542bb9cSMasami Hiramatsu }
22450542bb9cSMasami Hiramatsu 
224692f6c72eSMasami Hiramatsu void clear_probe_trace_event(struct probe_trace_event *tev)
22474235b045SMasami Hiramatsu {
22480e60836bSSrikar Dronamraju 	struct probe_trace_arg_ref *ref, *next;
22494235b045SMasami Hiramatsu 	int i;
22504235b045SMasami Hiramatsu 
22514235b045SMasami Hiramatsu 	free(tev->event);
22524235b045SMasami Hiramatsu 	free(tev->group);
22534235b045SMasami Hiramatsu 	free(tev->point.symbol);
22544c859351SMasami Hiramatsu 	free(tev->point.realname);
2255190b57fcSMasami Hiramatsu 	free(tev->point.module);
22564235b045SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++) {
22574235b045SMasami Hiramatsu 		free(tev->args[i].name);
22584235b045SMasami Hiramatsu 		free(tev->args[i].value);
22594984912eSMasami Hiramatsu 		free(tev->args[i].type);
22604235b045SMasami Hiramatsu 		ref = tev->args[i].ref;
22614235b045SMasami Hiramatsu 		while (ref) {
22624235b045SMasami Hiramatsu 			next = ref->next;
22634235b045SMasami Hiramatsu 			free(ref);
22644235b045SMasami Hiramatsu 			ref = next;
22654235b045SMasami Hiramatsu 		}
22664235b045SMasami Hiramatsu 	}
22674235b045SMasami Hiramatsu 	free(tev->args);
22684235b045SMasami Hiramatsu 	memset(tev, 0, sizeof(*tev));
22694de189feSMasami Hiramatsu }
22704de189feSMasami Hiramatsu 
22719aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node {
22729aaf5a5fSMasami Hiramatsu 	struct list_head list;
22739aaf5a5fSMasami Hiramatsu 	unsigned long start;
22749aaf5a5fSMasami Hiramatsu 	unsigned long end;
22759aaf5a5fSMasami Hiramatsu 	char *symbol;
22769aaf5a5fSMasami Hiramatsu };
22779aaf5a5fSMasami Hiramatsu 
22789aaf5a5fSMasami Hiramatsu static void kprobe_blacklist__delete(struct list_head *blacklist)
22799aaf5a5fSMasami Hiramatsu {
22809aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
22819aaf5a5fSMasami Hiramatsu 
22829aaf5a5fSMasami Hiramatsu 	while (!list_empty(blacklist)) {
22839aaf5a5fSMasami Hiramatsu 		node = list_first_entry(blacklist,
22849aaf5a5fSMasami Hiramatsu 					struct kprobe_blacklist_node, list);
22859aaf5a5fSMasami Hiramatsu 		list_del(&node->list);
22869aaf5a5fSMasami Hiramatsu 		free(node->symbol);
22879aaf5a5fSMasami Hiramatsu 		free(node);
22889aaf5a5fSMasami Hiramatsu 	}
22899aaf5a5fSMasami Hiramatsu }
22909aaf5a5fSMasami Hiramatsu 
22919aaf5a5fSMasami Hiramatsu static int kprobe_blacklist__load(struct list_head *blacklist)
22929aaf5a5fSMasami Hiramatsu {
22939aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
22944605eab3SJiri Olsa 	const char *__debugfs = debugfs__mountpoint();
22959aaf5a5fSMasami Hiramatsu 	char buf[PATH_MAX], *p;
22969aaf5a5fSMasami Hiramatsu 	FILE *fp;
22979aaf5a5fSMasami Hiramatsu 	int ret;
22989aaf5a5fSMasami Hiramatsu 
22999aaf5a5fSMasami Hiramatsu 	if (__debugfs == NULL)
23009aaf5a5fSMasami Hiramatsu 		return -ENOTSUP;
23019aaf5a5fSMasami Hiramatsu 
23029aaf5a5fSMasami Hiramatsu 	ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
23039aaf5a5fSMasami Hiramatsu 	if (ret < 0)
23049aaf5a5fSMasami Hiramatsu 		return ret;
23059aaf5a5fSMasami Hiramatsu 
23069aaf5a5fSMasami Hiramatsu 	fp = fopen(buf, "r");
23079aaf5a5fSMasami Hiramatsu 	if (!fp)
23089aaf5a5fSMasami Hiramatsu 		return -errno;
23099aaf5a5fSMasami Hiramatsu 
23109aaf5a5fSMasami Hiramatsu 	ret = 0;
23119aaf5a5fSMasami Hiramatsu 	while (fgets(buf, PATH_MAX, fp)) {
23129aaf5a5fSMasami Hiramatsu 		node = zalloc(sizeof(*node));
23139aaf5a5fSMasami Hiramatsu 		if (!node) {
23149aaf5a5fSMasami Hiramatsu 			ret = -ENOMEM;
23159aaf5a5fSMasami Hiramatsu 			break;
23169aaf5a5fSMasami Hiramatsu 		}
23179aaf5a5fSMasami Hiramatsu 		INIT_LIST_HEAD(&node->list);
23189aaf5a5fSMasami Hiramatsu 		list_add_tail(&node->list, blacklist);
23199aaf5a5fSMasami Hiramatsu 		if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
23209aaf5a5fSMasami Hiramatsu 			ret = -EINVAL;
23219aaf5a5fSMasami Hiramatsu 			break;
23229aaf5a5fSMasami Hiramatsu 		}
23239aaf5a5fSMasami Hiramatsu 		p = strchr(buf, '\t');
23249aaf5a5fSMasami Hiramatsu 		if (p) {
23259aaf5a5fSMasami Hiramatsu 			p++;
23269aaf5a5fSMasami Hiramatsu 			if (p[strlen(p) - 1] == '\n')
23279aaf5a5fSMasami Hiramatsu 				p[strlen(p) - 1] = '\0';
23289aaf5a5fSMasami Hiramatsu 		} else
23299aaf5a5fSMasami Hiramatsu 			p = (char *)"unknown";
23309aaf5a5fSMasami Hiramatsu 		node->symbol = strdup(p);
23319aaf5a5fSMasami Hiramatsu 		if (!node->symbol) {
23329aaf5a5fSMasami Hiramatsu 			ret = -ENOMEM;
23339aaf5a5fSMasami Hiramatsu 			break;
23349aaf5a5fSMasami Hiramatsu 		}
23359aaf5a5fSMasami Hiramatsu 		pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
23369aaf5a5fSMasami Hiramatsu 			  node->start, node->end, node->symbol);
23379aaf5a5fSMasami Hiramatsu 		ret++;
23389aaf5a5fSMasami Hiramatsu 	}
23399aaf5a5fSMasami Hiramatsu 	if (ret < 0)
23409aaf5a5fSMasami Hiramatsu 		kprobe_blacklist__delete(blacklist);
23419aaf5a5fSMasami Hiramatsu 	fclose(fp);
23429aaf5a5fSMasami Hiramatsu 
23439aaf5a5fSMasami Hiramatsu 	return ret;
23449aaf5a5fSMasami Hiramatsu }
23459aaf5a5fSMasami Hiramatsu 
23469aaf5a5fSMasami Hiramatsu static struct kprobe_blacklist_node *
23479aaf5a5fSMasami Hiramatsu kprobe_blacklist__find_by_address(struct list_head *blacklist,
23489aaf5a5fSMasami Hiramatsu 				  unsigned long address)
23499aaf5a5fSMasami Hiramatsu {
23509aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
23519aaf5a5fSMasami Hiramatsu 
23529aaf5a5fSMasami Hiramatsu 	list_for_each_entry(node, blacklist, list) {
23539aaf5a5fSMasami Hiramatsu 		if (node->start <= address && address <= node->end)
23549aaf5a5fSMasami Hiramatsu 			return node;
23559aaf5a5fSMasami Hiramatsu 	}
23569aaf5a5fSMasami Hiramatsu 
23579aaf5a5fSMasami Hiramatsu 	return NULL;
23589aaf5a5fSMasami Hiramatsu }
23599aaf5a5fSMasami Hiramatsu 
2360b031220dSMasami Hiramatsu static LIST_HEAD(kprobe_blacklist);
2361b031220dSMasami Hiramatsu 
2362b031220dSMasami Hiramatsu static void kprobe_blacklist__init(void)
2363b031220dSMasami Hiramatsu {
2364b031220dSMasami Hiramatsu 	if (!list_empty(&kprobe_blacklist))
2365b031220dSMasami Hiramatsu 		return;
2366b031220dSMasami Hiramatsu 
2367b031220dSMasami Hiramatsu 	if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2368b031220dSMasami Hiramatsu 		pr_debug("No kprobe blacklist support, ignored\n");
2369b031220dSMasami Hiramatsu }
2370b031220dSMasami Hiramatsu 
2371b031220dSMasami Hiramatsu static void kprobe_blacklist__release(void)
2372b031220dSMasami Hiramatsu {
2373b031220dSMasami Hiramatsu 	kprobe_blacklist__delete(&kprobe_blacklist);
2374b031220dSMasami Hiramatsu }
2375b031220dSMasami Hiramatsu 
2376b031220dSMasami Hiramatsu static bool kprobe_blacklist__listed(unsigned long address)
2377b031220dSMasami Hiramatsu {
2378b031220dSMasami Hiramatsu 	return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2379b031220dSMasami Hiramatsu }
2380b031220dSMasami Hiramatsu 
2381d350bd57SMasami Hiramatsu static int perf_probe_event__sprintf(const char *group, const char *event,
2382d350bd57SMasami Hiramatsu 				     struct perf_probe_event *pev,
2383ba7ecb02SMasami Hiramatsu 				     const char *module,
2384ba7ecb02SMasami Hiramatsu 				     struct strbuf *result)
2385278498d4SMasami Hiramatsu {
2386bf4d5f25SMasami Hiramatsu 	int i, ret;
2387909b0360SMasami Hiramatsu 	char *buf;
2388909b0360SMasami Hiramatsu 
2389909b0360SMasami Hiramatsu 	if (asprintf(&buf, "%s:%s", group, event) < 0)
2390909b0360SMasami Hiramatsu 		return -errno;
2391bf4d5f25SMasami Hiramatsu 	ret = strbuf_addf(result, "  %-20s (on ", buf);
2392909b0360SMasami Hiramatsu 	free(buf);
2393bf4d5f25SMasami Hiramatsu 	if (ret)
2394bf4d5f25SMasami Hiramatsu 		return ret;
2395278498d4SMasami Hiramatsu 
23964235b045SMasami Hiramatsu 	/* Synthesize only event probe point */
2397909b0360SMasami Hiramatsu 	buf = synthesize_perf_probe_point(&pev->point);
2398909b0360SMasami Hiramatsu 	if (!buf)
2399909b0360SMasami Hiramatsu 		return -ENOMEM;
2400bf4d5f25SMasami Hiramatsu 	ret = strbuf_addstr(result, buf);
2401909b0360SMasami Hiramatsu 	free(buf);
24024235b045SMasami Hiramatsu 
2403bf4d5f25SMasami Hiramatsu 	if (!ret && module)
2404bf4d5f25SMasami Hiramatsu 		ret = strbuf_addf(result, " in %s", module);
2405278498d4SMasami Hiramatsu 
2406bf4d5f25SMasami Hiramatsu 	if (!ret && pev->nargs > 0) {
2407bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(result, " with", 5);
2408bf4d5f25SMasami Hiramatsu 		for (i = 0; !ret && i < pev->nargs; i++) {
2409909b0360SMasami Hiramatsu 			buf = synthesize_perf_probe_arg(&pev->args[i]);
2410909b0360SMasami Hiramatsu 			if (!buf)
2411909b0360SMasami Hiramatsu 				return -ENOMEM;
2412bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(result, " %s", buf);
2413909b0360SMasami Hiramatsu 			free(buf);
24147df2f329SMasami Hiramatsu 		}
2415278498d4SMasami Hiramatsu 	}
2416bf4d5f25SMasami Hiramatsu 	if (!ret)
2417bf4d5f25SMasami Hiramatsu 		ret = strbuf_addch(result, ')');
2418909b0360SMasami Hiramatsu 
2419bf4d5f25SMasami Hiramatsu 	return ret;
2420278498d4SMasami Hiramatsu }
2421278498d4SMasami Hiramatsu 
2422ba7ecb02SMasami Hiramatsu /* Show an event */
2423b02137ccSNamhyung Kim int show_perf_probe_event(const char *group, const char *event,
2424d350bd57SMasami Hiramatsu 			  struct perf_probe_event *pev,
2425ba7ecb02SMasami Hiramatsu 			  const char *module, bool use_stdout)
2426ba7ecb02SMasami Hiramatsu {
2427ba7ecb02SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
2428ba7ecb02SMasami Hiramatsu 	int ret;
2429ba7ecb02SMasami Hiramatsu 
2430d350bd57SMasami Hiramatsu 	ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2431ba7ecb02SMasami Hiramatsu 	if (ret >= 0) {
2432ba7ecb02SMasami Hiramatsu 		if (use_stdout)
2433ba7ecb02SMasami Hiramatsu 			printf("%s\n", buf.buf);
2434ba7ecb02SMasami Hiramatsu 		else
2435ba7ecb02SMasami Hiramatsu 			pr_info("%s\n", buf.buf);
2436ba7ecb02SMasami Hiramatsu 	}
2437ba7ecb02SMasami Hiramatsu 	strbuf_release(&buf);
2438ba7ecb02SMasami Hiramatsu 
2439ba7ecb02SMasami Hiramatsu 	return ret;
2440ba7ecb02SMasami Hiramatsu }
2441ba7ecb02SMasami Hiramatsu 
2442b6a89643SMasami Hiramatsu static bool filter_probe_trace_event(struct probe_trace_event *tev,
2443b6a89643SMasami Hiramatsu 				     struct strfilter *filter)
2444b6a89643SMasami Hiramatsu {
2445b6a89643SMasami Hiramatsu 	char tmp[128];
2446b6a89643SMasami Hiramatsu 
2447b6a89643SMasami Hiramatsu 	/* At first, check the event name itself */
2448b6a89643SMasami Hiramatsu 	if (strfilter__compare(filter, tev->event))
2449b6a89643SMasami Hiramatsu 		return true;
2450b6a89643SMasami Hiramatsu 
2451b6a89643SMasami Hiramatsu 	/* Next, check the combination of name and group */
2452b6a89643SMasami Hiramatsu 	if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2453b6a89643SMasami Hiramatsu 		return false;
2454b6a89643SMasami Hiramatsu 	return strfilter__compare(filter, tmp);
2455b6a89643SMasami Hiramatsu }
2456b6a89643SMasami Hiramatsu 
2457b6a89643SMasami Hiramatsu static int __show_perf_probe_events(int fd, bool is_kprobe,
2458b6a89643SMasami Hiramatsu 				    struct strfilter *filter)
24594de189feSMasami Hiramatsu {
2460225466f1SSrikar Dronamraju 	int ret = 0;
24610e60836bSSrikar Dronamraju 	struct probe_trace_event tev;
24624235b045SMasami Hiramatsu 	struct perf_probe_event pev;
24634de189feSMasami Hiramatsu 	struct strlist *rawlist;
24644de189feSMasami Hiramatsu 	struct str_node *ent;
24654de189feSMasami Hiramatsu 
24664235b045SMasami Hiramatsu 	memset(&tev, 0, sizeof(tev));
24674235b045SMasami Hiramatsu 	memset(&pev, 0, sizeof(pev));
246872041334SMasami Hiramatsu 
246992f6c72eSMasami Hiramatsu 	rawlist = probe_file__get_rawlist(fd);
2470146a1439SMasami Hiramatsu 	if (!rawlist)
24716eb08660SMasami Hiramatsu 		return -ENOMEM;
24724de189feSMasami Hiramatsu 
2473602a1f4dSArnaldo Carvalho de Melo 	strlist__for_each_entry(ent, rawlist) {
24740e60836bSSrikar Dronamraju 		ret = parse_probe_trace_command(ent->s, &tev);
2475146a1439SMasami Hiramatsu 		if (ret >= 0) {
2476b6a89643SMasami Hiramatsu 			if (!filter_probe_trace_event(&tev, filter))
2477b6a89643SMasami Hiramatsu 				goto next;
2478225466f1SSrikar Dronamraju 			ret = convert_to_perf_probe_event(&tev, &pev,
2479225466f1SSrikar Dronamraju 								is_kprobe);
2480ba7ecb02SMasami Hiramatsu 			if (ret < 0)
2481ba7ecb02SMasami Hiramatsu 				goto next;
2482d350bd57SMasami Hiramatsu 			ret = show_perf_probe_event(pev.group, pev.event,
2483d350bd57SMasami Hiramatsu 						    &pev, tev.point.module,
2484ba7ecb02SMasami Hiramatsu 						    true);
2485146a1439SMasami Hiramatsu 		}
2486b6a89643SMasami Hiramatsu next:
24874235b045SMasami Hiramatsu 		clear_perf_probe_event(&pev);
24880e60836bSSrikar Dronamraju 		clear_probe_trace_event(&tev);
2489146a1439SMasami Hiramatsu 		if (ret < 0)
2490146a1439SMasami Hiramatsu 			break;
24914de189feSMasami Hiramatsu 	}
24924de189feSMasami Hiramatsu 	strlist__delete(rawlist);
24937737af01SMasami Hiramatsu 	/* Cleanup cached debuginfo if needed */
24947737af01SMasami Hiramatsu 	debuginfo_cache__exit();
2495146a1439SMasami Hiramatsu 
2496146a1439SMasami Hiramatsu 	return ret;
24974de189feSMasami Hiramatsu }
24984de189feSMasami Hiramatsu 
2499225466f1SSrikar Dronamraju /* List up current perf-probe events */
2500b6a89643SMasami Hiramatsu int show_perf_probe_events(struct strfilter *filter)
2501225466f1SSrikar Dronamraju {
25025e45187cSMasami Hiramatsu 	int kp_fd, up_fd, ret;
2503225466f1SSrikar Dronamraju 
2504225466f1SSrikar Dronamraju 	setup_pager();
2505225466f1SSrikar Dronamraju 
25061f3736c9SMasami Hiramatsu 	if (probe_conf.cache)
25071f3736c9SMasami Hiramatsu 		return probe_cache__show_all_caches(filter);
25081f3736c9SMasami Hiramatsu 
25099bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(false);
2510225466f1SSrikar Dronamraju 	if (ret < 0)
2511225466f1SSrikar Dronamraju 		return ret;
2512225466f1SSrikar Dronamraju 
251392f6c72eSMasami Hiramatsu 	ret = probe_file__open_both(&kp_fd, &up_fd, 0);
25145e45187cSMasami Hiramatsu 	if (ret < 0)
2515225466f1SSrikar Dronamraju 		return ret;
2516225466f1SSrikar Dronamraju 
251792f6c72eSMasami Hiramatsu 	if (kp_fd >= 0)
251892f6c72eSMasami Hiramatsu 		ret = __show_perf_probe_events(kp_fd, true, filter);
251992f6c72eSMasami Hiramatsu 	if (up_fd >= 0 && ret >= 0)
252092f6c72eSMasami Hiramatsu 		ret = __show_perf_probe_events(up_fd, false, filter);
252192f6c72eSMasami Hiramatsu 	if (kp_fd > 0)
252292f6c72eSMasami Hiramatsu 		close(kp_fd);
252392f6c72eSMasami Hiramatsu 	if (up_fd > 0)
252492f6c72eSMasami Hiramatsu 		close(up_fd);
25259bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
2526b498ce1fSMasami Hiramatsu 
2527146a1439SMasami Hiramatsu 	return ret;
2528f4d7da49SMasami Hiramatsu }
252950656eecSMasami Hiramatsu 
2530146a1439SMasami Hiramatsu static int get_new_event_name(char *buf, size_t len, const char *base,
2531d761b08bSMasami Hiramatsu 			      struct strlist *namelist, bool allow_suffix)
2532b498ce1fSMasami Hiramatsu {
2533b498ce1fSMasami Hiramatsu 	int i, ret;
2534663b1151SMasami Hiramatsu 	char *p, *nbase;
253517f88fcdSMasami Hiramatsu 
25363099c026SNaveen N. Rao 	if (*base == '.')
25373099c026SNaveen N. Rao 		base++;
2538663b1151SMasami Hiramatsu 	nbase = strdup(base);
2539663b1151SMasami Hiramatsu 	if (!nbase)
2540663b1151SMasami Hiramatsu 		return -ENOMEM;
25413099c026SNaveen N. Rao 
2542663b1151SMasami Hiramatsu 	/* Cut off the dot suffixes (e.g. .const, .isra)*/
2543663b1151SMasami Hiramatsu 	p = strchr(nbase, '.');
2544663b1151SMasami Hiramatsu 	if (p && p != nbase)
2545663b1151SMasami Hiramatsu 		*p = '\0';
2546663b1151SMasami Hiramatsu 
2547663b1151SMasami Hiramatsu 	/* Try no suffix number */
2548663b1151SMasami Hiramatsu 	ret = e_snprintf(buf, len, "%s", nbase);
2549146a1439SMasami Hiramatsu 	if (ret < 0) {
25505f03cba4SMasami Hiramatsu 		pr_debug("snprintf() failed: %d\n", ret);
2551663b1151SMasami Hiramatsu 		goto out;
2552146a1439SMasami Hiramatsu 	}
255317f88fcdSMasami Hiramatsu 	if (!strlist__has_entry(namelist, buf))
2554663b1151SMasami Hiramatsu 		goto out;
255517f88fcdSMasami Hiramatsu 
2556d761b08bSMasami Hiramatsu 	if (!allow_suffix) {
255703e01f56SWang Nan 		pr_warning("Error: event \"%s\" already exists.\n"
255803e01f56SWang Nan 			   " Hint: Remove existing event by 'perf probe -d'\n"
255903e01f56SWang Nan 			   "       or force duplicates by 'perf probe -f'\n"
256003e01f56SWang Nan 			   "       or set 'force=yes' in BPF source.\n",
256103e01f56SWang Nan 			   buf);
2562663b1151SMasami Hiramatsu 		ret = -EEXIST;
2563663b1151SMasami Hiramatsu 		goto out;
2564d761b08bSMasami Hiramatsu 	}
2565d761b08bSMasami Hiramatsu 
256617f88fcdSMasami Hiramatsu 	/* Try to add suffix */
256717f88fcdSMasami Hiramatsu 	for (i = 1; i < MAX_EVENT_INDEX; i++) {
2568663b1151SMasami Hiramatsu 		ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2569146a1439SMasami Hiramatsu 		if (ret < 0) {
25705f03cba4SMasami Hiramatsu 			pr_debug("snprintf() failed: %d\n", ret);
2571663b1151SMasami Hiramatsu 			goto out;
2572146a1439SMasami Hiramatsu 		}
2573b498ce1fSMasami Hiramatsu 		if (!strlist__has_entry(namelist, buf))
2574b498ce1fSMasami Hiramatsu 			break;
2575b498ce1fSMasami Hiramatsu 	}
2576146a1439SMasami Hiramatsu 	if (i == MAX_EVENT_INDEX) {
2577146a1439SMasami Hiramatsu 		pr_warning("Too many events are on the same function.\n");
2578146a1439SMasami Hiramatsu 		ret = -ERANGE;
2579b498ce1fSMasami Hiramatsu 	}
2580b498ce1fSMasami Hiramatsu 
2581663b1151SMasami Hiramatsu out:
2582663b1151SMasami Hiramatsu 	free(nbase);
2583146a1439SMasami Hiramatsu 	return ret;
2584146a1439SMasami Hiramatsu }
2585146a1439SMasami Hiramatsu 
258679702f61SMasami Hiramatsu /* Warn if the current kernel's uprobe implementation is old */
258779702f61SMasami Hiramatsu static void warn_uprobe_event_compat(struct probe_trace_event *tev)
258879702f61SMasami Hiramatsu {
258979702f61SMasami Hiramatsu 	int i;
259079702f61SMasami Hiramatsu 	char *buf = synthesize_probe_trace_command(tev);
259179702f61SMasami Hiramatsu 
259279702f61SMasami Hiramatsu 	/* Old uprobe event doesn't support memory dereference */
259379702f61SMasami Hiramatsu 	if (!tev->uprobes || tev->nargs == 0 || !buf)
259479702f61SMasami Hiramatsu 		goto out;
259579702f61SMasami Hiramatsu 
259679702f61SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++)
259779702f61SMasami Hiramatsu 		if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
259879702f61SMasami Hiramatsu 			pr_warning("Please upgrade your kernel to at least "
259979702f61SMasami Hiramatsu 				   "3.14 to have access to feature %s\n",
260079702f61SMasami Hiramatsu 				   tev->args[i].value);
260179702f61SMasami Hiramatsu 			break;
260279702f61SMasami Hiramatsu 		}
260379702f61SMasami Hiramatsu out:
260479702f61SMasami Hiramatsu 	free(buf);
260579702f61SMasami Hiramatsu }
260679702f61SMasami Hiramatsu 
2607a3c9de62SMasami Hiramatsu /* Set new name from original perf_probe_event and namelist */
2608a3c9de62SMasami Hiramatsu static int probe_trace_event__set_name(struct probe_trace_event *tev,
2609a3c9de62SMasami Hiramatsu 				       struct perf_probe_event *pev,
2610a3c9de62SMasami Hiramatsu 				       struct strlist *namelist,
2611a3c9de62SMasami Hiramatsu 				       bool allow_suffix)
2612a3c9de62SMasami Hiramatsu {
2613a3c9de62SMasami Hiramatsu 	const char *event, *group;
2614a3c9de62SMasami Hiramatsu 	char buf[64];
2615a3c9de62SMasami Hiramatsu 	int ret;
2616a3c9de62SMasami Hiramatsu 
2617bc062230SMasami Hiramatsu 	/* If probe_event or trace_event already have the name, reuse it */
261842bba263SMasami Hiramatsu 	if (pev->event && !pev->sdt)
2619a3c9de62SMasami Hiramatsu 		event = pev->event;
2620bc062230SMasami Hiramatsu 	else if (tev->event)
2621bc062230SMasami Hiramatsu 		event = tev->event;
2622bc062230SMasami Hiramatsu 	else {
2623bc062230SMasami Hiramatsu 		/* Or generate new one from probe point */
2624da15bd9dSWang Nan 		if (pev->point.function &&
2625da15bd9dSWang Nan 			(strncmp(pev->point.function, "0x", 2) != 0) &&
2626da15bd9dSWang Nan 			!strisglob(pev->point.function))
2627a3c9de62SMasami Hiramatsu 			event = pev->point.function;
2628a3c9de62SMasami Hiramatsu 		else
2629a3c9de62SMasami Hiramatsu 			event = tev->point.realname;
2630bc062230SMasami Hiramatsu 	}
263142bba263SMasami Hiramatsu 	if (pev->group && !pev->sdt)
2632a3c9de62SMasami Hiramatsu 		group = pev->group;
2633bc062230SMasami Hiramatsu 	else if (tev->group)
2634bc062230SMasami Hiramatsu 		group = tev->group;
2635a3c9de62SMasami Hiramatsu 	else
2636a3c9de62SMasami Hiramatsu 		group = PERFPROBE_GROUP;
2637a3c9de62SMasami Hiramatsu 
2638a3c9de62SMasami Hiramatsu 	/* Get an unused new event name */
2639a3c9de62SMasami Hiramatsu 	ret = get_new_event_name(buf, 64, event,
2640a3c9de62SMasami Hiramatsu 				 namelist, allow_suffix);
2641a3c9de62SMasami Hiramatsu 	if (ret < 0)
2642a3c9de62SMasami Hiramatsu 		return ret;
2643a3c9de62SMasami Hiramatsu 
2644a3c9de62SMasami Hiramatsu 	event = buf;
2645a3c9de62SMasami Hiramatsu 
2646a3c9de62SMasami Hiramatsu 	tev->event = strdup(event);
2647a3c9de62SMasami Hiramatsu 	tev->group = strdup(group);
2648a3c9de62SMasami Hiramatsu 	if (tev->event == NULL || tev->group == NULL)
2649a3c9de62SMasami Hiramatsu 		return -ENOMEM;
2650a3c9de62SMasami Hiramatsu 
2651a3c9de62SMasami Hiramatsu 	/* Add added event name to namelist */
2652a3c9de62SMasami Hiramatsu 	strlist__add(namelist, event);
2653a3c9de62SMasami Hiramatsu 	return 0;
2654a3c9de62SMasami Hiramatsu }
2655a3c9de62SMasami Hiramatsu 
26561de7b8bfSMasami Hiramatsu static int __open_probe_file_and_namelist(bool uprobe,
26571de7b8bfSMasami Hiramatsu 					  struct strlist **namelist)
265850656eecSMasami Hiramatsu {
26591de7b8bfSMasami Hiramatsu 	int fd;
266050656eecSMasami Hiramatsu 
26611de7b8bfSMasami Hiramatsu 	fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
266292f6c72eSMasami Hiramatsu 	if (fd < 0)
2663146a1439SMasami Hiramatsu 		return fd;
26645e45187cSMasami Hiramatsu 
2665b498ce1fSMasami Hiramatsu 	/* Get current event names */
26661de7b8bfSMasami Hiramatsu 	*namelist = probe_file__get_namelist(fd);
26671de7b8bfSMasami Hiramatsu 	if (!(*namelist)) {
2668146a1439SMasami Hiramatsu 		pr_debug("Failed to get current event list.\n");
26691de7b8bfSMasami Hiramatsu 		close(fd);
26701de7b8bfSMasami Hiramatsu 		return -ENOMEM;
2671146a1439SMasami Hiramatsu 	}
26721de7b8bfSMasami Hiramatsu 	return fd;
26731de7b8bfSMasami Hiramatsu }
26741de7b8bfSMasami Hiramatsu 
26751de7b8bfSMasami Hiramatsu static int __add_probe_trace_events(struct perf_probe_event *pev,
26761de7b8bfSMasami Hiramatsu 				     struct probe_trace_event *tevs,
26771de7b8bfSMasami Hiramatsu 				     int ntevs, bool allow_suffix)
26781de7b8bfSMasami Hiramatsu {
26791de7b8bfSMasami Hiramatsu 	int i, fd[2] = {-1, -1}, up, ret;
26801de7b8bfSMasami Hiramatsu 	struct probe_trace_event *tev = NULL;
26811de7b8bfSMasami Hiramatsu 	struct probe_cache *cache = NULL;
26821de7b8bfSMasami Hiramatsu 	struct strlist *namelist[2] = {NULL, NULL};
26831de7b8bfSMasami Hiramatsu 
26841de7b8bfSMasami Hiramatsu 	up = pev->uprobes ? 1 : 0;
26851de7b8bfSMasami Hiramatsu 	fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
26861de7b8bfSMasami Hiramatsu 	if (fd[up] < 0)
26871de7b8bfSMasami Hiramatsu 		return fd[up];
268850656eecSMasami Hiramatsu 
2689146a1439SMasami Hiramatsu 	ret = 0;
269002b95dadSMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
26914235b045SMasami Hiramatsu 		tev = &tevs[i];
26921de7b8bfSMasami Hiramatsu 		up = tev->uprobes ? 1 : 0;
26931de7b8bfSMasami Hiramatsu 		if (fd[up] == -1) {	/* Open the kprobe/uprobe_events */
26941de7b8bfSMasami Hiramatsu 			fd[up] = __open_probe_file_and_namelist(up,
26951de7b8bfSMasami Hiramatsu 								&namelist[up]);
26961de7b8bfSMasami Hiramatsu 			if (fd[up] < 0)
26971de7b8bfSMasami Hiramatsu 				goto close_out;
26981de7b8bfSMasami Hiramatsu 		}
2699b031220dSMasami Hiramatsu 		/* Skip if the symbol is out of .text or blacklisted */
2700bc062230SMasami Hiramatsu 		if (!tev->point.symbol && !pev->uprobes)
27015a51fcd1SMasami Hiramatsu 			continue;
27029aaf5a5fSMasami Hiramatsu 
2703a3c9de62SMasami Hiramatsu 		/* Set new name for tev (and update namelist) */
27041de7b8bfSMasami Hiramatsu 		ret = probe_trace_event__set_name(tev, pev, namelist[up],
2705a3c9de62SMasami Hiramatsu 						  allow_suffix);
2706146a1439SMasami Hiramatsu 		if (ret < 0)
2707146a1439SMasami Hiramatsu 			break;
27084235b045SMasami Hiramatsu 
27091de7b8bfSMasami Hiramatsu 		ret = probe_file__add_event(fd[up], tev);
2710146a1439SMasami Hiramatsu 		if (ret < 0)
2711146a1439SMasami Hiramatsu 			break;
27124235b045SMasami Hiramatsu 
2713d761b08bSMasami Hiramatsu 		/*
2714d761b08bSMasami Hiramatsu 		 * Probes after the first probe which comes from same
2715d761b08bSMasami Hiramatsu 		 * user input are always allowed to add suffix, because
2716d761b08bSMasami Hiramatsu 		 * there might be several addresses corresponding to
2717d761b08bSMasami Hiramatsu 		 * one code line.
2718d761b08bSMasami Hiramatsu 		 */
2719d761b08bSMasami Hiramatsu 		allow_suffix = true;
272050656eecSMasami Hiramatsu 	}
272179702f61SMasami Hiramatsu 	if (ret == -EINVAL && pev->uprobes)
272279702f61SMasami Hiramatsu 		warn_uprobe_event_compat(tev);
27232fd457a3SMasami Hiramatsu 	if (ret == 0 && probe_conf.cache) {
27242fd457a3SMasami Hiramatsu 		cache = probe_cache__new(pev->target);
27252fd457a3SMasami Hiramatsu 		if (!cache ||
27262fd457a3SMasami Hiramatsu 		    probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
27272fd457a3SMasami Hiramatsu 		    probe_cache__commit(cache) < 0)
27282fd457a3SMasami Hiramatsu 			pr_warning("Failed to add event to probe cache\n");
27292fd457a3SMasami Hiramatsu 		probe_cache__delete(cache);
27302fd457a3SMasami Hiramatsu 	}
2731146a1439SMasami Hiramatsu 
2732ae2cb1acSMasami Hiramatsu close_out:
27331de7b8bfSMasami Hiramatsu 	for (up = 0; up < 2; up++) {
27341de7b8bfSMasami Hiramatsu 		strlist__delete(namelist[up]);
27351de7b8bfSMasami Hiramatsu 		if (fd[up] >= 0)
27361de7b8bfSMasami Hiramatsu 			close(fd[up]);
27371de7b8bfSMasami Hiramatsu 	}
2738146a1439SMasami Hiramatsu 	return ret;
273950656eecSMasami Hiramatsu }
2740fa28244dSMasami Hiramatsu 
27416bb536ccSWang Nan static int find_probe_functions(struct map *map, char *name,
27426bb536ccSWang Nan 				struct symbol **syms)
2743eb948e50SMasami Hiramatsu {
2744564c62a4SNamhyung Kim 	int found = 0;
27450a3873a8SArnaldo Carvalho de Melo 	struct symbol *sym;
27464c859351SMasami Hiramatsu 	struct rb_node *tmp;
2747564c62a4SNamhyung Kim 
2748be39db9fSArnaldo Carvalho de Melo 	if (map__load(map) < 0)
274975e4a2a6SWang Nan 		return 0;
275075e4a2a6SWang Nan 
27514c859351SMasami Hiramatsu 	map__for_each_symbol(map, sym, tmp) {
27526bb536ccSWang Nan 		if (strglobmatch(sym->name, name)) {
2753564c62a4SNamhyung Kim 			found++;
27546bb536ccSWang Nan 			if (syms && found < probe_conf.max_probes)
27556bb536ccSWang Nan 				syms[found - 1] = sym;
27566bb536ccSWang Nan 		}
2757eb948e50SMasami Hiramatsu 	}
2758564c62a4SNamhyung Kim 
2759564c62a4SNamhyung Kim 	return found;
2760eb948e50SMasami Hiramatsu }
2761eb948e50SMasami Hiramatsu 
27627b6ff0bdSNaveen N. Rao void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
27637b6ff0bdSNaveen N. Rao 				struct probe_trace_event *tev __maybe_unused,
27640b3c2264SNaveen N. Rao 				struct map *map __maybe_unused,
27650b3c2264SNaveen N. Rao 				struct symbol *sym __maybe_unused) { }
27667b6ff0bdSNaveen N. Rao 
2767eb948e50SMasami Hiramatsu /*
2768eb948e50SMasami Hiramatsu  * Find probe function addresses from map.
2769eb948e50SMasami Hiramatsu  * Return an error or the number of found probe_trace_event
2770eb948e50SMasami Hiramatsu  */
2771eb948e50SMasami Hiramatsu static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2772ddb2f58fSMasami Hiramatsu 					    struct probe_trace_event **tevs)
2773eb948e50SMasami Hiramatsu {
2774eb948e50SMasami Hiramatsu 	struct map *map = NULL;
2775eb948e50SMasami Hiramatsu 	struct ref_reloc_sym *reloc_sym = NULL;
2776eb948e50SMasami Hiramatsu 	struct symbol *sym;
27776bb536ccSWang Nan 	struct symbol **syms = NULL;
2778eb948e50SMasami Hiramatsu 	struct probe_trace_event *tev;
2779eb948e50SMasami Hiramatsu 	struct perf_probe_point *pp = &pev->point;
2780eb948e50SMasami Hiramatsu 	struct probe_trace_point *tp;
2781564c62a4SNamhyung Kim 	int num_matched_functions;
2782b031220dSMasami Hiramatsu 	int ret, i, j, skipped = 0;
2783c61fb959SRavi Bangoria 	char *mod_name;
2784eb948e50SMasami Hiramatsu 
278544225521SMasami Hiramatsu 	map = get_target_map(pev->target, pev->uprobes);
2786eb948e50SMasami Hiramatsu 	if (!map) {
2787eb948e50SMasami Hiramatsu 		ret = -EINVAL;
2788eb948e50SMasami Hiramatsu 		goto out;
2789eb948e50SMasami Hiramatsu 	}
2790eb948e50SMasami Hiramatsu 
27916bb536ccSWang Nan 	syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
27926bb536ccSWang Nan 	if (!syms) {
27936bb536ccSWang Nan 		ret = -ENOMEM;
27946bb536ccSWang Nan 		goto out;
27956bb536ccSWang Nan 	}
27966bb536ccSWang Nan 
2797eb948e50SMasami Hiramatsu 	/*
2798eb948e50SMasami Hiramatsu 	 * Load matched symbols: Since the different local symbols may have
2799eb948e50SMasami Hiramatsu 	 * same name but different addresses, this lists all the symbols.
2800eb948e50SMasami Hiramatsu 	 */
28016bb536ccSWang Nan 	num_matched_functions = find_probe_functions(map, pp->function, syms);
2802564c62a4SNamhyung Kim 	if (num_matched_functions == 0) {
2803eb948e50SMasami Hiramatsu 		pr_err("Failed to find symbol %s in %s\n", pp->function,
280444225521SMasami Hiramatsu 			pev->target ? : "kernel");
2805eb948e50SMasami Hiramatsu 		ret = -ENOENT;
2806eb948e50SMasami Hiramatsu 		goto out;
2807ddb2f58fSMasami Hiramatsu 	} else if (num_matched_functions > probe_conf.max_probes) {
2808eb948e50SMasami Hiramatsu 		pr_err("Too many functions matched in %s\n",
280944225521SMasami Hiramatsu 			pev->target ? : "kernel");
2810eb948e50SMasami Hiramatsu 		ret = -E2BIG;
2811eb948e50SMasami Hiramatsu 		goto out;
2812eb948e50SMasami Hiramatsu 	}
2813eb948e50SMasami Hiramatsu 
28141a8ac29cSMasami Hiramatsu 	/* Note that the symbols in the kmodule are not relocated */
28151a8ac29cSMasami Hiramatsu 	if (!pev->uprobes && !pp->retprobe && !pev->target) {
28160560a0c4SHe Kuang 		reloc_sym = kernel_get_ref_reloc_sym();
2817eb948e50SMasami Hiramatsu 		if (!reloc_sym) {
2818eb948e50SMasami Hiramatsu 			pr_warning("Relocated base symbol is not found!\n");
2819eb948e50SMasami Hiramatsu 			ret = -EINVAL;
2820eb948e50SMasami Hiramatsu 			goto out;
2821eb948e50SMasami Hiramatsu 		}
2822eb948e50SMasami Hiramatsu 	}
2823eb948e50SMasami Hiramatsu 
2824eb948e50SMasami Hiramatsu 	/* Setup result trace-probe-events */
2825eb948e50SMasami Hiramatsu 	*tevs = zalloc(sizeof(*tev) * num_matched_functions);
2826eb948e50SMasami Hiramatsu 	if (!*tevs) {
2827eb948e50SMasami Hiramatsu 		ret = -ENOMEM;
2828eb948e50SMasami Hiramatsu 		goto out;
2829eb948e50SMasami Hiramatsu 	}
2830eb948e50SMasami Hiramatsu 
2831eb948e50SMasami Hiramatsu 	ret = 0;
2832564c62a4SNamhyung Kim 
28336bb536ccSWang Nan 	for (j = 0; j < num_matched_functions; j++) {
28346bb536ccSWang Nan 		sym = syms[j];
28356bb536ccSWang Nan 
2836eb948e50SMasami Hiramatsu 		tev = (*tevs) + ret;
2837eb948e50SMasami Hiramatsu 		tp = &tev->point;
2838eb948e50SMasami Hiramatsu 		if (ret == num_matched_functions) {
2839eb948e50SMasami Hiramatsu 			pr_warning("Too many symbols are listed. Skip it.\n");
2840eb948e50SMasami Hiramatsu 			break;
2841eb948e50SMasami Hiramatsu 		}
2842eb948e50SMasami Hiramatsu 		ret++;
2843eb948e50SMasami Hiramatsu 
2844eb948e50SMasami Hiramatsu 		if (pp->offset > sym->end - sym->start) {
2845eb948e50SMasami Hiramatsu 			pr_warning("Offset %ld is bigger than the size of %s\n",
2846eb948e50SMasami Hiramatsu 				   pp->offset, sym->name);
2847eb948e50SMasami Hiramatsu 			ret = -ENOENT;
2848eb948e50SMasami Hiramatsu 			goto err_out;
2849eb948e50SMasami Hiramatsu 		}
2850eb948e50SMasami Hiramatsu 		/* Add one probe point */
2851eb948e50SMasami Hiramatsu 		tp->address = map->unmap_ip(map, sym->start) + pp->offset;
28521a8ac29cSMasami Hiramatsu 
28531a8ac29cSMasami Hiramatsu 		/* Check the kprobe (not in module) is within .text  */
28541a8ac29cSMasami Hiramatsu 		if (!pev->uprobes && !pev->target &&
2855b031220dSMasami Hiramatsu 		    kprobe_warn_out_range(sym->name, tp->address)) {
2856b031220dSMasami Hiramatsu 			tp->symbol = NULL;	/* Skip it */
2857b031220dSMasami Hiramatsu 			skipped++;
2858b031220dSMasami Hiramatsu 		} else if (reloc_sym) {
2859eb948e50SMasami Hiramatsu 			tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2860eb948e50SMasami Hiramatsu 			tp->offset = tp->address - reloc_sym->addr;
2861eb948e50SMasami Hiramatsu 		} else {
2862eb948e50SMasami Hiramatsu 			tp->symbol = strdup_or_goto(sym->name, nomem_out);
2863eb948e50SMasami Hiramatsu 			tp->offset = pp->offset;
2864eb948e50SMasami Hiramatsu 		}
28656bb536ccSWang Nan 		tp->realname = strdup_or_goto(sym->name, nomem_out);
28666bb536ccSWang Nan 
2867eb948e50SMasami Hiramatsu 		tp->retprobe = pp->retprobe;
2868c61fb959SRavi Bangoria 		if (pev->target) {
2869c61fb959SRavi Bangoria 			if (pev->uprobes) {
287044225521SMasami Hiramatsu 				tev->point.module = strdup_or_goto(pev->target,
287144225521SMasami Hiramatsu 								   nomem_out);
2872c61fb959SRavi Bangoria 			} else {
2873c61fb959SRavi Bangoria 				mod_name = find_module_name(pev->target);
2874c61fb959SRavi Bangoria 				tev->point.module =
2875c61fb959SRavi Bangoria 					strdup(mod_name ? mod_name : pev->target);
2876c61fb959SRavi Bangoria 				free(mod_name);
2877c61fb959SRavi Bangoria 				if (!tev->point.module)
2878c61fb959SRavi Bangoria 					goto nomem_out;
2879c61fb959SRavi Bangoria 			}
2880c61fb959SRavi Bangoria 		}
2881eb948e50SMasami Hiramatsu 		tev->uprobes = pev->uprobes;
2882eb948e50SMasami Hiramatsu 		tev->nargs = pev->nargs;
2883eb948e50SMasami Hiramatsu 		if (tev->nargs) {
2884eb948e50SMasami Hiramatsu 			tev->args = zalloc(sizeof(struct probe_trace_arg) *
2885eb948e50SMasami Hiramatsu 					   tev->nargs);
2886eb948e50SMasami Hiramatsu 			if (tev->args == NULL)
2887eb948e50SMasami Hiramatsu 				goto nomem_out;
2888eb948e50SMasami Hiramatsu 		}
2889eb948e50SMasami Hiramatsu 		for (i = 0; i < tev->nargs; i++) {
2890eb948e50SMasami Hiramatsu 			if (pev->args[i].name)
2891eb948e50SMasami Hiramatsu 				tev->args[i].name =
2892eb948e50SMasami Hiramatsu 					strdup_or_goto(pev->args[i].name,
2893eb948e50SMasami Hiramatsu 							nomem_out);
2894eb948e50SMasami Hiramatsu 
2895eb948e50SMasami Hiramatsu 			tev->args[i].value = strdup_or_goto(pev->args[i].var,
2896eb948e50SMasami Hiramatsu 							    nomem_out);
2897eb948e50SMasami Hiramatsu 			if (pev->args[i].type)
2898eb948e50SMasami Hiramatsu 				tev->args[i].type =
2899eb948e50SMasami Hiramatsu 					strdup_or_goto(pev->args[i].type,
2900eb948e50SMasami Hiramatsu 							nomem_out);
2901eb948e50SMasami Hiramatsu 		}
29020b3c2264SNaveen N. Rao 		arch__fix_tev_from_maps(pev, tev, map, sym);
2903eb948e50SMasami Hiramatsu 	}
2904b031220dSMasami Hiramatsu 	if (ret == skipped) {
2905b031220dSMasami Hiramatsu 		ret = -ENOENT;
2906b031220dSMasami Hiramatsu 		goto err_out;
2907b031220dSMasami Hiramatsu 	}
2908eb948e50SMasami Hiramatsu 
2909eb948e50SMasami Hiramatsu out:
2910eebc509bSMasami Hiramatsu 	map__put(map);
29116bb536ccSWang Nan 	free(syms);
2912eb948e50SMasami Hiramatsu 	return ret;
2913eb948e50SMasami Hiramatsu 
2914eb948e50SMasami Hiramatsu nomem_out:
2915eb948e50SMasami Hiramatsu 	ret = -ENOMEM;
2916eb948e50SMasami Hiramatsu err_out:
2917eb948e50SMasami Hiramatsu 	clear_probe_trace_events(*tevs, num_matched_functions);
2918eb948e50SMasami Hiramatsu 	zfree(tevs);
2919eb948e50SMasami Hiramatsu 	goto out;
2920eb948e50SMasami Hiramatsu }
2921eb948e50SMasami Hiramatsu 
2922da15bd9dSWang Nan static int try_to_find_absolute_address(struct perf_probe_event *pev,
2923da15bd9dSWang Nan 					struct probe_trace_event **tevs)
2924da15bd9dSWang Nan {
2925da15bd9dSWang Nan 	struct perf_probe_point *pp = &pev->point;
2926da15bd9dSWang Nan 	struct probe_trace_event *tev;
2927da15bd9dSWang Nan 	struct probe_trace_point *tp;
2928da15bd9dSWang Nan 	int i, err;
2929da15bd9dSWang Nan 
2930da15bd9dSWang Nan 	if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2931da15bd9dSWang Nan 		return -EINVAL;
2932da15bd9dSWang Nan 	if (perf_probe_event_need_dwarf(pev))
2933da15bd9dSWang Nan 		return -EINVAL;
2934da15bd9dSWang Nan 
2935da15bd9dSWang Nan 	/*
2936da15bd9dSWang Nan 	 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2937da15bd9dSWang Nan 	 * absolute address.
2938da15bd9dSWang Nan 	 *
2939da15bd9dSWang Nan 	 * Only one tev can be generated by this.
2940da15bd9dSWang Nan 	 */
2941da15bd9dSWang Nan 	*tevs = zalloc(sizeof(*tev));
2942da15bd9dSWang Nan 	if (!*tevs)
2943da15bd9dSWang Nan 		return -ENOMEM;
2944da15bd9dSWang Nan 
2945da15bd9dSWang Nan 	tev = *tevs;
2946da15bd9dSWang Nan 	tp = &tev->point;
2947da15bd9dSWang Nan 
2948da15bd9dSWang Nan 	/*
2949da15bd9dSWang Nan 	 * Don't use tp->offset, use address directly, because
2950da15bd9dSWang Nan 	 * in synthesize_probe_trace_command() address cannot be
2951da15bd9dSWang Nan 	 * zero.
2952da15bd9dSWang Nan 	 */
2953da15bd9dSWang Nan 	tp->address = pev->point.abs_address;
2954da15bd9dSWang Nan 	tp->retprobe = pp->retprobe;
2955da15bd9dSWang Nan 	tev->uprobes = pev->uprobes;
2956da15bd9dSWang Nan 
2957da15bd9dSWang Nan 	err = -ENOMEM;
2958da15bd9dSWang Nan 	/*
2959da15bd9dSWang Nan 	 * Give it a '0x' leading symbol name.
2960da15bd9dSWang Nan 	 * In __add_probe_trace_events, a NULL symbol is interpreted as
2961da15bd9dSWang Nan 	 * invalud.
2962da15bd9dSWang Nan 	 */
2963da15bd9dSWang Nan 	if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2964da15bd9dSWang Nan 		goto errout;
2965da15bd9dSWang Nan 
2966da15bd9dSWang Nan 	/* For kprobe, check range */
2967da15bd9dSWang Nan 	if ((!tev->uprobes) &&
2968da15bd9dSWang Nan 	    (kprobe_warn_out_range(tev->point.symbol,
2969da15bd9dSWang Nan 				   tev->point.address))) {
2970da15bd9dSWang Nan 		err = -EACCES;
2971da15bd9dSWang Nan 		goto errout;
2972da15bd9dSWang Nan 	}
2973da15bd9dSWang Nan 
2974da15bd9dSWang Nan 	if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2975da15bd9dSWang Nan 		goto errout;
2976da15bd9dSWang Nan 
2977da15bd9dSWang Nan 	if (pev->target) {
2978da15bd9dSWang Nan 		tp->module = strdup(pev->target);
2979da15bd9dSWang Nan 		if (!tp->module)
2980da15bd9dSWang Nan 			goto errout;
2981da15bd9dSWang Nan 	}
2982da15bd9dSWang Nan 
2983da15bd9dSWang Nan 	if (tev->group) {
2984da15bd9dSWang Nan 		tev->group = strdup(pev->group);
2985da15bd9dSWang Nan 		if (!tev->group)
2986da15bd9dSWang Nan 			goto errout;
2987da15bd9dSWang Nan 	}
2988da15bd9dSWang Nan 
2989da15bd9dSWang Nan 	if (pev->event) {
2990da15bd9dSWang Nan 		tev->event = strdup(pev->event);
2991da15bd9dSWang Nan 		if (!tev->event)
2992da15bd9dSWang Nan 			goto errout;
2993da15bd9dSWang Nan 	}
2994da15bd9dSWang Nan 
2995da15bd9dSWang Nan 	tev->nargs = pev->nargs;
2996da15bd9dSWang Nan 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2997*d1d0e29cSMarkus Elfring 	if (!tev->args)
2998da15bd9dSWang Nan 		goto errout;
2999*d1d0e29cSMarkus Elfring 
3000da15bd9dSWang Nan 	for (i = 0; i < tev->nargs; i++)
3001da15bd9dSWang Nan 		copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3002da15bd9dSWang Nan 
3003da15bd9dSWang Nan 	return 1;
3004da15bd9dSWang Nan 
3005da15bd9dSWang Nan errout:
3006da15bd9dSWang Nan 	clear_probe_trace_events(*tevs, 1);
3007da15bd9dSWang Nan 	*tevs = NULL;
3008da15bd9dSWang Nan 	return err;
3009da15bd9dSWang Nan }
3010da15bd9dSWang Nan 
301142bba263SMasami Hiramatsu /* Concatinate two arrays */
301242bba263SMasami Hiramatsu static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
301342bba263SMasami Hiramatsu {
301442bba263SMasami Hiramatsu 	void *ret;
301542bba263SMasami Hiramatsu 
301642bba263SMasami Hiramatsu 	ret = malloc(sz_a + sz_b);
301742bba263SMasami Hiramatsu 	if (ret) {
301842bba263SMasami Hiramatsu 		memcpy(ret, a, sz_a);
301942bba263SMasami Hiramatsu 		memcpy(ret + sz_a, b, sz_b);
302042bba263SMasami Hiramatsu 	}
302142bba263SMasami Hiramatsu 	return ret;
302242bba263SMasami Hiramatsu }
302342bba263SMasami Hiramatsu 
302442bba263SMasami Hiramatsu static int
302542bba263SMasami Hiramatsu concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
302642bba263SMasami Hiramatsu 			  struct probe_trace_event **tevs2, int ntevs2)
302742bba263SMasami Hiramatsu {
302842bba263SMasami Hiramatsu 	struct probe_trace_event *new_tevs;
302942bba263SMasami Hiramatsu 	int ret = 0;
303042bba263SMasami Hiramatsu 
303142bba263SMasami Hiramatsu 	if (ntevs == 0) {
303242bba263SMasami Hiramatsu 		*tevs = *tevs2;
303342bba263SMasami Hiramatsu 		*ntevs = ntevs2;
303442bba263SMasami Hiramatsu 		*tevs2 = NULL;
303542bba263SMasami Hiramatsu 		return 0;
303642bba263SMasami Hiramatsu 	}
303742bba263SMasami Hiramatsu 
303842bba263SMasami Hiramatsu 	if (*ntevs + ntevs2 > probe_conf.max_probes)
303942bba263SMasami Hiramatsu 		ret = -E2BIG;
304042bba263SMasami Hiramatsu 	else {
304142bba263SMasami Hiramatsu 		/* Concatinate the array of probe_trace_event */
304242bba263SMasami Hiramatsu 		new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
304342bba263SMasami Hiramatsu 				  *tevs2, ntevs2 * sizeof(**tevs2));
304442bba263SMasami Hiramatsu 		if (!new_tevs)
304542bba263SMasami Hiramatsu 			ret = -ENOMEM;
304642bba263SMasami Hiramatsu 		else {
304742bba263SMasami Hiramatsu 			free(*tevs);
304842bba263SMasami Hiramatsu 			*tevs = new_tevs;
304942bba263SMasami Hiramatsu 			*ntevs += ntevs2;
305042bba263SMasami Hiramatsu 		}
305142bba263SMasami Hiramatsu 	}
305242bba263SMasami Hiramatsu 	if (ret < 0)
305342bba263SMasami Hiramatsu 		clear_probe_trace_events(*tevs2, ntevs2);
305442bba263SMasami Hiramatsu 	zfree(tevs2);
305542bba263SMasami Hiramatsu 
305642bba263SMasami Hiramatsu 	return ret;
305742bba263SMasami Hiramatsu }
305842bba263SMasami Hiramatsu 
305942bba263SMasami Hiramatsu /*
306042bba263SMasami Hiramatsu  * Try to find probe_trace_event from given probe caches. Return the number
306142bba263SMasami Hiramatsu  * of cached events found, if an error occurs return the error.
306242bba263SMasami Hiramatsu  */
306342bba263SMasami Hiramatsu static int find_cached_events(struct perf_probe_event *pev,
306442bba263SMasami Hiramatsu 			      struct probe_trace_event **tevs,
306542bba263SMasami Hiramatsu 			      const char *target)
306642bba263SMasami Hiramatsu {
306742bba263SMasami Hiramatsu 	struct probe_cache *cache;
306842bba263SMasami Hiramatsu 	struct probe_cache_entry *entry;
306942bba263SMasami Hiramatsu 	struct probe_trace_event *tmp_tevs = NULL;
307042bba263SMasami Hiramatsu 	int ntevs = 0;
307142bba263SMasami Hiramatsu 	int ret = 0;
307242bba263SMasami Hiramatsu 
307342bba263SMasami Hiramatsu 	cache = probe_cache__new(target);
307442bba263SMasami Hiramatsu 	/* Return 0 ("not found") if the target has no probe cache. */
307542bba263SMasami Hiramatsu 	if (!cache)
307642bba263SMasami Hiramatsu 		return 0;
307742bba263SMasami Hiramatsu 
307842bba263SMasami Hiramatsu 	for_each_probe_cache_entry(entry, cache) {
307942bba263SMasami Hiramatsu 		/* Skip the cache entry which has no name */
308042bba263SMasami Hiramatsu 		if (!entry->pev.event || !entry->pev.group)
308142bba263SMasami Hiramatsu 			continue;
308242bba263SMasami Hiramatsu 		if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
308342bba263SMasami Hiramatsu 		    strglobmatch(entry->pev.event, pev->event)) {
308442bba263SMasami Hiramatsu 			ret = probe_cache_entry__get_event(entry, &tmp_tevs);
308542bba263SMasami Hiramatsu 			if (ret > 0)
308642bba263SMasami Hiramatsu 				ret = concat_probe_trace_events(tevs, &ntevs,
308742bba263SMasami Hiramatsu 								&tmp_tevs, ret);
308842bba263SMasami Hiramatsu 			if (ret < 0)
308942bba263SMasami Hiramatsu 				break;
309042bba263SMasami Hiramatsu 		}
309142bba263SMasami Hiramatsu 	}
309242bba263SMasami Hiramatsu 	probe_cache__delete(cache);
309342bba263SMasami Hiramatsu 	if (ret < 0) {
309442bba263SMasami Hiramatsu 		clear_probe_trace_events(*tevs, ntevs);
309542bba263SMasami Hiramatsu 		zfree(tevs);
309642bba263SMasami Hiramatsu 	} else {
309742bba263SMasami Hiramatsu 		ret = ntevs;
309842bba263SMasami Hiramatsu 		if (ntevs > 0 && target && target[0] == '/')
309942bba263SMasami Hiramatsu 			pev->uprobes = true;
310042bba263SMasami Hiramatsu 	}
310142bba263SMasami Hiramatsu 
310242bba263SMasami Hiramatsu 	return ret;
310342bba263SMasami Hiramatsu }
310442bba263SMasami Hiramatsu 
31051de7b8bfSMasami Hiramatsu /* Try to find probe_trace_event from all probe caches */
31061de7b8bfSMasami Hiramatsu static int find_cached_events_all(struct perf_probe_event *pev,
31071de7b8bfSMasami Hiramatsu 				   struct probe_trace_event **tevs)
31081de7b8bfSMasami Hiramatsu {
31091de7b8bfSMasami Hiramatsu 	struct probe_trace_event *tmp_tevs = NULL;
31101de7b8bfSMasami Hiramatsu 	struct strlist *bidlist;
31111de7b8bfSMasami Hiramatsu 	struct str_node *nd;
31121de7b8bfSMasami Hiramatsu 	char *pathname;
31131de7b8bfSMasami Hiramatsu 	int ntevs = 0;
31141de7b8bfSMasami Hiramatsu 	int ret;
31151de7b8bfSMasami Hiramatsu 
31161de7b8bfSMasami Hiramatsu 	/* Get the buildid list of all valid caches */
31171de7b8bfSMasami Hiramatsu 	bidlist = build_id_cache__list_all(true);
31181de7b8bfSMasami Hiramatsu 	if (!bidlist) {
31191de7b8bfSMasami Hiramatsu 		ret = -errno;
31201de7b8bfSMasami Hiramatsu 		pr_debug("Failed to get buildids: %d\n", ret);
31211de7b8bfSMasami Hiramatsu 		return ret;
31221de7b8bfSMasami Hiramatsu 	}
31231de7b8bfSMasami Hiramatsu 
31241de7b8bfSMasami Hiramatsu 	ret = 0;
31251de7b8bfSMasami Hiramatsu 	strlist__for_each_entry(nd, bidlist) {
31261de7b8bfSMasami Hiramatsu 		pathname = build_id_cache__origname(nd->s);
31271de7b8bfSMasami Hiramatsu 		ret = find_cached_events(pev, &tmp_tevs, pathname);
31281de7b8bfSMasami Hiramatsu 		/* In the case of cnt == 0, we just skip it */
31291de7b8bfSMasami Hiramatsu 		if (ret > 0)
31301de7b8bfSMasami Hiramatsu 			ret = concat_probe_trace_events(tevs, &ntevs,
31311de7b8bfSMasami Hiramatsu 							&tmp_tevs, ret);
31321de7b8bfSMasami Hiramatsu 		free(pathname);
31331de7b8bfSMasami Hiramatsu 		if (ret < 0)
31341de7b8bfSMasami Hiramatsu 			break;
31351de7b8bfSMasami Hiramatsu 	}
31361de7b8bfSMasami Hiramatsu 	strlist__delete(bidlist);
31371de7b8bfSMasami Hiramatsu 
31381de7b8bfSMasami Hiramatsu 	if (ret < 0) {
31391de7b8bfSMasami Hiramatsu 		clear_probe_trace_events(*tevs, ntevs);
31401de7b8bfSMasami Hiramatsu 		zfree(tevs);
31411de7b8bfSMasami Hiramatsu 	} else
31421de7b8bfSMasami Hiramatsu 		ret = ntevs;
31431de7b8bfSMasami Hiramatsu 
31441de7b8bfSMasami Hiramatsu 	return ret;
31451de7b8bfSMasami Hiramatsu }
31461de7b8bfSMasami Hiramatsu 
3147bc062230SMasami Hiramatsu static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3148bc062230SMasami Hiramatsu 					      struct probe_trace_event **tevs)
3149bc062230SMasami Hiramatsu {
3150bc062230SMasami Hiramatsu 	struct probe_cache *cache;
3151bc062230SMasami Hiramatsu 	struct probe_cache_entry *entry;
3152bc062230SMasami Hiramatsu 	struct probe_trace_event *tev;
3153bc062230SMasami Hiramatsu 	struct str_node *node;
3154bc062230SMasami Hiramatsu 	int ret, i;
3155bc062230SMasami Hiramatsu 
31561de7b8bfSMasami Hiramatsu 	if (pev->sdt) {
315742bba263SMasami Hiramatsu 		/* For SDT/cached events, we use special search functions */
31581de7b8bfSMasami Hiramatsu 		if (!pev->target)
31591de7b8bfSMasami Hiramatsu 			return find_cached_events_all(pev, tevs);
31601de7b8bfSMasami Hiramatsu 		else
316142bba263SMasami Hiramatsu 			return find_cached_events(pev, tevs, pev->target);
31621de7b8bfSMasami Hiramatsu 	}
3163bc062230SMasami Hiramatsu 	cache = probe_cache__new(pev->target);
3164bc062230SMasami Hiramatsu 	if (!cache)
3165bc062230SMasami Hiramatsu 		return 0;
3166bc062230SMasami Hiramatsu 
3167bc062230SMasami Hiramatsu 	entry = probe_cache__find(cache, pev);
3168bc062230SMasami Hiramatsu 	if (!entry) {
316936a009feSMasami Hiramatsu 		/* SDT must be in the cache */
317036a009feSMasami Hiramatsu 		ret = pev->sdt ? -ENOENT : 0;
3171bc062230SMasami Hiramatsu 		goto out;
3172bc062230SMasami Hiramatsu 	}
3173bc062230SMasami Hiramatsu 
3174bc062230SMasami Hiramatsu 	ret = strlist__nr_entries(entry->tevlist);
3175bc062230SMasami Hiramatsu 	if (ret > probe_conf.max_probes) {
3176bc062230SMasami Hiramatsu 		pr_debug("Too many entries matched in the cache of %s\n",
3177bc062230SMasami Hiramatsu 			 pev->target ? : "kernel");
3178bc062230SMasami Hiramatsu 		ret = -E2BIG;
3179bc062230SMasami Hiramatsu 		goto out;
3180bc062230SMasami Hiramatsu 	}
3181bc062230SMasami Hiramatsu 
3182bc062230SMasami Hiramatsu 	*tevs = zalloc(ret * sizeof(*tev));
3183bc062230SMasami Hiramatsu 	if (!*tevs) {
3184bc062230SMasami Hiramatsu 		ret = -ENOMEM;
3185bc062230SMasami Hiramatsu 		goto out;
3186bc062230SMasami Hiramatsu 	}
3187bc062230SMasami Hiramatsu 
3188bc062230SMasami Hiramatsu 	i = 0;
3189bc062230SMasami Hiramatsu 	strlist__for_each_entry(node, entry->tevlist) {
3190bc062230SMasami Hiramatsu 		tev = &(*tevs)[i++];
3191bc062230SMasami Hiramatsu 		ret = parse_probe_trace_command(node->s, tev);
3192bc062230SMasami Hiramatsu 		if (ret < 0)
3193bc062230SMasami Hiramatsu 			goto out;
3194bc062230SMasami Hiramatsu 		/* Set the uprobes attribute as same as original */
3195bc062230SMasami Hiramatsu 		tev->uprobes = pev->uprobes;
3196bc062230SMasami Hiramatsu 	}
3197bc062230SMasami Hiramatsu 	ret = i;
3198bc062230SMasami Hiramatsu 
3199bc062230SMasami Hiramatsu out:
3200bc062230SMasami Hiramatsu 	probe_cache__delete(cache);
3201bc062230SMasami Hiramatsu 	return ret;
3202bc062230SMasami Hiramatsu }
3203bc062230SMasami Hiramatsu 
32040e60836bSSrikar Dronamraju static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3205ddb2f58fSMasami Hiramatsu 					 struct probe_trace_event **tevs)
3206e0faa8d3SMasami Hiramatsu {
3207eb948e50SMasami Hiramatsu 	int ret;
32084235b045SMasami Hiramatsu 
320936a009feSMasami Hiramatsu 	if (!pev->group && !pev->sdt) {
32102a12ec13SMasami Hiramatsu 		/* Set group name if not given */
32112a12ec13SMasami Hiramatsu 		if (!pev->uprobes) {
32122a12ec13SMasami Hiramatsu 			pev->group = strdup(PERFPROBE_GROUP);
32132a12ec13SMasami Hiramatsu 			ret = pev->group ? 0 : -ENOMEM;
32142a12ec13SMasami Hiramatsu 		} else
321544225521SMasami Hiramatsu 			ret = convert_exec_to_group(pev->target, &pev->group);
3216fb7345bbSMasami Hiramatsu 		if (ret != 0) {
3217fb7345bbSMasami Hiramatsu 			pr_warning("Failed to make a group name.\n");
3218fb7345bbSMasami Hiramatsu 			return ret;
3219fb7345bbSMasami Hiramatsu 		}
3220fb7345bbSMasami Hiramatsu 	}
3221fb7345bbSMasami Hiramatsu 
3222da15bd9dSWang Nan 	ret = try_to_find_absolute_address(pev, tevs);
3223da15bd9dSWang Nan 	if (ret > 0)
3224da15bd9dSWang Nan 		return ret;
3225da15bd9dSWang Nan 
3226bc062230SMasami Hiramatsu 	/* At first, we need to lookup cache entry */
3227bc062230SMasami Hiramatsu 	ret = find_probe_trace_events_from_cache(pev, tevs);
322836a009feSMasami Hiramatsu 	if (ret > 0 || pev->sdt)	/* SDT can be found only in the cache */
322936a009feSMasami Hiramatsu 		return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3230bc062230SMasami Hiramatsu 
32314b4da7f7SMasami Hiramatsu 	/* Convert perf_probe_event with debuginfo */
3232ddb2f58fSMasami Hiramatsu 	ret = try_to_find_probe_trace_events(pev, tevs);
3233e334016fSMasami Hiramatsu 	if (ret != 0)
3234190b57fcSMasami Hiramatsu 		return ret;	/* Found in debuginfo or got an error */
3235e0faa8d3SMasami Hiramatsu 
3236ddb2f58fSMasami Hiramatsu 	return find_probe_trace_events_from_map(pev, tevs);
32374235b045SMasami Hiramatsu }
32384235b045SMasami Hiramatsu 
323912fae5efSWang Nan int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
32404235b045SMasami Hiramatsu {
3241844dffa5SNamhyung Kim 	int i, ret;
32424235b045SMasami Hiramatsu 
32434235b045SMasami Hiramatsu 	/* Loop 1: convert all events */
32444235b045SMasami Hiramatsu 	for (i = 0; i < npevs; i++) {
3245b031220dSMasami Hiramatsu 		/* Init kprobe blacklist if needed */
324612fae5efSWang Nan 		if (!pevs[i].uprobes)
3247b031220dSMasami Hiramatsu 			kprobe_blacklist__init();
32484235b045SMasami Hiramatsu 		/* Convert with or without debuginfo */
324912fae5efSWang Nan 		ret  = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3250146a1439SMasami Hiramatsu 		if (ret < 0)
3251844dffa5SNamhyung Kim 			return ret;
325212fae5efSWang Nan 		pevs[i].ntevs = ret;
32534235b045SMasami Hiramatsu 	}
3254b031220dSMasami Hiramatsu 	/* This just release blacklist only if allocated */
3255b031220dSMasami Hiramatsu 	kprobe_blacklist__release();
32564235b045SMasami Hiramatsu 
3257844dffa5SNamhyung Kim 	return 0;
3258844dffa5SNamhyung Kim }
3259844dffa5SNamhyung Kim 
32601c20b1d1SMasami Hiramatsu static int show_probe_trace_event(struct probe_trace_event *tev)
32611c20b1d1SMasami Hiramatsu {
32621c20b1d1SMasami Hiramatsu 	char *buf = synthesize_probe_trace_command(tev);
32631c20b1d1SMasami Hiramatsu 
32641c20b1d1SMasami Hiramatsu 	if (!buf) {
32651c20b1d1SMasami Hiramatsu 		pr_debug("Failed to synthesize probe trace event.\n");
32661c20b1d1SMasami Hiramatsu 		return -EINVAL;
32671c20b1d1SMasami Hiramatsu 	}
32681c20b1d1SMasami Hiramatsu 
32691c20b1d1SMasami Hiramatsu 	/* Showing definition always go stdout */
32701c20b1d1SMasami Hiramatsu 	printf("%s\n", buf);
32711c20b1d1SMasami Hiramatsu 	free(buf);
32721c20b1d1SMasami Hiramatsu 
32731c20b1d1SMasami Hiramatsu 	return 0;
32741c20b1d1SMasami Hiramatsu }
32751c20b1d1SMasami Hiramatsu 
32761c20b1d1SMasami Hiramatsu int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
32771c20b1d1SMasami Hiramatsu {
32781c20b1d1SMasami Hiramatsu 	struct strlist *namelist = strlist__new(NULL, NULL);
32791c20b1d1SMasami Hiramatsu 	struct probe_trace_event *tev;
32801c20b1d1SMasami Hiramatsu 	struct perf_probe_event *pev;
32811c20b1d1SMasami Hiramatsu 	int i, j, ret = 0;
32821c20b1d1SMasami Hiramatsu 
32831c20b1d1SMasami Hiramatsu 	if (!namelist)
32841c20b1d1SMasami Hiramatsu 		return -ENOMEM;
32851c20b1d1SMasami Hiramatsu 
32861c20b1d1SMasami Hiramatsu 	for (j = 0; j < npevs && !ret; j++) {
32871c20b1d1SMasami Hiramatsu 		pev = &pevs[j];
32881c20b1d1SMasami Hiramatsu 		for (i = 0; i < pev->ntevs && !ret; i++) {
32891c20b1d1SMasami Hiramatsu 			tev = &pev->tevs[i];
32901c20b1d1SMasami Hiramatsu 			/* Skip if the symbol is out of .text or blacklisted */
32911c20b1d1SMasami Hiramatsu 			if (!tev->point.symbol && !pev->uprobes)
32921c20b1d1SMasami Hiramatsu 				continue;
32931c20b1d1SMasami Hiramatsu 
32941c20b1d1SMasami Hiramatsu 			/* Set new name for tev (and update namelist) */
32951c20b1d1SMasami Hiramatsu 			ret = probe_trace_event__set_name(tev, pev,
32961c20b1d1SMasami Hiramatsu 							  namelist, true);
32971c20b1d1SMasami Hiramatsu 			if (!ret)
32981c20b1d1SMasami Hiramatsu 				ret = show_probe_trace_event(tev);
32991c20b1d1SMasami Hiramatsu 		}
33001c20b1d1SMasami Hiramatsu 	}
33011c20b1d1SMasami Hiramatsu 	strlist__delete(namelist);
33021c20b1d1SMasami Hiramatsu 
33031c20b1d1SMasami Hiramatsu 	return ret;
33041c20b1d1SMasami Hiramatsu }
33051c20b1d1SMasami Hiramatsu 
330612fae5efSWang Nan int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3307844dffa5SNamhyung Kim {
3308844dffa5SNamhyung Kim 	int i, ret = 0;
3309844dffa5SNamhyung Kim 
33104235b045SMasami Hiramatsu 	/* Loop 2: add all events */
33118635bf6eSArnaldo Carvalho de Melo 	for (i = 0; i < npevs; i++) {
331212fae5efSWang Nan 		ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
331312fae5efSWang Nan 					       pevs[i].ntevs,
3314ddb2f58fSMasami Hiramatsu 					       probe_conf.force_add);
3315fbee632dSArnaldo Carvalho de Melo 		if (ret < 0)
3316fbee632dSArnaldo Carvalho de Melo 			break;
3317fbee632dSArnaldo Carvalho de Melo 	}
3318844dffa5SNamhyung Kim 	return ret;
3319844dffa5SNamhyung Kim }
3320844dffa5SNamhyung Kim 
332112fae5efSWang Nan void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3322844dffa5SNamhyung Kim {
3323844dffa5SNamhyung Kim 	int i, j;
3324844dffa5SNamhyung Kim 
3325449e5b24SMasami Hiramatsu 	/* Loop 3: cleanup and free trace events  */
3326449e5b24SMasami Hiramatsu 	for (i = 0; i < npevs; i++) {
332712fae5efSWang Nan 		for (j = 0; j < pevs[i].ntevs; j++)
332812fae5efSWang Nan 			clear_probe_trace_event(&pevs[i].tevs[j]);
332912fae5efSWang Nan 		zfree(&pevs[i].tevs);
333012fae5efSWang Nan 		pevs[i].ntevs = 0;
3331a43aac29SNamhyung Kim 		clear_perf_probe_event(&pevs[i]);
3332449e5b24SMasami Hiramatsu 	}
3333844dffa5SNamhyung Kim }
3334844dffa5SNamhyung Kim 
3335844dffa5SNamhyung Kim int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3336844dffa5SNamhyung Kim {
3337844dffa5SNamhyung Kim 	int ret;
3338844dffa5SNamhyung Kim 
33399bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(pevs->uprobes);
33409bae1e8cSNamhyung Kim 	if (ret < 0)
33419bae1e8cSNamhyung Kim 		return ret;
33429bae1e8cSNamhyung Kim 
334312fae5efSWang Nan 	ret = convert_perf_probe_events(pevs, npevs);
3344844dffa5SNamhyung Kim 	if (ret == 0)
334512fae5efSWang Nan 		ret = apply_perf_probe_events(pevs, npevs);
3346844dffa5SNamhyung Kim 
334712fae5efSWang Nan 	cleanup_perf_probe_events(pevs, npevs);
3348146a1439SMasami Hiramatsu 
33499bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
3350146a1439SMasami Hiramatsu 	return ret;
3351e0faa8d3SMasami Hiramatsu }
3352e0faa8d3SMasami Hiramatsu 
3353307a464bSMasami Hiramatsu int del_perf_probe_events(struct strfilter *filter)
3354fa28244dSMasami Hiramatsu {
3355307a464bSMasami Hiramatsu 	int ret, ret2, ufd = -1, kfd = -1;
3356307a464bSMasami Hiramatsu 	char *str = strfilter__string(filter);
3357307a464bSMasami Hiramatsu 
3358307a464bSMasami Hiramatsu 	if (!str)
3359307a464bSMasami Hiramatsu 		return -EINVAL;
3360307a464bSMasami Hiramatsu 
3361fa28244dSMasami Hiramatsu 	/* Get current event names */
336292f6c72eSMasami Hiramatsu 	ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
336392f6c72eSMasami Hiramatsu 	if (ret < 0)
336492f6c72eSMasami Hiramatsu 		goto out;
3365225466f1SSrikar Dronamraju 
336692f6c72eSMasami Hiramatsu 	ret = probe_file__del_events(kfd, filter);
3367307a464bSMasami Hiramatsu 	if (ret < 0 && ret != -ENOENT)
3368225466f1SSrikar Dronamraju 		goto error;
3369fa28244dSMasami Hiramatsu 
337092f6c72eSMasami Hiramatsu 	ret2 = probe_file__del_events(ufd, filter);
3371dddc7ee3SMasami Hiramatsu 	if (ret2 < 0 && ret2 != -ENOENT) {
33726dbe31f7SMasami Hiramatsu 		ret = ret2;
3373dddc7ee3SMasami Hiramatsu 		goto error;
3374dddc7ee3SMasami Hiramatsu 	}
3375307a464bSMasami Hiramatsu 	ret = 0;
3376225466f1SSrikar Dronamraju 
3377225466f1SSrikar Dronamraju error:
337892f6c72eSMasami Hiramatsu 	if (kfd >= 0)
3379225466f1SSrikar Dronamraju 		close(kfd);
338092f6c72eSMasami Hiramatsu 	if (ufd >= 0)
3381225466f1SSrikar Dronamraju 		close(ufd);
338292f6c72eSMasami Hiramatsu out:
3383307a464bSMasami Hiramatsu 	free(str);
3384146a1439SMasami Hiramatsu 
3385146a1439SMasami Hiramatsu 	return ret;
3386fa28244dSMasami Hiramatsu }
3387225466f1SSrikar Dronamraju 
33882df58634SMasami Hiramatsu int show_available_funcs(const char *target, struct strfilter *_filter,
33892df58634SMasami Hiramatsu 					bool user)
3390e80711caSMasami Hiramatsu {
3391fd227598SArnaldo Carvalho de Melo         struct rb_node *nd;
33922df58634SMasami Hiramatsu 	struct map *map;
33932df58634SMasami Hiramatsu 	int ret;
33942df58634SMasami Hiramatsu 
33959bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(user);
33962df58634SMasami Hiramatsu 	if (ret < 0)
33972df58634SMasami Hiramatsu 		return ret;
33982df58634SMasami Hiramatsu 
33992df58634SMasami Hiramatsu 	/* Get a symbol map */
3400eebc509bSMasami Hiramatsu 	map = get_target_map(target, user);
34012df58634SMasami Hiramatsu 	if (!map) {
34022df58634SMasami Hiramatsu 		pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3403e80711caSMasami Hiramatsu 		return -EINVAL;
3404e80711caSMasami Hiramatsu 	}
34052df58634SMasami Hiramatsu 
3406be39db9fSArnaldo Carvalho de Melo 	ret = map__load(map);
3407e7049342SMasami Hiramatsu 	if (ret) {
3408e7049342SMasami Hiramatsu 		if (ret == -2) {
3409e7049342SMasami Hiramatsu 			char *str = strfilter__string(_filter);
3410e7049342SMasami Hiramatsu 			pr_err("Failed to find symbols matched to \"%s\"\n",
3411e7049342SMasami Hiramatsu 			       str);
3412e7049342SMasami Hiramatsu 			free(str);
3413e7049342SMasami Hiramatsu 		} else
3414e7049342SMasami Hiramatsu 			pr_err("Failed to load symbols in %s\n",
3415e7049342SMasami Hiramatsu 			       (target) ? : "kernel");
34162df58634SMasami Hiramatsu 		goto end;
34172df58634SMasami Hiramatsu 	}
3418e80711caSMasami Hiramatsu 	if (!dso__sorted_by_name(map->dso, map->type))
3419e80711caSMasami Hiramatsu 		dso__sort_by_name(map->dso, map->type);
3420e80711caSMasami Hiramatsu 
34212df58634SMasami Hiramatsu 	/* Show all (filtered) symbols */
34222df58634SMasami Hiramatsu 	setup_pager();
3423fd227598SArnaldo Carvalho de Melo 
3424fd227598SArnaldo Carvalho de Melo         for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) {
3425fd227598SArnaldo Carvalho de Melo 		struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3426fd227598SArnaldo Carvalho de Melo 
3427fd227598SArnaldo Carvalho de Melo 		if (strfilter__compare(_filter, pos->sym.name))
3428fd227598SArnaldo Carvalho de Melo 			printf("%s\n", pos->sym.name);
3429fd227598SArnaldo Carvalho de Melo         }
3430fd227598SArnaldo Carvalho de Melo 
34312df58634SMasami Hiramatsu end:
343284c2cafaSArnaldo Carvalho de Melo 	map__put(map);
34339bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
3434225466f1SSrikar Dronamraju 
34352df58634SMasami Hiramatsu 	return ret;
3436225466f1SSrikar Dronamraju }
3437225466f1SSrikar Dronamraju 
3438da15bd9dSWang Nan int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3439da15bd9dSWang Nan 			    struct perf_probe_arg *pvar)
3440da15bd9dSWang Nan {
3441da15bd9dSWang Nan 	tvar->value = strdup(pvar->var);
3442da15bd9dSWang Nan 	if (tvar->value == NULL)
3443da15bd9dSWang Nan 		return -ENOMEM;
3444da15bd9dSWang Nan 	if (pvar->type) {
3445da15bd9dSWang Nan 		tvar->type = strdup(pvar->type);
3446da15bd9dSWang Nan 		if (tvar->type == NULL)
3447da15bd9dSWang Nan 			return -ENOMEM;
3448da15bd9dSWang Nan 	}
3449da15bd9dSWang Nan 	if (pvar->name) {
3450da15bd9dSWang Nan 		tvar->name = strdup(pvar->name);
3451da15bd9dSWang Nan 		if (tvar->name == NULL)
3452da15bd9dSWang Nan 			return -ENOMEM;
3453da15bd9dSWang Nan 	} else
3454da15bd9dSWang Nan 		tvar->name = NULL;
3455da15bd9dSWang Nan 	return 0;
3456da15bd9dSWang Nan }
3457