xref: /linux/tools/perf/util/probe-event.c (revision 8e34189b347d76acf48ce05831176582201b664d)
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 {
113ee45b6c2SMasami Hiramatsu 	return machine__find_kernel_function_by_name(host_machine, name, mapp,
114469b9b88SMasami Hiramatsu 						     NULL);
115e0faa8d3SMasami Hiramatsu }
116469b9b88SMasami Hiramatsu 
1178f33f7deSMasami Hiramatsu static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
1188f33f7deSMasami Hiramatsu {
1198f33f7deSMasami Hiramatsu 	return machine__find_kernel_function(host_machine, addr, mapp, NULL);
1208f33f7deSMasami Hiramatsu }
1218f33f7deSMasami Hiramatsu 
1228f33f7deSMasami Hiramatsu static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
1238f33f7deSMasami Hiramatsu {
1248f33f7deSMasami Hiramatsu 	/* kmap->ref_reloc_sym should be set if host_machine is initialized */
1258f33f7deSMasami Hiramatsu 	struct kmap *kmap;
126a5e813c6SArnaldo Carvalho de Melo 	struct map *map = machine__kernel_map(host_machine);
1278f33f7deSMasami Hiramatsu 
12877e65977SArnaldo Carvalho de Melo 	if (map__load(map, NULL) < 0)
1298f33f7deSMasami Hiramatsu 		return NULL;
1308f33f7deSMasami Hiramatsu 
13177e65977SArnaldo Carvalho de Melo 	kmap = map__kmap(map);
132ba92732eSWang Nan 	if (!kmap)
133ba92732eSWang Nan 		return NULL;
1348f33f7deSMasami Hiramatsu 	return kmap->ref_reloc_sym;
1358f33f7deSMasami Hiramatsu }
1368f33f7deSMasami Hiramatsu 
1379b239a12SMasami Hiramatsu static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
1389b239a12SMasami Hiramatsu 					     bool reloc, bool reladdr)
1398f33f7deSMasami Hiramatsu {
1408f33f7deSMasami Hiramatsu 	struct ref_reloc_sym *reloc_sym;
1418f33f7deSMasami Hiramatsu 	struct symbol *sym;
1428f33f7deSMasami Hiramatsu 	struct map *map;
1438f33f7deSMasami Hiramatsu 
1448f33f7deSMasami Hiramatsu 	/* ref_reloc_sym is just a label. Need a special fix*/
1458f33f7deSMasami Hiramatsu 	reloc_sym = kernel_get_ref_reloc_sym();
1468f33f7deSMasami Hiramatsu 	if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
1479b239a12SMasami Hiramatsu 		*addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
1488f33f7deSMasami Hiramatsu 	else {
1498f33f7deSMasami Hiramatsu 		sym = __find_kernel_function_by_name(name, &map);
1509b239a12SMasami Hiramatsu 		if (!sym)
1519b239a12SMasami Hiramatsu 			return -ENOENT;
1529b239a12SMasami Hiramatsu 		*addr = map->unmap_ip(map, sym->start) -
1539b239a12SMasami Hiramatsu 			((reloc) ? 0 : map->reloc) -
1549b239a12SMasami Hiramatsu 			((reladdr) ? map->start : 0);
1558f33f7deSMasami Hiramatsu 	}
1568f33f7deSMasami Hiramatsu 	return 0;
1578f33f7deSMasami Hiramatsu }
1588f33f7deSMasami Hiramatsu 
159e80711caSMasami Hiramatsu static struct map *kernel_get_module_map(const char *module)
160e80711caSMasami Hiramatsu {
161ee45b6c2SMasami Hiramatsu 	struct map_groups *grp = &host_machine->kmaps;
1621eee78aeSArnaldo Carvalho de Melo 	struct maps *maps = &grp->maps[MAP__FUNCTION];
1634bb7123dSArnaldo Carvalho de Melo 	struct map *pos;
164e80711caSMasami Hiramatsu 
16514a8fd7cSMasami Hiramatsu 	/* A file path -- this is an offline module */
16614a8fd7cSMasami Hiramatsu 	if (module && strchr(module, '/'))
1679f2de315SArnaldo Carvalho de Melo 		return machine__findnew_module_map(host_machine, 0, module);
16814a8fd7cSMasami Hiramatsu 
169e80711caSMasami Hiramatsu 	if (!module)
170e80711caSMasami Hiramatsu 		module = "kernel";
171e80711caSMasami Hiramatsu 
1724bb7123dSArnaldo Carvalho de Melo 	for (pos = maps__first(maps); pos; pos = map__next(pos)) {
173e80711caSMasami Hiramatsu 		if (strncmp(pos->dso->short_name + 1, module,
174e80711caSMasami Hiramatsu 			    pos->dso->short_name_len - 2) == 0) {
175e80711caSMasami Hiramatsu 			return pos;
176e80711caSMasami Hiramatsu 		}
177e80711caSMasami Hiramatsu 	}
178e80711caSMasami Hiramatsu 	return NULL;
179e80711caSMasami Hiramatsu }
180e80711caSMasami Hiramatsu 
1819b118acaSMasami Hiramatsu static struct map *get_target_map(const char *target, bool user)
1829b118acaSMasami Hiramatsu {
1839b118acaSMasami Hiramatsu 	/* Init maps of given executable or kernel */
1849b118acaSMasami Hiramatsu 	if (user)
1859b118acaSMasami Hiramatsu 		return dso__new_map(target);
1869b118acaSMasami Hiramatsu 	else
1879b118acaSMasami Hiramatsu 		return kernel_get_module_map(target);
1889b118acaSMasami Hiramatsu }
1899b118acaSMasami Hiramatsu 
1909b118acaSMasami Hiramatsu static void put_target_map(struct map *map, bool user)
1919b118acaSMasami Hiramatsu {
1929b118acaSMasami Hiramatsu 	if (map && user) {
1939b118acaSMasami Hiramatsu 		/* Only the user map needs to be released */
19484c2cafaSArnaldo Carvalho de Melo 		map__put(map);
1959b118acaSMasami Hiramatsu 	}
1969b118acaSMasami Hiramatsu }
1979b118acaSMasami Hiramatsu 
1989b118acaSMasami Hiramatsu 
199fb7345bbSMasami Hiramatsu static int convert_exec_to_group(const char *exec, char **result)
200fb7345bbSMasami Hiramatsu {
201fb7345bbSMasami Hiramatsu 	char *ptr1, *ptr2, *exec_copy;
202fb7345bbSMasami Hiramatsu 	char buf[64];
203fb7345bbSMasami Hiramatsu 	int ret;
204fb7345bbSMasami Hiramatsu 
205fb7345bbSMasami Hiramatsu 	exec_copy = strdup(exec);
206fb7345bbSMasami Hiramatsu 	if (!exec_copy)
207fb7345bbSMasami Hiramatsu 		return -ENOMEM;
208fb7345bbSMasami Hiramatsu 
209fb7345bbSMasami Hiramatsu 	ptr1 = basename(exec_copy);
210fb7345bbSMasami Hiramatsu 	if (!ptr1) {
211fb7345bbSMasami Hiramatsu 		ret = -EINVAL;
212fb7345bbSMasami Hiramatsu 		goto out;
213fb7345bbSMasami Hiramatsu 	}
214fb7345bbSMasami Hiramatsu 
215fb7345bbSMasami Hiramatsu 	ptr2 = strpbrk(ptr1, "-._");
216fb7345bbSMasami Hiramatsu 	if (ptr2)
217fb7345bbSMasami Hiramatsu 		*ptr2 = '\0';
218fb7345bbSMasami Hiramatsu 	ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
219fb7345bbSMasami Hiramatsu 	if (ret < 0)
220fb7345bbSMasami Hiramatsu 		goto out;
221fb7345bbSMasami Hiramatsu 
222fb7345bbSMasami Hiramatsu 	*result = strdup(buf);
223fb7345bbSMasami Hiramatsu 	ret = *result ? 0 : -ENOMEM;
224fb7345bbSMasami Hiramatsu 
225fb7345bbSMasami Hiramatsu out:
226fb7345bbSMasami Hiramatsu 	free(exec_copy);
227fb7345bbSMasami Hiramatsu 	return ret;
228fb7345bbSMasami Hiramatsu }
229fb7345bbSMasami Hiramatsu 
2309b118acaSMasami Hiramatsu static void clear_perf_probe_point(struct perf_probe_point *pp)
2319b118acaSMasami Hiramatsu {
2329b118acaSMasami Hiramatsu 	free(pp->file);
2339b118acaSMasami Hiramatsu 	free(pp->function);
2349b118acaSMasami Hiramatsu 	free(pp->lazy_line);
2359b118acaSMasami Hiramatsu }
2369b118acaSMasami Hiramatsu 
237eb948e50SMasami Hiramatsu static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
238eb948e50SMasami Hiramatsu {
239eb948e50SMasami Hiramatsu 	int i;
240eb948e50SMasami Hiramatsu 
241eb948e50SMasami Hiramatsu 	for (i = 0; i < ntevs; i++)
242eb948e50SMasami Hiramatsu 		clear_probe_trace_event(tevs + i);
243eb948e50SMasami Hiramatsu }
244eb948e50SMasami Hiramatsu 
245b031220dSMasami Hiramatsu static bool kprobe_blacklist__listed(unsigned long address);
246b031220dSMasami Hiramatsu static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
247b031220dSMasami Hiramatsu {
2489b239a12SMasami Hiramatsu 	u64 etext_addr = 0;
2499b239a12SMasami Hiramatsu 	int ret;
2507c31bb8cSHe Kuang 
251b031220dSMasami Hiramatsu 	/* Get the address of _etext for checking non-probable text symbol */
2529b239a12SMasami Hiramatsu 	ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
2539b239a12SMasami Hiramatsu 						false, false);
2547c31bb8cSHe Kuang 
2559b239a12SMasami Hiramatsu 	if (ret == 0 && etext_addr < address)
256b031220dSMasami Hiramatsu 		pr_warning("%s is out of .text, skip it.\n", symbol);
257b031220dSMasami Hiramatsu 	else if (kprobe_blacklist__listed(address))
258b031220dSMasami Hiramatsu 		pr_warning("%s is blacklisted function, skip it.\n", symbol);
259b031220dSMasami Hiramatsu 	else
260b031220dSMasami Hiramatsu 		return false;
261b031220dSMasami Hiramatsu 
262b031220dSMasami Hiramatsu 	return true;
263b031220dSMasami Hiramatsu }
264b031220dSMasami Hiramatsu 
265c61fb959SRavi Bangoria /*
266c61fb959SRavi Bangoria  * NOTE:
267c61fb959SRavi Bangoria  * '.gnu.linkonce.this_module' section of kernel module elf directly
268c61fb959SRavi Bangoria  * maps to 'struct module' from linux/module.h. This section contains
269c61fb959SRavi Bangoria  * actual module name which will be used by kernel after loading it.
270c61fb959SRavi Bangoria  * But, we cannot use 'struct module' here since linux/module.h is not
271c61fb959SRavi Bangoria  * exposed to user-space. Offset of 'name' has remained same from long
272c61fb959SRavi Bangoria  * time, so hardcoding it here.
273c61fb959SRavi Bangoria  */
274c61fb959SRavi Bangoria #ifdef __LP64__
275c61fb959SRavi Bangoria #define MOD_NAME_OFFSET 24
276c61fb959SRavi Bangoria #else
277c61fb959SRavi Bangoria #define MOD_NAME_OFFSET 12
278c61fb959SRavi Bangoria #endif
279c61fb959SRavi Bangoria 
280c61fb959SRavi Bangoria /*
281c61fb959SRavi Bangoria  * @module can be module name of module file path. In case of path,
282c61fb959SRavi Bangoria  * inspect elf and find out what is actual module name.
283c61fb959SRavi Bangoria  * Caller has to free mod_name after using it.
284c61fb959SRavi Bangoria  */
285c61fb959SRavi Bangoria static char *find_module_name(const char *module)
286c61fb959SRavi Bangoria {
287c61fb959SRavi Bangoria 	int fd;
288c61fb959SRavi Bangoria 	Elf *elf;
289c61fb959SRavi Bangoria 	GElf_Ehdr ehdr;
290c61fb959SRavi Bangoria 	GElf_Shdr shdr;
291c61fb959SRavi Bangoria 	Elf_Data *data;
292c61fb959SRavi Bangoria 	Elf_Scn *sec;
293c61fb959SRavi Bangoria 	char *mod_name = NULL;
294c61fb959SRavi Bangoria 
295c61fb959SRavi Bangoria 	fd = open(module, O_RDONLY);
296c61fb959SRavi Bangoria 	if (fd < 0)
297c61fb959SRavi Bangoria 		return NULL;
298c61fb959SRavi Bangoria 
299c61fb959SRavi Bangoria 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
300c61fb959SRavi Bangoria 	if (elf == NULL)
301c61fb959SRavi Bangoria 		goto elf_err;
302c61fb959SRavi Bangoria 
303c61fb959SRavi Bangoria 	if (gelf_getehdr(elf, &ehdr) == NULL)
304c61fb959SRavi Bangoria 		goto ret_err;
305c61fb959SRavi Bangoria 
306c61fb959SRavi Bangoria 	sec = elf_section_by_name(elf, &ehdr, &shdr,
307c61fb959SRavi Bangoria 			".gnu.linkonce.this_module", NULL);
308c61fb959SRavi Bangoria 	if (!sec)
309c61fb959SRavi Bangoria 		goto ret_err;
310c61fb959SRavi Bangoria 
311c61fb959SRavi Bangoria 	data = elf_getdata(sec, NULL);
312c61fb959SRavi Bangoria 	if (!data || !data->d_buf)
313c61fb959SRavi Bangoria 		goto ret_err;
314c61fb959SRavi Bangoria 
315c61fb959SRavi Bangoria 	mod_name = strdup((char *)data->d_buf + MOD_NAME_OFFSET);
316c61fb959SRavi Bangoria 
317c61fb959SRavi Bangoria ret_err:
318c61fb959SRavi Bangoria 	elf_end(elf);
319c61fb959SRavi Bangoria elf_err:
320c61fb959SRavi Bangoria 	close(fd);
321c61fb959SRavi Bangoria 	return mod_name;
322c61fb959SRavi Bangoria }
323c61fb959SRavi Bangoria 
32489fe808aSIngo Molnar #ifdef HAVE_DWARF_SUPPORT
32560fb7742SWang Nan 
32660fb7742SWang Nan static int kernel_get_module_dso(const char *module, struct dso **pdso)
32760fb7742SWang Nan {
32860fb7742SWang Nan 	struct dso *dso;
32960fb7742SWang Nan 	struct map *map;
33060fb7742SWang Nan 	const char *vmlinux_name;
33160fb7742SWang Nan 	int ret = 0;
33260fb7742SWang Nan 
33360fb7742SWang Nan 	if (module) {
334266fa2b2SArnaldo Carvalho de Melo 		char module_name[128];
335266fa2b2SArnaldo Carvalho de Melo 
336266fa2b2SArnaldo Carvalho de Melo 		snprintf(module_name, sizeof(module_name), "[%s]", module);
337266fa2b2SArnaldo Carvalho de Melo 		map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
338266fa2b2SArnaldo Carvalho de Melo 		if (map) {
339266fa2b2SArnaldo Carvalho de Melo 			dso = map->dso;
34060fb7742SWang Nan 			goto found;
34160fb7742SWang Nan 		}
34260fb7742SWang Nan 		pr_debug("Failed to find module %s.\n", module);
34360fb7742SWang Nan 		return -ENOENT;
34460fb7742SWang Nan 	}
34560fb7742SWang Nan 
346a5e813c6SArnaldo Carvalho de Melo 	map = machine__kernel_map(host_machine);
34760fb7742SWang Nan 	dso = map->dso;
34860fb7742SWang Nan 
34960fb7742SWang Nan 	vmlinux_name = symbol_conf.vmlinux_name;
35060fb7742SWang Nan 	dso->load_errno = 0;
35160fb7742SWang Nan 	if (vmlinux_name)
35260fb7742SWang Nan 		ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
35360fb7742SWang Nan 	else
35460fb7742SWang Nan 		ret = dso__load_vmlinux_path(dso, map, NULL);
35560fb7742SWang Nan found:
35660fb7742SWang Nan 	*pdso = dso;
35760fb7742SWang Nan 	return ret;
35860fb7742SWang Nan }
35960fb7742SWang Nan 
3609b118acaSMasami Hiramatsu /*
3619b118acaSMasami Hiramatsu  * Some binaries like glibc have special symbols which are on the symbol
3629b118acaSMasami Hiramatsu  * table, but not in the debuginfo. If we can find the address of the
3639b118acaSMasami Hiramatsu  * symbol from map, we can translate the address back to the probe point.
3649b118acaSMasami Hiramatsu  */
3659b118acaSMasami Hiramatsu static int find_alternative_probe_point(struct debuginfo *dinfo,
3669b118acaSMasami Hiramatsu 					struct perf_probe_point *pp,
3679b118acaSMasami Hiramatsu 					struct perf_probe_point *result,
3689b118acaSMasami Hiramatsu 					const char *target, bool uprobes)
3699b118acaSMasami Hiramatsu {
3709b118acaSMasami Hiramatsu 	struct map *map = NULL;
3719b118acaSMasami Hiramatsu 	struct symbol *sym;
3729b118acaSMasami Hiramatsu 	u64 address = 0;
3739b118acaSMasami Hiramatsu 	int ret = -ENOENT;
3749b118acaSMasami Hiramatsu 
3759b118acaSMasami Hiramatsu 	/* This can work only for function-name based one */
3769b118acaSMasami Hiramatsu 	if (!pp->function || pp->file)
3779b118acaSMasami Hiramatsu 		return -ENOTSUP;
3789b118acaSMasami Hiramatsu 
3799b118acaSMasami Hiramatsu 	map = get_target_map(target, uprobes);
3809b118acaSMasami Hiramatsu 	if (!map)
3819b118acaSMasami Hiramatsu 		return -EINVAL;
3829b118acaSMasami Hiramatsu 
3839b118acaSMasami Hiramatsu 	/* Find the address of given function */
3849b118acaSMasami Hiramatsu 	map__for_each_symbol_by_name(map, pp->function, sym) {
385e6d7c91cSMasami Hiramatsu 		if (uprobes)
3869b118acaSMasami Hiramatsu 			address = sym->start;
387e6d7c91cSMasami Hiramatsu 		else
388*8e34189bSMasami Hiramatsu 			address = map->unmap_ip(map, sym->start) - map->reloc;
3899b118acaSMasami Hiramatsu 		break;
3909b118acaSMasami Hiramatsu 	}
3919b118acaSMasami Hiramatsu 	if (!address) {
3929b118acaSMasami Hiramatsu 		ret = -ENOENT;
3939b118acaSMasami Hiramatsu 		goto out;
3949b118acaSMasami Hiramatsu 	}
395f6c15621SWang Nan 	pr_debug("Symbol %s address found : %" PRIx64 "\n",
396f6c15621SWang Nan 			pp->function, address);
3979b118acaSMasami Hiramatsu 
3989b118acaSMasami Hiramatsu 	ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
3999b118acaSMasami Hiramatsu 					  result);
4009b118acaSMasami Hiramatsu 	if (ret <= 0)
4019b118acaSMasami Hiramatsu 		ret = (!ret) ? -ENOENT : ret;
4029b118acaSMasami Hiramatsu 	else {
4039b118acaSMasami Hiramatsu 		result->offset += pp->offset;
4049b118acaSMasami Hiramatsu 		result->line += pp->line;
4059d7b45c5SHe Kuang 		result->retprobe = pp->retprobe;
4069b118acaSMasami Hiramatsu 		ret = 0;
4079b118acaSMasami Hiramatsu 	}
4089b118acaSMasami Hiramatsu 
4099b118acaSMasami Hiramatsu out:
4109b118acaSMasami Hiramatsu 	put_target_map(map, uprobes);
4119b118acaSMasami Hiramatsu 	return ret;
4129b118acaSMasami Hiramatsu 
4139b118acaSMasami Hiramatsu }
4149b118acaSMasami Hiramatsu 
4159b118acaSMasami Hiramatsu static int get_alternative_probe_event(struct debuginfo *dinfo,
4169b118acaSMasami Hiramatsu 				       struct perf_probe_event *pev,
41744225521SMasami Hiramatsu 				       struct perf_probe_point *tmp)
4189b118acaSMasami Hiramatsu {
4199b118acaSMasami Hiramatsu 	int ret;
4209b118acaSMasami Hiramatsu 
4219b118acaSMasami Hiramatsu 	memcpy(tmp, &pev->point, sizeof(*tmp));
4229b118acaSMasami Hiramatsu 	memset(&pev->point, 0, sizeof(pev->point));
4239b118acaSMasami Hiramatsu 	ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
42444225521SMasami Hiramatsu 					   pev->target, pev->uprobes);
4259b118acaSMasami Hiramatsu 	if (ret < 0)
4269b118acaSMasami Hiramatsu 		memcpy(&pev->point, tmp, sizeof(*tmp));
4279b118acaSMasami Hiramatsu 
4289b118acaSMasami Hiramatsu 	return ret;
4299b118acaSMasami Hiramatsu }
430a15ad2f5SMasami Hiramatsu 
431811dd2aeSMasami Hiramatsu static int get_alternative_line_range(struct debuginfo *dinfo,
432811dd2aeSMasami Hiramatsu 				      struct line_range *lr,
433811dd2aeSMasami Hiramatsu 				      const char *target, bool user)
434811dd2aeSMasami Hiramatsu {
4356d4a4896SDavid Ahern 	struct perf_probe_point pp = { .function = lr->function,
4366d4a4896SDavid Ahern 				       .file = lr->file,
4376d4a4896SDavid Ahern 				       .line = lr->start };
4386d4a4896SDavid Ahern 	struct perf_probe_point result;
439811dd2aeSMasami Hiramatsu 	int ret, len = 0;
440811dd2aeSMasami Hiramatsu 
4416d4a4896SDavid Ahern 	memset(&result, 0, sizeof(result));
4426d4a4896SDavid Ahern 
443811dd2aeSMasami Hiramatsu 	if (lr->end != INT_MAX)
444811dd2aeSMasami Hiramatsu 		len = lr->end - lr->start;
445811dd2aeSMasami Hiramatsu 	ret = find_alternative_probe_point(dinfo, &pp, &result,
446811dd2aeSMasami Hiramatsu 					   target, user);
447811dd2aeSMasami Hiramatsu 	if (!ret) {
448811dd2aeSMasami Hiramatsu 		lr->function = result.function;
449811dd2aeSMasami Hiramatsu 		lr->file = result.file;
450811dd2aeSMasami Hiramatsu 		lr->start = result.line;
451811dd2aeSMasami Hiramatsu 		if (lr->end != INT_MAX)
452811dd2aeSMasami Hiramatsu 			lr->end = lr->start + len;
453811dd2aeSMasami Hiramatsu 		clear_perf_probe_point(&pp);
454811dd2aeSMasami Hiramatsu 	}
455811dd2aeSMasami Hiramatsu 	return ret;
456811dd2aeSMasami Hiramatsu }
457811dd2aeSMasami Hiramatsu 
458ff741783SMasami Hiramatsu /* Open new debuginfo of given module */
45992561cb7SMasami Hiramatsu static struct debuginfo *open_debuginfo(const char *module, bool silent)
460469b9b88SMasami Hiramatsu {
461a15ad2f5SMasami Hiramatsu 	const char *path = module;
462419e8738SMasami Hiramatsu 	char reason[STRERR_BUFSIZE];
463419e8738SMasami Hiramatsu 	struct debuginfo *ret = NULL;
464419e8738SMasami Hiramatsu 	struct dso *dso = NULL;
465419e8738SMasami Hiramatsu 	int err;
46614a8fd7cSMasami Hiramatsu 
467a15ad2f5SMasami Hiramatsu 	if (!module || !strchr(module, '/')) {
468419e8738SMasami Hiramatsu 		err = kernel_get_module_dso(module, &dso);
469419e8738SMasami Hiramatsu 		if (err < 0) {
470419e8738SMasami Hiramatsu 			if (!dso || dso->load_errno == 0) {
471c8b5f2c9SArnaldo Carvalho de Melo 				if (!str_error_r(-err, reason, STRERR_BUFSIZE))
472419e8738SMasami Hiramatsu 					strcpy(reason, "(unknown)");
473419e8738SMasami Hiramatsu 			} else
474419e8738SMasami Hiramatsu 				dso__strerror_load(dso, reason, STRERR_BUFSIZE);
47592561cb7SMasami Hiramatsu 			if (!silent)
476419e8738SMasami Hiramatsu 				pr_err("Failed to find the path for %s: %s\n",
477419e8738SMasami Hiramatsu 					module ?: "kernel", reason);
478ff741783SMasami Hiramatsu 			return NULL;
479469b9b88SMasami Hiramatsu 		}
480419e8738SMasami Hiramatsu 		path = dso->long_name;
48114a8fd7cSMasami Hiramatsu 	}
48292561cb7SMasami Hiramatsu 	ret = debuginfo__new(path);
48392561cb7SMasami Hiramatsu 	if (!ret && !silent) {
48492561cb7SMasami Hiramatsu 		pr_warning("The %s file has no debug information.\n", path);
48592561cb7SMasami Hiramatsu 		if (!module || !strtailcmp(path, ".ko"))
48692561cb7SMasami Hiramatsu 			pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
48792561cb7SMasami Hiramatsu 		else
48892561cb7SMasami Hiramatsu 			pr_warning("Rebuild with -g, ");
48992561cb7SMasami Hiramatsu 		pr_warning("or install an appropriate debuginfo package.\n");
490e0faa8d3SMasami Hiramatsu 	}
49192561cb7SMasami Hiramatsu 	return ret;
49292561cb7SMasami Hiramatsu }
49392561cb7SMasami Hiramatsu 
4947737af01SMasami Hiramatsu /* For caching the last debuginfo */
4957737af01SMasami Hiramatsu static struct debuginfo *debuginfo_cache;
4967737af01SMasami Hiramatsu static char *debuginfo_cache_path;
4977737af01SMasami Hiramatsu 
4987737af01SMasami Hiramatsu static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
4997737af01SMasami Hiramatsu {
50020f49859SMasami Hiramatsu 	const char *path = module;
50120f49859SMasami Hiramatsu 
50220f49859SMasami Hiramatsu 	/* If the module is NULL, it should be the kernel. */
50320f49859SMasami Hiramatsu 	if (!module)
50420f49859SMasami Hiramatsu 		path = "kernel";
50520f49859SMasami Hiramatsu 
50620f49859SMasami Hiramatsu 	if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
5077737af01SMasami Hiramatsu 		goto out;
5087737af01SMasami Hiramatsu 
5097737af01SMasami Hiramatsu 	/* Copy module path */
5107737af01SMasami Hiramatsu 	free(debuginfo_cache_path);
51120f49859SMasami Hiramatsu 	debuginfo_cache_path = strdup(path);
5127737af01SMasami Hiramatsu 	if (!debuginfo_cache_path) {
5137737af01SMasami Hiramatsu 		debuginfo__delete(debuginfo_cache);
5147737af01SMasami Hiramatsu 		debuginfo_cache = NULL;
5157737af01SMasami Hiramatsu 		goto out;
5167737af01SMasami Hiramatsu 	}
5177737af01SMasami Hiramatsu 
5187737af01SMasami Hiramatsu 	debuginfo_cache = open_debuginfo(module, silent);
5197737af01SMasami Hiramatsu 	if (!debuginfo_cache)
5207737af01SMasami Hiramatsu 		zfree(&debuginfo_cache_path);
5217737af01SMasami Hiramatsu out:
5227737af01SMasami Hiramatsu 	return debuginfo_cache;
5237737af01SMasami Hiramatsu }
5247737af01SMasami Hiramatsu 
5257737af01SMasami Hiramatsu static void debuginfo_cache__exit(void)
5267737af01SMasami Hiramatsu {
5277737af01SMasami Hiramatsu 	debuginfo__delete(debuginfo_cache);
5287737af01SMasami Hiramatsu 	debuginfo_cache = NULL;
5297737af01SMasami Hiramatsu 	zfree(&debuginfo_cache_path);
5307737af01SMasami Hiramatsu }
5317737af01SMasami Hiramatsu 
5324b4da7f7SMasami Hiramatsu 
53399ca4233SMasami Hiramatsu static int get_text_start_address(const char *exec, unsigned long *address)
53499ca4233SMasami Hiramatsu {
53599ca4233SMasami Hiramatsu 	Elf *elf;
53699ca4233SMasami Hiramatsu 	GElf_Ehdr ehdr;
53799ca4233SMasami Hiramatsu 	GElf_Shdr shdr;
53899ca4233SMasami Hiramatsu 	int fd, ret = -ENOENT;
53999ca4233SMasami Hiramatsu 
54099ca4233SMasami Hiramatsu 	fd = open(exec, O_RDONLY);
54199ca4233SMasami Hiramatsu 	if (fd < 0)
54299ca4233SMasami Hiramatsu 		return -errno;
54399ca4233SMasami Hiramatsu 
54499ca4233SMasami Hiramatsu 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
545062d6c2aSMasami Hiramatsu 	if (elf == NULL) {
546062d6c2aSMasami Hiramatsu 		ret = -EINVAL;
547062d6c2aSMasami Hiramatsu 		goto out_close;
548062d6c2aSMasami Hiramatsu 	}
54999ca4233SMasami Hiramatsu 
55099ca4233SMasami Hiramatsu 	if (gelf_getehdr(elf, &ehdr) == NULL)
55199ca4233SMasami Hiramatsu 		goto out;
55299ca4233SMasami Hiramatsu 
55399ca4233SMasami Hiramatsu 	if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
55499ca4233SMasami Hiramatsu 		goto out;
55599ca4233SMasami Hiramatsu 
55699ca4233SMasami Hiramatsu 	*address = shdr.sh_addr - shdr.sh_offset;
55799ca4233SMasami Hiramatsu 	ret = 0;
55899ca4233SMasami Hiramatsu out:
55999ca4233SMasami Hiramatsu 	elf_end(elf);
560062d6c2aSMasami Hiramatsu out_close:
561062d6c2aSMasami Hiramatsu 	close(fd);
562062d6c2aSMasami Hiramatsu 
56399ca4233SMasami Hiramatsu 	return ret;
56499ca4233SMasami Hiramatsu }
56599ca4233SMasami Hiramatsu 
5665a6f6314SMasami Hiramatsu /*
5675a6f6314SMasami Hiramatsu  * Convert trace point to probe point with debuginfo
5685a6f6314SMasami Hiramatsu  */
5695a6f6314SMasami Hiramatsu static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
5705a6f6314SMasami Hiramatsu 					    struct perf_probe_point *pp,
5715a6f6314SMasami Hiramatsu 					    bool is_kprobe)
5725a6f6314SMasami Hiramatsu {
5735a6f6314SMasami Hiramatsu 	struct debuginfo *dinfo = NULL;
5745a6f6314SMasami Hiramatsu 	unsigned long stext = 0;
5755a6f6314SMasami Hiramatsu 	u64 addr = tp->address;
5765a6f6314SMasami Hiramatsu 	int ret = -ENOENT;
5775a6f6314SMasami Hiramatsu 
5785a6f6314SMasami Hiramatsu 	/* convert the address to dwarf address */
5795a6f6314SMasami Hiramatsu 	if (!is_kprobe) {
5805a6f6314SMasami Hiramatsu 		if (!addr) {
5815a6f6314SMasami Hiramatsu 			ret = -EINVAL;
5825a6f6314SMasami Hiramatsu 			goto error;
5835a6f6314SMasami Hiramatsu 		}
5845a6f6314SMasami Hiramatsu 		ret = get_text_start_address(tp->module, &stext);
5855a6f6314SMasami Hiramatsu 		if (ret < 0)
5865a6f6314SMasami Hiramatsu 			goto error;
5875a6f6314SMasami Hiramatsu 		addr += stext;
588e486367fSWang Nan 	} else if (tp->symbol) {
5899b239a12SMasami Hiramatsu 		/* If the module is given, this returns relative address */
5909b239a12SMasami Hiramatsu 		ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
5919b239a12SMasami Hiramatsu 							false, !!tp->module);
5929b239a12SMasami Hiramatsu 		if (ret != 0)
5935a6f6314SMasami Hiramatsu 			goto error;
5945a6f6314SMasami Hiramatsu 		addr += tp->offset;
5955a6f6314SMasami Hiramatsu 	}
5965a6f6314SMasami Hiramatsu 
5975a6f6314SMasami Hiramatsu 	pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
5985a6f6314SMasami Hiramatsu 		 tp->module ? : "kernel");
5995a6f6314SMasami Hiramatsu 
6007737af01SMasami Hiramatsu 	dinfo = debuginfo_cache__open(tp->module, verbose == 0);
6017737af01SMasami Hiramatsu 	if (dinfo)
6025a6f6314SMasami Hiramatsu 		ret = debuginfo__find_probe_point(dinfo,
6035a6f6314SMasami Hiramatsu 						 (unsigned long)addr, pp);
6047737af01SMasami Hiramatsu 	else
6055a6f6314SMasami Hiramatsu 		ret = -ENOENT;
6065a6f6314SMasami Hiramatsu 
6075a6f6314SMasami Hiramatsu 	if (ret > 0) {
6085a6f6314SMasami Hiramatsu 		pp->retprobe = tp->retprobe;
6095a6f6314SMasami Hiramatsu 		return 0;
6105a6f6314SMasami Hiramatsu 	}
6115a6f6314SMasami Hiramatsu error:
6125a6f6314SMasami Hiramatsu 	pr_debug("Failed to find corresponding probes from debuginfo.\n");
6135a6f6314SMasami Hiramatsu 	return ret ? : -ENOENT;
6145a6f6314SMasami Hiramatsu }
6155a6f6314SMasami Hiramatsu 
616fb7345bbSMasami Hiramatsu static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
617fb7345bbSMasami Hiramatsu 					  int ntevs, const char *exec)
618fb7345bbSMasami Hiramatsu {
619fb7345bbSMasami Hiramatsu 	int i, ret = 0;
620eb948e50SMasami Hiramatsu 	unsigned long stext = 0;
621fb7345bbSMasami Hiramatsu 
622fb7345bbSMasami Hiramatsu 	if (!exec)
623fb7345bbSMasami Hiramatsu 		return 0;
624fb7345bbSMasami Hiramatsu 
625fb7345bbSMasami Hiramatsu 	ret = get_text_start_address(exec, &stext);
626fb7345bbSMasami Hiramatsu 	if (ret < 0)
627fb7345bbSMasami Hiramatsu 		return ret;
628fb7345bbSMasami Hiramatsu 
629fb7345bbSMasami Hiramatsu 	for (i = 0; i < ntevs && ret >= 0; i++) {
630981a2379SMasami Hiramatsu 		/* point.address is the addres of point.symbol + point.offset */
631eb948e50SMasami Hiramatsu 		tevs[i].point.address -= stext;
632fb7345bbSMasami Hiramatsu 		tevs[i].point.module = strdup(exec);
633eb948e50SMasami Hiramatsu 		if (!tevs[i].point.module) {
634fb7345bbSMasami Hiramatsu 			ret = -ENOMEM;
635fb7345bbSMasami Hiramatsu 			break;
636fb7345bbSMasami Hiramatsu 		}
637fb7345bbSMasami Hiramatsu 		tevs[i].uprobes = true;
638fb7345bbSMasami Hiramatsu 	}
639fb7345bbSMasami Hiramatsu 
640fb7345bbSMasami Hiramatsu 	return ret;
641fb7345bbSMasami Hiramatsu }
642fb7345bbSMasami Hiramatsu 
643190b57fcSMasami Hiramatsu static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
644190b57fcSMasami Hiramatsu 					    int ntevs, const char *module)
645190b57fcSMasami Hiramatsu {
64614a8fd7cSMasami Hiramatsu 	int i, ret = 0;
64763a29613SRavi Bangoria 	char *mod_name = NULL;
64814a8fd7cSMasami Hiramatsu 
64914a8fd7cSMasami Hiramatsu 	if (!module)
65014a8fd7cSMasami Hiramatsu 		return 0;
65114a8fd7cSMasami Hiramatsu 
65263a29613SRavi Bangoria 	mod_name = find_module_name(module);
65314a8fd7cSMasami Hiramatsu 
654190b57fcSMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
65563a29613SRavi Bangoria 		tevs[i].point.module =
65663a29613SRavi Bangoria 			strdup(mod_name ? mod_name : module);
65714a8fd7cSMasami Hiramatsu 		if (!tevs[i].point.module) {
65814a8fd7cSMasami Hiramatsu 			ret = -ENOMEM;
65914a8fd7cSMasami Hiramatsu 			break;
660190b57fcSMasami Hiramatsu 		}
66114a8fd7cSMasami Hiramatsu 	}
66214a8fd7cSMasami Hiramatsu 
66363a29613SRavi Bangoria 	free(mod_name);
66414a8fd7cSMasami Hiramatsu 	return ret;
665190b57fcSMasami Hiramatsu }
666190b57fcSMasami Hiramatsu 
667dfef99cdSMasami Hiramatsu /* Post processing the probe events */
668dfef99cdSMasami Hiramatsu static int post_process_probe_trace_events(struct probe_trace_event *tevs,
669dfef99cdSMasami Hiramatsu 					   int ntevs, const char *module,
670dfef99cdSMasami Hiramatsu 					   bool uprobe)
671dfef99cdSMasami Hiramatsu {
672dfef99cdSMasami Hiramatsu 	struct ref_reloc_sym *reloc_sym;
673dfef99cdSMasami Hiramatsu 	char *tmp;
6745a51fcd1SMasami Hiramatsu 	int i, skipped = 0;
675dfef99cdSMasami Hiramatsu 
676dfef99cdSMasami Hiramatsu 	if (uprobe)
677dfef99cdSMasami Hiramatsu 		return add_exec_to_probe_trace_events(tevs, ntevs, module);
678dfef99cdSMasami Hiramatsu 
679dfef99cdSMasami Hiramatsu 	/* Note that currently ref_reloc_sym based probe is not for drivers */
680dfef99cdSMasami Hiramatsu 	if (module)
681dfef99cdSMasami Hiramatsu 		return add_module_to_probe_trace_events(tevs, ntevs, module);
682dfef99cdSMasami Hiramatsu 
6838f33f7deSMasami Hiramatsu 	reloc_sym = kernel_get_ref_reloc_sym();
684dfef99cdSMasami Hiramatsu 	if (!reloc_sym) {
685dfef99cdSMasami Hiramatsu 		pr_warning("Relocated base symbol is not found!\n");
686dfef99cdSMasami Hiramatsu 		return -EINVAL;
687dfef99cdSMasami Hiramatsu 	}
688dfef99cdSMasami Hiramatsu 
689dfef99cdSMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
690b031220dSMasami Hiramatsu 		if (!tevs[i].point.address || tevs[i].point.retprobe)
691b031220dSMasami Hiramatsu 			continue;
6925a51fcd1SMasami Hiramatsu 		/* If we found a wrong one, mark it by NULL symbol */
693b031220dSMasami Hiramatsu 		if (kprobe_warn_out_range(tevs[i].point.symbol,
694b031220dSMasami Hiramatsu 					  tevs[i].point.address)) {
6955a51fcd1SMasami Hiramatsu 			tmp = NULL;
6965a51fcd1SMasami Hiramatsu 			skipped++;
6975a51fcd1SMasami Hiramatsu 		} else {
698dfef99cdSMasami Hiramatsu 			tmp = strdup(reloc_sym->name);
699dfef99cdSMasami Hiramatsu 			if (!tmp)
700dfef99cdSMasami Hiramatsu 				return -ENOMEM;
7015a51fcd1SMasami Hiramatsu 		}
7024c859351SMasami Hiramatsu 		/* If we have no realname, use symbol for it */
7034c859351SMasami Hiramatsu 		if (!tevs[i].point.realname)
7044c859351SMasami Hiramatsu 			tevs[i].point.realname = tevs[i].point.symbol;
7054c859351SMasami Hiramatsu 		else
706dfef99cdSMasami Hiramatsu 			free(tevs[i].point.symbol);
707dfef99cdSMasami Hiramatsu 		tevs[i].point.symbol = tmp;
708dfef99cdSMasami Hiramatsu 		tevs[i].point.offset = tevs[i].point.address -
709dfef99cdSMasami Hiramatsu 				       reloc_sym->unrelocated_addr;
710dfef99cdSMasami Hiramatsu 	}
7115a51fcd1SMasami Hiramatsu 	return skipped;
712dfef99cdSMasami Hiramatsu }
713dfef99cdSMasami Hiramatsu 
7144b4da7f7SMasami Hiramatsu /* Try to find perf_probe_event with debuginfo */
7150e60836bSSrikar Dronamraju static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
716ddb2f58fSMasami Hiramatsu 					  struct probe_trace_event **tevs)
7174b4da7f7SMasami Hiramatsu {
7184b4da7f7SMasami Hiramatsu 	bool need_dwarf = perf_probe_event_need_dwarf(pev);
7199b118acaSMasami Hiramatsu 	struct perf_probe_point tmp;
720225466f1SSrikar Dronamraju 	struct debuginfo *dinfo;
721190b57fcSMasami Hiramatsu 	int ntevs, ret = 0;
7224b4da7f7SMasami Hiramatsu 
72344225521SMasami Hiramatsu 	dinfo = open_debuginfo(pev->target, !need_dwarf);
724ff741783SMasami Hiramatsu 	if (!dinfo) {
72592561cb7SMasami Hiramatsu 		if (need_dwarf)
726ff741783SMasami Hiramatsu 			return -ENOENT;
727ff741783SMasami Hiramatsu 		pr_debug("Could not open debuginfo. Try to use symbols.\n");
7284b4da7f7SMasami Hiramatsu 		return 0;
7294b4da7f7SMasami Hiramatsu 	}
7304b4da7f7SMasami Hiramatsu 
731dfef99cdSMasami Hiramatsu 	pr_debug("Try to find probe point from debuginfo.\n");
732ff741783SMasami Hiramatsu 	/* Searching trace events corresponding to a probe event */
733ddb2f58fSMasami Hiramatsu 	ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
734ff741783SMasami Hiramatsu 
7359b118acaSMasami Hiramatsu 	if (ntevs == 0)	{  /* Not found, retry with an alternative */
73644225521SMasami Hiramatsu 		ret = get_alternative_probe_event(dinfo, pev, &tmp);
7379b118acaSMasami Hiramatsu 		if (!ret) {
738ddb2f58fSMasami Hiramatsu 			ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
7399b118acaSMasami Hiramatsu 			/*
7409b118acaSMasami Hiramatsu 			 * Write back to the original probe_event for
7419b118acaSMasami Hiramatsu 			 * setting appropriate (user given) event name
7429b118acaSMasami Hiramatsu 			 */
7439b118acaSMasami Hiramatsu 			clear_perf_probe_point(&pev->point);
7449b118acaSMasami Hiramatsu 			memcpy(&pev->point, &tmp, sizeof(tmp));
7459b118acaSMasami Hiramatsu 		}
7469b118acaSMasami Hiramatsu 	}
7479b118acaSMasami Hiramatsu 
748ff741783SMasami Hiramatsu 	debuginfo__delete(dinfo);
7494b4da7f7SMasami Hiramatsu 
750146a1439SMasami Hiramatsu 	if (ntevs > 0) {	/* Succeeded to find trace events */
751dfef99cdSMasami Hiramatsu 		pr_debug("Found %d probe_trace_events.\n", ntevs);
752dfef99cdSMasami Hiramatsu 		ret = post_process_probe_trace_events(*tevs, ntevs,
75344225521SMasami Hiramatsu 						pev->target, pev->uprobes);
7545a51fcd1SMasami Hiramatsu 		if (ret < 0 || ret == ntevs) {
755981d05adSMasami Hiramatsu 			clear_probe_trace_events(*tevs, ntevs);
756981d05adSMasami Hiramatsu 			zfree(tevs);
757981d05adSMasami Hiramatsu 		}
7585a51fcd1SMasami Hiramatsu 		if (ret != ntevs)
759190b57fcSMasami Hiramatsu 			return ret < 0 ? ret : ntevs;
7605a51fcd1SMasami Hiramatsu 		ntevs = 0;
7615a51fcd1SMasami Hiramatsu 		/* Fall through */
762146a1439SMasami Hiramatsu 	}
7634b4da7f7SMasami Hiramatsu 
764146a1439SMasami Hiramatsu 	if (ntevs == 0)	{	/* No error but failed to find probe point. */
7650687eba7SMasami Hiramatsu 		pr_warning("Probe point '%s' not found.\n",
7664b4da7f7SMasami Hiramatsu 			   synthesize_perf_probe_point(&pev->point));
767146a1439SMasami Hiramatsu 		return -ENOENT;
768146a1439SMasami Hiramatsu 	}
769146a1439SMasami Hiramatsu 	/* Error path : ntevs < 0 */
77015eca306SMasami Hiramatsu 	pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
7711c0bd0e8SWang Nan 	if (ntevs < 0) {
7721c0bd0e8SWang Nan 		if (ntevs == -EBADF)
77315eca306SMasami Hiramatsu 			pr_warning("Warning: No dwarf info found in the vmlinux - "
77415eca306SMasami Hiramatsu 				"please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
77515eca306SMasami Hiramatsu 		if (!need_dwarf) {
7760e43e5d2SMasami Hiramatsu 			pr_debug("Trying to use symbols.\n");
7774b4da7f7SMasami Hiramatsu 			return 0;
7784b4da7f7SMasami Hiramatsu 		}
77915eca306SMasami Hiramatsu 	}
78015eca306SMasami Hiramatsu 	return ntevs;
78115eca306SMasami Hiramatsu }
7824b4da7f7SMasami Hiramatsu 
7834b4da7f7SMasami Hiramatsu #define LINEBUF_SIZE 256
7844b4da7f7SMasami Hiramatsu #define NR_ADDITIONAL_LINES 2
7854b4da7f7SMasami Hiramatsu 
786fde52dbdSFranck Bui-Huu static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
7874b4da7f7SMasami Hiramatsu {
7885f03cba4SMasami Hiramatsu 	char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
789befe3414SFranck Bui-Huu 	const char *color = show_num ? "" : PERF_COLOR_BLUE;
790befe3414SFranck Bui-Huu 	const char *prefix = NULL;
7914b4da7f7SMasami Hiramatsu 
792befe3414SFranck Bui-Huu 	do {
7934b4da7f7SMasami Hiramatsu 		if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
7944b4da7f7SMasami Hiramatsu 			goto error;
795befe3414SFranck Bui-Huu 		if (skip)
796befe3414SFranck Bui-Huu 			continue;
797befe3414SFranck Bui-Huu 		if (!prefix) {
798befe3414SFranck Bui-Huu 			prefix = show_num ? "%7d  " : "         ";
799befe3414SFranck Bui-Huu 			color_fprintf(stdout, color, prefix, l);
8004b4da7f7SMasami Hiramatsu 		}
801befe3414SFranck Bui-Huu 		color_fprintf(stdout, color, "%s", buf);
8024b4da7f7SMasami Hiramatsu 
803befe3414SFranck Bui-Huu 	} while (strchr(buf, '\n') == NULL);
804146a1439SMasami Hiramatsu 
805fde52dbdSFranck Bui-Huu 	return 1;
8064b4da7f7SMasami Hiramatsu error:
807fde52dbdSFranck Bui-Huu 	if (ferror(fp)) {
8085f03cba4SMasami Hiramatsu 		pr_warning("File read error: %s\n",
809c8b5f2c9SArnaldo Carvalho de Melo 			   str_error_r(errno, sbuf, sizeof(sbuf)));
810146a1439SMasami Hiramatsu 		return -1;
8114b4da7f7SMasami Hiramatsu 	}
812fde52dbdSFranck Bui-Huu 	return 0;
813fde52dbdSFranck Bui-Huu }
814fde52dbdSFranck Bui-Huu 
815fde52dbdSFranck Bui-Huu static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
816fde52dbdSFranck Bui-Huu {
817fde52dbdSFranck Bui-Huu 	int rv = __show_one_line(fp, l, skip, show_num);
818fde52dbdSFranck Bui-Huu 	if (rv == 0) {
819fde52dbdSFranck Bui-Huu 		pr_warning("Source file is shorter than expected.\n");
820fde52dbdSFranck Bui-Huu 		rv = -1;
821fde52dbdSFranck Bui-Huu 	}
822fde52dbdSFranck Bui-Huu 	return rv;
823fde52dbdSFranck Bui-Huu }
824fde52dbdSFranck Bui-Huu 
825fde52dbdSFranck Bui-Huu #define show_one_line_with_num(f,l)	_show_one_line(f,l,false,true)
826fde52dbdSFranck Bui-Huu #define show_one_line(f,l)		_show_one_line(f,l,false,false)
827fde52dbdSFranck Bui-Huu #define skip_one_line(f,l)		_show_one_line(f,l,true,false)
828fde52dbdSFranck Bui-Huu #define show_one_line_or_eof(f,l)	__show_one_line(f,l,false,false)
8294b4da7f7SMasami Hiramatsu 
8304b4da7f7SMasami Hiramatsu /*
8314b4da7f7SMasami Hiramatsu  * Show line-range always requires debuginfo to find source file and
8324b4da7f7SMasami Hiramatsu  * line number.
8334b4da7f7SMasami Hiramatsu  */
834811dd2aeSMasami Hiramatsu static int __show_line_range(struct line_range *lr, const char *module,
835811dd2aeSMasami Hiramatsu 			     bool user)
8364b4da7f7SMasami Hiramatsu {
837d3b63d7aSMasami Hiramatsu 	int l = 1;
8385a62257aSMasami Hiramatsu 	struct int_node *ln;
839ff741783SMasami Hiramatsu 	struct debuginfo *dinfo;
8404b4da7f7SMasami Hiramatsu 	FILE *fp;
841ff741783SMasami Hiramatsu 	int ret;
8427cf0b79eSMasami Hiramatsu 	char *tmp;
8435f03cba4SMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE];
8444b4da7f7SMasami Hiramatsu 
8454b4da7f7SMasami Hiramatsu 	/* Search a line range */
84692561cb7SMasami Hiramatsu 	dinfo = open_debuginfo(module, false);
84792561cb7SMasami Hiramatsu 	if (!dinfo)
848ff741783SMasami Hiramatsu 		return -ENOENT;
849146a1439SMasami Hiramatsu 
850ff741783SMasami Hiramatsu 	ret = debuginfo__find_line_range(dinfo, lr);
851811dd2aeSMasami Hiramatsu 	if (!ret) {	/* Not found, retry with an alternative */
852811dd2aeSMasami Hiramatsu 		ret = get_alternative_line_range(dinfo, lr, module, user);
853811dd2aeSMasami Hiramatsu 		if (!ret)
854811dd2aeSMasami Hiramatsu 			ret = debuginfo__find_line_range(dinfo, lr);
855811dd2aeSMasami Hiramatsu 	}
856ff741783SMasami Hiramatsu 	debuginfo__delete(dinfo);
8575ee05b88SMasami Hiramatsu 	if (ret == 0 || ret == -ENOENT) {
858146a1439SMasami Hiramatsu 		pr_warning("Specified source line is not found.\n");
859146a1439SMasami Hiramatsu 		return -ENOENT;
860146a1439SMasami Hiramatsu 	} else if (ret < 0) {
8615ee05b88SMasami Hiramatsu 		pr_warning("Debuginfo analysis failed.\n");
862146a1439SMasami Hiramatsu 		return ret;
863146a1439SMasami Hiramatsu 	}
8644b4da7f7SMasami Hiramatsu 
8657cf0b79eSMasami Hiramatsu 	/* Convert source file path */
8667cf0b79eSMasami Hiramatsu 	tmp = lr->path;
8676a330a3cSMasami Hiramatsu 	ret = get_real_path(tmp, lr->comp_dir, &lr->path);
868a78604deSHe Kuang 
869a78604deSHe Kuang 	/* Free old path when new path is assigned */
870a78604deSHe Kuang 	if (tmp != lr->path)
871a78604deSHe Kuang 		free(tmp);
872a78604deSHe Kuang 
8737cf0b79eSMasami Hiramatsu 	if (ret < 0) {
8745ee05b88SMasami Hiramatsu 		pr_warning("Failed to find source file path.\n");
8757cf0b79eSMasami Hiramatsu 		return ret;
8767cf0b79eSMasami Hiramatsu 	}
8777cf0b79eSMasami Hiramatsu 
8784b4da7f7SMasami Hiramatsu 	setup_pager();
8794b4da7f7SMasami Hiramatsu 
8804b4da7f7SMasami Hiramatsu 	if (lr->function)
8818737ebdeSMasami Hiramatsu 		fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
8824b4da7f7SMasami Hiramatsu 			lr->start - lr->offset);
8834b4da7f7SMasami Hiramatsu 	else
88462c15fc4SFranck Bui-Huu 		fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
8854b4da7f7SMasami Hiramatsu 
8864b4da7f7SMasami Hiramatsu 	fp = fopen(lr->path, "r");
887146a1439SMasami Hiramatsu 	if (fp == NULL) {
888146a1439SMasami Hiramatsu 		pr_warning("Failed to open %s: %s\n", lr->path,
889c8b5f2c9SArnaldo Carvalho de Melo 			   str_error_r(errno, sbuf, sizeof(sbuf)));
890146a1439SMasami Hiramatsu 		return -errno;
891146a1439SMasami Hiramatsu 	}
8924b4da7f7SMasami Hiramatsu 	/* Skip to starting line number */
89344b81e92SFranck Bui-Huu 	while (l < lr->start) {
894fde52dbdSFranck Bui-Huu 		ret = skip_one_line(fp, l++);
895146a1439SMasami Hiramatsu 		if (ret < 0)
896146a1439SMasami Hiramatsu 			goto end;
89744b81e92SFranck Bui-Huu 	}
8984b4da7f7SMasami Hiramatsu 
89910daf4d0SArnaldo Carvalho de Melo 	intlist__for_each_entry(ln, lr->line_list) {
9005a62257aSMasami Hiramatsu 		for (; ln->i > l; l++) {
901fde52dbdSFranck Bui-Huu 			ret = show_one_line(fp, l - lr->offset);
90244b81e92SFranck Bui-Huu 			if (ret < 0)
90344b81e92SFranck Bui-Huu 				goto end;
90444b81e92SFranck Bui-Huu 		}
905fde52dbdSFranck Bui-Huu 		ret = show_one_line_with_num(fp, l++ - lr->offset);
906146a1439SMasami Hiramatsu 		if (ret < 0)
907146a1439SMasami Hiramatsu 			goto end;
9084b4da7f7SMasami Hiramatsu 	}
9094b4da7f7SMasami Hiramatsu 
9104b4da7f7SMasami Hiramatsu 	if (lr->end == INT_MAX)
9114b4da7f7SMasami Hiramatsu 		lr->end = l + NR_ADDITIONAL_LINES;
912fde52dbdSFranck Bui-Huu 	while (l <= lr->end) {
913fde52dbdSFranck Bui-Huu 		ret = show_one_line_or_eof(fp, l++ - lr->offset);
914fde52dbdSFranck Bui-Huu 		if (ret <= 0)
91544b81e92SFranck Bui-Huu 			break;
91644b81e92SFranck Bui-Huu 	}
917146a1439SMasami Hiramatsu end:
9184b4da7f7SMasami Hiramatsu 	fclose(fp);
919146a1439SMasami Hiramatsu 	return ret;
9204b4da7f7SMasami Hiramatsu }
9214b4da7f7SMasami Hiramatsu 
9222b394bc4SMasami Hiramatsu int show_line_range(struct line_range *lr, const char *module, bool user)
923ee45b6c2SMasami Hiramatsu {
924ee45b6c2SMasami Hiramatsu 	int ret;
925ee45b6c2SMasami Hiramatsu 
9269bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(user);
927ee45b6c2SMasami Hiramatsu 	if (ret < 0)
928ee45b6c2SMasami Hiramatsu 		return ret;
929811dd2aeSMasami Hiramatsu 	ret = __show_line_range(lr, module, user);
9309bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
931ee45b6c2SMasami Hiramatsu 
932ee45b6c2SMasami Hiramatsu 	return ret;
933ee45b6c2SMasami Hiramatsu }
934ee45b6c2SMasami Hiramatsu 
935ff741783SMasami Hiramatsu static int show_available_vars_at(struct debuginfo *dinfo,
936ff741783SMasami Hiramatsu 				  struct perf_probe_event *pev,
937ddb2f58fSMasami Hiramatsu 				  struct strfilter *_filter)
938cf6eb489SMasami Hiramatsu {
939cf6eb489SMasami Hiramatsu 	char *buf;
940bd09d7b5SMasami Hiramatsu 	int ret, i, nvars;
941cf6eb489SMasami Hiramatsu 	struct str_node *node;
942cf6eb489SMasami Hiramatsu 	struct variable_list *vls = NULL, *vl;
9439b118acaSMasami Hiramatsu 	struct perf_probe_point tmp;
944bd09d7b5SMasami Hiramatsu 	const char *var;
945cf6eb489SMasami Hiramatsu 
946cf6eb489SMasami Hiramatsu 	buf = synthesize_perf_probe_point(&pev->point);
947cf6eb489SMasami Hiramatsu 	if (!buf)
948cf6eb489SMasami Hiramatsu 		return -EINVAL;
949cf6eb489SMasami Hiramatsu 	pr_debug("Searching variables at %s\n", buf);
950cf6eb489SMasami Hiramatsu 
951ddb2f58fSMasami Hiramatsu 	ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
9529b118acaSMasami Hiramatsu 	if (!ret) {  /* Not found, retry with an alternative */
95344225521SMasami Hiramatsu 		ret = get_alternative_probe_event(dinfo, pev, &tmp);
9549b118acaSMasami Hiramatsu 		if (!ret) {
9559b118acaSMasami Hiramatsu 			ret = debuginfo__find_available_vars_at(dinfo, pev,
956ddb2f58fSMasami Hiramatsu 								&vls);
9579b118acaSMasami Hiramatsu 			/* Release the old probe_point */
9589b118acaSMasami Hiramatsu 			clear_perf_probe_point(&tmp);
9599b118acaSMasami Hiramatsu 		}
9609b118acaSMasami Hiramatsu 	}
961bd09d7b5SMasami Hiramatsu 	if (ret <= 0) {
96269e96eaaSMasami Hiramatsu 		if (ret == 0 || ret == -ENOENT) {
96369e96eaaSMasami Hiramatsu 			pr_err("Failed to find the address of %s\n", buf);
96469e96eaaSMasami Hiramatsu 			ret = -ENOENT;
96569e96eaaSMasami Hiramatsu 		} else
96669e96eaaSMasami Hiramatsu 			pr_warning("Debuginfo analysis failed.\n");
967bd09d7b5SMasami Hiramatsu 		goto end;
968bd09d7b5SMasami Hiramatsu 	}
96969e96eaaSMasami Hiramatsu 
970bd09d7b5SMasami Hiramatsu 	/* Some variables are found */
971cf6eb489SMasami Hiramatsu 	fprintf(stdout, "Available variables at %s\n", buf);
972cf6eb489SMasami Hiramatsu 	for (i = 0; i < ret; i++) {
973cf6eb489SMasami Hiramatsu 		vl = &vls[i];
974cf6eb489SMasami Hiramatsu 		/*
975cf6eb489SMasami Hiramatsu 		 * A probe point might be converted to
976cf6eb489SMasami Hiramatsu 		 * several trace points.
977cf6eb489SMasami Hiramatsu 		 */
978cf6eb489SMasami Hiramatsu 		fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
979cf6eb489SMasami Hiramatsu 			vl->point.offset);
98074cf249dSArnaldo Carvalho de Melo 		zfree(&vl->point.symbol);
981bd09d7b5SMasami Hiramatsu 		nvars = 0;
982cf6eb489SMasami Hiramatsu 		if (vl->vars) {
983602a1f4dSArnaldo Carvalho de Melo 			strlist__for_each_entry(node, vl->vars) {
984bd09d7b5SMasami Hiramatsu 				var = strchr(node->s, '\t') + 1;
985bd09d7b5SMasami Hiramatsu 				if (strfilter__compare(_filter, var)) {
986cf6eb489SMasami Hiramatsu 					fprintf(stdout, "\t\t%s\n", node->s);
987bd09d7b5SMasami Hiramatsu 					nvars++;
988bd09d7b5SMasami Hiramatsu 				}
989bd09d7b5SMasami Hiramatsu 			}
990cf6eb489SMasami Hiramatsu 			strlist__delete(vl->vars);
991bd09d7b5SMasami Hiramatsu 		}
992bd09d7b5SMasami Hiramatsu 		if (nvars == 0)
993bd09d7b5SMasami Hiramatsu 			fprintf(stdout, "\t\t(No matched variables)\n");
994cf6eb489SMasami Hiramatsu 	}
995cf6eb489SMasami Hiramatsu 	free(vls);
996bd09d7b5SMasami Hiramatsu end:
997cf6eb489SMasami Hiramatsu 	free(buf);
998cf6eb489SMasami Hiramatsu 	return ret;
999cf6eb489SMasami Hiramatsu }
1000cf6eb489SMasami Hiramatsu 
1001cf6eb489SMasami Hiramatsu /* Show available variables on given probe point */
1002cf6eb489SMasami Hiramatsu int show_available_vars(struct perf_probe_event *pevs, int npevs,
1003ddb2f58fSMasami Hiramatsu 			struct strfilter *_filter)
1004cf6eb489SMasami Hiramatsu {
1005ff741783SMasami Hiramatsu 	int i, ret = 0;
1006ff741783SMasami Hiramatsu 	struct debuginfo *dinfo;
1007cf6eb489SMasami Hiramatsu 
10089bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(pevs->uprobes);
1009cf6eb489SMasami Hiramatsu 	if (ret < 0)
1010cf6eb489SMasami Hiramatsu 		return ret;
1011cf6eb489SMasami Hiramatsu 
101244225521SMasami Hiramatsu 	dinfo = open_debuginfo(pevs->target, false);
1013ff741783SMasami Hiramatsu 	if (!dinfo) {
1014ee45b6c2SMasami Hiramatsu 		ret = -ENOENT;
1015ee45b6c2SMasami Hiramatsu 		goto out;
1016ff741783SMasami Hiramatsu 	}
1017ff741783SMasami Hiramatsu 
1018cc446446SMasami Hiramatsu 	setup_pager();
1019cc446446SMasami Hiramatsu 
1020ff741783SMasami Hiramatsu 	for (i = 0; i < npevs && ret >= 0; i++)
1021ddb2f58fSMasami Hiramatsu 		ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1022ff741783SMasami Hiramatsu 
1023ff741783SMasami Hiramatsu 	debuginfo__delete(dinfo);
1024ee45b6c2SMasami Hiramatsu out:
10259bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
1026cf6eb489SMasami Hiramatsu 	return ret;
1027cf6eb489SMasami Hiramatsu }
1028cf6eb489SMasami Hiramatsu 
102989fe808aSIngo Molnar #else	/* !HAVE_DWARF_SUPPORT */
10304b4da7f7SMasami Hiramatsu 
10317737af01SMasami Hiramatsu static void debuginfo_cache__exit(void)
10327737af01SMasami Hiramatsu {
10337737af01SMasami Hiramatsu }
10347737af01SMasami Hiramatsu 
10355a6f6314SMasami Hiramatsu static int
10365a6f6314SMasami Hiramatsu find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
10375a6f6314SMasami Hiramatsu 				 struct perf_probe_point *pp __maybe_unused,
10385a6f6314SMasami Hiramatsu 				 bool is_kprobe __maybe_unused)
10394b4da7f7SMasami Hiramatsu {
10405a6f6314SMasami Hiramatsu 	return -ENOSYS;
10414b4da7f7SMasami Hiramatsu }
10424b4da7f7SMasami Hiramatsu 
10430e60836bSSrikar Dronamraju static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1044ddb2f58fSMasami Hiramatsu 				struct probe_trace_event **tevs __maybe_unused)
10454b4da7f7SMasami Hiramatsu {
1046146a1439SMasami Hiramatsu 	if (perf_probe_event_need_dwarf(pev)) {
1047146a1439SMasami Hiramatsu 		pr_warning("Debuginfo-analysis is not supported.\n");
1048146a1439SMasami Hiramatsu 		return -ENOSYS;
1049146a1439SMasami Hiramatsu 	}
1050225466f1SSrikar Dronamraju 
10514b4da7f7SMasami Hiramatsu 	return 0;
10524b4da7f7SMasami Hiramatsu }
10534b4da7f7SMasami Hiramatsu 
10541d037ca1SIrina Tirdea int show_line_range(struct line_range *lr __maybe_unused,
10552b394bc4SMasami Hiramatsu 		    const char *module __maybe_unused,
10562b394bc4SMasami Hiramatsu 		    bool user __maybe_unused)
10574b4da7f7SMasami Hiramatsu {
1058146a1439SMasami Hiramatsu 	pr_warning("Debuginfo-analysis is not supported.\n");
1059146a1439SMasami Hiramatsu 	return -ENOSYS;
10604b4da7f7SMasami Hiramatsu }
10614b4da7f7SMasami Hiramatsu 
10621d037ca1SIrina Tirdea int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1063ddb2f58fSMasami Hiramatsu 			int npevs __maybe_unused,
1064ddb2f58fSMasami Hiramatsu 			struct strfilter *filter __maybe_unused)
1065cf6eb489SMasami Hiramatsu {
1066cf6eb489SMasami Hiramatsu 	pr_warning("Debuginfo-analysis is not supported.\n");
1067cf6eb489SMasami Hiramatsu 	return -ENOSYS;
1068cf6eb489SMasami Hiramatsu }
1069e0faa8d3SMasami Hiramatsu #endif
1070e0faa8d3SMasami Hiramatsu 
1071e53b00d3SMasami Hiramatsu void line_range__clear(struct line_range *lr)
1072e53b00d3SMasami Hiramatsu {
1073e53b00d3SMasami Hiramatsu 	free(lr->function);
1074e53b00d3SMasami Hiramatsu 	free(lr->file);
1075e53b00d3SMasami Hiramatsu 	free(lr->path);
1076e53b00d3SMasami Hiramatsu 	free(lr->comp_dir);
10775a62257aSMasami Hiramatsu 	intlist__delete(lr->line_list);
1078e53b00d3SMasami Hiramatsu 	memset(lr, 0, sizeof(*lr));
1079e53b00d3SMasami Hiramatsu }
1080e53b00d3SMasami Hiramatsu 
10815a62257aSMasami Hiramatsu int line_range__init(struct line_range *lr)
1082e53b00d3SMasami Hiramatsu {
1083e53b00d3SMasami Hiramatsu 	memset(lr, 0, sizeof(*lr));
10845a62257aSMasami Hiramatsu 	lr->line_list = intlist__new(NULL);
10855a62257aSMasami Hiramatsu 	if (!lr->line_list)
10865a62257aSMasami Hiramatsu 		return -ENOMEM;
10875a62257aSMasami Hiramatsu 	else
10885a62257aSMasami Hiramatsu 		return 0;
1089e53b00d3SMasami Hiramatsu }
1090e53b00d3SMasami Hiramatsu 
109121dd9ae5SFranck Bui-Huu static int parse_line_num(char **ptr, int *val, const char *what)
109221dd9ae5SFranck Bui-Huu {
109321dd9ae5SFranck Bui-Huu 	const char *start = *ptr;
109421dd9ae5SFranck Bui-Huu 
109521dd9ae5SFranck Bui-Huu 	errno = 0;
109621dd9ae5SFranck Bui-Huu 	*val = strtol(*ptr, ptr, 0);
109721dd9ae5SFranck Bui-Huu 	if (errno || *ptr == start) {
109821dd9ae5SFranck Bui-Huu 		semantic_error("'%s' is not a valid number.\n", what);
109921dd9ae5SFranck Bui-Huu 		return -EINVAL;
110021dd9ae5SFranck Bui-Huu 	}
110121dd9ae5SFranck Bui-Huu 	return 0;
110221dd9ae5SFranck Bui-Huu }
110321dd9ae5SFranck Bui-Huu 
1104573709fdSMasami Hiramatsu /* Check the name is good for event, group or function */
1105573709fdSMasami Hiramatsu static bool is_c_func_name(const char *name)
1106573709fdSMasami Hiramatsu {
1107573709fdSMasami Hiramatsu 	if (!isalpha(*name) && *name != '_')
1108573709fdSMasami Hiramatsu 		return false;
1109573709fdSMasami Hiramatsu 	while (*++name != '\0') {
1110573709fdSMasami Hiramatsu 		if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1111573709fdSMasami Hiramatsu 			return false;
1112573709fdSMasami Hiramatsu 	}
1113573709fdSMasami Hiramatsu 	return true;
1114573709fdSMasami Hiramatsu }
1115573709fdSMasami Hiramatsu 
11169d95b580SFranck Bui-Huu /*
11179d95b580SFranck Bui-Huu  * Stuff 'lr' according to the line range described by 'arg'.
11189d95b580SFranck Bui-Huu  * The line range syntax is described by:
11199d95b580SFranck Bui-Huu  *
11209d95b580SFranck Bui-Huu  *         SRC[:SLN[+NUM|-ELN]]
1121e116dfa1SMasami Hiramatsu  *         FNC[@SRC][:SLN[+NUM|-ELN]]
11229d95b580SFranck Bui-Huu  */
1123146a1439SMasami Hiramatsu int parse_line_range_desc(const char *arg, struct line_range *lr)
1124631c9defSMasami Hiramatsu {
1125e116dfa1SMasami Hiramatsu 	char *range, *file, *name = strdup(arg);
112621dd9ae5SFranck Bui-Huu 	int err;
11279d95b580SFranck Bui-Huu 
112821dd9ae5SFranck Bui-Huu 	if (!name)
112921dd9ae5SFranck Bui-Huu 		return -ENOMEM;
113021dd9ae5SFranck Bui-Huu 
113121dd9ae5SFranck Bui-Huu 	lr->start = 0;
113221dd9ae5SFranck Bui-Huu 	lr->end = INT_MAX;
113321dd9ae5SFranck Bui-Huu 
113421dd9ae5SFranck Bui-Huu 	range = strchr(name, ':');
113521dd9ae5SFranck Bui-Huu 	if (range) {
113621dd9ae5SFranck Bui-Huu 		*range++ = '\0';
113721dd9ae5SFranck Bui-Huu 
113821dd9ae5SFranck Bui-Huu 		err = parse_line_num(&range, &lr->start, "start line");
113921dd9ae5SFranck Bui-Huu 		if (err)
114021dd9ae5SFranck Bui-Huu 			goto err;
114121dd9ae5SFranck Bui-Huu 
114221dd9ae5SFranck Bui-Huu 		if (*range == '+' || *range == '-') {
114321dd9ae5SFranck Bui-Huu 			const char c = *range++;
114421dd9ae5SFranck Bui-Huu 
114521dd9ae5SFranck Bui-Huu 			err = parse_line_num(&range, &lr->end, "end line");
114621dd9ae5SFranck Bui-Huu 			if (err)
114721dd9ae5SFranck Bui-Huu 				goto err;
114821dd9ae5SFranck Bui-Huu 
114921dd9ae5SFranck Bui-Huu 			if (c == '+') {
115021dd9ae5SFranck Bui-Huu 				lr->end += lr->start;
115121dd9ae5SFranck Bui-Huu 				/*
1152dda4ab34SMasami Hiramatsu 				 * Adjust the number of lines here.
1153dda4ab34SMasami Hiramatsu 				 * If the number of lines == 1, the
1154dda4ab34SMasami Hiramatsu 				 * the end of line should be equal to
1155dda4ab34SMasami Hiramatsu 				 * the start of line.
1156dda4ab34SMasami Hiramatsu 				 */
115721dd9ae5SFranck Bui-Huu 				lr->end--;
115821dd9ae5SFranck Bui-Huu 			}
115921dd9ae5SFranck Bui-Huu 		}
116021dd9ae5SFranck Bui-Huu 
1161d3b63d7aSMasami Hiramatsu 		pr_debug("Line range is %d to %d\n", lr->start, lr->end);
116221dd9ae5SFranck Bui-Huu 
116321dd9ae5SFranck Bui-Huu 		err = -EINVAL;
1164d3b63d7aSMasami Hiramatsu 		if (lr->start > lr->end) {
1165631c9defSMasami Hiramatsu 			semantic_error("Start line must be smaller"
1166146a1439SMasami Hiramatsu 				       " than end line.\n");
116721dd9ae5SFranck Bui-Huu 			goto err;
1168146a1439SMasami Hiramatsu 		}
116921dd9ae5SFranck Bui-Huu 		if (*range != '\0') {
117021dd9ae5SFranck Bui-Huu 			semantic_error("Tailing with invalid str '%s'.\n", range);
117121dd9ae5SFranck Bui-Huu 			goto err;
1172146a1439SMasami Hiramatsu 		}
1173d3b63d7aSMasami Hiramatsu 	}
117402b95dadSMasami Hiramatsu 
1175e116dfa1SMasami Hiramatsu 	file = strchr(name, '@');
1176e116dfa1SMasami Hiramatsu 	if (file) {
1177e116dfa1SMasami Hiramatsu 		*file = '\0';
1178e116dfa1SMasami Hiramatsu 		lr->file = strdup(++file);
1179e116dfa1SMasami Hiramatsu 		if (lr->file == NULL) {
1180e116dfa1SMasami Hiramatsu 			err = -ENOMEM;
1181e116dfa1SMasami Hiramatsu 			goto err;
1182e116dfa1SMasami Hiramatsu 		}
1183e116dfa1SMasami Hiramatsu 		lr->function = name;
1184573709fdSMasami Hiramatsu 	} else if (strchr(name, '/') || strchr(name, '.'))
118521dd9ae5SFranck Bui-Huu 		lr->file = name;
1186573709fdSMasami Hiramatsu 	else if (is_c_func_name(name))/* We reuse it for checking funcname */
118721dd9ae5SFranck Bui-Huu 		lr->function = name;
1188573709fdSMasami Hiramatsu 	else {	/* Invalid name */
1189573709fdSMasami Hiramatsu 		semantic_error("'%s' is not a valid function name.\n", name);
1190573709fdSMasami Hiramatsu 		err = -EINVAL;
1191573709fdSMasami Hiramatsu 		goto err;
1192573709fdSMasami Hiramatsu 	}
1193146a1439SMasami Hiramatsu 
1194146a1439SMasami Hiramatsu 	return 0;
119521dd9ae5SFranck Bui-Huu err:
119621dd9ae5SFranck Bui-Huu 	free(name);
119721dd9ae5SFranck Bui-Huu 	return err;
1198631c9defSMasami Hiramatsu }
1199631c9defSMasami Hiramatsu 
120036a009feSMasami Hiramatsu static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
120136a009feSMasami Hiramatsu {
120236a009feSMasami Hiramatsu 	char *ptr;
120336a009feSMasami Hiramatsu 
120436a009feSMasami Hiramatsu 	ptr = strchr(*arg, ':');
120536a009feSMasami Hiramatsu 	if (ptr) {
120636a009feSMasami Hiramatsu 		*ptr = '\0';
120742bba263SMasami Hiramatsu 		if (!pev->sdt && !is_c_func_name(*arg))
120836a009feSMasami Hiramatsu 			goto ng_name;
120936a009feSMasami Hiramatsu 		pev->group = strdup(*arg);
121036a009feSMasami Hiramatsu 		if (!pev->group)
121136a009feSMasami Hiramatsu 			return -ENOMEM;
121236a009feSMasami Hiramatsu 		*arg = ptr + 1;
121336a009feSMasami Hiramatsu 	} else
121436a009feSMasami Hiramatsu 		pev->group = NULL;
121542bba263SMasami Hiramatsu 	if (!pev->sdt && !is_c_func_name(*arg)) {
121636a009feSMasami Hiramatsu ng_name:
121736a009feSMasami Hiramatsu 		semantic_error("%s is bad for event name -it must "
121836a009feSMasami Hiramatsu 			       "follow C symbol-naming rule.\n", *arg);
121936a009feSMasami Hiramatsu 		return -EINVAL;
122036a009feSMasami Hiramatsu 	}
122136a009feSMasami Hiramatsu 	pev->event = strdup(*arg);
122236a009feSMasami Hiramatsu 	if (pev->event == NULL)
122336a009feSMasami Hiramatsu 		return -ENOMEM;
122436a009feSMasami Hiramatsu 
122536a009feSMasami Hiramatsu 	return 0;
122636a009feSMasami Hiramatsu }
122736a009feSMasami Hiramatsu 
122850656eecSMasami Hiramatsu /* Parse probepoint definition. */
1229146a1439SMasami Hiramatsu static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
123050656eecSMasami Hiramatsu {
12314235b045SMasami Hiramatsu 	struct perf_probe_point *pp = &pev->point;
123250656eecSMasami Hiramatsu 	char *ptr, *tmp;
123350656eecSMasami Hiramatsu 	char c, nc = 0;
12343099c026SNaveen N. Rao 	bool file_spec = false;
123536a009feSMasami Hiramatsu 	int ret;
123636a009feSMasami Hiramatsu 
123750656eecSMasami Hiramatsu 	/*
123850656eecSMasami Hiramatsu 	 * <Syntax>
12398d993d96SMasami Hiramatsu 	 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
12408d993d96SMasami Hiramatsu 	 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
124136a009feSMasami Hiramatsu 	 * perf probe %[GRP:]SDT_EVENT
124250656eecSMasami Hiramatsu 	 */
1243e59d29e8SWang Nan 	if (!arg)
1244e59d29e8SWang Nan 		return -EINVAL;
124550656eecSMasami Hiramatsu 
12467e9fca51SMasami Hiramatsu 	/*
12477e9fca51SMasami Hiramatsu 	 * If the probe point starts with '%',
12487e9fca51SMasami Hiramatsu 	 * or starts with "sdt_" and has a ':' but no '=',
12497e9fca51SMasami Hiramatsu 	 * then it should be a SDT/cached probe point.
12507e9fca51SMasami Hiramatsu 	 */
12517e9fca51SMasami Hiramatsu 	if (arg[0] == '%' ||
12527e9fca51SMasami Hiramatsu 	    (!strncmp(arg, "sdt_", 4) &&
12537e9fca51SMasami Hiramatsu 	     !!strchr(arg, ':') && !strchr(arg, '='))) {
125436a009feSMasami Hiramatsu 		pev->sdt = true;
12557e9fca51SMasami Hiramatsu 		if (arg[0] == '%')
125636a009feSMasami Hiramatsu 			arg++;
125736a009feSMasami Hiramatsu 	}
125836a009feSMasami Hiramatsu 
12592a9c8c36SMasami Hiramatsu 	ptr = strpbrk(arg, ";=@+%");
126036a009feSMasami Hiramatsu 	if (pev->sdt) {
126136a009feSMasami Hiramatsu 		if (ptr) {
1262a598180aSMasami Hiramatsu 			if (*ptr != '@') {
1263a598180aSMasami Hiramatsu 				semantic_error("%s must be an SDT name.\n",
1264a598180aSMasami Hiramatsu 					       arg);
126536a009feSMasami Hiramatsu 				return -EINVAL;
126636a009feSMasami Hiramatsu 			}
1267a598180aSMasami Hiramatsu 			/* This must be a target file name or build id */
1268a598180aSMasami Hiramatsu 			tmp = build_id_cache__complement(ptr + 1);
1269a598180aSMasami Hiramatsu 			if (tmp) {
1270a598180aSMasami Hiramatsu 				pev->target = build_id_cache__origname(tmp);
1271a598180aSMasami Hiramatsu 				free(tmp);
1272a598180aSMasami Hiramatsu 			} else
1273a598180aSMasami Hiramatsu 				pev->target = strdup(ptr + 1);
1274a598180aSMasami Hiramatsu 			if (!pev->target)
1275a598180aSMasami Hiramatsu 				return -ENOMEM;
1276a598180aSMasami Hiramatsu 			*ptr = '\0';
1277a598180aSMasami Hiramatsu 		}
127836a009feSMasami Hiramatsu 		ret = parse_perf_probe_event_name(&arg, pev);
127936a009feSMasami Hiramatsu 		if (ret == 0) {
128036a009feSMasami Hiramatsu 			if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
128136a009feSMasami Hiramatsu 				ret = -errno;
128236a009feSMasami Hiramatsu 		}
128336a009feSMasami Hiramatsu 		return ret;
128436a009feSMasami Hiramatsu 	}
128536a009feSMasami Hiramatsu 
12862a9c8c36SMasami Hiramatsu 	if (ptr && *ptr == '=') {	/* Event name */
1287af663d75SMasami Hiramatsu 		*ptr = '\0';
1288af663d75SMasami Hiramatsu 		tmp = ptr + 1;
128936a009feSMasami Hiramatsu 		ret = parse_perf_probe_event_name(&arg, pev);
129036a009feSMasami Hiramatsu 		if (ret < 0)
129136a009feSMasami Hiramatsu 			return ret;
129236a009feSMasami Hiramatsu 
1293af663d75SMasami Hiramatsu 		arg = tmp;
1294af663d75SMasami Hiramatsu 	}
1295af663d75SMasami Hiramatsu 
12963099c026SNaveen N. Rao 	/*
12973099c026SNaveen N. Rao 	 * Check arg is function or file name and copy it.
12983099c026SNaveen N. Rao 	 *
12993099c026SNaveen N. Rao 	 * We consider arg to be a file spec if and only if it satisfies
13003099c026SNaveen N. Rao 	 * all of the below criteria::
13013099c026SNaveen N. Rao 	 * - it does not include any of "+@%",
13023099c026SNaveen N. Rao 	 * - it includes one of ":;", and
13033099c026SNaveen N. Rao 	 * - it has a period '.' in the name.
13043099c026SNaveen N. Rao 	 *
13053099c026SNaveen N. Rao 	 * Otherwise, we consider arg to be a function specification.
13063099c026SNaveen N. Rao 	 */
13073099c026SNaveen N. Rao 	if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
13083099c026SNaveen N. Rao 		/* This is a file spec if it includes a '.' before ; or : */
13093099c026SNaveen N. Rao 		if (memchr(arg, '.', ptr - arg))
13103099c026SNaveen N. Rao 			file_spec = true;
13113099c026SNaveen N. Rao 	}
13123099c026SNaveen N. Rao 
13132a9c8c36SMasami Hiramatsu 	ptr = strpbrk(arg, ";:+@%");
131450656eecSMasami Hiramatsu 	if (ptr) {
131550656eecSMasami Hiramatsu 		nc = *ptr;
131650656eecSMasami Hiramatsu 		*ptr++ = '\0';
131750656eecSMasami Hiramatsu 	}
131850656eecSMasami Hiramatsu 
13196c6e024fSWang Nan 	if (arg[0] == '\0')
13206c6e024fSWang Nan 		tmp = NULL;
13216c6e024fSWang Nan 	else {
132202b95dadSMasami Hiramatsu 		tmp = strdup(arg);
132302b95dadSMasami Hiramatsu 		if (tmp == NULL)
132402b95dadSMasami Hiramatsu 			return -ENOMEM;
13256c6e024fSWang Nan 	}
132602b95dadSMasami Hiramatsu 
13273099c026SNaveen N. Rao 	if (file_spec)
132802b95dadSMasami Hiramatsu 		pp->file = tmp;
1329da15bd9dSWang Nan 	else {
133002b95dadSMasami Hiramatsu 		pp->function = tmp;
133150656eecSMasami Hiramatsu 
1332da15bd9dSWang Nan 		/*
1333da15bd9dSWang Nan 		 * Keep pp->function even if this is absolute address,
1334da15bd9dSWang Nan 		 * so it can mark whether abs_address is valid.
1335da15bd9dSWang Nan 		 * Which make 'perf probe lib.bin 0x0' possible.
1336da15bd9dSWang Nan 		 *
1337da15bd9dSWang Nan 		 * Note that checking length of tmp is not needed
1338da15bd9dSWang Nan 		 * because when we access tmp[1] we know tmp[0] is '0',
1339da15bd9dSWang Nan 		 * so tmp[1] should always valid (but could be '\0').
1340da15bd9dSWang Nan 		 */
1341da15bd9dSWang Nan 		if (tmp && !strncmp(tmp, "0x", 2)) {
1342da15bd9dSWang Nan 			pp->abs_address = strtoul(pp->function, &tmp, 0);
1343da15bd9dSWang Nan 			if (*tmp != '\0') {
1344da15bd9dSWang Nan 				semantic_error("Invalid absolute address.\n");
1345da15bd9dSWang Nan 				return -EINVAL;
1346da15bd9dSWang Nan 			}
1347da15bd9dSWang Nan 		}
1348da15bd9dSWang Nan 	}
1349da15bd9dSWang Nan 
135050656eecSMasami Hiramatsu 	/* Parse other options */
135150656eecSMasami Hiramatsu 	while (ptr) {
135250656eecSMasami Hiramatsu 		arg = ptr;
135350656eecSMasami Hiramatsu 		c = nc;
13542a9c8c36SMasami Hiramatsu 		if (c == ';') {	/* Lazy pattern must be the last part */
135502b95dadSMasami Hiramatsu 			pp->lazy_line = strdup(arg);
135602b95dadSMasami Hiramatsu 			if (pp->lazy_line == NULL)
135702b95dadSMasami Hiramatsu 				return -ENOMEM;
13582a9c8c36SMasami Hiramatsu 			break;
13592a9c8c36SMasami Hiramatsu 		}
13602a9c8c36SMasami Hiramatsu 		ptr = strpbrk(arg, ";:+@%");
136150656eecSMasami Hiramatsu 		if (ptr) {
136250656eecSMasami Hiramatsu 			nc = *ptr;
136350656eecSMasami Hiramatsu 			*ptr++ = '\0';
136450656eecSMasami Hiramatsu 		}
136550656eecSMasami Hiramatsu 		switch (c) {
136650656eecSMasami Hiramatsu 		case ':':	/* Line number */
136750656eecSMasami Hiramatsu 			pp->line = strtoul(arg, &tmp, 0);
1368146a1439SMasami Hiramatsu 			if (*tmp != '\0') {
13692a9c8c36SMasami Hiramatsu 				semantic_error("There is non-digit char"
1370146a1439SMasami Hiramatsu 					       " in line number.\n");
1371146a1439SMasami Hiramatsu 				return -EINVAL;
1372146a1439SMasami Hiramatsu 			}
137350656eecSMasami Hiramatsu 			break;
137450656eecSMasami Hiramatsu 		case '+':	/* Byte offset from a symbol */
137550656eecSMasami Hiramatsu 			pp->offset = strtoul(arg, &tmp, 0);
1376146a1439SMasami Hiramatsu 			if (*tmp != '\0') {
13772a9c8c36SMasami Hiramatsu 				semantic_error("There is non-digit character"
1378146a1439SMasami Hiramatsu 						" in offset.\n");
1379146a1439SMasami Hiramatsu 				return -EINVAL;
1380146a1439SMasami Hiramatsu 			}
138150656eecSMasami Hiramatsu 			break;
138250656eecSMasami Hiramatsu 		case '@':	/* File name */
1383146a1439SMasami Hiramatsu 			if (pp->file) {
1384146a1439SMasami Hiramatsu 				semantic_error("SRC@SRC is not allowed.\n");
1385146a1439SMasami Hiramatsu 				return -EINVAL;
1386146a1439SMasami Hiramatsu 			}
138702b95dadSMasami Hiramatsu 			pp->file = strdup(arg);
138802b95dadSMasami Hiramatsu 			if (pp->file == NULL)
138902b95dadSMasami Hiramatsu 				return -ENOMEM;
139050656eecSMasami Hiramatsu 			break;
139150656eecSMasami Hiramatsu 		case '%':	/* Probe places */
139250656eecSMasami Hiramatsu 			if (strcmp(arg, "return") == 0) {
139350656eecSMasami Hiramatsu 				pp->retprobe = 1;
1394146a1439SMasami Hiramatsu 			} else {	/* Others not supported yet */
1395146a1439SMasami Hiramatsu 				semantic_error("%%%s is not supported.\n", arg);
1396146a1439SMasami Hiramatsu 				return -ENOTSUP;
1397146a1439SMasami Hiramatsu 			}
139850656eecSMasami Hiramatsu 			break;
1399146a1439SMasami Hiramatsu 		default:	/* Buggy case */
1400146a1439SMasami Hiramatsu 			pr_err("This program has a bug at %s:%d.\n",
1401146a1439SMasami Hiramatsu 				__FILE__, __LINE__);
1402146a1439SMasami Hiramatsu 			return -ENOTSUP;
140350656eecSMasami Hiramatsu 			break;
140450656eecSMasami Hiramatsu 		}
140550656eecSMasami Hiramatsu 	}
140650656eecSMasami Hiramatsu 
140750656eecSMasami Hiramatsu 	/* Exclusion check */
1408146a1439SMasami Hiramatsu 	if (pp->lazy_line && pp->line) {
14090e43e5d2SMasami Hiramatsu 		semantic_error("Lazy pattern can't be used with"
14100e43e5d2SMasami Hiramatsu 			       " line number.\n");
1411146a1439SMasami Hiramatsu 		return -EINVAL;
1412146a1439SMasami Hiramatsu 	}
14132a9c8c36SMasami Hiramatsu 
1414146a1439SMasami Hiramatsu 	if (pp->lazy_line && pp->offset) {
14150e43e5d2SMasami Hiramatsu 		semantic_error("Lazy pattern can't be used with offset.\n");
1416146a1439SMasami Hiramatsu 		return -EINVAL;
1417146a1439SMasami Hiramatsu 	}
14182a9c8c36SMasami Hiramatsu 
1419146a1439SMasami Hiramatsu 	if (pp->line && pp->offset) {
14200e43e5d2SMasami Hiramatsu 		semantic_error("Offset can't be used with line number.\n");
1421146a1439SMasami Hiramatsu 		return -EINVAL;
1422146a1439SMasami Hiramatsu 	}
142350656eecSMasami Hiramatsu 
1424146a1439SMasami Hiramatsu 	if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
14252a9c8c36SMasami Hiramatsu 		semantic_error("File always requires line number or "
14260e43e5d2SMasami Hiramatsu 			       "lazy pattern.\n");
1427146a1439SMasami Hiramatsu 		return -EINVAL;
1428146a1439SMasami Hiramatsu 	}
142950656eecSMasami Hiramatsu 
1430146a1439SMasami Hiramatsu 	if (pp->offset && !pp->function) {
14310e43e5d2SMasami Hiramatsu 		semantic_error("Offset requires an entry function.\n");
1432146a1439SMasami Hiramatsu 		return -EINVAL;
1433146a1439SMasami Hiramatsu 	}
143450656eecSMasami Hiramatsu 
1435146a1439SMasami Hiramatsu 	if (pp->retprobe && !pp->function) {
14360e43e5d2SMasami Hiramatsu 		semantic_error("Return probe requires an entry function.\n");
1437146a1439SMasami Hiramatsu 		return -EINVAL;
1438146a1439SMasami Hiramatsu 	}
143950656eecSMasami Hiramatsu 
1440146a1439SMasami Hiramatsu 	if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
14412a9c8c36SMasami Hiramatsu 		semantic_error("Offset/Line/Lazy pattern can't be used with "
14420e43e5d2SMasami Hiramatsu 			       "return probe.\n");
1443146a1439SMasami Hiramatsu 		return -EINVAL;
1444146a1439SMasami Hiramatsu 	}
144550656eecSMasami Hiramatsu 
14464235b045SMasami Hiramatsu 	pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
14472a9c8c36SMasami Hiramatsu 		 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
14482a9c8c36SMasami Hiramatsu 		 pp->lazy_line);
1449146a1439SMasami Hiramatsu 	return 0;
145050656eecSMasami Hiramatsu }
145150656eecSMasami Hiramatsu 
14527df2f329SMasami Hiramatsu /* Parse perf-probe event argument */
1453146a1439SMasami Hiramatsu static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
14547df2f329SMasami Hiramatsu {
1455b2a3c12bSMasami Hiramatsu 	char *tmp, *goodname;
14567df2f329SMasami Hiramatsu 	struct perf_probe_arg_field **fieldp;
14577df2f329SMasami Hiramatsu 
14587df2f329SMasami Hiramatsu 	pr_debug("parsing arg: %s into ", str);
14597df2f329SMasami Hiramatsu 
146048481938SMasami Hiramatsu 	tmp = strchr(str, '=');
146148481938SMasami Hiramatsu 	if (tmp) {
146202b95dadSMasami Hiramatsu 		arg->name = strndup(str, tmp - str);
146302b95dadSMasami Hiramatsu 		if (arg->name == NULL)
146402b95dadSMasami Hiramatsu 			return -ENOMEM;
146511a1ca35SMasami Hiramatsu 		pr_debug("name:%s ", arg->name);
146648481938SMasami Hiramatsu 		str = tmp + 1;
146748481938SMasami Hiramatsu 	}
146848481938SMasami Hiramatsu 
146911a1ca35SMasami Hiramatsu 	tmp = strchr(str, ':');
147011a1ca35SMasami Hiramatsu 	if (tmp) {	/* Type setting */
147111a1ca35SMasami Hiramatsu 		*tmp = '\0';
147202b95dadSMasami Hiramatsu 		arg->type = strdup(tmp + 1);
147302b95dadSMasami Hiramatsu 		if (arg->type == NULL)
147402b95dadSMasami Hiramatsu 			return -ENOMEM;
147511a1ca35SMasami Hiramatsu 		pr_debug("type:%s ", arg->type);
147611a1ca35SMasami Hiramatsu 	}
147711a1ca35SMasami Hiramatsu 
1478b2a3c12bSMasami Hiramatsu 	tmp = strpbrk(str, "-.[");
14797df2f329SMasami Hiramatsu 	if (!is_c_varname(str) || !tmp) {
14807df2f329SMasami Hiramatsu 		/* A variable, register, symbol or special value */
148102b95dadSMasami Hiramatsu 		arg->var = strdup(str);
148202b95dadSMasami Hiramatsu 		if (arg->var == NULL)
148302b95dadSMasami Hiramatsu 			return -ENOMEM;
148448481938SMasami Hiramatsu 		pr_debug("%s\n", arg->var);
1485146a1439SMasami Hiramatsu 		return 0;
14867df2f329SMasami Hiramatsu 	}
14877df2f329SMasami Hiramatsu 
1488b2a3c12bSMasami Hiramatsu 	/* Structure fields or array element */
148902b95dadSMasami Hiramatsu 	arg->var = strndup(str, tmp - str);
149002b95dadSMasami Hiramatsu 	if (arg->var == NULL)
149102b95dadSMasami Hiramatsu 		return -ENOMEM;
1492b2a3c12bSMasami Hiramatsu 	goodname = arg->var;
149348481938SMasami Hiramatsu 	pr_debug("%s, ", arg->var);
14947df2f329SMasami Hiramatsu 	fieldp = &arg->field;
14957df2f329SMasami Hiramatsu 
14967df2f329SMasami Hiramatsu 	do {
1497e334016fSMasami Hiramatsu 		*fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1498e334016fSMasami Hiramatsu 		if (*fieldp == NULL)
1499e334016fSMasami Hiramatsu 			return -ENOMEM;
1500b2a3c12bSMasami Hiramatsu 		if (*tmp == '[') {	/* Array */
1501b2a3c12bSMasami Hiramatsu 			str = tmp;
1502b2a3c12bSMasami Hiramatsu 			(*fieldp)->index = strtol(str + 1, &tmp, 0);
1503b2a3c12bSMasami Hiramatsu 			(*fieldp)->ref = true;
1504b2a3c12bSMasami Hiramatsu 			if (*tmp != ']' || tmp == str + 1) {
1505b2a3c12bSMasami Hiramatsu 				semantic_error("Array index must be a"
1506b2a3c12bSMasami Hiramatsu 						" number.\n");
1507b2a3c12bSMasami Hiramatsu 				return -EINVAL;
1508b2a3c12bSMasami Hiramatsu 			}
1509b2a3c12bSMasami Hiramatsu 			tmp++;
1510b2a3c12bSMasami Hiramatsu 			if (*tmp == '\0')
1511b2a3c12bSMasami Hiramatsu 				tmp = NULL;
1512b2a3c12bSMasami Hiramatsu 		} else {		/* Structure */
15137df2f329SMasami Hiramatsu 			if (*tmp == '.') {
15147df2f329SMasami Hiramatsu 				str = tmp + 1;
15157df2f329SMasami Hiramatsu 				(*fieldp)->ref = false;
15167df2f329SMasami Hiramatsu 			} else if (tmp[1] == '>') {
15177df2f329SMasami Hiramatsu 				str = tmp + 2;
15187df2f329SMasami Hiramatsu 				(*fieldp)->ref = true;
1519146a1439SMasami Hiramatsu 			} else {
1520b2a3c12bSMasami Hiramatsu 				semantic_error("Argument parse error: %s\n",
1521b2a3c12bSMasami Hiramatsu 					       str);
1522146a1439SMasami Hiramatsu 				return -EINVAL;
1523146a1439SMasami Hiramatsu 			}
1524b2a3c12bSMasami Hiramatsu 			tmp = strpbrk(str, "-.[");
1525b2a3c12bSMasami Hiramatsu 		}
15267df2f329SMasami Hiramatsu 		if (tmp) {
152702b95dadSMasami Hiramatsu 			(*fieldp)->name = strndup(str, tmp - str);
152802b95dadSMasami Hiramatsu 			if ((*fieldp)->name == NULL)
152902b95dadSMasami Hiramatsu 				return -ENOMEM;
1530b2a3c12bSMasami Hiramatsu 			if (*str != '[')
1531b2a3c12bSMasami Hiramatsu 				goodname = (*fieldp)->name;
15327df2f329SMasami Hiramatsu 			pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
15337df2f329SMasami Hiramatsu 			fieldp = &(*fieldp)->next;
15347df2f329SMasami Hiramatsu 		}
15357df2f329SMasami Hiramatsu 	} while (tmp);
153602b95dadSMasami Hiramatsu 	(*fieldp)->name = strdup(str);
153702b95dadSMasami Hiramatsu 	if ((*fieldp)->name == NULL)
153802b95dadSMasami Hiramatsu 		return -ENOMEM;
1539b2a3c12bSMasami Hiramatsu 	if (*str != '[')
1540b2a3c12bSMasami Hiramatsu 		goodname = (*fieldp)->name;
15417df2f329SMasami Hiramatsu 	pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1542df0faf4bSMasami Hiramatsu 
1543b2a3c12bSMasami Hiramatsu 	/* If no name is specified, set the last field name (not array index)*/
154402b95dadSMasami Hiramatsu 	if (!arg->name) {
1545b2a3c12bSMasami Hiramatsu 		arg->name = strdup(goodname);
154602b95dadSMasami Hiramatsu 		if (arg->name == NULL)
154702b95dadSMasami Hiramatsu 			return -ENOMEM;
154802b95dadSMasami Hiramatsu 	}
1549146a1439SMasami Hiramatsu 	return 0;
15507df2f329SMasami Hiramatsu }
15517df2f329SMasami Hiramatsu 
15524235b045SMasami Hiramatsu /* Parse perf-probe event command */
1553146a1439SMasami Hiramatsu int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
155450656eecSMasami Hiramatsu {
1555e1c01d61SMasami Hiramatsu 	char **argv;
1556146a1439SMasami Hiramatsu 	int argc, i, ret = 0;
1557fac13fd5SMasami Hiramatsu 
15584235b045SMasami Hiramatsu 	argv = argv_split(cmd, &argc);
1559146a1439SMasami Hiramatsu 	if (!argv) {
1560146a1439SMasami Hiramatsu 		pr_debug("Failed to split arguments.\n");
1561146a1439SMasami Hiramatsu 		return -ENOMEM;
1562146a1439SMasami Hiramatsu 	}
1563146a1439SMasami Hiramatsu 	if (argc - 1 > MAX_PROBE_ARGS) {
1564146a1439SMasami Hiramatsu 		semantic_error("Too many probe arguments (%d).\n", argc - 1);
1565146a1439SMasami Hiramatsu 		ret = -ERANGE;
1566146a1439SMasami Hiramatsu 		goto out;
1567146a1439SMasami Hiramatsu 	}
156850656eecSMasami Hiramatsu 	/* Parse probe point */
1569146a1439SMasami Hiramatsu 	ret = parse_perf_probe_point(argv[0], pev);
1570146a1439SMasami Hiramatsu 	if (ret < 0)
1571146a1439SMasami Hiramatsu 		goto out;
157250656eecSMasami Hiramatsu 
1573e1c01d61SMasami Hiramatsu 	/* Copy arguments and ensure return probe has no C argument */
15744235b045SMasami Hiramatsu 	pev->nargs = argc - 1;
1575e334016fSMasami Hiramatsu 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1576e334016fSMasami Hiramatsu 	if (pev->args == NULL) {
1577e334016fSMasami Hiramatsu 		ret = -ENOMEM;
1578e334016fSMasami Hiramatsu 		goto out;
1579e334016fSMasami Hiramatsu 	}
1580146a1439SMasami Hiramatsu 	for (i = 0; i < pev->nargs && ret >= 0; i++) {
1581146a1439SMasami Hiramatsu 		ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1582146a1439SMasami Hiramatsu 		if (ret >= 0 &&
1583146a1439SMasami Hiramatsu 		    is_c_varname(pev->args[i].var) && pev->point.retprobe) {
15844235b045SMasami Hiramatsu 			semantic_error("You can't specify local variable for"
1585146a1439SMasami Hiramatsu 				       " kretprobe.\n");
1586146a1439SMasami Hiramatsu 			ret = -EINVAL;
1587e1c01d61SMasami Hiramatsu 		}
1588146a1439SMasami Hiramatsu 	}
1589146a1439SMasami Hiramatsu out:
1590e1c01d61SMasami Hiramatsu 	argv_free(argv);
1591146a1439SMasami Hiramatsu 
1592146a1439SMasami Hiramatsu 	return ret;
159350656eecSMasami Hiramatsu }
159450656eecSMasami Hiramatsu 
15954235b045SMasami Hiramatsu /* Return true if this perf_probe_event requires debuginfo */
15964235b045SMasami Hiramatsu bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
15974de189feSMasami Hiramatsu {
15984235b045SMasami Hiramatsu 	int i;
15994235b045SMasami Hiramatsu 
16004235b045SMasami Hiramatsu 	if (pev->point.file || pev->point.line || pev->point.lazy_line)
16014235b045SMasami Hiramatsu 		return true;
16024235b045SMasami Hiramatsu 
16034235b045SMasami Hiramatsu 	for (i = 0; i < pev->nargs; i++)
1604f6eb0518SMasami Hiramatsu 		if (is_c_varname(pev->args[i].var) ||
1605f6eb0518SMasami Hiramatsu 		    !strcmp(pev->args[i].var, "$params") ||
1606f6eb0518SMasami Hiramatsu 		    !strcmp(pev->args[i].var, "$vars"))
16074235b045SMasami Hiramatsu 			return true;
16084235b045SMasami Hiramatsu 
16094235b045SMasami Hiramatsu 	return false;
16104235b045SMasami Hiramatsu }
16114235b045SMasami Hiramatsu 
16120e60836bSSrikar Dronamraju /* Parse probe_events event into struct probe_point */
161392f6c72eSMasami Hiramatsu int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
16144235b045SMasami Hiramatsu {
16150e60836bSSrikar Dronamraju 	struct probe_trace_point *tp = &tev->point;
16164de189feSMasami Hiramatsu 	char pr;
16174de189feSMasami Hiramatsu 	char *p;
1618bcbd0040SIrina Tirdea 	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
16194de189feSMasami Hiramatsu 	int ret, i, argc;
16204de189feSMasami Hiramatsu 	char **argv;
16214de189feSMasami Hiramatsu 
16220e60836bSSrikar Dronamraju 	pr_debug("Parsing probe_events: %s\n", cmd);
16234235b045SMasami Hiramatsu 	argv = argv_split(cmd, &argc);
1624146a1439SMasami Hiramatsu 	if (!argv) {
1625146a1439SMasami Hiramatsu 		pr_debug("Failed to split arguments.\n");
1626146a1439SMasami Hiramatsu 		return -ENOMEM;
1627146a1439SMasami Hiramatsu 	}
1628146a1439SMasami Hiramatsu 	if (argc < 2) {
1629146a1439SMasami Hiramatsu 		semantic_error("Too few probe arguments.\n");
1630146a1439SMasami Hiramatsu 		ret = -ERANGE;
1631146a1439SMasami Hiramatsu 		goto out;
1632146a1439SMasami Hiramatsu 	}
16334de189feSMasami Hiramatsu 
16344de189feSMasami Hiramatsu 	/* Scan event and group name. */
1635bcbd0040SIrina Tirdea 	argv0_str = strdup(argv[0]);
1636bcbd0040SIrina Tirdea 	if (argv0_str == NULL) {
1637bcbd0040SIrina Tirdea 		ret = -ENOMEM;
1638bcbd0040SIrina Tirdea 		goto out;
1639bcbd0040SIrina Tirdea 	}
1640bcbd0040SIrina Tirdea 	fmt1_str = strtok_r(argv0_str, ":", &fmt);
1641bcbd0040SIrina Tirdea 	fmt2_str = strtok_r(NULL, "/", &fmt);
1642bcbd0040SIrina Tirdea 	fmt3_str = strtok_r(NULL, " \t", &fmt);
1643bcbd0040SIrina Tirdea 	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1644bcbd0040SIrina Tirdea 	    || fmt3_str == NULL) {
1645146a1439SMasami Hiramatsu 		semantic_error("Failed to parse event name: %s\n", argv[0]);
1646146a1439SMasami Hiramatsu 		ret = -EINVAL;
1647146a1439SMasami Hiramatsu 		goto out;
1648146a1439SMasami Hiramatsu 	}
1649bcbd0040SIrina Tirdea 	pr = fmt1_str[0];
1650bcbd0040SIrina Tirdea 	tev->group = strdup(fmt2_str);
1651bcbd0040SIrina Tirdea 	tev->event = strdup(fmt3_str);
1652bcbd0040SIrina Tirdea 	if (tev->group == NULL || tev->event == NULL) {
1653bcbd0040SIrina Tirdea 		ret = -ENOMEM;
1654bcbd0040SIrina Tirdea 		goto out;
1655bcbd0040SIrina Tirdea 	}
16564235b045SMasami Hiramatsu 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
16574de189feSMasami Hiramatsu 
16584235b045SMasami Hiramatsu 	tp->retprobe = (pr == 'r');
16594de189feSMasami Hiramatsu 
1660190b57fcSMasami Hiramatsu 	/* Scan module name(if there), function name and offset */
1661190b57fcSMasami Hiramatsu 	p = strchr(argv[1], ':');
1662190b57fcSMasami Hiramatsu 	if (p) {
1663190b57fcSMasami Hiramatsu 		tp->module = strndup(argv[1], p - argv[1]);
1664844faa4bSMasami Hiramatsu 		if (!tp->module) {
1665844faa4bSMasami Hiramatsu 			ret = -ENOMEM;
1666844faa4bSMasami Hiramatsu 			goto out;
1667844faa4bSMasami Hiramatsu 		}
166842bba263SMasami Hiramatsu 		tev->uprobes = (tp->module[0] == '/');
1669190b57fcSMasami Hiramatsu 		p++;
1670190b57fcSMasami Hiramatsu 	} else
1671190b57fcSMasami Hiramatsu 		p = argv[1];
1672bcbd0040SIrina Tirdea 	fmt1_str = strtok_r(p, "+", &fmt);
1673be07afe9SWang Nan 	/* only the address started with 0x */
1674be07afe9SWang Nan 	if (fmt1_str[0] == '0')	{
1675be07afe9SWang Nan 		/*
1676be07afe9SWang Nan 		 * Fix a special case:
1677be07afe9SWang Nan 		 * if address == 0, kernel reports something like:
1678be07afe9SWang Nan 		 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x          (null) arg1=%ax
1679be07afe9SWang Nan 		 * Newer kernel may fix that, but we want to
1680be07afe9SWang Nan 		 * support old kernel also.
1681be07afe9SWang Nan 		 */
1682be07afe9SWang Nan 		if (strcmp(fmt1_str, "0x") == 0) {
1683be07afe9SWang Nan 			if (!argv[2] || strcmp(argv[2], "(null)")) {
1684be07afe9SWang Nan 				ret = -EINVAL;
1685be07afe9SWang Nan 				goto out;
1686be07afe9SWang Nan 			}
1687be07afe9SWang Nan 			tp->address = 0;
1688be07afe9SWang Nan 
1689be07afe9SWang Nan 			free(argv[2]);
1690be07afe9SWang Nan 			for (i = 2; argv[i + 1] != NULL; i++)
1691be07afe9SWang Nan 				argv[i] = argv[i + 1];
1692be07afe9SWang Nan 
1693be07afe9SWang Nan 			argv[i] = NULL;
1694be07afe9SWang Nan 			argc -= 1;
1695be07afe9SWang Nan 		} else
16965a6f6314SMasami Hiramatsu 			tp->address = strtoul(fmt1_str, NULL, 0);
1697be07afe9SWang Nan 	} else {
16985a6f6314SMasami Hiramatsu 		/* Only the symbol-based probe has offset */
1699bcbd0040SIrina Tirdea 		tp->symbol = strdup(fmt1_str);
1700bcbd0040SIrina Tirdea 		if (tp->symbol == NULL) {
1701bcbd0040SIrina Tirdea 			ret = -ENOMEM;
1702bcbd0040SIrina Tirdea 			goto out;
1703bcbd0040SIrina Tirdea 		}
1704bcbd0040SIrina Tirdea 		fmt2_str = strtok_r(NULL, "", &fmt);
1705bcbd0040SIrina Tirdea 		if (fmt2_str == NULL)
17064235b045SMasami Hiramatsu 			tp->offset = 0;
1707bcbd0040SIrina Tirdea 		else
1708bcbd0040SIrina Tirdea 			tp->offset = strtoul(fmt2_str, NULL, 10);
17095a6f6314SMasami Hiramatsu 	}
17104de189feSMasami Hiramatsu 
17114235b045SMasami Hiramatsu 	tev->nargs = argc - 2;
17120e60836bSSrikar Dronamraju 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1713e334016fSMasami Hiramatsu 	if (tev->args == NULL) {
1714e334016fSMasami Hiramatsu 		ret = -ENOMEM;
1715e334016fSMasami Hiramatsu 		goto out;
1716e334016fSMasami Hiramatsu 	}
17174235b045SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++) {
17184de189feSMasami Hiramatsu 		p = strchr(argv[i + 2], '=');
17194de189feSMasami Hiramatsu 		if (p)	/* We don't need which register is assigned. */
17204235b045SMasami Hiramatsu 			*p++ = '\0';
17214235b045SMasami Hiramatsu 		else
17224235b045SMasami Hiramatsu 			p = argv[i + 2];
172302b95dadSMasami Hiramatsu 		tev->args[i].name = strdup(argv[i + 2]);
17244235b045SMasami Hiramatsu 		/* TODO: parse regs and offset */
172502b95dadSMasami Hiramatsu 		tev->args[i].value = strdup(p);
172602b95dadSMasami Hiramatsu 		if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
172702b95dadSMasami Hiramatsu 			ret = -ENOMEM;
172802b95dadSMasami Hiramatsu 			goto out;
172902b95dadSMasami Hiramatsu 		}
17304de189feSMasami Hiramatsu 	}
1731146a1439SMasami Hiramatsu 	ret = 0;
1732146a1439SMasami Hiramatsu out:
1733bcbd0040SIrina Tirdea 	free(argv0_str);
17344de189feSMasami Hiramatsu 	argv_free(argv);
1735146a1439SMasami Hiramatsu 	return ret;
17364de189feSMasami Hiramatsu }
17374de189feSMasami Hiramatsu 
17387df2f329SMasami Hiramatsu /* Compose only probe arg */
1739909b0360SMasami Hiramatsu char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
17407df2f329SMasami Hiramatsu {
17417df2f329SMasami Hiramatsu 	struct perf_probe_arg_field *field = pa->field;
1742909b0360SMasami Hiramatsu 	struct strbuf buf;
1743bf4d5f25SMasami Hiramatsu 	char *ret = NULL;
1744bf4d5f25SMasami Hiramatsu 	int err;
17457df2f329SMasami Hiramatsu 
1746bf4d5f25SMasami Hiramatsu 	if (strbuf_init(&buf, 64) < 0)
1747bf4d5f25SMasami Hiramatsu 		return NULL;
1748bf4d5f25SMasami Hiramatsu 
174948481938SMasami Hiramatsu 	if (pa->name && pa->var)
1750bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
175148481938SMasami Hiramatsu 	else
1752bf4d5f25SMasami Hiramatsu 		err = strbuf_addstr(&buf, pa->name ?: pa->var);
1753bf4d5f25SMasami Hiramatsu 	if (err)
1754bf4d5f25SMasami Hiramatsu 		goto out;
17557df2f329SMasami Hiramatsu 
17567df2f329SMasami Hiramatsu 	while (field) {
1757b2a3c12bSMasami Hiramatsu 		if (field->name[0] == '[')
1758bf4d5f25SMasami Hiramatsu 			err = strbuf_addstr(&buf, field->name);
1759b2a3c12bSMasami Hiramatsu 		else
1760bf4d5f25SMasami Hiramatsu 			err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1761909b0360SMasami Hiramatsu 					  field->name);
17627df2f329SMasami Hiramatsu 		field = field->next;
1763bf4d5f25SMasami Hiramatsu 		if (err)
1764bf4d5f25SMasami Hiramatsu 			goto out;
17657df2f329SMasami Hiramatsu 	}
176611a1ca35SMasami Hiramatsu 
1767909b0360SMasami Hiramatsu 	if (pa->type)
1768bf4d5f25SMasami Hiramatsu 		if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1769bf4d5f25SMasami Hiramatsu 			goto out;
177011a1ca35SMasami Hiramatsu 
1771909b0360SMasami Hiramatsu 	ret = strbuf_detach(&buf, NULL);
1772bf4d5f25SMasami Hiramatsu out:
1773bf4d5f25SMasami Hiramatsu 	strbuf_release(&buf);
1774146a1439SMasami Hiramatsu 	return ret;
17757df2f329SMasami Hiramatsu }
17767df2f329SMasami Hiramatsu 
17774235b045SMasami Hiramatsu /* Compose only probe point (not argument) */
1778c4ff4920SMasami Hiramatsu char *synthesize_perf_probe_point(struct perf_probe_point *pp)
177950656eecSMasami Hiramatsu {
1780909b0360SMasami Hiramatsu 	struct strbuf buf;
1781bf4d5f25SMasami Hiramatsu 	char *tmp, *ret = NULL;
1782bf4d5f25SMasami Hiramatsu 	int len, err = 0;
178350656eecSMasami Hiramatsu 
1784bf4d5f25SMasami Hiramatsu 	if (strbuf_init(&buf, 64) < 0)
1785bf4d5f25SMasami Hiramatsu 		return NULL;
1786bf4d5f25SMasami Hiramatsu 
1787909b0360SMasami Hiramatsu 	if (pp->function) {
1788bf4d5f25SMasami Hiramatsu 		if (strbuf_addstr(&buf, pp->function) < 0)
1789bf4d5f25SMasami Hiramatsu 			goto out;
1790909b0360SMasami Hiramatsu 		if (pp->offset)
1791bf4d5f25SMasami Hiramatsu 			err = strbuf_addf(&buf, "+%lu", pp->offset);
1792909b0360SMasami Hiramatsu 		else if (pp->line)
1793bf4d5f25SMasami Hiramatsu 			err = strbuf_addf(&buf, ":%d", pp->line);
1794909b0360SMasami Hiramatsu 		else if (pp->retprobe)
1795bf4d5f25SMasami Hiramatsu 			err = strbuf_addstr(&buf, "%return");
1796bf4d5f25SMasami Hiramatsu 		if (err)
1797bf4d5f25SMasami Hiramatsu 			goto out;
1798fb1587d8SMasami Hiramatsu 	}
1799fb1587d8SMasami Hiramatsu 	if (pp->file) {
180032ae2adeSFranck Bui-Huu 		tmp = pp->file;
180132ae2adeSFranck Bui-Huu 		len = strlen(tmp);
180232ae2adeSFranck Bui-Huu 		if (len > 30) {
180332ae2adeSFranck Bui-Huu 			tmp = strchr(pp->file + len - 30, '/');
180432ae2adeSFranck Bui-Huu 			tmp = tmp ? tmp + 1 : pp->file + len - 30;
180532ae2adeSFranck Bui-Huu 		}
1806bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(&buf, "@%s", tmp);
1807bf4d5f25SMasami Hiramatsu 		if (!err && !pp->function && pp->line)
1808bf4d5f25SMasami Hiramatsu 			err = strbuf_addf(&buf, ":%d", pp->line);
18094de189feSMasami Hiramatsu 	}
1810bf4d5f25SMasami Hiramatsu 	if (!err)
1811bf4d5f25SMasami Hiramatsu 		ret = strbuf_detach(&buf, NULL);
1812bf4d5f25SMasami Hiramatsu out:
1813bf4d5f25SMasami Hiramatsu 	strbuf_release(&buf);
1814bf4d5f25SMasami Hiramatsu 	return ret;
18154235b045SMasami Hiramatsu }
18164235b045SMasami Hiramatsu 
18174235b045SMasami Hiramatsu char *synthesize_perf_probe_command(struct perf_probe_event *pev)
18184235b045SMasami Hiramatsu {
1819c4ff4920SMasami Hiramatsu 	struct strbuf buf;
1820c4ff4920SMasami Hiramatsu 	char *tmp, *ret = NULL;
1821c4ff4920SMasami Hiramatsu 	int i;
18224235b045SMasami Hiramatsu 
1823c4ff4920SMasami Hiramatsu 	if (strbuf_init(&buf, 64))
18244235b045SMasami Hiramatsu 		return NULL;
1825c4ff4920SMasami Hiramatsu 	if (pev->event)
1826c4ff4920SMasami Hiramatsu 		if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1827c4ff4920SMasami Hiramatsu 				pev->event) < 0)
1828c4ff4920SMasami Hiramatsu 			goto out;
18294235b045SMasami Hiramatsu 
1830c4ff4920SMasami Hiramatsu 	tmp = synthesize_perf_probe_point(&pev->point);
1831c4ff4920SMasami Hiramatsu 	if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1832c4ff4920SMasami Hiramatsu 		goto out;
1833c4ff4920SMasami Hiramatsu 	free(tmp);
1834c4ff4920SMasami Hiramatsu 
18354235b045SMasami Hiramatsu 	for (i = 0; i < pev->nargs; i++) {
1836c4ff4920SMasami Hiramatsu 		tmp = synthesize_perf_probe_arg(pev->args + i);
1837c4ff4920SMasami Hiramatsu 		if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1838c4ff4920SMasami Hiramatsu 			goto out;
1839c4ff4920SMasami Hiramatsu 		free(tmp);
18407ef17aafSMasami Hiramatsu 	}
184150656eecSMasami Hiramatsu 
1842c4ff4920SMasami Hiramatsu 	ret = strbuf_detach(&buf, NULL);
1843c4ff4920SMasami Hiramatsu out:
1844c4ff4920SMasami Hiramatsu 	strbuf_release(&buf);
1845c4ff4920SMasami Hiramatsu 	return ret;
18464235b045SMasami Hiramatsu }
18474235b045SMasami Hiramatsu 
18480e60836bSSrikar Dronamraju static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1849909b0360SMasami Hiramatsu 					    struct strbuf *buf, int depth)
18507ef17aafSMasami Hiramatsu {
1851bf4d5f25SMasami Hiramatsu 	int err;
18524235b045SMasami Hiramatsu 	if (ref->next) {
18530e60836bSSrikar Dronamraju 		depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1854909b0360SMasami Hiramatsu 							 depth + 1);
18554235b045SMasami Hiramatsu 		if (depth < 0)
18564235b045SMasami Hiramatsu 			return depth;
18574235b045SMasami Hiramatsu 	}
1858bf4d5f25SMasami Hiramatsu 	err = strbuf_addf(buf, "%+ld(", ref->offset);
1859bf4d5f25SMasami Hiramatsu 	return (err < 0) ? err : depth;
1860bf4d5f25SMasami Hiramatsu }
18614235b045SMasami Hiramatsu 
18620e60836bSSrikar Dronamraju static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1863909b0360SMasami Hiramatsu 				      struct strbuf *buf)
18644235b045SMasami Hiramatsu {
18650e60836bSSrikar Dronamraju 	struct probe_trace_arg_ref *ref = arg->ref;
1866bf4d5f25SMasami Hiramatsu 	int depth = 0, err;
18674235b045SMasami Hiramatsu 
18684235b045SMasami Hiramatsu 	/* Argument name or separator */
18694235b045SMasami Hiramatsu 	if (arg->name)
1870bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(buf, " %s=", arg->name);
18714235b045SMasami Hiramatsu 	else
1872bf4d5f25SMasami Hiramatsu 		err = strbuf_addch(buf, ' ');
1873bf4d5f25SMasami Hiramatsu 	if (err)
1874bf4d5f25SMasami Hiramatsu 		return err;
18754235b045SMasami Hiramatsu 
1876b7dcb857SMasami Hiramatsu 	/* Special case: @XXX */
1877b7dcb857SMasami Hiramatsu 	if (arg->value[0] == '@' && arg->ref)
1878b7dcb857SMasami Hiramatsu 			ref = ref->next;
1879b7dcb857SMasami Hiramatsu 
18804235b045SMasami Hiramatsu 	/* Dereferencing arguments */
1881b7dcb857SMasami Hiramatsu 	if (ref) {
1882909b0360SMasami Hiramatsu 		depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
18834235b045SMasami Hiramatsu 		if (depth < 0)
18844235b045SMasami Hiramatsu 			return depth;
18854235b045SMasami Hiramatsu 	}
18864235b045SMasami Hiramatsu 
18874235b045SMasami Hiramatsu 	/* Print argument value */
1888b7dcb857SMasami Hiramatsu 	if (arg->value[0] == '@' && arg->ref)
1889bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
1890b7dcb857SMasami Hiramatsu 	else
1891bf4d5f25SMasami Hiramatsu 		err = strbuf_addstr(buf, arg->value);
18924235b045SMasami Hiramatsu 
18934235b045SMasami Hiramatsu 	/* Closing */
1894bf4d5f25SMasami Hiramatsu 	while (!err && depth--)
1895bf4d5f25SMasami Hiramatsu 		err = strbuf_addch(buf, ')');
18964235b045SMasami Hiramatsu 
1897bf4d5f25SMasami Hiramatsu 	/* Print argument type */
1898bf4d5f25SMasami Hiramatsu 	if (!err && arg->type)
1899bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(buf, ":%s", arg->type);
1900bf4d5f25SMasami Hiramatsu 
1901bf4d5f25SMasami Hiramatsu 	return err;
19024235b045SMasami Hiramatsu }
19034235b045SMasami Hiramatsu 
19040e60836bSSrikar Dronamraju char *synthesize_probe_trace_command(struct probe_trace_event *tev)
19054235b045SMasami Hiramatsu {
19060e60836bSSrikar Dronamraju 	struct probe_trace_point *tp = &tev->point;
1907909b0360SMasami Hiramatsu 	struct strbuf buf;
1908909b0360SMasami Hiramatsu 	char *ret = NULL;
1909bf4d5f25SMasami Hiramatsu 	int i, err;
1910eb948e50SMasami Hiramatsu 
1911da15bd9dSWang Nan 	/* Uprobes must have tp->module */
1912da15bd9dSWang Nan 	if (tev->uprobes && !tp->module)
1913909b0360SMasami Hiramatsu 		return NULL;
1914909b0360SMasami Hiramatsu 
1915bf4d5f25SMasami Hiramatsu 	if (strbuf_init(&buf, 32) < 0)
1916bf4d5f25SMasami Hiramatsu 		return NULL;
1917bf4d5f25SMasami Hiramatsu 
1918bf4d5f25SMasami Hiramatsu 	if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1919bf4d5f25SMasami Hiramatsu 			tev->group, tev->event) < 0)
1920bf4d5f25SMasami Hiramatsu 		goto error;
1921da15bd9dSWang Nan 	/*
1922da15bd9dSWang Nan 	 * If tp->address == 0, then this point must be a
1923da15bd9dSWang Nan 	 * absolute address uprobe.
1924da15bd9dSWang Nan 	 * try_to_find_absolute_address() should have made
1925da15bd9dSWang Nan 	 * tp->symbol to "0x0".
1926da15bd9dSWang Nan 	 */
1927da15bd9dSWang Nan 	if (tev->uprobes && !tp->address) {
1928da15bd9dSWang Nan 		if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1929da15bd9dSWang Nan 			goto error;
1930da15bd9dSWang Nan 	}
1931eb948e50SMasami Hiramatsu 
1932eb948e50SMasami Hiramatsu 	/* Use the tp->address for uprobes */
1933225466f1SSrikar Dronamraju 	if (tev->uprobes)
1934bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
1935da15bd9dSWang Nan 	else if (!strncmp(tp->symbol, "0x", 2))
1936da15bd9dSWang Nan 		/* Absolute address. See try_to_find_absolute_address() */
1937bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
1938909b0360SMasami Hiramatsu 				  tp->module ? ":" : "", tp->address);
1939225466f1SSrikar Dronamraju 	else
1940bf4d5f25SMasami Hiramatsu 		err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
1941909b0360SMasami Hiramatsu 				tp->module ? ":" : "", tp->symbol, tp->offset);
1942bf4d5f25SMasami Hiramatsu 	if (err)
1943bf4d5f25SMasami Hiramatsu 		goto error;
1944225466f1SSrikar Dronamraju 
1945909b0360SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++)
1946909b0360SMasami Hiramatsu 		if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
19474235b045SMasami Hiramatsu 			goto error;
19487ef17aafSMasami Hiramatsu 
1949909b0360SMasami Hiramatsu 	ret = strbuf_detach(&buf, NULL);
195050656eecSMasami Hiramatsu error:
1951909b0360SMasami Hiramatsu 	strbuf_release(&buf);
1952909b0360SMasami Hiramatsu 	return ret;
195350656eecSMasami Hiramatsu }
195450656eecSMasami Hiramatsu 
19555a6f6314SMasami Hiramatsu static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
19565a6f6314SMasami Hiramatsu 					  struct perf_probe_point *pp,
19575a6f6314SMasami Hiramatsu 					  bool is_kprobe)
19585a6f6314SMasami Hiramatsu {
19595a6f6314SMasami Hiramatsu 	struct symbol *sym = NULL;
19605a6f6314SMasami Hiramatsu 	struct map *map;
1961e486367fSWang Nan 	u64 addr = tp->address;
19625a6f6314SMasami Hiramatsu 	int ret = -ENOENT;
19635a6f6314SMasami Hiramatsu 
19645a6f6314SMasami Hiramatsu 	if (!is_kprobe) {
19655a6f6314SMasami Hiramatsu 		map = dso__new_map(tp->module);
19665a6f6314SMasami Hiramatsu 		if (!map)
19675a6f6314SMasami Hiramatsu 			goto out;
19685a6f6314SMasami Hiramatsu 		sym = map__find_symbol(map, addr, NULL);
19695a6f6314SMasami Hiramatsu 	} else {
19709b239a12SMasami Hiramatsu 		if (tp->symbol && !addr) {
19710a62f686SMasami Hiramatsu 			if (kernel_get_symbol_address_by_name(tp->symbol,
19720a62f686SMasami Hiramatsu 						&addr, true, false) < 0)
19739b239a12SMasami Hiramatsu 				goto out;
19749b239a12SMasami Hiramatsu 		}
19755a6f6314SMasami Hiramatsu 		if (addr) {
19765a6f6314SMasami Hiramatsu 			addr += tp->offset;
19775a6f6314SMasami Hiramatsu 			sym = __find_kernel_function(addr, &map);
19785a6f6314SMasami Hiramatsu 		}
19795a6f6314SMasami Hiramatsu 	}
198098d3b258SWang Nan 
19815a6f6314SMasami Hiramatsu 	if (!sym)
19825a6f6314SMasami Hiramatsu 		goto out;
19835a6f6314SMasami Hiramatsu 
19845a6f6314SMasami Hiramatsu 	pp->retprobe = tp->retprobe;
19855a6f6314SMasami Hiramatsu 	pp->offset = addr - map->unmap_ip(map, sym->start);
19865a6f6314SMasami Hiramatsu 	pp->function = strdup(sym->name);
19875a6f6314SMasami Hiramatsu 	ret = pp->function ? 0 : -ENOMEM;
19885a6f6314SMasami Hiramatsu 
19895a6f6314SMasami Hiramatsu out:
19905a6f6314SMasami Hiramatsu 	if (map && !is_kprobe) {
199184c2cafaSArnaldo Carvalho de Melo 		map__put(map);
19925a6f6314SMasami Hiramatsu 	}
19935a6f6314SMasami Hiramatsu 
19945a6f6314SMasami Hiramatsu 	return ret;
19955a6f6314SMasami Hiramatsu }
19965a6f6314SMasami Hiramatsu 
19975a6f6314SMasami Hiramatsu static int convert_to_perf_probe_point(struct probe_trace_point *tp,
19985a6f6314SMasami Hiramatsu 				       struct perf_probe_point *pp,
19995a6f6314SMasami Hiramatsu 				       bool is_kprobe)
20005a6f6314SMasami Hiramatsu {
20015a6f6314SMasami Hiramatsu 	char buf[128];
20025a6f6314SMasami Hiramatsu 	int ret;
20035a6f6314SMasami Hiramatsu 
20045a6f6314SMasami Hiramatsu 	ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
20055a6f6314SMasami Hiramatsu 	if (!ret)
20065a6f6314SMasami Hiramatsu 		return 0;
20075a6f6314SMasami Hiramatsu 	ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
20085a6f6314SMasami Hiramatsu 	if (!ret)
20095a6f6314SMasami Hiramatsu 		return 0;
20105a6f6314SMasami Hiramatsu 
20115a6f6314SMasami Hiramatsu 	pr_debug("Failed to find probe point from both of dwarf and map.\n");
20125a6f6314SMasami Hiramatsu 
20135a6f6314SMasami Hiramatsu 	if (tp->symbol) {
20145a6f6314SMasami Hiramatsu 		pp->function = strdup(tp->symbol);
20155a6f6314SMasami Hiramatsu 		pp->offset = tp->offset;
2016614e2fdbSWang Nan 	} else {
20175a6f6314SMasami Hiramatsu 		ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
20185a6f6314SMasami Hiramatsu 		if (ret < 0)
20195a6f6314SMasami Hiramatsu 			return ret;
20205a6f6314SMasami Hiramatsu 		pp->function = strdup(buf);
20215a6f6314SMasami Hiramatsu 		pp->offset = 0;
20225a6f6314SMasami Hiramatsu 	}
20235a6f6314SMasami Hiramatsu 	if (pp->function == NULL)
20245a6f6314SMasami Hiramatsu 		return -ENOMEM;
20255a6f6314SMasami Hiramatsu 
20265a6f6314SMasami Hiramatsu 	pp->retprobe = tp->retprobe;
20275a6f6314SMasami Hiramatsu 
20285a6f6314SMasami Hiramatsu 	return 0;
20295a6f6314SMasami Hiramatsu }
20305a6f6314SMasami Hiramatsu 
20310e60836bSSrikar Dronamraju static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2032225466f1SSrikar Dronamraju 			       struct perf_probe_event *pev, bool is_kprobe)
20334de189feSMasami Hiramatsu {
2034909b0360SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
2035146a1439SMasami Hiramatsu 	int i, ret;
20364de189feSMasami Hiramatsu 
20374b4da7f7SMasami Hiramatsu 	/* Convert event/group name */
203802b95dadSMasami Hiramatsu 	pev->event = strdup(tev->event);
203902b95dadSMasami Hiramatsu 	pev->group = strdup(tev->group);
204002b95dadSMasami Hiramatsu 	if (pev->event == NULL || pev->group == NULL)
204102b95dadSMasami Hiramatsu 		return -ENOMEM;
2042fb1587d8SMasami Hiramatsu 
20434b4da7f7SMasami Hiramatsu 	/* Convert trace_point to probe_point */
20445a6f6314SMasami Hiramatsu 	ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2045146a1439SMasami Hiramatsu 	if (ret < 0)
2046146a1439SMasami Hiramatsu 		return ret;
20474b4da7f7SMasami Hiramatsu 
20484235b045SMasami Hiramatsu 	/* Convert trace_arg to probe_arg */
20494235b045SMasami Hiramatsu 	pev->nargs = tev->nargs;
2050e334016fSMasami Hiramatsu 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2051e334016fSMasami Hiramatsu 	if (pev->args == NULL)
2052e334016fSMasami Hiramatsu 		return -ENOMEM;
205302b95dadSMasami Hiramatsu 	for (i = 0; i < tev->nargs && ret >= 0; i++) {
20544235b045SMasami Hiramatsu 		if (tev->args[i].name)
205502b95dadSMasami Hiramatsu 			pev->args[i].name = strdup(tev->args[i].name);
20564235b045SMasami Hiramatsu 		else {
2057bf4d5f25SMasami Hiramatsu 			if ((ret = strbuf_init(&buf, 32)) < 0)
2058bf4d5f25SMasami Hiramatsu 				goto error;
2059909b0360SMasami Hiramatsu 			ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2060909b0360SMasami Hiramatsu 			pev->args[i].name = strbuf_detach(&buf, NULL);
206102b95dadSMasami Hiramatsu 		}
206202b95dadSMasami Hiramatsu 		if (pev->args[i].name == NULL && ret >= 0)
206302b95dadSMasami Hiramatsu 			ret = -ENOMEM;
20644de189feSMasami Hiramatsu 	}
2065bf4d5f25SMasami Hiramatsu error:
2066146a1439SMasami Hiramatsu 	if (ret < 0)
2067146a1439SMasami Hiramatsu 		clear_perf_probe_event(pev);
2068146a1439SMasami Hiramatsu 
2069146a1439SMasami Hiramatsu 	return ret;
20704235b045SMasami Hiramatsu }
20714de189feSMasami Hiramatsu 
20724235b045SMasami Hiramatsu void clear_perf_probe_event(struct perf_probe_event *pev)
20734235b045SMasami Hiramatsu {
20747df2f329SMasami Hiramatsu 	struct perf_probe_arg_field *field, *next;
20754235b045SMasami Hiramatsu 	int i;
20764de189feSMasami Hiramatsu 
20774235b045SMasami Hiramatsu 	free(pev->event);
20784235b045SMasami Hiramatsu 	free(pev->group);
20797afb3fabSMasami Hiramatsu 	free(pev->target);
20809b118acaSMasami Hiramatsu 	clear_perf_probe_point(&pev->point);
2081f5385650SArnaldo Carvalho de Melo 
20827df2f329SMasami Hiramatsu 	for (i = 0; i < pev->nargs; i++) {
20834235b045SMasami Hiramatsu 		free(pev->args[i].name);
208448481938SMasami Hiramatsu 		free(pev->args[i].var);
208511a1ca35SMasami Hiramatsu 		free(pev->args[i].type);
20867df2f329SMasami Hiramatsu 		field = pev->args[i].field;
20877df2f329SMasami Hiramatsu 		while (field) {
20887df2f329SMasami Hiramatsu 			next = field->next;
208974cf249dSArnaldo Carvalho de Melo 			zfree(&field->name);
20907df2f329SMasami Hiramatsu 			free(field);
20917df2f329SMasami Hiramatsu 			field = next;
20927df2f329SMasami Hiramatsu 		}
20937df2f329SMasami Hiramatsu 	}
20944235b045SMasami Hiramatsu 	free(pev->args);
20954235b045SMasami Hiramatsu 	memset(pev, 0, sizeof(*pev));
20964235b045SMasami Hiramatsu }
20974235b045SMasami Hiramatsu 
20980542bb9cSMasami Hiramatsu #define strdup_or_goto(str, label)	\
20990542bb9cSMasami Hiramatsu ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
21000542bb9cSMasami Hiramatsu 
21010542bb9cSMasami Hiramatsu static int perf_probe_point__copy(struct perf_probe_point *dst,
21020542bb9cSMasami Hiramatsu 				  struct perf_probe_point *src)
21030542bb9cSMasami Hiramatsu {
21040542bb9cSMasami Hiramatsu 	dst->file = strdup_or_goto(src->file, out_err);
21050542bb9cSMasami Hiramatsu 	dst->function = strdup_or_goto(src->function, out_err);
21060542bb9cSMasami Hiramatsu 	dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
21070542bb9cSMasami Hiramatsu 	dst->line = src->line;
21080542bb9cSMasami Hiramatsu 	dst->retprobe = src->retprobe;
21090542bb9cSMasami Hiramatsu 	dst->offset = src->offset;
21100542bb9cSMasami Hiramatsu 	return 0;
21110542bb9cSMasami Hiramatsu 
21120542bb9cSMasami Hiramatsu out_err:
21130542bb9cSMasami Hiramatsu 	clear_perf_probe_point(dst);
21140542bb9cSMasami Hiramatsu 	return -ENOMEM;
21150542bb9cSMasami Hiramatsu }
21160542bb9cSMasami Hiramatsu 
21170542bb9cSMasami Hiramatsu static int perf_probe_arg__copy(struct perf_probe_arg *dst,
21180542bb9cSMasami Hiramatsu 				struct perf_probe_arg *src)
21190542bb9cSMasami Hiramatsu {
21200542bb9cSMasami Hiramatsu 	struct perf_probe_arg_field *field, **ppfield;
21210542bb9cSMasami Hiramatsu 
21220542bb9cSMasami Hiramatsu 	dst->name = strdup_or_goto(src->name, out_err);
21230542bb9cSMasami Hiramatsu 	dst->var = strdup_or_goto(src->var, out_err);
21240542bb9cSMasami Hiramatsu 	dst->type = strdup_or_goto(src->type, out_err);
21250542bb9cSMasami Hiramatsu 
21260542bb9cSMasami Hiramatsu 	field = src->field;
21270542bb9cSMasami Hiramatsu 	ppfield = &(dst->field);
21280542bb9cSMasami Hiramatsu 	while (field) {
21290542bb9cSMasami Hiramatsu 		*ppfield = zalloc(sizeof(*field));
21300542bb9cSMasami Hiramatsu 		if (!*ppfield)
21310542bb9cSMasami Hiramatsu 			goto out_err;
21320542bb9cSMasami Hiramatsu 		(*ppfield)->name = strdup_or_goto(field->name, out_err);
21330542bb9cSMasami Hiramatsu 		(*ppfield)->index = field->index;
21340542bb9cSMasami Hiramatsu 		(*ppfield)->ref = field->ref;
21350542bb9cSMasami Hiramatsu 		field = field->next;
21360542bb9cSMasami Hiramatsu 		ppfield = &((*ppfield)->next);
21370542bb9cSMasami Hiramatsu 	}
21380542bb9cSMasami Hiramatsu 	return 0;
21390542bb9cSMasami Hiramatsu out_err:
21400542bb9cSMasami Hiramatsu 	return -ENOMEM;
21410542bb9cSMasami Hiramatsu }
21420542bb9cSMasami Hiramatsu 
21430542bb9cSMasami Hiramatsu int perf_probe_event__copy(struct perf_probe_event *dst,
21440542bb9cSMasami Hiramatsu 			   struct perf_probe_event *src)
21450542bb9cSMasami Hiramatsu {
21460542bb9cSMasami Hiramatsu 	int i;
21470542bb9cSMasami Hiramatsu 
21480542bb9cSMasami Hiramatsu 	dst->event = strdup_or_goto(src->event, out_err);
21490542bb9cSMasami Hiramatsu 	dst->group = strdup_or_goto(src->group, out_err);
21500542bb9cSMasami Hiramatsu 	dst->target = strdup_or_goto(src->target, out_err);
21510542bb9cSMasami Hiramatsu 	dst->uprobes = src->uprobes;
21520542bb9cSMasami Hiramatsu 
21530542bb9cSMasami Hiramatsu 	if (perf_probe_point__copy(&dst->point, &src->point) < 0)
21540542bb9cSMasami Hiramatsu 		goto out_err;
21550542bb9cSMasami Hiramatsu 
21560542bb9cSMasami Hiramatsu 	dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
21570542bb9cSMasami Hiramatsu 	if (!dst->args)
21580542bb9cSMasami Hiramatsu 		goto out_err;
21590542bb9cSMasami Hiramatsu 	dst->nargs = src->nargs;
21600542bb9cSMasami Hiramatsu 
21610542bb9cSMasami Hiramatsu 	for (i = 0; i < src->nargs; i++)
21620542bb9cSMasami Hiramatsu 		if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
21630542bb9cSMasami Hiramatsu 			goto out_err;
21640542bb9cSMasami Hiramatsu 	return 0;
21650542bb9cSMasami Hiramatsu 
21660542bb9cSMasami Hiramatsu out_err:
21670542bb9cSMasami Hiramatsu 	clear_perf_probe_event(dst);
21680542bb9cSMasami Hiramatsu 	return -ENOMEM;
21690542bb9cSMasami Hiramatsu }
21700542bb9cSMasami Hiramatsu 
217192f6c72eSMasami Hiramatsu void clear_probe_trace_event(struct probe_trace_event *tev)
21724235b045SMasami Hiramatsu {
21730e60836bSSrikar Dronamraju 	struct probe_trace_arg_ref *ref, *next;
21744235b045SMasami Hiramatsu 	int i;
21754235b045SMasami Hiramatsu 
21764235b045SMasami Hiramatsu 	free(tev->event);
21774235b045SMasami Hiramatsu 	free(tev->group);
21784235b045SMasami Hiramatsu 	free(tev->point.symbol);
21794c859351SMasami Hiramatsu 	free(tev->point.realname);
2180190b57fcSMasami Hiramatsu 	free(tev->point.module);
21814235b045SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++) {
21824235b045SMasami Hiramatsu 		free(tev->args[i].name);
21834235b045SMasami Hiramatsu 		free(tev->args[i].value);
21844984912eSMasami Hiramatsu 		free(tev->args[i].type);
21854235b045SMasami Hiramatsu 		ref = tev->args[i].ref;
21864235b045SMasami Hiramatsu 		while (ref) {
21874235b045SMasami Hiramatsu 			next = ref->next;
21884235b045SMasami Hiramatsu 			free(ref);
21894235b045SMasami Hiramatsu 			ref = next;
21904235b045SMasami Hiramatsu 		}
21914235b045SMasami Hiramatsu 	}
21924235b045SMasami Hiramatsu 	free(tev->args);
21934235b045SMasami Hiramatsu 	memset(tev, 0, sizeof(*tev));
21944de189feSMasami Hiramatsu }
21954de189feSMasami Hiramatsu 
21969aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node {
21979aaf5a5fSMasami Hiramatsu 	struct list_head list;
21989aaf5a5fSMasami Hiramatsu 	unsigned long start;
21999aaf5a5fSMasami Hiramatsu 	unsigned long end;
22009aaf5a5fSMasami Hiramatsu 	char *symbol;
22019aaf5a5fSMasami Hiramatsu };
22029aaf5a5fSMasami Hiramatsu 
22039aaf5a5fSMasami Hiramatsu static void kprobe_blacklist__delete(struct list_head *blacklist)
22049aaf5a5fSMasami Hiramatsu {
22059aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
22069aaf5a5fSMasami Hiramatsu 
22079aaf5a5fSMasami Hiramatsu 	while (!list_empty(blacklist)) {
22089aaf5a5fSMasami Hiramatsu 		node = list_first_entry(blacklist,
22099aaf5a5fSMasami Hiramatsu 					struct kprobe_blacklist_node, list);
22109aaf5a5fSMasami Hiramatsu 		list_del(&node->list);
22119aaf5a5fSMasami Hiramatsu 		free(node->symbol);
22129aaf5a5fSMasami Hiramatsu 		free(node);
22139aaf5a5fSMasami Hiramatsu 	}
22149aaf5a5fSMasami Hiramatsu }
22159aaf5a5fSMasami Hiramatsu 
22169aaf5a5fSMasami Hiramatsu static int kprobe_blacklist__load(struct list_head *blacklist)
22179aaf5a5fSMasami Hiramatsu {
22189aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
22194605eab3SJiri Olsa 	const char *__debugfs = debugfs__mountpoint();
22209aaf5a5fSMasami Hiramatsu 	char buf[PATH_MAX], *p;
22219aaf5a5fSMasami Hiramatsu 	FILE *fp;
22229aaf5a5fSMasami Hiramatsu 	int ret;
22239aaf5a5fSMasami Hiramatsu 
22249aaf5a5fSMasami Hiramatsu 	if (__debugfs == NULL)
22259aaf5a5fSMasami Hiramatsu 		return -ENOTSUP;
22269aaf5a5fSMasami Hiramatsu 
22279aaf5a5fSMasami Hiramatsu 	ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
22289aaf5a5fSMasami Hiramatsu 	if (ret < 0)
22299aaf5a5fSMasami Hiramatsu 		return ret;
22309aaf5a5fSMasami Hiramatsu 
22319aaf5a5fSMasami Hiramatsu 	fp = fopen(buf, "r");
22329aaf5a5fSMasami Hiramatsu 	if (!fp)
22339aaf5a5fSMasami Hiramatsu 		return -errno;
22349aaf5a5fSMasami Hiramatsu 
22359aaf5a5fSMasami Hiramatsu 	ret = 0;
22369aaf5a5fSMasami Hiramatsu 	while (fgets(buf, PATH_MAX, fp)) {
22379aaf5a5fSMasami Hiramatsu 		node = zalloc(sizeof(*node));
22389aaf5a5fSMasami Hiramatsu 		if (!node) {
22399aaf5a5fSMasami Hiramatsu 			ret = -ENOMEM;
22409aaf5a5fSMasami Hiramatsu 			break;
22419aaf5a5fSMasami Hiramatsu 		}
22429aaf5a5fSMasami Hiramatsu 		INIT_LIST_HEAD(&node->list);
22439aaf5a5fSMasami Hiramatsu 		list_add_tail(&node->list, blacklist);
22449aaf5a5fSMasami Hiramatsu 		if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
22459aaf5a5fSMasami Hiramatsu 			ret = -EINVAL;
22469aaf5a5fSMasami Hiramatsu 			break;
22479aaf5a5fSMasami Hiramatsu 		}
22489aaf5a5fSMasami Hiramatsu 		p = strchr(buf, '\t');
22499aaf5a5fSMasami Hiramatsu 		if (p) {
22509aaf5a5fSMasami Hiramatsu 			p++;
22519aaf5a5fSMasami Hiramatsu 			if (p[strlen(p) - 1] == '\n')
22529aaf5a5fSMasami Hiramatsu 				p[strlen(p) - 1] = '\0';
22539aaf5a5fSMasami Hiramatsu 		} else
22549aaf5a5fSMasami Hiramatsu 			p = (char *)"unknown";
22559aaf5a5fSMasami Hiramatsu 		node->symbol = strdup(p);
22569aaf5a5fSMasami Hiramatsu 		if (!node->symbol) {
22579aaf5a5fSMasami Hiramatsu 			ret = -ENOMEM;
22589aaf5a5fSMasami Hiramatsu 			break;
22599aaf5a5fSMasami Hiramatsu 		}
22609aaf5a5fSMasami Hiramatsu 		pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
22619aaf5a5fSMasami Hiramatsu 			  node->start, node->end, node->symbol);
22629aaf5a5fSMasami Hiramatsu 		ret++;
22639aaf5a5fSMasami Hiramatsu 	}
22649aaf5a5fSMasami Hiramatsu 	if (ret < 0)
22659aaf5a5fSMasami Hiramatsu 		kprobe_blacklist__delete(blacklist);
22669aaf5a5fSMasami Hiramatsu 	fclose(fp);
22679aaf5a5fSMasami Hiramatsu 
22689aaf5a5fSMasami Hiramatsu 	return ret;
22699aaf5a5fSMasami Hiramatsu }
22709aaf5a5fSMasami Hiramatsu 
22719aaf5a5fSMasami Hiramatsu static struct kprobe_blacklist_node *
22729aaf5a5fSMasami Hiramatsu kprobe_blacklist__find_by_address(struct list_head *blacklist,
22739aaf5a5fSMasami Hiramatsu 				  unsigned long address)
22749aaf5a5fSMasami Hiramatsu {
22759aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
22769aaf5a5fSMasami Hiramatsu 
22779aaf5a5fSMasami Hiramatsu 	list_for_each_entry(node, blacklist, list) {
22789aaf5a5fSMasami Hiramatsu 		if (node->start <= address && address <= node->end)
22799aaf5a5fSMasami Hiramatsu 			return node;
22809aaf5a5fSMasami Hiramatsu 	}
22819aaf5a5fSMasami Hiramatsu 
22829aaf5a5fSMasami Hiramatsu 	return NULL;
22839aaf5a5fSMasami Hiramatsu }
22849aaf5a5fSMasami Hiramatsu 
2285b031220dSMasami Hiramatsu static LIST_HEAD(kprobe_blacklist);
2286b031220dSMasami Hiramatsu 
2287b031220dSMasami Hiramatsu static void kprobe_blacklist__init(void)
2288b031220dSMasami Hiramatsu {
2289b031220dSMasami Hiramatsu 	if (!list_empty(&kprobe_blacklist))
2290b031220dSMasami Hiramatsu 		return;
2291b031220dSMasami Hiramatsu 
2292b031220dSMasami Hiramatsu 	if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2293b031220dSMasami Hiramatsu 		pr_debug("No kprobe blacklist support, ignored\n");
2294b031220dSMasami Hiramatsu }
2295b031220dSMasami Hiramatsu 
2296b031220dSMasami Hiramatsu static void kprobe_blacklist__release(void)
2297b031220dSMasami Hiramatsu {
2298b031220dSMasami Hiramatsu 	kprobe_blacklist__delete(&kprobe_blacklist);
2299b031220dSMasami Hiramatsu }
2300b031220dSMasami Hiramatsu 
2301b031220dSMasami Hiramatsu static bool kprobe_blacklist__listed(unsigned long address)
2302b031220dSMasami Hiramatsu {
2303b031220dSMasami Hiramatsu 	return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2304b031220dSMasami Hiramatsu }
2305b031220dSMasami Hiramatsu 
2306d350bd57SMasami Hiramatsu static int perf_probe_event__sprintf(const char *group, const char *event,
2307d350bd57SMasami Hiramatsu 				     struct perf_probe_event *pev,
2308ba7ecb02SMasami Hiramatsu 				     const char *module,
2309ba7ecb02SMasami Hiramatsu 				     struct strbuf *result)
2310278498d4SMasami Hiramatsu {
2311bf4d5f25SMasami Hiramatsu 	int i, ret;
2312909b0360SMasami Hiramatsu 	char *buf;
2313909b0360SMasami Hiramatsu 
2314909b0360SMasami Hiramatsu 	if (asprintf(&buf, "%s:%s", group, event) < 0)
2315909b0360SMasami Hiramatsu 		return -errno;
2316bf4d5f25SMasami Hiramatsu 	ret = strbuf_addf(result, "  %-20s (on ", buf);
2317909b0360SMasami Hiramatsu 	free(buf);
2318bf4d5f25SMasami Hiramatsu 	if (ret)
2319bf4d5f25SMasami Hiramatsu 		return ret;
2320278498d4SMasami Hiramatsu 
23214235b045SMasami Hiramatsu 	/* Synthesize only event probe point */
2322909b0360SMasami Hiramatsu 	buf = synthesize_perf_probe_point(&pev->point);
2323909b0360SMasami Hiramatsu 	if (!buf)
2324909b0360SMasami Hiramatsu 		return -ENOMEM;
2325bf4d5f25SMasami Hiramatsu 	ret = strbuf_addstr(result, buf);
2326909b0360SMasami Hiramatsu 	free(buf);
23274235b045SMasami Hiramatsu 
2328bf4d5f25SMasami Hiramatsu 	if (!ret && module)
2329bf4d5f25SMasami Hiramatsu 		ret = strbuf_addf(result, " in %s", module);
2330278498d4SMasami Hiramatsu 
2331bf4d5f25SMasami Hiramatsu 	if (!ret && pev->nargs > 0) {
2332bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(result, " with", 5);
2333bf4d5f25SMasami Hiramatsu 		for (i = 0; !ret && i < pev->nargs; i++) {
2334909b0360SMasami Hiramatsu 			buf = synthesize_perf_probe_arg(&pev->args[i]);
2335909b0360SMasami Hiramatsu 			if (!buf)
2336909b0360SMasami Hiramatsu 				return -ENOMEM;
2337bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(result, " %s", buf);
2338909b0360SMasami Hiramatsu 			free(buf);
23397df2f329SMasami Hiramatsu 		}
2340278498d4SMasami Hiramatsu 	}
2341bf4d5f25SMasami Hiramatsu 	if (!ret)
2342bf4d5f25SMasami Hiramatsu 		ret = strbuf_addch(result, ')');
2343909b0360SMasami Hiramatsu 
2344bf4d5f25SMasami Hiramatsu 	return ret;
2345278498d4SMasami Hiramatsu }
2346278498d4SMasami Hiramatsu 
2347ba7ecb02SMasami Hiramatsu /* Show an event */
2348b02137ccSNamhyung Kim int show_perf_probe_event(const char *group, const char *event,
2349d350bd57SMasami Hiramatsu 			  struct perf_probe_event *pev,
2350ba7ecb02SMasami Hiramatsu 			  const char *module, bool use_stdout)
2351ba7ecb02SMasami Hiramatsu {
2352ba7ecb02SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
2353ba7ecb02SMasami Hiramatsu 	int ret;
2354ba7ecb02SMasami Hiramatsu 
2355d350bd57SMasami Hiramatsu 	ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2356ba7ecb02SMasami Hiramatsu 	if (ret >= 0) {
2357ba7ecb02SMasami Hiramatsu 		if (use_stdout)
2358ba7ecb02SMasami Hiramatsu 			printf("%s\n", buf.buf);
2359ba7ecb02SMasami Hiramatsu 		else
2360ba7ecb02SMasami Hiramatsu 			pr_info("%s\n", buf.buf);
2361ba7ecb02SMasami Hiramatsu 	}
2362ba7ecb02SMasami Hiramatsu 	strbuf_release(&buf);
2363ba7ecb02SMasami Hiramatsu 
2364ba7ecb02SMasami Hiramatsu 	return ret;
2365ba7ecb02SMasami Hiramatsu }
2366ba7ecb02SMasami Hiramatsu 
2367b6a89643SMasami Hiramatsu static bool filter_probe_trace_event(struct probe_trace_event *tev,
2368b6a89643SMasami Hiramatsu 				     struct strfilter *filter)
2369b6a89643SMasami Hiramatsu {
2370b6a89643SMasami Hiramatsu 	char tmp[128];
2371b6a89643SMasami Hiramatsu 
2372b6a89643SMasami Hiramatsu 	/* At first, check the event name itself */
2373b6a89643SMasami Hiramatsu 	if (strfilter__compare(filter, tev->event))
2374b6a89643SMasami Hiramatsu 		return true;
2375b6a89643SMasami Hiramatsu 
2376b6a89643SMasami Hiramatsu 	/* Next, check the combination of name and group */
2377b6a89643SMasami Hiramatsu 	if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2378b6a89643SMasami Hiramatsu 		return false;
2379b6a89643SMasami Hiramatsu 	return strfilter__compare(filter, tmp);
2380b6a89643SMasami Hiramatsu }
2381b6a89643SMasami Hiramatsu 
2382b6a89643SMasami Hiramatsu static int __show_perf_probe_events(int fd, bool is_kprobe,
2383b6a89643SMasami Hiramatsu 				    struct strfilter *filter)
23844de189feSMasami Hiramatsu {
2385225466f1SSrikar Dronamraju 	int ret = 0;
23860e60836bSSrikar Dronamraju 	struct probe_trace_event tev;
23874235b045SMasami Hiramatsu 	struct perf_probe_event pev;
23884de189feSMasami Hiramatsu 	struct strlist *rawlist;
23894de189feSMasami Hiramatsu 	struct str_node *ent;
23904de189feSMasami Hiramatsu 
23914235b045SMasami Hiramatsu 	memset(&tev, 0, sizeof(tev));
23924235b045SMasami Hiramatsu 	memset(&pev, 0, sizeof(pev));
239372041334SMasami Hiramatsu 
239492f6c72eSMasami Hiramatsu 	rawlist = probe_file__get_rawlist(fd);
2395146a1439SMasami Hiramatsu 	if (!rawlist)
23966eb08660SMasami Hiramatsu 		return -ENOMEM;
23974de189feSMasami Hiramatsu 
2398602a1f4dSArnaldo Carvalho de Melo 	strlist__for_each_entry(ent, rawlist) {
23990e60836bSSrikar Dronamraju 		ret = parse_probe_trace_command(ent->s, &tev);
2400146a1439SMasami Hiramatsu 		if (ret >= 0) {
2401b6a89643SMasami Hiramatsu 			if (!filter_probe_trace_event(&tev, filter))
2402b6a89643SMasami Hiramatsu 				goto next;
2403225466f1SSrikar Dronamraju 			ret = convert_to_perf_probe_event(&tev, &pev,
2404225466f1SSrikar Dronamraju 								is_kprobe);
2405ba7ecb02SMasami Hiramatsu 			if (ret < 0)
2406ba7ecb02SMasami Hiramatsu 				goto next;
2407d350bd57SMasami Hiramatsu 			ret = show_perf_probe_event(pev.group, pev.event,
2408d350bd57SMasami Hiramatsu 						    &pev, tev.point.module,
2409ba7ecb02SMasami Hiramatsu 						    true);
2410146a1439SMasami Hiramatsu 		}
2411b6a89643SMasami Hiramatsu next:
24124235b045SMasami Hiramatsu 		clear_perf_probe_event(&pev);
24130e60836bSSrikar Dronamraju 		clear_probe_trace_event(&tev);
2414146a1439SMasami Hiramatsu 		if (ret < 0)
2415146a1439SMasami Hiramatsu 			break;
24164de189feSMasami Hiramatsu 	}
24174de189feSMasami Hiramatsu 	strlist__delete(rawlist);
24187737af01SMasami Hiramatsu 	/* Cleanup cached debuginfo if needed */
24197737af01SMasami Hiramatsu 	debuginfo_cache__exit();
2420146a1439SMasami Hiramatsu 
2421146a1439SMasami Hiramatsu 	return ret;
24224de189feSMasami Hiramatsu }
24234de189feSMasami Hiramatsu 
2424225466f1SSrikar Dronamraju /* List up current perf-probe events */
2425b6a89643SMasami Hiramatsu int show_perf_probe_events(struct strfilter *filter)
2426225466f1SSrikar Dronamraju {
24275e45187cSMasami Hiramatsu 	int kp_fd, up_fd, ret;
2428225466f1SSrikar Dronamraju 
2429225466f1SSrikar Dronamraju 	setup_pager();
2430225466f1SSrikar Dronamraju 
24311f3736c9SMasami Hiramatsu 	if (probe_conf.cache)
24321f3736c9SMasami Hiramatsu 		return probe_cache__show_all_caches(filter);
24331f3736c9SMasami Hiramatsu 
24349bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(false);
2435225466f1SSrikar Dronamraju 	if (ret < 0)
2436225466f1SSrikar Dronamraju 		return ret;
2437225466f1SSrikar Dronamraju 
243892f6c72eSMasami Hiramatsu 	ret = probe_file__open_both(&kp_fd, &up_fd, 0);
24395e45187cSMasami Hiramatsu 	if (ret < 0)
2440225466f1SSrikar Dronamraju 		return ret;
2441225466f1SSrikar Dronamraju 
244292f6c72eSMasami Hiramatsu 	if (kp_fd >= 0)
244392f6c72eSMasami Hiramatsu 		ret = __show_perf_probe_events(kp_fd, true, filter);
244492f6c72eSMasami Hiramatsu 	if (up_fd >= 0 && ret >= 0)
244592f6c72eSMasami Hiramatsu 		ret = __show_perf_probe_events(up_fd, false, filter);
244692f6c72eSMasami Hiramatsu 	if (kp_fd > 0)
244792f6c72eSMasami Hiramatsu 		close(kp_fd);
244892f6c72eSMasami Hiramatsu 	if (up_fd > 0)
244992f6c72eSMasami Hiramatsu 		close(up_fd);
24509bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
2451b498ce1fSMasami Hiramatsu 
2452146a1439SMasami Hiramatsu 	return ret;
2453f4d7da49SMasami Hiramatsu }
245450656eecSMasami Hiramatsu 
2455146a1439SMasami Hiramatsu static int get_new_event_name(char *buf, size_t len, const char *base,
2456d761b08bSMasami Hiramatsu 			      struct strlist *namelist, bool allow_suffix)
2457b498ce1fSMasami Hiramatsu {
2458b498ce1fSMasami Hiramatsu 	int i, ret;
2459663b1151SMasami Hiramatsu 	char *p, *nbase;
246017f88fcdSMasami Hiramatsu 
24613099c026SNaveen N. Rao 	if (*base == '.')
24623099c026SNaveen N. Rao 		base++;
2463663b1151SMasami Hiramatsu 	nbase = strdup(base);
2464663b1151SMasami Hiramatsu 	if (!nbase)
2465663b1151SMasami Hiramatsu 		return -ENOMEM;
24663099c026SNaveen N. Rao 
2467663b1151SMasami Hiramatsu 	/* Cut off the dot suffixes (e.g. .const, .isra)*/
2468663b1151SMasami Hiramatsu 	p = strchr(nbase, '.');
2469663b1151SMasami Hiramatsu 	if (p && p != nbase)
2470663b1151SMasami Hiramatsu 		*p = '\0';
2471663b1151SMasami Hiramatsu 
2472663b1151SMasami Hiramatsu 	/* Try no suffix number */
2473663b1151SMasami Hiramatsu 	ret = e_snprintf(buf, len, "%s", nbase);
2474146a1439SMasami Hiramatsu 	if (ret < 0) {
24755f03cba4SMasami Hiramatsu 		pr_debug("snprintf() failed: %d\n", ret);
2476663b1151SMasami Hiramatsu 		goto out;
2477146a1439SMasami Hiramatsu 	}
247817f88fcdSMasami Hiramatsu 	if (!strlist__has_entry(namelist, buf))
2479663b1151SMasami Hiramatsu 		goto out;
248017f88fcdSMasami Hiramatsu 
2481d761b08bSMasami Hiramatsu 	if (!allow_suffix) {
248203e01f56SWang Nan 		pr_warning("Error: event \"%s\" already exists.\n"
248303e01f56SWang Nan 			   " Hint: Remove existing event by 'perf probe -d'\n"
248403e01f56SWang Nan 			   "       or force duplicates by 'perf probe -f'\n"
248503e01f56SWang Nan 			   "       or set 'force=yes' in BPF source.\n",
248603e01f56SWang Nan 			   buf);
2487663b1151SMasami Hiramatsu 		ret = -EEXIST;
2488663b1151SMasami Hiramatsu 		goto out;
2489d761b08bSMasami Hiramatsu 	}
2490d761b08bSMasami Hiramatsu 
249117f88fcdSMasami Hiramatsu 	/* Try to add suffix */
249217f88fcdSMasami Hiramatsu 	for (i = 1; i < MAX_EVENT_INDEX; i++) {
2493663b1151SMasami Hiramatsu 		ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2494146a1439SMasami Hiramatsu 		if (ret < 0) {
24955f03cba4SMasami Hiramatsu 			pr_debug("snprintf() failed: %d\n", ret);
2496663b1151SMasami Hiramatsu 			goto out;
2497146a1439SMasami Hiramatsu 		}
2498b498ce1fSMasami Hiramatsu 		if (!strlist__has_entry(namelist, buf))
2499b498ce1fSMasami Hiramatsu 			break;
2500b498ce1fSMasami Hiramatsu 	}
2501146a1439SMasami Hiramatsu 	if (i == MAX_EVENT_INDEX) {
2502146a1439SMasami Hiramatsu 		pr_warning("Too many events are on the same function.\n");
2503146a1439SMasami Hiramatsu 		ret = -ERANGE;
2504b498ce1fSMasami Hiramatsu 	}
2505b498ce1fSMasami Hiramatsu 
2506663b1151SMasami Hiramatsu out:
2507663b1151SMasami Hiramatsu 	free(nbase);
2508146a1439SMasami Hiramatsu 	return ret;
2509146a1439SMasami Hiramatsu }
2510146a1439SMasami Hiramatsu 
251179702f61SMasami Hiramatsu /* Warn if the current kernel's uprobe implementation is old */
251279702f61SMasami Hiramatsu static void warn_uprobe_event_compat(struct probe_trace_event *tev)
251379702f61SMasami Hiramatsu {
251479702f61SMasami Hiramatsu 	int i;
251579702f61SMasami Hiramatsu 	char *buf = synthesize_probe_trace_command(tev);
251679702f61SMasami Hiramatsu 
251779702f61SMasami Hiramatsu 	/* Old uprobe event doesn't support memory dereference */
251879702f61SMasami Hiramatsu 	if (!tev->uprobes || tev->nargs == 0 || !buf)
251979702f61SMasami Hiramatsu 		goto out;
252079702f61SMasami Hiramatsu 
252179702f61SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++)
252279702f61SMasami Hiramatsu 		if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
252379702f61SMasami Hiramatsu 			pr_warning("Please upgrade your kernel to at least "
252479702f61SMasami Hiramatsu 				   "3.14 to have access to feature %s\n",
252579702f61SMasami Hiramatsu 				   tev->args[i].value);
252679702f61SMasami Hiramatsu 			break;
252779702f61SMasami Hiramatsu 		}
252879702f61SMasami Hiramatsu out:
252979702f61SMasami Hiramatsu 	free(buf);
253079702f61SMasami Hiramatsu }
253179702f61SMasami Hiramatsu 
2532a3c9de62SMasami Hiramatsu /* Set new name from original perf_probe_event and namelist */
2533a3c9de62SMasami Hiramatsu static int probe_trace_event__set_name(struct probe_trace_event *tev,
2534a3c9de62SMasami Hiramatsu 				       struct perf_probe_event *pev,
2535a3c9de62SMasami Hiramatsu 				       struct strlist *namelist,
2536a3c9de62SMasami Hiramatsu 				       bool allow_suffix)
2537a3c9de62SMasami Hiramatsu {
2538a3c9de62SMasami Hiramatsu 	const char *event, *group;
2539a3c9de62SMasami Hiramatsu 	char buf[64];
2540a3c9de62SMasami Hiramatsu 	int ret;
2541a3c9de62SMasami Hiramatsu 
2542bc062230SMasami Hiramatsu 	/* If probe_event or trace_event already have the name, reuse it */
254342bba263SMasami Hiramatsu 	if (pev->event && !pev->sdt)
2544a3c9de62SMasami Hiramatsu 		event = pev->event;
2545bc062230SMasami Hiramatsu 	else if (tev->event)
2546bc062230SMasami Hiramatsu 		event = tev->event;
2547bc062230SMasami Hiramatsu 	else {
2548bc062230SMasami Hiramatsu 		/* Or generate new one from probe point */
2549da15bd9dSWang Nan 		if (pev->point.function &&
2550da15bd9dSWang Nan 			(strncmp(pev->point.function, "0x", 2) != 0) &&
2551da15bd9dSWang Nan 			!strisglob(pev->point.function))
2552a3c9de62SMasami Hiramatsu 			event = pev->point.function;
2553a3c9de62SMasami Hiramatsu 		else
2554a3c9de62SMasami Hiramatsu 			event = tev->point.realname;
2555bc062230SMasami Hiramatsu 	}
255642bba263SMasami Hiramatsu 	if (pev->group && !pev->sdt)
2557a3c9de62SMasami Hiramatsu 		group = pev->group;
2558bc062230SMasami Hiramatsu 	else if (tev->group)
2559bc062230SMasami Hiramatsu 		group = tev->group;
2560a3c9de62SMasami Hiramatsu 	else
2561a3c9de62SMasami Hiramatsu 		group = PERFPROBE_GROUP;
2562a3c9de62SMasami Hiramatsu 
2563a3c9de62SMasami Hiramatsu 	/* Get an unused new event name */
2564a3c9de62SMasami Hiramatsu 	ret = get_new_event_name(buf, 64, event,
2565a3c9de62SMasami Hiramatsu 				 namelist, allow_suffix);
2566a3c9de62SMasami Hiramatsu 	if (ret < 0)
2567a3c9de62SMasami Hiramatsu 		return ret;
2568a3c9de62SMasami Hiramatsu 
2569a3c9de62SMasami Hiramatsu 	event = buf;
2570a3c9de62SMasami Hiramatsu 
2571a3c9de62SMasami Hiramatsu 	tev->event = strdup(event);
2572a3c9de62SMasami Hiramatsu 	tev->group = strdup(group);
2573a3c9de62SMasami Hiramatsu 	if (tev->event == NULL || tev->group == NULL)
2574a3c9de62SMasami Hiramatsu 		return -ENOMEM;
2575a3c9de62SMasami Hiramatsu 
2576a3c9de62SMasami Hiramatsu 	/* Add added event name to namelist */
2577a3c9de62SMasami Hiramatsu 	strlist__add(namelist, event);
2578a3c9de62SMasami Hiramatsu 	return 0;
2579a3c9de62SMasami Hiramatsu }
2580a3c9de62SMasami Hiramatsu 
25811de7b8bfSMasami Hiramatsu static int __open_probe_file_and_namelist(bool uprobe,
25821de7b8bfSMasami Hiramatsu 					  struct strlist **namelist)
258350656eecSMasami Hiramatsu {
25841de7b8bfSMasami Hiramatsu 	int fd;
258550656eecSMasami Hiramatsu 
25861de7b8bfSMasami Hiramatsu 	fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
258792f6c72eSMasami Hiramatsu 	if (fd < 0)
2588146a1439SMasami Hiramatsu 		return fd;
25895e45187cSMasami Hiramatsu 
2590b498ce1fSMasami Hiramatsu 	/* Get current event names */
25911de7b8bfSMasami Hiramatsu 	*namelist = probe_file__get_namelist(fd);
25921de7b8bfSMasami Hiramatsu 	if (!(*namelist)) {
2593146a1439SMasami Hiramatsu 		pr_debug("Failed to get current event list.\n");
25941de7b8bfSMasami Hiramatsu 		close(fd);
25951de7b8bfSMasami Hiramatsu 		return -ENOMEM;
2596146a1439SMasami Hiramatsu 	}
25971de7b8bfSMasami Hiramatsu 	return fd;
25981de7b8bfSMasami Hiramatsu }
25991de7b8bfSMasami Hiramatsu 
26001de7b8bfSMasami Hiramatsu static int __add_probe_trace_events(struct perf_probe_event *pev,
26011de7b8bfSMasami Hiramatsu 				     struct probe_trace_event *tevs,
26021de7b8bfSMasami Hiramatsu 				     int ntevs, bool allow_suffix)
26031de7b8bfSMasami Hiramatsu {
26041de7b8bfSMasami Hiramatsu 	int i, fd[2] = {-1, -1}, up, ret;
26051de7b8bfSMasami Hiramatsu 	struct probe_trace_event *tev = NULL;
26061de7b8bfSMasami Hiramatsu 	struct probe_cache *cache = NULL;
26071de7b8bfSMasami Hiramatsu 	struct strlist *namelist[2] = {NULL, NULL};
26081de7b8bfSMasami Hiramatsu 
26091de7b8bfSMasami Hiramatsu 	up = pev->uprobes ? 1 : 0;
26101de7b8bfSMasami Hiramatsu 	fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
26111de7b8bfSMasami Hiramatsu 	if (fd[up] < 0)
26121de7b8bfSMasami Hiramatsu 		return fd[up];
261350656eecSMasami Hiramatsu 
2614146a1439SMasami Hiramatsu 	ret = 0;
261502b95dadSMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
26164235b045SMasami Hiramatsu 		tev = &tevs[i];
26171de7b8bfSMasami Hiramatsu 		up = tev->uprobes ? 1 : 0;
26181de7b8bfSMasami Hiramatsu 		if (fd[up] == -1) {	/* Open the kprobe/uprobe_events */
26191de7b8bfSMasami Hiramatsu 			fd[up] = __open_probe_file_and_namelist(up,
26201de7b8bfSMasami Hiramatsu 								&namelist[up]);
26211de7b8bfSMasami Hiramatsu 			if (fd[up] < 0)
26221de7b8bfSMasami Hiramatsu 				goto close_out;
26231de7b8bfSMasami Hiramatsu 		}
2624b031220dSMasami Hiramatsu 		/* Skip if the symbol is out of .text or blacklisted */
2625bc062230SMasami Hiramatsu 		if (!tev->point.symbol && !pev->uprobes)
26265a51fcd1SMasami Hiramatsu 			continue;
26279aaf5a5fSMasami Hiramatsu 
2628a3c9de62SMasami Hiramatsu 		/* Set new name for tev (and update namelist) */
26291de7b8bfSMasami Hiramatsu 		ret = probe_trace_event__set_name(tev, pev, namelist[up],
2630a3c9de62SMasami Hiramatsu 						  allow_suffix);
2631146a1439SMasami Hiramatsu 		if (ret < 0)
2632146a1439SMasami Hiramatsu 			break;
26334235b045SMasami Hiramatsu 
26341de7b8bfSMasami Hiramatsu 		ret = probe_file__add_event(fd[up], tev);
2635146a1439SMasami Hiramatsu 		if (ret < 0)
2636146a1439SMasami Hiramatsu 			break;
26374235b045SMasami Hiramatsu 
2638d761b08bSMasami Hiramatsu 		/*
2639d761b08bSMasami Hiramatsu 		 * Probes after the first probe which comes from same
2640d761b08bSMasami Hiramatsu 		 * user input are always allowed to add suffix, because
2641d761b08bSMasami Hiramatsu 		 * there might be several addresses corresponding to
2642d761b08bSMasami Hiramatsu 		 * one code line.
2643d761b08bSMasami Hiramatsu 		 */
2644d761b08bSMasami Hiramatsu 		allow_suffix = true;
264550656eecSMasami Hiramatsu 	}
264679702f61SMasami Hiramatsu 	if (ret == -EINVAL && pev->uprobes)
264779702f61SMasami Hiramatsu 		warn_uprobe_event_compat(tev);
26482fd457a3SMasami Hiramatsu 	if (ret == 0 && probe_conf.cache) {
26492fd457a3SMasami Hiramatsu 		cache = probe_cache__new(pev->target);
26502fd457a3SMasami Hiramatsu 		if (!cache ||
26512fd457a3SMasami Hiramatsu 		    probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
26522fd457a3SMasami Hiramatsu 		    probe_cache__commit(cache) < 0)
26532fd457a3SMasami Hiramatsu 			pr_warning("Failed to add event to probe cache\n");
26542fd457a3SMasami Hiramatsu 		probe_cache__delete(cache);
26552fd457a3SMasami Hiramatsu 	}
2656146a1439SMasami Hiramatsu 
2657ae2cb1acSMasami Hiramatsu close_out:
26581de7b8bfSMasami Hiramatsu 	for (up = 0; up < 2; up++) {
26591de7b8bfSMasami Hiramatsu 		strlist__delete(namelist[up]);
26601de7b8bfSMasami Hiramatsu 		if (fd[up] >= 0)
26611de7b8bfSMasami Hiramatsu 			close(fd[up]);
26621de7b8bfSMasami Hiramatsu 	}
2663146a1439SMasami Hiramatsu 	return ret;
266450656eecSMasami Hiramatsu }
2665fa28244dSMasami Hiramatsu 
26666bb536ccSWang Nan static int find_probe_functions(struct map *map, char *name,
26676bb536ccSWang Nan 				struct symbol **syms)
2668eb948e50SMasami Hiramatsu {
2669564c62a4SNamhyung Kim 	int found = 0;
26700a3873a8SArnaldo Carvalho de Melo 	struct symbol *sym;
26714c859351SMasami Hiramatsu 	struct rb_node *tmp;
2672564c62a4SNamhyung Kim 
267375e4a2a6SWang Nan 	if (map__load(map, NULL) < 0)
267475e4a2a6SWang Nan 		return 0;
267575e4a2a6SWang Nan 
26764c859351SMasami Hiramatsu 	map__for_each_symbol(map, sym, tmp) {
26776bb536ccSWang Nan 		if (strglobmatch(sym->name, name)) {
2678564c62a4SNamhyung Kim 			found++;
26796bb536ccSWang Nan 			if (syms && found < probe_conf.max_probes)
26806bb536ccSWang Nan 				syms[found - 1] = sym;
26816bb536ccSWang Nan 		}
2682eb948e50SMasami Hiramatsu 	}
2683564c62a4SNamhyung Kim 
2684564c62a4SNamhyung Kim 	return found;
2685eb948e50SMasami Hiramatsu }
2686eb948e50SMasami Hiramatsu 
26877b6ff0bdSNaveen N. Rao void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
26887b6ff0bdSNaveen N. Rao 				struct probe_trace_event *tev __maybe_unused,
26890b3c2264SNaveen N. Rao 				struct map *map __maybe_unused,
26900b3c2264SNaveen N. Rao 				struct symbol *sym __maybe_unused) { }
26917b6ff0bdSNaveen N. Rao 
2692eb948e50SMasami Hiramatsu /*
2693eb948e50SMasami Hiramatsu  * Find probe function addresses from map.
2694eb948e50SMasami Hiramatsu  * Return an error or the number of found probe_trace_event
2695eb948e50SMasami Hiramatsu  */
2696eb948e50SMasami Hiramatsu static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2697ddb2f58fSMasami Hiramatsu 					    struct probe_trace_event **tevs)
2698eb948e50SMasami Hiramatsu {
2699eb948e50SMasami Hiramatsu 	struct map *map = NULL;
2700eb948e50SMasami Hiramatsu 	struct ref_reloc_sym *reloc_sym = NULL;
2701eb948e50SMasami Hiramatsu 	struct symbol *sym;
27026bb536ccSWang Nan 	struct symbol **syms = NULL;
2703eb948e50SMasami Hiramatsu 	struct probe_trace_event *tev;
2704eb948e50SMasami Hiramatsu 	struct perf_probe_point *pp = &pev->point;
2705eb948e50SMasami Hiramatsu 	struct probe_trace_point *tp;
2706564c62a4SNamhyung Kim 	int num_matched_functions;
2707b031220dSMasami Hiramatsu 	int ret, i, j, skipped = 0;
2708c61fb959SRavi Bangoria 	char *mod_name;
2709eb948e50SMasami Hiramatsu 
271044225521SMasami Hiramatsu 	map = get_target_map(pev->target, pev->uprobes);
2711eb948e50SMasami Hiramatsu 	if (!map) {
2712eb948e50SMasami Hiramatsu 		ret = -EINVAL;
2713eb948e50SMasami Hiramatsu 		goto out;
2714eb948e50SMasami Hiramatsu 	}
2715eb948e50SMasami Hiramatsu 
27166bb536ccSWang Nan 	syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
27176bb536ccSWang Nan 	if (!syms) {
27186bb536ccSWang Nan 		ret = -ENOMEM;
27196bb536ccSWang Nan 		goto out;
27206bb536ccSWang Nan 	}
27216bb536ccSWang Nan 
2722eb948e50SMasami Hiramatsu 	/*
2723eb948e50SMasami Hiramatsu 	 * Load matched symbols: Since the different local symbols may have
2724eb948e50SMasami Hiramatsu 	 * same name but different addresses, this lists all the symbols.
2725eb948e50SMasami Hiramatsu 	 */
27266bb536ccSWang Nan 	num_matched_functions = find_probe_functions(map, pp->function, syms);
2727564c62a4SNamhyung Kim 	if (num_matched_functions == 0) {
2728eb948e50SMasami Hiramatsu 		pr_err("Failed to find symbol %s in %s\n", pp->function,
272944225521SMasami Hiramatsu 			pev->target ? : "kernel");
2730eb948e50SMasami Hiramatsu 		ret = -ENOENT;
2731eb948e50SMasami Hiramatsu 		goto out;
2732ddb2f58fSMasami Hiramatsu 	} else if (num_matched_functions > probe_conf.max_probes) {
2733eb948e50SMasami Hiramatsu 		pr_err("Too many functions matched in %s\n",
273444225521SMasami Hiramatsu 			pev->target ? : "kernel");
2735eb948e50SMasami Hiramatsu 		ret = -E2BIG;
2736eb948e50SMasami Hiramatsu 		goto out;
2737eb948e50SMasami Hiramatsu 	}
2738eb948e50SMasami Hiramatsu 
27391a8ac29cSMasami Hiramatsu 	/* Note that the symbols in the kmodule are not relocated */
27401a8ac29cSMasami Hiramatsu 	if (!pev->uprobes && !pp->retprobe && !pev->target) {
27410560a0c4SHe Kuang 		reloc_sym = kernel_get_ref_reloc_sym();
2742eb948e50SMasami Hiramatsu 		if (!reloc_sym) {
2743eb948e50SMasami Hiramatsu 			pr_warning("Relocated base symbol is not found!\n");
2744eb948e50SMasami Hiramatsu 			ret = -EINVAL;
2745eb948e50SMasami Hiramatsu 			goto out;
2746eb948e50SMasami Hiramatsu 		}
2747eb948e50SMasami Hiramatsu 	}
2748eb948e50SMasami Hiramatsu 
2749eb948e50SMasami Hiramatsu 	/* Setup result trace-probe-events */
2750eb948e50SMasami Hiramatsu 	*tevs = zalloc(sizeof(*tev) * num_matched_functions);
2751eb948e50SMasami Hiramatsu 	if (!*tevs) {
2752eb948e50SMasami Hiramatsu 		ret = -ENOMEM;
2753eb948e50SMasami Hiramatsu 		goto out;
2754eb948e50SMasami Hiramatsu 	}
2755eb948e50SMasami Hiramatsu 
2756eb948e50SMasami Hiramatsu 	ret = 0;
2757564c62a4SNamhyung Kim 
27586bb536ccSWang Nan 	for (j = 0; j < num_matched_functions; j++) {
27596bb536ccSWang Nan 		sym = syms[j];
27606bb536ccSWang Nan 
2761eb948e50SMasami Hiramatsu 		tev = (*tevs) + ret;
2762eb948e50SMasami Hiramatsu 		tp = &tev->point;
2763eb948e50SMasami Hiramatsu 		if (ret == num_matched_functions) {
2764eb948e50SMasami Hiramatsu 			pr_warning("Too many symbols are listed. Skip it.\n");
2765eb948e50SMasami Hiramatsu 			break;
2766eb948e50SMasami Hiramatsu 		}
2767eb948e50SMasami Hiramatsu 		ret++;
2768eb948e50SMasami Hiramatsu 
2769eb948e50SMasami Hiramatsu 		if (pp->offset > sym->end - sym->start) {
2770eb948e50SMasami Hiramatsu 			pr_warning("Offset %ld is bigger than the size of %s\n",
2771eb948e50SMasami Hiramatsu 				   pp->offset, sym->name);
2772eb948e50SMasami Hiramatsu 			ret = -ENOENT;
2773eb948e50SMasami Hiramatsu 			goto err_out;
2774eb948e50SMasami Hiramatsu 		}
2775eb948e50SMasami Hiramatsu 		/* Add one probe point */
2776eb948e50SMasami Hiramatsu 		tp->address = map->unmap_ip(map, sym->start) + pp->offset;
27771a8ac29cSMasami Hiramatsu 
27781a8ac29cSMasami Hiramatsu 		/* Check the kprobe (not in module) is within .text  */
27791a8ac29cSMasami Hiramatsu 		if (!pev->uprobes && !pev->target &&
2780b031220dSMasami Hiramatsu 		    kprobe_warn_out_range(sym->name, tp->address)) {
2781b031220dSMasami Hiramatsu 			tp->symbol = NULL;	/* Skip it */
2782b031220dSMasami Hiramatsu 			skipped++;
2783b031220dSMasami Hiramatsu 		} else if (reloc_sym) {
2784eb948e50SMasami Hiramatsu 			tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2785eb948e50SMasami Hiramatsu 			tp->offset = tp->address - reloc_sym->addr;
2786eb948e50SMasami Hiramatsu 		} else {
2787eb948e50SMasami Hiramatsu 			tp->symbol = strdup_or_goto(sym->name, nomem_out);
2788eb948e50SMasami Hiramatsu 			tp->offset = pp->offset;
2789eb948e50SMasami Hiramatsu 		}
27906bb536ccSWang Nan 		tp->realname = strdup_or_goto(sym->name, nomem_out);
27916bb536ccSWang Nan 
2792eb948e50SMasami Hiramatsu 		tp->retprobe = pp->retprobe;
2793c61fb959SRavi Bangoria 		if (pev->target) {
2794c61fb959SRavi Bangoria 			if (pev->uprobes) {
279544225521SMasami Hiramatsu 				tev->point.module = strdup_or_goto(pev->target,
279644225521SMasami Hiramatsu 								   nomem_out);
2797c61fb959SRavi Bangoria 			} else {
2798c61fb959SRavi Bangoria 				mod_name = find_module_name(pev->target);
2799c61fb959SRavi Bangoria 				tev->point.module =
2800c61fb959SRavi Bangoria 					strdup(mod_name ? mod_name : pev->target);
2801c61fb959SRavi Bangoria 				free(mod_name);
2802c61fb959SRavi Bangoria 				if (!tev->point.module)
2803c61fb959SRavi Bangoria 					goto nomem_out;
2804c61fb959SRavi Bangoria 			}
2805c61fb959SRavi Bangoria 		}
2806eb948e50SMasami Hiramatsu 		tev->uprobes = pev->uprobes;
2807eb948e50SMasami Hiramatsu 		tev->nargs = pev->nargs;
2808eb948e50SMasami Hiramatsu 		if (tev->nargs) {
2809eb948e50SMasami Hiramatsu 			tev->args = zalloc(sizeof(struct probe_trace_arg) *
2810eb948e50SMasami Hiramatsu 					   tev->nargs);
2811eb948e50SMasami Hiramatsu 			if (tev->args == NULL)
2812eb948e50SMasami Hiramatsu 				goto nomem_out;
2813eb948e50SMasami Hiramatsu 		}
2814eb948e50SMasami Hiramatsu 		for (i = 0; i < tev->nargs; i++) {
2815eb948e50SMasami Hiramatsu 			if (pev->args[i].name)
2816eb948e50SMasami Hiramatsu 				tev->args[i].name =
2817eb948e50SMasami Hiramatsu 					strdup_or_goto(pev->args[i].name,
2818eb948e50SMasami Hiramatsu 							nomem_out);
2819eb948e50SMasami Hiramatsu 
2820eb948e50SMasami Hiramatsu 			tev->args[i].value = strdup_or_goto(pev->args[i].var,
2821eb948e50SMasami Hiramatsu 							    nomem_out);
2822eb948e50SMasami Hiramatsu 			if (pev->args[i].type)
2823eb948e50SMasami Hiramatsu 				tev->args[i].type =
2824eb948e50SMasami Hiramatsu 					strdup_or_goto(pev->args[i].type,
2825eb948e50SMasami Hiramatsu 							nomem_out);
2826eb948e50SMasami Hiramatsu 		}
28270b3c2264SNaveen N. Rao 		arch__fix_tev_from_maps(pev, tev, map, sym);
2828eb948e50SMasami Hiramatsu 	}
2829b031220dSMasami Hiramatsu 	if (ret == skipped) {
2830b031220dSMasami Hiramatsu 		ret = -ENOENT;
2831b031220dSMasami Hiramatsu 		goto err_out;
2832b031220dSMasami Hiramatsu 	}
2833eb948e50SMasami Hiramatsu 
2834eb948e50SMasami Hiramatsu out:
28359b118acaSMasami Hiramatsu 	put_target_map(map, pev->uprobes);
28366bb536ccSWang Nan 	free(syms);
2837eb948e50SMasami Hiramatsu 	return ret;
2838eb948e50SMasami Hiramatsu 
2839eb948e50SMasami Hiramatsu nomem_out:
2840eb948e50SMasami Hiramatsu 	ret = -ENOMEM;
2841eb948e50SMasami Hiramatsu err_out:
2842eb948e50SMasami Hiramatsu 	clear_probe_trace_events(*tevs, num_matched_functions);
2843eb948e50SMasami Hiramatsu 	zfree(tevs);
2844eb948e50SMasami Hiramatsu 	goto out;
2845eb948e50SMasami Hiramatsu }
2846eb948e50SMasami Hiramatsu 
2847da15bd9dSWang Nan static int try_to_find_absolute_address(struct perf_probe_event *pev,
2848da15bd9dSWang Nan 					struct probe_trace_event **tevs)
2849da15bd9dSWang Nan {
2850da15bd9dSWang Nan 	struct perf_probe_point *pp = &pev->point;
2851da15bd9dSWang Nan 	struct probe_trace_event *tev;
2852da15bd9dSWang Nan 	struct probe_trace_point *tp;
2853da15bd9dSWang Nan 	int i, err;
2854da15bd9dSWang Nan 
2855da15bd9dSWang Nan 	if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2856da15bd9dSWang Nan 		return -EINVAL;
2857da15bd9dSWang Nan 	if (perf_probe_event_need_dwarf(pev))
2858da15bd9dSWang Nan 		return -EINVAL;
2859da15bd9dSWang Nan 
2860da15bd9dSWang Nan 	/*
2861da15bd9dSWang Nan 	 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2862da15bd9dSWang Nan 	 * absolute address.
2863da15bd9dSWang Nan 	 *
2864da15bd9dSWang Nan 	 * Only one tev can be generated by this.
2865da15bd9dSWang Nan 	 */
2866da15bd9dSWang Nan 	*tevs = zalloc(sizeof(*tev));
2867da15bd9dSWang Nan 	if (!*tevs)
2868da15bd9dSWang Nan 		return -ENOMEM;
2869da15bd9dSWang Nan 
2870da15bd9dSWang Nan 	tev = *tevs;
2871da15bd9dSWang Nan 	tp = &tev->point;
2872da15bd9dSWang Nan 
2873da15bd9dSWang Nan 	/*
2874da15bd9dSWang Nan 	 * Don't use tp->offset, use address directly, because
2875da15bd9dSWang Nan 	 * in synthesize_probe_trace_command() address cannot be
2876da15bd9dSWang Nan 	 * zero.
2877da15bd9dSWang Nan 	 */
2878da15bd9dSWang Nan 	tp->address = pev->point.abs_address;
2879da15bd9dSWang Nan 	tp->retprobe = pp->retprobe;
2880da15bd9dSWang Nan 	tev->uprobes = pev->uprobes;
2881da15bd9dSWang Nan 
2882da15bd9dSWang Nan 	err = -ENOMEM;
2883da15bd9dSWang Nan 	/*
2884da15bd9dSWang Nan 	 * Give it a '0x' leading symbol name.
2885da15bd9dSWang Nan 	 * In __add_probe_trace_events, a NULL symbol is interpreted as
2886da15bd9dSWang Nan 	 * invalud.
2887da15bd9dSWang Nan 	 */
2888da15bd9dSWang Nan 	if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2889da15bd9dSWang Nan 		goto errout;
2890da15bd9dSWang Nan 
2891da15bd9dSWang Nan 	/* For kprobe, check range */
2892da15bd9dSWang Nan 	if ((!tev->uprobes) &&
2893da15bd9dSWang Nan 	    (kprobe_warn_out_range(tev->point.symbol,
2894da15bd9dSWang Nan 				   tev->point.address))) {
2895da15bd9dSWang Nan 		err = -EACCES;
2896da15bd9dSWang Nan 		goto errout;
2897da15bd9dSWang Nan 	}
2898da15bd9dSWang Nan 
2899da15bd9dSWang Nan 	if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2900da15bd9dSWang Nan 		goto errout;
2901da15bd9dSWang Nan 
2902da15bd9dSWang Nan 	if (pev->target) {
2903da15bd9dSWang Nan 		tp->module = strdup(pev->target);
2904da15bd9dSWang Nan 		if (!tp->module)
2905da15bd9dSWang Nan 			goto errout;
2906da15bd9dSWang Nan 	}
2907da15bd9dSWang Nan 
2908da15bd9dSWang Nan 	if (tev->group) {
2909da15bd9dSWang Nan 		tev->group = strdup(pev->group);
2910da15bd9dSWang Nan 		if (!tev->group)
2911da15bd9dSWang Nan 			goto errout;
2912da15bd9dSWang Nan 	}
2913da15bd9dSWang Nan 
2914da15bd9dSWang Nan 	if (pev->event) {
2915da15bd9dSWang Nan 		tev->event = strdup(pev->event);
2916da15bd9dSWang Nan 		if (!tev->event)
2917da15bd9dSWang Nan 			goto errout;
2918da15bd9dSWang Nan 	}
2919da15bd9dSWang Nan 
2920da15bd9dSWang Nan 	tev->nargs = pev->nargs;
2921da15bd9dSWang Nan 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2922da15bd9dSWang Nan 	if (!tev->args) {
2923da15bd9dSWang Nan 		err = -ENOMEM;
2924da15bd9dSWang Nan 		goto errout;
2925da15bd9dSWang Nan 	}
2926da15bd9dSWang Nan 	for (i = 0; i < tev->nargs; i++)
2927da15bd9dSWang Nan 		copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2928da15bd9dSWang Nan 
2929da15bd9dSWang Nan 	return 1;
2930da15bd9dSWang Nan 
2931da15bd9dSWang Nan errout:
2932da15bd9dSWang Nan 	if (*tevs) {
2933da15bd9dSWang Nan 		clear_probe_trace_events(*tevs, 1);
2934da15bd9dSWang Nan 		*tevs = NULL;
2935da15bd9dSWang Nan 	}
2936da15bd9dSWang Nan 	return err;
2937da15bd9dSWang Nan }
2938da15bd9dSWang Nan 
2939d5c2e2c1SNaveen N. Rao bool __weak arch__prefers_symtab(void) { return false; }
2940d5c2e2c1SNaveen N. Rao 
294142bba263SMasami Hiramatsu /* Concatinate two arrays */
294242bba263SMasami Hiramatsu static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
294342bba263SMasami Hiramatsu {
294442bba263SMasami Hiramatsu 	void *ret;
294542bba263SMasami Hiramatsu 
294642bba263SMasami Hiramatsu 	ret = malloc(sz_a + sz_b);
294742bba263SMasami Hiramatsu 	if (ret) {
294842bba263SMasami Hiramatsu 		memcpy(ret, a, sz_a);
294942bba263SMasami Hiramatsu 		memcpy(ret + sz_a, b, sz_b);
295042bba263SMasami Hiramatsu 	}
295142bba263SMasami Hiramatsu 	return ret;
295242bba263SMasami Hiramatsu }
295342bba263SMasami Hiramatsu 
295442bba263SMasami Hiramatsu static int
295542bba263SMasami Hiramatsu concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
295642bba263SMasami Hiramatsu 			  struct probe_trace_event **tevs2, int ntevs2)
295742bba263SMasami Hiramatsu {
295842bba263SMasami Hiramatsu 	struct probe_trace_event *new_tevs;
295942bba263SMasami Hiramatsu 	int ret = 0;
296042bba263SMasami Hiramatsu 
296142bba263SMasami Hiramatsu 	if (ntevs == 0) {
296242bba263SMasami Hiramatsu 		*tevs = *tevs2;
296342bba263SMasami Hiramatsu 		*ntevs = ntevs2;
296442bba263SMasami Hiramatsu 		*tevs2 = NULL;
296542bba263SMasami Hiramatsu 		return 0;
296642bba263SMasami Hiramatsu 	}
296742bba263SMasami Hiramatsu 
296842bba263SMasami Hiramatsu 	if (*ntevs + ntevs2 > probe_conf.max_probes)
296942bba263SMasami Hiramatsu 		ret = -E2BIG;
297042bba263SMasami Hiramatsu 	else {
297142bba263SMasami Hiramatsu 		/* Concatinate the array of probe_trace_event */
297242bba263SMasami Hiramatsu 		new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
297342bba263SMasami Hiramatsu 				  *tevs2, ntevs2 * sizeof(**tevs2));
297442bba263SMasami Hiramatsu 		if (!new_tevs)
297542bba263SMasami Hiramatsu 			ret = -ENOMEM;
297642bba263SMasami Hiramatsu 		else {
297742bba263SMasami Hiramatsu 			free(*tevs);
297842bba263SMasami Hiramatsu 			*tevs = new_tevs;
297942bba263SMasami Hiramatsu 			*ntevs += ntevs2;
298042bba263SMasami Hiramatsu 		}
298142bba263SMasami Hiramatsu 	}
298242bba263SMasami Hiramatsu 	if (ret < 0)
298342bba263SMasami Hiramatsu 		clear_probe_trace_events(*tevs2, ntevs2);
298442bba263SMasami Hiramatsu 	zfree(tevs2);
298542bba263SMasami Hiramatsu 
298642bba263SMasami Hiramatsu 	return ret;
298742bba263SMasami Hiramatsu }
298842bba263SMasami Hiramatsu 
298942bba263SMasami Hiramatsu /*
299042bba263SMasami Hiramatsu  * Try to find probe_trace_event from given probe caches. Return the number
299142bba263SMasami Hiramatsu  * of cached events found, if an error occurs return the error.
299242bba263SMasami Hiramatsu  */
299342bba263SMasami Hiramatsu static int find_cached_events(struct perf_probe_event *pev,
299442bba263SMasami Hiramatsu 			      struct probe_trace_event **tevs,
299542bba263SMasami Hiramatsu 			      const char *target)
299642bba263SMasami Hiramatsu {
299742bba263SMasami Hiramatsu 	struct probe_cache *cache;
299842bba263SMasami Hiramatsu 	struct probe_cache_entry *entry;
299942bba263SMasami Hiramatsu 	struct probe_trace_event *tmp_tevs = NULL;
300042bba263SMasami Hiramatsu 	int ntevs = 0;
300142bba263SMasami Hiramatsu 	int ret = 0;
300242bba263SMasami Hiramatsu 
300342bba263SMasami Hiramatsu 	cache = probe_cache__new(target);
300442bba263SMasami Hiramatsu 	/* Return 0 ("not found") if the target has no probe cache. */
300542bba263SMasami Hiramatsu 	if (!cache)
300642bba263SMasami Hiramatsu 		return 0;
300742bba263SMasami Hiramatsu 
300842bba263SMasami Hiramatsu 	for_each_probe_cache_entry(entry, cache) {
300942bba263SMasami Hiramatsu 		/* Skip the cache entry which has no name */
301042bba263SMasami Hiramatsu 		if (!entry->pev.event || !entry->pev.group)
301142bba263SMasami Hiramatsu 			continue;
301242bba263SMasami Hiramatsu 		if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
301342bba263SMasami Hiramatsu 		    strglobmatch(entry->pev.event, pev->event)) {
301442bba263SMasami Hiramatsu 			ret = probe_cache_entry__get_event(entry, &tmp_tevs);
301542bba263SMasami Hiramatsu 			if (ret > 0)
301642bba263SMasami Hiramatsu 				ret = concat_probe_trace_events(tevs, &ntevs,
301742bba263SMasami Hiramatsu 								&tmp_tevs, ret);
301842bba263SMasami Hiramatsu 			if (ret < 0)
301942bba263SMasami Hiramatsu 				break;
302042bba263SMasami Hiramatsu 		}
302142bba263SMasami Hiramatsu 	}
302242bba263SMasami Hiramatsu 	probe_cache__delete(cache);
302342bba263SMasami Hiramatsu 	if (ret < 0) {
302442bba263SMasami Hiramatsu 		clear_probe_trace_events(*tevs, ntevs);
302542bba263SMasami Hiramatsu 		zfree(tevs);
302642bba263SMasami Hiramatsu 	} else {
302742bba263SMasami Hiramatsu 		ret = ntevs;
302842bba263SMasami Hiramatsu 		if (ntevs > 0 && target && target[0] == '/')
302942bba263SMasami Hiramatsu 			pev->uprobes = true;
303042bba263SMasami Hiramatsu 	}
303142bba263SMasami Hiramatsu 
303242bba263SMasami Hiramatsu 	return ret;
303342bba263SMasami Hiramatsu }
303442bba263SMasami Hiramatsu 
30351de7b8bfSMasami Hiramatsu /* Try to find probe_trace_event from all probe caches */
30361de7b8bfSMasami Hiramatsu static int find_cached_events_all(struct perf_probe_event *pev,
30371de7b8bfSMasami Hiramatsu 				   struct probe_trace_event **tevs)
30381de7b8bfSMasami Hiramatsu {
30391de7b8bfSMasami Hiramatsu 	struct probe_trace_event *tmp_tevs = NULL;
30401de7b8bfSMasami Hiramatsu 	struct strlist *bidlist;
30411de7b8bfSMasami Hiramatsu 	struct str_node *nd;
30421de7b8bfSMasami Hiramatsu 	char *pathname;
30431de7b8bfSMasami Hiramatsu 	int ntevs = 0;
30441de7b8bfSMasami Hiramatsu 	int ret;
30451de7b8bfSMasami Hiramatsu 
30461de7b8bfSMasami Hiramatsu 	/* Get the buildid list of all valid caches */
30471de7b8bfSMasami Hiramatsu 	bidlist = build_id_cache__list_all(true);
30481de7b8bfSMasami Hiramatsu 	if (!bidlist) {
30491de7b8bfSMasami Hiramatsu 		ret = -errno;
30501de7b8bfSMasami Hiramatsu 		pr_debug("Failed to get buildids: %d\n", ret);
30511de7b8bfSMasami Hiramatsu 		return ret;
30521de7b8bfSMasami Hiramatsu 	}
30531de7b8bfSMasami Hiramatsu 
30541de7b8bfSMasami Hiramatsu 	ret = 0;
30551de7b8bfSMasami Hiramatsu 	strlist__for_each_entry(nd, bidlist) {
30561de7b8bfSMasami Hiramatsu 		pathname = build_id_cache__origname(nd->s);
30571de7b8bfSMasami Hiramatsu 		ret = find_cached_events(pev, &tmp_tevs, pathname);
30581de7b8bfSMasami Hiramatsu 		/* In the case of cnt == 0, we just skip it */
30591de7b8bfSMasami Hiramatsu 		if (ret > 0)
30601de7b8bfSMasami Hiramatsu 			ret = concat_probe_trace_events(tevs, &ntevs,
30611de7b8bfSMasami Hiramatsu 							&tmp_tevs, ret);
30621de7b8bfSMasami Hiramatsu 		free(pathname);
30631de7b8bfSMasami Hiramatsu 		if (ret < 0)
30641de7b8bfSMasami Hiramatsu 			break;
30651de7b8bfSMasami Hiramatsu 	}
30661de7b8bfSMasami Hiramatsu 	strlist__delete(bidlist);
30671de7b8bfSMasami Hiramatsu 
30681de7b8bfSMasami Hiramatsu 	if (ret < 0) {
30691de7b8bfSMasami Hiramatsu 		clear_probe_trace_events(*tevs, ntevs);
30701de7b8bfSMasami Hiramatsu 		zfree(tevs);
30711de7b8bfSMasami Hiramatsu 	} else
30721de7b8bfSMasami Hiramatsu 		ret = ntevs;
30731de7b8bfSMasami Hiramatsu 
30741de7b8bfSMasami Hiramatsu 	return ret;
30751de7b8bfSMasami Hiramatsu }
30761de7b8bfSMasami Hiramatsu 
3077bc062230SMasami Hiramatsu static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3078bc062230SMasami Hiramatsu 					      struct probe_trace_event **tevs)
3079bc062230SMasami Hiramatsu {
3080bc062230SMasami Hiramatsu 	struct probe_cache *cache;
3081bc062230SMasami Hiramatsu 	struct probe_cache_entry *entry;
3082bc062230SMasami Hiramatsu 	struct probe_trace_event *tev;
3083bc062230SMasami Hiramatsu 	struct str_node *node;
3084bc062230SMasami Hiramatsu 	int ret, i;
3085bc062230SMasami Hiramatsu 
30861de7b8bfSMasami Hiramatsu 	if (pev->sdt) {
308742bba263SMasami Hiramatsu 		/* For SDT/cached events, we use special search functions */
30881de7b8bfSMasami Hiramatsu 		if (!pev->target)
30891de7b8bfSMasami Hiramatsu 			return find_cached_events_all(pev, tevs);
30901de7b8bfSMasami Hiramatsu 		else
309142bba263SMasami Hiramatsu 			return find_cached_events(pev, tevs, pev->target);
30921de7b8bfSMasami Hiramatsu 	}
3093bc062230SMasami Hiramatsu 	cache = probe_cache__new(pev->target);
3094bc062230SMasami Hiramatsu 	if (!cache)
3095bc062230SMasami Hiramatsu 		return 0;
3096bc062230SMasami Hiramatsu 
3097bc062230SMasami Hiramatsu 	entry = probe_cache__find(cache, pev);
3098bc062230SMasami Hiramatsu 	if (!entry) {
309936a009feSMasami Hiramatsu 		/* SDT must be in the cache */
310036a009feSMasami Hiramatsu 		ret = pev->sdt ? -ENOENT : 0;
3101bc062230SMasami Hiramatsu 		goto out;
3102bc062230SMasami Hiramatsu 	}
3103bc062230SMasami Hiramatsu 
3104bc062230SMasami Hiramatsu 	ret = strlist__nr_entries(entry->tevlist);
3105bc062230SMasami Hiramatsu 	if (ret > probe_conf.max_probes) {
3106bc062230SMasami Hiramatsu 		pr_debug("Too many entries matched in the cache of %s\n",
3107bc062230SMasami Hiramatsu 			 pev->target ? : "kernel");
3108bc062230SMasami Hiramatsu 		ret = -E2BIG;
3109bc062230SMasami Hiramatsu 		goto out;
3110bc062230SMasami Hiramatsu 	}
3111bc062230SMasami Hiramatsu 
3112bc062230SMasami Hiramatsu 	*tevs = zalloc(ret * sizeof(*tev));
3113bc062230SMasami Hiramatsu 	if (!*tevs) {
3114bc062230SMasami Hiramatsu 		ret = -ENOMEM;
3115bc062230SMasami Hiramatsu 		goto out;
3116bc062230SMasami Hiramatsu 	}
3117bc062230SMasami Hiramatsu 
3118bc062230SMasami Hiramatsu 	i = 0;
3119bc062230SMasami Hiramatsu 	strlist__for_each_entry(node, entry->tevlist) {
3120bc062230SMasami Hiramatsu 		tev = &(*tevs)[i++];
3121bc062230SMasami Hiramatsu 		ret = parse_probe_trace_command(node->s, tev);
3122bc062230SMasami Hiramatsu 		if (ret < 0)
3123bc062230SMasami Hiramatsu 			goto out;
3124bc062230SMasami Hiramatsu 		/* Set the uprobes attribute as same as original */
3125bc062230SMasami Hiramatsu 		tev->uprobes = pev->uprobes;
3126bc062230SMasami Hiramatsu 	}
3127bc062230SMasami Hiramatsu 	ret = i;
3128bc062230SMasami Hiramatsu 
3129bc062230SMasami Hiramatsu out:
3130bc062230SMasami Hiramatsu 	probe_cache__delete(cache);
3131bc062230SMasami Hiramatsu 	return ret;
3132bc062230SMasami Hiramatsu }
3133bc062230SMasami Hiramatsu 
31340e60836bSSrikar Dronamraju static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3135ddb2f58fSMasami Hiramatsu 					 struct probe_trace_event **tevs)
3136e0faa8d3SMasami Hiramatsu {
3137eb948e50SMasami Hiramatsu 	int ret;
31384235b045SMasami Hiramatsu 
313936a009feSMasami Hiramatsu 	if (!pev->group && !pev->sdt) {
31402a12ec13SMasami Hiramatsu 		/* Set group name if not given */
31412a12ec13SMasami Hiramatsu 		if (!pev->uprobes) {
31422a12ec13SMasami Hiramatsu 			pev->group = strdup(PERFPROBE_GROUP);
31432a12ec13SMasami Hiramatsu 			ret = pev->group ? 0 : -ENOMEM;
31442a12ec13SMasami Hiramatsu 		} else
314544225521SMasami Hiramatsu 			ret = convert_exec_to_group(pev->target, &pev->group);
3146fb7345bbSMasami Hiramatsu 		if (ret != 0) {
3147fb7345bbSMasami Hiramatsu 			pr_warning("Failed to make a group name.\n");
3148fb7345bbSMasami Hiramatsu 			return ret;
3149fb7345bbSMasami Hiramatsu 		}
3150fb7345bbSMasami Hiramatsu 	}
3151fb7345bbSMasami Hiramatsu 
3152da15bd9dSWang Nan 	ret = try_to_find_absolute_address(pev, tevs);
3153da15bd9dSWang Nan 	if (ret > 0)
3154da15bd9dSWang Nan 		return ret;
3155da15bd9dSWang Nan 
3156bc062230SMasami Hiramatsu 	/* At first, we need to lookup cache entry */
3157bc062230SMasami Hiramatsu 	ret = find_probe_trace_events_from_cache(pev, tevs);
315836a009feSMasami Hiramatsu 	if (ret > 0 || pev->sdt)	/* SDT can be found only in the cache */
315936a009feSMasami Hiramatsu 		return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3160bc062230SMasami Hiramatsu 
3161d5c2e2c1SNaveen N. Rao 	if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
3162ddb2f58fSMasami Hiramatsu 		ret = find_probe_trace_events_from_map(pev, tevs);
3163d5c2e2c1SNaveen N. Rao 		if (ret > 0)
3164d5c2e2c1SNaveen N. Rao 			return ret; /* Found in symbol table */
3165d5c2e2c1SNaveen N. Rao 	}
3166d5c2e2c1SNaveen N. Rao 
31674b4da7f7SMasami Hiramatsu 	/* Convert perf_probe_event with debuginfo */
3168ddb2f58fSMasami Hiramatsu 	ret = try_to_find_probe_trace_events(pev, tevs);
3169e334016fSMasami Hiramatsu 	if (ret != 0)
3170190b57fcSMasami Hiramatsu 		return ret;	/* Found in debuginfo or got an error */
3171e0faa8d3SMasami Hiramatsu 
3172ddb2f58fSMasami Hiramatsu 	return find_probe_trace_events_from_map(pev, tevs);
31734235b045SMasami Hiramatsu }
31744235b045SMasami Hiramatsu 
317512fae5efSWang Nan int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
31764235b045SMasami Hiramatsu {
3177844dffa5SNamhyung Kim 	int i, ret;
31784235b045SMasami Hiramatsu 
31794235b045SMasami Hiramatsu 	/* Loop 1: convert all events */
31804235b045SMasami Hiramatsu 	for (i = 0; i < npevs; i++) {
3181b031220dSMasami Hiramatsu 		/* Init kprobe blacklist if needed */
318212fae5efSWang Nan 		if (!pevs[i].uprobes)
3183b031220dSMasami Hiramatsu 			kprobe_blacklist__init();
31844235b045SMasami Hiramatsu 		/* Convert with or without debuginfo */
318512fae5efSWang Nan 		ret  = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3186146a1439SMasami Hiramatsu 		if (ret < 0)
3187844dffa5SNamhyung Kim 			return ret;
318812fae5efSWang Nan 		pevs[i].ntevs = ret;
31894235b045SMasami Hiramatsu 	}
3190b031220dSMasami Hiramatsu 	/* This just release blacklist only if allocated */
3191b031220dSMasami Hiramatsu 	kprobe_blacklist__release();
31924235b045SMasami Hiramatsu 
3193844dffa5SNamhyung Kim 	return 0;
3194844dffa5SNamhyung Kim }
3195844dffa5SNamhyung Kim 
319612fae5efSWang Nan int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3197844dffa5SNamhyung Kim {
3198844dffa5SNamhyung Kim 	int i, ret = 0;
3199844dffa5SNamhyung Kim 
32004235b045SMasami Hiramatsu 	/* Loop 2: add all events */
32018635bf6eSArnaldo Carvalho de Melo 	for (i = 0; i < npevs; i++) {
320212fae5efSWang Nan 		ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
320312fae5efSWang Nan 					       pevs[i].ntevs,
3204ddb2f58fSMasami Hiramatsu 					       probe_conf.force_add);
3205fbee632dSArnaldo Carvalho de Melo 		if (ret < 0)
3206fbee632dSArnaldo Carvalho de Melo 			break;
3207fbee632dSArnaldo Carvalho de Melo 	}
3208844dffa5SNamhyung Kim 	return ret;
3209844dffa5SNamhyung Kim }
3210844dffa5SNamhyung Kim 
321112fae5efSWang Nan void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3212844dffa5SNamhyung Kim {
3213844dffa5SNamhyung Kim 	int i, j;
3214844dffa5SNamhyung Kim 
3215449e5b24SMasami Hiramatsu 	/* Loop 3: cleanup and free trace events  */
3216449e5b24SMasami Hiramatsu 	for (i = 0; i < npevs; i++) {
321712fae5efSWang Nan 		for (j = 0; j < pevs[i].ntevs; j++)
321812fae5efSWang Nan 			clear_probe_trace_event(&pevs[i].tevs[j]);
321912fae5efSWang Nan 		zfree(&pevs[i].tevs);
322012fae5efSWang Nan 		pevs[i].ntevs = 0;
3221a43aac29SNamhyung Kim 		clear_perf_probe_event(&pevs[i]);
3222449e5b24SMasami Hiramatsu 	}
3223844dffa5SNamhyung Kim }
3224844dffa5SNamhyung Kim 
3225844dffa5SNamhyung Kim int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3226844dffa5SNamhyung Kim {
3227844dffa5SNamhyung Kim 	int ret;
3228844dffa5SNamhyung Kim 
32299bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(pevs->uprobes);
32309bae1e8cSNamhyung Kim 	if (ret < 0)
32319bae1e8cSNamhyung Kim 		return ret;
32329bae1e8cSNamhyung Kim 
323312fae5efSWang Nan 	ret = convert_perf_probe_events(pevs, npevs);
3234844dffa5SNamhyung Kim 	if (ret == 0)
323512fae5efSWang Nan 		ret = apply_perf_probe_events(pevs, npevs);
3236844dffa5SNamhyung Kim 
323712fae5efSWang Nan 	cleanup_perf_probe_events(pevs, npevs);
3238146a1439SMasami Hiramatsu 
32399bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
3240146a1439SMasami Hiramatsu 	return ret;
3241e0faa8d3SMasami Hiramatsu }
3242e0faa8d3SMasami Hiramatsu 
3243307a464bSMasami Hiramatsu int del_perf_probe_events(struct strfilter *filter)
3244fa28244dSMasami Hiramatsu {
3245307a464bSMasami Hiramatsu 	int ret, ret2, ufd = -1, kfd = -1;
3246307a464bSMasami Hiramatsu 	char *str = strfilter__string(filter);
3247307a464bSMasami Hiramatsu 
3248307a464bSMasami Hiramatsu 	if (!str)
3249307a464bSMasami Hiramatsu 		return -EINVAL;
3250307a464bSMasami Hiramatsu 
3251fa28244dSMasami Hiramatsu 	/* Get current event names */
325292f6c72eSMasami Hiramatsu 	ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
325392f6c72eSMasami Hiramatsu 	if (ret < 0)
325492f6c72eSMasami Hiramatsu 		goto out;
3255225466f1SSrikar Dronamraju 
325692f6c72eSMasami Hiramatsu 	ret = probe_file__del_events(kfd, filter);
3257307a464bSMasami Hiramatsu 	if (ret < 0 && ret != -ENOENT)
3258225466f1SSrikar Dronamraju 		goto error;
3259fa28244dSMasami Hiramatsu 
326092f6c72eSMasami Hiramatsu 	ret2 = probe_file__del_events(ufd, filter);
3261dddc7ee3SMasami Hiramatsu 	if (ret2 < 0 && ret2 != -ENOENT) {
32626dbe31f7SMasami Hiramatsu 		ret = ret2;
3263dddc7ee3SMasami Hiramatsu 		goto error;
3264dddc7ee3SMasami Hiramatsu 	}
3265307a464bSMasami Hiramatsu 	ret = 0;
3266225466f1SSrikar Dronamraju 
3267225466f1SSrikar Dronamraju error:
326892f6c72eSMasami Hiramatsu 	if (kfd >= 0)
3269225466f1SSrikar Dronamraju 		close(kfd);
327092f6c72eSMasami Hiramatsu 	if (ufd >= 0)
3271225466f1SSrikar Dronamraju 		close(ufd);
327292f6c72eSMasami Hiramatsu out:
3273307a464bSMasami Hiramatsu 	free(str);
3274146a1439SMasami Hiramatsu 
3275146a1439SMasami Hiramatsu 	return ret;
3276fa28244dSMasami Hiramatsu }
3277225466f1SSrikar Dronamraju 
32783c42258cSMasami Hiramatsu /* TODO: don't use a global variable for filter ... */
32793c42258cSMasami Hiramatsu static struct strfilter *available_func_filter;
3280fa28244dSMasami Hiramatsu 
3281e80711caSMasami Hiramatsu /*
32823c42258cSMasami Hiramatsu  * If a symbol corresponds to a function with global binding and
32833c42258cSMasami Hiramatsu  * matches filter return 0. For all others return 1.
3284e80711caSMasami Hiramatsu  */
32851d037ca1SIrina Tirdea static int filter_available_functions(struct map *map __maybe_unused,
3286e80711caSMasami Hiramatsu 				      struct symbol *sym)
3287e80711caSMasami Hiramatsu {
3288e578da3bSNamhyung Kim 	if (strfilter__compare(available_func_filter, sym->name))
3289e80711caSMasami Hiramatsu 		return 0;
32903c42258cSMasami Hiramatsu 	return 1;
3291e80711caSMasami Hiramatsu }
3292e80711caSMasami Hiramatsu 
32932df58634SMasami Hiramatsu int show_available_funcs(const char *target, struct strfilter *_filter,
32942df58634SMasami Hiramatsu 					bool user)
3295e80711caSMasami Hiramatsu {
32962df58634SMasami Hiramatsu 	struct map *map;
32972df58634SMasami Hiramatsu 	int ret;
32982df58634SMasami Hiramatsu 
32999bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(user);
33002df58634SMasami Hiramatsu 	if (ret < 0)
33012df58634SMasami Hiramatsu 		return ret;
33022df58634SMasami Hiramatsu 
33032df58634SMasami Hiramatsu 	/* Get a symbol map */
33042df58634SMasami Hiramatsu 	if (user)
33052df58634SMasami Hiramatsu 		map = dso__new_map(target);
33062df58634SMasami Hiramatsu 	else
33072df58634SMasami Hiramatsu 		map = kernel_get_module_map(target);
33082df58634SMasami Hiramatsu 	if (!map) {
33092df58634SMasami Hiramatsu 		pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3310e80711caSMasami Hiramatsu 		return -EINVAL;
3311e80711caSMasami Hiramatsu 	}
33122df58634SMasami Hiramatsu 
33132df58634SMasami Hiramatsu 	/* Load symbols with given filter */
33142df58634SMasami Hiramatsu 	available_func_filter = _filter;
3315e7049342SMasami Hiramatsu 	ret = map__load(map, filter_available_functions);
3316e7049342SMasami Hiramatsu 	if (ret) {
3317e7049342SMasami Hiramatsu 		if (ret == -2) {
3318e7049342SMasami Hiramatsu 			char *str = strfilter__string(_filter);
3319e7049342SMasami Hiramatsu 			pr_err("Failed to find symbols matched to \"%s\"\n",
3320e7049342SMasami Hiramatsu 			       str);
3321e7049342SMasami Hiramatsu 			free(str);
3322e7049342SMasami Hiramatsu 		} else
3323e7049342SMasami Hiramatsu 			pr_err("Failed to load symbols in %s\n",
3324e7049342SMasami Hiramatsu 			       (target) ? : "kernel");
33252df58634SMasami Hiramatsu 		goto end;
33262df58634SMasami Hiramatsu 	}
3327e80711caSMasami Hiramatsu 	if (!dso__sorted_by_name(map->dso, map->type))
3328e80711caSMasami Hiramatsu 		dso__sort_by_name(map->dso, map->type);
3329e80711caSMasami Hiramatsu 
33302df58634SMasami Hiramatsu 	/* Show all (filtered) symbols */
33312df58634SMasami Hiramatsu 	setup_pager();
3332e80711caSMasami Hiramatsu 	dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
33332df58634SMasami Hiramatsu end:
33342df58634SMasami Hiramatsu 	if (user) {
333584c2cafaSArnaldo Carvalho de Melo 		map__put(map);
3336225466f1SSrikar Dronamraju 	}
33379bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
3338225466f1SSrikar Dronamraju 
33392df58634SMasami Hiramatsu 	return ret;
3340225466f1SSrikar Dronamraju }
3341225466f1SSrikar Dronamraju 
3342da15bd9dSWang Nan int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3343da15bd9dSWang Nan 			    struct perf_probe_arg *pvar)
3344da15bd9dSWang Nan {
3345da15bd9dSWang Nan 	tvar->value = strdup(pvar->var);
3346da15bd9dSWang Nan 	if (tvar->value == NULL)
3347da15bd9dSWang Nan 		return -ENOMEM;
3348da15bd9dSWang Nan 	if (pvar->type) {
3349da15bd9dSWang Nan 		tvar->type = strdup(pvar->type);
3350da15bd9dSWang Nan 		if (tvar->type == NULL)
3351da15bd9dSWang Nan 			return -ENOMEM;
3352da15bd9dSWang Nan 	}
3353da15bd9dSWang Nan 	if (pvar->name) {
3354da15bd9dSWang Nan 		tvar->name = strdup(pvar->name);
3355da15bd9dSWang Nan 		if (tvar->name == NULL)
3356da15bd9dSWang Nan 			return -ENOMEM;
3357da15bd9dSWang Nan 	} else
3358da15bd9dSWang Nan 		tvar->name = NULL;
3359da15bd9dSWang Nan 	return 0;
3360da15bd9dSWang Nan }
3361