xref: /linux/tools/perf/util/probe-event.c (revision b8dc3984c1fce87a36d3247c9f722229692bec72)
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"
43553873e1SBorislav Petkov #include <api/fs/debugfs.h>
4423773ca1SSteven Rostedt (Red Hat) #include <api/fs/tracefs.h>
451d037ca1SIrina Tirdea #include "trace-event.h"	/* For __maybe_unused */
4650656eecSMasami Hiramatsu #include "probe-event.h"
474235b045SMasami Hiramatsu #include "probe-finder.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 */
54f4d7da49SMasami Hiramatsu 
55146a1439SMasami Hiramatsu #define semantic_error(msg ...) pr_err("Semantic error :" msg)
5650656eecSMasami Hiramatsu 
574de189feSMasami Hiramatsu /* If there is no space to write, returns -E2BIG. */
584de189feSMasami Hiramatsu static int e_snprintf(char *str, size_t size, const char *format, ...)
5984988450SMasami Hiramatsu 	__attribute__((format(printf, 3, 4)));
6084988450SMasami Hiramatsu 
6184988450SMasami Hiramatsu static int e_snprintf(char *str, size_t size, const char *format, ...)
624de189feSMasami Hiramatsu {
634de189feSMasami Hiramatsu 	int ret;
644de189feSMasami Hiramatsu 	va_list ap;
654de189feSMasami Hiramatsu 	va_start(ap, format);
664de189feSMasami Hiramatsu 	ret = vsnprintf(str, size, format, ap);
674de189feSMasami Hiramatsu 	va_end(ap);
684de189feSMasami Hiramatsu 	if (ret >= (int)size)
694de189feSMasami Hiramatsu 		ret = -E2BIG;
704de189feSMasami Hiramatsu 	return ret;
714de189feSMasami Hiramatsu }
724de189feSMasami Hiramatsu 
734b4da7f7SMasami Hiramatsu static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
74981d05adSMasami Hiramatsu static void clear_probe_trace_event(struct probe_trace_event *tev);
75ee45b6c2SMasami Hiramatsu static struct machine *host_machine;
76e0faa8d3SMasami Hiramatsu 
77469b9b88SMasami Hiramatsu /* Initialize symbol maps and path of vmlinux/modules */
78ee45b6c2SMasami Hiramatsu static int init_symbol_maps(bool user_only)
79e0faa8d3SMasami Hiramatsu {
80146a1439SMasami Hiramatsu 	int ret;
81146a1439SMasami Hiramatsu 
82e0faa8d3SMasami Hiramatsu 	symbol_conf.sort_by_name = true;
83680d926aSNamhyung Kim 	symbol_conf.allow_aliases = true;
840a7e6d1bSNamhyung Kim 	ret = symbol__init(NULL);
85146a1439SMasami Hiramatsu 	if (ret < 0) {
86146a1439SMasami Hiramatsu 		pr_debug("Failed to init symbol map.\n");
87146a1439SMasami Hiramatsu 		goto out;
88146a1439SMasami Hiramatsu 	}
89e0faa8d3SMasami Hiramatsu 
90ee45b6c2SMasami Hiramatsu 	if (host_machine || user_only)	/* already initialized */
91ee45b6c2SMasami Hiramatsu 		return 0;
92d28c6223SArnaldo Carvalho de Melo 
93ee45b6c2SMasami Hiramatsu 	if (symbol_conf.vmlinux_name)
94ee45b6c2SMasami Hiramatsu 		pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
95ee45b6c2SMasami Hiramatsu 
96ee45b6c2SMasami Hiramatsu 	host_machine = machine__new_host();
97ee45b6c2SMasami Hiramatsu 	if (!host_machine) {
98ee45b6c2SMasami Hiramatsu 		pr_debug("machine__new_host() failed.\n");
99ee45b6c2SMasami Hiramatsu 		symbol__exit();
100ee45b6c2SMasami Hiramatsu 		ret = -1;
101469b9b88SMasami Hiramatsu 	}
102146a1439SMasami Hiramatsu out:
103146a1439SMasami Hiramatsu 	if (ret < 0)
104146a1439SMasami Hiramatsu 		pr_warning("Failed to init vmlinux path.\n");
105146a1439SMasami Hiramatsu 	return ret;
106e0faa8d3SMasami Hiramatsu }
107e0faa8d3SMasami Hiramatsu 
108ee45b6c2SMasami Hiramatsu static void exit_symbol_maps(void)
109ee45b6c2SMasami Hiramatsu {
110ee45b6c2SMasami Hiramatsu 	if (host_machine) {
111ee45b6c2SMasami Hiramatsu 		machine__delete(host_machine);
112ee45b6c2SMasami Hiramatsu 		host_machine = NULL;
113ee45b6c2SMasami Hiramatsu 	}
114ee45b6c2SMasami Hiramatsu 	symbol__exit();
115ee45b6c2SMasami Hiramatsu }
116ee45b6c2SMasami Hiramatsu 
117469b9b88SMasami Hiramatsu static struct symbol *__find_kernel_function_by_name(const char *name,
118469b9b88SMasami Hiramatsu 						     struct map **mapp)
119e0faa8d3SMasami Hiramatsu {
120ee45b6c2SMasami Hiramatsu 	return machine__find_kernel_function_by_name(host_machine, name, mapp,
121469b9b88SMasami Hiramatsu 						     NULL);
122e0faa8d3SMasami Hiramatsu }
123469b9b88SMasami Hiramatsu 
1248f33f7deSMasami Hiramatsu static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
1258f33f7deSMasami Hiramatsu {
1268f33f7deSMasami Hiramatsu 	return machine__find_kernel_function(host_machine, addr, mapp, NULL);
1278f33f7deSMasami Hiramatsu }
1288f33f7deSMasami Hiramatsu 
1298f33f7deSMasami Hiramatsu static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
1308f33f7deSMasami Hiramatsu {
1318f33f7deSMasami Hiramatsu 	/* kmap->ref_reloc_sym should be set if host_machine is initialized */
1328f33f7deSMasami Hiramatsu 	struct kmap *kmap;
1338f33f7deSMasami Hiramatsu 
1348f33f7deSMasami Hiramatsu 	if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
1358f33f7deSMasami Hiramatsu 		return NULL;
1368f33f7deSMasami Hiramatsu 
1378f33f7deSMasami Hiramatsu 	kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
138ba92732eSWang Nan 	if (!kmap)
139ba92732eSWang Nan 		return NULL;
1408f33f7deSMasami Hiramatsu 	return kmap->ref_reloc_sym;
1418f33f7deSMasami Hiramatsu }
1428f33f7deSMasami Hiramatsu 
1438f33f7deSMasami Hiramatsu static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
1448f33f7deSMasami Hiramatsu {
1458f33f7deSMasami Hiramatsu 	struct ref_reloc_sym *reloc_sym;
1468f33f7deSMasami Hiramatsu 	struct symbol *sym;
1478f33f7deSMasami Hiramatsu 	struct map *map;
1488f33f7deSMasami Hiramatsu 
1498f33f7deSMasami Hiramatsu 	/* ref_reloc_sym is just a label. Need a special fix*/
1508f33f7deSMasami Hiramatsu 	reloc_sym = kernel_get_ref_reloc_sym();
1518f33f7deSMasami Hiramatsu 	if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
1528f33f7deSMasami Hiramatsu 		return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
1538f33f7deSMasami Hiramatsu 	else {
1548f33f7deSMasami Hiramatsu 		sym = __find_kernel_function_by_name(name, &map);
1558f33f7deSMasami Hiramatsu 		if (sym)
1568f33f7deSMasami Hiramatsu 			return map->unmap_ip(map, sym->start) -
157f56847c2SHe Kuang 				((reloc) ? 0 : map->reloc);
1588f33f7deSMasami Hiramatsu 	}
1598f33f7deSMasami Hiramatsu 	return 0;
1608f33f7deSMasami Hiramatsu }
1618f33f7deSMasami Hiramatsu 
162e80711caSMasami Hiramatsu static struct map *kernel_get_module_map(const char *module)
163e80711caSMasami Hiramatsu {
164e80711caSMasami Hiramatsu 	struct rb_node *nd;
165ee45b6c2SMasami Hiramatsu 	struct map_groups *grp = &host_machine->kmaps;
166e80711caSMasami Hiramatsu 
16714a8fd7cSMasami Hiramatsu 	/* A file path -- this is an offline module */
16814a8fd7cSMasami Hiramatsu 	if (module && strchr(module, '/'))
169ee45b6c2SMasami Hiramatsu 		return machine__new_module(host_machine, 0, module);
17014a8fd7cSMasami Hiramatsu 
171e80711caSMasami Hiramatsu 	if (!module)
172e80711caSMasami Hiramatsu 		module = "kernel";
173e80711caSMasami Hiramatsu 
174e80711caSMasami Hiramatsu 	for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
175e80711caSMasami Hiramatsu 		struct map *pos = rb_entry(nd, struct map, rb_node);
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 */
1979b118acaSMasami Hiramatsu 		dso__delete(map->dso);
1989b118acaSMasami Hiramatsu 		map__delete(map);
1999b118acaSMasami Hiramatsu 	}
2009b118acaSMasami Hiramatsu }
2019b118acaSMasami Hiramatsu 
2029b118acaSMasami Hiramatsu 
203e80711caSMasami Hiramatsu static struct dso *kernel_get_module_dso(const char *module)
204469b9b88SMasami Hiramatsu {
205469b9b88SMasami Hiramatsu 	struct dso *dso;
206fd930ff9SFranck Bui-Huu 	struct map *map;
207fd930ff9SFranck Bui-Huu 	const char *vmlinux_name;
208469b9b88SMasami Hiramatsu 
209469b9b88SMasami Hiramatsu 	if (module) {
2108fa7d87fSWaiman Long 		list_for_each_entry(dso, &host_machine->kernel_dsos.head,
2118fa7d87fSWaiman Long 				    node) {
212469b9b88SMasami Hiramatsu 			if (strncmp(dso->short_name + 1, module,
213469b9b88SMasami Hiramatsu 				    dso->short_name_len - 2) == 0)
214469b9b88SMasami Hiramatsu 				goto found;
215469b9b88SMasami Hiramatsu 		}
216469b9b88SMasami Hiramatsu 		pr_debug("Failed to find module %s.\n", module);
217469b9b88SMasami Hiramatsu 		return NULL;
218fd930ff9SFranck Bui-Huu 	}
219fd930ff9SFranck Bui-Huu 
220ee45b6c2SMasami Hiramatsu 	map = host_machine->vmlinux_maps[MAP__FUNCTION];
221fd930ff9SFranck Bui-Huu 	dso = map->dso;
222fd930ff9SFranck Bui-Huu 
223fd930ff9SFranck Bui-Huu 	vmlinux_name = symbol_conf.vmlinux_name;
224fd930ff9SFranck Bui-Huu 	if (vmlinux_name) {
2255230fb7dSArnaldo Carvalho de Melo 		if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
226fd930ff9SFranck Bui-Huu 			return NULL;
227469b9b88SMasami Hiramatsu 	} else {
228c3a34e06SFranck Bui-Huu 		if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
229469b9b88SMasami Hiramatsu 			pr_debug("Failed to load kernel map.\n");
230469b9b88SMasami Hiramatsu 			return NULL;
231469b9b88SMasami Hiramatsu 		}
232469b9b88SMasami Hiramatsu 	}
233469b9b88SMasami Hiramatsu found:
234e80711caSMasami Hiramatsu 	return dso;
235e80711caSMasami Hiramatsu }
236e80711caSMasami Hiramatsu 
237e80711caSMasami Hiramatsu const char *kernel_get_module_path(const char *module)
238e80711caSMasami Hiramatsu {
239e80711caSMasami Hiramatsu 	struct dso *dso = kernel_get_module_dso(module);
240e80711caSMasami Hiramatsu 	return (dso) ? dso->long_name : NULL;
241469b9b88SMasami Hiramatsu }
242469b9b88SMasami Hiramatsu 
243fb7345bbSMasami Hiramatsu static int convert_exec_to_group(const char *exec, char **result)
244fb7345bbSMasami Hiramatsu {
245fb7345bbSMasami Hiramatsu 	char *ptr1, *ptr2, *exec_copy;
246fb7345bbSMasami Hiramatsu 	char buf[64];
247fb7345bbSMasami Hiramatsu 	int ret;
248fb7345bbSMasami Hiramatsu 
249fb7345bbSMasami Hiramatsu 	exec_copy = strdup(exec);
250fb7345bbSMasami Hiramatsu 	if (!exec_copy)
251fb7345bbSMasami Hiramatsu 		return -ENOMEM;
252fb7345bbSMasami Hiramatsu 
253fb7345bbSMasami Hiramatsu 	ptr1 = basename(exec_copy);
254fb7345bbSMasami Hiramatsu 	if (!ptr1) {
255fb7345bbSMasami Hiramatsu 		ret = -EINVAL;
256fb7345bbSMasami Hiramatsu 		goto out;
257fb7345bbSMasami Hiramatsu 	}
258fb7345bbSMasami Hiramatsu 
259fb7345bbSMasami Hiramatsu 	ptr2 = strpbrk(ptr1, "-._");
260fb7345bbSMasami Hiramatsu 	if (ptr2)
261fb7345bbSMasami Hiramatsu 		*ptr2 = '\0';
262fb7345bbSMasami Hiramatsu 	ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
263fb7345bbSMasami Hiramatsu 	if (ret < 0)
264fb7345bbSMasami Hiramatsu 		goto out;
265fb7345bbSMasami Hiramatsu 
266fb7345bbSMasami Hiramatsu 	*result = strdup(buf);
267fb7345bbSMasami Hiramatsu 	ret = *result ? 0 : -ENOMEM;
268fb7345bbSMasami Hiramatsu 
269fb7345bbSMasami Hiramatsu out:
270fb7345bbSMasami Hiramatsu 	free(exec_copy);
271fb7345bbSMasami Hiramatsu 	return ret;
272fb7345bbSMasami Hiramatsu }
273fb7345bbSMasami Hiramatsu 
2749b118acaSMasami Hiramatsu static void clear_perf_probe_point(struct perf_probe_point *pp)
2759b118acaSMasami Hiramatsu {
2769b118acaSMasami Hiramatsu 	free(pp->file);
2779b118acaSMasami Hiramatsu 	free(pp->function);
2789b118acaSMasami Hiramatsu 	free(pp->lazy_line);
2799b118acaSMasami Hiramatsu }
2809b118acaSMasami Hiramatsu 
281eb948e50SMasami Hiramatsu static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
282eb948e50SMasami Hiramatsu {
283eb948e50SMasami Hiramatsu 	int i;
284eb948e50SMasami Hiramatsu 
285eb948e50SMasami Hiramatsu 	for (i = 0; i < ntevs; i++)
286eb948e50SMasami Hiramatsu 		clear_probe_trace_event(tevs + i);
287eb948e50SMasami Hiramatsu }
288eb948e50SMasami Hiramatsu 
28989fe808aSIngo Molnar #ifdef HAVE_DWARF_SUPPORT
2909b118acaSMasami Hiramatsu /*
2919b118acaSMasami Hiramatsu  * Some binaries like glibc have special symbols which are on the symbol
2929b118acaSMasami Hiramatsu  * table, but not in the debuginfo. If we can find the address of the
2939b118acaSMasami Hiramatsu  * symbol from map, we can translate the address back to the probe point.
2949b118acaSMasami Hiramatsu  */
2959b118acaSMasami Hiramatsu static int find_alternative_probe_point(struct debuginfo *dinfo,
2969b118acaSMasami Hiramatsu 					struct perf_probe_point *pp,
2979b118acaSMasami Hiramatsu 					struct perf_probe_point *result,
2989b118acaSMasami Hiramatsu 					const char *target, bool uprobes)
2999b118acaSMasami Hiramatsu {
3009b118acaSMasami Hiramatsu 	struct map *map = NULL;
3019b118acaSMasami Hiramatsu 	struct symbol *sym;
3029b118acaSMasami Hiramatsu 	u64 address = 0;
3039b118acaSMasami Hiramatsu 	int ret = -ENOENT;
3049b118acaSMasami Hiramatsu 
3059b118acaSMasami Hiramatsu 	/* This can work only for function-name based one */
3069b118acaSMasami Hiramatsu 	if (!pp->function || pp->file)
3079b118acaSMasami Hiramatsu 		return -ENOTSUP;
3089b118acaSMasami Hiramatsu 
3099b118acaSMasami Hiramatsu 	map = get_target_map(target, uprobes);
3109b118acaSMasami Hiramatsu 	if (!map)
3119b118acaSMasami Hiramatsu 		return -EINVAL;
3129b118acaSMasami Hiramatsu 
3139b118acaSMasami Hiramatsu 	/* Find the address of given function */
3149b118acaSMasami Hiramatsu 	map__for_each_symbol_by_name(map, pp->function, sym) {
315e6d7c91cSMasami Hiramatsu 		if (uprobes)
3169b118acaSMasami Hiramatsu 			address = sym->start;
317e6d7c91cSMasami Hiramatsu 		else
318e6d7c91cSMasami Hiramatsu 			address = map->unmap_ip(map, sym->start);
3199b118acaSMasami Hiramatsu 		break;
3209b118acaSMasami Hiramatsu 	}
3219b118acaSMasami Hiramatsu 	if (!address) {
3229b118acaSMasami Hiramatsu 		ret = -ENOENT;
3239b118acaSMasami Hiramatsu 		goto out;
3249b118acaSMasami Hiramatsu 	}
325f6c15621SWang Nan 	pr_debug("Symbol %s address found : %" PRIx64 "\n",
326f6c15621SWang Nan 			pp->function, address);
3279b118acaSMasami Hiramatsu 
3289b118acaSMasami Hiramatsu 	ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
3299b118acaSMasami Hiramatsu 					  result);
3309b118acaSMasami Hiramatsu 	if (ret <= 0)
3319b118acaSMasami Hiramatsu 		ret = (!ret) ? -ENOENT : ret;
3329b118acaSMasami Hiramatsu 	else {
3339b118acaSMasami Hiramatsu 		result->offset += pp->offset;
3349b118acaSMasami Hiramatsu 		result->line += pp->line;
3359d7b45c5SHe Kuang 		result->retprobe = pp->retprobe;
3369b118acaSMasami Hiramatsu 		ret = 0;
3379b118acaSMasami Hiramatsu 	}
3389b118acaSMasami Hiramatsu 
3399b118acaSMasami Hiramatsu out:
3409b118acaSMasami Hiramatsu 	put_target_map(map, uprobes);
3419b118acaSMasami Hiramatsu 	return ret;
3429b118acaSMasami Hiramatsu 
3439b118acaSMasami Hiramatsu }
3449b118acaSMasami Hiramatsu 
3459b118acaSMasami Hiramatsu static int get_alternative_probe_event(struct debuginfo *dinfo,
3469b118acaSMasami Hiramatsu 				       struct perf_probe_event *pev,
3479b118acaSMasami Hiramatsu 				       struct perf_probe_point *tmp,
3489b118acaSMasami Hiramatsu 				       const char *target)
3499b118acaSMasami Hiramatsu {
3509b118acaSMasami Hiramatsu 	int ret;
3519b118acaSMasami Hiramatsu 
3529b118acaSMasami Hiramatsu 	memcpy(tmp, &pev->point, sizeof(*tmp));
3539b118acaSMasami Hiramatsu 	memset(&pev->point, 0, sizeof(pev->point));
3549b118acaSMasami Hiramatsu 	ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
3559b118acaSMasami Hiramatsu 					   target, pev->uprobes);
3569b118acaSMasami Hiramatsu 	if (ret < 0)
3579b118acaSMasami Hiramatsu 		memcpy(&pev->point, tmp, sizeof(*tmp));
3589b118acaSMasami Hiramatsu 
3599b118acaSMasami Hiramatsu 	return ret;
3609b118acaSMasami Hiramatsu }
361a15ad2f5SMasami Hiramatsu 
362811dd2aeSMasami Hiramatsu static int get_alternative_line_range(struct debuginfo *dinfo,
363811dd2aeSMasami Hiramatsu 				      struct line_range *lr,
364811dd2aeSMasami Hiramatsu 				      const char *target, bool user)
365811dd2aeSMasami Hiramatsu {
3666d4a4896SDavid Ahern 	struct perf_probe_point pp = { .function = lr->function,
3676d4a4896SDavid Ahern 				       .file = lr->file,
3686d4a4896SDavid Ahern 				       .line = lr->start };
3696d4a4896SDavid Ahern 	struct perf_probe_point result;
370811dd2aeSMasami Hiramatsu 	int ret, len = 0;
371811dd2aeSMasami Hiramatsu 
3726d4a4896SDavid Ahern 	memset(&result, 0, sizeof(result));
3736d4a4896SDavid Ahern 
374811dd2aeSMasami Hiramatsu 	if (lr->end != INT_MAX)
375811dd2aeSMasami Hiramatsu 		len = lr->end - lr->start;
376811dd2aeSMasami Hiramatsu 	ret = find_alternative_probe_point(dinfo, &pp, &result,
377811dd2aeSMasami Hiramatsu 					   target, user);
378811dd2aeSMasami Hiramatsu 	if (!ret) {
379811dd2aeSMasami Hiramatsu 		lr->function = result.function;
380811dd2aeSMasami Hiramatsu 		lr->file = result.file;
381811dd2aeSMasami Hiramatsu 		lr->start = result.line;
382811dd2aeSMasami Hiramatsu 		if (lr->end != INT_MAX)
383811dd2aeSMasami Hiramatsu 			lr->end = lr->start + len;
384811dd2aeSMasami Hiramatsu 		clear_perf_probe_point(&pp);
385811dd2aeSMasami Hiramatsu 	}
386811dd2aeSMasami Hiramatsu 	return ret;
387811dd2aeSMasami Hiramatsu }
388811dd2aeSMasami Hiramatsu 
389ff741783SMasami Hiramatsu /* Open new debuginfo of given module */
39092561cb7SMasami Hiramatsu static struct debuginfo *open_debuginfo(const char *module, bool silent)
391469b9b88SMasami Hiramatsu {
392a15ad2f5SMasami Hiramatsu 	const char *path = module;
39392561cb7SMasami Hiramatsu 	struct debuginfo *ret;
39414a8fd7cSMasami Hiramatsu 
395a15ad2f5SMasami Hiramatsu 	if (!module || !strchr(module, '/')) {
39614a8fd7cSMasami Hiramatsu 		path = kernel_get_module_path(module);
397469b9b88SMasami Hiramatsu 		if (!path) {
39892561cb7SMasami Hiramatsu 			if (!silent)
3990e43e5d2SMasami Hiramatsu 				pr_err("Failed to find path of %s module.\n",
4000e43e5d2SMasami Hiramatsu 				       module ?: "kernel");
401ff741783SMasami Hiramatsu 			return NULL;
402469b9b88SMasami Hiramatsu 		}
40314a8fd7cSMasami Hiramatsu 	}
40492561cb7SMasami Hiramatsu 	ret = debuginfo__new(path);
40592561cb7SMasami Hiramatsu 	if (!ret && !silent) {
40692561cb7SMasami Hiramatsu 		pr_warning("The %s file has no debug information.\n", path);
40792561cb7SMasami Hiramatsu 		if (!module || !strtailcmp(path, ".ko"))
40892561cb7SMasami Hiramatsu 			pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
40992561cb7SMasami Hiramatsu 		else
41092561cb7SMasami Hiramatsu 			pr_warning("Rebuild with -g, ");
41192561cb7SMasami Hiramatsu 		pr_warning("or install an appropriate debuginfo package.\n");
412e0faa8d3SMasami Hiramatsu 	}
41392561cb7SMasami Hiramatsu 	return ret;
41492561cb7SMasami Hiramatsu }
41592561cb7SMasami Hiramatsu 
4164b4da7f7SMasami Hiramatsu 
41799ca4233SMasami Hiramatsu static int get_text_start_address(const char *exec, unsigned long *address)
41899ca4233SMasami Hiramatsu {
41999ca4233SMasami Hiramatsu 	Elf *elf;
42099ca4233SMasami Hiramatsu 	GElf_Ehdr ehdr;
42199ca4233SMasami Hiramatsu 	GElf_Shdr shdr;
42299ca4233SMasami Hiramatsu 	int fd, ret = -ENOENT;
42399ca4233SMasami Hiramatsu 
42499ca4233SMasami Hiramatsu 	fd = open(exec, O_RDONLY);
42599ca4233SMasami Hiramatsu 	if (fd < 0)
42699ca4233SMasami Hiramatsu 		return -errno;
42799ca4233SMasami Hiramatsu 
42899ca4233SMasami Hiramatsu 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
42999ca4233SMasami Hiramatsu 	if (elf == NULL)
43099ca4233SMasami Hiramatsu 		return -EINVAL;
43199ca4233SMasami Hiramatsu 
43299ca4233SMasami Hiramatsu 	if (gelf_getehdr(elf, &ehdr) == NULL)
43399ca4233SMasami Hiramatsu 		goto out;
43499ca4233SMasami Hiramatsu 
43599ca4233SMasami Hiramatsu 	if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
43699ca4233SMasami Hiramatsu 		goto out;
43799ca4233SMasami Hiramatsu 
43899ca4233SMasami Hiramatsu 	*address = shdr.sh_addr - shdr.sh_offset;
43999ca4233SMasami Hiramatsu 	ret = 0;
44099ca4233SMasami Hiramatsu out:
44199ca4233SMasami Hiramatsu 	elf_end(elf);
44299ca4233SMasami Hiramatsu 	return ret;
44399ca4233SMasami Hiramatsu }
44499ca4233SMasami Hiramatsu 
4455a6f6314SMasami Hiramatsu /*
4465a6f6314SMasami Hiramatsu  * Convert trace point to probe point with debuginfo
4475a6f6314SMasami Hiramatsu  */
4485a6f6314SMasami Hiramatsu static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
4495a6f6314SMasami Hiramatsu 					    struct perf_probe_point *pp,
4505a6f6314SMasami Hiramatsu 					    bool is_kprobe)
4515a6f6314SMasami Hiramatsu {
4525a6f6314SMasami Hiramatsu 	struct debuginfo *dinfo = NULL;
4535a6f6314SMasami Hiramatsu 	unsigned long stext = 0;
4545a6f6314SMasami Hiramatsu 	u64 addr = tp->address;
4555a6f6314SMasami Hiramatsu 	int ret = -ENOENT;
4565a6f6314SMasami Hiramatsu 
4575a6f6314SMasami Hiramatsu 	/* convert the address to dwarf address */
4585a6f6314SMasami Hiramatsu 	if (!is_kprobe) {
4595a6f6314SMasami Hiramatsu 		if (!addr) {
4605a6f6314SMasami Hiramatsu 			ret = -EINVAL;
4615a6f6314SMasami Hiramatsu 			goto error;
4625a6f6314SMasami Hiramatsu 		}
4635a6f6314SMasami Hiramatsu 		ret = get_text_start_address(tp->module, &stext);
4645a6f6314SMasami Hiramatsu 		if (ret < 0)
4655a6f6314SMasami Hiramatsu 			goto error;
4665a6f6314SMasami Hiramatsu 		addr += stext;
4675a6f6314SMasami Hiramatsu 	} else {
4685a6f6314SMasami Hiramatsu 		addr = kernel_get_symbol_address_by_name(tp->symbol, false);
4695a6f6314SMasami Hiramatsu 		if (addr == 0)
4705a6f6314SMasami Hiramatsu 			goto error;
4715a6f6314SMasami Hiramatsu 		addr += tp->offset;
4725a6f6314SMasami Hiramatsu 	}
4735a6f6314SMasami Hiramatsu 
4745a6f6314SMasami Hiramatsu 	pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
4755a6f6314SMasami Hiramatsu 		 tp->module ? : "kernel");
4765a6f6314SMasami Hiramatsu 
47792561cb7SMasami Hiramatsu 	dinfo = open_debuginfo(tp->module, verbose == 0);
4785a6f6314SMasami Hiramatsu 	if (dinfo) {
4795a6f6314SMasami Hiramatsu 		ret = debuginfo__find_probe_point(dinfo,
4805a6f6314SMasami Hiramatsu 						 (unsigned long)addr, pp);
4815a6f6314SMasami Hiramatsu 		debuginfo__delete(dinfo);
48292561cb7SMasami Hiramatsu 	} else
4835a6f6314SMasami Hiramatsu 		ret = -ENOENT;
4845a6f6314SMasami Hiramatsu 
4855a6f6314SMasami Hiramatsu 	if (ret > 0) {
4865a6f6314SMasami Hiramatsu 		pp->retprobe = tp->retprobe;
4875a6f6314SMasami Hiramatsu 		return 0;
4885a6f6314SMasami Hiramatsu 	}
4895a6f6314SMasami Hiramatsu error:
4905a6f6314SMasami Hiramatsu 	pr_debug("Failed to find corresponding probes from debuginfo.\n");
4915a6f6314SMasami Hiramatsu 	return ret ? : -ENOENT;
4925a6f6314SMasami Hiramatsu }
4935a6f6314SMasami Hiramatsu 
494fb7345bbSMasami Hiramatsu static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
495fb7345bbSMasami Hiramatsu 					  int ntevs, const char *exec)
496fb7345bbSMasami Hiramatsu {
497fb7345bbSMasami Hiramatsu 	int i, ret = 0;
498eb948e50SMasami Hiramatsu 	unsigned long stext = 0;
499fb7345bbSMasami Hiramatsu 
500fb7345bbSMasami Hiramatsu 	if (!exec)
501fb7345bbSMasami Hiramatsu 		return 0;
502fb7345bbSMasami Hiramatsu 
503fb7345bbSMasami Hiramatsu 	ret = get_text_start_address(exec, &stext);
504fb7345bbSMasami Hiramatsu 	if (ret < 0)
505fb7345bbSMasami Hiramatsu 		return ret;
506fb7345bbSMasami Hiramatsu 
507fb7345bbSMasami Hiramatsu 	for (i = 0; i < ntevs && ret >= 0; i++) {
508981a2379SMasami Hiramatsu 		/* point.address is the addres of point.symbol + point.offset */
509eb948e50SMasami Hiramatsu 		tevs[i].point.address -= stext;
510fb7345bbSMasami Hiramatsu 		tevs[i].point.module = strdup(exec);
511eb948e50SMasami Hiramatsu 		if (!tevs[i].point.module) {
512fb7345bbSMasami Hiramatsu 			ret = -ENOMEM;
513fb7345bbSMasami Hiramatsu 			break;
514fb7345bbSMasami Hiramatsu 		}
515fb7345bbSMasami Hiramatsu 		tevs[i].uprobes = true;
516fb7345bbSMasami Hiramatsu 	}
517fb7345bbSMasami Hiramatsu 
518fb7345bbSMasami Hiramatsu 	return ret;
519fb7345bbSMasami Hiramatsu }
520fb7345bbSMasami Hiramatsu 
521190b57fcSMasami Hiramatsu static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
522190b57fcSMasami Hiramatsu 					    int ntevs, const char *module)
523190b57fcSMasami Hiramatsu {
52414a8fd7cSMasami Hiramatsu 	int i, ret = 0;
52514a8fd7cSMasami Hiramatsu 	char *tmp;
52614a8fd7cSMasami Hiramatsu 
52714a8fd7cSMasami Hiramatsu 	if (!module)
52814a8fd7cSMasami Hiramatsu 		return 0;
52914a8fd7cSMasami Hiramatsu 
53014a8fd7cSMasami Hiramatsu 	tmp = strrchr(module, '/');
53114a8fd7cSMasami Hiramatsu 	if (tmp) {
53214a8fd7cSMasami Hiramatsu 		/* This is a module path -- get the module name */
53314a8fd7cSMasami Hiramatsu 		module = strdup(tmp + 1);
53414a8fd7cSMasami Hiramatsu 		if (!module)
53514a8fd7cSMasami Hiramatsu 			return -ENOMEM;
53614a8fd7cSMasami Hiramatsu 		tmp = strchr(module, '.');
53714a8fd7cSMasami Hiramatsu 		if (tmp)
53814a8fd7cSMasami Hiramatsu 			*tmp = '\0';
53914a8fd7cSMasami Hiramatsu 		tmp = (char *)module;	/* For free() */
54014a8fd7cSMasami Hiramatsu 	}
54114a8fd7cSMasami Hiramatsu 
542190b57fcSMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
543190b57fcSMasami Hiramatsu 		tevs[i].point.module = strdup(module);
54414a8fd7cSMasami Hiramatsu 		if (!tevs[i].point.module) {
54514a8fd7cSMasami Hiramatsu 			ret = -ENOMEM;
54614a8fd7cSMasami Hiramatsu 			break;
547190b57fcSMasami Hiramatsu 		}
54814a8fd7cSMasami Hiramatsu 	}
54914a8fd7cSMasami Hiramatsu 
55014a8fd7cSMasami Hiramatsu 	free(tmp);
55114a8fd7cSMasami Hiramatsu 	return ret;
552190b57fcSMasami Hiramatsu }
553190b57fcSMasami Hiramatsu 
554dfef99cdSMasami Hiramatsu /* Post processing the probe events */
555dfef99cdSMasami Hiramatsu static int post_process_probe_trace_events(struct probe_trace_event *tevs,
556dfef99cdSMasami Hiramatsu 					   int ntevs, const char *module,
557dfef99cdSMasami Hiramatsu 					   bool uprobe)
558dfef99cdSMasami Hiramatsu {
559dfef99cdSMasami Hiramatsu 	struct ref_reloc_sym *reloc_sym;
560dfef99cdSMasami Hiramatsu 	char *tmp;
561dfef99cdSMasami Hiramatsu 	int i;
562dfef99cdSMasami Hiramatsu 
563dfef99cdSMasami Hiramatsu 	if (uprobe)
564dfef99cdSMasami Hiramatsu 		return add_exec_to_probe_trace_events(tevs, ntevs, module);
565dfef99cdSMasami Hiramatsu 
566dfef99cdSMasami Hiramatsu 	/* Note that currently ref_reloc_sym based probe is not for drivers */
567dfef99cdSMasami Hiramatsu 	if (module)
568dfef99cdSMasami Hiramatsu 		return add_module_to_probe_trace_events(tevs, ntevs, module);
569dfef99cdSMasami Hiramatsu 
5708f33f7deSMasami Hiramatsu 	reloc_sym = kernel_get_ref_reloc_sym();
571dfef99cdSMasami Hiramatsu 	if (!reloc_sym) {
572dfef99cdSMasami Hiramatsu 		pr_warning("Relocated base symbol is not found!\n");
573dfef99cdSMasami Hiramatsu 		return -EINVAL;
574dfef99cdSMasami Hiramatsu 	}
575dfef99cdSMasami Hiramatsu 
576dfef99cdSMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
57725dd9171SNamhyung Kim 		if (tevs[i].point.address && !tevs[i].point.retprobe) {
578dfef99cdSMasami Hiramatsu 			tmp = strdup(reloc_sym->name);
579dfef99cdSMasami Hiramatsu 			if (!tmp)
580dfef99cdSMasami Hiramatsu 				return -ENOMEM;
581dfef99cdSMasami Hiramatsu 			free(tevs[i].point.symbol);
582dfef99cdSMasami Hiramatsu 			tevs[i].point.symbol = tmp;
583dfef99cdSMasami Hiramatsu 			tevs[i].point.offset = tevs[i].point.address -
584dfef99cdSMasami Hiramatsu 					       reloc_sym->unrelocated_addr;
585dfef99cdSMasami Hiramatsu 		}
586dfef99cdSMasami Hiramatsu 	}
587dfef99cdSMasami Hiramatsu 	return 0;
588dfef99cdSMasami Hiramatsu }
589dfef99cdSMasami Hiramatsu 
5904b4da7f7SMasami Hiramatsu /* Try to find perf_probe_event with debuginfo */
5910e60836bSSrikar Dronamraju static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
5920e60836bSSrikar Dronamraju 					  struct probe_trace_event **tevs,
5934eced234SSrikar Dronamraju 					  int max_tevs, const char *target)
5944b4da7f7SMasami Hiramatsu {
5954b4da7f7SMasami Hiramatsu 	bool need_dwarf = perf_probe_event_need_dwarf(pev);
5969b118acaSMasami Hiramatsu 	struct perf_probe_point tmp;
597225466f1SSrikar Dronamraju 	struct debuginfo *dinfo;
598190b57fcSMasami Hiramatsu 	int ntevs, ret = 0;
5994b4da7f7SMasami Hiramatsu 
60092561cb7SMasami Hiramatsu 	dinfo = open_debuginfo(target, !need_dwarf);
601225466f1SSrikar Dronamraju 
602ff741783SMasami Hiramatsu 	if (!dinfo) {
60392561cb7SMasami Hiramatsu 		if (need_dwarf)
604ff741783SMasami Hiramatsu 			return -ENOENT;
605ff741783SMasami Hiramatsu 		pr_debug("Could not open debuginfo. Try to use symbols.\n");
6064b4da7f7SMasami Hiramatsu 		return 0;
6074b4da7f7SMasami Hiramatsu 	}
6084b4da7f7SMasami Hiramatsu 
609dfef99cdSMasami Hiramatsu 	pr_debug("Try to find probe point from debuginfo.\n");
610ff741783SMasami Hiramatsu 	/* Searching trace events corresponding to a probe event */
611ff741783SMasami Hiramatsu 	ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
612ff741783SMasami Hiramatsu 
6139b118acaSMasami Hiramatsu 	if (ntevs == 0)	{  /* Not found, retry with an alternative */
6149b118acaSMasami Hiramatsu 		ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
6159b118acaSMasami Hiramatsu 		if (!ret) {
6169b118acaSMasami Hiramatsu 			ntevs = debuginfo__find_trace_events(dinfo, pev,
6179b118acaSMasami Hiramatsu 							     tevs, max_tevs);
6189b118acaSMasami Hiramatsu 			/*
6199b118acaSMasami Hiramatsu 			 * Write back to the original probe_event for
6209b118acaSMasami Hiramatsu 			 * setting appropriate (user given) event name
6219b118acaSMasami Hiramatsu 			 */
6229b118acaSMasami Hiramatsu 			clear_perf_probe_point(&pev->point);
6239b118acaSMasami Hiramatsu 			memcpy(&pev->point, &tmp, sizeof(tmp));
6249b118acaSMasami Hiramatsu 		}
6259b118acaSMasami Hiramatsu 	}
6269b118acaSMasami Hiramatsu 
627ff741783SMasami Hiramatsu 	debuginfo__delete(dinfo);
6284b4da7f7SMasami Hiramatsu 
629146a1439SMasami Hiramatsu 	if (ntevs > 0) {	/* Succeeded to find trace events */
630dfef99cdSMasami Hiramatsu 		pr_debug("Found %d probe_trace_events.\n", ntevs);
631dfef99cdSMasami Hiramatsu 		ret = post_process_probe_trace_events(*tevs, ntevs,
632dfef99cdSMasami Hiramatsu 							target, pev->uprobes);
633981d05adSMasami Hiramatsu 		if (ret < 0) {
634981d05adSMasami Hiramatsu 			clear_probe_trace_events(*tevs, ntevs);
635981d05adSMasami Hiramatsu 			zfree(tevs);
636981d05adSMasami Hiramatsu 		}
637190b57fcSMasami Hiramatsu 		return ret < 0 ? ret : ntevs;
638146a1439SMasami Hiramatsu 	}
6394b4da7f7SMasami Hiramatsu 
640146a1439SMasami Hiramatsu 	if (ntevs == 0)	{	/* No error but failed to find probe point. */
6410687eba7SMasami Hiramatsu 		pr_warning("Probe point '%s' not found.\n",
6424b4da7f7SMasami Hiramatsu 			   synthesize_perf_probe_point(&pev->point));
643146a1439SMasami Hiramatsu 		return -ENOENT;
644146a1439SMasami Hiramatsu 	}
645146a1439SMasami Hiramatsu 	/* Error path : ntevs < 0 */
64615eca306SMasami Hiramatsu 	pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
64715eca306SMasami Hiramatsu 	if (ntevs == -EBADF) {
64815eca306SMasami Hiramatsu 		pr_warning("Warning: No dwarf info found in the vmlinux - "
64915eca306SMasami Hiramatsu 			"please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
65015eca306SMasami Hiramatsu 		if (!need_dwarf) {
6510e43e5d2SMasami Hiramatsu 			pr_debug("Trying to use symbols.\n");
6524b4da7f7SMasami Hiramatsu 			return 0;
6534b4da7f7SMasami Hiramatsu 		}
65415eca306SMasami Hiramatsu 	}
65515eca306SMasami Hiramatsu 	return ntevs;
65615eca306SMasami Hiramatsu }
6574b4da7f7SMasami Hiramatsu 
6584b4da7f7SMasami Hiramatsu #define LINEBUF_SIZE 256
6594b4da7f7SMasami Hiramatsu #define NR_ADDITIONAL_LINES 2
6604b4da7f7SMasami Hiramatsu 
661fde52dbdSFranck Bui-Huu static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
6624b4da7f7SMasami Hiramatsu {
6635f03cba4SMasami Hiramatsu 	char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
664befe3414SFranck Bui-Huu 	const char *color = show_num ? "" : PERF_COLOR_BLUE;
665befe3414SFranck Bui-Huu 	const char *prefix = NULL;
6664b4da7f7SMasami Hiramatsu 
667befe3414SFranck Bui-Huu 	do {
6684b4da7f7SMasami Hiramatsu 		if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
6694b4da7f7SMasami Hiramatsu 			goto error;
670befe3414SFranck Bui-Huu 		if (skip)
671befe3414SFranck Bui-Huu 			continue;
672befe3414SFranck Bui-Huu 		if (!prefix) {
673befe3414SFranck Bui-Huu 			prefix = show_num ? "%7d  " : "         ";
674befe3414SFranck Bui-Huu 			color_fprintf(stdout, color, prefix, l);
6754b4da7f7SMasami Hiramatsu 		}
676befe3414SFranck Bui-Huu 		color_fprintf(stdout, color, "%s", buf);
6774b4da7f7SMasami Hiramatsu 
678befe3414SFranck Bui-Huu 	} while (strchr(buf, '\n') == NULL);
679146a1439SMasami Hiramatsu 
680fde52dbdSFranck Bui-Huu 	return 1;
6814b4da7f7SMasami Hiramatsu error:
682fde52dbdSFranck Bui-Huu 	if (ferror(fp)) {
6835f03cba4SMasami Hiramatsu 		pr_warning("File read error: %s\n",
6845f03cba4SMasami Hiramatsu 			   strerror_r(errno, sbuf, sizeof(sbuf)));
685146a1439SMasami Hiramatsu 		return -1;
6864b4da7f7SMasami Hiramatsu 	}
687fde52dbdSFranck Bui-Huu 	return 0;
688fde52dbdSFranck Bui-Huu }
689fde52dbdSFranck Bui-Huu 
690fde52dbdSFranck Bui-Huu static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
691fde52dbdSFranck Bui-Huu {
692fde52dbdSFranck Bui-Huu 	int rv = __show_one_line(fp, l, skip, show_num);
693fde52dbdSFranck Bui-Huu 	if (rv == 0) {
694fde52dbdSFranck Bui-Huu 		pr_warning("Source file is shorter than expected.\n");
695fde52dbdSFranck Bui-Huu 		rv = -1;
696fde52dbdSFranck Bui-Huu 	}
697fde52dbdSFranck Bui-Huu 	return rv;
698fde52dbdSFranck Bui-Huu }
699fde52dbdSFranck Bui-Huu 
700fde52dbdSFranck Bui-Huu #define show_one_line_with_num(f,l)	_show_one_line(f,l,false,true)
701fde52dbdSFranck Bui-Huu #define show_one_line(f,l)		_show_one_line(f,l,false,false)
702fde52dbdSFranck Bui-Huu #define skip_one_line(f,l)		_show_one_line(f,l,true,false)
703fde52dbdSFranck Bui-Huu #define show_one_line_or_eof(f,l)	__show_one_line(f,l,false,false)
7044b4da7f7SMasami Hiramatsu 
7054b4da7f7SMasami Hiramatsu /*
7064b4da7f7SMasami Hiramatsu  * Show line-range always requires debuginfo to find source file and
7074b4da7f7SMasami Hiramatsu  * line number.
7084b4da7f7SMasami Hiramatsu  */
709811dd2aeSMasami Hiramatsu static int __show_line_range(struct line_range *lr, const char *module,
710811dd2aeSMasami Hiramatsu 			     bool user)
7114b4da7f7SMasami Hiramatsu {
712d3b63d7aSMasami Hiramatsu 	int l = 1;
7135a62257aSMasami Hiramatsu 	struct int_node *ln;
714ff741783SMasami Hiramatsu 	struct debuginfo *dinfo;
7154b4da7f7SMasami Hiramatsu 	FILE *fp;
716ff741783SMasami Hiramatsu 	int ret;
7177cf0b79eSMasami Hiramatsu 	char *tmp;
7185f03cba4SMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE];
7194b4da7f7SMasami Hiramatsu 
7204b4da7f7SMasami Hiramatsu 	/* Search a line range */
72192561cb7SMasami Hiramatsu 	dinfo = open_debuginfo(module, false);
72292561cb7SMasami Hiramatsu 	if (!dinfo)
723ff741783SMasami Hiramatsu 		return -ENOENT;
724146a1439SMasami Hiramatsu 
725ff741783SMasami Hiramatsu 	ret = debuginfo__find_line_range(dinfo, lr);
726811dd2aeSMasami Hiramatsu 	if (!ret) {	/* Not found, retry with an alternative */
727811dd2aeSMasami Hiramatsu 		ret = get_alternative_line_range(dinfo, lr, module, user);
728811dd2aeSMasami Hiramatsu 		if (!ret)
729811dd2aeSMasami Hiramatsu 			ret = debuginfo__find_line_range(dinfo, lr);
730811dd2aeSMasami Hiramatsu 	}
731ff741783SMasami Hiramatsu 	debuginfo__delete(dinfo);
7325ee05b88SMasami Hiramatsu 	if (ret == 0 || ret == -ENOENT) {
733146a1439SMasami Hiramatsu 		pr_warning("Specified source line is not found.\n");
734146a1439SMasami Hiramatsu 		return -ENOENT;
735146a1439SMasami Hiramatsu 	} else if (ret < 0) {
7365ee05b88SMasami Hiramatsu 		pr_warning("Debuginfo analysis failed.\n");
737146a1439SMasami Hiramatsu 		return ret;
738146a1439SMasami Hiramatsu 	}
7394b4da7f7SMasami Hiramatsu 
7407cf0b79eSMasami Hiramatsu 	/* Convert source file path */
7417cf0b79eSMasami Hiramatsu 	tmp = lr->path;
7426a330a3cSMasami Hiramatsu 	ret = get_real_path(tmp, lr->comp_dir, &lr->path);
743a78604deSHe Kuang 
744a78604deSHe Kuang 	/* Free old path when new path is assigned */
745a78604deSHe Kuang 	if (tmp != lr->path)
746a78604deSHe Kuang 		free(tmp);
747a78604deSHe Kuang 
7487cf0b79eSMasami Hiramatsu 	if (ret < 0) {
7495ee05b88SMasami Hiramatsu 		pr_warning("Failed to find source file path.\n");
7507cf0b79eSMasami Hiramatsu 		return ret;
7517cf0b79eSMasami Hiramatsu 	}
7527cf0b79eSMasami Hiramatsu 
7534b4da7f7SMasami Hiramatsu 	setup_pager();
7544b4da7f7SMasami Hiramatsu 
7554b4da7f7SMasami Hiramatsu 	if (lr->function)
7568737ebdeSMasami Hiramatsu 		fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
7574b4da7f7SMasami Hiramatsu 			lr->start - lr->offset);
7584b4da7f7SMasami Hiramatsu 	else
75962c15fc4SFranck Bui-Huu 		fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
7604b4da7f7SMasami Hiramatsu 
7614b4da7f7SMasami Hiramatsu 	fp = fopen(lr->path, "r");
762146a1439SMasami Hiramatsu 	if (fp == NULL) {
763146a1439SMasami Hiramatsu 		pr_warning("Failed to open %s: %s\n", lr->path,
7645f03cba4SMasami Hiramatsu 			   strerror_r(errno, sbuf, sizeof(sbuf)));
765146a1439SMasami Hiramatsu 		return -errno;
766146a1439SMasami Hiramatsu 	}
7674b4da7f7SMasami Hiramatsu 	/* Skip to starting line number */
76844b81e92SFranck Bui-Huu 	while (l < lr->start) {
769fde52dbdSFranck Bui-Huu 		ret = skip_one_line(fp, l++);
770146a1439SMasami Hiramatsu 		if (ret < 0)
771146a1439SMasami Hiramatsu 			goto end;
77244b81e92SFranck Bui-Huu 	}
7734b4da7f7SMasami Hiramatsu 
7745a62257aSMasami Hiramatsu 	intlist__for_each(ln, lr->line_list) {
7755a62257aSMasami Hiramatsu 		for (; ln->i > l; l++) {
776fde52dbdSFranck Bui-Huu 			ret = show_one_line(fp, l - lr->offset);
77744b81e92SFranck Bui-Huu 			if (ret < 0)
77844b81e92SFranck Bui-Huu 				goto end;
77944b81e92SFranck Bui-Huu 		}
780fde52dbdSFranck Bui-Huu 		ret = show_one_line_with_num(fp, l++ - lr->offset);
781146a1439SMasami Hiramatsu 		if (ret < 0)
782146a1439SMasami Hiramatsu 			goto end;
7834b4da7f7SMasami Hiramatsu 	}
7844b4da7f7SMasami Hiramatsu 
7854b4da7f7SMasami Hiramatsu 	if (lr->end == INT_MAX)
7864b4da7f7SMasami Hiramatsu 		lr->end = l + NR_ADDITIONAL_LINES;
787fde52dbdSFranck Bui-Huu 	while (l <= lr->end) {
788fde52dbdSFranck Bui-Huu 		ret = show_one_line_or_eof(fp, l++ - lr->offset);
789fde52dbdSFranck Bui-Huu 		if (ret <= 0)
79044b81e92SFranck Bui-Huu 			break;
79144b81e92SFranck Bui-Huu 	}
792146a1439SMasami Hiramatsu end:
7934b4da7f7SMasami Hiramatsu 	fclose(fp);
794146a1439SMasami Hiramatsu 	return ret;
7954b4da7f7SMasami Hiramatsu }
7964b4da7f7SMasami Hiramatsu 
7972b394bc4SMasami Hiramatsu int show_line_range(struct line_range *lr, const char *module, bool user)
798ee45b6c2SMasami Hiramatsu {
799ee45b6c2SMasami Hiramatsu 	int ret;
800ee45b6c2SMasami Hiramatsu 
8012b394bc4SMasami Hiramatsu 	ret = init_symbol_maps(user);
802ee45b6c2SMasami Hiramatsu 	if (ret < 0)
803ee45b6c2SMasami Hiramatsu 		return ret;
804811dd2aeSMasami Hiramatsu 	ret = __show_line_range(lr, module, user);
805ee45b6c2SMasami Hiramatsu 	exit_symbol_maps();
806ee45b6c2SMasami Hiramatsu 
807ee45b6c2SMasami Hiramatsu 	return ret;
808ee45b6c2SMasami Hiramatsu }
809ee45b6c2SMasami Hiramatsu 
810ff741783SMasami Hiramatsu static int show_available_vars_at(struct debuginfo *dinfo,
811ff741783SMasami Hiramatsu 				  struct perf_probe_event *pev,
812bd09d7b5SMasami Hiramatsu 				  int max_vls, struct strfilter *_filter,
8139b118acaSMasami Hiramatsu 				  bool externs, const char *target)
814cf6eb489SMasami Hiramatsu {
815cf6eb489SMasami Hiramatsu 	char *buf;
816bd09d7b5SMasami Hiramatsu 	int ret, i, nvars;
817cf6eb489SMasami Hiramatsu 	struct str_node *node;
818cf6eb489SMasami Hiramatsu 	struct variable_list *vls = NULL, *vl;
8199b118acaSMasami Hiramatsu 	struct perf_probe_point tmp;
820bd09d7b5SMasami Hiramatsu 	const char *var;
821cf6eb489SMasami Hiramatsu 
822cf6eb489SMasami Hiramatsu 	buf = synthesize_perf_probe_point(&pev->point);
823cf6eb489SMasami Hiramatsu 	if (!buf)
824cf6eb489SMasami Hiramatsu 		return -EINVAL;
825cf6eb489SMasami Hiramatsu 	pr_debug("Searching variables at %s\n", buf);
826cf6eb489SMasami Hiramatsu 
827ff741783SMasami Hiramatsu 	ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
828ff741783SMasami Hiramatsu 						max_vls, externs);
8299b118acaSMasami Hiramatsu 	if (!ret) {  /* Not found, retry with an alternative */
8309b118acaSMasami Hiramatsu 		ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
8319b118acaSMasami Hiramatsu 		if (!ret) {
8329b118acaSMasami Hiramatsu 			ret = debuginfo__find_available_vars_at(dinfo, pev,
8339b118acaSMasami Hiramatsu 						&vls, max_vls, externs);
8349b118acaSMasami Hiramatsu 			/* Release the old probe_point */
8359b118acaSMasami Hiramatsu 			clear_perf_probe_point(&tmp);
8369b118acaSMasami Hiramatsu 		}
8379b118acaSMasami Hiramatsu 	}
838bd09d7b5SMasami Hiramatsu 	if (ret <= 0) {
83969e96eaaSMasami Hiramatsu 		if (ret == 0 || ret == -ENOENT) {
84069e96eaaSMasami Hiramatsu 			pr_err("Failed to find the address of %s\n", buf);
84169e96eaaSMasami Hiramatsu 			ret = -ENOENT;
84269e96eaaSMasami Hiramatsu 		} else
84369e96eaaSMasami Hiramatsu 			pr_warning("Debuginfo analysis failed.\n");
844bd09d7b5SMasami Hiramatsu 		goto end;
845bd09d7b5SMasami Hiramatsu 	}
84669e96eaaSMasami Hiramatsu 
847bd09d7b5SMasami Hiramatsu 	/* Some variables are found */
848cf6eb489SMasami Hiramatsu 	fprintf(stdout, "Available variables at %s\n", buf);
849cf6eb489SMasami Hiramatsu 	for (i = 0; i < ret; i++) {
850cf6eb489SMasami Hiramatsu 		vl = &vls[i];
851cf6eb489SMasami Hiramatsu 		/*
852cf6eb489SMasami Hiramatsu 		 * A probe point might be converted to
853cf6eb489SMasami Hiramatsu 		 * several trace points.
854cf6eb489SMasami Hiramatsu 		 */
855cf6eb489SMasami Hiramatsu 		fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
856cf6eb489SMasami Hiramatsu 			vl->point.offset);
85774cf249dSArnaldo Carvalho de Melo 		zfree(&vl->point.symbol);
858bd09d7b5SMasami Hiramatsu 		nvars = 0;
859cf6eb489SMasami Hiramatsu 		if (vl->vars) {
860bd09d7b5SMasami Hiramatsu 			strlist__for_each(node, vl->vars) {
861bd09d7b5SMasami Hiramatsu 				var = strchr(node->s, '\t') + 1;
862bd09d7b5SMasami Hiramatsu 				if (strfilter__compare(_filter, var)) {
863cf6eb489SMasami Hiramatsu 					fprintf(stdout, "\t\t%s\n", node->s);
864bd09d7b5SMasami Hiramatsu 					nvars++;
865bd09d7b5SMasami Hiramatsu 				}
866bd09d7b5SMasami Hiramatsu 			}
867cf6eb489SMasami Hiramatsu 			strlist__delete(vl->vars);
868bd09d7b5SMasami Hiramatsu 		}
869bd09d7b5SMasami Hiramatsu 		if (nvars == 0)
870bd09d7b5SMasami Hiramatsu 			fprintf(stdout, "\t\t(No matched variables)\n");
871cf6eb489SMasami Hiramatsu 	}
872cf6eb489SMasami Hiramatsu 	free(vls);
873bd09d7b5SMasami Hiramatsu end:
874cf6eb489SMasami Hiramatsu 	free(buf);
875cf6eb489SMasami Hiramatsu 	return ret;
876cf6eb489SMasami Hiramatsu }
877cf6eb489SMasami Hiramatsu 
878cf6eb489SMasami Hiramatsu /* Show available variables on given probe point */
879cf6eb489SMasami Hiramatsu int show_available_vars(struct perf_probe_event *pevs, int npevs,
880bd09d7b5SMasami Hiramatsu 			int max_vls, const char *module,
881bd09d7b5SMasami Hiramatsu 			struct strfilter *_filter, bool externs)
882cf6eb489SMasami Hiramatsu {
883ff741783SMasami Hiramatsu 	int i, ret = 0;
884ff741783SMasami Hiramatsu 	struct debuginfo *dinfo;
885cf6eb489SMasami Hiramatsu 
8862b394bc4SMasami Hiramatsu 	ret = init_symbol_maps(pevs->uprobes);
887cf6eb489SMasami Hiramatsu 	if (ret < 0)
888cf6eb489SMasami Hiramatsu 		return ret;
889cf6eb489SMasami Hiramatsu 
89092561cb7SMasami Hiramatsu 	dinfo = open_debuginfo(module, false);
891ff741783SMasami Hiramatsu 	if (!dinfo) {
892ee45b6c2SMasami Hiramatsu 		ret = -ENOENT;
893ee45b6c2SMasami Hiramatsu 		goto out;
894ff741783SMasami Hiramatsu 	}
895ff741783SMasami Hiramatsu 
896cc446446SMasami Hiramatsu 	setup_pager();
897cc446446SMasami Hiramatsu 
898ff741783SMasami Hiramatsu 	for (i = 0; i < npevs && ret >= 0; i++)
899ff741783SMasami Hiramatsu 		ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
9009b118acaSMasami Hiramatsu 					     externs, module);
901ff741783SMasami Hiramatsu 
902ff741783SMasami Hiramatsu 	debuginfo__delete(dinfo);
903ee45b6c2SMasami Hiramatsu out:
904ee45b6c2SMasami Hiramatsu 	exit_symbol_maps();
905cf6eb489SMasami Hiramatsu 	return ret;
906cf6eb489SMasami Hiramatsu }
907cf6eb489SMasami Hiramatsu 
90889fe808aSIngo Molnar #else	/* !HAVE_DWARF_SUPPORT */
9094b4da7f7SMasami Hiramatsu 
9105a6f6314SMasami Hiramatsu static int
9115a6f6314SMasami Hiramatsu find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
9125a6f6314SMasami Hiramatsu 				 struct perf_probe_point *pp __maybe_unused,
9135a6f6314SMasami Hiramatsu 				 bool is_kprobe __maybe_unused)
9144b4da7f7SMasami Hiramatsu {
9155a6f6314SMasami Hiramatsu 	return -ENOSYS;
9164b4da7f7SMasami Hiramatsu }
9174b4da7f7SMasami Hiramatsu 
9180e60836bSSrikar Dronamraju static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
9191d037ca1SIrina Tirdea 				struct probe_trace_event **tevs __maybe_unused,
9201d027ee9SArnaldo Carvalho de Melo 				int max_tevs __maybe_unused,
9211d027ee9SArnaldo Carvalho de Melo 				const char *target __maybe_unused)
9224b4da7f7SMasami Hiramatsu {
923146a1439SMasami Hiramatsu 	if (perf_probe_event_need_dwarf(pev)) {
924146a1439SMasami Hiramatsu 		pr_warning("Debuginfo-analysis is not supported.\n");
925146a1439SMasami Hiramatsu 		return -ENOSYS;
926146a1439SMasami Hiramatsu 	}
927225466f1SSrikar Dronamraju 
9284b4da7f7SMasami Hiramatsu 	return 0;
9294b4da7f7SMasami Hiramatsu }
9304b4da7f7SMasami Hiramatsu 
9311d037ca1SIrina Tirdea int show_line_range(struct line_range *lr __maybe_unused,
9322b394bc4SMasami Hiramatsu 		    const char *module __maybe_unused,
9332b394bc4SMasami Hiramatsu 		    bool user __maybe_unused)
9344b4da7f7SMasami Hiramatsu {
935146a1439SMasami Hiramatsu 	pr_warning("Debuginfo-analysis is not supported.\n");
936146a1439SMasami Hiramatsu 	return -ENOSYS;
9374b4da7f7SMasami Hiramatsu }
9384b4da7f7SMasami Hiramatsu 
9391d037ca1SIrina Tirdea int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
9401d037ca1SIrina Tirdea 			int npevs __maybe_unused, int max_vls __maybe_unused,
9411d037ca1SIrina Tirdea 			const char *module __maybe_unused,
9421d037ca1SIrina Tirdea 			struct strfilter *filter __maybe_unused,
9431d037ca1SIrina Tirdea 			bool externs __maybe_unused)
944cf6eb489SMasami Hiramatsu {
945cf6eb489SMasami Hiramatsu 	pr_warning("Debuginfo-analysis is not supported.\n");
946cf6eb489SMasami Hiramatsu 	return -ENOSYS;
947cf6eb489SMasami Hiramatsu }
948e0faa8d3SMasami Hiramatsu #endif
949e0faa8d3SMasami Hiramatsu 
950e53b00d3SMasami Hiramatsu void line_range__clear(struct line_range *lr)
951e53b00d3SMasami Hiramatsu {
952e53b00d3SMasami Hiramatsu 	free(lr->function);
953e53b00d3SMasami Hiramatsu 	free(lr->file);
954e53b00d3SMasami Hiramatsu 	free(lr->path);
955e53b00d3SMasami Hiramatsu 	free(lr->comp_dir);
9565a62257aSMasami Hiramatsu 	intlist__delete(lr->line_list);
957e53b00d3SMasami Hiramatsu 	memset(lr, 0, sizeof(*lr));
958e53b00d3SMasami Hiramatsu }
959e53b00d3SMasami Hiramatsu 
9605a62257aSMasami Hiramatsu int line_range__init(struct line_range *lr)
961e53b00d3SMasami Hiramatsu {
962e53b00d3SMasami Hiramatsu 	memset(lr, 0, sizeof(*lr));
9635a62257aSMasami Hiramatsu 	lr->line_list = intlist__new(NULL);
9645a62257aSMasami Hiramatsu 	if (!lr->line_list)
9655a62257aSMasami Hiramatsu 		return -ENOMEM;
9665a62257aSMasami Hiramatsu 	else
9675a62257aSMasami Hiramatsu 		return 0;
968e53b00d3SMasami Hiramatsu }
969e53b00d3SMasami Hiramatsu 
97021dd9ae5SFranck Bui-Huu static int parse_line_num(char **ptr, int *val, const char *what)
97121dd9ae5SFranck Bui-Huu {
97221dd9ae5SFranck Bui-Huu 	const char *start = *ptr;
97321dd9ae5SFranck Bui-Huu 
97421dd9ae5SFranck Bui-Huu 	errno = 0;
97521dd9ae5SFranck Bui-Huu 	*val = strtol(*ptr, ptr, 0);
97621dd9ae5SFranck Bui-Huu 	if (errno || *ptr == start) {
97721dd9ae5SFranck Bui-Huu 		semantic_error("'%s' is not a valid number.\n", what);
97821dd9ae5SFranck Bui-Huu 		return -EINVAL;
97921dd9ae5SFranck Bui-Huu 	}
98021dd9ae5SFranck Bui-Huu 	return 0;
98121dd9ae5SFranck Bui-Huu }
98221dd9ae5SFranck Bui-Huu 
9839d95b580SFranck Bui-Huu /*
9849d95b580SFranck Bui-Huu  * Stuff 'lr' according to the line range described by 'arg'.
9859d95b580SFranck Bui-Huu  * The line range syntax is described by:
9869d95b580SFranck Bui-Huu  *
9879d95b580SFranck Bui-Huu  *         SRC[:SLN[+NUM|-ELN]]
988e116dfa1SMasami Hiramatsu  *         FNC[@SRC][:SLN[+NUM|-ELN]]
9899d95b580SFranck Bui-Huu  */
990146a1439SMasami Hiramatsu int parse_line_range_desc(const char *arg, struct line_range *lr)
991631c9defSMasami Hiramatsu {
992e116dfa1SMasami Hiramatsu 	char *range, *file, *name = strdup(arg);
99321dd9ae5SFranck Bui-Huu 	int err;
9949d95b580SFranck Bui-Huu 
99521dd9ae5SFranck Bui-Huu 	if (!name)
99621dd9ae5SFranck Bui-Huu 		return -ENOMEM;
99721dd9ae5SFranck Bui-Huu 
99821dd9ae5SFranck Bui-Huu 	lr->start = 0;
99921dd9ae5SFranck Bui-Huu 	lr->end = INT_MAX;
100021dd9ae5SFranck Bui-Huu 
100121dd9ae5SFranck Bui-Huu 	range = strchr(name, ':');
100221dd9ae5SFranck Bui-Huu 	if (range) {
100321dd9ae5SFranck Bui-Huu 		*range++ = '\0';
100421dd9ae5SFranck Bui-Huu 
100521dd9ae5SFranck Bui-Huu 		err = parse_line_num(&range, &lr->start, "start line");
100621dd9ae5SFranck Bui-Huu 		if (err)
100721dd9ae5SFranck Bui-Huu 			goto err;
100821dd9ae5SFranck Bui-Huu 
100921dd9ae5SFranck Bui-Huu 		if (*range == '+' || *range == '-') {
101021dd9ae5SFranck Bui-Huu 			const char c = *range++;
101121dd9ae5SFranck Bui-Huu 
101221dd9ae5SFranck Bui-Huu 			err = parse_line_num(&range, &lr->end, "end line");
101321dd9ae5SFranck Bui-Huu 			if (err)
101421dd9ae5SFranck Bui-Huu 				goto err;
101521dd9ae5SFranck Bui-Huu 
101621dd9ae5SFranck Bui-Huu 			if (c == '+') {
101721dd9ae5SFranck Bui-Huu 				lr->end += lr->start;
101821dd9ae5SFranck Bui-Huu 				/*
1019dda4ab34SMasami Hiramatsu 				 * Adjust the number of lines here.
1020dda4ab34SMasami Hiramatsu 				 * If the number of lines == 1, the
1021dda4ab34SMasami Hiramatsu 				 * the end of line should be equal to
1022dda4ab34SMasami Hiramatsu 				 * the start of line.
1023dda4ab34SMasami Hiramatsu 				 */
102421dd9ae5SFranck Bui-Huu 				lr->end--;
102521dd9ae5SFranck Bui-Huu 			}
102621dd9ae5SFranck Bui-Huu 		}
102721dd9ae5SFranck Bui-Huu 
1028d3b63d7aSMasami Hiramatsu 		pr_debug("Line range is %d to %d\n", lr->start, lr->end);
102921dd9ae5SFranck Bui-Huu 
103021dd9ae5SFranck Bui-Huu 		err = -EINVAL;
1031d3b63d7aSMasami Hiramatsu 		if (lr->start > lr->end) {
1032631c9defSMasami Hiramatsu 			semantic_error("Start line must be smaller"
1033146a1439SMasami Hiramatsu 				       " than end line.\n");
103421dd9ae5SFranck Bui-Huu 			goto err;
1035146a1439SMasami Hiramatsu 		}
103621dd9ae5SFranck Bui-Huu 		if (*range != '\0') {
103721dd9ae5SFranck Bui-Huu 			semantic_error("Tailing with invalid str '%s'.\n", range);
103821dd9ae5SFranck Bui-Huu 			goto err;
1039146a1439SMasami Hiramatsu 		}
1040d3b63d7aSMasami Hiramatsu 	}
104102b95dadSMasami Hiramatsu 
1042e116dfa1SMasami Hiramatsu 	file = strchr(name, '@');
1043e116dfa1SMasami Hiramatsu 	if (file) {
1044e116dfa1SMasami Hiramatsu 		*file = '\0';
1045e116dfa1SMasami Hiramatsu 		lr->file = strdup(++file);
1046e116dfa1SMasami Hiramatsu 		if (lr->file == NULL) {
1047e116dfa1SMasami Hiramatsu 			err = -ENOMEM;
1048e116dfa1SMasami Hiramatsu 			goto err;
1049e116dfa1SMasami Hiramatsu 		}
1050e116dfa1SMasami Hiramatsu 		lr->function = name;
1051e116dfa1SMasami Hiramatsu 	} else if (strchr(name, '.'))
105221dd9ae5SFranck Bui-Huu 		lr->file = name;
1053631c9defSMasami Hiramatsu 	else
105421dd9ae5SFranck Bui-Huu 		lr->function = name;
1055146a1439SMasami Hiramatsu 
1056146a1439SMasami Hiramatsu 	return 0;
105721dd9ae5SFranck Bui-Huu err:
105821dd9ae5SFranck Bui-Huu 	free(name);
105921dd9ae5SFranck Bui-Huu 	return err;
1060631c9defSMasami Hiramatsu }
1061631c9defSMasami Hiramatsu 
1062b7702a21SMasami Hiramatsu /* Check the name is good for event/group */
1063b7702a21SMasami Hiramatsu static bool check_event_name(const char *name)
1064b7702a21SMasami Hiramatsu {
1065b7702a21SMasami Hiramatsu 	if (!isalpha(*name) && *name != '_')
1066b7702a21SMasami Hiramatsu 		return false;
1067b7702a21SMasami Hiramatsu 	while (*++name != '\0') {
1068b7702a21SMasami Hiramatsu 		if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1069b7702a21SMasami Hiramatsu 			return false;
1070b7702a21SMasami Hiramatsu 	}
1071b7702a21SMasami Hiramatsu 	return true;
1072b7702a21SMasami Hiramatsu }
1073b7702a21SMasami Hiramatsu 
107450656eecSMasami Hiramatsu /* Parse probepoint definition. */
1075146a1439SMasami Hiramatsu static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
107650656eecSMasami Hiramatsu {
10774235b045SMasami Hiramatsu 	struct perf_probe_point *pp = &pev->point;
107850656eecSMasami Hiramatsu 	char *ptr, *tmp;
107950656eecSMasami Hiramatsu 	char c, nc = 0;
10803099c026SNaveen N. Rao 	bool file_spec = false;
108150656eecSMasami Hiramatsu 	/*
108250656eecSMasami Hiramatsu 	 * <Syntax>
10832a9c8c36SMasami Hiramatsu 	 * perf probe [EVENT=]SRC[:LN|;PTN]
10842a9c8c36SMasami Hiramatsu 	 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1085af663d75SMasami Hiramatsu 	 *
1086af663d75SMasami Hiramatsu 	 * TODO:Group name support
108750656eecSMasami Hiramatsu 	 */
108850656eecSMasami Hiramatsu 
10892a9c8c36SMasami Hiramatsu 	ptr = strpbrk(arg, ";=@+%");
10902a9c8c36SMasami Hiramatsu 	if (ptr && *ptr == '=') {	/* Event name */
1091af663d75SMasami Hiramatsu 		*ptr = '\0';
1092af663d75SMasami Hiramatsu 		tmp = ptr + 1;
1093146a1439SMasami Hiramatsu 		if (strchr(arg, ':')) {
1094146a1439SMasami Hiramatsu 			semantic_error("Group name is not supported yet.\n");
1095146a1439SMasami Hiramatsu 			return -ENOTSUP;
1096146a1439SMasami Hiramatsu 		}
1097146a1439SMasami Hiramatsu 		if (!check_event_name(arg)) {
1098b7702a21SMasami Hiramatsu 			semantic_error("%s is bad for event name -it must "
1099146a1439SMasami Hiramatsu 				       "follow C symbol-naming rule.\n", arg);
1100146a1439SMasami Hiramatsu 			return -EINVAL;
1101146a1439SMasami Hiramatsu 		}
110202b95dadSMasami Hiramatsu 		pev->event = strdup(arg);
110302b95dadSMasami Hiramatsu 		if (pev->event == NULL)
110402b95dadSMasami Hiramatsu 			return -ENOMEM;
11054235b045SMasami Hiramatsu 		pev->group = NULL;
1106af663d75SMasami Hiramatsu 		arg = tmp;
1107af663d75SMasami Hiramatsu 	}
1108af663d75SMasami Hiramatsu 
11093099c026SNaveen N. Rao 	/*
11103099c026SNaveen N. Rao 	 * Check arg is function or file name and copy it.
11113099c026SNaveen N. Rao 	 *
11123099c026SNaveen N. Rao 	 * We consider arg to be a file spec if and only if it satisfies
11133099c026SNaveen N. Rao 	 * all of the below criteria::
11143099c026SNaveen N. Rao 	 * - it does not include any of "+@%",
11153099c026SNaveen N. Rao 	 * - it includes one of ":;", and
11163099c026SNaveen N. Rao 	 * - it has a period '.' in the name.
11173099c026SNaveen N. Rao 	 *
11183099c026SNaveen N. Rao 	 * Otherwise, we consider arg to be a function specification.
11193099c026SNaveen N. Rao 	 */
11203099c026SNaveen N. Rao 	if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
11213099c026SNaveen N. Rao 		/* This is a file spec if it includes a '.' before ; or : */
11223099c026SNaveen N. Rao 		if (memchr(arg, '.', ptr - arg))
11233099c026SNaveen N. Rao 			file_spec = true;
11243099c026SNaveen N. Rao 	}
11253099c026SNaveen N. Rao 
11262a9c8c36SMasami Hiramatsu 	ptr = strpbrk(arg, ";:+@%");
112750656eecSMasami Hiramatsu 	if (ptr) {
112850656eecSMasami Hiramatsu 		nc = *ptr;
112950656eecSMasami Hiramatsu 		*ptr++ = '\0';
113050656eecSMasami Hiramatsu 	}
113150656eecSMasami Hiramatsu 
113202b95dadSMasami Hiramatsu 	tmp = strdup(arg);
113302b95dadSMasami Hiramatsu 	if (tmp == NULL)
113402b95dadSMasami Hiramatsu 		return -ENOMEM;
113502b95dadSMasami Hiramatsu 
11363099c026SNaveen N. Rao 	if (file_spec)
113702b95dadSMasami Hiramatsu 		pp->file = tmp;
11383099c026SNaveen N. Rao 	else
113902b95dadSMasami Hiramatsu 		pp->function = tmp;
114050656eecSMasami Hiramatsu 
114150656eecSMasami Hiramatsu 	/* Parse other options */
114250656eecSMasami Hiramatsu 	while (ptr) {
114350656eecSMasami Hiramatsu 		arg = ptr;
114450656eecSMasami Hiramatsu 		c = nc;
11452a9c8c36SMasami Hiramatsu 		if (c == ';') {	/* Lazy pattern must be the last part */
114602b95dadSMasami Hiramatsu 			pp->lazy_line = strdup(arg);
114702b95dadSMasami Hiramatsu 			if (pp->lazy_line == NULL)
114802b95dadSMasami Hiramatsu 				return -ENOMEM;
11492a9c8c36SMasami Hiramatsu 			break;
11502a9c8c36SMasami Hiramatsu 		}
11512a9c8c36SMasami Hiramatsu 		ptr = strpbrk(arg, ";:+@%");
115250656eecSMasami Hiramatsu 		if (ptr) {
115350656eecSMasami Hiramatsu 			nc = *ptr;
115450656eecSMasami Hiramatsu 			*ptr++ = '\0';
115550656eecSMasami Hiramatsu 		}
115650656eecSMasami Hiramatsu 		switch (c) {
115750656eecSMasami Hiramatsu 		case ':':	/* Line number */
115850656eecSMasami Hiramatsu 			pp->line = strtoul(arg, &tmp, 0);
1159146a1439SMasami Hiramatsu 			if (*tmp != '\0') {
11602a9c8c36SMasami Hiramatsu 				semantic_error("There is non-digit char"
1161146a1439SMasami Hiramatsu 					       " in line number.\n");
1162146a1439SMasami Hiramatsu 				return -EINVAL;
1163146a1439SMasami Hiramatsu 			}
116450656eecSMasami Hiramatsu 			break;
116550656eecSMasami Hiramatsu 		case '+':	/* Byte offset from a symbol */
116650656eecSMasami Hiramatsu 			pp->offset = strtoul(arg, &tmp, 0);
1167146a1439SMasami Hiramatsu 			if (*tmp != '\0') {
11682a9c8c36SMasami Hiramatsu 				semantic_error("There is non-digit character"
1169146a1439SMasami Hiramatsu 						" in offset.\n");
1170146a1439SMasami Hiramatsu 				return -EINVAL;
1171146a1439SMasami Hiramatsu 			}
117250656eecSMasami Hiramatsu 			break;
117350656eecSMasami Hiramatsu 		case '@':	/* File name */
1174146a1439SMasami Hiramatsu 			if (pp->file) {
1175146a1439SMasami Hiramatsu 				semantic_error("SRC@SRC is not allowed.\n");
1176146a1439SMasami Hiramatsu 				return -EINVAL;
1177146a1439SMasami Hiramatsu 			}
117802b95dadSMasami Hiramatsu 			pp->file = strdup(arg);
117902b95dadSMasami Hiramatsu 			if (pp->file == NULL)
118002b95dadSMasami Hiramatsu 				return -ENOMEM;
118150656eecSMasami Hiramatsu 			break;
118250656eecSMasami Hiramatsu 		case '%':	/* Probe places */
118350656eecSMasami Hiramatsu 			if (strcmp(arg, "return") == 0) {
118450656eecSMasami Hiramatsu 				pp->retprobe = 1;
1185146a1439SMasami Hiramatsu 			} else {	/* Others not supported yet */
1186146a1439SMasami Hiramatsu 				semantic_error("%%%s is not supported.\n", arg);
1187146a1439SMasami Hiramatsu 				return -ENOTSUP;
1188146a1439SMasami Hiramatsu 			}
118950656eecSMasami Hiramatsu 			break;
1190146a1439SMasami Hiramatsu 		default:	/* Buggy case */
1191146a1439SMasami Hiramatsu 			pr_err("This program has a bug at %s:%d.\n",
1192146a1439SMasami Hiramatsu 				__FILE__, __LINE__);
1193146a1439SMasami Hiramatsu 			return -ENOTSUP;
119450656eecSMasami Hiramatsu 			break;
119550656eecSMasami Hiramatsu 		}
119650656eecSMasami Hiramatsu 	}
119750656eecSMasami Hiramatsu 
119850656eecSMasami Hiramatsu 	/* Exclusion check */
1199146a1439SMasami Hiramatsu 	if (pp->lazy_line && pp->line) {
12000e43e5d2SMasami Hiramatsu 		semantic_error("Lazy pattern can't be used with"
12010e43e5d2SMasami Hiramatsu 			       " line number.\n");
1202146a1439SMasami Hiramatsu 		return -EINVAL;
1203146a1439SMasami Hiramatsu 	}
12042a9c8c36SMasami Hiramatsu 
1205146a1439SMasami Hiramatsu 	if (pp->lazy_line && pp->offset) {
12060e43e5d2SMasami Hiramatsu 		semantic_error("Lazy pattern can't be used with offset.\n");
1207146a1439SMasami Hiramatsu 		return -EINVAL;
1208146a1439SMasami Hiramatsu 	}
12092a9c8c36SMasami Hiramatsu 
1210146a1439SMasami Hiramatsu 	if (pp->line && pp->offset) {
12110e43e5d2SMasami Hiramatsu 		semantic_error("Offset can't be used with line number.\n");
1212146a1439SMasami Hiramatsu 		return -EINVAL;
1213146a1439SMasami Hiramatsu 	}
121450656eecSMasami Hiramatsu 
1215146a1439SMasami Hiramatsu 	if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
12162a9c8c36SMasami Hiramatsu 		semantic_error("File always requires line number or "
12170e43e5d2SMasami Hiramatsu 			       "lazy pattern.\n");
1218146a1439SMasami Hiramatsu 		return -EINVAL;
1219146a1439SMasami Hiramatsu 	}
122050656eecSMasami Hiramatsu 
1221146a1439SMasami Hiramatsu 	if (pp->offset && !pp->function) {
12220e43e5d2SMasami Hiramatsu 		semantic_error("Offset requires an entry function.\n");
1223146a1439SMasami Hiramatsu 		return -EINVAL;
1224146a1439SMasami Hiramatsu 	}
122550656eecSMasami Hiramatsu 
1226146a1439SMasami Hiramatsu 	if (pp->retprobe && !pp->function) {
12270e43e5d2SMasami Hiramatsu 		semantic_error("Return probe requires an entry function.\n");
1228146a1439SMasami Hiramatsu 		return -EINVAL;
1229146a1439SMasami Hiramatsu 	}
123050656eecSMasami Hiramatsu 
1231146a1439SMasami Hiramatsu 	if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
12322a9c8c36SMasami Hiramatsu 		semantic_error("Offset/Line/Lazy pattern can't be used with "
12330e43e5d2SMasami Hiramatsu 			       "return probe.\n");
1234146a1439SMasami Hiramatsu 		return -EINVAL;
1235146a1439SMasami Hiramatsu 	}
123650656eecSMasami Hiramatsu 
12374235b045SMasami Hiramatsu 	pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
12382a9c8c36SMasami Hiramatsu 		 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
12392a9c8c36SMasami Hiramatsu 		 pp->lazy_line);
1240146a1439SMasami Hiramatsu 	return 0;
124150656eecSMasami Hiramatsu }
124250656eecSMasami Hiramatsu 
12437df2f329SMasami Hiramatsu /* Parse perf-probe event argument */
1244146a1439SMasami Hiramatsu static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
12457df2f329SMasami Hiramatsu {
1246b2a3c12bSMasami Hiramatsu 	char *tmp, *goodname;
12477df2f329SMasami Hiramatsu 	struct perf_probe_arg_field **fieldp;
12487df2f329SMasami Hiramatsu 
12497df2f329SMasami Hiramatsu 	pr_debug("parsing arg: %s into ", str);
12507df2f329SMasami Hiramatsu 
125148481938SMasami Hiramatsu 	tmp = strchr(str, '=');
125248481938SMasami Hiramatsu 	if (tmp) {
125302b95dadSMasami Hiramatsu 		arg->name = strndup(str, tmp - str);
125402b95dadSMasami Hiramatsu 		if (arg->name == NULL)
125502b95dadSMasami Hiramatsu 			return -ENOMEM;
125611a1ca35SMasami Hiramatsu 		pr_debug("name:%s ", arg->name);
125748481938SMasami Hiramatsu 		str = tmp + 1;
125848481938SMasami Hiramatsu 	}
125948481938SMasami Hiramatsu 
126011a1ca35SMasami Hiramatsu 	tmp = strchr(str, ':');
126111a1ca35SMasami Hiramatsu 	if (tmp) {	/* Type setting */
126211a1ca35SMasami Hiramatsu 		*tmp = '\0';
126302b95dadSMasami Hiramatsu 		arg->type = strdup(tmp + 1);
126402b95dadSMasami Hiramatsu 		if (arg->type == NULL)
126502b95dadSMasami Hiramatsu 			return -ENOMEM;
126611a1ca35SMasami Hiramatsu 		pr_debug("type:%s ", arg->type);
126711a1ca35SMasami Hiramatsu 	}
126811a1ca35SMasami Hiramatsu 
1269b2a3c12bSMasami Hiramatsu 	tmp = strpbrk(str, "-.[");
12707df2f329SMasami Hiramatsu 	if (!is_c_varname(str) || !tmp) {
12717df2f329SMasami Hiramatsu 		/* A variable, register, symbol or special value */
127202b95dadSMasami Hiramatsu 		arg->var = strdup(str);
127302b95dadSMasami Hiramatsu 		if (arg->var == NULL)
127402b95dadSMasami Hiramatsu 			return -ENOMEM;
127548481938SMasami Hiramatsu 		pr_debug("%s\n", arg->var);
1276146a1439SMasami Hiramatsu 		return 0;
12777df2f329SMasami Hiramatsu 	}
12787df2f329SMasami Hiramatsu 
1279b2a3c12bSMasami Hiramatsu 	/* Structure fields or array element */
128002b95dadSMasami Hiramatsu 	arg->var = strndup(str, tmp - str);
128102b95dadSMasami Hiramatsu 	if (arg->var == NULL)
128202b95dadSMasami Hiramatsu 		return -ENOMEM;
1283b2a3c12bSMasami Hiramatsu 	goodname = arg->var;
128448481938SMasami Hiramatsu 	pr_debug("%s, ", arg->var);
12857df2f329SMasami Hiramatsu 	fieldp = &arg->field;
12867df2f329SMasami Hiramatsu 
12877df2f329SMasami Hiramatsu 	do {
1288e334016fSMasami Hiramatsu 		*fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1289e334016fSMasami Hiramatsu 		if (*fieldp == NULL)
1290e334016fSMasami Hiramatsu 			return -ENOMEM;
1291b2a3c12bSMasami Hiramatsu 		if (*tmp == '[') {	/* Array */
1292b2a3c12bSMasami Hiramatsu 			str = tmp;
1293b2a3c12bSMasami Hiramatsu 			(*fieldp)->index = strtol(str + 1, &tmp, 0);
1294b2a3c12bSMasami Hiramatsu 			(*fieldp)->ref = true;
1295b2a3c12bSMasami Hiramatsu 			if (*tmp != ']' || tmp == str + 1) {
1296b2a3c12bSMasami Hiramatsu 				semantic_error("Array index must be a"
1297b2a3c12bSMasami Hiramatsu 						" number.\n");
1298b2a3c12bSMasami Hiramatsu 				return -EINVAL;
1299b2a3c12bSMasami Hiramatsu 			}
1300b2a3c12bSMasami Hiramatsu 			tmp++;
1301b2a3c12bSMasami Hiramatsu 			if (*tmp == '\0')
1302b2a3c12bSMasami Hiramatsu 				tmp = NULL;
1303b2a3c12bSMasami Hiramatsu 		} else {		/* Structure */
13047df2f329SMasami Hiramatsu 			if (*tmp == '.') {
13057df2f329SMasami Hiramatsu 				str = tmp + 1;
13067df2f329SMasami Hiramatsu 				(*fieldp)->ref = false;
13077df2f329SMasami Hiramatsu 			} else if (tmp[1] == '>') {
13087df2f329SMasami Hiramatsu 				str = tmp + 2;
13097df2f329SMasami Hiramatsu 				(*fieldp)->ref = true;
1310146a1439SMasami Hiramatsu 			} else {
1311b2a3c12bSMasami Hiramatsu 				semantic_error("Argument parse error: %s\n",
1312b2a3c12bSMasami Hiramatsu 					       str);
1313146a1439SMasami Hiramatsu 				return -EINVAL;
1314146a1439SMasami Hiramatsu 			}
1315b2a3c12bSMasami Hiramatsu 			tmp = strpbrk(str, "-.[");
1316b2a3c12bSMasami Hiramatsu 		}
13177df2f329SMasami Hiramatsu 		if (tmp) {
131802b95dadSMasami Hiramatsu 			(*fieldp)->name = strndup(str, tmp - str);
131902b95dadSMasami Hiramatsu 			if ((*fieldp)->name == NULL)
132002b95dadSMasami Hiramatsu 				return -ENOMEM;
1321b2a3c12bSMasami Hiramatsu 			if (*str != '[')
1322b2a3c12bSMasami Hiramatsu 				goodname = (*fieldp)->name;
13237df2f329SMasami Hiramatsu 			pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
13247df2f329SMasami Hiramatsu 			fieldp = &(*fieldp)->next;
13257df2f329SMasami Hiramatsu 		}
13267df2f329SMasami Hiramatsu 	} while (tmp);
132702b95dadSMasami Hiramatsu 	(*fieldp)->name = strdup(str);
132802b95dadSMasami Hiramatsu 	if ((*fieldp)->name == NULL)
132902b95dadSMasami Hiramatsu 		return -ENOMEM;
1330b2a3c12bSMasami Hiramatsu 	if (*str != '[')
1331b2a3c12bSMasami Hiramatsu 		goodname = (*fieldp)->name;
13327df2f329SMasami Hiramatsu 	pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1333df0faf4bSMasami Hiramatsu 
1334b2a3c12bSMasami Hiramatsu 	/* If no name is specified, set the last field name (not array index)*/
133502b95dadSMasami Hiramatsu 	if (!arg->name) {
1336b2a3c12bSMasami Hiramatsu 		arg->name = strdup(goodname);
133702b95dadSMasami Hiramatsu 		if (arg->name == NULL)
133802b95dadSMasami Hiramatsu 			return -ENOMEM;
133902b95dadSMasami Hiramatsu 	}
1340146a1439SMasami Hiramatsu 	return 0;
13417df2f329SMasami Hiramatsu }
13427df2f329SMasami Hiramatsu 
13434235b045SMasami Hiramatsu /* Parse perf-probe event command */
1344146a1439SMasami Hiramatsu int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
134550656eecSMasami Hiramatsu {
1346e1c01d61SMasami Hiramatsu 	char **argv;
1347146a1439SMasami Hiramatsu 	int argc, i, ret = 0;
1348fac13fd5SMasami Hiramatsu 
13494235b045SMasami Hiramatsu 	argv = argv_split(cmd, &argc);
1350146a1439SMasami Hiramatsu 	if (!argv) {
1351146a1439SMasami Hiramatsu 		pr_debug("Failed to split arguments.\n");
1352146a1439SMasami Hiramatsu 		return -ENOMEM;
1353146a1439SMasami Hiramatsu 	}
1354146a1439SMasami Hiramatsu 	if (argc - 1 > MAX_PROBE_ARGS) {
1355146a1439SMasami Hiramatsu 		semantic_error("Too many probe arguments (%d).\n", argc - 1);
1356146a1439SMasami Hiramatsu 		ret = -ERANGE;
1357146a1439SMasami Hiramatsu 		goto out;
1358146a1439SMasami Hiramatsu 	}
135950656eecSMasami Hiramatsu 	/* Parse probe point */
1360146a1439SMasami Hiramatsu 	ret = parse_perf_probe_point(argv[0], pev);
1361146a1439SMasami Hiramatsu 	if (ret < 0)
1362146a1439SMasami Hiramatsu 		goto out;
136350656eecSMasami Hiramatsu 
1364e1c01d61SMasami Hiramatsu 	/* Copy arguments and ensure return probe has no C argument */
13654235b045SMasami Hiramatsu 	pev->nargs = argc - 1;
1366e334016fSMasami Hiramatsu 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1367e334016fSMasami Hiramatsu 	if (pev->args == NULL) {
1368e334016fSMasami Hiramatsu 		ret = -ENOMEM;
1369e334016fSMasami Hiramatsu 		goto out;
1370e334016fSMasami Hiramatsu 	}
1371146a1439SMasami Hiramatsu 	for (i = 0; i < pev->nargs && ret >= 0; i++) {
1372146a1439SMasami Hiramatsu 		ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1373146a1439SMasami Hiramatsu 		if (ret >= 0 &&
1374146a1439SMasami Hiramatsu 		    is_c_varname(pev->args[i].var) && pev->point.retprobe) {
13754235b045SMasami Hiramatsu 			semantic_error("You can't specify local variable for"
1376146a1439SMasami Hiramatsu 				       " kretprobe.\n");
1377146a1439SMasami Hiramatsu 			ret = -EINVAL;
1378e1c01d61SMasami Hiramatsu 		}
1379146a1439SMasami Hiramatsu 	}
1380146a1439SMasami Hiramatsu out:
1381e1c01d61SMasami Hiramatsu 	argv_free(argv);
1382146a1439SMasami Hiramatsu 
1383146a1439SMasami Hiramatsu 	return ret;
138450656eecSMasami Hiramatsu }
138550656eecSMasami Hiramatsu 
13864235b045SMasami Hiramatsu /* Return true if this perf_probe_event requires debuginfo */
13874235b045SMasami Hiramatsu bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
13884de189feSMasami Hiramatsu {
13894235b045SMasami Hiramatsu 	int i;
13904235b045SMasami Hiramatsu 
13914235b045SMasami Hiramatsu 	if (pev->point.file || pev->point.line || pev->point.lazy_line)
13924235b045SMasami Hiramatsu 		return true;
13934235b045SMasami Hiramatsu 
13944235b045SMasami Hiramatsu 	for (i = 0; i < pev->nargs; i++)
139548481938SMasami Hiramatsu 		if (is_c_varname(pev->args[i].var))
13964235b045SMasami Hiramatsu 			return true;
13974235b045SMasami Hiramatsu 
13984235b045SMasami Hiramatsu 	return false;
13994235b045SMasami Hiramatsu }
14004235b045SMasami Hiramatsu 
14010e60836bSSrikar Dronamraju /* Parse probe_events event into struct probe_point */
14020e60836bSSrikar Dronamraju static int parse_probe_trace_command(const char *cmd,
14030e60836bSSrikar Dronamraju 				     struct probe_trace_event *tev)
14044235b045SMasami Hiramatsu {
14050e60836bSSrikar Dronamraju 	struct probe_trace_point *tp = &tev->point;
14064de189feSMasami Hiramatsu 	char pr;
14074de189feSMasami Hiramatsu 	char *p;
1408bcbd0040SIrina Tirdea 	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
14094de189feSMasami Hiramatsu 	int ret, i, argc;
14104de189feSMasami Hiramatsu 	char **argv;
14114de189feSMasami Hiramatsu 
14120e60836bSSrikar Dronamraju 	pr_debug("Parsing probe_events: %s\n", cmd);
14134235b045SMasami Hiramatsu 	argv = argv_split(cmd, &argc);
1414146a1439SMasami Hiramatsu 	if (!argv) {
1415146a1439SMasami Hiramatsu 		pr_debug("Failed to split arguments.\n");
1416146a1439SMasami Hiramatsu 		return -ENOMEM;
1417146a1439SMasami Hiramatsu 	}
1418146a1439SMasami Hiramatsu 	if (argc < 2) {
1419146a1439SMasami Hiramatsu 		semantic_error("Too few probe arguments.\n");
1420146a1439SMasami Hiramatsu 		ret = -ERANGE;
1421146a1439SMasami Hiramatsu 		goto out;
1422146a1439SMasami Hiramatsu 	}
14234de189feSMasami Hiramatsu 
14244de189feSMasami Hiramatsu 	/* Scan event and group name. */
1425bcbd0040SIrina Tirdea 	argv0_str = strdup(argv[0]);
1426bcbd0040SIrina Tirdea 	if (argv0_str == NULL) {
1427bcbd0040SIrina Tirdea 		ret = -ENOMEM;
1428bcbd0040SIrina Tirdea 		goto out;
1429bcbd0040SIrina Tirdea 	}
1430bcbd0040SIrina Tirdea 	fmt1_str = strtok_r(argv0_str, ":", &fmt);
1431bcbd0040SIrina Tirdea 	fmt2_str = strtok_r(NULL, "/", &fmt);
1432bcbd0040SIrina Tirdea 	fmt3_str = strtok_r(NULL, " \t", &fmt);
1433bcbd0040SIrina Tirdea 	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1434bcbd0040SIrina Tirdea 	    || fmt3_str == NULL) {
1435146a1439SMasami Hiramatsu 		semantic_error("Failed to parse event name: %s\n", argv[0]);
1436146a1439SMasami Hiramatsu 		ret = -EINVAL;
1437146a1439SMasami Hiramatsu 		goto out;
1438146a1439SMasami Hiramatsu 	}
1439bcbd0040SIrina Tirdea 	pr = fmt1_str[0];
1440bcbd0040SIrina Tirdea 	tev->group = strdup(fmt2_str);
1441bcbd0040SIrina Tirdea 	tev->event = strdup(fmt3_str);
1442bcbd0040SIrina Tirdea 	if (tev->group == NULL || tev->event == NULL) {
1443bcbd0040SIrina Tirdea 		ret = -ENOMEM;
1444bcbd0040SIrina Tirdea 		goto out;
1445bcbd0040SIrina Tirdea 	}
14464235b045SMasami Hiramatsu 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
14474de189feSMasami Hiramatsu 
14484235b045SMasami Hiramatsu 	tp->retprobe = (pr == 'r');
14494de189feSMasami Hiramatsu 
1450190b57fcSMasami Hiramatsu 	/* Scan module name(if there), function name and offset */
1451190b57fcSMasami Hiramatsu 	p = strchr(argv[1], ':');
1452190b57fcSMasami Hiramatsu 	if (p) {
1453190b57fcSMasami Hiramatsu 		tp->module = strndup(argv[1], p - argv[1]);
1454190b57fcSMasami Hiramatsu 		p++;
1455190b57fcSMasami Hiramatsu 	} else
1456190b57fcSMasami Hiramatsu 		p = argv[1];
1457bcbd0040SIrina Tirdea 	fmt1_str = strtok_r(p, "+", &fmt);
14585a6f6314SMasami Hiramatsu 	if (fmt1_str[0] == '0')	/* only the address started with 0x */
14595a6f6314SMasami Hiramatsu 		tp->address = strtoul(fmt1_str, NULL, 0);
14605a6f6314SMasami Hiramatsu 	else {
14615a6f6314SMasami Hiramatsu 		/* Only the symbol-based probe has offset */
1462bcbd0040SIrina Tirdea 		tp->symbol = strdup(fmt1_str);
1463bcbd0040SIrina Tirdea 		if (tp->symbol == NULL) {
1464bcbd0040SIrina Tirdea 			ret = -ENOMEM;
1465bcbd0040SIrina Tirdea 			goto out;
1466bcbd0040SIrina Tirdea 		}
1467bcbd0040SIrina Tirdea 		fmt2_str = strtok_r(NULL, "", &fmt);
1468bcbd0040SIrina Tirdea 		if (fmt2_str == NULL)
14694235b045SMasami Hiramatsu 			tp->offset = 0;
1470bcbd0040SIrina Tirdea 		else
1471bcbd0040SIrina Tirdea 			tp->offset = strtoul(fmt2_str, NULL, 10);
14725a6f6314SMasami Hiramatsu 	}
14734de189feSMasami Hiramatsu 
14744235b045SMasami Hiramatsu 	tev->nargs = argc - 2;
14750e60836bSSrikar Dronamraju 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1476e334016fSMasami Hiramatsu 	if (tev->args == NULL) {
1477e334016fSMasami Hiramatsu 		ret = -ENOMEM;
1478e334016fSMasami Hiramatsu 		goto out;
1479e334016fSMasami Hiramatsu 	}
14804235b045SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++) {
14814de189feSMasami Hiramatsu 		p = strchr(argv[i + 2], '=');
14824de189feSMasami Hiramatsu 		if (p)	/* We don't need which register is assigned. */
14834235b045SMasami Hiramatsu 			*p++ = '\0';
14844235b045SMasami Hiramatsu 		else
14854235b045SMasami Hiramatsu 			p = argv[i + 2];
148602b95dadSMasami Hiramatsu 		tev->args[i].name = strdup(argv[i + 2]);
14874235b045SMasami Hiramatsu 		/* TODO: parse regs and offset */
148802b95dadSMasami Hiramatsu 		tev->args[i].value = strdup(p);
148902b95dadSMasami Hiramatsu 		if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
149002b95dadSMasami Hiramatsu 			ret = -ENOMEM;
149102b95dadSMasami Hiramatsu 			goto out;
149202b95dadSMasami Hiramatsu 		}
14934de189feSMasami Hiramatsu 	}
1494146a1439SMasami Hiramatsu 	ret = 0;
1495146a1439SMasami Hiramatsu out:
1496bcbd0040SIrina Tirdea 	free(argv0_str);
14974de189feSMasami Hiramatsu 	argv_free(argv);
1498146a1439SMasami Hiramatsu 	return ret;
14994de189feSMasami Hiramatsu }
15004de189feSMasami Hiramatsu 
15017df2f329SMasami Hiramatsu /* Compose only probe arg */
15027df2f329SMasami Hiramatsu int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
15037df2f329SMasami Hiramatsu {
15047df2f329SMasami Hiramatsu 	struct perf_probe_arg_field *field = pa->field;
15057df2f329SMasami Hiramatsu 	int ret;
15067df2f329SMasami Hiramatsu 	char *tmp = buf;
15077df2f329SMasami Hiramatsu 
150848481938SMasami Hiramatsu 	if (pa->name && pa->var)
150948481938SMasami Hiramatsu 		ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
151048481938SMasami Hiramatsu 	else
151148481938SMasami Hiramatsu 		ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
15127df2f329SMasami Hiramatsu 	if (ret <= 0)
15137df2f329SMasami Hiramatsu 		goto error;
15147df2f329SMasami Hiramatsu 	tmp += ret;
15157df2f329SMasami Hiramatsu 	len -= ret;
15167df2f329SMasami Hiramatsu 
15177df2f329SMasami Hiramatsu 	while (field) {
1518b2a3c12bSMasami Hiramatsu 		if (field->name[0] == '[')
1519b2a3c12bSMasami Hiramatsu 			ret = e_snprintf(tmp, len, "%s", field->name);
1520b2a3c12bSMasami Hiramatsu 		else
1521b2a3c12bSMasami Hiramatsu 			ret = e_snprintf(tmp, len, "%s%s",
1522b2a3c12bSMasami Hiramatsu 					 field->ref ? "->" : ".", field->name);
15237df2f329SMasami Hiramatsu 		if (ret <= 0)
15247df2f329SMasami Hiramatsu 			goto error;
15257df2f329SMasami Hiramatsu 		tmp += ret;
15267df2f329SMasami Hiramatsu 		len -= ret;
15277df2f329SMasami Hiramatsu 		field = field->next;
15287df2f329SMasami Hiramatsu 	}
152911a1ca35SMasami Hiramatsu 
153011a1ca35SMasami Hiramatsu 	if (pa->type) {
153111a1ca35SMasami Hiramatsu 		ret = e_snprintf(tmp, len, ":%s", pa->type);
153211a1ca35SMasami Hiramatsu 		if (ret <= 0)
153311a1ca35SMasami Hiramatsu 			goto error;
153411a1ca35SMasami Hiramatsu 		tmp += ret;
153511a1ca35SMasami Hiramatsu 		len -= ret;
153611a1ca35SMasami Hiramatsu 	}
153711a1ca35SMasami Hiramatsu 
15387df2f329SMasami Hiramatsu 	return tmp - buf;
15397df2f329SMasami Hiramatsu error:
15405f03cba4SMasami Hiramatsu 	pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1541146a1439SMasami Hiramatsu 	return ret;
15427df2f329SMasami Hiramatsu }
15437df2f329SMasami Hiramatsu 
15444235b045SMasami Hiramatsu /* Compose only probe point (not argument) */
15454235b045SMasami Hiramatsu static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
154650656eecSMasami Hiramatsu {
1547fb1587d8SMasami Hiramatsu 	char *buf, *tmp;
1548fb1587d8SMasami Hiramatsu 	char offs[32] = "", line[32] = "", file[32] = "";
1549fb1587d8SMasami Hiramatsu 	int ret, len;
155050656eecSMasami Hiramatsu 
1551e334016fSMasami Hiramatsu 	buf = zalloc(MAX_CMDLEN);
1552e334016fSMasami Hiramatsu 	if (buf == NULL) {
1553e334016fSMasami Hiramatsu 		ret = -ENOMEM;
1554e334016fSMasami Hiramatsu 		goto error;
1555e334016fSMasami Hiramatsu 	}
15564de189feSMasami Hiramatsu 	if (pp->offset) {
1557fb1587d8SMasami Hiramatsu 		ret = e_snprintf(offs, 32, "+%lu", pp->offset);
15584de189feSMasami Hiramatsu 		if (ret <= 0)
15594de189feSMasami Hiramatsu 			goto error;
15604de189feSMasami Hiramatsu 	}
15614de189feSMasami Hiramatsu 	if (pp->line) {
1562fb1587d8SMasami Hiramatsu 		ret = e_snprintf(line, 32, ":%d", pp->line);
1563fb1587d8SMasami Hiramatsu 		if (ret <= 0)
1564fb1587d8SMasami Hiramatsu 			goto error;
1565fb1587d8SMasami Hiramatsu 	}
1566fb1587d8SMasami Hiramatsu 	if (pp->file) {
156732ae2adeSFranck Bui-Huu 		tmp = pp->file;
156832ae2adeSFranck Bui-Huu 		len = strlen(tmp);
156932ae2adeSFranck Bui-Huu 		if (len > 30) {
157032ae2adeSFranck Bui-Huu 			tmp = strchr(pp->file + len - 30, '/');
157132ae2adeSFranck Bui-Huu 			tmp = tmp ? tmp + 1 : pp->file + len - 30;
157232ae2adeSFranck Bui-Huu 		}
157332ae2adeSFranck Bui-Huu 		ret = e_snprintf(file, 32, "@%s", tmp);
15744de189feSMasami Hiramatsu 		if (ret <= 0)
15754de189feSMasami Hiramatsu 			goto error;
15764de189feSMasami Hiramatsu 	}
15774de189feSMasami Hiramatsu 
15784de189feSMasami Hiramatsu 	if (pp->function)
1579fb1587d8SMasami Hiramatsu 		ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1580fb1587d8SMasami Hiramatsu 				 offs, pp->retprobe ? "%return" : "", line,
1581fb1587d8SMasami Hiramatsu 				 file);
15824de189feSMasami Hiramatsu 	else
1583fb1587d8SMasami Hiramatsu 		ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
15844235b045SMasami Hiramatsu 	if (ret <= 0)
15854235b045SMasami Hiramatsu 		goto error;
15864235b045SMasami Hiramatsu 
15874235b045SMasami Hiramatsu 	return buf;
15884235b045SMasami Hiramatsu error:
15895f03cba4SMasami Hiramatsu 	pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1590146a1439SMasami Hiramatsu 	free(buf);
1591146a1439SMasami Hiramatsu 	return NULL;
15924235b045SMasami Hiramatsu }
15934235b045SMasami Hiramatsu 
15944235b045SMasami Hiramatsu #if 0
15954235b045SMasami Hiramatsu char *synthesize_perf_probe_command(struct perf_probe_event *pev)
15964235b045SMasami Hiramatsu {
15974235b045SMasami Hiramatsu 	char *buf;
15984235b045SMasami Hiramatsu 	int i, len, ret;
15994235b045SMasami Hiramatsu 
16004235b045SMasami Hiramatsu 	buf = synthesize_perf_probe_point(&pev->point);
16014235b045SMasami Hiramatsu 	if (!buf)
16024235b045SMasami Hiramatsu 		return NULL;
16034235b045SMasami Hiramatsu 
16044235b045SMasami Hiramatsu 	len = strlen(buf);
16054235b045SMasami Hiramatsu 	for (i = 0; i < pev->nargs; i++) {
16064235b045SMasami Hiramatsu 		ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
16074235b045SMasami Hiramatsu 				 pev->args[i].name);
16087ef17aafSMasami Hiramatsu 		if (ret <= 0) {
16094235b045SMasami Hiramatsu 			free(buf);
16104235b045SMasami Hiramatsu 			return NULL;
16117ef17aafSMasami Hiramatsu 		}
16124235b045SMasami Hiramatsu 		len += ret;
16137ef17aafSMasami Hiramatsu 	}
161450656eecSMasami Hiramatsu 
16154235b045SMasami Hiramatsu 	return buf;
16164235b045SMasami Hiramatsu }
16174235b045SMasami Hiramatsu #endif
16184235b045SMasami Hiramatsu 
16190e60836bSSrikar Dronamraju static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
16204235b045SMasami Hiramatsu 					     char **buf, size_t *buflen,
16214235b045SMasami Hiramatsu 					     int depth)
16227ef17aafSMasami Hiramatsu {
16234235b045SMasami Hiramatsu 	int ret;
16244235b045SMasami Hiramatsu 	if (ref->next) {
16250e60836bSSrikar Dronamraju 		depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
16264235b045SMasami Hiramatsu 							 buflen, depth + 1);
16274235b045SMasami Hiramatsu 		if (depth < 0)
16284235b045SMasami Hiramatsu 			goto out;
16294235b045SMasami Hiramatsu 	}
16304235b045SMasami Hiramatsu 
16314235b045SMasami Hiramatsu 	ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
16324235b045SMasami Hiramatsu 	if (ret < 0)
16334235b045SMasami Hiramatsu 		depth = ret;
16344235b045SMasami Hiramatsu 	else {
16354235b045SMasami Hiramatsu 		*buf += ret;
16364235b045SMasami Hiramatsu 		*buflen -= ret;
16374235b045SMasami Hiramatsu 	}
16384235b045SMasami Hiramatsu out:
16394235b045SMasami Hiramatsu 	return depth;
16404235b045SMasami Hiramatsu 
16414235b045SMasami Hiramatsu }
16424235b045SMasami Hiramatsu 
16430e60836bSSrikar Dronamraju static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
16444235b045SMasami Hiramatsu 				       char *buf, size_t buflen)
16454235b045SMasami Hiramatsu {
16460e60836bSSrikar Dronamraju 	struct probe_trace_arg_ref *ref = arg->ref;
16474235b045SMasami Hiramatsu 	int ret, depth = 0;
16484235b045SMasami Hiramatsu 	char *tmp = buf;
16494235b045SMasami Hiramatsu 
16504235b045SMasami Hiramatsu 	/* Argument name or separator */
16514235b045SMasami Hiramatsu 	if (arg->name)
16524235b045SMasami Hiramatsu 		ret = e_snprintf(buf, buflen, " %s=", arg->name);
16534235b045SMasami Hiramatsu 	else
16544235b045SMasami Hiramatsu 		ret = e_snprintf(buf, buflen, " ");
16554235b045SMasami Hiramatsu 	if (ret < 0)
16564235b045SMasami Hiramatsu 		return ret;
16574235b045SMasami Hiramatsu 	buf += ret;
16584235b045SMasami Hiramatsu 	buflen -= ret;
16594235b045SMasami Hiramatsu 
1660b7dcb857SMasami Hiramatsu 	/* Special case: @XXX */
1661b7dcb857SMasami Hiramatsu 	if (arg->value[0] == '@' && arg->ref)
1662b7dcb857SMasami Hiramatsu 			ref = ref->next;
1663b7dcb857SMasami Hiramatsu 
16644235b045SMasami Hiramatsu 	/* Dereferencing arguments */
1665b7dcb857SMasami Hiramatsu 	if (ref) {
16660e60836bSSrikar Dronamraju 		depth = __synthesize_probe_trace_arg_ref(ref, &buf,
16674235b045SMasami Hiramatsu 							  &buflen, 1);
16684235b045SMasami Hiramatsu 		if (depth < 0)
16694235b045SMasami Hiramatsu 			return depth;
16704235b045SMasami Hiramatsu 	}
16714235b045SMasami Hiramatsu 
16724235b045SMasami Hiramatsu 	/* Print argument value */
1673b7dcb857SMasami Hiramatsu 	if (arg->value[0] == '@' && arg->ref)
1674b7dcb857SMasami Hiramatsu 		ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1675b7dcb857SMasami Hiramatsu 				 arg->ref->offset);
1676b7dcb857SMasami Hiramatsu 	else
16774235b045SMasami Hiramatsu 		ret = e_snprintf(buf, buflen, "%s", arg->value);
16784235b045SMasami Hiramatsu 	if (ret < 0)
16794235b045SMasami Hiramatsu 		return ret;
16804235b045SMasami Hiramatsu 	buf += ret;
16814235b045SMasami Hiramatsu 	buflen -= ret;
16824235b045SMasami Hiramatsu 
16834235b045SMasami Hiramatsu 	/* Closing */
16844235b045SMasami Hiramatsu 	while (depth--) {
16854235b045SMasami Hiramatsu 		ret = e_snprintf(buf, buflen, ")");
16864235b045SMasami Hiramatsu 		if (ret < 0)
16874235b045SMasami Hiramatsu 			return ret;
16884235b045SMasami Hiramatsu 		buf += ret;
16894235b045SMasami Hiramatsu 		buflen -= ret;
16904235b045SMasami Hiramatsu 	}
16914984912eSMasami Hiramatsu 	/* Print argument type */
16924984912eSMasami Hiramatsu 	if (arg->type) {
16934984912eSMasami Hiramatsu 		ret = e_snprintf(buf, buflen, ":%s", arg->type);
16944984912eSMasami Hiramatsu 		if (ret <= 0)
16954984912eSMasami Hiramatsu 			return ret;
16964984912eSMasami Hiramatsu 		buf += ret;
16974984912eSMasami Hiramatsu 	}
16984235b045SMasami Hiramatsu 
16994235b045SMasami Hiramatsu 	return buf - tmp;
17004235b045SMasami Hiramatsu }
17014235b045SMasami Hiramatsu 
17020e60836bSSrikar Dronamraju char *synthesize_probe_trace_command(struct probe_trace_event *tev)
17034235b045SMasami Hiramatsu {
17040e60836bSSrikar Dronamraju 	struct probe_trace_point *tp = &tev->point;
17057ef17aafSMasami Hiramatsu 	char *buf;
17067ef17aafSMasami Hiramatsu 	int i, len, ret;
17077ef17aafSMasami Hiramatsu 
1708e334016fSMasami Hiramatsu 	buf = zalloc(MAX_CMDLEN);
1709e334016fSMasami Hiramatsu 	if (buf == NULL)
1710e334016fSMasami Hiramatsu 		return NULL;
1711e334016fSMasami Hiramatsu 
1712eb948e50SMasami Hiramatsu 	len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1713eb948e50SMasami Hiramatsu 			 tev->group, tev->event);
1714eb948e50SMasami Hiramatsu 	if (len <= 0)
1715eb948e50SMasami Hiramatsu 		goto error;
1716eb948e50SMasami Hiramatsu 
1717eb948e50SMasami Hiramatsu 	/* Uprobes must have tp->address and tp->module */
1718eb948e50SMasami Hiramatsu 	if (tev->uprobes && (!tp->address || !tp->module))
1719eb948e50SMasami Hiramatsu 		goto error;
1720eb948e50SMasami Hiramatsu 
1721eb948e50SMasami Hiramatsu 	/* Use the tp->address for uprobes */
1722225466f1SSrikar Dronamraju 	if (tev->uprobes)
1723eb948e50SMasami Hiramatsu 		ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1724eb948e50SMasami Hiramatsu 				 tp->module, tp->address);
1725225466f1SSrikar Dronamraju 	else
1726eb948e50SMasami Hiramatsu 		ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1727190b57fcSMasami Hiramatsu 				 tp->module ?: "", tp->module ? ":" : "",
17284235b045SMasami Hiramatsu 				 tp->symbol, tp->offset);
1729225466f1SSrikar Dronamraju 
1730eb948e50SMasami Hiramatsu 	if (ret <= 0)
17314235b045SMasami Hiramatsu 		goto error;
1732eb948e50SMasami Hiramatsu 	len += ret;
17337ef17aafSMasami Hiramatsu 
17344235b045SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++) {
17350e60836bSSrikar Dronamraju 		ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
17364235b045SMasami Hiramatsu 						  MAX_CMDLEN - len);
17374de189feSMasami Hiramatsu 		if (ret <= 0)
173850656eecSMasami Hiramatsu 			goto error;
173950656eecSMasami Hiramatsu 		len += ret;
174050656eecSMasami Hiramatsu 	}
174150656eecSMasami Hiramatsu 
17424235b045SMasami Hiramatsu 	return buf;
174350656eecSMasami Hiramatsu error:
17444235b045SMasami Hiramatsu 	free(buf);
17454235b045SMasami Hiramatsu 	return NULL;
174650656eecSMasami Hiramatsu }
174750656eecSMasami Hiramatsu 
17485a6f6314SMasami Hiramatsu static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
17495a6f6314SMasami Hiramatsu 					  struct perf_probe_point *pp,
17505a6f6314SMasami Hiramatsu 					  bool is_kprobe)
17515a6f6314SMasami Hiramatsu {
17525a6f6314SMasami Hiramatsu 	struct symbol *sym = NULL;
17535a6f6314SMasami Hiramatsu 	struct map *map;
17545a6f6314SMasami Hiramatsu 	u64 addr;
17555a6f6314SMasami Hiramatsu 	int ret = -ENOENT;
17565a6f6314SMasami Hiramatsu 
17575a6f6314SMasami Hiramatsu 	if (!is_kprobe) {
17585a6f6314SMasami Hiramatsu 		map = dso__new_map(tp->module);
17595a6f6314SMasami Hiramatsu 		if (!map)
17605a6f6314SMasami Hiramatsu 			goto out;
17615a6f6314SMasami Hiramatsu 		addr = tp->address;
17625a6f6314SMasami Hiramatsu 		sym = map__find_symbol(map, addr, NULL);
17635a6f6314SMasami Hiramatsu 	} else {
17645a6f6314SMasami Hiramatsu 		addr = kernel_get_symbol_address_by_name(tp->symbol, true);
17655a6f6314SMasami Hiramatsu 		if (addr) {
17665a6f6314SMasami Hiramatsu 			addr += tp->offset;
17675a6f6314SMasami Hiramatsu 			sym = __find_kernel_function(addr, &map);
17685a6f6314SMasami Hiramatsu 		}
17695a6f6314SMasami Hiramatsu 	}
17705a6f6314SMasami Hiramatsu 	if (!sym)
17715a6f6314SMasami Hiramatsu 		goto out;
17725a6f6314SMasami Hiramatsu 
17735a6f6314SMasami Hiramatsu 	pp->retprobe = tp->retprobe;
17745a6f6314SMasami Hiramatsu 	pp->offset = addr - map->unmap_ip(map, sym->start);
17755a6f6314SMasami Hiramatsu 	pp->function = strdup(sym->name);
17765a6f6314SMasami Hiramatsu 	ret = pp->function ? 0 : -ENOMEM;
17775a6f6314SMasami Hiramatsu 
17785a6f6314SMasami Hiramatsu out:
17795a6f6314SMasami Hiramatsu 	if (map && !is_kprobe) {
17805a6f6314SMasami Hiramatsu 		dso__delete(map->dso);
17815a6f6314SMasami Hiramatsu 		map__delete(map);
17825a6f6314SMasami Hiramatsu 	}
17835a6f6314SMasami Hiramatsu 
17845a6f6314SMasami Hiramatsu 	return ret;
17855a6f6314SMasami Hiramatsu }
17865a6f6314SMasami Hiramatsu 
17875a6f6314SMasami Hiramatsu static int convert_to_perf_probe_point(struct probe_trace_point *tp,
17885a6f6314SMasami Hiramatsu 					struct perf_probe_point *pp,
17895a6f6314SMasami Hiramatsu 					bool is_kprobe)
17905a6f6314SMasami Hiramatsu {
17915a6f6314SMasami Hiramatsu 	char buf[128];
17925a6f6314SMasami Hiramatsu 	int ret;
17935a6f6314SMasami Hiramatsu 
17945a6f6314SMasami Hiramatsu 	ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
17955a6f6314SMasami Hiramatsu 	if (!ret)
17965a6f6314SMasami Hiramatsu 		return 0;
17975a6f6314SMasami Hiramatsu 	ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
17985a6f6314SMasami Hiramatsu 	if (!ret)
17995a6f6314SMasami Hiramatsu 		return 0;
18005a6f6314SMasami Hiramatsu 
18015a6f6314SMasami Hiramatsu 	pr_debug("Failed to find probe point from both of dwarf and map.\n");
18025a6f6314SMasami Hiramatsu 
18035a6f6314SMasami Hiramatsu 	if (tp->symbol) {
18045a6f6314SMasami Hiramatsu 		pp->function = strdup(tp->symbol);
18055a6f6314SMasami Hiramatsu 		pp->offset = tp->offset;
18065a6f6314SMasami Hiramatsu 	} else if (!tp->module && !is_kprobe) {
18075a6f6314SMasami Hiramatsu 		ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
18085a6f6314SMasami Hiramatsu 		if (ret < 0)
18095a6f6314SMasami Hiramatsu 			return ret;
18105a6f6314SMasami Hiramatsu 		pp->function = strdup(buf);
18115a6f6314SMasami Hiramatsu 		pp->offset = 0;
18125a6f6314SMasami Hiramatsu 	}
18135a6f6314SMasami Hiramatsu 	if (pp->function == NULL)
18145a6f6314SMasami Hiramatsu 		return -ENOMEM;
18155a6f6314SMasami Hiramatsu 
18165a6f6314SMasami Hiramatsu 	pp->retprobe = tp->retprobe;
18175a6f6314SMasami Hiramatsu 
18185a6f6314SMasami Hiramatsu 	return 0;
18195a6f6314SMasami Hiramatsu }
18205a6f6314SMasami Hiramatsu 
18210e60836bSSrikar Dronamraju static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1822225466f1SSrikar Dronamraju 			       struct perf_probe_event *pev, bool is_kprobe)
18234de189feSMasami Hiramatsu {
182402b95dadSMasami Hiramatsu 	char buf[64] = "";
1825146a1439SMasami Hiramatsu 	int i, ret;
18264de189feSMasami Hiramatsu 
18274b4da7f7SMasami Hiramatsu 	/* Convert event/group name */
182802b95dadSMasami Hiramatsu 	pev->event = strdup(tev->event);
182902b95dadSMasami Hiramatsu 	pev->group = strdup(tev->group);
183002b95dadSMasami Hiramatsu 	if (pev->event == NULL || pev->group == NULL)
183102b95dadSMasami Hiramatsu 		return -ENOMEM;
1832fb1587d8SMasami Hiramatsu 
18334b4da7f7SMasami Hiramatsu 	/* Convert trace_point to probe_point */
18345a6f6314SMasami Hiramatsu 	ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1835146a1439SMasami Hiramatsu 	if (ret < 0)
1836146a1439SMasami Hiramatsu 		return ret;
18374b4da7f7SMasami Hiramatsu 
18384235b045SMasami Hiramatsu 	/* Convert trace_arg to probe_arg */
18394235b045SMasami Hiramatsu 	pev->nargs = tev->nargs;
1840e334016fSMasami Hiramatsu 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1841e334016fSMasami Hiramatsu 	if (pev->args == NULL)
1842e334016fSMasami Hiramatsu 		return -ENOMEM;
184302b95dadSMasami Hiramatsu 	for (i = 0; i < tev->nargs && ret >= 0; i++) {
18444235b045SMasami Hiramatsu 		if (tev->args[i].name)
184502b95dadSMasami Hiramatsu 			pev->args[i].name = strdup(tev->args[i].name);
18464235b045SMasami Hiramatsu 		else {
18470e60836bSSrikar Dronamraju 			ret = synthesize_probe_trace_arg(&tev->args[i],
1848146a1439SMasami Hiramatsu 							  buf, 64);
184902b95dadSMasami Hiramatsu 			pev->args[i].name = strdup(buf);
185002b95dadSMasami Hiramatsu 		}
185102b95dadSMasami Hiramatsu 		if (pev->args[i].name == NULL && ret >= 0)
185202b95dadSMasami Hiramatsu 			ret = -ENOMEM;
18534de189feSMasami Hiramatsu 	}
1854146a1439SMasami Hiramatsu 
1855146a1439SMasami Hiramatsu 	if (ret < 0)
1856146a1439SMasami Hiramatsu 		clear_perf_probe_event(pev);
1857146a1439SMasami Hiramatsu 
1858146a1439SMasami Hiramatsu 	return ret;
18594235b045SMasami Hiramatsu }
18604de189feSMasami Hiramatsu 
18614235b045SMasami Hiramatsu void clear_perf_probe_event(struct perf_probe_event *pev)
18624235b045SMasami Hiramatsu {
18637df2f329SMasami Hiramatsu 	struct perf_probe_arg_field *field, *next;
18644235b045SMasami Hiramatsu 	int i;
18654de189feSMasami Hiramatsu 
18664235b045SMasami Hiramatsu 	free(pev->event);
18674235b045SMasami Hiramatsu 	free(pev->group);
18687afb3fabSMasami Hiramatsu 	free(pev->target);
18699b118acaSMasami Hiramatsu 	clear_perf_probe_point(&pev->point);
1870f5385650SArnaldo Carvalho de Melo 
18717df2f329SMasami Hiramatsu 	for (i = 0; i < pev->nargs; i++) {
18724235b045SMasami Hiramatsu 		free(pev->args[i].name);
187348481938SMasami Hiramatsu 		free(pev->args[i].var);
187411a1ca35SMasami Hiramatsu 		free(pev->args[i].type);
18757df2f329SMasami Hiramatsu 		field = pev->args[i].field;
18767df2f329SMasami Hiramatsu 		while (field) {
18777df2f329SMasami Hiramatsu 			next = field->next;
187874cf249dSArnaldo Carvalho de Melo 			zfree(&field->name);
18797df2f329SMasami Hiramatsu 			free(field);
18807df2f329SMasami Hiramatsu 			field = next;
18817df2f329SMasami Hiramatsu 		}
18827df2f329SMasami Hiramatsu 	}
18834235b045SMasami Hiramatsu 	free(pev->args);
18844235b045SMasami Hiramatsu 	memset(pev, 0, sizeof(*pev));
18854235b045SMasami Hiramatsu }
18864235b045SMasami Hiramatsu 
18870e60836bSSrikar Dronamraju static void clear_probe_trace_event(struct probe_trace_event *tev)
18884235b045SMasami Hiramatsu {
18890e60836bSSrikar Dronamraju 	struct probe_trace_arg_ref *ref, *next;
18904235b045SMasami Hiramatsu 	int i;
18914235b045SMasami Hiramatsu 
18924235b045SMasami Hiramatsu 	free(tev->event);
18934235b045SMasami Hiramatsu 	free(tev->group);
18944235b045SMasami Hiramatsu 	free(tev->point.symbol);
1895190b57fcSMasami Hiramatsu 	free(tev->point.module);
18964235b045SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++) {
18974235b045SMasami Hiramatsu 		free(tev->args[i].name);
18984235b045SMasami Hiramatsu 		free(tev->args[i].value);
18994984912eSMasami Hiramatsu 		free(tev->args[i].type);
19004235b045SMasami Hiramatsu 		ref = tev->args[i].ref;
19014235b045SMasami Hiramatsu 		while (ref) {
19024235b045SMasami Hiramatsu 			next = ref->next;
19034235b045SMasami Hiramatsu 			free(ref);
19044235b045SMasami Hiramatsu 			ref = next;
19054235b045SMasami Hiramatsu 		}
19064235b045SMasami Hiramatsu 	}
19074235b045SMasami Hiramatsu 	free(tev->args);
19084235b045SMasami Hiramatsu 	memset(tev, 0, sizeof(*tev));
19094de189feSMasami Hiramatsu }
19104de189feSMasami Hiramatsu 
19115e45187cSMasami Hiramatsu static void print_open_warning(int err, bool is_kprobe)
1912225466f1SSrikar Dronamraju {
19135f03cba4SMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE];
1914225466f1SSrikar Dronamraju 
19155e45187cSMasami Hiramatsu 	if (err == -ENOENT) {
1916225466f1SSrikar Dronamraju 		const char *config;
1917225466f1SSrikar Dronamraju 
1918225466f1SSrikar Dronamraju 		if (!is_kprobe)
1919225466f1SSrikar Dronamraju 			config = "CONFIG_UPROBE_EVENTS";
1920225466f1SSrikar Dronamraju 		else
1921225466f1SSrikar Dronamraju 			config = "CONFIG_KPROBE_EVENTS";
1922225466f1SSrikar Dronamraju 
19235e45187cSMasami Hiramatsu 		pr_warning("%cprobe_events file does not exist"
19245e45187cSMasami Hiramatsu 			   " - please rebuild kernel with %s.\n",
19255e45187cSMasami Hiramatsu 			   is_kprobe ? 'k' : 'u', config);
19265e45187cSMasami Hiramatsu 	} else if (err == -ENOTSUP)
192723773ca1SSteven Rostedt (Red Hat) 		pr_warning("Tracefs or debugfs is not mounted.\n");
19285e45187cSMasami Hiramatsu 	else
19295e45187cSMasami Hiramatsu 		pr_warning("Failed to open %cprobe_events: %s\n",
19305e45187cSMasami Hiramatsu 			   is_kprobe ? 'k' : 'u',
19315e45187cSMasami Hiramatsu 			   strerror_r(-err, sbuf, sizeof(sbuf)));
1932225466f1SSrikar Dronamraju }
1933225466f1SSrikar Dronamraju 
1934467ec085SMasami Hiramatsu static void print_both_open_warning(int kerr, int uerr)
1935467ec085SMasami Hiramatsu {
1936467ec085SMasami Hiramatsu 	/* Both kprobes and uprobes are disabled, warn it. */
1937467ec085SMasami Hiramatsu 	if (kerr == -ENOTSUP && uerr == -ENOTSUP)
193823773ca1SSteven Rostedt (Red Hat) 		pr_warning("Tracefs or debugfs is not mounted.\n");
1939467ec085SMasami Hiramatsu 	else if (kerr == -ENOENT && uerr == -ENOENT)
1940467ec085SMasami Hiramatsu 		pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1941467ec085SMasami Hiramatsu 			   "or/and CONFIG_UPROBE_EVENTS.\n");
1942467ec085SMasami Hiramatsu 	else {
19435f03cba4SMasami Hiramatsu 		char sbuf[STRERR_BUFSIZE];
1944467ec085SMasami Hiramatsu 		pr_warning("Failed to open kprobe events: %s.\n",
1945467ec085SMasami Hiramatsu 			   strerror_r(-kerr, sbuf, sizeof(sbuf)));
1946467ec085SMasami Hiramatsu 		pr_warning("Failed to open uprobe events: %s.\n",
1947467ec085SMasami Hiramatsu 			   strerror_r(-uerr, sbuf, sizeof(sbuf)));
1948467ec085SMasami Hiramatsu 	}
1949467ec085SMasami Hiramatsu }
1950467ec085SMasami Hiramatsu 
19515e45187cSMasami Hiramatsu static int open_probe_events(const char *trace_file, bool readwrite)
19524de189feSMasami Hiramatsu {
19534de189feSMasami Hiramatsu 	char buf[PATH_MAX];
19547ca5989dSMasami Hiramatsu 	const char *__debugfs;
195523773ca1SSteven Rostedt (Red Hat) 	const char *tracing_dir = "";
19564de189feSMasami Hiramatsu 	int ret;
19574de189feSMasami Hiramatsu 
195823773ca1SSteven Rostedt (Red Hat) 	__debugfs = tracefs_find_mountpoint();
195923773ca1SSteven Rostedt (Red Hat) 	if (__debugfs == NULL) {
196023773ca1SSteven Rostedt (Red Hat) 		tracing_dir = "tracing/";
196123773ca1SSteven Rostedt (Red Hat) 
19627ca5989dSMasami Hiramatsu 		__debugfs = debugfs_find_mountpoint();
19635e45187cSMasami Hiramatsu 		if (__debugfs == NULL)
19645e45187cSMasami Hiramatsu 			return -ENOTSUP;
196523773ca1SSteven Rostedt (Red Hat) 	}
19667ca5989dSMasami Hiramatsu 
196723773ca1SSteven Rostedt (Red Hat) 	ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
196823773ca1SSteven Rostedt (Red Hat) 			 __debugfs, tracing_dir, trace_file);
1969146a1439SMasami Hiramatsu 	if (ret >= 0) {
19707ca5989dSMasami Hiramatsu 		pr_debug("Opening %s write=%d\n", buf, readwrite);
1971f4d7da49SMasami Hiramatsu 		if (readwrite && !probe_event_dry_run)
1972*b8dc3984SMasami Hiramatsu 			ret = open(buf, O_RDWR | O_APPEND, 0);
1973f4d7da49SMasami Hiramatsu 		else
1974f4d7da49SMasami Hiramatsu 			ret = open(buf, O_RDONLY, 0);
1975f4d7da49SMasami Hiramatsu 
1976225466f1SSrikar Dronamraju 		if (ret < 0)
19775e45187cSMasami Hiramatsu 			ret = -errno;
19784de189feSMasami Hiramatsu 	}
19794de189feSMasami Hiramatsu 	return ret;
19804de189feSMasami Hiramatsu }
19814de189feSMasami Hiramatsu 
1982225466f1SSrikar Dronamraju static int open_kprobe_events(bool readwrite)
1983225466f1SSrikar Dronamraju {
198423773ca1SSteven Rostedt (Red Hat) 	return open_probe_events("kprobe_events", readwrite);
1985225466f1SSrikar Dronamraju }
1986225466f1SSrikar Dronamraju 
1987225466f1SSrikar Dronamraju static int open_uprobe_events(bool readwrite)
1988225466f1SSrikar Dronamraju {
198923773ca1SSteven Rostedt (Red Hat) 	return open_probe_events("uprobe_events", readwrite);
1990225466f1SSrikar Dronamraju }
1991225466f1SSrikar Dronamraju 
1992225466f1SSrikar Dronamraju /* Get raw string list of current kprobe_events  or uprobe_events */
19930e60836bSSrikar Dronamraju static struct strlist *get_probe_trace_command_rawlist(int fd)
19944de189feSMasami Hiramatsu {
19954de189feSMasami Hiramatsu 	int ret, idx;
19964de189feSMasami Hiramatsu 	FILE *fp;
19974de189feSMasami Hiramatsu 	char buf[MAX_CMDLEN];
19984de189feSMasami Hiramatsu 	char *p;
19994de189feSMasami Hiramatsu 	struct strlist *sl;
20004de189feSMasami Hiramatsu 
20014de189feSMasami Hiramatsu 	sl = strlist__new(true, NULL);
20024de189feSMasami Hiramatsu 
20034de189feSMasami Hiramatsu 	fp = fdopen(dup(fd), "r");
20044de189feSMasami Hiramatsu 	while (!feof(fp)) {
20054de189feSMasami Hiramatsu 		p = fgets(buf, MAX_CMDLEN, fp);
20064de189feSMasami Hiramatsu 		if (!p)
20074de189feSMasami Hiramatsu 			break;
20084de189feSMasami Hiramatsu 
20094de189feSMasami Hiramatsu 		idx = strlen(p) - 1;
20104de189feSMasami Hiramatsu 		if (p[idx] == '\n')
20114de189feSMasami Hiramatsu 			p[idx] = '\0';
20124de189feSMasami Hiramatsu 		ret = strlist__add(sl, buf);
2013146a1439SMasami Hiramatsu 		if (ret < 0) {
20146eb08660SMasami Hiramatsu 			pr_debug("strlist__add failed (%d)\n", ret);
2015146a1439SMasami Hiramatsu 			strlist__delete(sl);
2016146a1439SMasami Hiramatsu 			return NULL;
2017146a1439SMasami Hiramatsu 		}
20184de189feSMasami Hiramatsu 	}
20194de189feSMasami Hiramatsu 	fclose(fp);
20204de189feSMasami Hiramatsu 
20214de189feSMasami Hiramatsu 	return sl;
20224de189feSMasami Hiramatsu }
20234de189feSMasami Hiramatsu 
20249aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node {
20259aaf5a5fSMasami Hiramatsu 	struct list_head list;
20269aaf5a5fSMasami Hiramatsu 	unsigned long start;
20279aaf5a5fSMasami Hiramatsu 	unsigned long end;
20289aaf5a5fSMasami Hiramatsu 	char *symbol;
20299aaf5a5fSMasami Hiramatsu };
20309aaf5a5fSMasami Hiramatsu 
20319aaf5a5fSMasami Hiramatsu static void kprobe_blacklist__delete(struct list_head *blacklist)
20329aaf5a5fSMasami Hiramatsu {
20339aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
20349aaf5a5fSMasami Hiramatsu 
20359aaf5a5fSMasami Hiramatsu 	while (!list_empty(blacklist)) {
20369aaf5a5fSMasami Hiramatsu 		node = list_first_entry(blacklist,
20379aaf5a5fSMasami Hiramatsu 					struct kprobe_blacklist_node, list);
20389aaf5a5fSMasami Hiramatsu 		list_del(&node->list);
20399aaf5a5fSMasami Hiramatsu 		free(node->symbol);
20409aaf5a5fSMasami Hiramatsu 		free(node);
20419aaf5a5fSMasami Hiramatsu 	}
20429aaf5a5fSMasami Hiramatsu }
20439aaf5a5fSMasami Hiramatsu 
20449aaf5a5fSMasami Hiramatsu static int kprobe_blacklist__load(struct list_head *blacklist)
20459aaf5a5fSMasami Hiramatsu {
20469aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
20479aaf5a5fSMasami Hiramatsu 	const char *__debugfs = debugfs_find_mountpoint();
20489aaf5a5fSMasami Hiramatsu 	char buf[PATH_MAX], *p;
20499aaf5a5fSMasami Hiramatsu 	FILE *fp;
20509aaf5a5fSMasami Hiramatsu 	int ret;
20519aaf5a5fSMasami Hiramatsu 
20529aaf5a5fSMasami Hiramatsu 	if (__debugfs == NULL)
20539aaf5a5fSMasami Hiramatsu 		return -ENOTSUP;
20549aaf5a5fSMasami Hiramatsu 
20559aaf5a5fSMasami Hiramatsu 	ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
20569aaf5a5fSMasami Hiramatsu 	if (ret < 0)
20579aaf5a5fSMasami Hiramatsu 		return ret;
20589aaf5a5fSMasami Hiramatsu 
20599aaf5a5fSMasami Hiramatsu 	fp = fopen(buf, "r");
20609aaf5a5fSMasami Hiramatsu 	if (!fp)
20619aaf5a5fSMasami Hiramatsu 		return -errno;
20629aaf5a5fSMasami Hiramatsu 
20639aaf5a5fSMasami Hiramatsu 	ret = 0;
20649aaf5a5fSMasami Hiramatsu 	while (fgets(buf, PATH_MAX, fp)) {
20659aaf5a5fSMasami Hiramatsu 		node = zalloc(sizeof(*node));
20669aaf5a5fSMasami Hiramatsu 		if (!node) {
20679aaf5a5fSMasami Hiramatsu 			ret = -ENOMEM;
20689aaf5a5fSMasami Hiramatsu 			break;
20699aaf5a5fSMasami Hiramatsu 		}
20709aaf5a5fSMasami Hiramatsu 		INIT_LIST_HEAD(&node->list);
20719aaf5a5fSMasami Hiramatsu 		list_add_tail(&node->list, blacklist);
20729aaf5a5fSMasami Hiramatsu 		if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
20739aaf5a5fSMasami Hiramatsu 			ret = -EINVAL;
20749aaf5a5fSMasami Hiramatsu 			break;
20759aaf5a5fSMasami Hiramatsu 		}
20769aaf5a5fSMasami Hiramatsu 		p = strchr(buf, '\t');
20779aaf5a5fSMasami Hiramatsu 		if (p) {
20789aaf5a5fSMasami Hiramatsu 			p++;
20799aaf5a5fSMasami Hiramatsu 			if (p[strlen(p) - 1] == '\n')
20809aaf5a5fSMasami Hiramatsu 				p[strlen(p) - 1] = '\0';
20819aaf5a5fSMasami Hiramatsu 		} else
20829aaf5a5fSMasami Hiramatsu 			p = (char *)"unknown";
20839aaf5a5fSMasami Hiramatsu 		node->symbol = strdup(p);
20849aaf5a5fSMasami Hiramatsu 		if (!node->symbol) {
20859aaf5a5fSMasami Hiramatsu 			ret = -ENOMEM;
20869aaf5a5fSMasami Hiramatsu 			break;
20879aaf5a5fSMasami Hiramatsu 		}
20889aaf5a5fSMasami Hiramatsu 		pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
20899aaf5a5fSMasami Hiramatsu 			  node->start, node->end, node->symbol);
20909aaf5a5fSMasami Hiramatsu 		ret++;
20919aaf5a5fSMasami Hiramatsu 	}
20929aaf5a5fSMasami Hiramatsu 	if (ret < 0)
20939aaf5a5fSMasami Hiramatsu 		kprobe_blacklist__delete(blacklist);
20949aaf5a5fSMasami Hiramatsu 	fclose(fp);
20959aaf5a5fSMasami Hiramatsu 
20969aaf5a5fSMasami Hiramatsu 	return ret;
20979aaf5a5fSMasami Hiramatsu }
20989aaf5a5fSMasami Hiramatsu 
20999aaf5a5fSMasami Hiramatsu static struct kprobe_blacklist_node *
21009aaf5a5fSMasami Hiramatsu kprobe_blacklist__find_by_address(struct list_head *blacklist,
21019aaf5a5fSMasami Hiramatsu 				  unsigned long address)
21029aaf5a5fSMasami Hiramatsu {
21039aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
21049aaf5a5fSMasami Hiramatsu 
21059aaf5a5fSMasami Hiramatsu 	list_for_each_entry(node, blacklist, list) {
21069aaf5a5fSMasami Hiramatsu 		if (node->start <= address && address <= node->end)
21079aaf5a5fSMasami Hiramatsu 			return node;
21089aaf5a5fSMasami Hiramatsu 	}
21099aaf5a5fSMasami Hiramatsu 
21109aaf5a5fSMasami Hiramatsu 	return NULL;
21119aaf5a5fSMasami Hiramatsu }
21129aaf5a5fSMasami Hiramatsu 
2113278498d4SMasami Hiramatsu /* Show an event */
2114fb226ccdSMasami Hiramatsu static int show_perf_probe_event(struct perf_probe_event *pev,
2115fb226ccdSMasami Hiramatsu 				 const char *module)
2116278498d4SMasami Hiramatsu {
21177e990a51SMasami Hiramatsu 	int i, ret;
2118278498d4SMasami Hiramatsu 	char buf[128];
21194235b045SMasami Hiramatsu 	char *place;
2120278498d4SMasami Hiramatsu 
21214235b045SMasami Hiramatsu 	/* Synthesize only event probe point */
21224235b045SMasami Hiramatsu 	place = synthesize_perf_probe_point(&pev->point);
2123146a1439SMasami Hiramatsu 	if (!place)
2124146a1439SMasami Hiramatsu 		return -EINVAL;
21254235b045SMasami Hiramatsu 
21264235b045SMasami Hiramatsu 	ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
21277e990a51SMasami Hiramatsu 	if (ret < 0)
2128146a1439SMasami Hiramatsu 		return ret;
2129146a1439SMasami Hiramatsu 
21305e17b28fSMasami Hiramatsu 	pr_info("  %-20s (on %s", buf, place);
2131fb226ccdSMasami Hiramatsu 	if (module)
21325e17b28fSMasami Hiramatsu 		pr_info(" in %s", module);
2133278498d4SMasami Hiramatsu 
21344235b045SMasami Hiramatsu 	if (pev->nargs > 0) {
21355e17b28fSMasami Hiramatsu 		pr_info(" with");
21367df2f329SMasami Hiramatsu 		for (i = 0; i < pev->nargs; i++) {
2137146a1439SMasami Hiramatsu 			ret = synthesize_perf_probe_arg(&pev->args[i],
2138146a1439SMasami Hiramatsu 							buf, 128);
2139146a1439SMasami Hiramatsu 			if (ret < 0)
2140146a1439SMasami Hiramatsu 				break;
21415e17b28fSMasami Hiramatsu 			pr_info(" %s", buf);
21427df2f329SMasami Hiramatsu 		}
2143278498d4SMasami Hiramatsu 	}
21445e17b28fSMasami Hiramatsu 	pr_info(")\n");
21454235b045SMasami Hiramatsu 	free(place);
2146146a1439SMasami Hiramatsu 	return ret;
2147278498d4SMasami Hiramatsu }
2148278498d4SMasami Hiramatsu 
2149b6a89643SMasami Hiramatsu static bool filter_probe_trace_event(struct probe_trace_event *tev,
2150b6a89643SMasami Hiramatsu 				     struct strfilter *filter)
2151b6a89643SMasami Hiramatsu {
2152b6a89643SMasami Hiramatsu 	char tmp[128];
2153b6a89643SMasami Hiramatsu 
2154b6a89643SMasami Hiramatsu 	/* At first, check the event name itself */
2155b6a89643SMasami Hiramatsu 	if (strfilter__compare(filter, tev->event))
2156b6a89643SMasami Hiramatsu 		return true;
2157b6a89643SMasami Hiramatsu 
2158b6a89643SMasami Hiramatsu 	/* Next, check the combination of name and group */
2159b6a89643SMasami Hiramatsu 	if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2160b6a89643SMasami Hiramatsu 		return false;
2161b6a89643SMasami Hiramatsu 	return strfilter__compare(filter, tmp);
2162b6a89643SMasami Hiramatsu }
2163b6a89643SMasami Hiramatsu 
2164b6a89643SMasami Hiramatsu static int __show_perf_probe_events(int fd, bool is_kprobe,
2165b6a89643SMasami Hiramatsu 				    struct strfilter *filter)
21664de189feSMasami Hiramatsu {
2167225466f1SSrikar Dronamraju 	int ret = 0;
21680e60836bSSrikar Dronamraju 	struct probe_trace_event tev;
21694235b045SMasami Hiramatsu 	struct perf_probe_event pev;
21704de189feSMasami Hiramatsu 	struct strlist *rawlist;
21714de189feSMasami Hiramatsu 	struct str_node *ent;
21724de189feSMasami Hiramatsu 
21734235b045SMasami Hiramatsu 	memset(&tev, 0, sizeof(tev));
21744235b045SMasami Hiramatsu 	memset(&pev, 0, sizeof(pev));
217572041334SMasami Hiramatsu 
21760e60836bSSrikar Dronamraju 	rawlist = get_probe_trace_command_rawlist(fd);
2177146a1439SMasami Hiramatsu 	if (!rawlist)
21786eb08660SMasami Hiramatsu 		return -ENOMEM;
21794de189feSMasami Hiramatsu 
2180adf365f4SMasami Hiramatsu 	strlist__for_each(ent, rawlist) {
21810e60836bSSrikar Dronamraju 		ret = parse_probe_trace_command(ent->s, &tev);
2182146a1439SMasami Hiramatsu 		if (ret >= 0) {
2183b6a89643SMasami Hiramatsu 			if (!filter_probe_trace_event(&tev, filter))
2184b6a89643SMasami Hiramatsu 				goto next;
2185225466f1SSrikar Dronamraju 			ret = convert_to_perf_probe_event(&tev, &pev,
2186225466f1SSrikar Dronamraju 								is_kprobe);
2187146a1439SMasami Hiramatsu 			if (ret >= 0)
2188fb226ccdSMasami Hiramatsu 				ret = show_perf_probe_event(&pev,
2189fb226ccdSMasami Hiramatsu 							    tev.point.module);
2190146a1439SMasami Hiramatsu 		}
2191b6a89643SMasami Hiramatsu next:
21924235b045SMasami Hiramatsu 		clear_perf_probe_event(&pev);
21930e60836bSSrikar Dronamraju 		clear_probe_trace_event(&tev);
2194146a1439SMasami Hiramatsu 		if (ret < 0)
2195146a1439SMasami Hiramatsu 			break;
21964de189feSMasami Hiramatsu 	}
21974de189feSMasami Hiramatsu 	strlist__delete(rawlist);
2198146a1439SMasami Hiramatsu 
2199146a1439SMasami Hiramatsu 	return ret;
22004de189feSMasami Hiramatsu }
22014de189feSMasami Hiramatsu 
2202225466f1SSrikar Dronamraju /* List up current perf-probe events */
2203b6a89643SMasami Hiramatsu int show_perf_probe_events(struct strfilter *filter)
2204225466f1SSrikar Dronamraju {
22055e45187cSMasami Hiramatsu 	int kp_fd, up_fd, ret;
2206225466f1SSrikar Dronamraju 
2207225466f1SSrikar Dronamraju 	setup_pager();
2208225466f1SSrikar Dronamraju 
2209ee45b6c2SMasami Hiramatsu 	ret = init_symbol_maps(false);
2210225466f1SSrikar Dronamraju 	if (ret < 0)
2211225466f1SSrikar Dronamraju 		return ret;
2212225466f1SSrikar Dronamraju 
22135e45187cSMasami Hiramatsu 	kp_fd = open_kprobe_events(false);
22145e45187cSMasami Hiramatsu 	if (kp_fd >= 0) {
2215b6a89643SMasami Hiramatsu 		ret = __show_perf_probe_events(kp_fd, true, filter);
22165e45187cSMasami Hiramatsu 		close(kp_fd);
22175e45187cSMasami Hiramatsu 		if (ret < 0)
22185e45187cSMasami Hiramatsu 			goto out;
2219225466f1SSrikar Dronamraju 	}
2220225466f1SSrikar Dronamraju 
22215e45187cSMasami Hiramatsu 	up_fd = open_uprobe_events(false);
22225e45187cSMasami Hiramatsu 	if (kp_fd < 0 && up_fd < 0) {
2223467ec085SMasami Hiramatsu 		print_both_open_warning(kp_fd, up_fd);
22245e45187cSMasami Hiramatsu 		ret = kp_fd;
22255e45187cSMasami Hiramatsu 		goto out;
22265e45187cSMasami Hiramatsu 	}
22275e45187cSMasami Hiramatsu 
22285e45187cSMasami Hiramatsu 	if (up_fd >= 0) {
2229b6a89643SMasami Hiramatsu 		ret = __show_perf_probe_events(up_fd, false, filter);
22305e45187cSMasami Hiramatsu 		close(up_fd);
22315e45187cSMasami Hiramatsu 	}
22325e45187cSMasami Hiramatsu out:
2233ee45b6c2SMasami Hiramatsu 	exit_symbol_maps();
2234225466f1SSrikar Dronamraju 	return ret;
2235225466f1SSrikar Dronamraju }
2236225466f1SSrikar Dronamraju 
2237b498ce1fSMasami Hiramatsu /* Get current perf-probe event names */
22380e60836bSSrikar Dronamraju static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
2239b498ce1fSMasami Hiramatsu {
2240fa28244dSMasami Hiramatsu 	char buf[128];
2241b498ce1fSMasami Hiramatsu 	struct strlist *sl, *rawlist;
2242b498ce1fSMasami Hiramatsu 	struct str_node *ent;
22430e60836bSSrikar Dronamraju 	struct probe_trace_event tev;
2244146a1439SMasami Hiramatsu 	int ret = 0;
2245b498ce1fSMasami Hiramatsu 
22464235b045SMasami Hiramatsu 	memset(&tev, 0, sizeof(tev));
22470e60836bSSrikar Dronamraju 	rawlist = get_probe_trace_command_rawlist(fd);
22486eb08660SMasami Hiramatsu 	if (!rawlist)
22496eb08660SMasami Hiramatsu 		return NULL;
2250e1d2017bSMasami Hiramatsu 	sl = strlist__new(true, NULL);
2251adf365f4SMasami Hiramatsu 	strlist__for_each(ent, rawlist) {
22520e60836bSSrikar Dronamraju 		ret = parse_probe_trace_command(ent->s, &tev);
2253146a1439SMasami Hiramatsu 		if (ret < 0)
2254146a1439SMasami Hiramatsu 			break;
2255fa28244dSMasami Hiramatsu 		if (include_group) {
2256146a1439SMasami Hiramatsu 			ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2257146a1439SMasami Hiramatsu 					tev.event);
2258146a1439SMasami Hiramatsu 			if (ret >= 0)
2259146a1439SMasami Hiramatsu 				ret = strlist__add(sl, buf);
2260fa28244dSMasami Hiramatsu 		} else
2261146a1439SMasami Hiramatsu 			ret = strlist__add(sl, tev.event);
22620e60836bSSrikar Dronamraju 		clear_probe_trace_event(&tev);
2263146a1439SMasami Hiramatsu 		if (ret < 0)
2264146a1439SMasami Hiramatsu 			break;
2265b498ce1fSMasami Hiramatsu 	}
2266b498ce1fSMasami Hiramatsu 	strlist__delete(rawlist);
2267b498ce1fSMasami Hiramatsu 
2268146a1439SMasami Hiramatsu 	if (ret < 0) {
2269146a1439SMasami Hiramatsu 		strlist__delete(sl);
2270146a1439SMasami Hiramatsu 		return NULL;
2271146a1439SMasami Hiramatsu 	}
2272b498ce1fSMasami Hiramatsu 	return sl;
2273b498ce1fSMasami Hiramatsu }
2274b498ce1fSMasami Hiramatsu 
22750e60836bSSrikar Dronamraju static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
227650656eecSMasami Hiramatsu {
22776eca8cc3SFrederic Weisbecker 	int ret = 0;
22780e60836bSSrikar Dronamraju 	char *buf = synthesize_probe_trace_command(tev);
22795f03cba4SMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE];
228050656eecSMasami Hiramatsu 
2281146a1439SMasami Hiramatsu 	if (!buf) {
22820e60836bSSrikar Dronamraju 		pr_debug("Failed to synthesize probe trace event.\n");
2283146a1439SMasami Hiramatsu 		return -EINVAL;
2284146a1439SMasami Hiramatsu 	}
2285146a1439SMasami Hiramatsu 
2286fa28244dSMasami Hiramatsu 	pr_debug("Writing event: %s\n", buf);
2287f4d7da49SMasami Hiramatsu 	if (!probe_event_dry_run) {
228850656eecSMasami Hiramatsu 		ret = write(fd, buf, strlen(buf));
22897949ba1fSNamhyung Kim 		if (ret <= 0) {
22907949ba1fSNamhyung Kim 			ret = -errno;
2291146a1439SMasami Hiramatsu 			pr_warning("Failed to write event: %s\n",
22925f03cba4SMasami Hiramatsu 				   strerror_r(errno, sbuf, sizeof(sbuf)));
229350656eecSMasami Hiramatsu 		}
22947949ba1fSNamhyung Kim 	}
22954235b045SMasami Hiramatsu 	free(buf);
2296146a1439SMasami Hiramatsu 	return ret;
2297f4d7da49SMasami Hiramatsu }
229850656eecSMasami Hiramatsu 
2299146a1439SMasami Hiramatsu static int get_new_event_name(char *buf, size_t len, const char *base,
2300d761b08bSMasami Hiramatsu 			      struct strlist *namelist, bool allow_suffix)
2301b498ce1fSMasami Hiramatsu {
2302b498ce1fSMasami Hiramatsu 	int i, ret;
230317f88fcdSMasami Hiramatsu 
23043099c026SNaveen N. Rao 	if (*base == '.')
23053099c026SNaveen N. Rao 		base++;
23063099c026SNaveen N. Rao 
230717f88fcdSMasami Hiramatsu 	/* Try no suffix */
230817f88fcdSMasami Hiramatsu 	ret = e_snprintf(buf, len, "%s", base);
2309146a1439SMasami Hiramatsu 	if (ret < 0) {
23105f03cba4SMasami Hiramatsu 		pr_debug("snprintf() failed: %d\n", ret);
2311146a1439SMasami Hiramatsu 		return ret;
2312146a1439SMasami Hiramatsu 	}
231317f88fcdSMasami Hiramatsu 	if (!strlist__has_entry(namelist, buf))
2314146a1439SMasami Hiramatsu 		return 0;
231517f88fcdSMasami Hiramatsu 
2316d761b08bSMasami Hiramatsu 	if (!allow_suffix) {
2317d761b08bSMasami Hiramatsu 		pr_warning("Error: event \"%s\" already exists. "
2318d761b08bSMasami Hiramatsu 			   "(Use -f to force duplicates.)\n", base);
2319146a1439SMasami Hiramatsu 		return -EEXIST;
2320d761b08bSMasami Hiramatsu 	}
2321d761b08bSMasami Hiramatsu 
232217f88fcdSMasami Hiramatsu 	/* Try to add suffix */
232317f88fcdSMasami Hiramatsu 	for (i = 1; i < MAX_EVENT_INDEX; i++) {
2324b498ce1fSMasami Hiramatsu 		ret = e_snprintf(buf, len, "%s_%d", base, i);
2325146a1439SMasami Hiramatsu 		if (ret < 0) {
23265f03cba4SMasami Hiramatsu 			pr_debug("snprintf() failed: %d\n", ret);
2327146a1439SMasami Hiramatsu 			return ret;
2328146a1439SMasami Hiramatsu 		}
2329b498ce1fSMasami Hiramatsu 		if (!strlist__has_entry(namelist, buf))
2330b498ce1fSMasami Hiramatsu 			break;
2331b498ce1fSMasami Hiramatsu 	}
2332146a1439SMasami Hiramatsu 	if (i == MAX_EVENT_INDEX) {
2333146a1439SMasami Hiramatsu 		pr_warning("Too many events are on the same function.\n");
2334146a1439SMasami Hiramatsu 		ret = -ERANGE;
2335b498ce1fSMasami Hiramatsu 	}
2336b498ce1fSMasami Hiramatsu 
2337146a1439SMasami Hiramatsu 	return ret;
2338146a1439SMasami Hiramatsu }
2339146a1439SMasami Hiramatsu 
234079702f61SMasami Hiramatsu /* Warn if the current kernel's uprobe implementation is old */
234179702f61SMasami Hiramatsu static void warn_uprobe_event_compat(struct probe_trace_event *tev)
234279702f61SMasami Hiramatsu {
234379702f61SMasami Hiramatsu 	int i;
234479702f61SMasami Hiramatsu 	char *buf = synthesize_probe_trace_command(tev);
234579702f61SMasami Hiramatsu 
234679702f61SMasami Hiramatsu 	/* Old uprobe event doesn't support memory dereference */
234779702f61SMasami Hiramatsu 	if (!tev->uprobes || tev->nargs == 0 || !buf)
234879702f61SMasami Hiramatsu 		goto out;
234979702f61SMasami Hiramatsu 
235079702f61SMasami Hiramatsu 	for (i = 0; i < tev->nargs; i++)
235179702f61SMasami Hiramatsu 		if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
235279702f61SMasami Hiramatsu 			pr_warning("Please upgrade your kernel to at least "
235379702f61SMasami Hiramatsu 				   "3.14 to have access to feature %s\n",
235479702f61SMasami Hiramatsu 				   tev->args[i].value);
235579702f61SMasami Hiramatsu 			break;
235679702f61SMasami Hiramatsu 		}
235779702f61SMasami Hiramatsu out:
235879702f61SMasami Hiramatsu 	free(buf);
235979702f61SMasami Hiramatsu }
236079702f61SMasami Hiramatsu 
23610e60836bSSrikar Dronamraju static int __add_probe_trace_events(struct perf_probe_event *pev,
23620e60836bSSrikar Dronamraju 				     struct probe_trace_event *tevs,
23634235b045SMasami Hiramatsu 				     int ntevs, bool allow_suffix)
236450656eecSMasami Hiramatsu {
2365146a1439SMasami Hiramatsu 	int i, fd, ret;
23660e60836bSSrikar Dronamraju 	struct probe_trace_event *tev = NULL;
23674235b045SMasami Hiramatsu 	char buf[64];
23684235b045SMasami Hiramatsu 	const char *event, *group;
2369b498ce1fSMasami Hiramatsu 	struct strlist *namelist;
23709aaf5a5fSMasami Hiramatsu 	LIST_HEAD(blacklist);
23719aaf5a5fSMasami Hiramatsu 	struct kprobe_blacklist_node *node;
237250656eecSMasami Hiramatsu 
2373225466f1SSrikar Dronamraju 	if (pev->uprobes)
2374225466f1SSrikar Dronamraju 		fd = open_uprobe_events(true);
2375225466f1SSrikar Dronamraju 	else
2376f4d7da49SMasami Hiramatsu 		fd = open_kprobe_events(true);
2377225466f1SSrikar Dronamraju 
23785e45187cSMasami Hiramatsu 	if (fd < 0) {
23795e45187cSMasami Hiramatsu 		print_open_warning(fd, !pev->uprobes);
2380146a1439SMasami Hiramatsu 		return fd;
23815e45187cSMasami Hiramatsu 	}
23825e45187cSMasami Hiramatsu 
2383b498ce1fSMasami Hiramatsu 	/* Get current event names */
23840e60836bSSrikar Dronamraju 	namelist = get_probe_trace_event_names(fd, false);
2385146a1439SMasami Hiramatsu 	if (!namelist) {
2386146a1439SMasami Hiramatsu 		pr_debug("Failed to get current event list.\n");
2387ae2cb1acSMasami Hiramatsu 		ret = -ENOMEM;
2388ae2cb1acSMasami Hiramatsu 		goto close_out;
2389146a1439SMasami Hiramatsu 	}
23909aaf5a5fSMasami Hiramatsu 	/* Get kprobe blacklist if exists */
23919aaf5a5fSMasami Hiramatsu 	if (!pev->uprobes) {
23929aaf5a5fSMasami Hiramatsu 		ret = kprobe_blacklist__load(&blacklist);
23939aaf5a5fSMasami Hiramatsu 		if (ret < 0)
23949aaf5a5fSMasami Hiramatsu 			pr_debug("No kprobe blacklist support, ignored\n");
23959aaf5a5fSMasami Hiramatsu 	}
239650656eecSMasami Hiramatsu 
2397146a1439SMasami Hiramatsu 	ret = 0;
23985e17b28fSMasami Hiramatsu 	pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
239902b95dadSMasami Hiramatsu 	for (i = 0; i < ntevs; i++) {
24004235b045SMasami Hiramatsu 		tev = &tevs[i];
24019aaf5a5fSMasami Hiramatsu 		/* Ensure that the address is NOT blacklisted */
24029aaf5a5fSMasami Hiramatsu 		node = kprobe_blacklist__find_by_address(&blacklist,
24039aaf5a5fSMasami Hiramatsu 							 tev->point.address);
24049aaf5a5fSMasami Hiramatsu 		if (node) {
24059aaf5a5fSMasami Hiramatsu 			pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
24069aaf5a5fSMasami Hiramatsu 			continue;
24079aaf5a5fSMasami Hiramatsu 		}
24089aaf5a5fSMasami Hiramatsu 
24094235b045SMasami Hiramatsu 		if (pev->event)
24104235b045SMasami Hiramatsu 			event = pev->event;
24114235b045SMasami Hiramatsu 		else
24124235b045SMasami Hiramatsu 			if (pev->point.function)
24134235b045SMasami Hiramatsu 				event = pev->point.function;
24144235b045SMasami Hiramatsu 			else
24154235b045SMasami Hiramatsu 				event = tev->point.symbol;
24164235b045SMasami Hiramatsu 		if (pev->group)
24174235b045SMasami Hiramatsu 			group = pev->group;
24184235b045SMasami Hiramatsu 		else
24194235b045SMasami Hiramatsu 			group = PERFPROBE_GROUP;
24204235b045SMasami Hiramatsu 
2421b498ce1fSMasami Hiramatsu 		/* Get an unused new event name */
2422146a1439SMasami Hiramatsu 		ret = get_new_event_name(buf, 64, event,
2423146a1439SMasami Hiramatsu 					 namelist, allow_suffix);
2424146a1439SMasami Hiramatsu 		if (ret < 0)
2425146a1439SMasami Hiramatsu 			break;
24264235b045SMasami Hiramatsu 		event = buf;
24274235b045SMasami Hiramatsu 
242802b95dadSMasami Hiramatsu 		tev->event = strdup(event);
242902b95dadSMasami Hiramatsu 		tev->group = strdup(group);
243002b95dadSMasami Hiramatsu 		if (tev->event == NULL || tev->group == NULL) {
243102b95dadSMasami Hiramatsu 			ret = -ENOMEM;
243202b95dadSMasami Hiramatsu 			break;
243302b95dadSMasami Hiramatsu 		}
24340e60836bSSrikar Dronamraju 		ret = write_probe_trace_event(fd, tev);
2435146a1439SMasami Hiramatsu 		if (ret < 0)
2436146a1439SMasami Hiramatsu 			break;
2437b498ce1fSMasami Hiramatsu 		/* Add added event name to namelist */
2438b498ce1fSMasami Hiramatsu 		strlist__add(namelist, event);
24394235b045SMasami Hiramatsu 
24404235b045SMasami Hiramatsu 		/* Trick here - save current event/group */
24414235b045SMasami Hiramatsu 		event = pev->event;
24424235b045SMasami Hiramatsu 		group = pev->group;
24434235b045SMasami Hiramatsu 		pev->event = tev->event;
24444235b045SMasami Hiramatsu 		pev->group = tev->group;
2445fb226ccdSMasami Hiramatsu 		show_perf_probe_event(pev, tev->point.module);
24464235b045SMasami Hiramatsu 		/* Trick here - restore current event/group */
24474235b045SMasami Hiramatsu 		pev->event = (char *)event;
24484235b045SMasami Hiramatsu 		pev->group = (char *)group;
24494235b045SMasami Hiramatsu 
2450d761b08bSMasami Hiramatsu 		/*
2451d761b08bSMasami Hiramatsu 		 * Probes after the first probe which comes from same
2452d761b08bSMasami Hiramatsu 		 * user input are always allowed to add suffix, because
2453d761b08bSMasami Hiramatsu 		 * there might be several addresses corresponding to
2454d761b08bSMasami Hiramatsu 		 * one code line.
2455d761b08bSMasami Hiramatsu 		 */
2456d761b08bSMasami Hiramatsu 		allow_suffix = true;
245750656eecSMasami Hiramatsu 	}
245879702f61SMasami Hiramatsu 	if (ret == -EINVAL && pev->uprobes)
245979702f61SMasami Hiramatsu 		warn_uprobe_event_compat(tev);
2460146a1439SMasami Hiramatsu 
24619aaf5a5fSMasami Hiramatsu 	/* Note that it is possible to skip all events because of blacklist */
24629aaf5a5fSMasami Hiramatsu 	if (ret >= 0 && tev->event) {
2463a9b495b0SMasami Hiramatsu 		/* Show how to use the event. */
24645e17b28fSMasami Hiramatsu 		pr_info("\nYou can now use it in all perf tools, such as:\n\n");
24655e17b28fSMasami Hiramatsu 		pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2466146a1439SMasami Hiramatsu 			 tev->event);
2467146a1439SMasami Hiramatsu 	}
2468a9b495b0SMasami Hiramatsu 
24699aaf5a5fSMasami Hiramatsu 	kprobe_blacklist__delete(&blacklist);
2470e1d2017bSMasami Hiramatsu 	strlist__delete(namelist);
2471ae2cb1acSMasami Hiramatsu close_out:
247250656eecSMasami Hiramatsu 	close(fd);
2473146a1439SMasami Hiramatsu 	return ret;
247450656eecSMasami Hiramatsu }
2475fa28244dSMasami Hiramatsu 
2476564c62a4SNamhyung Kim static int find_probe_functions(struct map *map, char *name)
2477eb948e50SMasami Hiramatsu {
2478564c62a4SNamhyung Kim 	int found = 0;
24790a3873a8SArnaldo Carvalho de Melo 	struct symbol *sym;
2480564c62a4SNamhyung Kim 
24810a3873a8SArnaldo Carvalho de Melo 	map__for_each_symbol_by_name(map, name, sym) {
2482564c62a4SNamhyung Kim 		found++;
2483eb948e50SMasami Hiramatsu 	}
2484564c62a4SNamhyung Kim 
2485564c62a4SNamhyung Kim 	return found;
2486eb948e50SMasami Hiramatsu }
2487eb948e50SMasami Hiramatsu 
2488eb948e50SMasami Hiramatsu #define strdup_or_goto(str, label)	\
2489eb948e50SMasami Hiramatsu 	({ char *__p = strdup(str); if (!__p) goto label; __p; })
2490eb948e50SMasami Hiramatsu 
24917b6ff0bdSNaveen N. Rao void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
24927b6ff0bdSNaveen N. Rao 				struct probe_trace_event *tev __maybe_unused,
24937b6ff0bdSNaveen N. Rao 				struct map *map __maybe_unused) { }
24947b6ff0bdSNaveen N. Rao 
2495eb948e50SMasami Hiramatsu /*
2496eb948e50SMasami Hiramatsu  * Find probe function addresses from map.
2497eb948e50SMasami Hiramatsu  * Return an error or the number of found probe_trace_event
2498eb948e50SMasami Hiramatsu  */
2499eb948e50SMasami Hiramatsu static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2500eb948e50SMasami Hiramatsu 					    struct probe_trace_event **tevs,
2501eb948e50SMasami Hiramatsu 					    int max_tevs, const char *target)
2502eb948e50SMasami Hiramatsu {
2503eb948e50SMasami Hiramatsu 	struct map *map = NULL;
2504eb948e50SMasami Hiramatsu 	struct ref_reloc_sym *reloc_sym = NULL;
2505eb948e50SMasami Hiramatsu 	struct symbol *sym;
2506eb948e50SMasami Hiramatsu 	struct probe_trace_event *tev;
2507eb948e50SMasami Hiramatsu 	struct perf_probe_point *pp = &pev->point;
2508eb948e50SMasami Hiramatsu 	struct probe_trace_point *tp;
2509564c62a4SNamhyung Kim 	int num_matched_functions;
2510eb948e50SMasami Hiramatsu 	int ret, i;
2511eb948e50SMasami Hiramatsu 
25129b118acaSMasami Hiramatsu 	map = get_target_map(target, pev->uprobes);
2513eb948e50SMasami Hiramatsu 	if (!map) {
2514eb948e50SMasami Hiramatsu 		ret = -EINVAL;
2515eb948e50SMasami Hiramatsu 		goto out;
2516eb948e50SMasami Hiramatsu 	}
2517eb948e50SMasami Hiramatsu 
2518eb948e50SMasami Hiramatsu 	/*
2519eb948e50SMasami Hiramatsu 	 * Load matched symbols: Since the different local symbols may have
2520eb948e50SMasami Hiramatsu 	 * same name but different addresses, this lists all the symbols.
2521eb948e50SMasami Hiramatsu 	 */
2522564c62a4SNamhyung Kim 	num_matched_functions = find_probe_functions(map, pp->function);
2523564c62a4SNamhyung Kim 	if (num_matched_functions == 0) {
2524eb948e50SMasami Hiramatsu 		pr_err("Failed to find symbol %s in %s\n", pp->function,
2525eb948e50SMasami Hiramatsu 			target ? : "kernel");
2526eb948e50SMasami Hiramatsu 		ret = -ENOENT;
2527eb948e50SMasami Hiramatsu 		goto out;
2528eb948e50SMasami Hiramatsu 	} else if (num_matched_functions > max_tevs) {
2529eb948e50SMasami Hiramatsu 		pr_err("Too many functions matched in %s\n",
2530eb948e50SMasami Hiramatsu 			target ? : "kernel");
2531eb948e50SMasami Hiramatsu 		ret = -E2BIG;
2532eb948e50SMasami Hiramatsu 		goto out;
2533eb948e50SMasami Hiramatsu 	}
2534eb948e50SMasami Hiramatsu 
253525dd9171SNamhyung Kim 	if (!pev->uprobes && !pp->retprobe) {
25360560a0c4SHe Kuang 		reloc_sym = kernel_get_ref_reloc_sym();
2537eb948e50SMasami Hiramatsu 		if (!reloc_sym) {
2538eb948e50SMasami Hiramatsu 			pr_warning("Relocated base symbol is not found!\n");
2539eb948e50SMasami Hiramatsu 			ret = -EINVAL;
2540eb948e50SMasami Hiramatsu 			goto out;
2541eb948e50SMasami Hiramatsu 		}
2542eb948e50SMasami Hiramatsu 	}
2543eb948e50SMasami Hiramatsu 
2544eb948e50SMasami Hiramatsu 	/* Setup result trace-probe-events */
2545eb948e50SMasami Hiramatsu 	*tevs = zalloc(sizeof(*tev) * num_matched_functions);
2546eb948e50SMasami Hiramatsu 	if (!*tevs) {
2547eb948e50SMasami Hiramatsu 		ret = -ENOMEM;
2548eb948e50SMasami Hiramatsu 		goto out;
2549eb948e50SMasami Hiramatsu 	}
2550eb948e50SMasami Hiramatsu 
2551eb948e50SMasami Hiramatsu 	ret = 0;
2552564c62a4SNamhyung Kim 
25530a3873a8SArnaldo Carvalho de Melo 	map__for_each_symbol_by_name(map, pp->function, sym) {
2554eb948e50SMasami Hiramatsu 		tev = (*tevs) + ret;
2555eb948e50SMasami Hiramatsu 		tp = &tev->point;
2556eb948e50SMasami Hiramatsu 		if (ret == num_matched_functions) {
2557eb948e50SMasami Hiramatsu 			pr_warning("Too many symbols are listed. Skip it.\n");
2558eb948e50SMasami Hiramatsu 			break;
2559eb948e50SMasami Hiramatsu 		}
2560eb948e50SMasami Hiramatsu 		ret++;
2561eb948e50SMasami Hiramatsu 
2562eb948e50SMasami Hiramatsu 		if (pp->offset > sym->end - sym->start) {
2563eb948e50SMasami Hiramatsu 			pr_warning("Offset %ld is bigger than the size of %s\n",
2564eb948e50SMasami Hiramatsu 				   pp->offset, sym->name);
2565eb948e50SMasami Hiramatsu 			ret = -ENOENT;
2566eb948e50SMasami Hiramatsu 			goto err_out;
2567eb948e50SMasami Hiramatsu 		}
2568eb948e50SMasami Hiramatsu 		/* Add one probe point */
2569eb948e50SMasami Hiramatsu 		tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2570eb948e50SMasami Hiramatsu 		if (reloc_sym) {
2571eb948e50SMasami Hiramatsu 			tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2572eb948e50SMasami Hiramatsu 			tp->offset = tp->address - reloc_sym->addr;
2573eb948e50SMasami Hiramatsu 		} else {
2574eb948e50SMasami Hiramatsu 			tp->symbol = strdup_or_goto(sym->name, nomem_out);
2575eb948e50SMasami Hiramatsu 			tp->offset = pp->offset;
2576eb948e50SMasami Hiramatsu 		}
2577eb948e50SMasami Hiramatsu 		tp->retprobe = pp->retprobe;
2578eb948e50SMasami Hiramatsu 		if (target)
2579eb948e50SMasami Hiramatsu 			tev->point.module = strdup_or_goto(target, nomem_out);
2580eb948e50SMasami Hiramatsu 		tev->uprobes = pev->uprobes;
2581eb948e50SMasami Hiramatsu 		tev->nargs = pev->nargs;
2582eb948e50SMasami Hiramatsu 		if (tev->nargs) {
2583eb948e50SMasami Hiramatsu 			tev->args = zalloc(sizeof(struct probe_trace_arg) *
2584eb948e50SMasami Hiramatsu 					   tev->nargs);
2585eb948e50SMasami Hiramatsu 			if (tev->args == NULL)
2586eb948e50SMasami Hiramatsu 				goto nomem_out;
2587eb948e50SMasami Hiramatsu 		}
2588eb948e50SMasami Hiramatsu 		for (i = 0; i < tev->nargs; i++) {
2589eb948e50SMasami Hiramatsu 			if (pev->args[i].name)
2590eb948e50SMasami Hiramatsu 				tev->args[i].name =
2591eb948e50SMasami Hiramatsu 					strdup_or_goto(pev->args[i].name,
2592eb948e50SMasami Hiramatsu 							nomem_out);
2593eb948e50SMasami Hiramatsu 
2594eb948e50SMasami Hiramatsu 			tev->args[i].value = strdup_or_goto(pev->args[i].var,
2595eb948e50SMasami Hiramatsu 							    nomem_out);
2596eb948e50SMasami Hiramatsu 			if (pev->args[i].type)
2597eb948e50SMasami Hiramatsu 				tev->args[i].type =
2598eb948e50SMasami Hiramatsu 					strdup_or_goto(pev->args[i].type,
2599eb948e50SMasami Hiramatsu 							nomem_out);
2600eb948e50SMasami Hiramatsu 		}
26017b6ff0bdSNaveen N. Rao 		arch__fix_tev_from_maps(pev, tev, map);
2602eb948e50SMasami Hiramatsu 	}
2603eb948e50SMasami Hiramatsu 
2604eb948e50SMasami Hiramatsu out:
26059b118acaSMasami Hiramatsu 	put_target_map(map, pev->uprobes);
2606eb948e50SMasami Hiramatsu 	return ret;
2607eb948e50SMasami Hiramatsu 
2608eb948e50SMasami Hiramatsu nomem_out:
2609eb948e50SMasami Hiramatsu 	ret = -ENOMEM;
2610eb948e50SMasami Hiramatsu err_out:
2611eb948e50SMasami Hiramatsu 	clear_probe_trace_events(*tevs, num_matched_functions);
2612eb948e50SMasami Hiramatsu 	zfree(tevs);
2613eb948e50SMasami Hiramatsu 	goto out;
2614eb948e50SMasami Hiramatsu }
2615eb948e50SMasami Hiramatsu 
2616d5c2e2c1SNaveen N. Rao bool __weak arch__prefers_symtab(void) { return false; }
2617d5c2e2c1SNaveen N. Rao 
26180e60836bSSrikar Dronamraju static int convert_to_probe_trace_events(struct perf_probe_event *pev,
26190e60836bSSrikar Dronamraju 					  struct probe_trace_event **tevs,
26204eced234SSrikar Dronamraju 					  int max_tevs, const char *target)
2621e0faa8d3SMasami Hiramatsu {
2622eb948e50SMasami Hiramatsu 	int ret;
26234235b045SMasami Hiramatsu 
2624fb7345bbSMasami Hiramatsu 	if (pev->uprobes && !pev->group) {
2625fb7345bbSMasami Hiramatsu 		/* Replace group name if not given */
2626fb7345bbSMasami Hiramatsu 		ret = convert_exec_to_group(target, &pev->group);
2627fb7345bbSMasami Hiramatsu 		if (ret != 0) {
2628fb7345bbSMasami Hiramatsu 			pr_warning("Failed to make a group name.\n");
2629fb7345bbSMasami Hiramatsu 			return ret;
2630fb7345bbSMasami Hiramatsu 		}
2631fb7345bbSMasami Hiramatsu 	}
2632fb7345bbSMasami Hiramatsu 
2633d5c2e2c1SNaveen N. Rao 	if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2634d5c2e2c1SNaveen N. Rao 		ret = find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
2635d5c2e2c1SNaveen N. Rao 		if (ret > 0)
2636d5c2e2c1SNaveen N. Rao 			return ret; /* Found in symbol table */
2637d5c2e2c1SNaveen N. Rao 	}
2638d5c2e2c1SNaveen N. Rao 
26394b4da7f7SMasami Hiramatsu 	/* Convert perf_probe_event with debuginfo */
26404eced234SSrikar Dronamraju 	ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
2641e334016fSMasami Hiramatsu 	if (ret != 0)
2642190b57fcSMasami Hiramatsu 		return ret;	/* Found in debuginfo or got an error */
2643e0faa8d3SMasami Hiramatsu 
2644eb948e50SMasami Hiramatsu 	return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
26454235b045SMasami Hiramatsu }
26464235b045SMasami Hiramatsu 
26474235b045SMasami Hiramatsu struct __event_package {
26484235b045SMasami Hiramatsu 	struct perf_probe_event		*pev;
26490e60836bSSrikar Dronamraju 	struct probe_trace_event	*tevs;
26504235b045SMasami Hiramatsu 	int				ntevs;
26514235b045SMasami Hiramatsu };
26524235b045SMasami Hiramatsu 
2653146a1439SMasami Hiramatsu int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
26547afb3fabSMasami Hiramatsu 			  int max_tevs, bool force_add)
26554235b045SMasami Hiramatsu {
2656146a1439SMasami Hiramatsu 	int i, j, ret;
26574235b045SMasami Hiramatsu 	struct __event_package *pkgs;
26584235b045SMasami Hiramatsu 
2659225466f1SSrikar Dronamraju 	ret = 0;
2660e334016fSMasami Hiramatsu 	pkgs = zalloc(sizeof(struct __event_package) * npevs);
2661225466f1SSrikar Dronamraju 
2662e334016fSMasami Hiramatsu 	if (pkgs == NULL)
2663e334016fSMasami Hiramatsu 		return -ENOMEM;
26644235b045SMasami Hiramatsu 
2665ee45b6c2SMasami Hiramatsu 	ret = init_symbol_maps(pevs->uprobes);
2666449e5b24SMasami Hiramatsu 	if (ret < 0) {
2667449e5b24SMasami Hiramatsu 		free(pkgs);
2668146a1439SMasami Hiramatsu 		return ret;
2669449e5b24SMasami Hiramatsu 	}
26704235b045SMasami Hiramatsu 
26714235b045SMasami Hiramatsu 	/* Loop 1: convert all events */
26724235b045SMasami Hiramatsu 	for (i = 0; i < npevs; i++) {
26734235b045SMasami Hiramatsu 		pkgs[i].pev = &pevs[i];
26744235b045SMasami Hiramatsu 		/* Convert with or without debuginfo */
26750e60836bSSrikar Dronamraju 		ret  = convert_to_probe_trace_events(pkgs[i].pev,
2676469b9b88SMasami Hiramatsu 						     &pkgs[i].tevs,
2677469b9b88SMasami Hiramatsu 						     max_tevs,
26787afb3fabSMasami Hiramatsu 						     pkgs[i].pev->target);
2679146a1439SMasami Hiramatsu 		if (ret < 0)
2680146a1439SMasami Hiramatsu 			goto end;
2681146a1439SMasami Hiramatsu 		pkgs[i].ntevs = ret;
26824235b045SMasami Hiramatsu 	}
26834235b045SMasami Hiramatsu 
26844235b045SMasami Hiramatsu 	/* Loop 2: add all events */
26858635bf6eSArnaldo Carvalho de Melo 	for (i = 0; i < npevs; i++) {
26860e60836bSSrikar Dronamraju 		ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
26874235b045SMasami Hiramatsu 						pkgs[i].ntevs, force_add);
2688fbee632dSArnaldo Carvalho de Melo 		if (ret < 0)
2689fbee632dSArnaldo Carvalho de Melo 			break;
2690fbee632dSArnaldo Carvalho de Melo 	}
2691146a1439SMasami Hiramatsu end:
2692449e5b24SMasami Hiramatsu 	/* Loop 3: cleanup and free trace events  */
2693449e5b24SMasami Hiramatsu 	for (i = 0; i < npevs; i++) {
2694146a1439SMasami Hiramatsu 		for (j = 0; j < pkgs[i].ntevs; j++)
26950e60836bSSrikar Dronamraju 			clear_probe_trace_event(&pkgs[i].tevs[j]);
269674cf249dSArnaldo Carvalho de Melo 		zfree(&pkgs[i].tevs);
2697449e5b24SMasami Hiramatsu 	}
2698449e5b24SMasami Hiramatsu 	free(pkgs);
2699ee45b6c2SMasami Hiramatsu 	exit_symbol_maps();
2700146a1439SMasami Hiramatsu 
2701146a1439SMasami Hiramatsu 	return ret;
2702e0faa8d3SMasami Hiramatsu }
2703e0faa8d3SMasami Hiramatsu 
27040e60836bSSrikar Dronamraju static int __del_trace_probe_event(int fd, struct str_node *ent)
2705bbbb521bSMasami Hiramatsu {
2706bbbb521bSMasami Hiramatsu 	char *p;
2707bbbb521bSMasami Hiramatsu 	char buf[128];
27084235b045SMasami Hiramatsu 	int ret;
2709bbbb521bSMasami Hiramatsu 
27100e60836bSSrikar Dronamraju 	/* Convert from perf-probe event to trace-probe event */
2711146a1439SMasami Hiramatsu 	ret = e_snprintf(buf, 128, "-:%s", ent->s);
2712146a1439SMasami Hiramatsu 	if (ret < 0)
2713146a1439SMasami Hiramatsu 		goto error;
2714146a1439SMasami Hiramatsu 
2715bbbb521bSMasami Hiramatsu 	p = strchr(buf + 2, ':');
2716146a1439SMasami Hiramatsu 	if (!p) {
2717146a1439SMasami Hiramatsu 		pr_debug("Internal error: %s should have ':' but not.\n",
2718146a1439SMasami Hiramatsu 			 ent->s);
2719146a1439SMasami Hiramatsu 		ret = -ENOTSUP;
2720146a1439SMasami Hiramatsu 		goto error;
2721146a1439SMasami Hiramatsu 	}
2722bbbb521bSMasami Hiramatsu 	*p = '/';
2723bbbb521bSMasami Hiramatsu 
27244235b045SMasami Hiramatsu 	pr_debug("Writing event: %s\n", buf);
27254235b045SMasami Hiramatsu 	ret = write(fd, buf, strlen(buf));
272644a56040SMasami Hiramatsu 	if (ret < 0) {
272744a56040SMasami Hiramatsu 		ret = -errno;
2728146a1439SMasami Hiramatsu 		goto error;
272944a56040SMasami Hiramatsu 	}
2730146a1439SMasami Hiramatsu 
27315e17b28fSMasami Hiramatsu 	pr_info("Removed event: %s\n", ent->s);
2732146a1439SMasami Hiramatsu 	return 0;
2733146a1439SMasami Hiramatsu error:
27345f03cba4SMasami Hiramatsu 	pr_warning("Failed to delete event: %s\n",
27355f03cba4SMasami Hiramatsu 		   strerror_r(-ret, buf, sizeof(buf)));
2736146a1439SMasami Hiramatsu 	return ret;
2737bbbb521bSMasami Hiramatsu }
2738bbbb521bSMasami Hiramatsu 
2739307a464bSMasami Hiramatsu static int del_trace_probe_events(int fd, struct strfilter *filter,
2740225466f1SSrikar Dronamraju 				  struct strlist *namelist)
2741fa28244dSMasami Hiramatsu {
2742307a464bSMasami Hiramatsu 	struct str_node *ent;
2743307a464bSMasami Hiramatsu 	const char *p;
27446dbe31f7SMasami Hiramatsu 	int ret = -ENOENT;
2745fa28244dSMasami Hiramatsu 
2746307a464bSMasami Hiramatsu 	if (!namelist)
2747307a464bSMasami Hiramatsu 		return -ENOENT;
2748307a464bSMasami Hiramatsu 
2749307a464bSMasami Hiramatsu 	strlist__for_each(ent, namelist) {
2750307a464bSMasami Hiramatsu 		p = strchr(ent->s, ':');
2751307a464bSMasami Hiramatsu 		if ((p && strfilter__compare(filter, p + 1)) ||
2752307a464bSMasami Hiramatsu 		    strfilter__compare(filter, ent->s)) {
27530e60836bSSrikar Dronamraju 			ret = __del_trace_probe_event(fd, ent);
2754146a1439SMasami Hiramatsu 			if (ret < 0)
2755146a1439SMasami Hiramatsu 				break;
2756bbbb521bSMasami Hiramatsu 		}
2757bbbb521bSMasami Hiramatsu 	}
2758146a1439SMasami Hiramatsu 
2759146a1439SMasami Hiramatsu 	return ret;
2760bbbb521bSMasami Hiramatsu }
2761fa28244dSMasami Hiramatsu 
2762307a464bSMasami Hiramatsu int del_perf_probe_events(struct strfilter *filter)
2763fa28244dSMasami Hiramatsu {
2764307a464bSMasami Hiramatsu 	int ret, ret2, ufd = -1, kfd = -1;
2765225466f1SSrikar Dronamraju 	struct strlist *namelist = NULL, *unamelist = NULL;
2766307a464bSMasami Hiramatsu 	char *str = strfilter__string(filter);
2767307a464bSMasami Hiramatsu 
2768307a464bSMasami Hiramatsu 	if (!str)
2769307a464bSMasami Hiramatsu 		return -EINVAL;
2770307a464bSMasami Hiramatsu 
2771307a464bSMasami Hiramatsu 	pr_debug("Delete filter: \'%s\'\n", str);
2772146a1439SMasami Hiramatsu 
2773fa28244dSMasami Hiramatsu 	/* Get current event names */
2774225466f1SSrikar Dronamraju 	kfd = open_kprobe_events(true);
2775467ec085SMasami Hiramatsu 	if (kfd >= 0)
2776225466f1SSrikar Dronamraju 		namelist = get_probe_trace_event_names(kfd, true);
2777225466f1SSrikar Dronamraju 
2778467ec085SMasami Hiramatsu 	ufd = open_uprobe_events(true);
2779467ec085SMasami Hiramatsu 	if (ufd >= 0)
2780225466f1SSrikar Dronamraju 		unamelist = get_probe_trace_event_names(ufd, true);
2781225466f1SSrikar Dronamraju 
2782467ec085SMasami Hiramatsu 	if (kfd < 0 && ufd < 0) {
2783467ec085SMasami Hiramatsu 		print_both_open_warning(kfd, ufd);
2784307a464bSMasami Hiramatsu 		ret = kfd;
2785467ec085SMasami Hiramatsu 		goto error;
2786467ec085SMasami Hiramatsu 	}
2787467ec085SMasami Hiramatsu 
2788307a464bSMasami Hiramatsu 	ret = del_trace_probe_events(kfd, filter, namelist);
2789307a464bSMasami Hiramatsu 	if (ret < 0 && ret != -ENOENT)
2790225466f1SSrikar Dronamraju 		goto error;
2791fa28244dSMasami Hiramatsu 
2792307a464bSMasami Hiramatsu 	ret2 = del_trace_probe_events(ufd, filter, unamelist);
2793307a464bSMasami Hiramatsu 	if (ret2 < 0 && ret2 != -ENOENT)
27946dbe31f7SMasami Hiramatsu 		ret = ret2;
2795307a464bSMasami Hiramatsu 	else if (ret == -ENOENT && ret2 == -ENOENT) {
2796307a464bSMasami Hiramatsu 		pr_debug("\"%s\" does not hit any event.\n", str);
2797307a464bSMasami Hiramatsu 		/* Note that this is silently ignored */
2798307a464bSMasami Hiramatsu 		ret = 0;
2799225466f1SSrikar Dronamraju 	}
2800225466f1SSrikar Dronamraju 
2801225466f1SSrikar Dronamraju error:
2802225466f1SSrikar Dronamraju 	if (kfd >= 0) {
2803fa28244dSMasami Hiramatsu 		strlist__delete(namelist);
2804225466f1SSrikar Dronamraju 		close(kfd);
2805225466f1SSrikar Dronamraju 	}
2806225466f1SSrikar Dronamraju 
2807225466f1SSrikar Dronamraju 	if (ufd >= 0) {
2808225466f1SSrikar Dronamraju 		strlist__delete(unamelist);
2809225466f1SSrikar Dronamraju 		close(ufd);
2810225466f1SSrikar Dronamraju 	}
2811307a464bSMasami Hiramatsu 	free(str);
2812146a1439SMasami Hiramatsu 
2813146a1439SMasami Hiramatsu 	return ret;
2814fa28244dSMasami Hiramatsu }
2815225466f1SSrikar Dronamraju 
28163c42258cSMasami Hiramatsu /* TODO: don't use a global variable for filter ... */
28173c42258cSMasami Hiramatsu static struct strfilter *available_func_filter;
2818fa28244dSMasami Hiramatsu 
2819e80711caSMasami Hiramatsu /*
28203c42258cSMasami Hiramatsu  * If a symbol corresponds to a function with global binding and
28213c42258cSMasami Hiramatsu  * matches filter return 0. For all others return 1.
2822e80711caSMasami Hiramatsu  */
28231d037ca1SIrina Tirdea static int filter_available_functions(struct map *map __maybe_unused,
2824e80711caSMasami Hiramatsu 				      struct symbol *sym)
2825e80711caSMasami Hiramatsu {
2826e578da3bSNamhyung Kim 	if (strfilter__compare(available_func_filter, sym->name))
2827e80711caSMasami Hiramatsu 		return 0;
28283c42258cSMasami Hiramatsu 	return 1;
2829e80711caSMasami Hiramatsu }
2830e80711caSMasami Hiramatsu 
28312df58634SMasami Hiramatsu int show_available_funcs(const char *target, struct strfilter *_filter,
28322df58634SMasami Hiramatsu 					bool user)
2833e80711caSMasami Hiramatsu {
28342df58634SMasami Hiramatsu 	struct map *map;
28352df58634SMasami Hiramatsu 	int ret;
28362df58634SMasami Hiramatsu 
28372df58634SMasami Hiramatsu 	ret = init_symbol_maps(user);
28382df58634SMasami Hiramatsu 	if (ret < 0)
28392df58634SMasami Hiramatsu 		return ret;
28402df58634SMasami Hiramatsu 
28412df58634SMasami Hiramatsu 	/* Get a symbol map */
28422df58634SMasami Hiramatsu 	if (user)
28432df58634SMasami Hiramatsu 		map = dso__new_map(target);
28442df58634SMasami Hiramatsu 	else
28452df58634SMasami Hiramatsu 		map = kernel_get_module_map(target);
28462df58634SMasami Hiramatsu 	if (!map) {
28472df58634SMasami Hiramatsu 		pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2848e80711caSMasami Hiramatsu 		return -EINVAL;
2849e80711caSMasami Hiramatsu 	}
28502df58634SMasami Hiramatsu 
28512df58634SMasami Hiramatsu 	/* Load symbols with given filter */
28522df58634SMasami Hiramatsu 	available_func_filter = _filter;
28532df58634SMasami Hiramatsu 	if (map__load(map, filter_available_functions)) {
28542df58634SMasami Hiramatsu 		pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
28552df58634SMasami Hiramatsu 		goto end;
28562df58634SMasami Hiramatsu 	}
2857e80711caSMasami Hiramatsu 	if (!dso__sorted_by_name(map->dso, map->type))
2858e80711caSMasami Hiramatsu 		dso__sort_by_name(map->dso, map->type);
2859e80711caSMasami Hiramatsu 
28602df58634SMasami Hiramatsu 	/* Show all (filtered) symbols */
28612df58634SMasami Hiramatsu 	setup_pager();
2862e80711caSMasami Hiramatsu 	dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
28632df58634SMasami Hiramatsu end:
28642df58634SMasami Hiramatsu 	if (user) {
2865225466f1SSrikar Dronamraju 		dso__delete(map->dso);
2866225466f1SSrikar Dronamraju 		map__delete(map);
2867225466f1SSrikar Dronamraju 	}
28682df58634SMasami Hiramatsu 	exit_symbol_maps();
2869225466f1SSrikar Dronamraju 
28702df58634SMasami Hiramatsu 	return ret;
2871225466f1SSrikar Dronamraju }
2872225466f1SSrikar Dronamraju 
2873