xref: /openbmc/linux/tools/perf/util/probe-event.c (revision 79f08d9e)
1 /*
2  * probe-event.c : perf-probe definition to probe_events format converter
3  *
4  * Written by Masami Hiramatsu <mhiramat@redhat.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */
21 
22 #include <sys/utsname.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <limits.h>
33 #include <elf.h>
34 
35 #include "util.h"
36 #include "event.h"
37 #include "strlist.h"
38 #include "debug.h"
39 #include "cache.h"
40 #include "color.h"
41 #include "symbol.h"
42 #include "thread.h"
43 #include <lk/debugfs.h>
44 #include "trace-event.h"	/* For __maybe_unused */
45 #include "probe-event.h"
46 #include "probe-finder.h"
47 #include "session.h"
48 
49 #define MAX_CMDLEN 256
50 #define PERFPROBE_GROUP "probe"
51 
52 bool probe_event_dry_run;	/* Dry run flag */
53 
54 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
55 
56 /* If there is no space to write, returns -E2BIG. */
57 static int e_snprintf(char *str, size_t size, const char *format, ...)
58 	__attribute__((format(printf, 3, 4)));
59 
60 static int e_snprintf(char *str, size_t size, const char *format, ...)
61 {
62 	int ret;
63 	va_list ap;
64 	va_start(ap, format);
65 	ret = vsnprintf(str, size, format, ap);
66 	va_end(ap);
67 	if (ret >= (int)size)
68 		ret = -E2BIG;
69 	return ret;
70 }
71 
72 static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
73 static int convert_name_to_addr(struct perf_probe_event *pev,
74 				const char *exec);
75 static struct machine machine;
76 
77 /* Initialize symbol maps and path of vmlinux/modules */
78 static int init_vmlinux(void)
79 {
80 	int ret;
81 
82 	symbol_conf.sort_by_name = true;
83 	if (symbol_conf.vmlinux_name == NULL)
84 		symbol_conf.try_vmlinux_path = true;
85 	else
86 		pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
87 	ret = symbol__init();
88 	if (ret < 0) {
89 		pr_debug("Failed to init symbol map.\n");
90 		goto out;
91 	}
92 
93 	ret = machine__init(&machine, "", HOST_KERNEL_ID);
94 	if (ret < 0)
95 		goto out;
96 
97 	if (machine__create_kernel_maps(&machine) < 0) {
98 		pr_debug("machine__create_kernel_maps() failed.\n");
99 		goto out;
100 	}
101 out:
102 	if (ret < 0)
103 		pr_warning("Failed to init vmlinux path.\n");
104 	return ret;
105 }
106 
107 static struct symbol *__find_kernel_function_by_name(const char *name,
108 						     struct map **mapp)
109 {
110 	return machine__find_kernel_function_by_name(&machine, name, mapp,
111 						     NULL);
112 }
113 
114 static struct map *kernel_get_module_map(const char *module)
115 {
116 	struct rb_node *nd;
117 	struct map_groups *grp = &machine.kmaps;
118 
119 	/* A file path -- this is an offline module */
120 	if (module && strchr(module, '/'))
121 		return machine__new_module(&machine, 0, module);
122 
123 	if (!module)
124 		module = "kernel";
125 
126 	for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
127 		struct map *pos = rb_entry(nd, struct map, rb_node);
128 		if (strncmp(pos->dso->short_name + 1, module,
129 			    pos->dso->short_name_len - 2) == 0) {
130 			return pos;
131 		}
132 	}
133 	return NULL;
134 }
135 
136 static struct dso *kernel_get_module_dso(const char *module)
137 {
138 	struct dso *dso;
139 	struct map *map;
140 	const char *vmlinux_name;
141 
142 	if (module) {
143 		list_for_each_entry(dso, &machine.kernel_dsos, node) {
144 			if (strncmp(dso->short_name + 1, module,
145 				    dso->short_name_len - 2) == 0)
146 				goto found;
147 		}
148 		pr_debug("Failed to find module %s.\n", module);
149 		return NULL;
150 	}
151 
152 	map = machine.vmlinux_maps[MAP__FUNCTION];
153 	dso = map->dso;
154 
155 	vmlinux_name = symbol_conf.vmlinux_name;
156 	if (vmlinux_name) {
157 		if (dso__load_vmlinux(dso, map, vmlinux_name, NULL) <= 0)
158 			return NULL;
159 	} else {
160 		if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
161 			pr_debug("Failed to load kernel map.\n");
162 			return NULL;
163 		}
164 	}
165 found:
166 	return dso;
167 }
168 
169 const char *kernel_get_module_path(const char *module)
170 {
171 	struct dso *dso = kernel_get_module_dso(module);
172 	return (dso) ? dso->long_name : NULL;
173 }
174 
175 static int init_user_exec(void)
176 {
177 	int ret = 0;
178 
179 	symbol_conf.try_vmlinux_path = false;
180 	symbol_conf.sort_by_name = true;
181 	ret = symbol__init();
182 
183 	if (ret < 0)
184 		pr_debug("Failed to init symbol map.\n");
185 
186 	return ret;
187 }
188 
189 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
190 					struct perf_probe_point *pp)
191 {
192 	pp->function = strdup(tp->symbol);
193 
194 	if (pp->function == NULL)
195 		return -ENOMEM;
196 
197 	pp->offset = tp->offset;
198 	pp->retprobe = tp->retprobe;
199 
200 	return 0;
201 }
202 
203 #ifdef HAVE_DWARF_SUPPORT
204 /* Open new debuginfo of given module */
205 static struct debuginfo *open_debuginfo(const char *module)
206 {
207 	const char *path;
208 
209 	/* A file path -- this is an offline module */
210 	if (module && strchr(module, '/'))
211 		path = module;
212 	else {
213 		path = kernel_get_module_path(module);
214 
215 		if (!path) {
216 			pr_err("Failed to find path of %s module.\n",
217 			       module ?: "kernel");
218 			return NULL;
219 		}
220 	}
221 	return debuginfo__new(path);
222 }
223 
224 /*
225  * Convert trace point to probe point with debuginfo
226  * Currently only handles kprobes.
227  */
228 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
229 					struct perf_probe_point *pp)
230 {
231 	struct symbol *sym;
232 	struct map *map;
233 	u64 addr;
234 	int ret = -ENOENT;
235 	struct debuginfo *dinfo;
236 
237 	sym = __find_kernel_function_by_name(tp->symbol, &map);
238 	if (sym) {
239 		addr = map->unmap_ip(map, sym->start + tp->offset);
240 		pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
241 			 tp->offset, addr);
242 
243 		dinfo = debuginfo__new_online_kernel(addr);
244 		if (dinfo) {
245 			ret = debuginfo__find_probe_point(dinfo,
246 						 (unsigned long)addr, pp);
247 			debuginfo__delete(dinfo);
248 		} else {
249 			pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
250 				 addr);
251 			ret = -ENOENT;
252 		}
253 	}
254 	if (ret <= 0) {
255 		pr_debug("Failed to find corresponding probes from "
256 			 "debuginfo. Use kprobe event information.\n");
257 		return convert_to_perf_probe_point(tp, pp);
258 	}
259 	pp->retprobe = tp->retprobe;
260 
261 	return 0;
262 }
263 
264 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
265 					    int ntevs, const char *module)
266 {
267 	int i, ret = 0;
268 	char *tmp;
269 
270 	if (!module)
271 		return 0;
272 
273 	tmp = strrchr(module, '/');
274 	if (tmp) {
275 		/* This is a module path -- get the module name */
276 		module = strdup(tmp + 1);
277 		if (!module)
278 			return -ENOMEM;
279 		tmp = strchr(module, '.');
280 		if (tmp)
281 			*tmp = '\0';
282 		tmp = (char *)module;	/* For free() */
283 	}
284 
285 	for (i = 0; i < ntevs; i++) {
286 		tevs[i].point.module = strdup(module);
287 		if (!tevs[i].point.module) {
288 			ret = -ENOMEM;
289 			break;
290 		}
291 	}
292 
293 	if (tmp)
294 		free(tmp);
295 
296 	return ret;
297 }
298 
299 /* Try to find perf_probe_event with debuginfo */
300 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
301 					  struct probe_trace_event **tevs,
302 					  int max_tevs, const char *target)
303 {
304 	bool need_dwarf = perf_probe_event_need_dwarf(pev);
305 	struct debuginfo *dinfo;
306 	int ntevs, ret = 0;
307 
308 	if (pev->uprobes) {
309 		if (need_dwarf) {
310 			pr_warning("Debuginfo-analysis is not yet supported"
311 					" with -x/--exec option.\n");
312 			return -ENOSYS;
313 		}
314 		return convert_name_to_addr(pev, target);
315 	}
316 
317 	dinfo = open_debuginfo(target);
318 
319 	if (!dinfo) {
320 		if (need_dwarf) {
321 			pr_warning("Failed to open debuginfo file.\n");
322 			return -ENOENT;
323 		}
324 		pr_debug("Could not open debuginfo. Try to use symbols.\n");
325 		return 0;
326 	}
327 
328 	/* Searching trace events corresponding to a probe event */
329 	ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
330 
331 	debuginfo__delete(dinfo);
332 
333 	if (ntevs > 0) {	/* Succeeded to find trace events */
334 		pr_debug("find %d probe_trace_events.\n", ntevs);
335 		if (target)
336 			ret = add_module_to_probe_trace_events(*tevs, ntevs,
337 							       target);
338 		return ret < 0 ? ret : ntevs;
339 	}
340 
341 	if (ntevs == 0)	{	/* No error but failed to find probe point. */
342 		pr_warning("Probe point '%s' not found.\n",
343 			   synthesize_perf_probe_point(&pev->point));
344 		return -ENOENT;
345 	}
346 	/* Error path : ntevs < 0 */
347 	pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
348 	if (ntevs == -EBADF) {
349 		pr_warning("Warning: No dwarf info found in the vmlinux - "
350 			"please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
351 		if (!need_dwarf) {
352 			pr_debug("Trying to use symbols.\n");
353 			return 0;
354 		}
355 	}
356 	return ntevs;
357 }
358 
359 /*
360  * Find a src file from a DWARF tag path. Prepend optional source path prefix
361  * and chop off leading directories that do not exist. Result is passed back as
362  * a newly allocated path on success.
363  * Return 0 if file was found and readable, -errno otherwise.
364  */
365 static int get_real_path(const char *raw_path, const char *comp_dir,
366 			 char **new_path)
367 {
368 	const char *prefix = symbol_conf.source_prefix;
369 
370 	if (!prefix) {
371 		if (raw_path[0] != '/' && comp_dir)
372 			/* If not an absolute path, try to use comp_dir */
373 			prefix = comp_dir;
374 		else {
375 			if (access(raw_path, R_OK) == 0) {
376 				*new_path = strdup(raw_path);
377 				return 0;
378 			} else
379 				return -errno;
380 		}
381 	}
382 
383 	*new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
384 	if (!*new_path)
385 		return -ENOMEM;
386 
387 	for (;;) {
388 		sprintf(*new_path, "%s/%s", prefix, raw_path);
389 
390 		if (access(*new_path, R_OK) == 0)
391 			return 0;
392 
393 		if (!symbol_conf.source_prefix)
394 			/* In case of searching comp_dir, don't retry */
395 			return -errno;
396 
397 		switch (errno) {
398 		case ENAMETOOLONG:
399 		case ENOENT:
400 		case EROFS:
401 		case EFAULT:
402 			raw_path = strchr(++raw_path, '/');
403 			if (!raw_path) {
404 				free(*new_path);
405 				*new_path = NULL;
406 				return -ENOENT;
407 			}
408 			continue;
409 
410 		default:
411 			free(*new_path);
412 			*new_path = NULL;
413 			return -errno;
414 		}
415 	}
416 }
417 
418 #define LINEBUF_SIZE 256
419 #define NR_ADDITIONAL_LINES 2
420 
421 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
422 {
423 	char buf[LINEBUF_SIZE];
424 	const char *color = show_num ? "" : PERF_COLOR_BLUE;
425 	const char *prefix = NULL;
426 
427 	do {
428 		if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
429 			goto error;
430 		if (skip)
431 			continue;
432 		if (!prefix) {
433 			prefix = show_num ? "%7d  " : "         ";
434 			color_fprintf(stdout, color, prefix, l);
435 		}
436 		color_fprintf(stdout, color, "%s", buf);
437 
438 	} while (strchr(buf, '\n') == NULL);
439 
440 	return 1;
441 error:
442 	if (ferror(fp)) {
443 		pr_warning("File read error: %s\n", strerror(errno));
444 		return -1;
445 	}
446 	return 0;
447 }
448 
449 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
450 {
451 	int rv = __show_one_line(fp, l, skip, show_num);
452 	if (rv == 0) {
453 		pr_warning("Source file is shorter than expected.\n");
454 		rv = -1;
455 	}
456 	return rv;
457 }
458 
459 #define show_one_line_with_num(f,l)	_show_one_line(f,l,false,true)
460 #define show_one_line(f,l)		_show_one_line(f,l,false,false)
461 #define skip_one_line(f,l)		_show_one_line(f,l,true,false)
462 #define show_one_line_or_eof(f,l)	__show_one_line(f,l,false,false)
463 
464 /*
465  * Show line-range always requires debuginfo to find source file and
466  * line number.
467  */
468 int show_line_range(struct line_range *lr, const char *module)
469 {
470 	int l = 1;
471 	struct line_node *ln;
472 	struct debuginfo *dinfo;
473 	FILE *fp;
474 	int ret;
475 	char *tmp;
476 
477 	/* Search a line range */
478 	ret = init_vmlinux();
479 	if (ret < 0)
480 		return ret;
481 
482 	dinfo = open_debuginfo(module);
483 	if (!dinfo) {
484 		pr_warning("Failed to open debuginfo file.\n");
485 		return -ENOENT;
486 	}
487 
488 	ret = debuginfo__find_line_range(dinfo, lr);
489 	debuginfo__delete(dinfo);
490 	if (ret == 0) {
491 		pr_warning("Specified source line is not found.\n");
492 		return -ENOENT;
493 	} else if (ret < 0) {
494 		pr_warning("Debuginfo analysis failed. (%d)\n", ret);
495 		return ret;
496 	}
497 
498 	/* Convert source file path */
499 	tmp = lr->path;
500 	ret = get_real_path(tmp, lr->comp_dir, &lr->path);
501 	free(tmp);	/* Free old path */
502 	if (ret < 0) {
503 		pr_warning("Failed to find source file. (%d)\n", ret);
504 		return ret;
505 	}
506 
507 	setup_pager();
508 
509 	if (lr->function)
510 		fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
511 			lr->start - lr->offset);
512 	else
513 		fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
514 
515 	fp = fopen(lr->path, "r");
516 	if (fp == NULL) {
517 		pr_warning("Failed to open %s: %s\n", lr->path,
518 			   strerror(errno));
519 		return -errno;
520 	}
521 	/* Skip to starting line number */
522 	while (l < lr->start) {
523 		ret = skip_one_line(fp, l++);
524 		if (ret < 0)
525 			goto end;
526 	}
527 
528 	list_for_each_entry(ln, &lr->line_list, list) {
529 		for (; ln->line > l; l++) {
530 			ret = show_one_line(fp, l - lr->offset);
531 			if (ret < 0)
532 				goto end;
533 		}
534 		ret = show_one_line_with_num(fp, l++ - lr->offset);
535 		if (ret < 0)
536 			goto end;
537 	}
538 
539 	if (lr->end == INT_MAX)
540 		lr->end = l + NR_ADDITIONAL_LINES;
541 	while (l <= lr->end) {
542 		ret = show_one_line_or_eof(fp, l++ - lr->offset);
543 		if (ret <= 0)
544 			break;
545 	}
546 end:
547 	fclose(fp);
548 	return ret;
549 }
550 
551 static int show_available_vars_at(struct debuginfo *dinfo,
552 				  struct perf_probe_event *pev,
553 				  int max_vls, struct strfilter *_filter,
554 				  bool externs)
555 {
556 	char *buf;
557 	int ret, i, nvars;
558 	struct str_node *node;
559 	struct variable_list *vls = NULL, *vl;
560 	const char *var;
561 
562 	buf = synthesize_perf_probe_point(&pev->point);
563 	if (!buf)
564 		return -EINVAL;
565 	pr_debug("Searching variables at %s\n", buf);
566 
567 	ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
568 						max_vls, externs);
569 	if (ret <= 0) {
570 		pr_err("Failed to find variables at %s (%d)\n", buf, ret);
571 		goto end;
572 	}
573 	/* Some variables are found */
574 	fprintf(stdout, "Available variables at %s\n", buf);
575 	for (i = 0; i < ret; i++) {
576 		vl = &vls[i];
577 		/*
578 		 * A probe point might be converted to
579 		 * several trace points.
580 		 */
581 		fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
582 			vl->point.offset);
583 		free(vl->point.symbol);
584 		nvars = 0;
585 		if (vl->vars) {
586 			strlist__for_each(node, vl->vars) {
587 				var = strchr(node->s, '\t') + 1;
588 				if (strfilter__compare(_filter, var)) {
589 					fprintf(stdout, "\t\t%s\n", node->s);
590 					nvars++;
591 				}
592 			}
593 			strlist__delete(vl->vars);
594 		}
595 		if (nvars == 0)
596 			fprintf(stdout, "\t\t(No matched variables)\n");
597 	}
598 	free(vls);
599 end:
600 	free(buf);
601 	return ret;
602 }
603 
604 /* Show available variables on given probe point */
605 int show_available_vars(struct perf_probe_event *pevs, int npevs,
606 			int max_vls, const char *module,
607 			struct strfilter *_filter, bool externs)
608 {
609 	int i, ret = 0;
610 	struct debuginfo *dinfo;
611 
612 	ret = init_vmlinux();
613 	if (ret < 0)
614 		return ret;
615 
616 	dinfo = open_debuginfo(module);
617 	if (!dinfo) {
618 		pr_warning("Failed to open debuginfo file.\n");
619 		return -ENOENT;
620 	}
621 
622 	setup_pager();
623 
624 	for (i = 0; i < npevs && ret >= 0; i++)
625 		ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
626 					     externs);
627 
628 	debuginfo__delete(dinfo);
629 	return ret;
630 }
631 
632 #else	/* !HAVE_DWARF_SUPPORT */
633 
634 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
635 					struct perf_probe_point *pp)
636 {
637 	struct symbol *sym;
638 
639 	sym = __find_kernel_function_by_name(tp->symbol, NULL);
640 	if (!sym) {
641 		pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
642 		return -ENOENT;
643 	}
644 
645 	return convert_to_perf_probe_point(tp, pp);
646 }
647 
648 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
649 				struct probe_trace_event **tevs __maybe_unused,
650 				int max_tevs __maybe_unused, const char *target)
651 {
652 	if (perf_probe_event_need_dwarf(pev)) {
653 		pr_warning("Debuginfo-analysis is not supported.\n");
654 		return -ENOSYS;
655 	}
656 
657 	if (pev->uprobes)
658 		return convert_name_to_addr(pev, target);
659 
660 	return 0;
661 }
662 
663 int show_line_range(struct line_range *lr __maybe_unused,
664 		    const char *module __maybe_unused)
665 {
666 	pr_warning("Debuginfo-analysis is not supported.\n");
667 	return -ENOSYS;
668 }
669 
670 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
671 			int npevs __maybe_unused, int max_vls __maybe_unused,
672 			const char *module __maybe_unused,
673 			struct strfilter *filter __maybe_unused,
674 			bool externs __maybe_unused)
675 {
676 	pr_warning("Debuginfo-analysis is not supported.\n");
677 	return -ENOSYS;
678 }
679 #endif
680 
681 static int parse_line_num(char **ptr, int *val, const char *what)
682 {
683 	const char *start = *ptr;
684 
685 	errno = 0;
686 	*val = strtol(*ptr, ptr, 0);
687 	if (errno || *ptr == start) {
688 		semantic_error("'%s' is not a valid number.\n", what);
689 		return -EINVAL;
690 	}
691 	return 0;
692 }
693 
694 /*
695  * Stuff 'lr' according to the line range described by 'arg'.
696  * The line range syntax is described by:
697  *
698  *         SRC[:SLN[+NUM|-ELN]]
699  *         FNC[@SRC][:SLN[+NUM|-ELN]]
700  */
701 int parse_line_range_desc(const char *arg, struct line_range *lr)
702 {
703 	char *range, *file, *name = strdup(arg);
704 	int err;
705 
706 	if (!name)
707 		return -ENOMEM;
708 
709 	lr->start = 0;
710 	lr->end = INT_MAX;
711 
712 	range = strchr(name, ':');
713 	if (range) {
714 		*range++ = '\0';
715 
716 		err = parse_line_num(&range, &lr->start, "start line");
717 		if (err)
718 			goto err;
719 
720 		if (*range == '+' || *range == '-') {
721 			const char c = *range++;
722 
723 			err = parse_line_num(&range, &lr->end, "end line");
724 			if (err)
725 				goto err;
726 
727 			if (c == '+') {
728 				lr->end += lr->start;
729 				/*
730 				 * Adjust the number of lines here.
731 				 * If the number of lines == 1, the
732 				 * the end of line should be equal to
733 				 * the start of line.
734 				 */
735 				lr->end--;
736 			}
737 		}
738 
739 		pr_debug("Line range is %d to %d\n", lr->start, lr->end);
740 
741 		err = -EINVAL;
742 		if (lr->start > lr->end) {
743 			semantic_error("Start line must be smaller"
744 				       " than end line.\n");
745 			goto err;
746 		}
747 		if (*range != '\0') {
748 			semantic_error("Tailing with invalid str '%s'.\n", range);
749 			goto err;
750 		}
751 	}
752 
753 	file = strchr(name, '@');
754 	if (file) {
755 		*file = '\0';
756 		lr->file = strdup(++file);
757 		if (lr->file == NULL) {
758 			err = -ENOMEM;
759 			goto err;
760 		}
761 		lr->function = name;
762 	} else if (strchr(name, '.'))
763 		lr->file = name;
764 	else
765 		lr->function = name;
766 
767 	return 0;
768 err:
769 	free(name);
770 	return err;
771 }
772 
773 /* Check the name is good for event/group */
774 static bool check_event_name(const char *name)
775 {
776 	if (!isalpha(*name) && *name != '_')
777 		return false;
778 	while (*++name != '\0') {
779 		if (!isalpha(*name) && !isdigit(*name) && *name != '_')
780 			return false;
781 	}
782 	return true;
783 }
784 
785 /* Parse probepoint definition. */
786 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
787 {
788 	struct perf_probe_point *pp = &pev->point;
789 	char *ptr, *tmp;
790 	char c, nc = 0;
791 	/*
792 	 * <Syntax>
793 	 * perf probe [EVENT=]SRC[:LN|;PTN]
794 	 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
795 	 *
796 	 * TODO:Group name support
797 	 */
798 
799 	ptr = strpbrk(arg, ";=@+%");
800 	if (ptr && *ptr == '=') {	/* Event name */
801 		*ptr = '\0';
802 		tmp = ptr + 1;
803 		if (strchr(arg, ':')) {
804 			semantic_error("Group name is not supported yet.\n");
805 			return -ENOTSUP;
806 		}
807 		if (!check_event_name(arg)) {
808 			semantic_error("%s is bad for event name -it must "
809 				       "follow C symbol-naming rule.\n", arg);
810 			return -EINVAL;
811 		}
812 		pev->event = strdup(arg);
813 		if (pev->event == NULL)
814 			return -ENOMEM;
815 		pev->group = NULL;
816 		arg = tmp;
817 	}
818 
819 	ptr = strpbrk(arg, ";:+@%");
820 	if (ptr) {
821 		nc = *ptr;
822 		*ptr++ = '\0';
823 	}
824 
825 	tmp = strdup(arg);
826 	if (tmp == NULL)
827 		return -ENOMEM;
828 
829 	/* Check arg is function or file and copy it */
830 	if (strchr(tmp, '.'))	/* File */
831 		pp->file = tmp;
832 	else			/* Function */
833 		pp->function = tmp;
834 
835 	/* Parse other options */
836 	while (ptr) {
837 		arg = ptr;
838 		c = nc;
839 		if (c == ';') {	/* Lazy pattern must be the last part */
840 			pp->lazy_line = strdup(arg);
841 			if (pp->lazy_line == NULL)
842 				return -ENOMEM;
843 			break;
844 		}
845 		ptr = strpbrk(arg, ";:+@%");
846 		if (ptr) {
847 			nc = *ptr;
848 			*ptr++ = '\0';
849 		}
850 		switch (c) {
851 		case ':':	/* Line number */
852 			pp->line = strtoul(arg, &tmp, 0);
853 			if (*tmp != '\0') {
854 				semantic_error("There is non-digit char"
855 					       " in line number.\n");
856 				return -EINVAL;
857 			}
858 			break;
859 		case '+':	/* Byte offset from a symbol */
860 			pp->offset = strtoul(arg, &tmp, 0);
861 			if (*tmp != '\0') {
862 				semantic_error("There is non-digit character"
863 						" in offset.\n");
864 				return -EINVAL;
865 			}
866 			break;
867 		case '@':	/* File name */
868 			if (pp->file) {
869 				semantic_error("SRC@SRC is not allowed.\n");
870 				return -EINVAL;
871 			}
872 			pp->file = strdup(arg);
873 			if (pp->file == NULL)
874 				return -ENOMEM;
875 			break;
876 		case '%':	/* Probe places */
877 			if (strcmp(arg, "return") == 0) {
878 				pp->retprobe = 1;
879 			} else {	/* Others not supported yet */
880 				semantic_error("%%%s is not supported.\n", arg);
881 				return -ENOTSUP;
882 			}
883 			break;
884 		default:	/* Buggy case */
885 			pr_err("This program has a bug at %s:%d.\n",
886 				__FILE__, __LINE__);
887 			return -ENOTSUP;
888 			break;
889 		}
890 	}
891 
892 	/* Exclusion check */
893 	if (pp->lazy_line && pp->line) {
894 		semantic_error("Lazy pattern can't be used with"
895 			       " line number.\n");
896 		return -EINVAL;
897 	}
898 
899 	if (pp->lazy_line && pp->offset) {
900 		semantic_error("Lazy pattern can't be used with offset.\n");
901 		return -EINVAL;
902 	}
903 
904 	if (pp->line && pp->offset) {
905 		semantic_error("Offset can't be used with line number.\n");
906 		return -EINVAL;
907 	}
908 
909 	if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
910 		semantic_error("File always requires line number or "
911 			       "lazy pattern.\n");
912 		return -EINVAL;
913 	}
914 
915 	if (pp->offset && !pp->function) {
916 		semantic_error("Offset requires an entry function.\n");
917 		return -EINVAL;
918 	}
919 
920 	if (pp->retprobe && !pp->function) {
921 		semantic_error("Return probe requires an entry function.\n");
922 		return -EINVAL;
923 	}
924 
925 	if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
926 		semantic_error("Offset/Line/Lazy pattern can't be used with "
927 			       "return probe.\n");
928 		return -EINVAL;
929 	}
930 
931 	pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
932 		 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
933 		 pp->lazy_line);
934 	return 0;
935 }
936 
937 /* Parse perf-probe event argument */
938 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
939 {
940 	char *tmp, *goodname;
941 	struct perf_probe_arg_field **fieldp;
942 
943 	pr_debug("parsing arg: %s into ", str);
944 
945 	tmp = strchr(str, '=');
946 	if (tmp) {
947 		arg->name = strndup(str, tmp - str);
948 		if (arg->name == NULL)
949 			return -ENOMEM;
950 		pr_debug("name:%s ", arg->name);
951 		str = tmp + 1;
952 	}
953 
954 	tmp = strchr(str, ':');
955 	if (tmp) {	/* Type setting */
956 		*tmp = '\0';
957 		arg->type = strdup(tmp + 1);
958 		if (arg->type == NULL)
959 			return -ENOMEM;
960 		pr_debug("type:%s ", arg->type);
961 	}
962 
963 	tmp = strpbrk(str, "-.[");
964 	if (!is_c_varname(str) || !tmp) {
965 		/* A variable, register, symbol or special value */
966 		arg->var = strdup(str);
967 		if (arg->var == NULL)
968 			return -ENOMEM;
969 		pr_debug("%s\n", arg->var);
970 		return 0;
971 	}
972 
973 	/* Structure fields or array element */
974 	arg->var = strndup(str, tmp - str);
975 	if (arg->var == NULL)
976 		return -ENOMEM;
977 	goodname = arg->var;
978 	pr_debug("%s, ", arg->var);
979 	fieldp = &arg->field;
980 
981 	do {
982 		*fieldp = zalloc(sizeof(struct perf_probe_arg_field));
983 		if (*fieldp == NULL)
984 			return -ENOMEM;
985 		if (*tmp == '[') {	/* Array */
986 			str = tmp;
987 			(*fieldp)->index = strtol(str + 1, &tmp, 0);
988 			(*fieldp)->ref = true;
989 			if (*tmp != ']' || tmp == str + 1) {
990 				semantic_error("Array index must be a"
991 						" number.\n");
992 				return -EINVAL;
993 			}
994 			tmp++;
995 			if (*tmp == '\0')
996 				tmp = NULL;
997 		} else {		/* Structure */
998 			if (*tmp == '.') {
999 				str = tmp + 1;
1000 				(*fieldp)->ref = false;
1001 			} else if (tmp[1] == '>') {
1002 				str = tmp + 2;
1003 				(*fieldp)->ref = true;
1004 			} else {
1005 				semantic_error("Argument parse error: %s\n",
1006 					       str);
1007 				return -EINVAL;
1008 			}
1009 			tmp = strpbrk(str, "-.[");
1010 		}
1011 		if (tmp) {
1012 			(*fieldp)->name = strndup(str, tmp - str);
1013 			if ((*fieldp)->name == NULL)
1014 				return -ENOMEM;
1015 			if (*str != '[')
1016 				goodname = (*fieldp)->name;
1017 			pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1018 			fieldp = &(*fieldp)->next;
1019 		}
1020 	} while (tmp);
1021 	(*fieldp)->name = strdup(str);
1022 	if ((*fieldp)->name == NULL)
1023 		return -ENOMEM;
1024 	if (*str != '[')
1025 		goodname = (*fieldp)->name;
1026 	pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1027 
1028 	/* If no name is specified, set the last field name (not array index)*/
1029 	if (!arg->name) {
1030 		arg->name = strdup(goodname);
1031 		if (arg->name == NULL)
1032 			return -ENOMEM;
1033 	}
1034 	return 0;
1035 }
1036 
1037 /* Parse perf-probe event command */
1038 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1039 {
1040 	char **argv;
1041 	int argc, i, ret = 0;
1042 
1043 	argv = argv_split(cmd, &argc);
1044 	if (!argv) {
1045 		pr_debug("Failed to split arguments.\n");
1046 		return -ENOMEM;
1047 	}
1048 	if (argc - 1 > MAX_PROBE_ARGS) {
1049 		semantic_error("Too many probe arguments (%d).\n", argc - 1);
1050 		ret = -ERANGE;
1051 		goto out;
1052 	}
1053 	/* Parse probe point */
1054 	ret = parse_perf_probe_point(argv[0], pev);
1055 	if (ret < 0)
1056 		goto out;
1057 
1058 	/* Copy arguments and ensure return probe has no C argument */
1059 	pev->nargs = argc - 1;
1060 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1061 	if (pev->args == NULL) {
1062 		ret = -ENOMEM;
1063 		goto out;
1064 	}
1065 	for (i = 0; i < pev->nargs && ret >= 0; i++) {
1066 		ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1067 		if (ret >= 0 &&
1068 		    is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1069 			semantic_error("You can't specify local variable for"
1070 				       " kretprobe.\n");
1071 			ret = -EINVAL;
1072 		}
1073 	}
1074 out:
1075 	argv_free(argv);
1076 
1077 	return ret;
1078 }
1079 
1080 /* Return true if this perf_probe_event requires debuginfo */
1081 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1082 {
1083 	int i;
1084 
1085 	if (pev->point.file || pev->point.line || pev->point.lazy_line)
1086 		return true;
1087 
1088 	for (i = 0; i < pev->nargs; i++)
1089 		if (is_c_varname(pev->args[i].var))
1090 			return true;
1091 
1092 	return false;
1093 }
1094 
1095 /* Parse probe_events event into struct probe_point */
1096 static int parse_probe_trace_command(const char *cmd,
1097 				     struct probe_trace_event *tev)
1098 {
1099 	struct probe_trace_point *tp = &tev->point;
1100 	char pr;
1101 	char *p;
1102 	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1103 	int ret, i, argc;
1104 	char **argv;
1105 
1106 	pr_debug("Parsing probe_events: %s\n", cmd);
1107 	argv = argv_split(cmd, &argc);
1108 	if (!argv) {
1109 		pr_debug("Failed to split arguments.\n");
1110 		return -ENOMEM;
1111 	}
1112 	if (argc < 2) {
1113 		semantic_error("Too few probe arguments.\n");
1114 		ret = -ERANGE;
1115 		goto out;
1116 	}
1117 
1118 	/* Scan event and group name. */
1119 	argv0_str = strdup(argv[0]);
1120 	if (argv0_str == NULL) {
1121 		ret = -ENOMEM;
1122 		goto out;
1123 	}
1124 	fmt1_str = strtok_r(argv0_str, ":", &fmt);
1125 	fmt2_str = strtok_r(NULL, "/", &fmt);
1126 	fmt3_str = strtok_r(NULL, " \t", &fmt);
1127 	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1128 	    || fmt3_str == NULL) {
1129 		semantic_error("Failed to parse event name: %s\n", argv[0]);
1130 		ret = -EINVAL;
1131 		goto out;
1132 	}
1133 	pr = fmt1_str[0];
1134 	tev->group = strdup(fmt2_str);
1135 	tev->event = strdup(fmt3_str);
1136 	if (tev->group == NULL || tev->event == NULL) {
1137 		ret = -ENOMEM;
1138 		goto out;
1139 	}
1140 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1141 
1142 	tp->retprobe = (pr == 'r');
1143 
1144 	/* Scan module name(if there), function name and offset */
1145 	p = strchr(argv[1], ':');
1146 	if (p) {
1147 		tp->module = strndup(argv[1], p - argv[1]);
1148 		p++;
1149 	} else
1150 		p = argv[1];
1151 	fmt1_str = strtok_r(p, "+", &fmt);
1152 	tp->symbol = strdup(fmt1_str);
1153 	if (tp->symbol == NULL) {
1154 		ret = -ENOMEM;
1155 		goto out;
1156 	}
1157 	fmt2_str = strtok_r(NULL, "", &fmt);
1158 	if (fmt2_str == NULL)
1159 		tp->offset = 0;
1160 	else
1161 		tp->offset = strtoul(fmt2_str, NULL, 10);
1162 
1163 	tev->nargs = argc - 2;
1164 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1165 	if (tev->args == NULL) {
1166 		ret = -ENOMEM;
1167 		goto out;
1168 	}
1169 	for (i = 0; i < tev->nargs; i++) {
1170 		p = strchr(argv[i + 2], '=');
1171 		if (p)	/* We don't need which register is assigned. */
1172 			*p++ = '\0';
1173 		else
1174 			p = argv[i + 2];
1175 		tev->args[i].name = strdup(argv[i + 2]);
1176 		/* TODO: parse regs and offset */
1177 		tev->args[i].value = strdup(p);
1178 		if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1179 			ret = -ENOMEM;
1180 			goto out;
1181 		}
1182 	}
1183 	ret = 0;
1184 out:
1185 	free(argv0_str);
1186 	argv_free(argv);
1187 	return ret;
1188 }
1189 
1190 /* Compose only probe arg */
1191 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1192 {
1193 	struct perf_probe_arg_field *field = pa->field;
1194 	int ret;
1195 	char *tmp = buf;
1196 
1197 	if (pa->name && pa->var)
1198 		ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1199 	else
1200 		ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1201 	if (ret <= 0)
1202 		goto error;
1203 	tmp += ret;
1204 	len -= ret;
1205 
1206 	while (field) {
1207 		if (field->name[0] == '[')
1208 			ret = e_snprintf(tmp, len, "%s", field->name);
1209 		else
1210 			ret = e_snprintf(tmp, len, "%s%s",
1211 					 field->ref ? "->" : ".", field->name);
1212 		if (ret <= 0)
1213 			goto error;
1214 		tmp += ret;
1215 		len -= ret;
1216 		field = field->next;
1217 	}
1218 
1219 	if (pa->type) {
1220 		ret = e_snprintf(tmp, len, ":%s", pa->type);
1221 		if (ret <= 0)
1222 			goto error;
1223 		tmp += ret;
1224 		len -= ret;
1225 	}
1226 
1227 	return tmp - buf;
1228 error:
1229 	pr_debug("Failed to synthesize perf probe argument: %s\n",
1230 		 strerror(-ret));
1231 	return ret;
1232 }
1233 
1234 /* Compose only probe point (not argument) */
1235 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1236 {
1237 	char *buf, *tmp;
1238 	char offs[32] = "", line[32] = "", file[32] = "";
1239 	int ret, len;
1240 
1241 	buf = zalloc(MAX_CMDLEN);
1242 	if (buf == NULL) {
1243 		ret = -ENOMEM;
1244 		goto error;
1245 	}
1246 	if (pp->offset) {
1247 		ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1248 		if (ret <= 0)
1249 			goto error;
1250 	}
1251 	if (pp->line) {
1252 		ret = e_snprintf(line, 32, ":%d", pp->line);
1253 		if (ret <= 0)
1254 			goto error;
1255 	}
1256 	if (pp->file) {
1257 		tmp = pp->file;
1258 		len = strlen(tmp);
1259 		if (len > 30) {
1260 			tmp = strchr(pp->file + len - 30, '/');
1261 			tmp = tmp ? tmp + 1 : pp->file + len - 30;
1262 		}
1263 		ret = e_snprintf(file, 32, "@%s", tmp);
1264 		if (ret <= 0)
1265 			goto error;
1266 	}
1267 
1268 	if (pp->function)
1269 		ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1270 				 offs, pp->retprobe ? "%return" : "", line,
1271 				 file);
1272 	else
1273 		ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1274 	if (ret <= 0)
1275 		goto error;
1276 
1277 	return buf;
1278 error:
1279 	pr_debug("Failed to synthesize perf probe point: %s\n",
1280 		 strerror(-ret));
1281 	if (buf)
1282 		free(buf);
1283 	return NULL;
1284 }
1285 
1286 #if 0
1287 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1288 {
1289 	char *buf;
1290 	int i, len, ret;
1291 
1292 	buf = synthesize_perf_probe_point(&pev->point);
1293 	if (!buf)
1294 		return NULL;
1295 
1296 	len = strlen(buf);
1297 	for (i = 0; i < pev->nargs; i++) {
1298 		ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1299 				 pev->args[i].name);
1300 		if (ret <= 0) {
1301 			free(buf);
1302 			return NULL;
1303 		}
1304 		len += ret;
1305 	}
1306 
1307 	return buf;
1308 }
1309 #endif
1310 
1311 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1312 					     char **buf, size_t *buflen,
1313 					     int depth)
1314 {
1315 	int ret;
1316 	if (ref->next) {
1317 		depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1318 							 buflen, depth + 1);
1319 		if (depth < 0)
1320 			goto out;
1321 	}
1322 
1323 	ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1324 	if (ret < 0)
1325 		depth = ret;
1326 	else {
1327 		*buf += ret;
1328 		*buflen -= ret;
1329 	}
1330 out:
1331 	return depth;
1332 
1333 }
1334 
1335 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1336 				       char *buf, size_t buflen)
1337 {
1338 	struct probe_trace_arg_ref *ref = arg->ref;
1339 	int ret, depth = 0;
1340 	char *tmp = buf;
1341 
1342 	/* Argument name or separator */
1343 	if (arg->name)
1344 		ret = e_snprintf(buf, buflen, " %s=", arg->name);
1345 	else
1346 		ret = e_snprintf(buf, buflen, " ");
1347 	if (ret < 0)
1348 		return ret;
1349 	buf += ret;
1350 	buflen -= ret;
1351 
1352 	/* Special case: @XXX */
1353 	if (arg->value[0] == '@' && arg->ref)
1354 			ref = ref->next;
1355 
1356 	/* Dereferencing arguments */
1357 	if (ref) {
1358 		depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1359 							  &buflen, 1);
1360 		if (depth < 0)
1361 			return depth;
1362 	}
1363 
1364 	/* Print argument value */
1365 	if (arg->value[0] == '@' && arg->ref)
1366 		ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1367 				 arg->ref->offset);
1368 	else
1369 		ret = e_snprintf(buf, buflen, "%s", arg->value);
1370 	if (ret < 0)
1371 		return ret;
1372 	buf += ret;
1373 	buflen -= ret;
1374 
1375 	/* Closing */
1376 	while (depth--) {
1377 		ret = e_snprintf(buf, buflen, ")");
1378 		if (ret < 0)
1379 			return ret;
1380 		buf += ret;
1381 		buflen -= ret;
1382 	}
1383 	/* Print argument type */
1384 	if (arg->type) {
1385 		ret = e_snprintf(buf, buflen, ":%s", arg->type);
1386 		if (ret <= 0)
1387 			return ret;
1388 		buf += ret;
1389 	}
1390 
1391 	return buf - tmp;
1392 }
1393 
1394 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1395 {
1396 	struct probe_trace_point *tp = &tev->point;
1397 	char *buf;
1398 	int i, len, ret;
1399 
1400 	buf = zalloc(MAX_CMDLEN);
1401 	if (buf == NULL)
1402 		return NULL;
1403 
1404 	if (tev->uprobes)
1405 		len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s:%s",
1406 				 tp->retprobe ? 'r' : 'p',
1407 				 tev->group, tev->event,
1408 				 tp->module, tp->symbol);
1409 	else
1410 		len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
1411 				 tp->retprobe ? 'r' : 'p',
1412 				 tev->group, tev->event,
1413 				 tp->module ?: "", tp->module ? ":" : "",
1414 				 tp->symbol, tp->offset);
1415 
1416 	if (len <= 0)
1417 		goto error;
1418 
1419 	for (i = 0; i < tev->nargs; i++) {
1420 		ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1421 						  MAX_CMDLEN - len);
1422 		if (ret <= 0)
1423 			goto error;
1424 		len += ret;
1425 	}
1426 
1427 	return buf;
1428 error:
1429 	free(buf);
1430 	return NULL;
1431 }
1432 
1433 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1434 			       struct perf_probe_event *pev, bool is_kprobe)
1435 {
1436 	char buf[64] = "";
1437 	int i, ret;
1438 
1439 	/* Convert event/group name */
1440 	pev->event = strdup(tev->event);
1441 	pev->group = strdup(tev->group);
1442 	if (pev->event == NULL || pev->group == NULL)
1443 		return -ENOMEM;
1444 
1445 	/* Convert trace_point to probe_point */
1446 	if (is_kprobe)
1447 		ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1448 	else
1449 		ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1450 
1451 	if (ret < 0)
1452 		return ret;
1453 
1454 	/* Convert trace_arg to probe_arg */
1455 	pev->nargs = tev->nargs;
1456 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1457 	if (pev->args == NULL)
1458 		return -ENOMEM;
1459 	for (i = 0; i < tev->nargs && ret >= 0; i++) {
1460 		if (tev->args[i].name)
1461 			pev->args[i].name = strdup(tev->args[i].name);
1462 		else {
1463 			ret = synthesize_probe_trace_arg(&tev->args[i],
1464 							  buf, 64);
1465 			pev->args[i].name = strdup(buf);
1466 		}
1467 		if (pev->args[i].name == NULL && ret >= 0)
1468 			ret = -ENOMEM;
1469 	}
1470 
1471 	if (ret < 0)
1472 		clear_perf_probe_event(pev);
1473 
1474 	return ret;
1475 }
1476 
1477 void clear_perf_probe_event(struct perf_probe_event *pev)
1478 {
1479 	struct perf_probe_point *pp = &pev->point;
1480 	struct perf_probe_arg_field *field, *next;
1481 	int i;
1482 
1483 	if (pev->event)
1484 		free(pev->event);
1485 	if (pev->group)
1486 		free(pev->group);
1487 	if (pp->file)
1488 		free(pp->file);
1489 	if (pp->function)
1490 		free(pp->function);
1491 	if (pp->lazy_line)
1492 		free(pp->lazy_line);
1493 	for (i = 0; i < pev->nargs; i++) {
1494 		if (pev->args[i].name)
1495 			free(pev->args[i].name);
1496 		if (pev->args[i].var)
1497 			free(pev->args[i].var);
1498 		if (pev->args[i].type)
1499 			free(pev->args[i].type);
1500 		field = pev->args[i].field;
1501 		while (field) {
1502 			next = field->next;
1503 			if (field->name)
1504 				free(field->name);
1505 			free(field);
1506 			field = next;
1507 		}
1508 	}
1509 	if (pev->args)
1510 		free(pev->args);
1511 	memset(pev, 0, sizeof(*pev));
1512 }
1513 
1514 static void clear_probe_trace_event(struct probe_trace_event *tev)
1515 {
1516 	struct probe_trace_arg_ref *ref, *next;
1517 	int i;
1518 
1519 	if (tev->event)
1520 		free(tev->event);
1521 	if (tev->group)
1522 		free(tev->group);
1523 	if (tev->point.symbol)
1524 		free(tev->point.symbol);
1525 	if (tev->point.module)
1526 		free(tev->point.module);
1527 	for (i = 0; i < tev->nargs; i++) {
1528 		if (tev->args[i].name)
1529 			free(tev->args[i].name);
1530 		if (tev->args[i].value)
1531 			free(tev->args[i].value);
1532 		if (tev->args[i].type)
1533 			free(tev->args[i].type);
1534 		ref = tev->args[i].ref;
1535 		while (ref) {
1536 			next = ref->next;
1537 			free(ref);
1538 			ref = next;
1539 		}
1540 	}
1541 	if (tev->args)
1542 		free(tev->args);
1543 	memset(tev, 0, sizeof(*tev));
1544 }
1545 
1546 static void print_warn_msg(const char *file, bool is_kprobe)
1547 {
1548 
1549 	if (errno == ENOENT) {
1550 		const char *config;
1551 
1552 		if (!is_kprobe)
1553 			config = "CONFIG_UPROBE_EVENTS";
1554 		else
1555 			config = "CONFIG_KPROBE_EVENTS";
1556 
1557 		pr_warning("%s file does not exist - please rebuild kernel"
1558 				" with %s.\n", file, config);
1559 	} else
1560 		pr_warning("Failed to open %s file: %s\n", file,
1561 				strerror(errno));
1562 }
1563 
1564 static int open_probe_events(const char *trace_file, bool readwrite,
1565 				bool is_kprobe)
1566 {
1567 	char buf[PATH_MAX];
1568 	const char *__debugfs;
1569 	int ret;
1570 
1571 	__debugfs = debugfs_find_mountpoint();
1572 	if (__debugfs == NULL) {
1573 		pr_warning("Debugfs is not mounted.\n");
1574 		return -ENOENT;
1575 	}
1576 
1577 	ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
1578 	if (ret >= 0) {
1579 		pr_debug("Opening %s write=%d\n", buf, readwrite);
1580 		if (readwrite && !probe_event_dry_run)
1581 			ret = open(buf, O_RDWR, O_APPEND);
1582 		else
1583 			ret = open(buf, O_RDONLY, 0);
1584 
1585 		if (ret < 0)
1586 			print_warn_msg(buf, is_kprobe);
1587 	}
1588 	return ret;
1589 }
1590 
1591 static int open_kprobe_events(bool readwrite)
1592 {
1593 	return open_probe_events("tracing/kprobe_events", readwrite, true);
1594 }
1595 
1596 static int open_uprobe_events(bool readwrite)
1597 {
1598 	return open_probe_events("tracing/uprobe_events", readwrite, false);
1599 }
1600 
1601 /* Get raw string list of current kprobe_events  or uprobe_events */
1602 static struct strlist *get_probe_trace_command_rawlist(int fd)
1603 {
1604 	int ret, idx;
1605 	FILE *fp;
1606 	char buf[MAX_CMDLEN];
1607 	char *p;
1608 	struct strlist *sl;
1609 
1610 	sl = strlist__new(true, NULL);
1611 
1612 	fp = fdopen(dup(fd), "r");
1613 	while (!feof(fp)) {
1614 		p = fgets(buf, MAX_CMDLEN, fp);
1615 		if (!p)
1616 			break;
1617 
1618 		idx = strlen(p) - 1;
1619 		if (p[idx] == '\n')
1620 			p[idx] = '\0';
1621 		ret = strlist__add(sl, buf);
1622 		if (ret < 0) {
1623 			pr_debug("strlist__add failed: %s\n", strerror(-ret));
1624 			strlist__delete(sl);
1625 			return NULL;
1626 		}
1627 	}
1628 	fclose(fp);
1629 
1630 	return sl;
1631 }
1632 
1633 /* Show an event */
1634 static int show_perf_probe_event(struct perf_probe_event *pev)
1635 {
1636 	int i, ret;
1637 	char buf[128];
1638 	char *place;
1639 
1640 	/* Synthesize only event probe point */
1641 	place = synthesize_perf_probe_point(&pev->point);
1642 	if (!place)
1643 		return -EINVAL;
1644 
1645 	ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
1646 	if (ret < 0)
1647 		return ret;
1648 
1649 	printf("  %-20s (on %s", buf, place);
1650 
1651 	if (pev->nargs > 0) {
1652 		printf(" with");
1653 		for (i = 0; i < pev->nargs; i++) {
1654 			ret = synthesize_perf_probe_arg(&pev->args[i],
1655 							buf, 128);
1656 			if (ret < 0)
1657 				break;
1658 			printf(" %s", buf);
1659 		}
1660 	}
1661 	printf(")\n");
1662 	free(place);
1663 	return ret;
1664 }
1665 
1666 static int __show_perf_probe_events(int fd, bool is_kprobe)
1667 {
1668 	int ret = 0;
1669 	struct probe_trace_event tev;
1670 	struct perf_probe_event pev;
1671 	struct strlist *rawlist;
1672 	struct str_node *ent;
1673 
1674 	memset(&tev, 0, sizeof(tev));
1675 	memset(&pev, 0, sizeof(pev));
1676 
1677 	rawlist = get_probe_trace_command_rawlist(fd);
1678 	if (!rawlist)
1679 		return -ENOENT;
1680 
1681 	strlist__for_each(ent, rawlist) {
1682 		ret = parse_probe_trace_command(ent->s, &tev);
1683 		if (ret >= 0) {
1684 			ret = convert_to_perf_probe_event(&tev, &pev,
1685 								is_kprobe);
1686 			if (ret >= 0)
1687 				ret = show_perf_probe_event(&pev);
1688 		}
1689 		clear_perf_probe_event(&pev);
1690 		clear_probe_trace_event(&tev);
1691 		if (ret < 0)
1692 			break;
1693 	}
1694 	strlist__delete(rawlist);
1695 
1696 	return ret;
1697 }
1698 
1699 /* List up current perf-probe events */
1700 int show_perf_probe_events(void)
1701 {
1702 	int fd, ret;
1703 
1704 	setup_pager();
1705 	fd = open_kprobe_events(false);
1706 
1707 	if (fd < 0)
1708 		return fd;
1709 
1710 	ret = init_vmlinux();
1711 	if (ret < 0)
1712 		return ret;
1713 
1714 	ret = __show_perf_probe_events(fd, true);
1715 	close(fd);
1716 
1717 	fd = open_uprobe_events(false);
1718 	if (fd >= 0) {
1719 		ret = __show_perf_probe_events(fd, false);
1720 		close(fd);
1721 	}
1722 
1723 	return ret;
1724 }
1725 
1726 /* Get current perf-probe event names */
1727 static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
1728 {
1729 	char buf[128];
1730 	struct strlist *sl, *rawlist;
1731 	struct str_node *ent;
1732 	struct probe_trace_event tev;
1733 	int ret = 0;
1734 
1735 	memset(&tev, 0, sizeof(tev));
1736 	rawlist = get_probe_trace_command_rawlist(fd);
1737 	sl = strlist__new(true, NULL);
1738 	strlist__for_each(ent, rawlist) {
1739 		ret = parse_probe_trace_command(ent->s, &tev);
1740 		if (ret < 0)
1741 			break;
1742 		if (include_group) {
1743 			ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1744 					tev.event);
1745 			if (ret >= 0)
1746 				ret = strlist__add(sl, buf);
1747 		} else
1748 			ret = strlist__add(sl, tev.event);
1749 		clear_probe_trace_event(&tev);
1750 		if (ret < 0)
1751 			break;
1752 	}
1753 	strlist__delete(rawlist);
1754 
1755 	if (ret < 0) {
1756 		strlist__delete(sl);
1757 		return NULL;
1758 	}
1759 	return sl;
1760 }
1761 
1762 static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
1763 {
1764 	int ret = 0;
1765 	char *buf = synthesize_probe_trace_command(tev);
1766 
1767 	if (!buf) {
1768 		pr_debug("Failed to synthesize probe trace event.\n");
1769 		return -EINVAL;
1770 	}
1771 
1772 	pr_debug("Writing event: %s\n", buf);
1773 	if (!probe_event_dry_run) {
1774 		ret = write(fd, buf, strlen(buf));
1775 		if (ret <= 0)
1776 			pr_warning("Failed to write event: %s\n",
1777 				   strerror(errno));
1778 	}
1779 	free(buf);
1780 	return ret;
1781 }
1782 
1783 static int get_new_event_name(char *buf, size_t len, const char *base,
1784 			      struct strlist *namelist, bool allow_suffix)
1785 {
1786 	int i, ret;
1787 
1788 	/* Try no suffix */
1789 	ret = e_snprintf(buf, len, "%s", base);
1790 	if (ret < 0) {
1791 		pr_debug("snprintf() failed: %s\n", strerror(-ret));
1792 		return ret;
1793 	}
1794 	if (!strlist__has_entry(namelist, buf))
1795 		return 0;
1796 
1797 	if (!allow_suffix) {
1798 		pr_warning("Error: event \"%s\" already exists. "
1799 			   "(Use -f to force duplicates.)\n", base);
1800 		return -EEXIST;
1801 	}
1802 
1803 	/* Try to add suffix */
1804 	for (i = 1; i < MAX_EVENT_INDEX; i++) {
1805 		ret = e_snprintf(buf, len, "%s_%d", base, i);
1806 		if (ret < 0) {
1807 			pr_debug("snprintf() failed: %s\n", strerror(-ret));
1808 			return ret;
1809 		}
1810 		if (!strlist__has_entry(namelist, buf))
1811 			break;
1812 	}
1813 	if (i == MAX_EVENT_INDEX) {
1814 		pr_warning("Too many events are on the same function.\n");
1815 		ret = -ERANGE;
1816 	}
1817 
1818 	return ret;
1819 }
1820 
1821 static int __add_probe_trace_events(struct perf_probe_event *pev,
1822 				     struct probe_trace_event *tevs,
1823 				     int ntevs, bool allow_suffix)
1824 {
1825 	int i, fd, ret;
1826 	struct probe_trace_event *tev = NULL;
1827 	char buf[64];
1828 	const char *event, *group;
1829 	struct strlist *namelist;
1830 
1831 	if (pev->uprobes)
1832 		fd = open_uprobe_events(true);
1833 	else
1834 		fd = open_kprobe_events(true);
1835 
1836 	if (fd < 0)
1837 		return fd;
1838 	/* Get current event names */
1839 	namelist = get_probe_trace_event_names(fd, false);
1840 	if (!namelist) {
1841 		pr_debug("Failed to get current event list.\n");
1842 		return -EIO;
1843 	}
1844 
1845 	ret = 0;
1846 	printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
1847 	for (i = 0; i < ntevs; i++) {
1848 		tev = &tevs[i];
1849 		if (pev->event)
1850 			event = pev->event;
1851 		else
1852 			if (pev->point.function)
1853 				event = pev->point.function;
1854 			else
1855 				event = tev->point.symbol;
1856 		if (pev->group)
1857 			group = pev->group;
1858 		else
1859 			group = PERFPROBE_GROUP;
1860 
1861 		/* Get an unused new event name */
1862 		ret = get_new_event_name(buf, 64, event,
1863 					 namelist, allow_suffix);
1864 		if (ret < 0)
1865 			break;
1866 		event = buf;
1867 
1868 		tev->event = strdup(event);
1869 		tev->group = strdup(group);
1870 		if (tev->event == NULL || tev->group == NULL) {
1871 			ret = -ENOMEM;
1872 			break;
1873 		}
1874 		ret = write_probe_trace_event(fd, tev);
1875 		if (ret < 0)
1876 			break;
1877 		/* Add added event name to namelist */
1878 		strlist__add(namelist, event);
1879 
1880 		/* Trick here - save current event/group */
1881 		event = pev->event;
1882 		group = pev->group;
1883 		pev->event = tev->event;
1884 		pev->group = tev->group;
1885 		show_perf_probe_event(pev);
1886 		/* Trick here - restore current event/group */
1887 		pev->event = (char *)event;
1888 		pev->group = (char *)group;
1889 
1890 		/*
1891 		 * Probes after the first probe which comes from same
1892 		 * user input are always allowed to add suffix, because
1893 		 * there might be several addresses corresponding to
1894 		 * one code line.
1895 		 */
1896 		allow_suffix = true;
1897 	}
1898 
1899 	if (ret >= 0) {
1900 		/* Show how to use the event. */
1901 		printf("\nYou can now use it in all perf tools, such as:\n\n");
1902 		printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1903 			 tev->event);
1904 	}
1905 
1906 	strlist__delete(namelist);
1907 	close(fd);
1908 	return ret;
1909 }
1910 
1911 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1912 					  struct probe_trace_event **tevs,
1913 					  int max_tevs, const char *target)
1914 {
1915 	struct symbol *sym;
1916 	int ret = 0, i;
1917 	struct probe_trace_event *tev;
1918 
1919 	/* Convert perf_probe_event with debuginfo */
1920 	ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
1921 	if (ret != 0)
1922 		return ret;	/* Found in debuginfo or got an error */
1923 
1924 	/* Allocate trace event buffer */
1925 	tev = *tevs = zalloc(sizeof(struct probe_trace_event));
1926 	if (tev == NULL)
1927 		return -ENOMEM;
1928 
1929 	/* Copy parameters */
1930 	tev->point.symbol = strdup(pev->point.function);
1931 	if (tev->point.symbol == NULL) {
1932 		ret = -ENOMEM;
1933 		goto error;
1934 	}
1935 
1936 	if (target) {
1937 		tev->point.module = strdup(target);
1938 		if (tev->point.module == NULL) {
1939 			ret = -ENOMEM;
1940 			goto error;
1941 		}
1942 	}
1943 
1944 	tev->point.offset = pev->point.offset;
1945 	tev->point.retprobe = pev->point.retprobe;
1946 	tev->nargs = pev->nargs;
1947 	tev->uprobes = pev->uprobes;
1948 
1949 	if (tev->nargs) {
1950 		tev->args = zalloc(sizeof(struct probe_trace_arg)
1951 				   * tev->nargs);
1952 		if (tev->args == NULL) {
1953 			ret = -ENOMEM;
1954 			goto error;
1955 		}
1956 		for (i = 0; i < tev->nargs; i++) {
1957 			if (pev->args[i].name) {
1958 				tev->args[i].name = strdup(pev->args[i].name);
1959 				if (tev->args[i].name == NULL) {
1960 					ret = -ENOMEM;
1961 					goto error;
1962 				}
1963 			}
1964 			tev->args[i].value = strdup(pev->args[i].var);
1965 			if (tev->args[i].value == NULL) {
1966 				ret = -ENOMEM;
1967 				goto error;
1968 			}
1969 			if (pev->args[i].type) {
1970 				tev->args[i].type = strdup(pev->args[i].type);
1971 				if (tev->args[i].type == NULL) {
1972 					ret = -ENOMEM;
1973 					goto error;
1974 				}
1975 			}
1976 		}
1977 	}
1978 
1979 	if (pev->uprobes)
1980 		return 1;
1981 
1982 	/* Currently just checking function name from symbol map */
1983 	sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
1984 	if (!sym) {
1985 		pr_warning("Kernel symbol \'%s\' not found.\n",
1986 			   tev->point.symbol);
1987 		ret = -ENOENT;
1988 		goto error;
1989 	} else if (tev->point.offset > sym->end - sym->start) {
1990 		pr_warning("Offset specified is greater than size of %s\n",
1991 			   tev->point.symbol);
1992 		ret = -ENOENT;
1993 		goto error;
1994 
1995 	}
1996 
1997 	return 1;
1998 error:
1999 	clear_probe_trace_event(tev);
2000 	free(tev);
2001 	*tevs = NULL;
2002 	return ret;
2003 }
2004 
2005 struct __event_package {
2006 	struct perf_probe_event		*pev;
2007 	struct probe_trace_event	*tevs;
2008 	int				ntevs;
2009 };
2010 
2011 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
2012 			  int max_tevs, const char *target, bool force_add)
2013 {
2014 	int i, j, ret;
2015 	struct __event_package *pkgs;
2016 
2017 	ret = 0;
2018 	pkgs = zalloc(sizeof(struct __event_package) * npevs);
2019 
2020 	if (pkgs == NULL)
2021 		return -ENOMEM;
2022 
2023 	if (!pevs->uprobes)
2024 		/* Init vmlinux path */
2025 		ret = init_vmlinux();
2026 	else
2027 		ret = init_user_exec();
2028 
2029 	if (ret < 0) {
2030 		free(pkgs);
2031 		return ret;
2032 	}
2033 
2034 	/* Loop 1: convert all events */
2035 	for (i = 0; i < npevs; i++) {
2036 		pkgs[i].pev = &pevs[i];
2037 		/* Convert with or without debuginfo */
2038 		ret  = convert_to_probe_trace_events(pkgs[i].pev,
2039 						     &pkgs[i].tevs,
2040 						     max_tevs,
2041 						     target);
2042 		if (ret < 0)
2043 			goto end;
2044 		pkgs[i].ntevs = ret;
2045 	}
2046 
2047 	/* Loop 2: add all events */
2048 	for (i = 0; i < npevs; i++) {
2049 		ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
2050 						pkgs[i].ntevs, force_add);
2051 		if (ret < 0)
2052 			break;
2053 	}
2054 end:
2055 	/* Loop 3: cleanup and free trace events  */
2056 	for (i = 0; i < npevs; i++) {
2057 		for (j = 0; j < pkgs[i].ntevs; j++)
2058 			clear_probe_trace_event(&pkgs[i].tevs[j]);
2059 		free(pkgs[i].tevs);
2060 	}
2061 	free(pkgs);
2062 
2063 	return ret;
2064 }
2065 
2066 static int __del_trace_probe_event(int fd, struct str_node *ent)
2067 {
2068 	char *p;
2069 	char buf[128];
2070 	int ret;
2071 
2072 	/* Convert from perf-probe event to trace-probe event */
2073 	ret = e_snprintf(buf, 128, "-:%s", ent->s);
2074 	if (ret < 0)
2075 		goto error;
2076 
2077 	p = strchr(buf + 2, ':');
2078 	if (!p) {
2079 		pr_debug("Internal error: %s should have ':' but not.\n",
2080 			 ent->s);
2081 		ret = -ENOTSUP;
2082 		goto error;
2083 	}
2084 	*p = '/';
2085 
2086 	pr_debug("Writing event: %s\n", buf);
2087 	ret = write(fd, buf, strlen(buf));
2088 	if (ret < 0) {
2089 		ret = -errno;
2090 		goto error;
2091 	}
2092 
2093 	printf("Removed event: %s\n", ent->s);
2094 	return 0;
2095 error:
2096 	pr_warning("Failed to delete event: %s\n", strerror(-ret));
2097 	return ret;
2098 }
2099 
2100 static int del_trace_probe_event(int fd, const char *buf,
2101 						  struct strlist *namelist)
2102 {
2103 	struct str_node *ent, *n;
2104 	int ret = -1;
2105 
2106 	if (strpbrk(buf, "*?")) { /* Glob-exp */
2107 		strlist__for_each_safe(ent, n, namelist)
2108 			if (strglobmatch(ent->s, buf)) {
2109 				ret = __del_trace_probe_event(fd, ent);
2110 				if (ret < 0)
2111 					break;
2112 				strlist__remove(namelist, ent);
2113 			}
2114 	} else {
2115 		ent = strlist__find(namelist, buf);
2116 		if (ent) {
2117 			ret = __del_trace_probe_event(fd, ent);
2118 			if (ret >= 0)
2119 				strlist__remove(namelist, ent);
2120 		}
2121 	}
2122 
2123 	return ret;
2124 }
2125 
2126 int del_perf_probe_events(struct strlist *dellist)
2127 {
2128 	int ret = -1, ufd = -1, kfd = -1;
2129 	char buf[128];
2130 	const char *group, *event;
2131 	char *p, *str;
2132 	struct str_node *ent;
2133 	struct strlist *namelist = NULL, *unamelist = NULL;
2134 
2135 	/* Get current event names */
2136 	kfd = open_kprobe_events(true);
2137 	if (kfd < 0)
2138 		return kfd;
2139 
2140 	namelist = get_probe_trace_event_names(kfd, true);
2141 	ufd = open_uprobe_events(true);
2142 
2143 	if (ufd >= 0)
2144 		unamelist = get_probe_trace_event_names(ufd, true);
2145 
2146 	if (namelist == NULL && unamelist == NULL)
2147 		goto error;
2148 
2149 	strlist__for_each(ent, dellist) {
2150 		str = strdup(ent->s);
2151 		if (str == NULL) {
2152 			ret = -ENOMEM;
2153 			goto error;
2154 		}
2155 		pr_debug("Parsing: %s\n", str);
2156 		p = strchr(str, ':');
2157 		if (p) {
2158 			group = str;
2159 			*p = '\0';
2160 			event = p + 1;
2161 		} else {
2162 			group = "*";
2163 			event = str;
2164 		}
2165 
2166 		ret = e_snprintf(buf, 128, "%s:%s", group, event);
2167 		if (ret < 0) {
2168 			pr_err("Failed to copy event.");
2169 			free(str);
2170 			goto error;
2171 		}
2172 
2173 		pr_debug("Group: %s, Event: %s\n", group, event);
2174 
2175 		if (namelist)
2176 			ret = del_trace_probe_event(kfd, buf, namelist);
2177 
2178 		if (unamelist && ret != 0)
2179 			ret = del_trace_probe_event(ufd, buf, unamelist);
2180 
2181 		if (ret != 0)
2182 			pr_info("Info: Event \"%s\" does not exist.\n", buf);
2183 
2184 		free(str);
2185 	}
2186 
2187 error:
2188 	if (kfd >= 0) {
2189 		strlist__delete(namelist);
2190 		close(kfd);
2191 	}
2192 
2193 	if (ufd >= 0) {
2194 		strlist__delete(unamelist);
2195 		close(ufd);
2196 	}
2197 
2198 	return ret;
2199 }
2200 
2201 /* TODO: don't use a global variable for filter ... */
2202 static struct strfilter *available_func_filter;
2203 
2204 /*
2205  * If a symbol corresponds to a function with global binding and
2206  * matches filter return 0. For all others return 1.
2207  */
2208 static int filter_available_functions(struct map *map __maybe_unused,
2209 				      struct symbol *sym)
2210 {
2211 	if (sym->binding == STB_GLOBAL &&
2212 	    strfilter__compare(available_func_filter, sym->name))
2213 		return 0;
2214 	return 1;
2215 }
2216 
2217 static int __show_available_funcs(struct map *map)
2218 {
2219 	if (map__load(map, filter_available_functions)) {
2220 		pr_err("Failed to load map.\n");
2221 		return -EINVAL;
2222 	}
2223 	if (!dso__sorted_by_name(map->dso, map->type))
2224 		dso__sort_by_name(map->dso, map->type);
2225 
2226 	dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2227 	return 0;
2228 }
2229 
2230 static int available_kernel_funcs(const char *module)
2231 {
2232 	struct map *map;
2233 	int ret;
2234 
2235 	ret = init_vmlinux();
2236 	if (ret < 0)
2237 		return ret;
2238 
2239 	map = kernel_get_module_map(module);
2240 	if (!map) {
2241 		pr_err("Failed to find %s map.\n", (module) ? : "kernel");
2242 		return -EINVAL;
2243 	}
2244 	return __show_available_funcs(map);
2245 }
2246 
2247 static int available_user_funcs(const char *target)
2248 {
2249 	struct map *map;
2250 	int ret;
2251 
2252 	ret = init_user_exec();
2253 	if (ret < 0)
2254 		return ret;
2255 
2256 	map = dso__new_map(target);
2257 	ret = __show_available_funcs(map);
2258 	dso__delete(map->dso);
2259 	map__delete(map);
2260 	return ret;
2261 }
2262 
2263 int show_available_funcs(const char *target, struct strfilter *_filter,
2264 					bool user)
2265 {
2266 	setup_pager();
2267 	available_func_filter = _filter;
2268 
2269 	if (!user)
2270 		return available_kernel_funcs(target);
2271 
2272 	return available_user_funcs(target);
2273 }
2274 
2275 /*
2276  * uprobe_events only accepts address:
2277  * Convert function and any offset to address
2278  */
2279 static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
2280 {
2281 	struct perf_probe_point *pp = &pev->point;
2282 	struct symbol *sym;
2283 	struct map *map = NULL;
2284 	char *function = NULL, *name = NULL;
2285 	int ret = -EINVAL;
2286 	unsigned long long vaddr = 0;
2287 
2288 	if (!pp->function) {
2289 		pr_warning("No function specified for uprobes");
2290 		goto out;
2291 	}
2292 
2293 	function = strdup(pp->function);
2294 	if (!function) {
2295 		pr_warning("Failed to allocate memory by strdup.\n");
2296 		ret = -ENOMEM;
2297 		goto out;
2298 	}
2299 
2300 	name = realpath(exec, NULL);
2301 	if (!name) {
2302 		pr_warning("Cannot find realpath for %s.\n", exec);
2303 		goto out;
2304 	}
2305 	map = dso__new_map(name);
2306 	if (!map) {
2307 		pr_warning("Cannot find appropriate DSO for %s.\n", exec);
2308 		goto out;
2309 	}
2310 	available_func_filter = strfilter__new(function, NULL);
2311 	if (map__load(map, filter_available_functions)) {
2312 		pr_err("Failed to load map.\n");
2313 		goto out;
2314 	}
2315 
2316 	sym = map__find_symbol_by_name(map, function, NULL);
2317 	if (!sym) {
2318 		pr_warning("Cannot find %s in DSO %s\n", function, exec);
2319 		goto out;
2320 	}
2321 
2322 	if (map->start > sym->start)
2323 		vaddr = map->start;
2324 	vaddr += sym->start + pp->offset + map->pgoff;
2325 	pp->offset = 0;
2326 
2327 	if (!pev->event) {
2328 		pev->event = function;
2329 		function = NULL;
2330 	}
2331 	if (!pev->group) {
2332 		char *ptr1, *ptr2, *exec_copy;
2333 
2334 		pev->group = zalloc(sizeof(char *) * 64);
2335 		exec_copy = strdup(exec);
2336 		if (!exec_copy) {
2337 			ret = -ENOMEM;
2338 			pr_warning("Failed to copy exec string.\n");
2339 			goto out;
2340 		}
2341 
2342 		ptr1 = strdup(basename(exec_copy));
2343 		if (ptr1) {
2344 			ptr2 = strpbrk(ptr1, "-._");
2345 			if (ptr2)
2346 				*ptr2 = '\0';
2347 			e_snprintf(pev->group, 64, "%s_%s", PERFPROBE_GROUP,
2348 					ptr1);
2349 			free(ptr1);
2350 		}
2351 		free(exec_copy);
2352 	}
2353 	free(pp->function);
2354 	pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
2355 	if (!pp->function) {
2356 		ret = -ENOMEM;
2357 		pr_warning("Failed to allocate memory by zalloc.\n");
2358 		goto out;
2359 	}
2360 	e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr);
2361 	ret = 0;
2362 
2363 out:
2364 	if (map) {
2365 		dso__delete(map->dso);
2366 		map__delete(map);
2367 	}
2368 	if (function)
2369 		free(function);
2370 	if (name)
2371 		free(name);
2372 	return ret;
2373 }
2374