xref: /linux/tools/perf/util/probe-event.c (revision c61fb959df898b994382d586046d7704476ff503)
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 
704b4da7f7SMasami Hiramatsu static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
71ee45b6c2SMasami Hiramatsu static struct machine *host_machine;
72e0faa8d3SMasami Hiramatsu 
73469b9b88SMasami Hiramatsu /* Initialize symbol maps and path of vmlinux/modules */
749bae1e8cSNamhyung Kim int init_probe_symbol_maps(bool user_only)
75e0faa8d3SMasami Hiramatsu {
76146a1439SMasami Hiramatsu 	int ret;
77146a1439SMasami Hiramatsu 
78e0faa8d3SMasami Hiramatsu 	symbol_conf.sort_by_name = true;
79680d926aSNamhyung Kim 	symbol_conf.allow_aliases = true;
800a7e6d1bSNamhyung Kim 	ret = symbol__init(NULL);
81146a1439SMasami Hiramatsu 	if (ret < 0) {
82146a1439SMasami Hiramatsu 		pr_debug("Failed to init symbol map.\n");
83146a1439SMasami Hiramatsu 		goto out;
84146a1439SMasami Hiramatsu 	}
85e0faa8d3SMasami Hiramatsu 
86ee45b6c2SMasami Hiramatsu 	if (host_machine || user_only)	/* already initialized */
87ee45b6c2SMasami Hiramatsu 		return 0;
88d28c6223SArnaldo Carvalho de Melo 
89ee45b6c2SMasami Hiramatsu 	if (symbol_conf.vmlinux_name)
90ee45b6c2SMasami Hiramatsu 		pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
91ee45b6c2SMasami Hiramatsu 
92ee45b6c2SMasami Hiramatsu 	host_machine = machine__new_host();
93ee45b6c2SMasami Hiramatsu 	if (!host_machine) {
94ee45b6c2SMasami Hiramatsu 		pr_debug("machine__new_host() failed.\n");
95ee45b6c2SMasami Hiramatsu 		symbol__exit();
96ee45b6c2SMasami Hiramatsu 		ret = -1;
97469b9b88SMasami Hiramatsu 	}
98146a1439SMasami Hiramatsu out:
99146a1439SMasami Hiramatsu 	if (ret < 0)
100146a1439SMasami Hiramatsu 		pr_warning("Failed to init vmlinux path.\n");
101146a1439SMasami Hiramatsu 	return ret;
102e0faa8d3SMasami Hiramatsu }
103e0faa8d3SMasami Hiramatsu 
1049bae1e8cSNamhyung Kim void exit_probe_symbol_maps(void)
105ee45b6c2SMasami Hiramatsu {
106ee45b6c2SMasami Hiramatsu 	if (host_machine) {
107ee45b6c2SMasami Hiramatsu 		machine__delete(host_machine);
108ee45b6c2SMasami Hiramatsu 		host_machine = NULL;
109ee45b6c2SMasami Hiramatsu 	}
110ee45b6c2SMasami Hiramatsu 	symbol__exit();
111ee45b6c2SMasami Hiramatsu }
112ee45b6c2SMasami Hiramatsu 
113469b9b88SMasami Hiramatsu static struct symbol *__find_kernel_function_by_name(const char *name,
114469b9b88SMasami Hiramatsu 						     struct map **mapp)
115e0faa8d3SMasami Hiramatsu {
116ee45b6c2SMasami Hiramatsu 	return machine__find_kernel_function_by_name(host_machine, name, mapp,
117469b9b88SMasami Hiramatsu 						     NULL);
118e0faa8d3SMasami Hiramatsu }
119469b9b88SMasami Hiramatsu 
1208f33f7deSMasami Hiramatsu static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
1218f33f7deSMasami Hiramatsu {
1228f33f7deSMasami Hiramatsu 	return machine__find_kernel_function(host_machine, addr, mapp, NULL);
1238f33f7deSMasami Hiramatsu }
1248f33f7deSMasami Hiramatsu 
1258f33f7deSMasami Hiramatsu static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
1268f33f7deSMasami Hiramatsu {
1278f33f7deSMasami Hiramatsu 	/* kmap->ref_reloc_sym should be set if host_machine is initialized */
1288f33f7deSMasami Hiramatsu 	struct kmap *kmap;
129a5e813c6SArnaldo Carvalho de Melo 	struct map *map = machine__kernel_map(host_machine);
1308f33f7deSMasami Hiramatsu 
13177e65977SArnaldo Carvalho de Melo 	if (map__load(map, NULL) < 0)
1328f33f7deSMasami Hiramatsu 		return NULL;
1338f33f7deSMasami Hiramatsu 
13477e65977SArnaldo Carvalho de Melo 	kmap = map__kmap(map);
135ba92732eSWang Nan 	if (!kmap)
136ba92732eSWang Nan 		return NULL;
1378f33f7deSMasami Hiramatsu 	return kmap->ref_reloc_sym;
1388f33f7deSMasami Hiramatsu }
1398f33f7deSMasami Hiramatsu 
1409b239a12SMasami Hiramatsu static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
1419b239a12SMasami Hiramatsu 					     bool reloc, bool reladdr)
1428f33f7deSMasami Hiramatsu {
1438f33f7deSMasami Hiramatsu 	struct ref_reloc_sym *reloc_sym;
1448f33f7deSMasami Hiramatsu 	struct symbol *sym;
1458f33f7deSMasami Hiramatsu 	struct map *map;
1468f33f7deSMasami Hiramatsu 
1478f33f7deSMasami Hiramatsu 	/* ref_reloc_sym is just a label. Need a special fix*/
1488f33f7deSMasami Hiramatsu 	reloc_sym = kernel_get_ref_reloc_sym();
1498f33f7deSMasami Hiramatsu 	if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
1509b239a12SMasami Hiramatsu 		*addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
1518f33f7deSMasami Hiramatsu 	else {
1528f33f7deSMasami Hiramatsu 		sym = __find_kernel_function_by_name(name, &map);
1539b239a12SMasami Hiramatsu 		if (!sym)
1549b239a12SMasami Hiramatsu 			return -ENOENT;
1559b239a12SMasami Hiramatsu 		*addr = map->unmap_ip(map, sym->start) -
1569b239a12SMasami Hiramatsu 			((reloc) ? 0 : map->reloc) -
1579b239a12SMasami Hiramatsu 			((reladdr) ? map->start : 0);
1588f33f7deSMasami Hiramatsu 	}
1598f33f7deSMasami Hiramatsu 	return 0;
1608f33f7deSMasami Hiramatsu }
1618f33f7deSMasami Hiramatsu 
162e80711caSMasami Hiramatsu static struct map *kernel_get_module_map(const char *module)
163e80711caSMasami Hiramatsu {
164ee45b6c2SMasami Hiramatsu 	struct map_groups *grp = &host_machine->kmaps;
1651eee78aeSArnaldo Carvalho de Melo 	struct maps *maps = &grp->maps[MAP__FUNCTION];
1664bb7123dSArnaldo Carvalho de Melo 	struct map *pos;
167e80711caSMasami Hiramatsu 
16814a8fd7cSMasami Hiramatsu 	/* A file path -- this is an offline module */
16914a8fd7cSMasami Hiramatsu 	if (module && strchr(module, '/'))
1709f2de315SArnaldo Carvalho de Melo 		return machine__findnew_module_map(host_machine, 0, module);
17114a8fd7cSMasami Hiramatsu 
172e80711caSMasami Hiramatsu 	if (!module)
173e80711caSMasami Hiramatsu 		module = "kernel";
174e80711caSMasami Hiramatsu 
1754bb7123dSArnaldo Carvalho de Melo 	for (pos = maps__first(maps); pos; pos = map__next(pos)) {
176e80711caSMasami Hiramatsu 		if (strncmp(pos->dso->short_name + 1, module,
177e80711caSMasami Hiramatsu 			    pos->dso->short_name_len - 2) == 0) {
178e80711caSMasami Hiramatsu 			return pos;
179e80711caSMasami Hiramatsu 		}
180e80711caSMasami Hiramatsu 	}
181e80711caSMasami Hiramatsu 	return NULL;
182e80711caSMasami Hiramatsu }
183e80711caSMasami Hiramatsu 
1849b118acaSMasami Hiramatsu static struct map *get_target_map(const char *target, bool user)
1859b118acaSMasami Hiramatsu {
1869b118acaSMasami Hiramatsu 	/* Init maps of given executable or kernel */
1879b118acaSMasami Hiramatsu 	if (user)
1889b118acaSMasami Hiramatsu 		return dso__new_map(target);
1899b118acaSMasami Hiramatsu 	else
1909b118acaSMasami Hiramatsu 		return kernel_get_module_map(target);
1919b118acaSMasami Hiramatsu }
1929b118acaSMasami Hiramatsu 
1939b118acaSMasami Hiramatsu static void put_target_map(struct map *map, bool user)
1949b118acaSMasami Hiramatsu {
1959b118acaSMasami Hiramatsu 	if (map && user) {
1969b118acaSMasami Hiramatsu 		/* Only the user map needs to be released */
19784c2cafaSArnaldo Carvalho de Melo 		map__put(map);
1989b118acaSMasami Hiramatsu 	}
1999b118acaSMasami Hiramatsu }
2009b118acaSMasami Hiramatsu 
2019b118acaSMasami Hiramatsu 
202fb7345bbSMasami Hiramatsu static int convert_exec_to_group(const char *exec, char **result)
203fb7345bbSMasami Hiramatsu {
204fb7345bbSMasami Hiramatsu 	char *ptr1, *ptr2, *exec_copy;
205fb7345bbSMasami Hiramatsu 	char buf[64];
206fb7345bbSMasami Hiramatsu 	int ret;
207fb7345bbSMasami Hiramatsu 
208fb7345bbSMasami Hiramatsu 	exec_copy = strdup(exec);
209fb7345bbSMasami Hiramatsu 	if (!exec_copy)
210fb7345bbSMasami Hiramatsu 		return -ENOMEM;
211fb7345bbSMasami Hiramatsu 
212fb7345bbSMasami Hiramatsu 	ptr1 = basename(exec_copy);
213fb7345bbSMasami Hiramatsu 	if (!ptr1) {
214fb7345bbSMasami Hiramatsu 		ret = -EINVAL;
215fb7345bbSMasami Hiramatsu 		goto out;
216fb7345bbSMasami Hiramatsu 	}
217fb7345bbSMasami Hiramatsu 
218fb7345bbSMasami Hiramatsu 	ptr2 = strpbrk(ptr1, "-._");
219fb7345bbSMasami Hiramatsu 	if (ptr2)
220fb7345bbSMasami Hiramatsu 		*ptr2 = '\0';
221fb7345bbSMasami Hiramatsu 	ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
222fb7345bbSMasami Hiramatsu 	if (ret < 0)
223fb7345bbSMasami Hiramatsu 		goto out;
224fb7345bbSMasami Hiramatsu 
225fb7345bbSMasami Hiramatsu 	*result = strdup(buf);
226fb7345bbSMasami Hiramatsu 	ret = *result ? 0 : -ENOMEM;
227fb7345bbSMasami Hiramatsu 
228fb7345bbSMasami Hiramatsu out:
229fb7345bbSMasami Hiramatsu 	free(exec_copy);
230fb7345bbSMasami Hiramatsu 	return ret;
231fb7345bbSMasami Hiramatsu }
232fb7345bbSMasami Hiramatsu 
2339b118acaSMasami Hiramatsu static void clear_perf_probe_point(struct perf_probe_point *pp)
2349b118acaSMasami Hiramatsu {
2359b118acaSMasami Hiramatsu 	free(pp->file);
2369b118acaSMasami Hiramatsu 	free(pp->function);
2379b118acaSMasami Hiramatsu 	free(pp->lazy_line);
2389b118acaSMasami Hiramatsu }
2399b118acaSMasami Hiramatsu 
240eb948e50SMasami Hiramatsu static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
241eb948e50SMasami Hiramatsu {
242eb948e50SMasami Hiramatsu 	int i;
243eb948e50SMasami Hiramatsu 
244eb948e50SMasami Hiramatsu 	for (i = 0; i < ntevs; i++)
245eb948e50SMasami Hiramatsu 		clear_probe_trace_event(tevs + i);
246eb948e50SMasami Hiramatsu }
247eb948e50SMasami Hiramatsu 
248b031220dSMasami Hiramatsu static bool kprobe_blacklist__listed(unsigned long address);
249b031220dSMasami Hiramatsu static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
250b031220dSMasami Hiramatsu {
2519b239a12SMasami Hiramatsu 	u64 etext_addr = 0;
2529b239a12SMasami Hiramatsu 	int ret;
2537c31bb8cSHe Kuang 
254b031220dSMasami Hiramatsu 	/* Get the address of _etext for checking non-probable text symbol */
2559b239a12SMasami Hiramatsu 	ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
2569b239a12SMasami Hiramatsu 						false, false);
2577c31bb8cSHe Kuang 
2589b239a12SMasami Hiramatsu 	if (ret == 0 && etext_addr < address)
259b031220dSMasami Hiramatsu 		pr_warning("%s is out of .text, skip it.\n", symbol);
260b031220dSMasami Hiramatsu 	else if (kprobe_blacklist__listed(address))
261b031220dSMasami Hiramatsu 		pr_warning("%s is blacklisted function, skip it.\n", symbol);
262b031220dSMasami Hiramatsu 	else
263b031220dSMasami Hiramatsu 		return false;
264b031220dSMasami Hiramatsu 
265b031220dSMasami Hiramatsu 	return true;
266b031220dSMasami Hiramatsu }
267b031220dSMasami Hiramatsu 
268*c61fb959SRavi Bangoria /*
269*c61fb959SRavi Bangoria  * NOTE:
270*c61fb959SRavi Bangoria  * '.gnu.linkonce.this_module' section of kernel module elf directly
271*c61fb959SRavi Bangoria  * maps to 'struct module' from linux/module.h. This section contains
272*c61fb959SRavi Bangoria  * actual module name which will be used by kernel after loading it.
273*c61fb959SRavi Bangoria  * But, we cannot use 'struct module' here since linux/module.h is not
274*c61fb959SRavi Bangoria  * exposed to user-space. Offset of 'name' has remained same from long
275*c61fb959SRavi Bangoria  * time, so hardcoding it here.
276*c61fb959SRavi Bangoria  */
277*c61fb959SRavi Bangoria #ifdef __LP64__
278*c61fb959SRavi Bangoria #define MOD_NAME_OFFSET 24
279*c61fb959SRavi Bangoria #else
280*c61fb959SRavi Bangoria #define MOD_NAME_OFFSET 12
281*c61fb959SRavi Bangoria #endif
282*c61fb959SRavi Bangoria 
283*c61fb959SRavi Bangoria /*
284*c61fb959SRavi Bangoria  * @module can be module name of module file path. In case of path,
285*c61fb959SRavi Bangoria  * inspect elf and find out what is actual module name.
286*c61fb959SRavi Bangoria  * Caller has to free mod_name after using it.
287*c61fb959SRavi Bangoria  */
288*c61fb959SRavi Bangoria static char *find_module_name(const char *module)
289*c61fb959SRavi Bangoria {
290*c61fb959SRavi Bangoria 	int fd;
291*c61fb959SRavi Bangoria 	Elf *elf;
292*c61fb959SRavi Bangoria 	GElf_Ehdr ehdr;
293*c61fb959SRavi Bangoria 	GElf_Shdr shdr;
294*c61fb959SRavi Bangoria 	Elf_Data *data;
295*c61fb959SRavi Bangoria 	Elf_Scn *sec;
296*c61fb959SRavi Bangoria 	char *mod_name = NULL;
297*c61fb959SRavi Bangoria 
298*c61fb959SRavi Bangoria 	fd = open(module, O_RDONLY);
299*c61fb959SRavi Bangoria 	if (fd < 0)
300*c61fb959SRavi Bangoria 		return NULL;
301*c61fb959SRavi Bangoria 
302*c61fb959SRavi Bangoria 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
303*c61fb959SRavi Bangoria 	if (elf == NULL)
304*c61fb959SRavi Bangoria 		goto elf_err;
305*c61fb959SRavi Bangoria 
306*c61fb959SRavi Bangoria 	if (gelf_getehdr(elf, &ehdr) == NULL)
307*c61fb959SRavi Bangoria 		goto ret_err;
308*c61fb959SRavi Bangoria 
309*c61fb959SRavi Bangoria 	sec = elf_section_by_name(elf, &ehdr, &shdr,
310*c61fb959SRavi Bangoria 			".gnu.linkonce.this_module", NULL);
311*c61fb959SRavi Bangoria 	if (!sec)
312*c61fb959SRavi Bangoria 		goto ret_err;
313*c61fb959SRavi Bangoria 
314*c61fb959SRavi Bangoria 	data = elf_getdata(sec, NULL);
315*c61fb959SRavi Bangoria 	if (!data || !data->d_buf)
316*c61fb959SRavi Bangoria 		goto ret_err;
317*c61fb959SRavi Bangoria 
318*c61fb959SRavi Bangoria 	mod_name = strdup((char *)data->d_buf + MOD_NAME_OFFSET);
319*c61fb959SRavi Bangoria 
320*c61fb959SRavi Bangoria ret_err:
321*c61fb959SRavi Bangoria 	elf_end(elf);
322*c61fb959SRavi Bangoria elf_err:
323*c61fb959SRavi Bangoria 	close(fd);
324*c61fb959SRavi Bangoria 	return mod_name;
325*c61fb959SRavi Bangoria }
326*c61fb959SRavi Bangoria 
32789fe808aSIngo Molnar #ifdef HAVE_DWARF_SUPPORT
32860fb7742SWang Nan 
32960fb7742SWang Nan static int kernel_get_module_dso(const char *module, struct dso **pdso)
33060fb7742SWang Nan {
33160fb7742SWang Nan 	struct dso *dso;
33260fb7742SWang Nan 	struct map *map;
33360fb7742SWang Nan 	const char *vmlinux_name;
33460fb7742SWang Nan 	int ret = 0;
33560fb7742SWang Nan 
33660fb7742SWang Nan 	if (module) {
337266fa2b2SArnaldo Carvalho de Melo 		char module_name[128];
338266fa2b2SArnaldo Carvalho de Melo 
339266fa2b2SArnaldo Carvalho de Melo 		snprintf(module_name, sizeof(module_name), "[%s]", module);
340266fa2b2SArnaldo Carvalho de Melo 		map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
341266fa2b2SArnaldo Carvalho de Melo 		if (map) {
342266fa2b2SArnaldo Carvalho de Melo 			dso = map->dso;
34360fb7742SWang Nan 			goto found;
34460fb7742SWang Nan 		}
34560fb7742SWang Nan 		pr_debug("Failed to find module %s.\n", module);
34660fb7742SWang Nan 		return -ENOENT;
34760fb7742SWang Nan 	}
34860fb7742SWang Nan 
349a5e813c6SArnaldo Carvalho de Melo 	map = machine__kernel_map(host_machine);
35060fb7742SWang Nan 	dso = map->dso;
35160fb7742SWang Nan 
35260fb7742SWang Nan 	vmlinux_name = symbol_conf.vmlinux_name;
35360fb7742SWang Nan 	dso->load_errno = 0;
35460fb7742SWang Nan 	if (vmlinux_name)
35560fb7742SWang Nan 		ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
35660fb7742SWang Nan 	else
35760fb7742SWang Nan 		ret = dso__load_vmlinux_path(dso, map, NULL);
35860fb7742SWang Nan found:
35960fb7742SWang Nan 	*pdso = dso;
36060fb7742SWang Nan 	return ret;
36160fb7742SWang Nan }
36260fb7742SWang Nan 
3639b118acaSMasami Hiramatsu /*
3649b118acaSMasami Hiramatsu  * Some binaries like glibc have special symbols which are on the symbol
3659b118acaSMasami Hiramatsu  * table, but not in the debuginfo. If we can find the address of the
3669b118acaSMasami Hiramatsu  * symbol from map, we can translate the address back to the probe point.
3679b118acaSMasami Hiramatsu  */
3689b118acaSMasami Hiramatsu static int find_alternative_probe_point(struct debuginfo *dinfo,
3699b118acaSMasami Hiramatsu 					struct perf_probe_point *pp,
3709b118acaSMasami Hiramatsu 					struct perf_probe_point *result,
3719b118acaSMasami Hiramatsu 					const char *target, bool uprobes)
3729b118acaSMasami Hiramatsu {
3739b118acaSMasami Hiramatsu 	struct map *map = NULL;
3749b118acaSMasami Hiramatsu 	struct symbol *sym;
3759b118acaSMasami Hiramatsu 	u64 address = 0;
3769b118acaSMasami Hiramatsu 	int ret = -ENOENT;
3779b118acaSMasami Hiramatsu 
3789b118acaSMasami Hiramatsu 	/* This can work only for function-name based one */
3799b118acaSMasami Hiramatsu 	if (!pp->function || pp->file)
3809b118acaSMasami Hiramatsu 		return -ENOTSUP;
3819b118acaSMasami Hiramatsu 
3829b118acaSMasami Hiramatsu 	map = get_target_map(target, uprobes);
3839b118acaSMasami Hiramatsu 	if (!map)
3849b118acaSMasami Hiramatsu 		return -EINVAL;
3859b118acaSMasami Hiramatsu 
3869b118acaSMasami Hiramatsu 	/* Find the address of given function */
3879b118acaSMasami Hiramatsu 	map__for_each_symbol_by_name(map, pp->function, sym) {
388e6d7c91cSMasami Hiramatsu 		if (uprobes)
3899b118acaSMasami Hiramatsu 			address = sym->start;
390e6d7c91cSMasami Hiramatsu 		else
391e6d7c91cSMasami Hiramatsu 			address = map->unmap_ip(map, sym->start);
3929b118acaSMasami Hiramatsu 		break;
3939b118acaSMasami Hiramatsu 	}
3949b118acaSMasami Hiramatsu 	if (!address) {
3959b118acaSMasami Hiramatsu 		ret = -ENOENT;
3969b118acaSMasami Hiramatsu 		goto out;
3979b118acaSMasami Hiramatsu 	}
398f6c15621SWang Nan 	pr_debug("Symbol %s address found : %" PRIx64 "\n",
399f6c15621SWang Nan 			pp->function, address);
4009b118acaSMasami Hiramatsu 
4019b118acaSMasami Hiramatsu 	ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
4029b118acaSMasami Hiramatsu 					  result);
4039b118acaSMasami Hiramatsu 	if (ret <= 0)
4049b118acaSMasami Hiramatsu 		ret = (!ret) ? -ENOENT : ret;
4059b118acaSMasami Hiramatsu 	else {
4069b118acaSMasami Hiramatsu 		result->offset += pp->offset;
4079b118acaSMasami Hiramatsu 		result->line += pp->line;
4089d7b45c5SHe Kuang 		result->retprobe = pp->retprobe;
4099b118acaSMasami Hiramatsu 		ret = 0;
4109b118acaSMasami Hiramatsu 	}
4119b118acaSMasami Hiramatsu 
4129b118acaSMasami Hiramatsu out:
4139b118acaSMasami Hiramatsu 	put_target_map(map, uprobes);
4149b118acaSMasami Hiramatsu 	return ret;
4159b118acaSMasami Hiramatsu 
4169b118acaSMasami Hiramatsu }
4179b118acaSMasami Hiramatsu 
4189b118acaSMasami Hiramatsu static int get_alternative_probe_event(struct debuginfo *dinfo,
4199b118acaSMasami Hiramatsu 				       struct perf_probe_event *pev,
42044225521SMasami Hiramatsu 				       struct perf_probe_point *tmp)
4219b118acaSMasami Hiramatsu {
4229b118acaSMasami Hiramatsu 	int ret;
4239b118acaSMasami Hiramatsu 
4249b118acaSMasami Hiramatsu 	memcpy(tmp, &pev->point, sizeof(*tmp));
4259b118acaSMasami Hiramatsu 	memset(&pev->point, 0, sizeof(pev->point));
4269b118acaSMasami Hiramatsu 	ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
42744225521SMasami Hiramatsu 					   pev->target, pev->uprobes);
4289b118acaSMasami Hiramatsu 	if (ret < 0)
4299b118acaSMasami Hiramatsu 		memcpy(&pev->point, tmp, sizeof(*tmp));
4309b118acaSMasami Hiramatsu 
4319b118acaSMasami Hiramatsu 	return ret;
4329b118acaSMasami Hiramatsu }
433a15ad2f5SMasami Hiramatsu 
434811dd2aeSMasami Hiramatsu static int get_alternative_line_range(struct debuginfo *dinfo,
435811dd2aeSMasami Hiramatsu 				      struct line_range *lr,
436811dd2aeSMasami Hiramatsu 				      const char *target, bool user)
437811dd2aeSMasami Hiramatsu {
4386d4a4896SDavid Ahern 	struct perf_probe_point pp = { .function = lr->function,
4396d4a4896SDavid Ahern 				       .file = lr->file,
4406d4a4896SDavid Ahern 				       .line = lr->start };
4416d4a4896SDavid Ahern 	struct perf_probe_point result;
442811dd2aeSMasami Hiramatsu 	int ret, len = 0;
443811dd2aeSMasami Hiramatsu 
4446d4a4896SDavid Ahern 	memset(&result, 0, sizeof(result));
4456d4a4896SDavid Ahern 
446811dd2aeSMasami Hiramatsu 	if (lr->end != INT_MAX)
447811dd2aeSMasami Hiramatsu 		len = lr->end - lr->start;
448811dd2aeSMasami Hiramatsu 	ret = find_alternative_probe_point(dinfo, &pp, &result,
449811dd2aeSMasami Hiramatsu 					   target, user);
450811dd2aeSMasami Hiramatsu 	if (!ret) {
451811dd2aeSMasami Hiramatsu 		lr->function = result.function;
452811dd2aeSMasami Hiramatsu 		lr->file = result.file;
453811dd2aeSMasami Hiramatsu 		lr->start = result.line;
454811dd2aeSMasami Hiramatsu 		if (lr->end != INT_MAX)
455811dd2aeSMasami Hiramatsu 			lr->end = lr->start + len;
456811dd2aeSMasami Hiramatsu 		clear_perf_probe_point(&pp);
457811dd2aeSMasami Hiramatsu 	}
458811dd2aeSMasami Hiramatsu 	return ret;
459811dd2aeSMasami Hiramatsu }
460811dd2aeSMasami Hiramatsu 
461ff741783SMasami Hiramatsu /* Open new debuginfo of given module */
46292561cb7SMasami Hiramatsu static struct debuginfo *open_debuginfo(const char *module, bool silent)
463469b9b88SMasami Hiramatsu {
464a15ad2f5SMasami Hiramatsu 	const char *path = module;
465419e8738SMasami Hiramatsu 	char reason[STRERR_BUFSIZE];
466419e8738SMasami Hiramatsu 	struct debuginfo *ret = NULL;
467419e8738SMasami Hiramatsu 	struct dso *dso = NULL;
468419e8738SMasami Hiramatsu 	int err;
46914a8fd7cSMasami Hiramatsu 
470a15ad2f5SMasami Hiramatsu 	if (!module || !strchr(module, '/')) {
471419e8738SMasami Hiramatsu 		err = kernel_get_module_dso(module, &dso);
472419e8738SMasami Hiramatsu 		if (err < 0) {
473419e8738SMasami Hiramatsu 			if (!dso || dso->load_errno == 0) {
474419e8738SMasami Hiramatsu 				if (!strerror_r(-err, reason, STRERR_BUFSIZE))
475419e8738SMasami Hiramatsu 					strcpy(reason, "(unknown)");
476419e8738SMasami Hiramatsu 			} else
477419e8738SMasami Hiramatsu 				dso__strerror_load(dso, reason, STRERR_BUFSIZE);
47892561cb7SMasami Hiramatsu 			if (!silent)
479419e8738SMasami Hiramatsu 				pr_err("Failed to find the path for %s: %s\n",
480419e8738SMasami Hiramatsu 					module ?: "kernel", reason);
481ff741783SMasami Hiramatsu 			return NULL;
482469b9b88SMasami Hiramatsu 		}
483419e8738SMasami Hiramatsu 		path = dso->long_name;
48414a8fd7cSMasami Hiramatsu 	}
48592561cb7SMasami Hiramatsu 	ret = debuginfo__new(path);
48692561cb7SMasami Hiramatsu 	if (!ret && !silent) {
48792561cb7SMasami Hiramatsu 		pr_warning("The %s file has no debug information.\n", path);
48892561cb7SMasami Hiramatsu 		if (!module || !strtailcmp(path, ".ko"))
48992561cb7SMasami Hiramatsu 			pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
49092561cb7SMasami Hiramatsu 		else
49192561cb7SMasami Hiramatsu 			pr_warning("Rebuild with -g, ");
49292561cb7SMasami Hiramatsu 		pr_warning("or install an appropriate debuginfo package.\n");
493e0faa8d3SMasami Hiramatsu 	}
49492561cb7SMasami Hiramatsu 	return ret;
49592561cb7SMasami Hiramatsu }
49692561cb7SMasami Hiramatsu 
4977737af01SMasami Hiramatsu /* For caching the last debuginfo */
4987737af01SMasami Hiramatsu static struct debuginfo *debuginfo_cache;
4997737af01SMasami Hiramatsu static char *debuginfo_cache_path;
5007737af01SMasami Hiramatsu 
5017737af01SMasami Hiramatsu static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
5027737af01SMasami Hiramatsu {
50320f49859SMasami Hiramatsu 	const char *path = module;
50420f49859SMasami Hiramatsu 
50520f49859SMasami Hiramatsu 	/* If the module is NULL, it should be the kernel. */
50620f49859SMasami Hiramatsu 	if (!module)
50720f49859SMasami Hiramatsu 		path = "kernel";
50820f49859SMasami Hiramatsu 
50920f49859SMasami Hiramatsu 	if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
5107737af01SMasami Hiramatsu 		goto out;
5117737af01SMasami Hiramatsu 
5127737af01SMasami Hiramatsu 	/* Copy module path */
5137737af01SMasami Hiramatsu 	free(debuginfo_cache_path);
51420f49859SMasami Hiramatsu 	debuginfo_cache_path = strdup(path);
5157737af01SMasami Hiramatsu 	if (!debuginfo_cache_path) {
5167737af01SMasami Hiramatsu 		debuginfo__delete(debuginfo_cache);
5177737af01SMasami Hiramatsu 		debuginfo_cache = NULL;
5187737af01SMasami Hiramatsu 		goto out;
5197737af01SMasami Hiramatsu 	}
5207737af01SMasami Hiramatsu 
5217737af01SMasami Hiramatsu 	debuginfo_cache = open_debuginfo(module, silent);
5227737af01SMasami Hiramatsu 	if (!debuginfo_cache)
5237737af01SMasami Hiramatsu 		zfree(&debuginfo_cache_path);
5247737af01SMasami Hiramatsu out:
5257737af01SMasami Hiramatsu 	return debuginfo_cache;
5267737af01SMasami Hiramatsu }
5277737af01SMasami Hiramatsu 
5287737af01SMasami Hiramatsu static void debuginfo_cache__exit(void)
5297737af01SMasami Hiramatsu {
5307737af01SMasami Hiramatsu 	debuginfo__delete(debuginfo_cache);
5317737af01SMasami Hiramatsu 	debuginfo_cache = NULL;
5327737af01SMasami Hiramatsu 	zfree(&debuginfo_cache_path);
5337737af01SMasami Hiramatsu }
5347737af01SMasami Hiramatsu 
5354b4da7f7SMasami Hiramatsu 
53699ca4233SMasami Hiramatsu static int get_text_start_address(const char *exec, unsigned long *address)
53799ca4233SMasami Hiramatsu {
53899ca4233SMasami Hiramatsu 	Elf *elf;
53999ca4233SMasami Hiramatsu 	GElf_Ehdr ehdr;
54099ca4233SMasami Hiramatsu 	GElf_Shdr shdr;
54199ca4233SMasami Hiramatsu 	int fd, ret = -ENOENT;
54299ca4233SMasami Hiramatsu 
54399ca4233SMasami Hiramatsu 	fd = open(exec, O_RDONLY);
54499ca4233SMasami Hiramatsu 	if (fd < 0)
54599ca4233SMasami Hiramatsu 		return -errno;
54699ca4233SMasami Hiramatsu 
54799ca4233SMasami Hiramatsu 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
548062d6c2aSMasami Hiramatsu 	if (elf == NULL) {
549062d6c2aSMasami Hiramatsu 		ret = -EINVAL;
550062d6c2aSMasami Hiramatsu 		goto out_close;
551062d6c2aSMasami Hiramatsu 	}
55299ca4233SMasami Hiramatsu 
55399ca4233SMasami Hiramatsu 	if (gelf_getehdr(elf, &ehdr) == NULL)
55499ca4233SMasami Hiramatsu 		goto out;
55599ca4233SMasami Hiramatsu 
55699ca4233SMasami Hiramatsu 	if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
55799ca4233SMasami Hiramatsu 		goto out;
55899ca4233SMasami Hiramatsu 
55999ca4233SMasami Hiramatsu 	*address = shdr.sh_addr - shdr.sh_offset;
56099ca4233SMasami Hiramatsu 	ret = 0;
56199ca4233SMasami Hiramatsu out:
56299ca4233SMasami Hiramatsu 	elf_end(elf);
563062d6c2aSMasami Hiramatsu out_close:
564062d6c2aSMasami Hiramatsu 	close(fd);
565062d6c2aSMasami Hiramatsu 
56699ca4233SMasami Hiramatsu 	return ret;
56799ca4233SMasami Hiramatsu }
56899ca4233SMasami Hiramatsu 
5695a6f6314SMasami Hiramatsu /*
5705a6f6314SMasami Hiramatsu  * Convert trace point to probe point with debuginfo
5715a6f6314SMasami Hiramatsu  */
5725a6f6314SMasami Hiramatsu static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
5735a6f6314SMasami Hiramatsu 					    struct perf_probe_point *pp,
5745a6f6314SMasami Hiramatsu 					    bool is_kprobe)
5755a6f6314SMasami Hiramatsu {
5765a6f6314SMasami Hiramatsu 	struct debuginfo *dinfo = NULL;
5775a6f6314SMasami Hiramatsu 	unsigned long stext = 0;
5785a6f6314SMasami Hiramatsu 	u64 addr = tp->address;
5795a6f6314SMasami Hiramatsu 	int ret = -ENOENT;
5805a6f6314SMasami Hiramatsu 
5815a6f6314SMasami Hiramatsu 	/* convert the address to dwarf address */
5825a6f6314SMasami Hiramatsu 	if (!is_kprobe) {
5835a6f6314SMasami Hiramatsu 		if (!addr) {
5845a6f6314SMasami Hiramatsu 			ret = -EINVAL;
5855a6f6314SMasami Hiramatsu 			goto error;
5865a6f6314SMasami Hiramatsu 		}
5875a6f6314SMasami Hiramatsu 		ret = get_text_start_address(tp->module, &stext);
5885a6f6314SMasami Hiramatsu 		if (ret < 0)
5895a6f6314SMasami Hiramatsu 			goto error;
5905a6f6314SMasami Hiramatsu 		addr += stext;
591e486367fSWang Nan 	} else if (tp->symbol) {
5929b239a12SMasami Hiramatsu 		/* If the module is given, this returns relative address */
5939b239a12SMasami Hiramatsu 		ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
5949b239a12SMasami Hiramatsu 							false, !!tp->module);
5959b239a12SMasami Hiramatsu 		if (ret != 0)
5965a6f6314SMasami Hiramatsu 			goto error;
5975a6f6314SMasami Hiramatsu 		addr += tp->offset;
5985a6f6314SMasami Hiramatsu 	}
5995a6f6314SMasami Hiramatsu 
6005a6f6314SMasami Hiramatsu 	pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
6015a6f6314SMasami Hiramatsu 		 tp->module ? : "kernel");
6025a6f6314SMasami Hiramatsu 
6037737af01SMasami Hiramatsu 	dinfo = debuginfo_cache__open(tp->module, verbose == 0);
6047737af01SMasami Hiramatsu 	if (dinfo)
6055a6f6314SMasami Hiramatsu 		ret = debuginfo__find_probe_point(dinfo,
6065a6f6314SMasami Hiramatsu 						 (unsigned long)addr, pp);
6077737af01SMasami Hiramatsu 	else
6085a6f6314SMasami Hiramatsu 		ret = -ENOENT;
6095a6f6314SMasami Hiramatsu 
6105a6f6314SMasami Hiramatsu 	if (ret > 0) {
6115a6f6314SMasami Hiramatsu 		pp->retprobe = tp->retprobe;
6125a6f6314SMasami Hiramatsu 		return 0;
6135a6f6314SMasami Hiramatsu 	}
6145a6f6314SMasami Hiramatsu error:
6155a6f6314SMasami Hiramatsu 	pr_debug("Failed to find corresponding probes from debuginfo.\n");
6165a6f6314SMasami Hiramatsu 	return ret ? : -ENOENT;
6175a6f6314SMasami Hiramatsu }
6185a6f6314SMasami Hiramatsu 
619fb7345bbSMasami Hiramatsu static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
620fb7345bbSMasami Hiramatsu 					  int ntevs, const char *exec)
621fb7345bbSMasami Hiramatsu {
622fb7345bbSMasami Hiramatsu 	int i, ret = 0;
623eb948e50SMasami Hiramatsu 	unsigned long stext = 0;
624fb7345bbSMasami Hiramatsu 
625fb7345bbSMasami Hiramatsu 	if (!exec)
626fb7345bbSMasami Hiramatsu 		return 0;
627fb7345bbSMasami Hiramatsu 
628fb7345bbSMasami Hiramatsu 	ret = get_text_start_address(exec, &stext);
629fb7345bbSMasami Hiramatsu 	if (ret < 0)
630fb7345bbSMasami Hiramatsu 		return ret;
631fb7345bbSMasami Hiramatsu 
632fb7345bbSMasami Hiramatsu 	for (i = 0; i < ntevs && ret >= 0; i++) {
633981a2379SMasami Hiramatsu 		/* point.address is the addres of point.symbol + point.offset */
634eb948e50SMasami Hiramatsu 		tevs[i].point.address -= stext;
635fb7345bbSMasami Hiramatsu 		tevs[i].point.module = strdup(exec);
636eb948e50SMasami Hiramatsu 		if (!tevs[i].point.module) {
637fb7345bbSMasami Hiramatsu 			ret = -ENOMEM;
638fb7345bbSMasami Hiramatsu 			break;
639fb7345bbSMasami Hiramatsu 		}
640fb7345bbSMasami Hiramatsu 		tevs[i].uprobes = true;
641fb7345bbSMasami Hiramatsu 	}
642fb7345bbSMasami Hiramatsu 
643fb7345bbSMasami Hiramatsu 	return ret;
644fb7345bbSMasami Hiramatsu }
645fb7345bbSMasami Hiramatsu 
646190b57fcSMasami Hiramatsu static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
647190b57fcSMasami Hiramatsu 					    int ntevs, const char *module)
648190b57fcSMasami Hiramatsu {
64914a8fd7cSMasami Hiramatsu 	int i, ret = 0;
65063a29613SRavi Bangoria 	char *mod_name = NULL;
65114a8fd7cSMasami Hiramatsu 
65214a8fd7cSMasami Hiramatsu 	if (!module)
65314a8fd7cSMasami Hiramatsu 		return 0;
65414a8fd7cSMasami Hiramatsu 
65563a29613SRavi Bangoria 	mod_name = find_module_name(module);
65614a8fd7cSMasami Hiramatsu 
657190b57fcSMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
65863a29613SRavi Bangoria 		tevs[i].point.module =
65963a29613SRavi Bangoria 			strdup(mod_name ? mod_name : module);
66014a8fd7cSMasami Hiramatsu 		if (!tevs[i].point.module) {
66114a8fd7cSMasami Hiramatsu 			ret = -ENOMEM;
66214a8fd7cSMasami Hiramatsu 			break;
663190b57fcSMasami Hiramatsu 		}
66414a8fd7cSMasami Hiramatsu 	}
66514a8fd7cSMasami Hiramatsu 
66663a29613SRavi Bangoria 	free(mod_name);
66714a8fd7cSMasami Hiramatsu 	return ret;
668190b57fcSMasami Hiramatsu }
669190b57fcSMasami Hiramatsu 
670dfef99cdSMasami Hiramatsu /* Post processing the probe events */
671dfef99cdSMasami Hiramatsu static int post_process_probe_trace_events(struct probe_trace_event *tevs,
672dfef99cdSMasami Hiramatsu 					   int ntevs, const char *module,
673dfef99cdSMasami Hiramatsu 					   bool uprobe)
674dfef99cdSMasami Hiramatsu {
675dfef99cdSMasami Hiramatsu 	struct ref_reloc_sym *reloc_sym;
676dfef99cdSMasami Hiramatsu 	char *tmp;
6775a51fcd1SMasami Hiramatsu 	int i, skipped = 0;
678dfef99cdSMasami Hiramatsu 
679dfef99cdSMasami Hiramatsu 	if (uprobe)
680dfef99cdSMasami Hiramatsu 		return add_exec_to_probe_trace_events(tevs, ntevs, module);
681dfef99cdSMasami Hiramatsu 
682dfef99cdSMasami Hiramatsu 	/* Note that currently ref_reloc_sym based probe is not for drivers */
683dfef99cdSMasami Hiramatsu 	if (module)
684dfef99cdSMasami Hiramatsu 		return add_module_to_probe_trace_events(tevs, ntevs, module);
685dfef99cdSMasami Hiramatsu 
6868f33f7deSMasami Hiramatsu 	reloc_sym = kernel_get_ref_reloc_sym();
687dfef99cdSMasami Hiramatsu 	if (!reloc_sym) {
688dfef99cdSMasami Hiramatsu 		pr_warning("Relocated base symbol is not found!\n");
689dfef99cdSMasami Hiramatsu 		return -EINVAL;
690dfef99cdSMasami Hiramatsu 	}
691dfef99cdSMasami Hiramatsu 
692dfef99cdSMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
693b031220dSMasami Hiramatsu 		if (!tevs[i].point.address || tevs[i].point.retprobe)
694b031220dSMasami Hiramatsu 			continue;
6955a51fcd1SMasami Hiramatsu 		/* If we found a wrong one, mark it by NULL symbol */
696b031220dSMasami Hiramatsu 		if (kprobe_warn_out_range(tevs[i].point.symbol,
697b031220dSMasami Hiramatsu 					  tevs[i].point.address)) {
6985a51fcd1SMasami Hiramatsu 			tmp = NULL;
6995a51fcd1SMasami Hiramatsu 			skipped++;
7005a51fcd1SMasami Hiramatsu 		} else {
701dfef99cdSMasami Hiramatsu 			tmp = strdup(reloc_sym->name);
702dfef99cdSMasami Hiramatsu 			if (!tmp)
703dfef99cdSMasami Hiramatsu 				return -ENOMEM;
7045a51fcd1SMasami Hiramatsu 		}
7054c859351SMasami Hiramatsu 		/* If we have no realname, use symbol for it */
7064c859351SMasami Hiramatsu 		if (!tevs[i].point.realname)
7074c859351SMasami Hiramatsu 			tevs[i].point.realname = tevs[i].point.symbol;
7084c859351SMasami Hiramatsu 		else
709dfef99cdSMasami Hiramatsu 			free(tevs[i].point.symbol);
710dfef99cdSMasami Hiramatsu 		tevs[i].point.symbol = tmp;
711dfef99cdSMasami Hiramatsu 		tevs[i].point.offset = tevs[i].point.address -
712dfef99cdSMasami Hiramatsu 				       reloc_sym->unrelocated_addr;
713dfef99cdSMasami Hiramatsu 	}
7145a51fcd1SMasami Hiramatsu 	return skipped;
715dfef99cdSMasami Hiramatsu }
716dfef99cdSMasami Hiramatsu 
7174b4da7f7SMasami Hiramatsu /* Try to find perf_probe_event with debuginfo */
7180e60836bSSrikar Dronamraju static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
719ddb2f58fSMasami Hiramatsu 					  struct probe_trace_event **tevs)
7204b4da7f7SMasami Hiramatsu {
7214b4da7f7SMasami Hiramatsu 	bool need_dwarf = perf_probe_event_need_dwarf(pev);
7229b118acaSMasami Hiramatsu 	struct perf_probe_point tmp;
723225466f1SSrikar Dronamraju 	struct debuginfo *dinfo;
724190b57fcSMasami Hiramatsu 	int ntevs, ret = 0;
7254b4da7f7SMasami Hiramatsu 
72644225521SMasami Hiramatsu 	dinfo = open_debuginfo(pev->target, !need_dwarf);
727ff741783SMasami Hiramatsu 	if (!dinfo) {
72892561cb7SMasami Hiramatsu 		if (need_dwarf)
729ff741783SMasami Hiramatsu 			return -ENOENT;
730ff741783SMasami Hiramatsu 		pr_debug("Could not open debuginfo. Try to use symbols.\n");
7314b4da7f7SMasami Hiramatsu 		return 0;
7324b4da7f7SMasami Hiramatsu 	}
7334b4da7f7SMasami Hiramatsu 
734dfef99cdSMasami Hiramatsu 	pr_debug("Try to find probe point from debuginfo.\n");
735ff741783SMasami Hiramatsu 	/* Searching trace events corresponding to a probe event */
736ddb2f58fSMasami Hiramatsu 	ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
737ff741783SMasami Hiramatsu 
7389b118acaSMasami Hiramatsu 	if (ntevs == 0)	{  /* Not found, retry with an alternative */
73944225521SMasami Hiramatsu 		ret = get_alternative_probe_event(dinfo, pev, &tmp);
7409b118acaSMasami Hiramatsu 		if (!ret) {
741ddb2f58fSMasami Hiramatsu 			ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
7429b118acaSMasami Hiramatsu 			/*
7439b118acaSMasami Hiramatsu 			 * Write back to the original probe_event for
7449b118acaSMasami Hiramatsu 			 * setting appropriate (user given) event name
7459b118acaSMasami Hiramatsu 			 */
7469b118acaSMasami Hiramatsu 			clear_perf_probe_point(&pev->point);
7479b118acaSMasami Hiramatsu 			memcpy(&pev->point, &tmp, sizeof(tmp));
7489b118acaSMasami Hiramatsu 		}
7499b118acaSMasami Hiramatsu 	}
7509b118acaSMasami Hiramatsu 
751ff741783SMasami Hiramatsu 	debuginfo__delete(dinfo);
7524b4da7f7SMasami Hiramatsu 
753146a1439SMasami Hiramatsu 	if (ntevs > 0) {	/* Succeeded to find trace events */
754dfef99cdSMasami Hiramatsu 		pr_debug("Found %d probe_trace_events.\n", ntevs);
755dfef99cdSMasami Hiramatsu 		ret = post_process_probe_trace_events(*tevs, ntevs,
75644225521SMasami Hiramatsu 						pev->target, pev->uprobes);
7575a51fcd1SMasami Hiramatsu 		if (ret < 0 || ret == ntevs) {
758981d05adSMasami Hiramatsu 			clear_probe_trace_events(*tevs, ntevs);
759981d05adSMasami Hiramatsu 			zfree(tevs);
760981d05adSMasami Hiramatsu 		}
7615a51fcd1SMasami Hiramatsu 		if (ret != ntevs)
762190b57fcSMasami Hiramatsu 			return ret < 0 ? ret : ntevs;
7635a51fcd1SMasami Hiramatsu 		ntevs = 0;
7645a51fcd1SMasami Hiramatsu 		/* Fall through */
765146a1439SMasami Hiramatsu 	}
7664b4da7f7SMasami Hiramatsu 
767146a1439SMasami Hiramatsu 	if (ntevs == 0)	{	/* No error but failed to find probe point. */
7680687eba7SMasami Hiramatsu 		pr_warning("Probe point '%s' not found.\n",
7694b4da7f7SMasami Hiramatsu 			   synthesize_perf_probe_point(&pev->point));
770146a1439SMasami Hiramatsu 		return -ENOENT;
771146a1439SMasami Hiramatsu 	}
772146a1439SMasami Hiramatsu 	/* Error path : ntevs < 0 */
77315eca306SMasami Hiramatsu 	pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
7741c0bd0e8SWang Nan 	if (ntevs < 0) {
7751c0bd0e8SWang Nan 		if (ntevs == -EBADF)
77615eca306SMasami Hiramatsu 			pr_warning("Warning: No dwarf info found in the vmlinux - "
77715eca306SMasami Hiramatsu 				"please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
77815eca306SMasami Hiramatsu 		if (!need_dwarf) {
7790e43e5d2SMasami Hiramatsu 			pr_debug("Trying to use symbols.\n");
7804b4da7f7SMasami Hiramatsu 			return 0;
7814b4da7f7SMasami Hiramatsu 		}
78215eca306SMasami Hiramatsu 	}
78315eca306SMasami Hiramatsu 	return ntevs;
78415eca306SMasami Hiramatsu }
7854b4da7f7SMasami Hiramatsu 
7864b4da7f7SMasami Hiramatsu #define LINEBUF_SIZE 256
7874b4da7f7SMasami Hiramatsu #define NR_ADDITIONAL_LINES 2
7884b4da7f7SMasami Hiramatsu 
789fde52dbdSFranck Bui-Huu static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
7904b4da7f7SMasami Hiramatsu {
7915f03cba4SMasami Hiramatsu 	char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
792befe3414SFranck Bui-Huu 	const char *color = show_num ? "" : PERF_COLOR_BLUE;
793befe3414SFranck Bui-Huu 	const char *prefix = NULL;
7944b4da7f7SMasami Hiramatsu 
795befe3414SFranck Bui-Huu 	do {
7964b4da7f7SMasami Hiramatsu 		if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
7974b4da7f7SMasami Hiramatsu 			goto error;
798befe3414SFranck Bui-Huu 		if (skip)
799befe3414SFranck Bui-Huu 			continue;
800befe3414SFranck Bui-Huu 		if (!prefix) {
801befe3414SFranck Bui-Huu 			prefix = show_num ? "%7d  " : "         ";
802befe3414SFranck Bui-Huu 			color_fprintf(stdout, color, prefix, l);
8034b4da7f7SMasami Hiramatsu 		}
804befe3414SFranck Bui-Huu 		color_fprintf(stdout, color, "%s", buf);
8054b4da7f7SMasami Hiramatsu 
806befe3414SFranck Bui-Huu 	} while (strchr(buf, '\n') == NULL);
807146a1439SMasami Hiramatsu 
808fde52dbdSFranck Bui-Huu 	return 1;
8094b4da7f7SMasami Hiramatsu error:
810fde52dbdSFranck Bui-Huu 	if (ferror(fp)) {
8115f03cba4SMasami Hiramatsu 		pr_warning("File read error: %s\n",
8125f03cba4SMasami Hiramatsu 			   strerror_r(errno, sbuf, sizeof(sbuf)));
813146a1439SMasami Hiramatsu 		return -1;
8144b4da7f7SMasami Hiramatsu 	}
815fde52dbdSFranck Bui-Huu 	return 0;
816fde52dbdSFranck Bui-Huu }
817fde52dbdSFranck Bui-Huu 
818fde52dbdSFranck Bui-Huu static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
819fde52dbdSFranck Bui-Huu {
820fde52dbdSFranck Bui-Huu 	int rv = __show_one_line(fp, l, skip, show_num);
821fde52dbdSFranck Bui-Huu 	if (rv == 0) {
822fde52dbdSFranck Bui-Huu 		pr_warning("Source file is shorter than expected.\n");
823fde52dbdSFranck Bui-Huu 		rv = -1;
824fde52dbdSFranck Bui-Huu 	}
825fde52dbdSFranck Bui-Huu 	return rv;
826fde52dbdSFranck Bui-Huu }
827fde52dbdSFranck Bui-Huu 
828fde52dbdSFranck Bui-Huu #define show_one_line_with_num(f,l)	_show_one_line(f,l,false,true)
829fde52dbdSFranck Bui-Huu #define show_one_line(f,l)		_show_one_line(f,l,false,false)
830fde52dbdSFranck Bui-Huu #define skip_one_line(f,l)		_show_one_line(f,l,true,false)
831fde52dbdSFranck Bui-Huu #define show_one_line_or_eof(f,l)	__show_one_line(f,l,false,false)
8324b4da7f7SMasami Hiramatsu 
8334b4da7f7SMasami Hiramatsu /*
8344b4da7f7SMasami Hiramatsu  * Show line-range always requires debuginfo to find source file and
8354b4da7f7SMasami Hiramatsu  * line number.
8364b4da7f7SMasami Hiramatsu  */
837811dd2aeSMasami Hiramatsu static int __show_line_range(struct line_range *lr, const char *module,
838811dd2aeSMasami Hiramatsu 			     bool user)
8394b4da7f7SMasami Hiramatsu {
840d3b63d7aSMasami Hiramatsu 	int l = 1;
8415a62257aSMasami Hiramatsu 	struct int_node *ln;
842ff741783SMasami Hiramatsu 	struct debuginfo *dinfo;
8434b4da7f7SMasami Hiramatsu 	FILE *fp;
844ff741783SMasami Hiramatsu 	int ret;
8457cf0b79eSMasami Hiramatsu 	char *tmp;
8465f03cba4SMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE];
8474b4da7f7SMasami Hiramatsu 
8484b4da7f7SMasami Hiramatsu 	/* Search a line range */
84992561cb7SMasami Hiramatsu 	dinfo = open_debuginfo(module, false);
85092561cb7SMasami Hiramatsu 	if (!dinfo)
851ff741783SMasami Hiramatsu 		return -ENOENT;
852146a1439SMasami Hiramatsu 
853ff741783SMasami Hiramatsu 	ret = debuginfo__find_line_range(dinfo, lr);
854811dd2aeSMasami Hiramatsu 	if (!ret) {	/* Not found, retry with an alternative */
855811dd2aeSMasami Hiramatsu 		ret = get_alternative_line_range(dinfo, lr, module, user);
856811dd2aeSMasami Hiramatsu 		if (!ret)
857811dd2aeSMasami Hiramatsu 			ret = debuginfo__find_line_range(dinfo, lr);
858811dd2aeSMasami Hiramatsu 	}
859ff741783SMasami Hiramatsu 	debuginfo__delete(dinfo);
8605ee05b88SMasami Hiramatsu 	if (ret == 0 || ret == -ENOENT) {
861146a1439SMasami Hiramatsu 		pr_warning("Specified source line is not found.\n");
862146a1439SMasami Hiramatsu 		return -ENOENT;
863146a1439SMasami Hiramatsu 	} else if (ret < 0) {
8645ee05b88SMasami Hiramatsu 		pr_warning("Debuginfo analysis failed.\n");
865146a1439SMasami Hiramatsu 		return ret;
866146a1439SMasami Hiramatsu 	}
8674b4da7f7SMasami Hiramatsu 
8687cf0b79eSMasami Hiramatsu 	/* Convert source file path */
8697cf0b79eSMasami Hiramatsu 	tmp = lr->path;
8706a330a3cSMasami Hiramatsu 	ret = get_real_path(tmp, lr->comp_dir, &lr->path);
871a78604deSHe Kuang 
872a78604deSHe Kuang 	/* Free old path when new path is assigned */
873a78604deSHe Kuang 	if (tmp != lr->path)
874a78604deSHe Kuang 		free(tmp);
875a78604deSHe Kuang 
8767cf0b79eSMasami Hiramatsu 	if (ret < 0) {
8775ee05b88SMasami Hiramatsu 		pr_warning("Failed to find source file path.\n");
8787cf0b79eSMasami Hiramatsu 		return ret;
8797cf0b79eSMasami Hiramatsu 	}
8807cf0b79eSMasami Hiramatsu 
8814b4da7f7SMasami Hiramatsu 	setup_pager();
8824b4da7f7SMasami Hiramatsu 
8834b4da7f7SMasami Hiramatsu 	if (lr->function)
8848737ebdeSMasami Hiramatsu 		fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
8854b4da7f7SMasami Hiramatsu 			lr->start - lr->offset);
8864b4da7f7SMasami Hiramatsu 	else
88762c15fc4SFranck Bui-Huu 		fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
8884b4da7f7SMasami Hiramatsu 
8894b4da7f7SMasami Hiramatsu 	fp = fopen(lr->path, "r");
890146a1439SMasami Hiramatsu 	if (fp == NULL) {
891146a1439SMasami Hiramatsu 		pr_warning("Failed to open %s: %s\n", lr->path,
8925f03cba4SMasami Hiramatsu 			   strerror_r(errno, sbuf, sizeof(sbuf)));
893146a1439SMasami Hiramatsu 		return -errno;
894146a1439SMasami Hiramatsu 	}
8954b4da7f7SMasami Hiramatsu 	/* Skip to starting line number */
89644b81e92SFranck Bui-Huu 	while (l < lr->start) {
897fde52dbdSFranck Bui-Huu 		ret = skip_one_line(fp, l++);
898146a1439SMasami Hiramatsu 		if (ret < 0)
899146a1439SMasami Hiramatsu 			goto end;
90044b81e92SFranck Bui-Huu 	}
9014b4da7f7SMasami Hiramatsu 
9025a62257aSMasami Hiramatsu 	intlist__for_each(ln, lr->line_list) {
9035a62257aSMasami Hiramatsu 		for (; ln->i > l; l++) {
904fde52dbdSFranck Bui-Huu 			ret = show_one_line(fp, l - lr->offset);
90544b81e92SFranck Bui-Huu 			if (ret < 0)
90644b81e92SFranck Bui-Huu 				goto end;
90744b81e92SFranck Bui-Huu 		}
908fde52dbdSFranck Bui-Huu 		ret = show_one_line_with_num(fp, l++ - lr->offset);
909146a1439SMasami Hiramatsu 		if (ret < 0)
910146a1439SMasami Hiramatsu 			goto end;
9114b4da7f7SMasami Hiramatsu 	}
9124b4da7f7SMasami Hiramatsu 
9134b4da7f7SMasami Hiramatsu 	if (lr->end == INT_MAX)
9144b4da7f7SMasami Hiramatsu 		lr->end = l + NR_ADDITIONAL_LINES;
915fde52dbdSFranck Bui-Huu 	while (l <= lr->end) {
916fde52dbdSFranck Bui-Huu 		ret = show_one_line_or_eof(fp, l++ - lr->offset);
917fde52dbdSFranck Bui-Huu 		if (ret <= 0)
91844b81e92SFranck Bui-Huu 			break;
91944b81e92SFranck Bui-Huu 	}
920146a1439SMasami Hiramatsu end:
9214b4da7f7SMasami Hiramatsu 	fclose(fp);
922146a1439SMasami Hiramatsu 	return ret;
9234b4da7f7SMasami Hiramatsu }
9244b4da7f7SMasami Hiramatsu 
9252b394bc4SMasami Hiramatsu int show_line_range(struct line_range *lr, const char *module, bool user)
926ee45b6c2SMasami Hiramatsu {
927ee45b6c2SMasami Hiramatsu 	int ret;
928ee45b6c2SMasami Hiramatsu 
9299bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(user);
930ee45b6c2SMasami Hiramatsu 	if (ret < 0)
931ee45b6c2SMasami Hiramatsu 		return ret;
932811dd2aeSMasami Hiramatsu 	ret = __show_line_range(lr, module, user);
9339bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
934ee45b6c2SMasami Hiramatsu 
935ee45b6c2SMasami Hiramatsu 	return ret;
936ee45b6c2SMasami Hiramatsu }
937ee45b6c2SMasami Hiramatsu 
938ff741783SMasami Hiramatsu static int show_available_vars_at(struct debuginfo *dinfo,
939ff741783SMasami Hiramatsu 				  struct perf_probe_event *pev,
940ddb2f58fSMasami Hiramatsu 				  struct strfilter *_filter)
941cf6eb489SMasami Hiramatsu {
942cf6eb489SMasami Hiramatsu 	char *buf;
943bd09d7b5SMasami Hiramatsu 	int ret, i, nvars;
944cf6eb489SMasami Hiramatsu 	struct str_node *node;
945cf6eb489SMasami Hiramatsu 	struct variable_list *vls = NULL, *vl;
9469b118acaSMasami Hiramatsu 	struct perf_probe_point tmp;
947bd09d7b5SMasami Hiramatsu 	const char *var;
948cf6eb489SMasami Hiramatsu 
949cf6eb489SMasami Hiramatsu 	buf = synthesize_perf_probe_point(&pev->point);
950cf6eb489SMasami Hiramatsu 	if (!buf)
951cf6eb489SMasami Hiramatsu 		return -EINVAL;
952cf6eb489SMasami Hiramatsu 	pr_debug("Searching variables at %s\n", buf);
953cf6eb489SMasami Hiramatsu 
954ddb2f58fSMasami Hiramatsu 	ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
9559b118acaSMasami Hiramatsu 	if (!ret) {  /* Not found, retry with an alternative */
95644225521SMasami Hiramatsu 		ret = get_alternative_probe_event(dinfo, pev, &tmp);
9579b118acaSMasami Hiramatsu 		if (!ret) {
9589b118acaSMasami Hiramatsu 			ret = debuginfo__find_available_vars_at(dinfo, pev,
959ddb2f58fSMasami Hiramatsu 								&vls);
9609b118acaSMasami Hiramatsu 			/* Release the old probe_point */
9619b118acaSMasami Hiramatsu 			clear_perf_probe_point(&tmp);
9629b118acaSMasami Hiramatsu 		}
9639b118acaSMasami Hiramatsu 	}
964bd09d7b5SMasami Hiramatsu 	if (ret <= 0) {
96569e96eaaSMasami Hiramatsu 		if (ret == 0 || ret == -ENOENT) {
96669e96eaaSMasami Hiramatsu 			pr_err("Failed to find the address of %s\n", buf);
96769e96eaaSMasami Hiramatsu 			ret = -ENOENT;
96869e96eaaSMasami Hiramatsu 		} else
96969e96eaaSMasami Hiramatsu 			pr_warning("Debuginfo analysis failed.\n");
970bd09d7b5SMasami Hiramatsu 		goto end;
971bd09d7b5SMasami Hiramatsu 	}
97269e96eaaSMasami Hiramatsu 
973bd09d7b5SMasami Hiramatsu 	/* Some variables are found */
974cf6eb489SMasami Hiramatsu 	fprintf(stdout, "Available variables at %s\n", buf);
975cf6eb489SMasami Hiramatsu 	for (i = 0; i < ret; i++) {
976cf6eb489SMasami Hiramatsu 		vl = &vls[i];
977cf6eb489SMasami Hiramatsu 		/*
978cf6eb489SMasami Hiramatsu 		 * A probe point might be converted to
979cf6eb489SMasami Hiramatsu 		 * several trace points.
980cf6eb489SMasami Hiramatsu 		 */
981cf6eb489SMasami Hiramatsu 		fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
982cf6eb489SMasami Hiramatsu 			vl->point.offset);
98374cf249dSArnaldo Carvalho de Melo 		zfree(&vl->point.symbol);
984bd09d7b5SMasami Hiramatsu 		nvars = 0;
985cf6eb489SMasami Hiramatsu 		if (vl->vars) {
986bd09d7b5SMasami Hiramatsu 			strlist__for_each(node, vl->vars) {
987bd09d7b5SMasami Hiramatsu 				var = strchr(node->s, '\t') + 1;
988bd09d7b5SMasami Hiramatsu 				if (strfilter__compare(_filter, var)) {
989cf6eb489SMasami Hiramatsu 					fprintf(stdout, "\t\t%s\n", node->s);
990bd09d7b5SMasami Hiramatsu 					nvars++;
991bd09d7b5SMasami Hiramatsu 				}
992bd09d7b5SMasami Hiramatsu 			}
993cf6eb489SMasami Hiramatsu 			strlist__delete(vl->vars);
994bd09d7b5SMasami Hiramatsu 		}
995bd09d7b5SMasami Hiramatsu 		if (nvars == 0)
996bd09d7b5SMasami Hiramatsu 			fprintf(stdout, "\t\t(No matched variables)\n");
997cf6eb489SMasami Hiramatsu 	}
998cf6eb489SMasami Hiramatsu 	free(vls);
999bd09d7b5SMasami Hiramatsu end:
1000cf6eb489SMasami Hiramatsu 	free(buf);
1001cf6eb489SMasami Hiramatsu 	return ret;
1002cf6eb489SMasami Hiramatsu }
1003cf6eb489SMasami Hiramatsu 
1004cf6eb489SMasami Hiramatsu /* Show available variables on given probe point */
1005cf6eb489SMasami Hiramatsu int show_available_vars(struct perf_probe_event *pevs, int npevs,
1006ddb2f58fSMasami Hiramatsu 			struct strfilter *_filter)
1007cf6eb489SMasami Hiramatsu {
1008ff741783SMasami Hiramatsu 	int i, ret = 0;
1009ff741783SMasami Hiramatsu 	struct debuginfo *dinfo;
1010cf6eb489SMasami Hiramatsu 
10119bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(pevs->uprobes);
1012cf6eb489SMasami Hiramatsu 	if (ret < 0)
1013cf6eb489SMasami Hiramatsu 		return ret;
1014cf6eb489SMasami Hiramatsu 
101544225521SMasami Hiramatsu 	dinfo = open_debuginfo(pevs->target, false);
1016ff741783SMasami Hiramatsu 	if (!dinfo) {
1017ee45b6c2SMasami Hiramatsu 		ret = -ENOENT;
1018ee45b6c2SMasami Hiramatsu 		goto out;
1019ff741783SMasami Hiramatsu 	}
1020ff741783SMasami Hiramatsu 
1021cc446446SMasami Hiramatsu 	setup_pager();
1022cc446446SMasami Hiramatsu 
1023ff741783SMasami Hiramatsu 	for (i = 0; i < npevs && ret >= 0; i++)
1024ddb2f58fSMasami Hiramatsu 		ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1025ff741783SMasami Hiramatsu 
1026ff741783SMasami Hiramatsu 	debuginfo__delete(dinfo);
1027ee45b6c2SMasami Hiramatsu out:
10289bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
1029cf6eb489SMasami Hiramatsu 	return ret;
1030cf6eb489SMasami Hiramatsu }
1031cf6eb489SMasami Hiramatsu 
103289fe808aSIngo Molnar #else	/* !HAVE_DWARF_SUPPORT */
10334b4da7f7SMasami Hiramatsu 
10347737af01SMasami Hiramatsu static void debuginfo_cache__exit(void)
10357737af01SMasami Hiramatsu {
10367737af01SMasami Hiramatsu }
10377737af01SMasami Hiramatsu 
10385a6f6314SMasami Hiramatsu static int
10395a6f6314SMasami Hiramatsu find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
10405a6f6314SMasami Hiramatsu 				 struct perf_probe_point *pp __maybe_unused,
10415a6f6314SMasami Hiramatsu 				 bool is_kprobe __maybe_unused)
10424b4da7f7SMasami Hiramatsu {
10435a6f6314SMasami Hiramatsu 	return -ENOSYS;
10444b4da7f7SMasami Hiramatsu }
10454b4da7f7SMasami Hiramatsu 
10460e60836bSSrikar Dronamraju static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1047ddb2f58fSMasami Hiramatsu 				struct probe_trace_event **tevs __maybe_unused)
10484b4da7f7SMasami Hiramatsu {
1049146a1439SMasami Hiramatsu 	if (perf_probe_event_need_dwarf(pev)) {
1050146a1439SMasami Hiramatsu 		pr_warning("Debuginfo-analysis is not supported.\n");
1051146a1439SMasami Hiramatsu 		return -ENOSYS;
1052146a1439SMasami Hiramatsu 	}
1053225466f1SSrikar Dronamraju 
10544b4da7f7SMasami Hiramatsu 	return 0;
10554b4da7f7SMasami Hiramatsu }
10564b4da7f7SMasami Hiramatsu 
10571d037ca1SIrina Tirdea int show_line_range(struct line_range *lr __maybe_unused,
10582b394bc4SMasami Hiramatsu 		    const char *module __maybe_unused,
10592b394bc4SMasami Hiramatsu 		    bool user __maybe_unused)
10604b4da7f7SMasami Hiramatsu {
1061146a1439SMasami Hiramatsu 	pr_warning("Debuginfo-analysis is not supported.\n");
1062146a1439SMasami Hiramatsu 	return -ENOSYS;
10634b4da7f7SMasami Hiramatsu }
10644b4da7f7SMasami Hiramatsu 
10651d037ca1SIrina Tirdea int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1066ddb2f58fSMasami Hiramatsu 			int npevs __maybe_unused,
1067ddb2f58fSMasami Hiramatsu 			struct strfilter *filter __maybe_unused)
1068cf6eb489SMasami Hiramatsu {
1069cf6eb489SMasami Hiramatsu 	pr_warning("Debuginfo-analysis is not supported.\n");
1070cf6eb489SMasami Hiramatsu 	return -ENOSYS;
1071cf6eb489SMasami Hiramatsu }
1072e0faa8d3SMasami Hiramatsu #endif
1073e0faa8d3SMasami Hiramatsu 
1074e53b00d3SMasami Hiramatsu void line_range__clear(struct line_range *lr)
1075e53b00d3SMasami Hiramatsu {
1076e53b00d3SMasami Hiramatsu 	free(lr->function);
1077e53b00d3SMasami Hiramatsu 	free(lr->file);
1078e53b00d3SMasami Hiramatsu 	free(lr->path);
1079e53b00d3SMasami Hiramatsu 	free(lr->comp_dir);
10805a62257aSMasami Hiramatsu 	intlist__delete(lr->line_list);
1081e53b00d3SMasami Hiramatsu 	memset(lr, 0, sizeof(*lr));
1082e53b00d3SMasami Hiramatsu }
1083e53b00d3SMasami Hiramatsu 
10845a62257aSMasami Hiramatsu int line_range__init(struct line_range *lr)
1085e53b00d3SMasami Hiramatsu {
1086e53b00d3SMasami Hiramatsu 	memset(lr, 0, sizeof(*lr));
10875a62257aSMasami Hiramatsu 	lr->line_list = intlist__new(NULL);
10885a62257aSMasami Hiramatsu 	if (!lr->line_list)
10895a62257aSMasami Hiramatsu 		return -ENOMEM;
10905a62257aSMasami Hiramatsu 	else
10915a62257aSMasami Hiramatsu 		return 0;
1092e53b00d3SMasami Hiramatsu }
1093e53b00d3SMasami Hiramatsu 
109421dd9ae5SFranck Bui-Huu static int parse_line_num(char **ptr, int *val, const char *what)
109521dd9ae5SFranck Bui-Huu {
109621dd9ae5SFranck Bui-Huu 	const char *start = *ptr;
109721dd9ae5SFranck Bui-Huu 
109821dd9ae5SFranck Bui-Huu 	errno = 0;
109921dd9ae5SFranck Bui-Huu 	*val = strtol(*ptr, ptr, 0);
110021dd9ae5SFranck Bui-Huu 	if (errno || *ptr == start) {
110121dd9ae5SFranck Bui-Huu 		semantic_error("'%s' is not a valid number.\n", what);
110221dd9ae5SFranck Bui-Huu 		return -EINVAL;
110321dd9ae5SFranck Bui-Huu 	}
110421dd9ae5SFranck Bui-Huu 	return 0;
110521dd9ae5SFranck Bui-Huu }
110621dd9ae5SFranck Bui-Huu 
1107573709fdSMasami Hiramatsu /* Check the name is good for event, group or function */
1108573709fdSMasami Hiramatsu static bool is_c_func_name(const char *name)
1109573709fdSMasami Hiramatsu {
1110573709fdSMasami Hiramatsu 	if (!isalpha(*name) && *name != '_')
1111573709fdSMasami Hiramatsu 		return false;
1112573709fdSMasami Hiramatsu 	while (*++name != '\0') {
1113573709fdSMasami Hiramatsu 		if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1114573709fdSMasami Hiramatsu 			return false;
1115573709fdSMasami Hiramatsu 	}
1116573709fdSMasami Hiramatsu 	return true;
1117573709fdSMasami Hiramatsu }
1118573709fdSMasami Hiramatsu 
11199d95b580SFranck Bui-Huu /*
11209d95b580SFranck Bui-Huu  * Stuff 'lr' according to the line range described by 'arg'.
11219d95b580SFranck Bui-Huu  * The line range syntax is described by:
11229d95b580SFranck Bui-Huu  *
11239d95b580SFranck Bui-Huu  *         SRC[:SLN[+NUM|-ELN]]
1124e116dfa1SMasami Hiramatsu  *         FNC[@SRC][:SLN[+NUM|-ELN]]
11259d95b580SFranck Bui-Huu  */
1126146a1439SMasami Hiramatsu int parse_line_range_desc(const char *arg, struct line_range *lr)
1127631c9defSMasami Hiramatsu {
1128e116dfa1SMasami Hiramatsu 	char *range, *file, *name = strdup(arg);
112921dd9ae5SFranck Bui-Huu 	int err;
11309d95b580SFranck Bui-Huu 
113121dd9ae5SFranck Bui-Huu 	if (!name)
113221dd9ae5SFranck Bui-Huu 		return -ENOMEM;
113321dd9ae5SFranck Bui-Huu 
113421dd9ae5SFranck Bui-Huu 	lr->start = 0;
113521dd9ae5SFranck Bui-Huu 	lr->end = INT_MAX;
113621dd9ae5SFranck Bui-Huu 
113721dd9ae5SFranck Bui-Huu 	range = strchr(name, ':');
113821dd9ae5SFranck Bui-Huu 	if (range) {
113921dd9ae5SFranck Bui-Huu 		*range++ = '\0';
114021dd9ae5SFranck Bui-Huu 
114121dd9ae5SFranck Bui-Huu 		err = parse_line_num(&range, &lr->start, "start line");
114221dd9ae5SFranck Bui-Huu 		if (err)
114321dd9ae5SFranck Bui-Huu 			goto err;
114421dd9ae5SFranck Bui-Huu 
114521dd9ae5SFranck Bui-Huu 		if (*range == '+' || *range == '-') {
114621dd9ae5SFranck Bui-Huu 			const char c = *range++;
114721dd9ae5SFranck Bui-Huu 
114821dd9ae5SFranck Bui-Huu 			err = parse_line_num(&range, &lr->end, "end line");
114921dd9ae5SFranck Bui-Huu 			if (err)
115021dd9ae5SFranck Bui-Huu 				goto err;
115121dd9ae5SFranck Bui-Huu 
115221dd9ae5SFranck Bui-Huu 			if (c == '+') {
115321dd9ae5SFranck Bui-Huu 				lr->end += lr->start;
115421dd9ae5SFranck Bui-Huu 				/*
1155dda4ab34SMasami Hiramatsu 				 * Adjust the number of lines here.
1156dda4ab34SMasami Hiramatsu 				 * If the number of lines == 1, the
1157dda4ab34SMasami Hiramatsu 				 * the end of line should be equal to
1158dda4ab34SMasami Hiramatsu 				 * the start of line.
1159dda4ab34SMasami Hiramatsu 				 */
116021dd9ae5SFranck Bui-Huu 				lr->end--;
116121dd9ae5SFranck Bui-Huu 			}
116221dd9ae5SFranck Bui-Huu 		}
116321dd9ae5SFranck Bui-Huu 
1164d3b63d7aSMasami Hiramatsu 		pr_debug("Line range is %d to %d\n", lr->start, lr->end);
116521dd9ae5SFranck Bui-Huu 
116621dd9ae5SFranck Bui-Huu 		err = -EINVAL;
1167d3b63d7aSMasami Hiramatsu 		if (lr->start > lr->end) {
1168631c9defSMasami Hiramatsu 			semantic_error("Start line must be smaller"
1169146a1439SMasami Hiramatsu 				       " than end line.\n");
117021dd9ae5SFranck Bui-Huu 			goto err;
1171146a1439SMasami Hiramatsu 		}
117221dd9ae5SFranck Bui-Huu 		if (*range != '\0') {
117321dd9ae5SFranck Bui-Huu 			semantic_error("Tailing with invalid str '%s'.\n", range);
117421dd9ae5SFranck Bui-Huu 			goto err;
1175146a1439SMasami Hiramatsu 		}
1176d3b63d7aSMasami Hiramatsu 	}
117702b95dadSMasami Hiramatsu 
1178e116dfa1SMasami Hiramatsu 	file = strchr(name, '@');
1179e116dfa1SMasami Hiramatsu 	if (file) {
1180e116dfa1SMasami Hiramatsu 		*file = '\0';
1181e116dfa1SMasami Hiramatsu 		lr->file = strdup(++file);
1182e116dfa1SMasami Hiramatsu 		if (lr->file == NULL) {
1183e116dfa1SMasami Hiramatsu 			err = -ENOMEM;
1184e116dfa1SMasami Hiramatsu 			goto err;
1185e116dfa1SMasami Hiramatsu 		}
1186e116dfa1SMasami Hiramatsu 		lr->function = name;
1187573709fdSMasami Hiramatsu 	} else if (strchr(name, '/') || strchr(name, '.'))
118821dd9ae5SFranck Bui-Huu 		lr->file = name;
1189573709fdSMasami Hiramatsu 	else if (is_c_func_name(name))/* We reuse it for checking funcname */
119021dd9ae5SFranck Bui-Huu 		lr->function = name;
1191573709fdSMasami Hiramatsu 	else {	/* Invalid name */
1192573709fdSMasami Hiramatsu 		semantic_error("'%s' is not a valid function name.\n", name);
1193573709fdSMasami Hiramatsu 		err = -EINVAL;
1194573709fdSMasami Hiramatsu 		goto err;
1195573709fdSMasami Hiramatsu 	}
1196146a1439SMasami Hiramatsu 
1197146a1439SMasami Hiramatsu 	return 0;
119821dd9ae5SFranck Bui-Huu err:
119921dd9ae5SFranck Bui-Huu 	free(name);
120021dd9ae5SFranck Bui-Huu 	return err;
1201631c9defSMasami Hiramatsu }
1202631c9defSMasami Hiramatsu 
120350656eecSMasami Hiramatsu /* Parse probepoint definition. */
1204146a1439SMasami Hiramatsu static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
120550656eecSMasami Hiramatsu {
12064235b045SMasami Hiramatsu 	struct perf_probe_point *pp = &pev->point;
120750656eecSMasami Hiramatsu 	char *ptr, *tmp;
120850656eecSMasami Hiramatsu 	char c, nc = 0;
12093099c026SNaveen N. Rao 	bool file_spec = false;
121050656eecSMasami Hiramatsu 	/*
121150656eecSMasami Hiramatsu 	 * <Syntax>
12122a9c8c36SMasami Hiramatsu 	 * perf probe [EVENT=]SRC[:LN|;PTN]
12132a9c8c36SMasami Hiramatsu 	 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1214af663d75SMasami Hiramatsu 	 *
1215af663d75SMasami Hiramatsu 	 * TODO:Group name support
121650656eecSMasami Hiramatsu 	 */
1217e59d29e8SWang Nan 	if (!arg)
1218e59d29e8SWang Nan 		return -EINVAL;
121950656eecSMasami Hiramatsu 
12202a9c8c36SMasami Hiramatsu 	ptr = strpbrk(arg, ";=@+%");
12212a9c8c36SMasami Hiramatsu 	if (ptr && *ptr == '=') {	/* Event name */
1222af663d75SMasami Hiramatsu 		*ptr = '\0';
1223af663d75SMasami Hiramatsu 		tmp = ptr + 1;
1224146a1439SMasami Hiramatsu 		if (strchr(arg, ':')) {
1225146a1439SMasami Hiramatsu 			semantic_error("Group name is not supported yet.\n");
1226146a1439SMasami Hiramatsu 			return -ENOTSUP;
1227146a1439SMasami Hiramatsu 		}
1228573709fdSMasami Hiramatsu 		if (!is_c_func_name(arg)) {
1229b7702a21SMasami Hiramatsu 			semantic_error("%s is bad for event name -it must "
1230146a1439SMasami Hiramatsu 				       "follow C symbol-naming rule.\n", arg);
1231146a1439SMasami Hiramatsu 			return -EINVAL;
1232146a1439SMasami Hiramatsu 		}
123302b95dadSMasami Hiramatsu 		pev->event = strdup(arg);
123402b95dadSMasami Hiramatsu 		if (pev->event == NULL)
123502b95dadSMasami Hiramatsu 			return -ENOMEM;
12364235b045SMasami Hiramatsu 		pev->group = NULL;
1237af663d75SMasami Hiramatsu 		arg = tmp;
1238af663d75SMasami Hiramatsu 	}
1239af663d75SMasami Hiramatsu 
12403099c026SNaveen N. Rao 	/*
12413099c026SNaveen N. Rao 	 * Check arg is function or file name and copy it.
12423099c026SNaveen N. Rao 	 *
12433099c026SNaveen N. Rao 	 * We consider arg to be a file spec if and only if it satisfies
12443099c026SNaveen N. Rao 	 * all of the below criteria::
12453099c026SNaveen N. Rao 	 * - it does not include any of "+@%",
12463099c026SNaveen N. Rao 	 * - it includes one of ":;", and
12473099c026SNaveen N. Rao 	 * - it has a period '.' in the name.
12483099c026SNaveen N. Rao 	 *
12493099c026SNaveen N. Rao 	 * Otherwise, we consider arg to be a function specification.
12503099c026SNaveen N. Rao 	 */
12513099c026SNaveen N. Rao 	if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
12523099c026SNaveen N. Rao 		/* This is a file spec if it includes a '.' before ; or : */
12533099c026SNaveen N. Rao 		if (memchr(arg, '.', ptr - arg))
12543099c026SNaveen N. Rao 			file_spec = true;
12553099c026SNaveen N. Rao 	}
12563099c026SNaveen N. Rao 
12572a9c8c36SMasami Hiramatsu 	ptr = strpbrk(arg, ";:+@%");
125850656eecSMasami Hiramatsu 	if (ptr) {
125950656eecSMasami Hiramatsu 		nc = *ptr;
126050656eecSMasami Hiramatsu 		*ptr++ = '\0';
126150656eecSMasami Hiramatsu 	}
126250656eecSMasami Hiramatsu 
12636c6e024fSWang Nan 	if (arg[0] == '\0')
12646c6e024fSWang Nan 		tmp = NULL;
12656c6e024fSWang Nan 	else {
126602b95dadSMasami Hiramatsu 		tmp = strdup(arg);
126702b95dadSMasami Hiramatsu 		if (tmp == NULL)
126802b95dadSMasami Hiramatsu 			return -ENOMEM;
12696c6e024fSWang Nan 	}
127002b95dadSMasami Hiramatsu 
12713099c026SNaveen N. Rao 	if (file_spec)
127202b95dadSMasami Hiramatsu 		pp->file = tmp;
1273da15bd9dSWang Nan 	else {
127402b95dadSMasami Hiramatsu 		pp->function = tmp;
127550656eecSMasami Hiramatsu 
1276da15bd9dSWang Nan 		/*
1277da15bd9dSWang Nan 		 * Keep pp->function even if this is absolute address,
1278da15bd9dSWang Nan 		 * so it can mark whether abs_address is valid.
1279da15bd9dSWang Nan 		 * Which make 'perf probe lib.bin 0x0' possible.
1280da15bd9dSWang Nan 		 *
1281da15bd9dSWang Nan 		 * Note that checking length of tmp is not needed
1282da15bd9dSWang Nan 		 * because when we access tmp[1] we know tmp[0] is '0',
1283da15bd9dSWang Nan 		 * so tmp[1] should always valid (but could be '\0').
1284da15bd9dSWang Nan 		 */
1285da15bd9dSWang Nan 		if (tmp && !strncmp(tmp, "0x", 2)) {
1286da15bd9dSWang Nan 			pp->abs_address = strtoul(pp->function, &tmp, 0);
1287da15bd9dSWang Nan 			if (*tmp != '\0') {
1288da15bd9dSWang Nan 				semantic_error("Invalid absolute address.\n");
1289da15bd9dSWang Nan 				return -EINVAL;
1290da15bd9dSWang Nan 			}
1291da15bd9dSWang Nan 		}
1292da15bd9dSWang Nan 	}
1293da15bd9dSWang Nan 
129450656eecSMasami Hiramatsu 	/* Parse other options */
129550656eecSMasami Hiramatsu 	while (ptr) {
129650656eecSMasami Hiramatsu 		arg = ptr;
129750656eecSMasami Hiramatsu 		c = nc;
12982a9c8c36SMasami Hiramatsu 		if (c == ';') {	/* Lazy pattern must be the last part */
129902b95dadSMasami Hiramatsu 			pp->lazy_line = strdup(arg);
130002b95dadSMasami Hiramatsu 			if (pp->lazy_line == NULL)
130102b95dadSMasami Hiramatsu 				return -ENOMEM;
13022a9c8c36SMasami Hiramatsu 			break;
13032a9c8c36SMasami Hiramatsu 		}
13042a9c8c36SMasami Hiramatsu 		ptr = strpbrk(arg, ";:+@%");
130550656eecSMasami Hiramatsu 		if (ptr) {
130650656eecSMasami Hiramatsu 			nc = *ptr;
130750656eecSMasami Hiramatsu 			*ptr++ = '\0';
130850656eecSMasami Hiramatsu 		}
130950656eecSMasami Hiramatsu 		switch (c) {
131050656eecSMasami Hiramatsu 		case ':':	/* Line number */
131150656eecSMasami Hiramatsu 			pp->line = strtoul(arg, &tmp, 0);
1312146a1439SMasami Hiramatsu 			if (*tmp != '\0') {
13132a9c8c36SMasami Hiramatsu 				semantic_error("There is non-digit char"
1314146a1439SMasami Hiramatsu 					       " in line number.\n");
1315146a1439SMasami Hiramatsu 				return -EINVAL;
1316146a1439SMasami Hiramatsu 			}
131750656eecSMasami Hiramatsu 			break;
131850656eecSMasami Hiramatsu 		case '+':	/* Byte offset from a symbol */
131950656eecSMasami Hiramatsu 			pp->offset = strtoul(arg, &tmp, 0);
1320146a1439SMasami Hiramatsu 			if (*tmp != '\0') {
13212a9c8c36SMasami Hiramatsu 				semantic_error("There is non-digit character"
1322146a1439SMasami Hiramatsu 						" in offset.\n");
1323146a1439SMasami Hiramatsu 				return -EINVAL;
1324146a1439SMasami Hiramatsu 			}
132550656eecSMasami Hiramatsu 			break;
132650656eecSMasami Hiramatsu 		case '@':	/* File name */
1327146a1439SMasami Hiramatsu 			if (pp->file) {
1328146a1439SMasami Hiramatsu 				semantic_error("SRC@SRC is not allowed.\n");
1329146a1439SMasami Hiramatsu 				return -EINVAL;
1330146a1439SMasami Hiramatsu 			}
133102b95dadSMasami Hiramatsu 			pp->file = strdup(arg);
133202b95dadSMasami Hiramatsu 			if (pp->file == NULL)
133302b95dadSMasami Hiramatsu 				return -ENOMEM;
133450656eecSMasami Hiramatsu 			break;
133550656eecSMasami Hiramatsu 		case '%':	/* Probe places */
133650656eecSMasami Hiramatsu 			if (strcmp(arg, "return") == 0) {
133750656eecSMasami Hiramatsu 				pp->retprobe = 1;
1338146a1439SMasami Hiramatsu 			} else {	/* Others not supported yet */
1339146a1439SMasami Hiramatsu 				semantic_error("%%%s is not supported.\n", arg);
1340146a1439SMasami Hiramatsu 				return -ENOTSUP;
1341146a1439SMasami Hiramatsu 			}
134250656eecSMasami Hiramatsu 			break;
1343146a1439SMasami Hiramatsu 		default:	/* Buggy case */
1344146a1439SMasami Hiramatsu 			pr_err("This program has a bug at %s:%d.\n",
1345146a1439SMasami Hiramatsu 				__FILE__, __LINE__);
1346146a1439SMasami Hiramatsu 			return -ENOTSUP;
134750656eecSMasami Hiramatsu 			break;
134850656eecSMasami Hiramatsu 		}
134950656eecSMasami Hiramatsu 	}
135050656eecSMasami Hiramatsu 
135150656eecSMasami Hiramatsu 	/* Exclusion check */
1352146a1439SMasami Hiramatsu 	if (pp->lazy_line && pp->line) {
13530e43e5d2SMasami Hiramatsu 		semantic_error("Lazy pattern can't be used with"
13540e43e5d2SMasami Hiramatsu 			       " line number.\n");
1355146a1439SMasami Hiramatsu 		return -EINVAL;
1356146a1439SMasami Hiramatsu 	}
13572a9c8c36SMasami Hiramatsu 
1358146a1439SMasami Hiramatsu 	if (pp->lazy_line && pp->offset) {
13590e43e5d2SMasami Hiramatsu 		semantic_error("Lazy pattern can't be used with offset.\n");
1360146a1439SMasami Hiramatsu 		return -EINVAL;
1361146a1439SMasami Hiramatsu 	}
13622a9c8c36SMasami Hiramatsu 
1363146a1439SMasami Hiramatsu 	if (pp->line && pp->offset) {
13640e43e5d2SMasami Hiramatsu 		semantic_error("Offset can't be used with line number.\n");
1365146a1439SMasami Hiramatsu 		return -EINVAL;
1366146a1439SMasami Hiramatsu 	}
136750656eecSMasami Hiramatsu 
1368146a1439SMasami Hiramatsu 	if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
13692a9c8c36SMasami Hiramatsu 		semantic_error("File always requires line number or "
13700e43e5d2SMasami Hiramatsu 			       "lazy pattern.\n");
1371146a1439SMasami Hiramatsu 		return -EINVAL;
1372146a1439SMasami Hiramatsu 	}
137350656eecSMasami Hiramatsu 
1374146a1439SMasami Hiramatsu 	if (pp->offset && !pp->function) {
13750e43e5d2SMasami Hiramatsu 		semantic_error("Offset requires an entry function.\n");
1376146a1439SMasami Hiramatsu 		return -EINVAL;
1377146a1439SMasami Hiramatsu 	}
137850656eecSMasami Hiramatsu 
1379146a1439SMasami Hiramatsu 	if (pp->retprobe && !pp->function) {
13800e43e5d2SMasami Hiramatsu 		semantic_error("Return probe requires an entry function.\n");
1381146a1439SMasami Hiramatsu 		return -EINVAL;
1382146a1439SMasami Hiramatsu 	}
138350656eecSMasami Hiramatsu 
1384146a1439SMasami Hiramatsu 	if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
13852a9c8c36SMasami Hiramatsu 		semantic_error("Offset/Line/Lazy pattern can't be used with "
13860e43e5d2SMasami Hiramatsu 			       "return probe.\n");
1387146a1439SMasami Hiramatsu 		return -EINVAL;
1388146a1439SMasami Hiramatsu 	}
138950656eecSMasami Hiramatsu 
13904235b045SMasami Hiramatsu 	pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
13912a9c8c36SMasami Hiramatsu 		 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
13922a9c8c36SMasami Hiramatsu 		 pp->lazy_line);
1393146a1439SMasami Hiramatsu 	return 0;
139450656eecSMasami Hiramatsu }
139550656eecSMasami Hiramatsu 
13967df2f329SMasami Hiramatsu /* Parse perf-probe event argument */
1397146a1439SMasami Hiramatsu static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
13987df2f329SMasami Hiramatsu {
1399b2a3c12bSMasami Hiramatsu 	char *tmp, *goodname;
14007df2f329SMasami Hiramatsu 	struct perf_probe_arg_field **fieldp;
14017df2f329SMasami Hiramatsu 
14027df2f329SMasami Hiramatsu 	pr_debug("parsing arg: %s into ", str);
14037df2f329SMasami Hiramatsu 
140448481938SMasami Hiramatsu 	tmp = strchr(str, '=');
140548481938SMasami Hiramatsu 	if (tmp) {
140602b95dadSMasami Hiramatsu 		arg->name = strndup(str, tmp - str);
140702b95dadSMasami Hiramatsu 		if (arg->name == NULL)
140802b95dadSMasami Hiramatsu 			return -ENOMEM;
140911a1ca35SMasami Hiramatsu 		pr_debug("name:%s ", arg->name);
141048481938SMasami Hiramatsu 		str = tmp + 1;
141148481938SMasami Hiramatsu 	}
141248481938SMasami Hiramatsu 
141311a1ca35SMasami Hiramatsu 	tmp = strchr(str, ':');
141411a1ca35SMasami Hiramatsu 	if (tmp) {	/* Type setting */
141511a1ca35SMasami Hiramatsu 		*tmp = '\0';
141602b95dadSMasami Hiramatsu 		arg->type = strdup(tmp + 1);
141702b95dadSMasami Hiramatsu 		if (arg->type == NULL)
141802b95dadSMasami Hiramatsu 			return -ENOMEM;
141911a1ca35SMasami Hiramatsu 		pr_debug("type:%s ", arg->type);
142011a1ca35SMasami Hiramatsu 	}
142111a1ca35SMasami Hiramatsu 
1422b2a3c12bSMasami Hiramatsu 	tmp = strpbrk(str, "-.[");
14237df2f329SMasami Hiramatsu 	if (!is_c_varname(str) || !tmp) {
14247df2f329SMasami Hiramatsu 		/* A variable, register, symbol or special value */
142502b95dadSMasami Hiramatsu 		arg->var = strdup(str);
142602b95dadSMasami Hiramatsu 		if (arg->var == NULL)
142702b95dadSMasami Hiramatsu 			return -ENOMEM;
142848481938SMasami Hiramatsu 		pr_debug("%s\n", arg->var);
1429146a1439SMasami Hiramatsu 		return 0;
14307df2f329SMasami Hiramatsu 	}
14317df2f329SMasami Hiramatsu 
1432b2a3c12bSMasami Hiramatsu 	/* Structure fields or array element */
143302b95dadSMasami Hiramatsu 	arg->var = strndup(str, tmp - str);
143402b95dadSMasami Hiramatsu 	if (arg->var == NULL)
143502b95dadSMasami Hiramatsu 		return -ENOMEM;
1436b2a3c12bSMasami Hiramatsu 	goodname = arg->var;
143748481938SMasami Hiramatsu 	pr_debug("%s, ", arg->var);
14387df2f329SMasami Hiramatsu 	fieldp = &arg->field;
14397df2f329SMasami Hiramatsu 
14407df2f329SMasami Hiramatsu 	do {
1441e334016fSMasami Hiramatsu 		*fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1442e334016fSMasami Hiramatsu 		if (*fieldp == NULL)
1443e334016fSMasami Hiramatsu 			return -ENOMEM;
1444b2a3c12bSMasami Hiramatsu 		if (*tmp == '[') {	/* Array */
1445b2a3c12bSMasami Hiramatsu 			str = tmp;
1446b2a3c12bSMasami Hiramatsu 			(*fieldp)->index = strtol(str + 1, &tmp, 0);
1447b2a3c12bSMasami Hiramatsu 			(*fieldp)->ref = true;
1448b2a3c12bSMasami Hiramatsu 			if (*tmp != ']' || tmp == str + 1) {
1449b2a3c12bSMasami Hiramatsu 				semantic_error("Array index must be a"
1450b2a3c12bSMasami Hiramatsu 						" number.\n");
1451b2a3c12bSMasami Hiramatsu 				return -EINVAL;
1452b2a3c12bSMasami Hiramatsu 			}
1453b2a3c12bSMasami Hiramatsu 			tmp++;
1454b2a3c12bSMasami Hiramatsu 			if (*tmp == '\0')
1455b2a3c12bSMasami Hiramatsu 				tmp = NULL;
1456b2a3c12bSMasami Hiramatsu 		} else {		/* Structure */
14577df2f329SMasami Hiramatsu 			if (*tmp == '.') {
14587df2f329SMasami Hiramatsu 				str = tmp + 1;
14597df2f329SMasami Hiramatsu 				(*fieldp)->ref = false;
14607df2f329SMasami Hiramatsu 			} else if (tmp[1] == '>') {
14617df2f329SMasami Hiramatsu 				str = tmp + 2;
14627df2f329SMasami Hiramatsu 				(*fieldp)->ref = true;
1463146a1439SMasami Hiramatsu 			} else {
1464b2a3c12bSMasami Hiramatsu 				semantic_error("Argument parse error: %s\n",
1465b2a3c12bSMasami Hiramatsu 					       str);
1466146a1439SMasami Hiramatsu 				return -EINVAL;
1467146a1439SMasami Hiramatsu 			}
1468b2a3c12bSMasami Hiramatsu 			tmp = strpbrk(str, "-.[");
1469b2a3c12bSMasami Hiramatsu 		}
14707df2f329SMasami Hiramatsu 		if (tmp) {
147102b95dadSMasami Hiramatsu 			(*fieldp)->name = strndup(str, tmp - str);
147202b95dadSMasami Hiramatsu 			if ((*fieldp)->name == NULL)
147302b95dadSMasami Hiramatsu 				return -ENOMEM;
1474b2a3c12bSMasami Hiramatsu 			if (*str != '[')
1475b2a3c12bSMasami Hiramatsu 				goodname = (*fieldp)->name;
14767df2f329SMasami Hiramatsu 			pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
14777df2f329SMasami Hiramatsu 			fieldp = &(*fieldp)->next;
14787df2f329SMasami Hiramatsu 		}
14797df2f329SMasami Hiramatsu 	} while (tmp);
148002b95dadSMasami Hiramatsu 	(*fieldp)->name = strdup(str);
148102b95dadSMasami Hiramatsu 	if ((*fieldp)->name == NULL)
148202b95dadSMasami Hiramatsu 		return -ENOMEM;
1483b2a3c12bSMasami Hiramatsu 	if (*str != '[')
1484b2a3c12bSMasami Hiramatsu 		goodname = (*fieldp)->name;
14857df2f329SMasami Hiramatsu 	pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1486df0faf4bSMasami Hiramatsu 
1487b2a3c12bSMasami Hiramatsu 	/* If no name is specified, set the last field name (not array index)*/
148802b95dadSMasami Hiramatsu 	if (!arg->name) {
1489b2a3c12bSMasami Hiramatsu 		arg->name = strdup(goodname);
149002b95dadSMasami Hiramatsu 		if (arg->name == NULL)
149102b95dadSMasami Hiramatsu 			return -ENOMEM;
149202b95dadSMasami Hiramatsu 	}
1493146a1439SMasami Hiramatsu 	return 0;
14947df2f329SMasami Hiramatsu }
14957df2f329SMasami Hiramatsu 
14964235b045SMasami Hiramatsu /* Parse perf-probe event command */
1497146a1439SMasami Hiramatsu int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
149850656eecSMasami Hiramatsu {
1499e1c01d61SMasami Hiramatsu 	char **argv;
1500146a1439SMasami Hiramatsu 	int argc, i, ret = 0;
1501fac13fd5SMasami Hiramatsu 
15024235b045SMasami Hiramatsu 	argv = argv_split(cmd, &argc);
1503146a1439SMasami Hiramatsu 	if (!argv) {
1504146a1439SMasami Hiramatsu 		pr_debug("Failed to split arguments.\n");
1505146a1439SMasami Hiramatsu 		return -ENOMEM;
1506146a1439SMasami Hiramatsu 	}
1507146a1439SMasami Hiramatsu 	if (argc - 1 > MAX_PROBE_ARGS) {
1508146a1439SMasami Hiramatsu 		semantic_error("Too many probe arguments (%d).\n", argc - 1);
1509146a1439SMasami Hiramatsu 		ret = -ERANGE;
1510146a1439SMasami Hiramatsu 		goto out;
1511146a1439SMasami Hiramatsu 	}
151250656eecSMasami Hiramatsu 	/* Parse probe point */
1513146a1439SMasami Hiramatsu 	ret = parse_perf_probe_point(argv[0], pev);
1514146a1439SMasami Hiramatsu 	if (ret < 0)
1515146a1439SMasami Hiramatsu 		goto out;
151650656eecSMasami Hiramatsu 
1517e1c01d61SMasami Hiramatsu 	/* Copy arguments and ensure return probe has no C argument */
15184235b045SMasami Hiramatsu 	pev->nargs = argc - 1;
1519e334016fSMasami Hiramatsu 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1520e334016fSMasami Hiramatsu 	if (pev->args == NULL) {
1521e334016fSMasami Hiramatsu 		ret = -ENOMEM;
1522e334016fSMasami Hiramatsu 		goto out;
1523e334016fSMasami Hiramatsu 	}
1524146a1439SMasami Hiramatsu 	for (i = 0; i < pev->nargs && ret >= 0; i++) {
1525146a1439SMasami Hiramatsu 		ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1526146a1439SMasami Hiramatsu 		if (ret >= 0 &&
1527146a1439SMasami Hiramatsu 		    is_c_varname(pev->args[i].var) && pev->point.retprobe) {
15284235b045SMasami Hiramatsu 			semantic_error("You can't specify local variable for"
1529146a1439SMasami Hiramatsu 				       " kretprobe.\n");
1530146a1439SMasami Hiramatsu 			ret = -EINVAL;
1531e1c01d61SMasami Hiramatsu 		}
1532146a1439SMasami Hiramatsu 	}
1533146a1439SMasami Hiramatsu out:
1534e1c01d61SMasami Hiramatsu 	argv_free(argv);
1535146a1439SMasami Hiramatsu 
1536146a1439SMasami Hiramatsu 	return ret;
153750656eecSMasami Hiramatsu }
153850656eecSMasami Hiramatsu 
15394235b045SMasami Hiramatsu /* Return true if this perf_probe_event requires debuginfo */
15404235b045SMasami Hiramatsu bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
15414de189feSMasami Hiramatsu {
15424235b045SMasami Hiramatsu 	int i;
15434235b045SMasami Hiramatsu 
15444235b045SMasami Hiramatsu 	if (pev->point.file || pev->point.line || pev->point.lazy_line)
15454235b045SMasami Hiramatsu 		return true;
15464235b045SMasami Hiramatsu 
15474235b045SMasami Hiramatsu 	for (i = 0; i < pev->nargs; i++)
154848481938SMasami Hiramatsu 		if (is_c_varname(pev->args[i].var))
15494235b045SMasami Hiramatsu 			return true;
15504235b045SMasami Hiramatsu 
15514235b045SMasami Hiramatsu 	return false;
15524235b045SMasami Hiramatsu }
15534235b045SMasami Hiramatsu 
15540e60836bSSrikar Dronamraju /* Parse probe_events event into struct probe_point */
155592f6c72eSMasami Hiramatsu int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
15564235b045SMasami Hiramatsu {
15570e60836bSSrikar Dronamraju 	struct probe_trace_point *tp = &tev->point;
15584de189feSMasami Hiramatsu 	char pr;
15594de189feSMasami Hiramatsu 	char *p;
1560bcbd0040SIrina Tirdea 	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
15614de189feSMasami Hiramatsu 	int ret, i, argc;
15624de189feSMasami Hiramatsu 	char **argv;
15634de189feSMasami Hiramatsu 
15640e60836bSSrikar Dronamraju 	pr_debug("Parsing probe_events: %s\n", cmd);
15654235b045SMasami Hiramatsu 	argv = argv_split(cmd, &argc);
1566146a1439SMasami Hiramatsu 	if (!argv) {
1567146a1439SMasami Hiramatsu 		pr_debug("Failed to split arguments.\n");
1568146a1439SMasami Hiramatsu 		return -ENOMEM;
1569146a1439SMasami Hiramatsu 	}
1570146a1439SMasami Hiramatsu 	if (argc < 2) {
1571146a1439SMasami Hiramatsu 		semantic_error("Too few probe arguments.\n");
1572146a1439SMasami Hiramatsu 		ret = -ERANGE;
1573146a1439SMasami Hiramatsu 		goto out;
1574146a1439SMasami Hiramatsu 	}
15754de189feSMasami Hiramatsu 
15764de189feSMasami Hiramatsu 	/* Scan event and group name. */
1577bcbd0040SIrina Tirdea 	argv0_str = strdup(argv[0]);
1578bcbd0040SIrina Tirdea 	if (argv0_str == NULL) {
1579bcbd0040SIrina Tirdea 		ret = -ENOMEM;
1580bcbd0040SIrina Tirdea 		goto out;
1581bcbd0040SIrina Tirdea 	}
1582bcbd0040SIrina Tirdea 	fmt1_str = strtok_r(argv0_str, ":", &fmt);
1583bcbd0040SIrina Tirdea 	fmt2_str = strtok_r(NULL, "/", &fmt);
1584bcbd0040SIrina Tirdea 	fmt3_str = strtok_r(NULL, " \t", &fmt);
1585bcbd0040SIrina Tirdea 	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1586bcbd0040SIrina Tirdea 	    || fmt3_str == NULL) {
1587146a1439SMasami Hiramatsu 		semantic_error("Failed to parse event name: %s\n", argv[0]);
1588146a1439SMasami Hiramatsu 		ret = -EINVAL;
1589146a1439SMasami Hiramatsu 		goto out;
1590146a1439SMasami Hiramatsu 	}
1591bcbd0040SIrina Tirdea 	pr = fmt1_str[0];
1592bcbd0040SIrina Tirdea 	tev->group = strdup(fmt2_str);
1593bcbd0040SIrina Tirdea 	tev->event = strdup(fmt3_str);
1594bcbd0040SIrina Tirdea 	if (tev->group == NULL || tev->event == NULL) {
1595bcbd0040SIrina Tirdea 		ret = -ENOMEM;
1596bcbd0040SIrina Tirdea 		goto out;
1597bcbd0040SIrina Tirdea 	}
15984235b045SMasami Hiramatsu 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
15994de189feSMasami Hiramatsu 
16004235b045SMasami Hiramatsu 	tp->retprobe = (pr == 'r');
16014de189feSMasami Hiramatsu 
1602190b57fcSMasami Hiramatsu 	/* Scan module name(if there), function name and offset */
1603190b57fcSMasami Hiramatsu 	p = strchr(argv[1], ':');
1604190b57fcSMasami Hiramatsu 	if (p) {
1605190b57fcSMasami Hiramatsu 		tp->module = strndup(argv[1], p - argv[1]);
1606190b57fcSMasami Hiramatsu 		p++;
1607190b57fcSMasami Hiramatsu 	} else
1608190b57fcSMasami Hiramatsu 		p = argv[1];
1609bcbd0040SIrina Tirdea 	fmt1_str = strtok_r(p, "+", &fmt);
1610be07afe9SWang Nan 	/* only the address started with 0x */
1611be07afe9SWang Nan 	if (fmt1_str[0] == '0')	{
1612be07afe9SWang Nan 		/*
1613be07afe9SWang Nan 		 * Fix a special case:
1614be07afe9SWang Nan 		 * if address == 0, kernel reports something like:
1615be07afe9SWang Nan 		 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x          (null) arg1=%ax
1616be07afe9SWang Nan 		 * Newer kernel may fix that, but we want to
1617be07afe9SWang Nan 		 * support old kernel also.
1618be07afe9SWang Nan 		 */
1619be07afe9SWang Nan 		if (strcmp(fmt1_str, "0x") == 0) {
1620be07afe9SWang Nan 			if (!argv[2] || strcmp(argv[2], "(null)")) {
1621be07afe9SWang Nan 				ret = -EINVAL;
1622be07afe9SWang Nan 				goto out;
1623be07afe9SWang Nan 			}
1624be07afe9SWang Nan 			tp->address = 0;
1625be07afe9SWang Nan 
1626be07afe9SWang Nan 			free(argv[2]);
1627be07afe9SWang Nan 			for (i = 2; argv[i + 1] != NULL; i++)
1628be07afe9SWang Nan 				argv[i] = argv[i + 1];
1629be07afe9SWang Nan 
1630be07afe9SWang Nan 			argv[i] = NULL;
1631be07afe9SWang Nan 			argc -= 1;
1632be07afe9SWang Nan 		} else
16335a6f6314SMasami Hiramatsu 			tp->address = strtoul(fmt1_str, NULL, 0);
1634be07afe9SWang Nan 	} else {
16355a6f6314SMasami Hiramatsu 		/* Only the symbol-based probe has offset */
1636bcbd0040SIrina Tirdea 		tp->symbol = strdup(fmt1_str);
1637bcbd0040SIrina Tirdea 		if (tp->symbol == NULL) {
1638bcbd0040SIrina Tirdea 			ret = -ENOMEM;
1639bcbd0040SIrina Tirdea 			goto out;
1640bcbd0040SIrina Tirdea 		}
1641bcbd0040SIrina Tirdea 		fmt2_str = strtok_r(NULL, "", &fmt);
1642bcbd0040SIrina Tirdea 		if (fmt2_str == NULL)
16434235b045SMasami Hiramatsu 			tp->offset = 0;
1644bcbd0040SIrina Tirdea 		else
1645bcbd0040SIrina Tirdea 			tp->offset = strtoul(fmt2_str, NULL, 10);
16465a6f6314SMasami Hiramatsu 	}
16474de189feSMasami Hiramatsu 
16484235b045SMasami Hiramatsu 	tev->nargs = argc - 2;
16490e60836bSSrikar Dronamraju 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1650e334016fSMasami Hiramatsu 	if (tev->args == NULL) {
1651e334016fSMasami Hiramatsu 		ret = -ENOMEM;
1652e334016fSMasami Hiramatsu 		goto out;
1653e334016fSMasami Hiramatsu 	}
16544235b045SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++) {
16554de189feSMasami Hiramatsu 		p = strchr(argv[i + 2], '=');
16564de189feSMasami Hiramatsu 		if (p)	/* We don't need which register is assigned. */
16574235b045SMasami Hiramatsu 			*p++ = '\0';
16584235b045SMasami Hiramatsu 		else
16594235b045SMasami Hiramatsu 			p = argv[i + 2];
166002b95dadSMasami Hiramatsu 		tev->args[i].name = strdup(argv[i + 2]);
16614235b045SMasami Hiramatsu 		/* TODO: parse regs and offset */
166202b95dadSMasami Hiramatsu 		tev->args[i].value = strdup(p);
166302b95dadSMasami Hiramatsu 		if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
166402b95dadSMasami Hiramatsu 			ret = -ENOMEM;
166502b95dadSMasami Hiramatsu 			goto out;
166602b95dadSMasami Hiramatsu 		}
16674de189feSMasami Hiramatsu 	}
1668146a1439SMasami Hiramatsu 	ret = 0;
1669146a1439SMasami Hiramatsu out:
1670bcbd0040SIrina Tirdea 	free(argv0_str);
16714de189feSMasami Hiramatsu 	argv_free(argv);
1672146a1439SMasami Hiramatsu 	return ret;
16734de189feSMasami Hiramatsu }
16744de189feSMasami Hiramatsu 
16757df2f329SMasami Hiramatsu /* Compose only probe arg */
16767df2f329SMasami Hiramatsu int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
16777df2f329SMasami Hiramatsu {
16787df2f329SMasami Hiramatsu 	struct perf_probe_arg_field *field = pa->field;
16797df2f329SMasami Hiramatsu 	int ret;
16807df2f329SMasami Hiramatsu 	char *tmp = buf;
16817df2f329SMasami Hiramatsu 
168248481938SMasami Hiramatsu 	if (pa->name && pa->var)
168348481938SMasami Hiramatsu 		ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
168448481938SMasami Hiramatsu 	else
168548481938SMasami Hiramatsu 		ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
16867df2f329SMasami Hiramatsu 	if (ret <= 0)
16877df2f329SMasami Hiramatsu 		goto error;
16887df2f329SMasami Hiramatsu 	tmp += ret;
16897df2f329SMasami Hiramatsu 	len -= ret;
16907df2f329SMasami Hiramatsu 
16917df2f329SMasami Hiramatsu 	while (field) {
1692b2a3c12bSMasami Hiramatsu 		if (field->name[0] == '[')
1693b2a3c12bSMasami Hiramatsu 			ret = e_snprintf(tmp, len, "%s", field->name);
1694b2a3c12bSMasami Hiramatsu 		else
1695b2a3c12bSMasami Hiramatsu 			ret = e_snprintf(tmp, len, "%s%s",
1696b2a3c12bSMasami Hiramatsu 					 field->ref ? "->" : ".", field->name);
16977df2f329SMasami Hiramatsu 		if (ret <= 0)
16987df2f329SMasami Hiramatsu 			goto error;
16997df2f329SMasami Hiramatsu 		tmp += ret;
17007df2f329SMasami Hiramatsu 		len -= ret;
17017df2f329SMasami Hiramatsu 		field = field->next;
17027df2f329SMasami Hiramatsu 	}
170311a1ca35SMasami Hiramatsu 
170411a1ca35SMasami Hiramatsu 	if (pa->type) {
170511a1ca35SMasami Hiramatsu 		ret = e_snprintf(tmp, len, ":%s", pa->type);
170611a1ca35SMasami Hiramatsu 		if (ret <= 0)
170711a1ca35SMasami Hiramatsu 			goto error;
170811a1ca35SMasami Hiramatsu 		tmp += ret;
170911a1ca35SMasami Hiramatsu 		len -= ret;
171011a1ca35SMasami Hiramatsu 	}
171111a1ca35SMasami Hiramatsu 
17127df2f329SMasami Hiramatsu 	return tmp - buf;
17137df2f329SMasami Hiramatsu error:
17145f03cba4SMasami Hiramatsu 	pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1715146a1439SMasami Hiramatsu 	return ret;
17167df2f329SMasami Hiramatsu }
17177df2f329SMasami Hiramatsu 
17184235b045SMasami Hiramatsu /* Compose only probe point (not argument) */
17194235b045SMasami Hiramatsu static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
172050656eecSMasami Hiramatsu {
1721fb1587d8SMasami Hiramatsu 	char *buf, *tmp;
1722fb1587d8SMasami Hiramatsu 	char offs[32] = "", line[32] = "", file[32] = "";
1723fb1587d8SMasami Hiramatsu 	int ret, len;
172450656eecSMasami Hiramatsu 
1725e334016fSMasami Hiramatsu 	buf = zalloc(MAX_CMDLEN);
1726e334016fSMasami Hiramatsu 	if (buf == NULL) {
1727e334016fSMasami Hiramatsu 		ret = -ENOMEM;
1728e334016fSMasami Hiramatsu 		goto error;
1729e334016fSMasami Hiramatsu 	}
17304de189feSMasami Hiramatsu 	if (pp->offset) {
1731fb1587d8SMasami Hiramatsu 		ret = e_snprintf(offs, 32, "+%lu", pp->offset);
17324de189feSMasami Hiramatsu 		if (ret <= 0)
17334de189feSMasami Hiramatsu 			goto error;
17344de189feSMasami Hiramatsu 	}
17354de189feSMasami Hiramatsu 	if (pp->line) {
1736fb1587d8SMasami Hiramatsu 		ret = e_snprintf(line, 32, ":%d", pp->line);
1737fb1587d8SMasami Hiramatsu 		if (ret <= 0)
1738fb1587d8SMasami Hiramatsu 			goto error;
1739fb1587d8SMasami Hiramatsu 	}
1740fb1587d8SMasami Hiramatsu 	if (pp->file) {
174132ae2adeSFranck Bui-Huu 		tmp = pp->file;
174232ae2adeSFranck Bui-Huu 		len = strlen(tmp);
174332ae2adeSFranck Bui-Huu 		if (len > 30) {
174432ae2adeSFranck Bui-Huu 			tmp = strchr(pp->file + len - 30, '/');
174532ae2adeSFranck Bui-Huu 			tmp = tmp ? tmp + 1 : pp->file + len - 30;
174632ae2adeSFranck Bui-Huu 		}
174732ae2adeSFranck Bui-Huu 		ret = e_snprintf(file, 32, "@%s", tmp);
17484de189feSMasami Hiramatsu 		if (ret <= 0)
17494de189feSMasami Hiramatsu 			goto error;
17504de189feSMasami Hiramatsu 	}
17514de189feSMasami Hiramatsu 
17524de189feSMasami Hiramatsu 	if (pp->function)
1753fb1587d8SMasami Hiramatsu 		ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1754fb1587d8SMasami Hiramatsu 				 offs, pp->retprobe ? "%return" : "", line,
1755fb1587d8SMasami Hiramatsu 				 file);
17564de189feSMasami Hiramatsu 	else
1757fb1587d8SMasami Hiramatsu 		ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
17584235b045SMasami Hiramatsu 	if (ret <= 0)
17594235b045SMasami Hiramatsu 		goto error;
17604235b045SMasami Hiramatsu 
17614235b045SMasami Hiramatsu 	return buf;
17624235b045SMasami Hiramatsu error:
17635f03cba4SMasami Hiramatsu 	pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1764146a1439SMasami Hiramatsu 	free(buf);
1765146a1439SMasami Hiramatsu 	return NULL;
17664235b045SMasami Hiramatsu }
17674235b045SMasami Hiramatsu 
17684235b045SMasami Hiramatsu #if 0
17694235b045SMasami Hiramatsu char *synthesize_perf_probe_command(struct perf_probe_event *pev)
17704235b045SMasami Hiramatsu {
17714235b045SMasami Hiramatsu 	char *buf;
17724235b045SMasami Hiramatsu 	int i, len, ret;
17734235b045SMasami Hiramatsu 
17744235b045SMasami Hiramatsu 	buf = synthesize_perf_probe_point(&pev->point);
17754235b045SMasami Hiramatsu 	if (!buf)
17764235b045SMasami Hiramatsu 		return NULL;
17774235b045SMasami Hiramatsu 
17784235b045SMasami Hiramatsu 	len = strlen(buf);
17794235b045SMasami Hiramatsu 	for (i = 0; i < pev->nargs; i++) {
17804235b045SMasami Hiramatsu 		ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
17814235b045SMasami Hiramatsu 				 pev->args[i].name);
17827ef17aafSMasami Hiramatsu 		if (ret <= 0) {
17834235b045SMasami Hiramatsu 			free(buf);
17844235b045SMasami Hiramatsu 			return NULL;
17857ef17aafSMasami Hiramatsu 		}
17864235b045SMasami Hiramatsu 		len += ret;
17877ef17aafSMasami Hiramatsu 	}
178850656eecSMasami Hiramatsu 
17894235b045SMasami Hiramatsu 	return buf;
17904235b045SMasami Hiramatsu }
17914235b045SMasami Hiramatsu #endif
17924235b045SMasami Hiramatsu 
17930e60836bSSrikar Dronamraju static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
17944235b045SMasami Hiramatsu 					     char **buf, size_t *buflen,
17954235b045SMasami Hiramatsu 					     int depth)
17967ef17aafSMasami Hiramatsu {
17974235b045SMasami Hiramatsu 	int ret;
17984235b045SMasami Hiramatsu 	if (ref->next) {
17990e60836bSSrikar Dronamraju 		depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
18004235b045SMasami Hiramatsu 							 buflen, depth + 1);
18014235b045SMasami Hiramatsu 		if (depth < 0)
18024235b045SMasami Hiramatsu 			goto out;
18034235b045SMasami Hiramatsu 	}
18044235b045SMasami Hiramatsu 
18054235b045SMasami Hiramatsu 	ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
18064235b045SMasami Hiramatsu 	if (ret < 0)
18074235b045SMasami Hiramatsu 		depth = ret;
18084235b045SMasami Hiramatsu 	else {
18094235b045SMasami Hiramatsu 		*buf += ret;
18104235b045SMasami Hiramatsu 		*buflen -= ret;
18114235b045SMasami Hiramatsu 	}
18124235b045SMasami Hiramatsu out:
18134235b045SMasami Hiramatsu 	return depth;
18144235b045SMasami Hiramatsu 
18154235b045SMasami Hiramatsu }
18164235b045SMasami Hiramatsu 
18170e60836bSSrikar Dronamraju static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
18184235b045SMasami Hiramatsu 				       char *buf, size_t buflen)
18194235b045SMasami Hiramatsu {
18200e60836bSSrikar Dronamraju 	struct probe_trace_arg_ref *ref = arg->ref;
18214235b045SMasami Hiramatsu 	int ret, depth = 0;
18224235b045SMasami Hiramatsu 	char *tmp = buf;
18234235b045SMasami Hiramatsu 
18244235b045SMasami Hiramatsu 	/* Argument name or separator */
18254235b045SMasami Hiramatsu 	if (arg->name)
18264235b045SMasami Hiramatsu 		ret = e_snprintf(buf, buflen, " %s=", arg->name);
18274235b045SMasami Hiramatsu 	else
18284235b045SMasami Hiramatsu 		ret = e_snprintf(buf, buflen, " ");
18294235b045SMasami Hiramatsu 	if (ret < 0)
18304235b045SMasami Hiramatsu 		return ret;
18314235b045SMasami Hiramatsu 	buf += ret;
18324235b045SMasami Hiramatsu 	buflen -= ret;
18334235b045SMasami Hiramatsu 
1834b7dcb857SMasami Hiramatsu 	/* Special case: @XXX */
1835b7dcb857SMasami Hiramatsu 	if (arg->value[0] == '@' && arg->ref)
1836b7dcb857SMasami Hiramatsu 			ref = ref->next;
1837b7dcb857SMasami Hiramatsu 
18384235b045SMasami Hiramatsu 	/* Dereferencing arguments */
1839b7dcb857SMasami Hiramatsu 	if (ref) {
18400e60836bSSrikar Dronamraju 		depth = __synthesize_probe_trace_arg_ref(ref, &buf,
18414235b045SMasami Hiramatsu 							  &buflen, 1);
18424235b045SMasami Hiramatsu 		if (depth < 0)
18434235b045SMasami Hiramatsu 			return depth;
18444235b045SMasami Hiramatsu 	}
18454235b045SMasami Hiramatsu 
18464235b045SMasami Hiramatsu 	/* Print argument value */
1847b7dcb857SMasami Hiramatsu 	if (arg->value[0] == '@' && arg->ref)
1848b7dcb857SMasami Hiramatsu 		ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1849b7dcb857SMasami Hiramatsu 				 arg->ref->offset);
1850b7dcb857SMasami Hiramatsu 	else
18514235b045SMasami Hiramatsu 		ret = e_snprintf(buf, buflen, "%s", arg->value);
18524235b045SMasami Hiramatsu 	if (ret < 0)
18534235b045SMasami Hiramatsu 		return ret;
18544235b045SMasami Hiramatsu 	buf += ret;
18554235b045SMasami Hiramatsu 	buflen -= ret;
18564235b045SMasami Hiramatsu 
18574235b045SMasami Hiramatsu 	/* Closing */
18584235b045SMasami Hiramatsu 	while (depth--) {
18594235b045SMasami Hiramatsu 		ret = e_snprintf(buf, buflen, ")");
18604235b045SMasami Hiramatsu 		if (ret < 0)
18614235b045SMasami Hiramatsu 			return ret;
18624235b045SMasami Hiramatsu 		buf += ret;
18634235b045SMasami Hiramatsu 		buflen -= ret;
18644235b045SMasami Hiramatsu 	}
18654984912eSMasami Hiramatsu 	/* Print argument type */
18664984912eSMasami Hiramatsu 	if (arg->type) {
18674984912eSMasami Hiramatsu 		ret = e_snprintf(buf, buflen, ":%s", arg->type);
18684984912eSMasami Hiramatsu 		if (ret <= 0)
18694984912eSMasami Hiramatsu 			return ret;
18704984912eSMasami Hiramatsu 		buf += ret;
18714984912eSMasami Hiramatsu 	}
18724235b045SMasami Hiramatsu 
18734235b045SMasami Hiramatsu 	return buf - tmp;
18744235b045SMasami Hiramatsu }
18754235b045SMasami Hiramatsu 
18760e60836bSSrikar Dronamraju char *synthesize_probe_trace_command(struct probe_trace_event *tev)
18774235b045SMasami Hiramatsu {
18780e60836bSSrikar Dronamraju 	struct probe_trace_point *tp = &tev->point;
18797ef17aafSMasami Hiramatsu 	char *buf;
18807ef17aafSMasami Hiramatsu 	int i, len, ret;
18817ef17aafSMasami Hiramatsu 
1882e334016fSMasami Hiramatsu 	buf = zalloc(MAX_CMDLEN);
1883e334016fSMasami Hiramatsu 	if (buf == NULL)
1884e334016fSMasami Hiramatsu 		return NULL;
1885e334016fSMasami Hiramatsu 
1886eb948e50SMasami Hiramatsu 	len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1887eb948e50SMasami Hiramatsu 			 tev->group, tev->event);
1888eb948e50SMasami Hiramatsu 	if (len <= 0)
1889eb948e50SMasami Hiramatsu 		goto error;
1890eb948e50SMasami Hiramatsu 
1891da15bd9dSWang Nan 	/* Uprobes must have tp->module */
1892da15bd9dSWang Nan 	if (tev->uprobes && !tp->module)
1893eb948e50SMasami Hiramatsu 		goto error;
1894da15bd9dSWang Nan 	/*
1895da15bd9dSWang Nan 	 * If tp->address == 0, then this point must be a
1896da15bd9dSWang Nan 	 * absolute address uprobe.
1897da15bd9dSWang Nan 	 * try_to_find_absolute_address() should have made
1898da15bd9dSWang Nan 	 * tp->symbol to "0x0".
1899da15bd9dSWang Nan 	 */
1900da15bd9dSWang Nan 	if (tev->uprobes && !tp->address) {
1901da15bd9dSWang Nan 		if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1902da15bd9dSWang Nan 			goto error;
1903da15bd9dSWang Nan 	}
1904eb948e50SMasami Hiramatsu 
1905eb948e50SMasami Hiramatsu 	/* Use the tp->address for uprobes */
1906225466f1SSrikar Dronamraju 	if (tev->uprobes)
1907eb948e50SMasami Hiramatsu 		ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1908eb948e50SMasami Hiramatsu 				 tp->module, tp->address);
1909da15bd9dSWang Nan 	else if (!strncmp(tp->symbol, "0x", 2))
1910da15bd9dSWang Nan 		/* Absolute address. See try_to_find_absolute_address() */
1911da15bd9dSWang Nan 		ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s0x%lx",
1912da15bd9dSWang Nan 				 tp->module ?: "", tp->module ? ":" : "",
1913da15bd9dSWang Nan 				 tp->address);
1914225466f1SSrikar Dronamraju 	else
1915eb948e50SMasami Hiramatsu 		ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1916190b57fcSMasami Hiramatsu 				 tp->module ?: "", tp->module ? ":" : "",
19174235b045SMasami Hiramatsu 				 tp->symbol, tp->offset);
1918225466f1SSrikar Dronamraju 
1919eb948e50SMasami Hiramatsu 	if (ret <= 0)
19204235b045SMasami Hiramatsu 		goto error;
1921eb948e50SMasami Hiramatsu 	len += ret;
19227ef17aafSMasami Hiramatsu 
19234235b045SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++) {
19240e60836bSSrikar Dronamraju 		ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
19254235b045SMasami Hiramatsu 						  MAX_CMDLEN - len);
19264de189feSMasami Hiramatsu 		if (ret <= 0)
192750656eecSMasami Hiramatsu 			goto error;
192850656eecSMasami Hiramatsu 		len += ret;
192950656eecSMasami Hiramatsu 	}
193050656eecSMasami Hiramatsu 
19314235b045SMasami Hiramatsu 	return buf;
193250656eecSMasami Hiramatsu error:
19334235b045SMasami Hiramatsu 	free(buf);
19344235b045SMasami Hiramatsu 	return NULL;
193550656eecSMasami Hiramatsu }
193650656eecSMasami Hiramatsu 
19375a6f6314SMasami Hiramatsu static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
19385a6f6314SMasami Hiramatsu 					  struct perf_probe_point *pp,
19395a6f6314SMasami Hiramatsu 					  bool is_kprobe)
19405a6f6314SMasami Hiramatsu {
19415a6f6314SMasami Hiramatsu 	struct symbol *sym = NULL;
19425a6f6314SMasami Hiramatsu 	struct map *map;
1943e486367fSWang Nan 	u64 addr = tp->address;
19445a6f6314SMasami Hiramatsu 	int ret = -ENOENT;
19455a6f6314SMasami Hiramatsu 
19465a6f6314SMasami Hiramatsu 	if (!is_kprobe) {
19475a6f6314SMasami Hiramatsu 		map = dso__new_map(tp->module);
19485a6f6314SMasami Hiramatsu 		if (!map)
19495a6f6314SMasami Hiramatsu 			goto out;
19505a6f6314SMasami Hiramatsu 		sym = map__find_symbol(map, addr, NULL);
19515a6f6314SMasami Hiramatsu 	} else {
19529b239a12SMasami Hiramatsu 		if (tp->symbol && !addr) {
19530a62f686SMasami Hiramatsu 			if (kernel_get_symbol_address_by_name(tp->symbol,
19540a62f686SMasami Hiramatsu 						&addr, true, false) < 0)
19559b239a12SMasami Hiramatsu 				goto out;
19569b239a12SMasami Hiramatsu 		}
19575a6f6314SMasami Hiramatsu 		if (addr) {
19585a6f6314SMasami Hiramatsu 			addr += tp->offset;
19595a6f6314SMasami Hiramatsu 			sym = __find_kernel_function(addr, &map);
19605a6f6314SMasami Hiramatsu 		}
19615a6f6314SMasami Hiramatsu 	}
196298d3b258SWang Nan 
19635a6f6314SMasami Hiramatsu 	if (!sym)
19645a6f6314SMasami Hiramatsu 		goto out;
19655a6f6314SMasami Hiramatsu 
19665a6f6314SMasami Hiramatsu 	pp->retprobe = tp->retprobe;
19675a6f6314SMasami Hiramatsu 	pp->offset = addr - map->unmap_ip(map, sym->start);
19685a6f6314SMasami Hiramatsu 	pp->function = strdup(sym->name);
19695a6f6314SMasami Hiramatsu 	ret = pp->function ? 0 : -ENOMEM;
19705a6f6314SMasami Hiramatsu 
19715a6f6314SMasami Hiramatsu out:
19725a6f6314SMasami Hiramatsu 	if (map && !is_kprobe) {
197384c2cafaSArnaldo Carvalho de Melo 		map__put(map);
19745a6f6314SMasami Hiramatsu 	}
19755a6f6314SMasami Hiramatsu 
19765a6f6314SMasami Hiramatsu 	return ret;
19775a6f6314SMasami Hiramatsu }
19785a6f6314SMasami Hiramatsu 
19795a6f6314SMasami Hiramatsu static int convert_to_perf_probe_point(struct probe_trace_point *tp,
19805a6f6314SMasami Hiramatsu 				       struct perf_probe_point *pp,
19815a6f6314SMasami Hiramatsu 				       bool is_kprobe)
19825a6f6314SMasami Hiramatsu {
19835a6f6314SMasami Hiramatsu 	char buf[128];
19845a6f6314SMasami Hiramatsu 	int ret;
19855a6f6314SMasami Hiramatsu 
19865a6f6314SMasami Hiramatsu 	ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
19875a6f6314SMasami Hiramatsu 	if (!ret)
19885a6f6314SMasami Hiramatsu 		return 0;
19895a6f6314SMasami Hiramatsu 	ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
19905a6f6314SMasami Hiramatsu 	if (!ret)
19915a6f6314SMasami Hiramatsu 		return 0;
19925a6f6314SMasami Hiramatsu 
19935a6f6314SMasami Hiramatsu 	pr_debug("Failed to find probe point from both of dwarf and map.\n");
19945a6f6314SMasami Hiramatsu 
19955a6f6314SMasami Hiramatsu 	if (tp->symbol) {
19965a6f6314SMasami Hiramatsu 		pp->function = strdup(tp->symbol);
19975a6f6314SMasami Hiramatsu 		pp->offset = tp->offset;
1998614e2fdbSWang Nan 	} else {
19995a6f6314SMasami Hiramatsu 		ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
20005a6f6314SMasami Hiramatsu 		if (ret < 0)
20015a6f6314SMasami Hiramatsu 			return ret;
20025a6f6314SMasami Hiramatsu 		pp->function = strdup(buf);
20035a6f6314SMasami Hiramatsu 		pp->offset = 0;
20045a6f6314SMasami Hiramatsu 	}
20055a6f6314SMasami Hiramatsu 	if (pp->function == NULL)
20065a6f6314SMasami Hiramatsu 		return -ENOMEM;
20075a6f6314SMasami Hiramatsu 
20085a6f6314SMasami Hiramatsu 	pp->retprobe = tp->retprobe;
20095a6f6314SMasami Hiramatsu 
20105a6f6314SMasami Hiramatsu 	return 0;
20115a6f6314SMasami Hiramatsu }
20125a6f6314SMasami Hiramatsu 
20130e60836bSSrikar Dronamraju static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2014225466f1SSrikar Dronamraju 			       struct perf_probe_event *pev, bool is_kprobe)
20154de189feSMasami Hiramatsu {
201602b95dadSMasami Hiramatsu 	char buf[64] = "";
2017146a1439SMasami Hiramatsu 	int i, ret;
20184de189feSMasami Hiramatsu 
20194b4da7f7SMasami Hiramatsu 	/* Convert event/group name */
202002b95dadSMasami Hiramatsu 	pev->event = strdup(tev->event);
202102b95dadSMasami Hiramatsu 	pev->group = strdup(tev->group);
202202b95dadSMasami Hiramatsu 	if (pev->event == NULL || pev->group == NULL)
202302b95dadSMasami Hiramatsu 		return -ENOMEM;
2024fb1587d8SMasami Hiramatsu 
20254b4da7f7SMasami Hiramatsu 	/* Convert trace_point to probe_point */
20265a6f6314SMasami Hiramatsu 	ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2027146a1439SMasami Hiramatsu 	if (ret < 0)
2028146a1439SMasami Hiramatsu 		return ret;
20294b4da7f7SMasami Hiramatsu 
20304235b045SMasami Hiramatsu 	/* Convert trace_arg to probe_arg */
20314235b045SMasami Hiramatsu 	pev->nargs = tev->nargs;
2032e334016fSMasami Hiramatsu 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2033e334016fSMasami Hiramatsu 	if (pev->args == NULL)
2034e334016fSMasami Hiramatsu 		return -ENOMEM;
203502b95dadSMasami Hiramatsu 	for (i = 0; i < tev->nargs && ret >= 0; i++) {
20364235b045SMasami Hiramatsu 		if (tev->args[i].name)
203702b95dadSMasami Hiramatsu 			pev->args[i].name = strdup(tev->args[i].name);
20384235b045SMasami Hiramatsu 		else {
20390e60836bSSrikar Dronamraju 			ret = synthesize_probe_trace_arg(&tev->args[i],
2040146a1439SMasami Hiramatsu 							  buf, 64);
204102b95dadSMasami Hiramatsu 			pev->args[i].name = strdup(buf);
204202b95dadSMasami Hiramatsu 		}
204302b95dadSMasami Hiramatsu 		if (pev->args[i].name == NULL && ret >= 0)
204402b95dadSMasami Hiramatsu 			ret = -ENOMEM;
20454de189feSMasami Hiramatsu 	}
2046146a1439SMasami Hiramatsu 
2047146a1439SMasami Hiramatsu 	if (ret < 0)
2048146a1439SMasami Hiramatsu 		clear_perf_probe_event(pev);
2049146a1439SMasami Hiramatsu 
2050146a1439SMasami Hiramatsu 	return ret;
20514235b045SMasami Hiramatsu }
20524de189feSMasami Hiramatsu 
20534235b045SMasami Hiramatsu void clear_perf_probe_event(struct perf_probe_event *pev)
20544235b045SMasami Hiramatsu {
20557df2f329SMasami Hiramatsu 	struct perf_probe_arg_field *field, *next;
20564235b045SMasami Hiramatsu 	int i;
20574de189feSMasami Hiramatsu 
20584235b045SMasami Hiramatsu 	free(pev->event);
20594235b045SMasami Hiramatsu 	free(pev->group);
20607afb3fabSMasami Hiramatsu 	free(pev->target);
20619b118acaSMasami Hiramatsu 	clear_perf_probe_point(&pev->point);
2062f5385650SArnaldo Carvalho de Melo 
20637df2f329SMasami Hiramatsu 	for (i = 0; i < pev->nargs; i++) {
20644235b045SMasami Hiramatsu 		free(pev->args[i].name);
206548481938SMasami Hiramatsu 		free(pev->args[i].var);
206611a1ca35SMasami Hiramatsu 		free(pev->args[i].type);
20677df2f329SMasami Hiramatsu 		field = pev->args[i].field;
20687df2f329SMasami Hiramatsu 		while (field) {
20697df2f329SMasami Hiramatsu 			next = field->next;
207074cf249dSArnaldo Carvalho de Melo 			zfree(&field->name);
20717df2f329SMasami Hiramatsu 			free(field);
20727df2f329SMasami Hiramatsu 			field = next;
20737df2f329SMasami Hiramatsu 		}
20747df2f329SMasami Hiramatsu 	}
20754235b045SMasami Hiramatsu 	free(pev->args);
20764235b045SMasami Hiramatsu 	memset(pev, 0, sizeof(*pev));
20774235b045SMasami Hiramatsu }
20784235b045SMasami Hiramatsu 
207992f6c72eSMasami Hiramatsu void clear_probe_trace_event(struct probe_trace_event *tev)
20804235b045SMasami Hiramatsu {
20810e60836bSSrikar Dronamraju 	struct probe_trace_arg_ref *ref, *next;
20824235b045SMasami Hiramatsu 	int i;
20834235b045SMasami Hiramatsu 
20844235b045SMasami Hiramatsu 	free(tev->event);
20854235b045SMasami Hiramatsu 	free(tev->group);
20864235b045SMasami Hiramatsu 	free(tev->point.symbol);
20874c859351SMasami Hiramatsu 	free(tev->point.realname);
2088190b57fcSMasami Hiramatsu 	free(tev->point.module);
20894235b045SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++) {
20904235b045SMasami Hiramatsu 		free(tev->args[i].name);
20914235b045SMasami Hiramatsu 		free(tev->args[i].value);
20924984912eSMasami Hiramatsu 		free(tev->args[i].type);
20934235b045SMasami Hiramatsu 		ref = tev->args[i].ref;
20944235b045SMasami Hiramatsu 		while (ref) {
20954235b045SMasami Hiramatsu 			next = ref->next;
20964235b045SMasami Hiramatsu 			free(ref);
20974235b045SMasami Hiramatsu 			ref = next;
20984235b045SMasami Hiramatsu 		}
20994235b045SMasami Hiramatsu 	}
21004235b045SMasami Hiramatsu 	free(tev->args);
21014235b045SMasami Hiramatsu 	memset(tev, 0, sizeof(*tev));
21024de189feSMasami Hiramatsu }
21034de189feSMasami Hiramatsu 
21049aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node {
21059aaf5a5fSMasami Hiramatsu 	struct list_head list;
21069aaf5a5fSMasami Hiramatsu 	unsigned long start;
21079aaf5a5fSMasami Hiramatsu 	unsigned long end;
21089aaf5a5fSMasami Hiramatsu 	char *symbol;
21099aaf5a5fSMasami Hiramatsu };
21109aaf5a5fSMasami Hiramatsu 
21119aaf5a5fSMasami Hiramatsu static void kprobe_blacklist__delete(struct list_head *blacklist)
21129aaf5a5fSMasami Hiramatsu {
21139aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
21149aaf5a5fSMasami Hiramatsu 
21159aaf5a5fSMasami Hiramatsu 	while (!list_empty(blacklist)) {
21169aaf5a5fSMasami Hiramatsu 		node = list_first_entry(blacklist,
21179aaf5a5fSMasami Hiramatsu 					struct kprobe_blacklist_node, list);
21189aaf5a5fSMasami Hiramatsu 		list_del(&node->list);
21199aaf5a5fSMasami Hiramatsu 		free(node->symbol);
21209aaf5a5fSMasami Hiramatsu 		free(node);
21219aaf5a5fSMasami Hiramatsu 	}
21229aaf5a5fSMasami Hiramatsu }
21239aaf5a5fSMasami Hiramatsu 
21249aaf5a5fSMasami Hiramatsu static int kprobe_blacklist__load(struct list_head *blacklist)
21259aaf5a5fSMasami Hiramatsu {
21269aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
21274605eab3SJiri Olsa 	const char *__debugfs = debugfs__mountpoint();
21289aaf5a5fSMasami Hiramatsu 	char buf[PATH_MAX], *p;
21299aaf5a5fSMasami Hiramatsu 	FILE *fp;
21309aaf5a5fSMasami Hiramatsu 	int ret;
21319aaf5a5fSMasami Hiramatsu 
21329aaf5a5fSMasami Hiramatsu 	if (__debugfs == NULL)
21339aaf5a5fSMasami Hiramatsu 		return -ENOTSUP;
21349aaf5a5fSMasami Hiramatsu 
21359aaf5a5fSMasami Hiramatsu 	ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
21369aaf5a5fSMasami Hiramatsu 	if (ret < 0)
21379aaf5a5fSMasami Hiramatsu 		return ret;
21389aaf5a5fSMasami Hiramatsu 
21399aaf5a5fSMasami Hiramatsu 	fp = fopen(buf, "r");
21409aaf5a5fSMasami Hiramatsu 	if (!fp)
21419aaf5a5fSMasami Hiramatsu 		return -errno;
21429aaf5a5fSMasami Hiramatsu 
21439aaf5a5fSMasami Hiramatsu 	ret = 0;
21449aaf5a5fSMasami Hiramatsu 	while (fgets(buf, PATH_MAX, fp)) {
21459aaf5a5fSMasami Hiramatsu 		node = zalloc(sizeof(*node));
21469aaf5a5fSMasami Hiramatsu 		if (!node) {
21479aaf5a5fSMasami Hiramatsu 			ret = -ENOMEM;
21489aaf5a5fSMasami Hiramatsu 			break;
21499aaf5a5fSMasami Hiramatsu 		}
21509aaf5a5fSMasami Hiramatsu 		INIT_LIST_HEAD(&node->list);
21519aaf5a5fSMasami Hiramatsu 		list_add_tail(&node->list, blacklist);
21529aaf5a5fSMasami Hiramatsu 		if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
21539aaf5a5fSMasami Hiramatsu 			ret = -EINVAL;
21549aaf5a5fSMasami Hiramatsu 			break;
21559aaf5a5fSMasami Hiramatsu 		}
21569aaf5a5fSMasami Hiramatsu 		p = strchr(buf, '\t');
21579aaf5a5fSMasami Hiramatsu 		if (p) {
21589aaf5a5fSMasami Hiramatsu 			p++;
21599aaf5a5fSMasami Hiramatsu 			if (p[strlen(p) - 1] == '\n')
21609aaf5a5fSMasami Hiramatsu 				p[strlen(p) - 1] = '\0';
21619aaf5a5fSMasami Hiramatsu 		} else
21629aaf5a5fSMasami Hiramatsu 			p = (char *)"unknown";
21639aaf5a5fSMasami Hiramatsu 		node->symbol = strdup(p);
21649aaf5a5fSMasami Hiramatsu 		if (!node->symbol) {
21659aaf5a5fSMasami Hiramatsu 			ret = -ENOMEM;
21669aaf5a5fSMasami Hiramatsu 			break;
21679aaf5a5fSMasami Hiramatsu 		}
21689aaf5a5fSMasami Hiramatsu 		pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
21699aaf5a5fSMasami Hiramatsu 			  node->start, node->end, node->symbol);
21709aaf5a5fSMasami Hiramatsu 		ret++;
21719aaf5a5fSMasami Hiramatsu 	}
21729aaf5a5fSMasami Hiramatsu 	if (ret < 0)
21739aaf5a5fSMasami Hiramatsu 		kprobe_blacklist__delete(blacklist);
21749aaf5a5fSMasami Hiramatsu 	fclose(fp);
21759aaf5a5fSMasami Hiramatsu 
21769aaf5a5fSMasami Hiramatsu 	return ret;
21779aaf5a5fSMasami Hiramatsu }
21789aaf5a5fSMasami Hiramatsu 
21799aaf5a5fSMasami Hiramatsu static struct kprobe_blacklist_node *
21809aaf5a5fSMasami Hiramatsu kprobe_blacklist__find_by_address(struct list_head *blacklist,
21819aaf5a5fSMasami Hiramatsu 				  unsigned long address)
21829aaf5a5fSMasami Hiramatsu {
21839aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
21849aaf5a5fSMasami Hiramatsu 
21859aaf5a5fSMasami Hiramatsu 	list_for_each_entry(node, blacklist, list) {
21869aaf5a5fSMasami Hiramatsu 		if (node->start <= address && address <= node->end)
21879aaf5a5fSMasami Hiramatsu 			return node;
21889aaf5a5fSMasami Hiramatsu 	}
21899aaf5a5fSMasami Hiramatsu 
21909aaf5a5fSMasami Hiramatsu 	return NULL;
21919aaf5a5fSMasami Hiramatsu }
21929aaf5a5fSMasami Hiramatsu 
2193b031220dSMasami Hiramatsu static LIST_HEAD(kprobe_blacklist);
2194b031220dSMasami Hiramatsu 
2195b031220dSMasami Hiramatsu static void kprobe_blacklist__init(void)
2196b031220dSMasami Hiramatsu {
2197b031220dSMasami Hiramatsu 	if (!list_empty(&kprobe_blacklist))
2198b031220dSMasami Hiramatsu 		return;
2199b031220dSMasami Hiramatsu 
2200b031220dSMasami Hiramatsu 	if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2201b031220dSMasami Hiramatsu 		pr_debug("No kprobe blacklist support, ignored\n");
2202b031220dSMasami Hiramatsu }
2203b031220dSMasami Hiramatsu 
2204b031220dSMasami Hiramatsu static void kprobe_blacklist__release(void)
2205b031220dSMasami Hiramatsu {
2206b031220dSMasami Hiramatsu 	kprobe_blacklist__delete(&kprobe_blacklist);
2207b031220dSMasami Hiramatsu }
2208b031220dSMasami Hiramatsu 
2209b031220dSMasami Hiramatsu static bool kprobe_blacklist__listed(unsigned long address)
2210b031220dSMasami Hiramatsu {
2211b031220dSMasami Hiramatsu 	return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2212b031220dSMasami Hiramatsu }
2213b031220dSMasami Hiramatsu 
2214d350bd57SMasami Hiramatsu static int perf_probe_event__sprintf(const char *group, const char *event,
2215d350bd57SMasami Hiramatsu 				     struct perf_probe_event *pev,
2216ba7ecb02SMasami Hiramatsu 				     const char *module,
2217ba7ecb02SMasami Hiramatsu 				     struct strbuf *result)
2218278498d4SMasami Hiramatsu {
22197e990a51SMasami Hiramatsu 	int i, ret;
2220278498d4SMasami Hiramatsu 	char buf[128];
22214235b045SMasami Hiramatsu 	char *place;
2222278498d4SMasami Hiramatsu 
22234235b045SMasami Hiramatsu 	/* Synthesize only event probe point */
22244235b045SMasami Hiramatsu 	place = synthesize_perf_probe_point(&pev->point);
2225146a1439SMasami Hiramatsu 	if (!place)
2226146a1439SMasami Hiramatsu 		return -EINVAL;
22274235b045SMasami Hiramatsu 
2228d350bd57SMasami Hiramatsu 	ret = e_snprintf(buf, 128, "%s:%s", group, event);
22297e990a51SMasami Hiramatsu 	if (ret < 0)
2230ba7ecb02SMasami Hiramatsu 		goto out;
2231146a1439SMasami Hiramatsu 
2232ba7ecb02SMasami Hiramatsu 	strbuf_addf(result, "  %-20s (on %s", buf, place);
2233fb226ccdSMasami Hiramatsu 	if (module)
2234ba7ecb02SMasami Hiramatsu 		strbuf_addf(result, " in %s", module);
2235278498d4SMasami Hiramatsu 
22364235b045SMasami Hiramatsu 	if (pev->nargs > 0) {
223788fd633cSArnaldo Carvalho de Melo 		strbuf_add(result, " with", 5);
22387df2f329SMasami Hiramatsu 		for (i = 0; i < pev->nargs; i++) {
2239146a1439SMasami Hiramatsu 			ret = synthesize_perf_probe_arg(&pev->args[i],
2240146a1439SMasami Hiramatsu 							buf, 128);
2241146a1439SMasami Hiramatsu 			if (ret < 0)
2242ba7ecb02SMasami Hiramatsu 				goto out;
2243ba7ecb02SMasami Hiramatsu 			strbuf_addf(result, " %s", buf);
22447df2f329SMasami Hiramatsu 		}
2245278498d4SMasami Hiramatsu 	}
2246ba7ecb02SMasami Hiramatsu 	strbuf_addch(result, ')');
2247ba7ecb02SMasami Hiramatsu out:
22484235b045SMasami Hiramatsu 	free(place);
2249146a1439SMasami Hiramatsu 	return ret;
2250278498d4SMasami Hiramatsu }
2251278498d4SMasami Hiramatsu 
2252ba7ecb02SMasami Hiramatsu /* Show an event */
2253b02137ccSNamhyung Kim int show_perf_probe_event(const char *group, const char *event,
2254d350bd57SMasami Hiramatsu 			  struct perf_probe_event *pev,
2255ba7ecb02SMasami Hiramatsu 			  const char *module, bool use_stdout)
2256ba7ecb02SMasami Hiramatsu {
2257ba7ecb02SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
2258ba7ecb02SMasami Hiramatsu 	int ret;
2259ba7ecb02SMasami Hiramatsu 
2260d350bd57SMasami Hiramatsu 	ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2261ba7ecb02SMasami Hiramatsu 	if (ret >= 0) {
2262ba7ecb02SMasami Hiramatsu 		if (use_stdout)
2263ba7ecb02SMasami Hiramatsu 			printf("%s\n", buf.buf);
2264ba7ecb02SMasami Hiramatsu 		else
2265ba7ecb02SMasami Hiramatsu 			pr_info("%s\n", buf.buf);
2266ba7ecb02SMasami Hiramatsu 	}
2267ba7ecb02SMasami Hiramatsu 	strbuf_release(&buf);
2268ba7ecb02SMasami Hiramatsu 
2269ba7ecb02SMasami Hiramatsu 	return ret;
2270ba7ecb02SMasami Hiramatsu }
2271ba7ecb02SMasami Hiramatsu 
2272b6a89643SMasami Hiramatsu static bool filter_probe_trace_event(struct probe_trace_event *tev,
2273b6a89643SMasami Hiramatsu 				     struct strfilter *filter)
2274b6a89643SMasami Hiramatsu {
2275b6a89643SMasami Hiramatsu 	char tmp[128];
2276b6a89643SMasami Hiramatsu 
2277b6a89643SMasami Hiramatsu 	/* At first, check the event name itself */
2278b6a89643SMasami Hiramatsu 	if (strfilter__compare(filter, tev->event))
2279b6a89643SMasami Hiramatsu 		return true;
2280b6a89643SMasami Hiramatsu 
2281b6a89643SMasami Hiramatsu 	/* Next, check the combination of name and group */
2282b6a89643SMasami Hiramatsu 	if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2283b6a89643SMasami Hiramatsu 		return false;
2284b6a89643SMasami Hiramatsu 	return strfilter__compare(filter, tmp);
2285b6a89643SMasami Hiramatsu }
2286b6a89643SMasami Hiramatsu 
2287b6a89643SMasami Hiramatsu static int __show_perf_probe_events(int fd, bool is_kprobe,
2288b6a89643SMasami Hiramatsu 				    struct strfilter *filter)
22894de189feSMasami Hiramatsu {
2290225466f1SSrikar Dronamraju 	int ret = 0;
22910e60836bSSrikar Dronamraju 	struct probe_trace_event tev;
22924235b045SMasami Hiramatsu 	struct perf_probe_event pev;
22934de189feSMasami Hiramatsu 	struct strlist *rawlist;
22944de189feSMasami Hiramatsu 	struct str_node *ent;
22954de189feSMasami Hiramatsu 
22964235b045SMasami Hiramatsu 	memset(&tev, 0, sizeof(tev));
22974235b045SMasami Hiramatsu 	memset(&pev, 0, sizeof(pev));
229872041334SMasami Hiramatsu 
229992f6c72eSMasami Hiramatsu 	rawlist = probe_file__get_rawlist(fd);
2300146a1439SMasami Hiramatsu 	if (!rawlist)
23016eb08660SMasami Hiramatsu 		return -ENOMEM;
23024de189feSMasami Hiramatsu 
2303adf365f4SMasami Hiramatsu 	strlist__for_each(ent, rawlist) {
23040e60836bSSrikar Dronamraju 		ret = parse_probe_trace_command(ent->s, &tev);
2305146a1439SMasami Hiramatsu 		if (ret >= 0) {
2306b6a89643SMasami Hiramatsu 			if (!filter_probe_trace_event(&tev, filter))
2307b6a89643SMasami Hiramatsu 				goto next;
2308225466f1SSrikar Dronamraju 			ret = convert_to_perf_probe_event(&tev, &pev,
2309225466f1SSrikar Dronamraju 								is_kprobe);
2310ba7ecb02SMasami Hiramatsu 			if (ret < 0)
2311ba7ecb02SMasami Hiramatsu 				goto next;
2312d350bd57SMasami Hiramatsu 			ret = show_perf_probe_event(pev.group, pev.event,
2313d350bd57SMasami Hiramatsu 						    &pev, tev.point.module,
2314ba7ecb02SMasami Hiramatsu 						    true);
2315146a1439SMasami Hiramatsu 		}
2316b6a89643SMasami Hiramatsu next:
23174235b045SMasami Hiramatsu 		clear_perf_probe_event(&pev);
23180e60836bSSrikar Dronamraju 		clear_probe_trace_event(&tev);
2319146a1439SMasami Hiramatsu 		if (ret < 0)
2320146a1439SMasami Hiramatsu 			break;
23214de189feSMasami Hiramatsu 	}
23224de189feSMasami Hiramatsu 	strlist__delete(rawlist);
23237737af01SMasami Hiramatsu 	/* Cleanup cached debuginfo if needed */
23247737af01SMasami Hiramatsu 	debuginfo_cache__exit();
2325146a1439SMasami Hiramatsu 
2326146a1439SMasami Hiramatsu 	return ret;
23274de189feSMasami Hiramatsu }
23284de189feSMasami Hiramatsu 
2329225466f1SSrikar Dronamraju /* List up current perf-probe events */
2330b6a89643SMasami Hiramatsu int show_perf_probe_events(struct strfilter *filter)
2331225466f1SSrikar Dronamraju {
23325e45187cSMasami Hiramatsu 	int kp_fd, up_fd, ret;
2333225466f1SSrikar Dronamraju 
2334225466f1SSrikar Dronamraju 	setup_pager();
2335225466f1SSrikar Dronamraju 
23369bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(false);
2337225466f1SSrikar Dronamraju 	if (ret < 0)
2338225466f1SSrikar Dronamraju 		return ret;
2339225466f1SSrikar Dronamraju 
234092f6c72eSMasami Hiramatsu 	ret = probe_file__open_both(&kp_fd, &up_fd, 0);
23415e45187cSMasami Hiramatsu 	if (ret < 0)
2342225466f1SSrikar Dronamraju 		return ret;
2343225466f1SSrikar Dronamraju 
234492f6c72eSMasami Hiramatsu 	if (kp_fd >= 0)
234592f6c72eSMasami Hiramatsu 		ret = __show_perf_probe_events(kp_fd, true, filter);
234692f6c72eSMasami Hiramatsu 	if (up_fd >= 0 && ret >= 0)
234792f6c72eSMasami Hiramatsu 		ret = __show_perf_probe_events(up_fd, false, filter);
234892f6c72eSMasami Hiramatsu 	if (kp_fd > 0)
234992f6c72eSMasami Hiramatsu 		close(kp_fd);
235092f6c72eSMasami Hiramatsu 	if (up_fd > 0)
235192f6c72eSMasami Hiramatsu 		close(up_fd);
23529bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
2353b498ce1fSMasami Hiramatsu 
2354146a1439SMasami Hiramatsu 	return ret;
2355f4d7da49SMasami Hiramatsu }
235650656eecSMasami Hiramatsu 
2357146a1439SMasami Hiramatsu static int get_new_event_name(char *buf, size_t len, const char *base,
2358d761b08bSMasami Hiramatsu 			      struct strlist *namelist, bool allow_suffix)
2359b498ce1fSMasami Hiramatsu {
2360b498ce1fSMasami Hiramatsu 	int i, ret;
2361663b1151SMasami Hiramatsu 	char *p, *nbase;
236217f88fcdSMasami Hiramatsu 
23633099c026SNaveen N. Rao 	if (*base == '.')
23643099c026SNaveen N. Rao 		base++;
2365663b1151SMasami Hiramatsu 	nbase = strdup(base);
2366663b1151SMasami Hiramatsu 	if (!nbase)
2367663b1151SMasami Hiramatsu 		return -ENOMEM;
23683099c026SNaveen N. Rao 
2369663b1151SMasami Hiramatsu 	/* Cut off the dot suffixes (e.g. .const, .isra)*/
2370663b1151SMasami Hiramatsu 	p = strchr(nbase, '.');
2371663b1151SMasami Hiramatsu 	if (p && p != nbase)
2372663b1151SMasami Hiramatsu 		*p = '\0';
2373663b1151SMasami Hiramatsu 
2374663b1151SMasami Hiramatsu 	/* Try no suffix number */
2375663b1151SMasami Hiramatsu 	ret = e_snprintf(buf, len, "%s", nbase);
2376146a1439SMasami Hiramatsu 	if (ret < 0) {
23775f03cba4SMasami Hiramatsu 		pr_debug("snprintf() failed: %d\n", ret);
2378663b1151SMasami Hiramatsu 		goto out;
2379146a1439SMasami Hiramatsu 	}
238017f88fcdSMasami Hiramatsu 	if (!strlist__has_entry(namelist, buf))
2381663b1151SMasami Hiramatsu 		goto out;
238217f88fcdSMasami Hiramatsu 
2383d761b08bSMasami Hiramatsu 	if (!allow_suffix) {
238403e01f56SWang Nan 		pr_warning("Error: event \"%s\" already exists.\n"
238503e01f56SWang Nan 			   " Hint: Remove existing event by 'perf probe -d'\n"
238603e01f56SWang Nan 			   "       or force duplicates by 'perf probe -f'\n"
238703e01f56SWang Nan 			   "       or set 'force=yes' in BPF source.\n",
238803e01f56SWang Nan 			   buf);
2389663b1151SMasami Hiramatsu 		ret = -EEXIST;
2390663b1151SMasami Hiramatsu 		goto out;
2391d761b08bSMasami Hiramatsu 	}
2392d761b08bSMasami Hiramatsu 
239317f88fcdSMasami Hiramatsu 	/* Try to add suffix */
239417f88fcdSMasami Hiramatsu 	for (i = 1; i < MAX_EVENT_INDEX; i++) {
2395663b1151SMasami Hiramatsu 		ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2396146a1439SMasami Hiramatsu 		if (ret < 0) {
23975f03cba4SMasami Hiramatsu 			pr_debug("snprintf() failed: %d\n", ret);
2398663b1151SMasami Hiramatsu 			goto out;
2399146a1439SMasami Hiramatsu 		}
2400b498ce1fSMasami Hiramatsu 		if (!strlist__has_entry(namelist, buf))
2401b498ce1fSMasami Hiramatsu 			break;
2402b498ce1fSMasami Hiramatsu 	}
2403146a1439SMasami Hiramatsu 	if (i == MAX_EVENT_INDEX) {
2404146a1439SMasami Hiramatsu 		pr_warning("Too many events are on the same function.\n");
2405146a1439SMasami Hiramatsu 		ret = -ERANGE;
2406b498ce1fSMasami Hiramatsu 	}
2407b498ce1fSMasami Hiramatsu 
2408663b1151SMasami Hiramatsu out:
2409663b1151SMasami Hiramatsu 	free(nbase);
2410146a1439SMasami Hiramatsu 	return ret;
2411146a1439SMasami Hiramatsu }
2412146a1439SMasami Hiramatsu 
241379702f61SMasami Hiramatsu /* Warn if the current kernel's uprobe implementation is old */
241479702f61SMasami Hiramatsu static void warn_uprobe_event_compat(struct probe_trace_event *tev)
241579702f61SMasami Hiramatsu {
241679702f61SMasami Hiramatsu 	int i;
241779702f61SMasami Hiramatsu 	char *buf = synthesize_probe_trace_command(tev);
241879702f61SMasami Hiramatsu 
241979702f61SMasami Hiramatsu 	/* Old uprobe event doesn't support memory dereference */
242079702f61SMasami Hiramatsu 	if (!tev->uprobes || tev->nargs == 0 || !buf)
242179702f61SMasami Hiramatsu 		goto out;
242279702f61SMasami Hiramatsu 
242379702f61SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++)
242479702f61SMasami Hiramatsu 		if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
242579702f61SMasami Hiramatsu 			pr_warning("Please upgrade your kernel to at least "
242679702f61SMasami Hiramatsu 				   "3.14 to have access to feature %s\n",
242779702f61SMasami Hiramatsu 				   tev->args[i].value);
242879702f61SMasami Hiramatsu 			break;
242979702f61SMasami Hiramatsu 		}
243079702f61SMasami Hiramatsu out:
243179702f61SMasami Hiramatsu 	free(buf);
243279702f61SMasami Hiramatsu }
243379702f61SMasami Hiramatsu 
2434a3c9de62SMasami Hiramatsu /* Set new name from original perf_probe_event and namelist */
2435a3c9de62SMasami Hiramatsu static int probe_trace_event__set_name(struct probe_trace_event *tev,
2436a3c9de62SMasami Hiramatsu 				       struct perf_probe_event *pev,
2437a3c9de62SMasami Hiramatsu 				       struct strlist *namelist,
2438a3c9de62SMasami Hiramatsu 				       bool allow_suffix)
2439a3c9de62SMasami Hiramatsu {
2440a3c9de62SMasami Hiramatsu 	const char *event, *group;
2441a3c9de62SMasami Hiramatsu 	char buf[64];
2442a3c9de62SMasami Hiramatsu 	int ret;
2443a3c9de62SMasami Hiramatsu 
2444a3c9de62SMasami Hiramatsu 	if (pev->event)
2445a3c9de62SMasami Hiramatsu 		event = pev->event;
2446a3c9de62SMasami Hiramatsu 	else
2447da15bd9dSWang Nan 		if (pev->point.function &&
2448da15bd9dSWang Nan 			(strncmp(pev->point.function, "0x", 2) != 0) &&
2449da15bd9dSWang Nan 			!strisglob(pev->point.function))
2450a3c9de62SMasami Hiramatsu 			event = pev->point.function;
2451a3c9de62SMasami Hiramatsu 		else
2452a3c9de62SMasami Hiramatsu 			event = tev->point.realname;
2453a3c9de62SMasami Hiramatsu 	if (pev->group)
2454a3c9de62SMasami Hiramatsu 		group = pev->group;
2455a3c9de62SMasami Hiramatsu 	else
2456a3c9de62SMasami Hiramatsu 		group = PERFPROBE_GROUP;
2457a3c9de62SMasami Hiramatsu 
2458a3c9de62SMasami Hiramatsu 	/* Get an unused new event name */
2459a3c9de62SMasami Hiramatsu 	ret = get_new_event_name(buf, 64, event,
2460a3c9de62SMasami Hiramatsu 				 namelist, allow_suffix);
2461a3c9de62SMasami Hiramatsu 	if (ret < 0)
2462a3c9de62SMasami Hiramatsu 		return ret;
2463a3c9de62SMasami Hiramatsu 
2464a3c9de62SMasami Hiramatsu 	event = buf;
2465a3c9de62SMasami Hiramatsu 
2466a3c9de62SMasami Hiramatsu 	tev->event = strdup(event);
2467a3c9de62SMasami Hiramatsu 	tev->group = strdup(group);
2468a3c9de62SMasami Hiramatsu 	if (tev->event == NULL || tev->group == NULL)
2469a3c9de62SMasami Hiramatsu 		return -ENOMEM;
2470a3c9de62SMasami Hiramatsu 
2471a3c9de62SMasami Hiramatsu 	/* Add added event name to namelist */
2472a3c9de62SMasami Hiramatsu 	strlist__add(namelist, event);
2473a3c9de62SMasami Hiramatsu 	return 0;
2474a3c9de62SMasami Hiramatsu }
2475a3c9de62SMasami Hiramatsu 
24760e60836bSSrikar Dronamraju static int __add_probe_trace_events(struct perf_probe_event *pev,
24770e60836bSSrikar Dronamraju 				     struct probe_trace_event *tevs,
24784235b045SMasami Hiramatsu 				     int ntevs, bool allow_suffix)
247950656eecSMasami Hiramatsu {
2480146a1439SMasami Hiramatsu 	int i, fd, ret;
24810e60836bSSrikar Dronamraju 	struct probe_trace_event *tev = NULL;
2482b498ce1fSMasami Hiramatsu 	struct strlist *namelist;
248350656eecSMasami Hiramatsu 
248492f6c72eSMasami Hiramatsu 	fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
248592f6c72eSMasami Hiramatsu 	if (fd < 0)
2486146a1439SMasami Hiramatsu 		return fd;
24875e45187cSMasami Hiramatsu 
2488b498ce1fSMasami Hiramatsu 	/* Get current event names */
248992f6c72eSMasami Hiramatsu 	namelist = probe_file__get_namelist(fd);
2490146a1439SMasami Hiramatsu 	if (!namelist) {
2491146a1439SMasami Hiramatsu 		pr_debug("Failed to get current event list.\n");
2492ae2cb1acSMasami Hiramatsu 		ret = -ENOMEM;
2493ae2cb1acSMasami Hiramatsu 		goto close_out;
2494146a1439SMasami Hiramatsu 	}
249550656eecSMasami Hiramatsu 
2496146a1439SMasami Hiramatsu 	ret = 0;
249702b95dadSMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
24984235b045SMasami Hiramatsu 		tev = &tevs[i];
2499b031220dSMasami Hiramatsu 		/* Skip if the symbol is out of .text or blacklisted */
25005a51fcd1SMasami Hiramatsu 		if (!tev->point.symbol)
25015a51fcd1SMasami Hiramatsu 			continue;
25029aaf5a5fSMasami Hiramatsu 
2503a3c9de62SMasami Hiramatsu 		/* Set new name for tev (and update namelist) */
2504a3c9de62SMasami Hiramatsu 		ret = probe_trace_event__set_name(tev, pev, namelist,
2505a3c9de62SMasami Hiramatsu 						  allow_suffix);
2506146a1439SMasami Hiramatsu 		if (ret < 0)
2507146a1439SMasami Hiramatsu 			break;
25084235b045SMasami Hiramatsu 
250992f6c72eSMasami Hiramatsu 		ret = probe_file__add_event(fd, tev);
2510146a1439SMasami Hiramatsu 		if (ret < 0)
2511146a1439SMasami Hiramatsu 			break;
25124235b045SMasami Hiramatsu 
2513d761b08bSMasami Hiramatsu 		/*
2514d761b08bSMasami Hiramatsu 		 * Probes after the first probe which comes from same
2515d761b08bSMasami Hiramatsu 		 * user input are always allowed to add suffix, because
2516d761b08bSMasami Hiramatsu 		 * there might be several addresses corresponding to
2517d761b08bSMasami Hiramatsu 		 * one code line.
2518d761b08bSMasami Hiramatsu 		 */
2519d761b08bSMasami Hiramatsu 		allow_suffix = true;
252050656eecSMasami Hiramatsu 	}
252179702f61SMasami Hiramatsu 	if (ret == -EINVAL && pev->uprobes)
252279702f61SMasami Hiramatsu 		warn_uprobe_event_compat(tev);
2523146a1439SMasami Hiramatsu 
2524e1d2017bSMasami Hiramatsu 	strlist__delete(namelist);
2525ae2cb1acSMasami Hiramatsu close_out:
252650656eecSMasami Hiramatsu 	close(fd);
2527146a1439SMasami Hiramatsu 	return ret;
252850656eecSMasami Hiramatsu }
2529fa28244dSMasami Hiramatsu 
25306bb536ccSWang Nan static int find_probe_functions(struct map *map, char *name,
25316bb536ccSWang Nan 				struct symbol **syms)
2532eb948e50SMasami Hiramatsu {
2533564c62a4SNamhyung Kim 	int found = 0;
25340a3873a8SArnaldo Carvalho de Melo 	struct symbol *sym;
25354c859351SMasami Hiramatsu 	struct rb_node *tmp;
2536564c62a4SNamhyung Kim 
253775e4a2a6SWang Nan 	if (map__load(map, NULL) < 0)
253875e4a2a6SWang Nan 		return 0;
253975e4a2a6SWang Nan 
25404c859351SMasami Hiramatsu 	map__for_each_symbol(map, sym, tmp) {
25416bb536ccSWang Nan 		if (strglobmatch(sym->name, name)) {
2542564c62a4SNamhyung Kim 			found++;
25436bb536ccSWang Nan 			if (syms && found < probe_conf.max_probes)
25446bb536ccSWang Nan 				syms[found - 1] = sym;
25456bb536ccSWang Nan 		}
2546eb948e50SMasami Hiramatsu 	}
2547564c62a4SNamhyung Kim 
2548564c62a4SNamhyung Kim 	return found;
2549eb948e50SMasami Hiramatsu }
2550eb948e50SMasami Hiramatsu 
2551eb948e50SMasami Hiramatsu #define strdup_or_goto(str, label)	\
2552eb948e50SMasami Hiramatsu 	({ char *__p = strdup(str); if (!__p) goto label; __p; })
2553eb948e50SMasami Hiramatsu 
25547b6ff0bdSNaveen N. Rao void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
25557b6ff0bdSNaveen N. Rao 				struct probe_trace_event *tev __maybe_unused,
25567b6ff0bdSNaveen N. Rao 				struct map *map __maybe_unused) { }
25577b6ff0bdSNaveen N. Rao 
2558eb948e50SMasami Hiramatsu /*
2559eb948e50SMasami Hiramatsu  * Find probe function addresses from map.
2560eb948e50SMasami Hiramatsu  * Return an error or the number of found probe_trace_event
2561eb948e50SMasami Hiramatsu  */
2562eb948e50SMasami Hiramatsu static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2563ddb2f58fSMasami Hiramatsu 					    struct probe_trace_event **tevs)
2564eb948e50SMasami Hiramatsu {
2565eb948e50SMasami Hiramatsu 	struct map *map = NULL;
2566eb948e50SMasami Hiramatsu 	struct ref_reloc_sym *reloc_sym = NULL;
2567eb948e50SMasami Hiramatsu 	struct symbol *sym;
25686bb536ccSWang Nan 	struct symbol **syms = NULL;
2569eb948e50SMasami Hiramatsu 	struct probe_trace_event *tev;
2570eb948e50SMasami Hiramatsu 	struct perf_probe_point *pp = &pev->point;
2571eb948e50SMasami Hiramatsu 	struct probe_trace_point *tp;
2572564c62a4SNamhyung Kim 	int num_matched_functions;
2573b031220dSMasami Hiramatsu 	int ret, i, j, skipped = 0;
2574*c61fb959SRavi Bangoria 	char *mod_name;
2575eb948e50SMasami Hiramatsu 
257644225521SMasami Hiramatsu 	map = get_target_map(pev->target, pev->uprobes);
2577eb948e50SMasami Hiramatsu 	if (!map) {
2578eb948e50SMasami Hiramatsu 		ret = -EINVAL;
2579eb948e50SMasami Hiramatsu 		goto out;
2580eb948e50SMasami Hiramatsu 	}
2581eb948e50SMasami Hiramatsu 
25826bb536ccSWang Nan 	syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
25836bb536ccSWang Nan 	if (!syms) {
25846bb536ccSWang Nan 		ret = -ENOMEM;
25856bb536ccSWang Nan 		goto out;
25866bb536ccSWang Nan 	}
25876bb536ccSWang Nan 
2588eb948e50SMasami Hiramatsu 	/*
2589eb948e50SMasami Hiramatsu 	 * Load matched symbols: Since the different local symbols may have
2590eb948e50SMasami Hiramatsu 	 * same name but different addresses, this lists all the symbols.
2591eb948e50SMasami Hiramatsu 	 */
25926bb536ccSWang Nan 	num_matched_functions = find_probe_functions(map, pp->function, syms);
2593564c62a4SNamhyung Kim 	if (num_matched_functions == 0) {
2594eb948e50SMasami Hiramatsu 		pr_err("Failed to find symbol %s in %s\n", pp->function,
259544225521SMasami Hiramatsu 			pev->target ? : "kernel");
2596eb948e50SMasami Hiramatsu 		ret = -ENOENT;
2597eb948e50SMasami Hiramatsu 		goto out;
2598ddb2f58fSMasami Hiramatsu 	} else if (num_matched_functions > probe_conf.max_probes) {
2599eb948e50SMasami Hiramatsu 		pr_err("Too many functions matched in %s\n",
260044225521SMasami Hiramatsu 			pev->target ? : "kernel");
2601eb948e50SMasami Hiramatsu 		ret = -E2BIG;
2602eb948e50SMasami Hiramatsu 		goto out;
2603eb948e50SMasami Hiramatsu 	}
2604eb948e50SMasami Hiramatsu 
26051a8ac29cSMasami Hiramatsu 	/* Note that the symbols in the kmodule are not relocated */
26061a8ac29cSMasami Hiramatsu 	if (!pev->uprobes && !pp->retprobe && !pev->target) {
26070560a0c4SHe Kuang 		reloc_sym = kernel_get_ref_reloc_sym();
2608eb948e50SMasami Hiramatsu 		if (!reloc_sym) {
2609eb948e50SMasami Hiramatsu 			pr_warning("Relocated base symbol is not found!\n");
2610eb948e50SMasami Hiramatsu 			ret = -EINVAL;
2611eb948e50SMasami Hiramatsu 			goto out;
2612eb948e50SMasami Hiramatsu 		}
2613eb948e50SMasami Hiramatsu 	}
2614eb948e50SMasami Hiramatsu 
2615eb948e50SMasami Hiramatsu 	/* Setup result trace-probe-events */
2616eb948e50SMasami Hiramatsu 	*tevs = zalloc(sizeof(*tev) * num_matched_functions);
2617eb948e50SMasami Hiramatsu 	if (!*tevs) {
2618eb948e50SMasami Hiramatsu 		ret = -ENOMEM;
2619eb948e50SMasami Hiramatsu 		goto out;
2620eb948e50SMasami Hiramatsu 	}
2621eb948e50SMasami Hiramatsu 
2622eb948e50SMasami Hiramatsu 	ret = 0;
2623564c62a4SNamhyung Kim 
26246bb536ccSWang Nan 	for (j = 0; j < num_matched_functions; j++) {
26256bb536ccSWang Nan 		sym = syms[j];
26266bb536ccSWang Nan 
2627eb948e50SMasami Hiramatsu 		tev = (*tevs) + ret;
2628eb948e50SMasami Hiramatsu 		tp = &tev->point;
2629eb948e50SMasami Hiramatsu 		if (ret == num_matched_functions) {
2630eb948e50SMasami Hiramatsu 			pr_warning("Too many symbols are listed. Skip it.\n");
2631eb948e50SMasami Hiramatsu 			break;
2632eb948e50SMasami Hiramatsu 		}
2633eb948e50SMasami Hiramatsu 		ret++;
2634eb948e50SMasami Hiramatsu 
2635eb948e50SMasami Hiramatsu 		if (pp->offset > sym->end - sym->start) {
2636eb948e50SMasami Hiramatsu 			pr_warning("Offset %ld is bigger than the size of %s\n",
2637eb948e50SMasami Hiramatsu 				   pp->offset, sym->name);
2638eb948e50SMasami Hiramatsu 			ret = -ENOENT;
2639eb948e50SMasami Hiramatsu 			goto err_out;
2640eb948e50SMasami Hiramatsu 		}
2641eb948e50SMasami Hiramatsu 		/* Add one probe point */
2642eb948e50SMasami Hiramatsu 		tp->address = map->unmap_ip(map, sym->start) + pp->offset;
26431a8ac29cSMasami Hiramatsu 
26441a8ac29cSMasami Hiramatsu 		/* Check the kprobe (not in module) is within .text  */
26451a8ac29cSMasami Hiramatsu 		if (!pev->uprobes && !pev->target &&
2646b031220dSMasami Hiramatsu 		    kprobe_warn_out_range(sym->name, tp->address)) {
2647b031220dSMasami Hiramatsu 			tp->symbol = NULL;	/* Skip it */
2648b031220dSMasami Hiramatsu 			skipped++;
2649b031220dSMasami Hiramatsu 		} else if (reloc_sym) {
2650eb948e50SMasami Hiramatsu 			tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2651eb948e50SMasami Hiramatsu 			tp->offset = tp->address - reloc_sym->addr;
2652eb948e50SMasami Hiramatsu 		} else {
2653eb948e50SMasami Hiramatsu 			tp->symbol = strdup_or_goto(sym->name, nomem_out);
2654eb948e50SMasami Hiramatsu 			tp->offset = pp->offset;
2655eb948e50SMasami Hiramatsu 		}
26566bb536ccSWang Nan 		tp->realname = strdup_or_goto(sym->name, nomem_out);
26576bb536ccSWang Nan 
2658eb948e50SMasami Hiramatsu 		tp->retprobe = pp->retprobe;
2659*c61fb959SRavi Bangoria 		if (pev->target) {
2660*c61fb959SRavi Bangoria 			if (pev->uprobes) {
266144225521SMasami Hiramatsu 				tev->point.module = strdup_or_goto(pev->target,
266244225521SMasami Hiramatsu 								   nomem_out);
2663*c61fb959SRavi Bangoria 			} else {
2664*c61fb959SRavi Bangoria 				mod_name = find_module_name(pev->target);
2665*c61fb959SRavi Bangoria 				tev->point.module =
2666*c61fb959SRavi Bangoria 					strdup(mod_name ? mod_name : pev->target);
2667*c61fb959SRavi Bangoria 				free(mod_name);
2668*c61fb959SRavi Bangoria 				if (!tev->point.module)
2669*c61fb959SRavi Bangoria 					goto nomem_out;
2670*c61fb959SRavi Bangoria 			}
2671*c61fb959SRavi Bangoria 		}
2672eb948e50SMasami Hiramatsu 		tev->uprobes = pev->uprobes;
2673eb948e50SMasami Hiramatsu 		tev->nargs = pev->nargs;
2674eb948e50SMasami Hiramatsu 		if (tev->nargs) {
2675eb948e50SMasami Hiramatsu 			tev->args = zalloc(sizeof(struct probe_trace_arg) *
2676eb948e50SMasami Hiramatsu 					   tev->nargs);
2677eb948e50SMasami Hiramatsu 			if (tev->args == NULL)
2678eb948e50SMasami Hiramatsu 				goto nomem_out;
2679eb948e50SMasami Hiramatsu 		}
2680eb948e50SMasami Hiramatsu 		for (i = 0; i < tev->nargs; i++) {
2681eb948e50SMasami Hiramatsu 			if (pev->args[i].name)
2682eb948e50SMasami Hiramatsu 				tev->args[i].name =
2683eb948e50SMasami Hiramatsu 					strdup_or_goto(pev->args[i].name,
2684eb948e50SMasami Hiramatsu 							nomem_out);
2685eb948e50SMasami Hiramatsu 
2686eb948e50SMasami Hiramatsu 			tev->args[i].value = strdup_or_goto(pev->args[i].var,
2687eb948e50SMasami Hiramatsu 							    nomem_out);
2688eb948e50SMasami Hiramatsu 			if (pev->args[i].type)
2689eb948e50SMasami Hiramatsu 				tev->args[i].type =
2690eb948e50SMasami Hiramatsu 					strdup_or_goto(pev->args[i].type,
2691eb948e50SMasami Hiramatsu 							nomem_out);
2692eb948e50SMasami Hiramatsu 		}
26937b6ff0bdSNaveen N. Rao 		arch__fix_tev_from_maps(pev, tev, map);
2694eb948e50SMasami Hiramatsu 	}
2695b031220dSMasami Hiramatsu 	if (ret == skipped) {
2696b031220dSMasami Hiramatsu 		ret = -ENOENT;
2697b031220dSMasami Hiramatsu 		goto err_out;
2698b031220dSMasami Hiramatsu 	}
2699eb948e50SMasami Hiramatsu 
2700eb948e50SMasami Hiramatsu out:
27019b118acaSMasami Hiramatsu 	put_target_map(map, pev->uprobes);
27026bb536ccSWang Nan 	free(syms);
2703eb948e50SMasami Hiramatsu 	return ret;
2704eb948e50SMasami Hiramatsu 
2705eb948e50SMasami Hiramatsu nomem_out:
2706eb948e50SMasami Hiramatsu 	ret = -ENOMEM;
2707eb948e50SMasami Hiramatsu err_out:
2708eb948e50SMasami Hiramatsu 	clear_probe_trace_events(*tevs, num_matched_functions);
2709eb948e50SMasami Hiramatsu 	zfree(tevs);
2710eb948e50SMasami Hiramatsu 	goto out;
2711eb948e50SMasami Hiramatsu }
2712eb948e50SMasami Hiramatsu 
2713da15bd9dSWang Nan static int try_to_find_absolute_address(struct perf_probe_event *pev,
2714da15bd9dSWang Nan 					struct probe_trace_event **tevs)
2715da15bd9dSWang Nan {
2716da15bd9dSWang Nan 	struct perf_probe_point *pp = &pev->point;
2717da15bd9dSWang Nan 	struct probe_trace_event *tev;
2718da15bd9dSWang Nan 	struct probe_trace_point *tp;
2719da15bd9dSWang Nan 	int i, err;
2720da15bd9dSWang Nan 
2721da15bd9dSWang Nan 	if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2722da15bd9dSWang Nan 		return -EINVAL;
2723da15bd9dSWang Nan 	if (perf_probe_event_need_dwarf(pev))
2724da15bd9dSWang Nan 		return -EINVAL;
2725da15bd9dSWang Nan 
2726da15bd9dSWang Nan 	/*
2727da15bd9dSWang Nan 	 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2728da15bd9dSWang Nan 	 * absolute address.
2729da15bd9dSWang Nan 	 *
2730da15bd9dSWang Nan 	 * Only one tev can be generated by this.
2731da15bd9dSWang Nan 	 */
2732da15bd9dSWang Nan 	*tevs = zalloc(sizeof(*tev));
2733da15bd9dSWang Nan 	if (!*tevs)
2734da15bd9dSWang Nan 		return -ENOMEM;
2735da15bd9dSWang Nan 
2736da15bd9dSWang Nan 	tev = *tevs;
2737da15bd9dSWang Nan 	tp = &tev->point;
2738da15bd9dSWang Nan 
2739da15bd9dSWang Nan 	/*
2740da15bd9dSWang Nan 	 * Don't use tp->offset, use address directly, because
2741da15bd9dSWang Nan 	 * in synthesize_probe_trace_command() address cannot be
2742da15bd9dSWang Nan 	 * zero.
2743da15bd9dSWang Nan 	 */
2744da15bd9dSWang Nan 	tp->address = pev->point.abs_address;
2745da15bd9dSWang Nan 	tp->retprobe = pp->retprobe;
2746da15bd9dSWang Nan 	tev->uprobes = pev->uprobes;
2747da15bd9dSWang Nan 
2748da15bd9dSWang Nan 	err = -ENOMEM;
2749da15bd9dSWang Nan 	/*
2750da15bd9dSWang Nan 	 * Give it a '0x' leading symbol name.
2751da15bd9dSWang Nan 	 * In __add_probe_trace_events, a NULL symbol is interpreted as
2752da15bd9dSWang Nan 	 * invalud.
2753da15bd9dSWang Nan 	 */
2754da15bd9dSWang Nan 	if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2755da15bd9dSWang Nan 		goto errout;
2756da15bd9dSWang Nan 
2757da15bd9dSWang Nan 	/* For kprobe, check range */
2758da15bd9dSWang Nan 	if ((!tev->uprobes) &&
2759da15bd9dSWang Nan 	    (kprobe_warn_out_range(tev->point.symbol,
2760da15bd9dSWang Nan 				   tev->point.address))) {
2761da15bd9dSWang Nan 		err = -EACCES;
2762da15bd9dSWang Nan 		goto errout;
2763da15bd9dSWang Nan 	}
2764da15bd9dSWang Nan 
2765da15bd9dSWang Nan 	if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2766da15bd9dSWang Nan 		goto errout;
2767da15bd9dSWang Nan 
2768da15bd9dSWang Nan 	if (pev->target) {
2769da15bd9dSWang Nan 		tp->module = strdup(pev->target);
2770da15bd9dSWang Nan 		if (!tp->module)
2771da15bd9dSWang Nan 			goto errout;
2772da15bd9dSWang Nan 	}
2773da15bd9dSWang Nan 
2774da15bd9dSWang Nan 	if (tev->group) {
2775da15bd9dSWang Nan 		tev->group = strdup(pev->group);
2776da15bd9dSWang Nan 		if (!tev->group)
2777da15bd9dSWang Nan 			goto errout;
2778da15bd9dSWang Nan 	}
2779da15bd9dSWang Nan 
2780da15bd9dSWang Nan 	if (pev->event) {
2781da15bd9dSWang Nan 		tev->event = strdup(pev->event);
2782da15bd9dSWang Nan 		if (!tev->event)
2783da15bd9dSWang Nan 			goto errout;
2784da15bd9dSWang Nan 	}
2785da15bd9dSWang Nan 
2786da15bd9dSWang Nan 	tev->nargs = pev->nargs;
2787da15bd9dSWang Nan 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2788da15bd9dSWang Nan 	if (!tev->args) {
2789da15bd9dSWang Nan 		err = -ENOMEM;
2790da15bd9dSWang Nan 		goto errout;
2791da15bd9dSWang Nan 	}
2792da15bd9dSWang Nan 	for (i = 0; i < tev->nargs; i++)
2793da15bd9dSWang Nan 		copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2794da15bd9dSWang Nan 
2795da15bd9dSWang Nan 	return 1;
2796da15bd9dSWang Nan 
2797da15bd9dSWang Nan errout:
2798da15bd9dSWang Nan 	if (*tevs) {
2799da15bd9dSWang Nan 		clear_probe_trace_events(*tevs, 1);
2800da15bd9dSWang Nan 		*tevs = NULL;
2801da15bd9dSWang Nan 	}
2802da15bd9dSWang Nan 	return err;
2803da15bd9dSWang Nan }
2804da15bd9dSWang Nan 
2805d5c2e2c1SNaveen N. Rao bool __weak arch__prefers_symtab(void) { return false; }
2806d5c2e2c1SNaveen N. Rao 
28070e60836bSSrikar Dronamraju static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2808ddb2f58fSMasami Hiramatsu 					 struct probe_trace_event **tevs)
2809e0faa8d3SMasami Hiramatsu {
2810eb948e50SMasami Hiramatsu 	int ret;
28114235b045SMasami Hiramatsu 
28122a12ec13SMasami Hiramatsu 	if (!pev->group) {
28132a12ec13SMasami Hiramatsu 		/* Set group name if not given */
28142a12ec13SMasami Hiramatsu 		if (!pev->uprobes) {
28152a12ec13SMasami Hiramatsu 			pev->group = strdup(PERFPROBE_GROUP);
28162a12ec13SMasami Hiramatsu 			ret = pev->group ? 0 : -ENOMEM;
28172a12ec13SMasami Hiramatsu 		} else
281844225521SMasami Hiramatsu 			ret = convert_exec_to_group(pev->target, &pev->group);
2819fb7345bbSMasami Hiramatsu 		if (ret != 0) {
2820fb7345bbSMasami Hiramatsu 			pr_warning("Failed to make a group name.\n");
2821fb7345bbSMasami Hiramatsu 			return ret;
2822fb7345bbSMasami Hiramatsu 		}
2823fb7345bbSMasami Hiramatsu 	}
2824fb7345bbSMasami Hiramatsu 
2825da15bd9dSWang Nan 	ret = try_to_find_absolute_address(pev, tevs);
2826da15bd9dSWang Nan 	if (ret > 0)
2827da15bd9dSWang Nan 		return ret;
2828da15bd9dSWang Nan 
2829d5c2e2c1SNaveen N. Rao 	if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2830ddb2f58fSMasami Hiramatsu 		ret = find_probe_trace_events_from_map(pev, tevs);
2831d5c2e2c1SNaveen N. Rao 		if (ret > 0)
2832d5c2e2c1SNaveen N. Rao 			return ret; /* Found in symbol table */
2833d5c2e2c1SNaveen N. Rao 	}
2834d5c2e2c1SNaveen N. Rao 
28354b4da7f7SMasami Hiramatsu 	/* Convert perf_probe_event with debuginfo */
2836ddb2f58fSMasami Hiramatsu 	ret = try_to_find_probe_trace_events(pev, tevs);
2837e334016fSMasami Hiramatsu 	if (ret != 0)
2838190b57fcSMasami Hiramatsu 		return ret;	/* Found in debuginfo or got an error */
2839e0faa8d3SMasami Hiramatsu 
2840ddb2f58fSMasami Hiramatsu 	return find_probe_trace_events_from_map(pev, tevs);
28414235b045SMasami Hiramatsu }
28424235b045SMasami Hiramatsu 
284312fae5efSWang Nan int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
28444235b045SMasami Hiramatsu {
2845844dffa5SNamhyung Kim 	int i, ret;
28464235b045SMasami Hiramatsu 
28474235b045SMasami Hiramatsu 	/* Loop 1: convert all events */
28484235b045SMasami Hiramatsu 	for (i = 0; i < npevs; i++) {
2849b031220dSMasami Hiramatsu 		/* Init kprobe blacklist if needed */
285012fae5efSWang Nan 		if (!pevs[i].uprobes)
2851b031220dSMasami Hiramatsu 			kprobe_blacklist__init();
28524235b045SMasami Hiramatsu 		/* Convert with or without debuginfo */
285312fae5efSWang Nan 		ret  = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
2854146a1439SMasami Hiramatsu 		if (ret < 0)
2855844dffa5SNamhyung Kim 			return ret;
285612fae5efSWang Nan 		pevs[i].ntevs = ret;
28574235b045SMasami Hiramatsu 	}
2858b031220dSMasami Hiramatsu 	/* This just release blacklist only if allocated */
2859b031220dSMasami Hiramatsu 	kprobe_blacklist__release();
28604235b045SMasami Hiramatsu 
2861844dffa5SNamhyung Kim 	return 0;
2862844dffa5SNamhyung Kim }
2863844dffa5SNamhyung Kim 
286412fae5efSWang Nan int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2865844dffa5SNamhyung Kim {
2866844dffa5SNamhyung Kim 	int i, ret = 0;
2867844dffa5SNamhyung Kim 
28684235b045SMasami Hiramatsu 	/* Loop 2: add all events */
28698635bf6eSArnaldo Carvalho de Melo 	for (i = 0; i < npevs; i++) {
287012fae5efSWang Nan 		ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
287112fae5efSWang Nan 					       pevs[i].ntevs,
2872ddb2f58fSMasami Hiramatsu 					       probe_conf.force_add);
2873fbee632dSArnaldo Carvalho de Melo 		if (ret < 0)
2874fbee632dSArnaldo Carvalho de Melo 			break;
2875fbee632dSArnaldo Carvalho de Melo 	}
2876844dffa5SNamhyung Kim 	return ret;
2877844dffa5SNamhyung Kim }
2878844dffa5SNamhyung Kim 
287912fae5efSWang Nan void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2880844dffa5SNamhyung Kim {
2881844dffa5SNamhyung Kim 	int i, j;
2882844dffa5SNamhyung Kim 
2883449e5b24SMasami Hiramatsu 	/* Loop 3: cleanup and free trace events  */
2884449e5b24SMasami Hiramatsu 	for (i = 0; i < npevs; i++) {
288512fae5efSWang Nan 		for (j = 0; j < pevs[i].ntevs; j++)
288612fae5efSWang Nan 			clear_probe_trace_event(&pevs[i].tevs[j]);
288712fae5efSWang Nan 		zfree(&pevs[i].tevs);
288812fae5efSWang Nan 		pevs[i].ntevs = 0;
2889a43aac29SNamhyung Kim 		clear_perf_probe_event(&pevs[i]);
2890449e5b24SMasami Hiramatsu 	}
2891844dffa5SNamhyung Kim }
2892844dffa5SNamhyung Kim 
2893844dffa5SNamhyung Kim int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2894844dffa5SNamhyung Kim {
2895844dffa5SNamhyung Kim 	int ret;
2896844dffa5SNamhyung Kim 
28979bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(pevs->uprobes);
28989bae1e8cSNamhyung Kim 	if (ret < 0)
28999bae1e8cSNamhyung Kim 		return ret;
29009bae1e8cSNamhyung Kim 
290112fae5efSWang Nan 	ret = convert_perf_probe_events(pevs, npevs);
2902844dffa5SNamhyung Kim 	if (ret == 0)
290312fae5efSWang Nan 		ret = apply_perf_probe_events(pevs, npevs);
2904844dffa5SNamhyung Kim 
290512fae5efSWang Nan 	cleanup_perf_probe_events(pevs, npevs);
2906146a1439SMasami Hiramatsu 
29079bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
2908146a1439SMasami Hiramatsu 	return ret;
2909e0faa8d3SMasami Hiramatsu }
2910e0faa8d3SMasami Hiramatsu 
2911307a464bSMasami Hiramatsu int del_perf_probe_events(struct strfilter *filter)
2912fa28244dSMasami Hiramatsu {
2913307a464bSMasami Hiramatsu 	int ret, ret2, ufd = -1, kfd = -1;
2914307a464bSMasami Hiramatsu 	char *str = strfilter__string(filter);
2915307a464bSMasami Hiramatsu 
2916307a464bSMasami Hiramatsu 	if (!str)
2917307a464bSMasami Hiramatsu 		return -EINVAL;
2918307a464bSMasami Hiramatsu 
2919fa28244dSMasami Hiramatsu 	/* Get current event names */
292092f6c72eSMasami Hiramatsu 	ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
292192f6c72eSMasami Hiramatsu 	if (ret < 0)
292292f6c72eSMasami Hiramatsu 		goto out;
2923225466f1SSrikar Dronamraju 
292492f6c72eSMasami Hiramatsu 	ret = probe_file__del_events(kfd, filter);
2925307a464bSMasami Hiramatsu 	if (ret < 0 && ret != -ENOENT)
2926225466f1SSrikar Dronamraju 		goto error;
2927fa28244dSMasami Hiramatsu 
292892f6c72eSMasami Hiramatsu 	ret2 = probe_file__del_events(ufd, filter);
2929dddc7ee3SMasami Hiramatsu 	if (ret2 < 0 && ret2 != -ENOENT) {
29306dbe31f7SMasami Hiramatsu 		ret = ret2;
2931dddc7ee3SMasami Hiramatsu 		goto error;
2932dddc7ee3SMasami Hiramatsu 	}
2933307a464bSMasami Hiramatsu 	ret = 0;
2934225466f1SSrikar Dronamraju 
2935225466f1SSrikar Dronamraju error:
293692f6c72eSMasami Hiramatsu 	if (kfd >= 0)
2937225466f1SSrikar Dronamraju 		close(kfd);
293892f6c72eSMasami Hiramatsu 	if (ufd >= 0)
2939225466f1SSrikar Dronamraju 		close(ufd);
294092f6c72eSMasami Hiramatsu out:
2941307a464bSMasami Hiramatsu 	free(str);
2942146a1439SMasami Hiramatsu 
2943146a1439SMasami Hiramatsu 	return ret;
2944fa28244dSMasami Hiramatsu }
2945225466f1SSrikar Dronamraju 
29463c42258cSMasami Hiramatsu /* TODO: don't use a global variable for filter ... */
29473c42258cSMasami Hiramatsu static struct strfilter *available_func_filter;
2948fa28244dSMasami Hiramatsu 
2949e80711caSMasami Hiramatsu /*
29503c42258cSMasami Hiramatsu  * If a symbol corresponds to a function with global binding and
29513c42258cSMasami Hiramatsu  * matches filter return 0. For all others return 1.
2952e80711caSMasami Hiramatsu  */
29531d037ca1SIrina Tirdea static int filter_available_functions(struct map *map __maybe_unused,
2954e80711caSMasami Hiramatsu 				      struct symbol *sym)
2955e80711caSMasami Hiramatsu {
2956e578da3bSNamhyung Kim 	if (strfilter__compare(available_func_filter, sym->name))
2957e80711caSMasami Hiramatsu 		return 0;
29583c42258cSMasami Hiramatsu 	return 1;
2959e80711caSMasami Hiramatsu }
2960e80711caSMasami Hiramatsu 
29612df58634SMasami Hiramatsu int show_available_funcs(const char *target, struct strfilter *_filter,
29622df58634SMasami Hiramatsu 					bool user)
2963e80711caSMasami Hiramatsu {
29642df58634SMasami Hiramatsu 	struct map *map;
29652df58634SMasami Hiramatsu 	int ret;
29662df58634SMasami Hiramatsu 
29679bae1e8cSNamhyung Kim 	ret = init_probe_symbol_maps(user);
29682df58634SMasami Hiramatsu 	if (ret < 0)
29692df58634SMasami Hiramatsu 		return ret;
29702df58634SMasami Hiramatsu 
29712df58634SMasami Hiramatsu 	/* Get a symbol map */
29722df58634SMasami Hiramatsu 	if (user)
29732df58634SMasami Hiramatsu 		map = dso__new_map(target);
29742df58634SMasami Hiramatsu 	else
29752df58634SMasami Hiramatsu 		map = kernel_get_module_map(target);
29762df58634SMasami Hiramatsu 	if (!map) {
29772df58634SMasami Hiramatsu 		pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2978e80711caSMasami Hiramatsu 		return -EINVAL;
2979e80711caSMasami Hiramatsu 	}
29802df58634SMasami Hiramatsu 
29812df58634SMasami Hiramatsu 	/* Load symbols with given filter */
29822df58634SMasami Hiramatsu 	available_func_filter = _filter;
29832df58634SMasami Hiramatsu 	if (map__load(map, filter_available_functions)) {
29842df58634SMasami Hiramatsu 		pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
29852df58634SMasami Hiramatsu 		goto end;
29862df58634SMasami Hiramatsu 	}
2987e80711caSMasami Hiramatsu 	if (!dso__sorted_by_name(map->dso, map->type))
2988e80711caSMasami Hiramatsu 		dso__sort_by_name(map->dso, map->type);
2989e80711caSMasami Hiramatsu 
29902df58634SMasami Hiramatsu 	/* Show all (filtered) symbols */
29912df58634SMasami Hiramatsu 	setup_pager();
2992e80711caSMasami Hiramatsu 	dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
29932df58634SMasami Hiramatsu end:
29942df58634SMasami Hiramatsu 	if (user) {
299584c2cafaSArnaldo Carvalho de Melo 		map__put(map);
2996225466f1SSrikar Dronamraju 	}
29979bae1e8cSNamhyung Kim 	exit_probe_symbol_maps();
2998225466f1SSrikar Dronamraju 
29992df58634SMasami Hiramatsu 	return ret;
3000225466f1SSrikar Dronamraju }
3001225466f1SSrikar Dronamraju 
3002da15bd9dSWang Nan int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3003da15bd9dSWang Nan 			    struct perf_probe_arg *pvar)
3004da15bd9dSWang Nan {
3005da15bd9dSWang Nan 	tvar->value = strdup(pvar->var);
3006da15bd9dSWang Nan 	if (tvar->value == NULL)
3007da15bd9dSWang Nan 		return -ENOMEM;
3008da15bd9dSWang Nan 	if (pvar->type) {
3009da15bd9dSWang Nan 		tvar->type = strdup(pvar->type);
3010da15bd9dSWang Nan 		if (tvar->type == NULL)
3011da15bd9dSWang Nan 			return -ENOMEM;
3012da15bd9dSWang Nan 	}
3013da15bd9dSWang Nan 	if (pvar->name) {
3014da15bd9dSWang Nan 		tvar->name = strdup(pvar->name);
3015da15bd9dSWang Nan 		if (tvar->name == NULL)
3016da15bd9dSWang Nan 			return -ENOMEM;
3017da15bd9dSWang Nan 	} else
3018da15bd9dSWang Nan 		tvar->name = NULL;
3019da15bd9dSWang Nan 	return 0;
3020da15bd9dSWang Nan }
3021