xref: /openbmc/linux/tools/perf/util/probe-event.c (revision 4f3db074)
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 <api/fs/debugfs.h>
44 #include <api/fs/tracefs.h>
45 #include "trace-event.h"	/* For __maybe_unused */
46 #include "probe-event.h"
47 #include "probe-finder.h"
48 #include "session.h"
49 
50 #define MAX_CMDLEN 256
51 #define PERFPROBE_GROUP "probe"
52 
53 bool probe_event_dry_run;	/* Dry run flag */
54 
55 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
56 
57 /* If there is no space to write, returns -E2BIG. */
58 static int e_snprintf(char *str, size_t size, const char *format, ...)
59 	__attribute__((format(printf, 3, 4)));
60 
61 static int e_snprintf(char *str, size_t size, const char *format, ...)
62 {
63 	int ret;
64 	va_list ap;
65 	va_start(ap, format);
66 	ret = vsnprintf(str, size, format, ap);
67 	va_end(ap);
68 	if (ret >= (int)size)
69 		ret = -E2BIG;
70 	return ret;
71 }
72 
73 static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
74 static void clear_probe_trace_event(struct probe_trace_event *tev);
75 static struct machine *host_machine;
76 
77 /* Initialize symbol maps and path of vmlinux/modules */
78 static int init_symbol_maps(bool user_only)
79 {
80 	int ret;
81 
82 	symbol_conf.sort_by_name = true;
83 	symbol_conf.allow_aliases = true;
84 	ret = symbol__init(NULL);
85 	if (ret < 0) {
86 		pr_debug("Failed to init symbol map.\n");
87 		goto out;
88 	}
89 
90 	if (host_machine || user_only)	/* already initialized */
91 		return 0;
92 
93 	if (symbol_conf.vmlinux_name)
94 		pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
95 
96 	host_machine = machine__new_host();
97 	if (!host_machine) {
98 		pr_debug("machine__new_host() failed.\n");
99 		symbol__exit();
100 		ret = -1;
101 	}
102 out:
103 	if (ret < 0)
104 		pr_warning("Failed to init vmlinux path.\n");
105 	return ret;
106 }
107 
108 static void exit_symbol_maps(void)
109 {
110 	if (host_machine) {
111 		machine__delete(host_machine);
112 		host_machine = NULL;
113 	}
114 	symbol__exit();
115 }
116 
117 static struct symbol *__find_kernel_function_by_name(const char *name,
118 						     struct map **mapp)
119 {
120 	return machine__find_kernel_function_by_name(host_machine, name, mapp,
121 						     NULL);
122 }
123 
124 static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
125 {
126 	return machine__find_kernel_function(host_machine, addr, mapp, NULL);
127 }
128 
129 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
130 {
131 	/* kmap->ref_reloc_sym should be set if host_machine is initialized */
132 	struct kmap *kmap;
133 
134 	if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
135 		return NULL;
136 
137 	kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
138 	if (!kmap)
139 		return NULL;
140 	return kmap->ref_reloc_sym;
141 }
142 
143 static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
144 {
145 	struct ref_reloc_sym *reloc_sym;
146 	struct symbol *sym;
147 	struct map *map;
148 
149 	/* ref_reloc_sym is just a label. Need a special fix*/
150 	reloc_sym = kernel_get_ref_reloc_sym();
151 	if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
152 		return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
153 	else {
154 		sym = __find_kernel_function_by_name(name, &map);
155 		if (sym)
156 			return map->unmap_ip(map, sym->start) -
157 				((reloc) ? 0 : map->reloc);
158 	}
159 	return 0;
160 }
161 
162 static struct map *kernel_get_module_map(const char *module)
163 {
164 	struct rb_node *nd;
165 	struct map_groups *grp = &host_machine->kmaps;
166 
167 	/* A file path -- this is an offline module */
168 	if (module && strchr(module, '/'))
169 		return machine__new_module(host_machine, 0, module);
170 
171 	if (!module)
172 		module = "kernel";
173 
174 	for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
175 		struct map *pos = rb_entry(nd, struct map, rb_node);
176 		if (strncmp(pos->dso->short_name + 1, module,
177 			    pos->dso->short_name_len - 2) == 0) {
178 			return pos;
179 		}
180 	}
181 	return NULL;
182 }
183 
184 static struct map *get_target_map(const char *target, bool user)
185 {
186 	/* Init maps of given executable or kernel */
187 	if (user)
188 		return dso__new_map(target);
189 	else
190 		return kernel_get_module_map(target);
191 }
192 
193 static void put_target_map(struct map *map, bool user)
194 {
195 	if (map && user) {
196 		/* Only the user map needs to be released */
197 		dso__delete(map->dso);
198 		map__delete(map);
199 	}
200 }
201 
202 
203 static struct dso *kernel_get_module_dso(const char *module)
204 {
205 	struct dso *dso;
206 	struct map *map;
207 	const char *vmlinux_name;
208 
209 	if (module) {
210 		list_for_each_entry(dso, &host_machine->kernel_dsos.head,
211 				    node) {
212 			if (strncmp(dso->short_name + 1, module,
213 				    dso->short_name_len - 2) == 0)
214 				goto found;
215 		}
216 		pr_debug("Failed to find module %s.\n", module);
217 		return NULL;
218 	}
219 
220 	map = host_machine->vmlinux_maps[MAP__FUNCTION];
221 	dso = map->dso;
222 
223 	vmlinux_name = symbol_conf.vmlinux_name;
224 	if (vmlinux_name) {
225 		if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
226 			return NULL;
227 	} else {
228 		if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
229 			pr_debug("Failed to load kernel map.\n");
230 			return NULL;
231 		}
232 	}
233 found:
234 	return dso;
235 }
236 
237 const char *kernel_get_module_path(const char *module)
238 {
239 	struct dso *dso = kernel_get_module_dso(module);
240 	return (dso) ? dso->long_name : NULL;
241 }
242 
243 static int convert_exec_to_group(const char *exec, char **result)
244 {
245 	char *ptr1, *ptr2, *exec_copy;
246 	char buf[64];
247 	int ret;
248 
249 	exec_copy = strdup(exec);
250 	if (!exec_copy)
251 		return -ENOMEM;
252 
253 	ptr1 = basename(exec_copy);
254 	if (!ptr1) {
255 		ret = -EINVAL;
256 		goto out;
257 	}
258 
259 	ptr2 = strpbrk(ptr1, "-._");
260 	if (ptr2)
261 		*ptr2 = '\0';
262 	ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
263 	if (ret < 0)
264 		goto out;
265 
266 	*result = strdup(buf);
267 	ret = *result ? 0 : -ENOMEM;
268 
269 out:
270 	free(exec_copy);
271 	return ret;
272 }
273 
274 static void clear_perf_probe_point(struct perf_probe_point *pp)
275 {
276 	free(pp->file);
277 	free(pp->function);
278 	free(pp->lazy_line);
279 }
280 
281 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
282 {
283 	int i;
284 
285 	for (i = 0; i < ntevs; i++)
286 		clear_probe_trace_event(tevs + i);
287 }
288 
289 #ifdef HAVE_DWARF_SUPPORT
290 /*
291  * Some binaries like glibc have special symbols which are on the symbol
292  * table, but not in the debuginfo. If we can find the address of the
293  * symbol from map, we can translate the address back to the probe point.
294  */
295 static int find_alternative_probe_point(struct debuginfo *dinfo,
296 					struct perf_probe_point *pp,
297 					struct perf_probe_point *result,
298 					const char *target, bool uprobes)
299 {
300 	struct map *map = NULL;
301 	struct symbol *sym;
302 	u64 address = 0;
303 	int ret = -ENOENT;
304 
305 	/* This can work only for function-name based one */
306 	if (!pp->function || pp->file)
307 		return -ENOTSUP;
308 
309 	map = get_target_map(target, uprobes);
310 	if (!map)
311 		return -EINVAL;
312 
313 	/* Find the address of given function */
314 	map__for_each_symbol_by_name(map, pp->function, sym) {
315 		if (uprobes)
316 			address = sym->start;
317 		else
318 			address = map->unmap_ip(map, sym->start);
319 		break;
320 	}
321 	if (!address) {
322 		ret = -ENOENT;
323 		goto out;
324 	}
325 	pr_debug("Symbol %s address found : %" PRIx64 "\n",
326 			pp->function, address);
327 
328 	ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
329 					  result);
330 	if (ret <= 0)
331 		ret = (!ret) ? -ENOENT : ret;
332 	else {
333 		result->offset += pp->offset;
334 		result->line += pp->line;
335 		result->retprobe = pp->retprobe;
336 		ret = 0;
337 	}
338 
339 out:
340 	put_target_map(map, uprobes);
341 	return ret;
342 
343 }
344 
345 static int get_alternative_probe_event(struct debuginfo *dinfo,
346 				       struct perf_probe_event *pev,
347 				       struct perf_probe_point *tmp,
348 				       const char *target)
349 {
350 	int ret;
351 
352 	memcpy(tmp, &pev->point, sizeof(*tmp));
353 	memset(&pev->point, 0, sizeof(pev->point));
354 	ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
355 					   target, pev->uprobes);
356 	if (ret < 0)
357 		memcpy(&pev->point, tmp, sizeof(*tmp));
358 
359 	return ret;
360 }
361 
362 static int get_alternative_line_range(struct debuginfo *dinfo,
363 				      struct line_range *lr,
364 				      const char *target, bool user)
365 {
366 	struct perf_probe_point pp = { .function = lr->function,
367 				       .file = lr->file,
368 				       .line = lr->start };
369 	struct perf_probe_point result;
370 	int ret, len = 0;
371 
372 	memset(&result, 0, sizeof(result));
373 
374 	if (lr->end != INT_MAX)
375 		len = lr->end - lr->start;
376 	ret = find_alternative_probe_point(dinfo, &pp, &result,
377 					   target, user);
378 	if (!ret) {
379 		lr->function = result.function;
380 		lr->file = result.file;
381 		lr->start = result.line;
382 		if (lr->end != INT_MAX)
383 			lr->end = lr->start + len;
384 		clear_perf_probe_point(&pp);
385 	}
386 	return ret;
387 }
388 
389 /* Open new debuginfo of given module */
390 static struct debuginfo *open_debuginfo(const char *module, bool silent)
391 {
392 	const char *path = module;
393 	struct debuginfo *ret;
394 
395 	if (!module || !strchr(module, '/')) {
396 		path = kernel_get_module_path(module);
397 		if (!path) {
398 			if (!silent)
399 				pr_err("Failed to find path of %s module.\n",
400 				       module ?: "kernel");
401 			return NULL;
402 		}
403 	}
404 	ret = debuginfo__new(path);
405 	if (!ret && !silent) {
406 		pr_warning("The %s file has no debug information.\n", path);
407 		if (!module || !strtailcmp(path, ".ko"))
408 			pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
409 		else
410 			pr_warning("Rebuild with -g, ");
411 		pr_warning("or install an appropriate debuginfo package.\n");
412 	}
413 	return ret;
414 }
415 
416 
417 static int get_text_start_address(const char *exec, unsigned long *address)
418 {
419 	Elf *elf;
420 	GElf_Ehdr ehdr;
421 	GElf_Shdr shdr;
422 	int fd, ret = -ENOENT;
423 
424 	fd = open(exec, O_RDONLY);
425 	if (fd < 0)
426 		return -errno;
427 
428 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
429 	if (elf == NULL)
430 		return -EINVAL;
431 
432 	if (gelf_getehdr(elf, &ehdr) == NULL)
433 		goto out;
434 
435 	if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
436 		goto out;
437 
438 	*address = shdr.sh_addr - shdr.sh_offset;
439 	ret = 0;
440 out:
441 	elf_end(elf);
442 	return ret;
443 }
444 
445 /*
446  * Convert trace point to probe point with debuginfo
447  */
448 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
449 					    struct perf_probe_point *pp,
450 					    bool is_kprobe)
451 {
452 	struct debuginfo *dinfo = NULL;
453 	unsigned long stext = 0;
454 	u64 addr = tp->address;
455 	int ret = -ENOENT;
456 
457 	/* convert the address to dwarf address */
458 	if (!is_kprobe) {
459 		if (!addr) {
460 			ret = -EINVAL;
461 			goto error;
462 		}
463 		ret = get_text_start_address(tp->module, &stext);
464 		if (ret < 0)
465 			goto error;
466 		addr += stext;
467 	} else {
468 		addr = kernel_get_symbol_address_by_name(tp->symbol, false);
469 		if (addr == 0)
470 			goto error;
471 		addr += tp->offset;
472 	}
473 
474 	pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
475 		 tp->module ? : "kernel");
476 
477 	dinfo = open_debuginfo(tp->module, verbose == 0);
478 	if (dinfo) {
479 		ret = debuginfo__find_probe_point(dinfo,
480 						 (unsigned long)addr, pp);
481 		debuginfo__delete(dinfo);
482 	} else
483 		ret = -ENOENT;
484 
485 	if (ret > 0) {
486 		pp->retprobe = tp->retprobe;
487 		return 0;
488 	}
489 error:
490 	pr_debug("Failed to find corresponding probes from debuginfo.\n");
491 	return ret ? : -ENOENT;
492 }
493 
494 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
495 					  int ntevs, const char *exec)
496 {
497 	int i, ret = 0;
498 	unsigned long stext = 0;
499 
500 	if (!exec)
501 		return 0;
502 
503 	ret = get_text_start_address(exec, &stext);
504 	if (ret < 0)
505 		return ret;
506 
507 	for (i = 0; i < ntevs && ret >= 0; i++) {
508 		/* point.address is the addres of point.symbol + point.offset */
509 		tevs[i].point.address -= stext;
510 		tevs[i].point.module = strdup(exec);
511 		if (!tevs[i].point.module) {
512 			ret = -ENOMEM;
513 			break;
514 		}
515 		tevs[i].uprobes = true;
516 	}
517 
518 	return ret;
519 }
520 
521 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
522 					    int ntevs, const char *module)
523 {
524 	int i, ret = 0;
525 	char *tmp;
526 
527 	if (!module)
528 		return 0;
529 
530 	tmp = strrchr(module, '/');
531 	if (tmp) {
532 		/* This is a module path -- get the module name */
533 		module = strdup(tmp + 1);
534 		if (!module)
535 			return -ENOMEM;
536 		tmp = strchr(module, '.');
537 		if (tmp)
538 			*tmp = '\0';
539 		tmp = (char *)module;	/* For free() */
540 	}
541 
542 	for (i = 0; i < ntevs; i++) {
543 		tevs[i].point.module = strdup(module);
544 		if (!tevs[i].point.module) {
545 			ret = -ENOMEM;
546 			break;
547 		}
548 	}
549 
550 	free(tmp);
551 	return ret;
552 }
553 
554 /* Post processing the probe events */
555 static int post_process_probe_trace_events(struct probe_trace_event *tevs,
556 					   int ntevs, const char *module,
557 					   bool uprobe)
558 {
559 	struct ref_reloc_sym *reloc_sym;
560 	u64 etext_addr;
561 	char *tmp;
562 	int i, skipped = 0;
563 
564 	if (uprobe)
565 		return add_exec_to_probe_trace_events(tevs, ntevs, module);
566 
567 	/* Note that currently ref_reloc_sym based probe is not for drivers */
568 	if (module)
569 		return add_module_to_probe_trace_events(tevs, ntevs, module);
570 
571 	reloc_sym = kernel_get_ref_reloc_sym();
572 	if (!reloc_sym) {
573 		pr_warning("Relocated base symbol is not found!\n");
574 		return -EINVAL;
575 	}
576 	/* Get the address of _etext for checking non-probable text symbol */
577 	etext_addr = kernel_get_symbol_address_by_name("_etext", false);
578 
579 	for (i = 0; i < ntevs; i++) {
580 		if (tevs[i].point.address && !tevs[i].point.retprobe) {
581 			/* If we found a wrong one, mark it by NULL symbol */
582 			if (etext_addr < tevs[i].point.address) {
583 				pr_warning("%s+%lu is out of .text, skip it.\n",
584 				   tevs[i].point.symbol, tevs[i].point.offset);
585 				tmp = NULL;
586 				skipped++;
587 			} else {
588 				tmp = strdup(reloc_sym->name);
589 				if (!tmp)
590 					return -ENOMEM;
591 			}
592 			free(tevs[i].point.symbol);
593 			tevs[i].point.symbol = tmp;
594 			tevs[i].point.offset = tevs[i].point.address -
595 					       reloc_sym->unrelocated_addr;
596 		}
597 	}
598 	return skipped;
599 }
600 
601 /* Try to find perf_probe_event with debuginfo */
602 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
603 					  struct probe_trace_event **tevs,
604 					  int max_tevs, const char *target)
605 {
606 	bool need_dwarf = perf_probe_event_need_dwarf(pev);
607 	struct perf_probe_point tmp;
608 	struct debuginfo *dinfo;
609 	int ntevs, ret = 0;
610 
611 	dinfo = open_debuginfo(target, !need_dwarf);
612 
613 	if (!dinfo) {
614 		if (need_dwarf)
615 			return -ENOENT;
616 		pr_debug("Could not open debuginfo. Try to use symbols.\n");
617 		return 0;
618 	}
619 
620 	pr_debug("Try to find probe point from debuginfo.\n");
621 	/* Searching trace events corresponding to a probe event */
622 	ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
623 
624 	if (ntevs == 0)	{  /* Not found, retry with an alternative */
625 		ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
626 		if (!ret) {
627 			ntevs = debuginfo__find_trace_events(dinfo, pev,
628 							     tevs, max_tevs);
629 			/*
630 			 * Write back to the original probe_event for
631 			 * setting appropriate (user given) event name
632 			 */
633 			clear_perf_probe_point(&pev->point);
634 			memcpy(&pev->point, &tmp, sizeof(tmp));
635 		}
636 	}
637 
638 	debuginfo__delete(dinfo);
639 
640 	if (ntevs > 0) {	/* Succeeded to find trace events */
641 		pr_debug("Found %d probe_trace_events.\n", ntevs);
642 		ret = post_process_probe_trace_events(*tevs, ntevs,
643 							target, pev->uprobes);
644 		if (ret < 0 || ret == ntevs) {
645 			clear_probe_trace_events(*tevs, ntevs);
646 			zfree(tevs);
647 		}
648 		if (ret != ntevs)
649 			return ret < 0 ? ret : ntevs;
650 		ntevs = 0;
651 		/* Fall through */
652 	}
653 
654 	if (ntevs == 0)	{	/* No error but failed to find probe point. */
655 		pr_warning("Probe point '%s' not found.\n",
656 			   synthesize_perf_probe_point(&pev->point));
657 		return -ENOENT;
658 	}
659 	/* Error path : ntevs < 0 */
660 	pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
661 	if (ntevs == -EBADF) {
662 		pr_warning("Warning: No dwarf info found in the vmlinux - "
663 			"please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
664 		if (!need_dwarf) {
665 			pr_debug("Trying to use symbols.\n");
666 			return 0;
667 		}
668 	}
669 	return ntevs;
670 }
671 
672 #define LINEBUF_SIZE 256
673 #define NR_ADDITIONAL_LINES 2
674 
675 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
676 {
677 	char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
678 	const char *color = show_num ? "" : PERF_COLOR_BLUE;
679 	const char *prefix = NULL;
680 
681 	do {
682 		if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
683 			goto error;
684 		if (skip)
685 			continue;
686 		if (!prefix) {
687 			prefix = show_num ? "%7d  " : "         ";
688 			color_fprintf(stdout, color, prefix, l);
689 		}
690 		color_fprintf(stdout, color, "%s", buf);
691 
692 	} while (strchr(buf, '\n') == NULL);
693 
694 	return 1;
695 error:
696 	if (ferror(fp)) {
697 		pr_warning("File read error: %s\n",
698 			   strerror_r(errno, sbuf, sizeof(sbuf)));
699 		return -1;
700 	}
701 	return 0;
702 }
703 
704 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
705 {
706 	int rv = __show_one_line(fp, l, skip, show_num);
707 	if (rv == 0) {
708 		pr_warning("Source file is shorter than expected.\n");
709 		rv = -1;
710 	}
711 	return rv;
712 }
713 
714 #define show_one_line_with_num(f,l)	_show_one_line(f,l,false,true)
715 #define show_one_line(f,l)		_show_one_line(f,l,false,false)
716 #define skip_one_line(f,l)		_show_one_line(f,l,true,false)
717 #define show_one_line_or_eof(f,l)	__show_one_line(f,l,false,false)
718 
719 /*
720  * Show line-range always requires debuginfo to find source file and
721  * line number.
722  */
723 static int __show_line_range(struct line_range *lr, const char *module,
724 			     bool user)
725 {
726 	int l = 1;
727 	struct int_node *ln;
728 	struct debuginfo *dinfo;
729 	FILE *fp;
730 	int ret;
731 	char *tmp;
732 	char sbuf[STRERR_BUFSIZE];
733 
734 	/* Search a line range */
735 	dinfo = open_debuginfo(module, false);
736 	if (!dinfo)
737 		return -ENOENT;
738 
739 	ret = debuginfo__find_line_range(dinfo, lr);
740 	if (!ret) {	/* Not found, retry with an alternative */
741 		ret = get_alternative_line_range(dinfo, lr, module, user);
742 		if (!ret)
743 			ret = debuginfo__find_line_range(dinfo, lr);
744 	}
745 	debuginfo__delete(dinfo);
746 	if (ret == 0 || ret == -ENOENT) {
747 		pr_warning("Specified source line is not found.\n");
748 		return -ENOENT;
749 	} else if (ret < 0) {
750 		pr_warning("Debuginfo analysis failed.\n");
751 		return ret;
752 	}
753 
754 	/* Convert source file path */
755 	tmp = lr->path;
756 	ret = get_real_path(tmp, lr->comp_dir, &lr->path);
757 
758 	/* Free old path when new path is assigned */
759 	if (tmp != lr->path)
760 		free(tmp);
761 
762 	if (ret < 0) {
763 		pr_warning("Failed to find source file path.\n");
764 		return ret;
765 	}
766 
767 	setup_pager();
768 
769 	if (lr->function)
770 		fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
771 			lr->start - lr->offset);
772 	else
773 		fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
774 
775 	fp = fopen(lr->path, "r");
776 	if (fp == NULL) {
777 		pr_warning("Failed to open %s: %s\n", lr->path,
778 			   strerror_r(errno, sbuf, sizeof(sbuf)));
779 		return -errno;
780 	}
781 	/* Skip to starting line number */
782 	while (l < lr->start) {
783 		ret = skip_one_line(fp, l++);
784 		if (ret < 0)
785 			goto end;
786 	}
787 
788 	intlist__for_each(ln, lr->line_list) {
789 		for (; ln->i > l; l++) {
790 			ret = show_one_line(fp, l - lr->offset);
791 			if (ret < 0)
792 				goto end;
793 		}
794 		ret = show_one_line_with_num(fp, l++ - lr->offset);
795 		if (ret < 0)
796 			goto end;
797 	}
798 
799 	if (lr->end == INT_MAX)
800 		lr->end = l + NR_ADDITIONAL_LINES;
801 	while (l <= lr->end) {
802 		ret = show_one_line_or_eof(fp, l++ - lr->offset);
803 		if (ret <= 0)
804 			break;
805 	}
806 end:
807 	fclose(fp);
808 	return ret;
809 }
810 
811 int show_line_range(struct line_range *lr, const char *module, bool user)
812 {
813 	int ret;
814 
815 	ret = init_symbol_maps(user);
816 	if (ret < 0)
817 		return ret;
818 	ret = __show_line_range(lr, module, user);
819 	exit_symbol_maps();
820 
821 	return ret;
822 }
823 
824 static int show_available_vars_at(struct debuginfo *dinfo,
825 				  struct perf_probe_event *pev,
826 				  int max_vls, struct strfilter *_filter,
827 				  bool externs, const char *target)
828 {
829 	char *buf;
830 	int ret, i, nvars;
831 	struct str_node *node;
832 	struct variable_list *vls = NULL, *vl;
833 	struct perf_probe_point tmp;
834 	const char *var;
835 
836 	buf = synthesize_perf_probe_point(&pev->point);
837 	if (!buf)
838 		return -EINVAL;
839 	pr_debug("Searching variables at %s\n", buf);
840 
841 	ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
842 						max_vls, externs);
843 	if (!ret) {  /* Not found, retry with an alternative */
844 		ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
845 		if (!ret) {
846 			ret = debuginfo__find_available_vars_at(dinfo, pev,
847 						&vls, max_vls, externs);
848 			/* Release the old probe_point */
849 			clear_perf_probe_point(&tmp);
850 		}
851 	}
852 	if (ret <= 0) {
853 		if (ret == 0 || ret == -ENOENT) {
854 			pr_err("Failed to find the address of %s\n", buf);
855 			ret = -ENOENT;
856 		} else
857 			pr_warning("Debuginfo analysis failed.\n");
858 		goto end;
859 	}
860 
861 	/* Some variables are found */
862 	fprintf(stdout, "Available variables at %s\n", buf);
863 	for (i = 0; i < ret; i++) {
864 		vl = &vls[i];
865 		/*
866 		 * A probe point might be converted to
867 		 * several trace points.
868 		 */
869 		fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
870 			vl->point.offset);
871 		zfree(&vl->point.symbol);
872 		nvars = 0;
873 		if (vl->vars) {
874 			strlist__for_each(node, vl->vars) {
875 				var = strchr(node->s, '\t') + 1;
876 				if (strfilter__compare(_filter, var)) {
877 					fprintf(stdout, "\t\t%s\n", node->s);
878 					nvars++;
879 				}
880 			}
881 			strlist__delete(vl->vars);
882 		}
883 		if (nvars == 0)
884 			fprintf(stdout, "\t\t(No matched variables)\n");
885 	}
886 	free(vls);
887 end:
888 	free(buf);
889 	return ret;
890 }
891 
892 /* Show available variables on given probe point */
893 int show_available_vars(struct perf_probe_event *pevs, int npevs,
894 			int max_vls, const char *module,
895 			struct strfilter *_filter, bool externs)
896 {
897 	int i, ret = 0;
898 	struct debuginfo *dinfo;
899 
900 	ret = init_symbol_maps(pevs->uprobes);
901 	if (ret < 0)
902 		return ret;
903 
904 	dinfo = open_debuginfo(module, false);
905 	if (!dinfo) {
906 		ret = -ENOENT;
907 		goto out;
908 	}
909 
910 	setup_pager();
911 
912 	for (i = 0; i < npevs && ret >= 0; i++)
913 		ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
914 					     externs, module);
915 
916 	debuginfo__delete(dinfo);
917 out:
918 	exit_symbol_maps();
919 	return ret;
920 }
921 
922 #else	/* !HAVE_DWARF_SUPPORT */
923 
924 static int
925 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
926 				 struct perf_probe_point *pp __maybe_unused,
927 				 bool is_kprobe __maybe_unused)
928 {
929 	return -ENOSYS;
930 }
931 
932 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
933 				struct probe_trace_event **tevs __maybe_unused,
934 				int max_tevs __maybe_unused,
935 				const char *target __maybe_unused)
936 {
937 	if (perf_probe_event_need_dwarf(pev)) {
938 		pr_warning("Debuginfo-analysis is not supported.\n");
939 		return -ENOSYS;
940 	}
941 
942 	return 0;
943 }
944 
945 int show_line_range(struct line_range *lr __maybe_unused,
946 		    const char *module __maybe_unused,
947 		    bool user __maybe_unused)
948 {
949 	pr_warning("Debuginfo-analysis is not supported.\n");
950 	return -ENOSYS;
951 }
952 
953 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
954 			int npevs __maybe_unused, int max_vls __maybe_unused,
955 			const char *module __maybe_unused,
956 			struct strfilter *filter __maybe_unused,
957 			bool externs __maybe_unused)
958 {
959 	pr_warning("Debuginfo-analysis is not supported.\n");
960 	return -ENOSYS;
961 }
962 #endif
963 
964 void line_range__clear(struct line_range *lr)
965 {
966 	free(lr->function);
967 	free(lr->file);
968 	free(lr->path);
969 	free(lr->comp_dir);
970 	intlist__delete(lr->line_list);
971 	memset(lr, 0, sizeof(*lr));
972 }
973 
974 int line_range__init(struct line_range *lr)
975 {
976 	memset(lr, 0, sizeof(*lr));
977 	lr->line_list = intlist__new(NULL);
978 	if (!lr->line_list)
979 		return -ENOMEM;
980 	else
981 		return 0;
982 }
983 
984 static int parse_line_num(char **ptr, int *val, const char *what)
985 {
986 	const char *start = *ptr;
987 
988 	errno = 0;
989 	*val = strtol(*ptr, ptr, 0);
990 	if (errno || *ptr == start) {
991 		semantic_error("'%s' is not a valid number.\n", what);
992 		return -EINVAL;
993 	}
994 	return 0;
995 }
996 
997 /* Check the name is good for event, group or function */
998 static bool is_c_func_name(const char *name)
999 {
1000 	if (!isalpha(*name) && *name != '_')
1001 		return false;
1002 	while (*++name != '\0') {
1003 		if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1004 			return false;
1005 	}
1006 	return true;
1007 }
1008 
1009 /*
1010  * Stuff 'lr' according to the line range described by 'arg'.
1011  * The line range syntax is described by:
1012  *
1013  *         SRC[:SLN[+NUM|-ELN]]
1014  *         FNC[@SRC][:SLN[+NUM|-ELN]]
1015  */
1016 int parse_line_range_desc(const char *arg, struct line_range *lr)
1017 {
1018 	char *range, *file, *name = strdup(arg);
1019 	int err;
1020 
1021 	if (!name)
1022 		return -ENOMEM;
1023 
1024 	lr->start = 0;
1025 	lr->end = INT_MAX;
1026 
1027 	range = strchr(name, ':');
1028 	if (range) {
1029 		*range++ = '\0';
1030 
1031 		err = parse_line_num(&range, &lr->start, "start line");
1032 		if (err)
1033 			goto err;
1034 
1035 		if (*range == '+' || *range == '-') {
1036 			const char c = *range++;
1037 
1038 			err = parse_line_num(&range, &lr->end, "end line");
1039 			if (err)
1040 				goto err;
1041 
1042 			if (c == '+') {
1043 				lr->end += lr->start;
1044 				/*
1045 				 * Adjust the number of lines here.
1046 				 * If the number of lines == 1, the
1047 				 * the end of line should be equal to
1048 				 * the start of line.
1049 				 */
1050 				lr->end--;
1051 			}
1052 		}
1053 
1054 		pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1055 
1056 		err = -EINVAL;
1057 		if (lr->start > lr->end) {
1058 			semantic_error("Start line must be smaller"
1059 				       " than end line.\n");
1060 			goto err;
1061 		}
1062 		if (*range != '\0') {
1063 			semantic_error("Tailing with invalid str '%s'.\n", range);
1064 			goto err;
1065 		}
1066 	}
1067 
1068 	file = strchr(name, '@');
1069 	if (file) {
1070 		*file = '\0';
1071 		lr->file = strdup(++file);
1072 		if (lr->file == NULL) {
1073 			err = -ENOMEM;
1074 			goto err;
1075 		}
1076 		lr->function = name;
1077 	} else if (strchr(name, '/') || strchr(name, '.'))
1078 		lr->file = name;
1079 	else if (is_c_func_name(name))/* We reuse it for checking funcname */
1080 		lr->function = name;
1081 	else {	/* Invalid name */
1082 		semantic_error("'%s' is not a valid function name.\n", name);
1083 		err = -EINVAL;
1084 		goto err;
1085 	}
1086 
1087 	return 0;
1088 err:
1089 	free(name);
1090 	return err;
1091 }
1092 
1093 /* Parse probepoint definition. */
1094 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1095 {
1096 	struct perf_probe_point *pp = &pev->point;
1097 	char *ptr, *tmp;
1098 	char c, nc = 0;
1099 	bool file_spec = false;
1100 	/*
1101 	 * <Syntax>
1102 	 * perf probe [EVENT=]SRC[:LN|;PTN]
1103 	 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1104 	 *
1105 	 * TODO:Group name support
1106 	 */
1107 
1108 	ptr = strpbrk(arg, ";=@+%");
1109 	if (ptr && *ptr == '=') {	/* Event name */
1110 		*ptr = '\0';
1111 		tmp = ptr + 1;
1112 		if (strchr(arg, ':')) {
1113 			semantic_error("Group name is not supported yet.\n");
1114 			return -ENOTSUP;
1115 		}
1116 		if (!is_c_func_name(arg)) {
1117 			semantic_error("%s is bad for event name -it must "
1118 				       "follow C symbol-naming rule.\n", arg);
1119 			return -EINVAL;
1120 		}
1121 		pev->event = strdup(arg);
1122 		if (pev->event == NULL)
1123 			return -ENOMEM;
1124 		pev->group = NULL;
1125 		arg = tmp;
1126 	}
1127 
1128 	/*
1129 	 * Check arg is function or file name and copy it.
1130 	 *
1131 	 * We consider arg to be a file spec if and only if it satisfies
1132 	 * all of the below criteria::
1133 	 * - it does not include any of "+@%",
1134 	 * - it includes one of ":;", and
1135 	 * - it has a period '.' in the name.
1136 	 *
1137 	 * Otherwise, we consider arg to be a function specification.
1138 	 */
1139 	if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1140 		/* This is a file spec if it includes a '.' before ; or : */
1141 		if (memchr(arg, '.', ptr - arg))
1142 			file_spec = true;
1143 	}
1144 
1145 	ptr = strpbrk(arg, ";:+@%");
1146 	if (ptr) {
1147 		nc = *ptr;
1148 		*ptr++ = '\0';
1149 	}
1150 
1151 	tmp = strdup(arg);
1152 	if (tmp == NULL)
1153 		return -ENOMEM;
1154 
1155 	if (file_spec)
1156 		pp->file = tmp;
1157 	else
1158 		pp->function = tmp;
1159 
1160 	/* Parse other options */
1161 	while (ptr) {
1162 		arg = ptr;
1163 		c = nc;
1164 		if (c == ';') {	/* Lazy pattern must be the last part */
1165 			pp->lazy_line = strdup(arg);
1166 			if (pp->lazy_line == NULL)
1167 				return -ENOMEM;
1168 			break;
1169 		}
1170 		ptr = strpbrk(arg, ";:+@%");
1171 		if (ptr) {
1172 			nc = *ptr;
1173 			*ptr++ = '\0';
1174 		}
1175 		switch (c) {
1176 		case ':':	/* Line number */
1177 			pp->line = strtoul(arg, &tmp, 0);
1178 			if (*tmp != '\0') {
1179 				semantic_error("There is non-digit char"
1180 					       " in line number.\n");
1181 				return -EINVAL;
1182 			}
1183 			break;
1184 		case '+':	/* Byte offset from a symbol */
1185 			pp->offset = strtoul(arg, &tmp, 0);
1186 			if (*tmp != '\0') {
1187 				semantic_error("There is non-digit character"
1188 						" in offset.\n");
1189 				return -EINVAL;
1190 			}
1191 			break;
1192 		case '@':	/* File name */
1193 			if (pp->file) {
1194 				semantic_error("SRC@SRC is not allowed.\n");
1195 				return -EINVAL;
1196 			}
1197 			pp->file = strdup(arg);
1198 			if (pp->file == NULL)
1199 				return -ENOMEM;
1200 			break;
1201 		case '%':	/* Probe places */
1202 			if (strcmp(arg, "return") == 0) {
1203 				pp->retprobe = 1;
1204 			} else {	/* Others not supported yet */
1205 				semantic_error("%%%s is not supported.\n", arg);
1206 				return -ENOTSUP;
1207 			}
1208 			break;
1209 		default:	/* Buggy case */
1210 			pr_err("This program has a bug at %s:%d.\n",
1211 				__FILE__, __LINE__);
1212 			return -ENOTSUP;
1213 			break;
1214 		}
1215 	}
1216 
1217 	/* Exclusion check */
1218 	if (pp->lazy_line && pp->line) {
1219 		semantic_error("Lazy pattern can't be used with"
1220 			       " line number.\n");
1221 		return -EINVAL;
1222 	}
1223 
1224 	if (pp->lazy_line && pp->offset) {
1225 		semantic_error("Lazy pattern can't be used with offset.\n");
1226 		return -EINVAL;
1227 	}
1228 
1229 	if (pp->line && pp->offset) {
1230 		semantic_error("Offset can't be used with line number.\n");
1231 		return -EINVAL;
1232 	}
1233 
1234 	if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1235 		semantic_error("File always requires line number or "
1236 			       "lazy pattern.\n");
1237 		return -EINVAL;
1238 	}
1239 
1240 	if (pp->offset && !pp->function) {
1241 		semantic_error("Offset requires an entry function.\n");
1242 		return -EINVAL;
1243 	}
1244 
1245 	if (pp->retprobe && !pp->function) {
1246 		semantic_error("Return probe requires an entry function.\n");
1247 		return -EINVAL;
1248 	}
1249 
1250 	if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1251 		semantic_error("Offset/Line/Lazy pattern can't be used with "
1252 			       "return probe.\n");
1253 		return -EINVAL;
1254 	}
1255 
1256 	pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1257 		 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1258 		 pp->lazy_line);
1259 	return 0;
1260 }
1261 
1262 /* Parse perf-probe event argument */
1263 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1264 {
1265 	char *tmp, *goodname;
1266 	struct perf_probe_arg_field **fieldp;
1267 
1268 	pr_debug("parsing arg: %s into ", str);
1269 
1270 	tmp = strchr(str, '=');
1271 	if (tmp) {
1272 		arg->name = strndup(str, tmp - str);
1273 		if (arg->name == NULL)
1274 			return -ENOMEM;
1275 		pr_debug("name:%s ", arg->name);
1276 		str = tmp + 1;
1277 	}
1278 
1279 	tmp = strchr(str, ':');
1280 	if (tmp) {	/* Type setting */
1281 		*tmp = '\0';
1282 		arg->type = strdup(tmp + 1);
1283 		if (arg->type == NULL)
1284 			return -ENOMEM;
1285 		pr_debug("type:%s ", arg->type);
1286 	}
1287 
1288 	tmp = strpbrk(str, "-.[");
1289 	if (!is_c_varname(str) || !tmp) {
1290 		/* A variable, register, symbol or special value */
1291 		arg->var = strdup(str);
1292 		if (arg->var == NULL)
1293 			return -ENOMEM;
1294 		pr_debug("%s\n", arg->var);
1295 		return 0;
1296 	}
1297 
1298 	/* Structure fields or array element */
1299 	arg->var = strndup(str, tmp - str);
1300 	if (arg->var == NULL)
1301 		return -ENOMEM;
1302 	goodname = arg->var;
1303 	pr_debug("%s, ", arg->var);
1304 	fieldp = &arg->field;
1305 
1306 	do {
1307 		*fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1308 		if (*fieldp == NULL)
1309 			return -ENOMEM;
1310 		if (*tmp == '[') {	/* Array */
1311 			str = tmp;
1312 			(*fieldp)->index = strtol(str + 1, &tmp, 0);
1313 			(*fieldp)->ref = true;
1314 			if (*tmp != ']' || tmp == str + 1) {
1315 				semantic_error("Array index must be a"
1316 						" number.\n");
1317 				return -EINVAL;
1318 			}
1319 			tmp++;
1320 			if (*tmp == '\0')
1321 				tmp = NULL;
1322 		} else {		/* Structure */
1323 			if (*tmp == '.') {
1324 				str = tmp + 1;
1325 				(*fieldp)->ref = false;
1326 			} else if (tmp[1] == '>') {
1327 				str = tmp + 2;
1328 				(*fieldp)->ref = true;
1329 			} else {
1330 				semantic_error("Argument parse error: %s\n",
1331 					       str);
1332 				return -EINVAL;
1333 			}
1334 			tmp = strpbrk(str, "-.[");
1335 		}
1336 		if (tmp) {
1337 			(*fieldp)->name = strndup(str, tmp - str);
1338 			if ((*fieldp)->name == NULL)
1339 				return -ENOMEM;
1340 			if (*str != '[')
1341 				goodname = (*fieldp)->name;
1342 			pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1343 			fieldp = &(*fieldp)->next;
1344 		}
1345 	} while (tmp);
1346 	(*fieldp)->name = strdup(str);
1347 	if ((*fieldp)->name == NULL)
1348 		return -ENOMEM;
1349 	if (*str != '[')
1350 		goodname = (*fieldp)->name;
1351 	pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1352 
1353 	/* If no name is specified, set the last field name (not array index)*/
1354 	if (!arg->name) {
1355 		arg->name = strdup(goodname);
1356 		if (arg->name == NULL)
1357 			return -ENOMEM;
1358 	}
1359 	return 0;
1360 }
1361 
1362 /* Parse perf-probe event command */
1363 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1364 {
1365 	char **argv;
1366 	int argc, i, ret = 0;
1367 
1368 	argv = argv_split(cmd, &argc);
1369 	if (!argv) {
1370 		pr_debug("Failed to split arguments.\n");
1371 		return -ENOMEM;
1372 	}
1373 	if (argc - 1 > MAX_PROBE_ARGS) {
1374 		semantic_error("Too many probe arguments (%d).\n", argc - 1);
1375 		ret = -ERANGE;
1376 		goto out;
1377 	}
1378 	/* Parse probe point */
1379 	ret = parse_perf_probe_point(argv[0], pev);
1380 	if (ret < 0)
1381 		goto out;
1382 
1383 	/* Copy arguments and ensure return probe has no C argument */
1384 	pev->nargs = argc - 1;
1385 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1386 	if (pev->args == NULL) {
1387 		ret = -ENOMEM;
1388 		goto out;
1389 	}
1390 	for (i = 0; i < pev->nargs && ret >= 0; i++) {
1391 		ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1392 		if (ret >= 0 &&
1393 		    is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1394 			semantic_error("You can't specify local variable for"
1395 				       " kretprobe.\n");
1396 			ret = -EINVAL;
1397 		}
1398 	}
1399 out:
1400 	argv_free(argv);
1401 
1402 	return ret;
1403 }
1404 
1405 /* Return true if this perf_probe_event requires debuginfo */
1406 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1407 {
1408 	int i;
1409 
1410 	if (pev->point.file || pev->point.line || pev->point.lazy_line)
1411 		return true;
1412 
1413 	for (i = 0; i < pev->nargs; i++)
1414 		if (is_c_varname(pev->args[i].var))
1415 			return true;
1416 
1417 	return false;
1418 }
1419 
1420 /* Parse probe_events event into struct probe_point */
1421 static int parse_probe_trace_command(const char *cmd,
1422 				     struct probe_trace_event *tev)
1423 {
1424 	struct probe_trace_point *tp = &tev->point;
1425 	char pr;
1426 	char *p;
1427 	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1428 	int ret, i, argc;
1429 	char **argv;
1430 
1431 	pr_debug("Parsing probe_events: %s\n", cmd);
1432 	argv = argv_split(cmd, &argc);
1433 	if (!argv) {
1434 		pr_debug("Failed to split arguments.\n");
1435 		return -ENOMEM;
1436 	}
1437 	if (argc < 2) {
1438 		semantic_error("Too few probe arguments.\n");
1439 		ret = -ERANGE;
1440 		goto out;
1441 	}
1442 
1443 	/* Scan event and group name. */
1444 	argv0_str = strdup(argv[0]);
1445 	if (argv0_str == NULL) {
1446 		ret = -ENOMEM;
1447 		goto out;
1448 	}
1449 	fmt1_str = strtok_r(argv0_str, ":", &fmt);
1450 	fmt2_str = strtok_r(NULL, "/", &fmt);
1451 	fmt3_str = strtok_r(NULL, " \t", &fmt);
1452 	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1453 	    || fmt3_str == NULL) {
1454 		semantic_error("Failed to parse event name: %s\n", argv[0]);
1455 		ret = -EINVAL;
1456 		goto out;
1457 	}
1458 	pr = fmt1_str[0];
1459 	tev->group = strdup(fmt2_str);
1460 	tev->event = strdup(fmt3_str);
1461 	if (tev->group == NULL || tev->event == NULL) {
1462 		ret = -ENOMEM;
1463 		goto out;
1464 	}
1465 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1466 
1467 	tp->retprobe = (pr == 'r');
1468 
1469 	/* Scan module name(if there), function name and offset */
1470 	p = strchr(argv[1], ':');
1471 	if (p) {
1472 		tp->module = strndup(argv[1], p - argv[1]);
1473 		p++;
1474 	} else
1475 		p = argv[1];
1476 	fmt1_str = strtok_r(p, "+", &fmt);
1477 	if (fmt1_str[0] == '0')	/* only the address started with 0x */
1478 		tp->address = strtoul(fmt1_str, NULL, 0);
1479 	else {
1480 		/* Only the symbol-based probe has offset */
1481 		tp->symbol = strdup(fmt1_str);
1482 		if (tp->symbol == NULL) {
1483 			ret = -ENOMEM;
1484 			goto out;
1485 		}
1486 		fmt2_str = strtok_r(NULL, "", &fmt);
1487 		if (fmt2_str == NULL)
1488 			tp->offset = 0;
1489 		else
1490 			tp->offset = strtoul(fmt2_str, NULL, 10);
1491 	}
1492 
1493 	tev->nargs = argc - 2;
1494 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1495 	if (tev->args == NULL) {
1496 		ret = -ENOMEM;
1497 		goto out;
1498 	}
1499 	for (i = 0; i < tev->nargs; i++) {
1500 		p = strchr(argv[i + 2], '=');
1501 		if (p)	/* We don't need which register is assigned. */
1502 			*p++ = '\0';
1503 		else
1504 			p = argv[i + 2];
1505 		tev->args[i].name = strdup(argv[i + 2]);
1506 		/* TODO: parse regs and offset */
1507 		tev->args[i].value = strdup(p);
1508 		if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1509 			ret = -ENOMEM;
1510 			goto out;
1511 		}
1512 	}
1513 	ret = 0;
1514 out:
1515 	free(argv0_str);
1516 	argv_free(argv);
1517 	return ret;
1518 }
1519 
1520 /* Compose only probe arg */
1521 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1522 {
1523 	struct perf_probe_arg_field *field = pa->field;
1524 	int ret;
1525 	char *tmp = buf;
1526 
1527 	if (pa->name && pa->var)
1528 		ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1529 	else
1530 		ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1531 	if (ret <= 0)
1532 		goto error;
1533 	tmp += ret;
1534 	len -= ret;
1535 
1536 	while (field) {
1537 		if (field->name[0] == '[')
1538 			ret = e_snprintf(tmp, len, "%s", field->name);
1539 		else
1540 			ret = e_snprintf(tmp, len, "%s%s",
1541 					 field->ref ? "->" : ".", field->name);
1542 		if (ret <= 0)
1543 			goto error;
1544 		tmp += ret;
1545 		len -= ret;
1546 		field = field->next;
1547 	}
1548 
1549 	if (pa->type) {
1550 		ret = e_snprintf(tmp, len, ":%s", pa->type);
1551 		if (ret <= 0)
1552 			goto error;
1553 		tmp += ret;
1554 		len -= ret;
1555 	}
1556 
1557 	return tmp - buf;
1558 error:
1559 	pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1560 	return ret;
1561 }
1562 
1563 /* Compose only probe point (not argument) */
1564 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1565 {
1566 	char *buf, *tmp;
1567 	char offs[32] = "", line[32] = "", file[32] = "";
1568 	int ret, len;
1569 
1570 	buf = zalloc(MAX_CMDLEN);
1571 	if (buf == NULL) {
1572 		ret = -ENOMEM;
1573 		goto error;
1574 	}
1575 	if (pp->offset) {
1576 		ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1577 		if (ret <= 0)
1578 			goto error;
1579 	}
1580 	if (pp->line) {
1581 		ret = e_snprintf(line, 32, ":%d", pp->line);
1582 		if (ret <= 0)
1583 			goto error;
1584 	}
1585 	if (pp->file) {
1586 		tmp = pp->file;
1587 		len = strlen(tmp);
1588 		if (len > 30) {
1589 			tmp = strchr(pp->file + len - 30, '/');
1590 			tmp = tmp ? tmp + 1 : pp->file + len - 30;
1591 		}
1592 		ret = e_snprintf(file, 32, "@%s", tmp);
1593 		if (ret <= 0)
1594 			goto error;
1595 	}
1596 
1597 	if (pp->function)
1598 		ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1599 				 offs, pp->retprobe ? "%return" : "", line,
1600 				 file);
1601 	else
1602 		ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1603 	if (ret <= 0)
1604 		goto error;
1605 
1606 	return buf;
1607 error:
1608 	pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1609 	free(buf);
1610 	return NULL;
1611 }
1612 
1613 #if 0
1614 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1615 {
1616 	char *buf;
1617 	int i, len, ret;
1618 
1619 	buf = synthesize_perf_probe_point(&pev->point);
1620 	if (!buf)
1621 		return NULL;
1622 
1623 	len = strlen(buf);
1624 	for (i = 0; i < pev->nargs; i++) {
1625 		ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1626 				 pev->args[i].name);
1627 		if (ret <= 0) {
1628 			free(buf);
1629 			return NULL;
1630 		}
1631 		len += ret;
1632 	}
1633 
1634 	return buf;
1635 }
1636 #endif
1637 
1638 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1639 					     char **buf, size_t *buflen,
1640 					     int depth)
1641 {
1642 	int ret;
1643 	if (ref->next) {
1644 		depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1645 							 buflen, depth + 1);
1646 		if (depth < 0)
1647 			goto out;
1648 	}
1649 
1650 	ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1651 	if (ret < 0)
1652 		depth = ret;
1653 	else {
1654 		*buf += ret;
1655 		*buflen -= ret;
1656 	}
1657 out:
1658 	return depth;
1659 
1660 }
1661 
1662 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1663 				       char *buf, size_t buflen)
1664 {
1665 	struct probe_trace_arg_ref *ref = arg->ref;
1666 	int ret, depth = 0;
1667 	char *tmp = buf;
1668 
1669 	/* Argument name or separator */
1670 	if (arg->name)
1671 		ret = e_snprintf(buf, buflen, " %s=", arg->name);
1672 	else
1673 		ret = e_snprintf(buf, buflen, " ");
1674 	if (ret < 0)
1675 		return ret;
1676 	buf += ret;
1677 	buflen -= ret;
1678 
1679 	/* Special case: @XXX */
1680 	if (arg->value[0] == '@' && arg->ref)
1681 			ref = ref->next;
1682 
1683 	/* Dereferencing arguments */
1684 	if (ref) {
1685 		depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1686 							  &buflen, 1);
1687 		if (depth < 0)
1688 			return depth;
1689 	}
1690 
1691 	/* Print argument value */
1692 	if (arg->value[0] == '@' && arg->ref)
1693 		ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1694 				 arg->ref->offset);
1695 	else
1696 		ret = e_snprintf(buf, buflen, "%s", arg->value);
1697 	if (ret < 0)
1698 		return ret;
1699 	buf += ret;
1700 	buflen -= ret;
1701 
1702 	/* Closing */
1703 	while (depth--) {
1704 		ret = e_snprintf(buf, buflen, ")");
1705 		if (ret < 0)
1706 			return ret;
1707 		buf += ret;
1708 		buflen -= ret;
1709 	}
1710 	/* Print argument type */
1711 	if (arg->type) {
1712 		ret = e_snprintf(buf, buflen, ":%s", arg->type);
1713 		if (ret <= 0)
1714 			return ret;
1715 		buf += ret;
1716 	}
1717 
1718 	return buf - tmp;
1719 }
1720 
1721 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1722 {
1723 	struct probe_trace_point *tp = &tev->point;
1724 	char *buf;
1725 	int i, len, ret;
1726 
1727 	buf = zalloc(MAX_CMDLEN);
1728 	if (buf == NULL)
1729 		return NULL;
1730 
1731 	len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1732 			 tev->group, tev->event);
1733 	if (len <= 0)
1734 		goto error;
1735 
1736 	/* Uprobes must have tp->address and tp->module */
1737 	if (tev->uprobes && (!tp->address || !tp->module))
1738 		goto error;
1739 
1740 	/* Use the tp->address for uprobes */
1741 	if (tev->uprobes)
1742 		ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1743 				 tp->module, tp->address);
1744 	else
1745 		ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1746 				 tp->module ?: "", tp->module ? ":" : "",
1747 				 tp->symbol, tp->offset);
1748 
1749 	if (ret <= 0)
1750 		goto error;
1751 	len += ret;
1752 
1753 	for (i = 0; i < tev->nargs; i++) {
1754 		ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1755 						  MAX_CMDLEN - len);
1756 		if (ret <= 0)
1757 			goto error;
1758 		len += ret;
1759 	}
1760 
1761 	return buf;
1762 error:
1763 	free(buf);
1764 	return NULL;
1765 }
1766 
1767 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1768 					  struct perf_probe_point *pp,
1769 					  bool is_kprobe)
1770 {
1771 	struct symbol *sym = NULL;
1772 	struct map *map;
1773 	u64 addr;
1774 	int ret = -ENOENT;
1775 
1776 	if (!is_kprobe) {
1777 		map = dso__new_map(tp->module);
1778 		if (!map)
1779 			goto out;
1780 		addr = tp->address;
1781 		sym = map__find_symbol(map, addr, NULL);
1782 	} else {
1783 		addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1784 		if (addr) {
1785 			addr += tp->offset;
1786 			sym = __find_kernel_function(addr, &map);
1787 		}
1788 	}
1789 	if (!sym)
1790 		goto out;
1791 
1792 	pp->retprobe = tp->retprobe;
1793 	pp->offset = addr - map->unmap_ip(map, sym->start);
1794 	pp->function = strdup(sym->name);
1795 	ret = pp->function ? 0 : -ENOMEM;
1796 
1797 out:
1798 	if (map && !is_kprobe) {
1799 		dso__delete(map->dso);
1800 		map__delete(map);
1801 	}
1802 
1803 	return ret;
1804 }
1805 
1806 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1807 					struct perf_probe_point *pp,
1808 					bool is_kprobe)
1809 {
1810 	char buf[128];
1811 	int ret;
1812 
1813 	ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1814 	if (!ret)
1815 		return 0;
1816 	ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1817 	if (!ret)
1818 		return 0;
1819 
1820 	pr_debug("Failed to find probe point from both of dwarf and map.\n");
1821 
1822 	if (tp->symbol) {
1823 		pp->function = strdup(tp->symbol);
1824 		pp->offset = tp->offset;
1825 	} else if (!tp->module && !is_kprobe) {
1826 		ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1827 		if (ret < 0)
1828 			return ret;
1829 		pp->function = strdup(buf);
1830 		pp->offset = 0;
1831 	}
1832 	if (pp->function == NULL)
1833 		return -ENOMEM;
1834 
1835 	pp->retprobe = tp->retprobe;
1836 
1837 	return 0;
1838 }
1839 
1840 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1841 			       struct perf_probe_event *pev, bool is_kprobe)
1842 {
1843 	char buf[64] = "";
1844 	int i, ret;
1845 
1846 	/* Convert event/group name */
1847 	pev->event = strdup(tev->event);
1848 	pev->group = strdup(tev->group);
1849 	if (pev->event == NULL || pev->group == NULL)
1850 		return -ENOMEM;
1851 
1852 	/* Convert trace_point to probe_point */
1853 	ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1854 	if (ret < 0)
1855 		return ret;
1856 
1857 	/* Convert trace_arg to probe_arg */
1858 	pev->nargs = tev->nargs;
1859 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1860 	if (pev->args == NULL)
1861 		return -ENOMEM;
1862 	for (i = 0; i < tev->nargs && ret >= 0; i++) {
1863 		if (tev->args[i].name)
1864 			pev->args[i].name = strdup(tev->args[i].name);
1865 		else {
1866 			ret = synthesize_probe_trace_arg(&tev->args[i],
1867 							  buf, 64);
1868 			pev->args[i].name = strdup(buf);
1869 		}
1870 		if (pev->args[i].name == NULL && ret >= 0)
1871 			ret = -ENOMEM;
1872 	}
1873 
1874 	if (ret < 0)
1875 		clear_perf_probe_event(pev);
1876 
1877 	return ret;
1878 }
1879 
1880 void clear_perf_probe_event(struct perf_probe_event *pev)
1881 {
1882 	struct perf_probe_arg_field *field, *next;
1883 	int i;
1884 
1885 	free(pev->event);
1886 	free(pev->group);
1887 	free(pev->target);
1888 	clear_perf_probe_point(&pev->point);
1889 
1890 	for (i = 0; i < pev->nargs; i++) {
1891 		free(pev->args[i].name);
1892 		free(pev->args[i].var);
1893 		free(pev->args[i].type);
1894 		field = pev->args[i].field;
1895 		while (field) {
1896 			next = field->next;
1897 			zfree(&field->name);
1898 			free(field);
1899 			field = next;
1900 		}
1901 	}
1902 	free(pev->args);
1903 	memset(pev, 0, sizeof(*pev));
1904 }
1905 
1906 static void clear_probe_trace_event(struct probe_trace_event *tev)
1907 {
1908 	struct probe_trace_arg_ref *ref, *next;
1909 	int i;
1910 
1911 	free(tev->event);
1912 	free(tev->group);
1913 	free(tev->point.symbol);
1914 	free(tev->point.module);
1915 	for (i = 0; i < tev->nargs; i++) {
1916 		free(tev->args[i].name);
1917 		free(tev->args[i].value);
1918 		free(tev->args[i].type);
1919 		ref = tev->args[i].ref;
1920 		while (ref) {
1921 			next = ref->next;
1922 			free(ref);
1923 			ref = next;
1924 		}
1925 	}
1926 	free(tev->args);
1927 	memset(tev, 0, sizeof(*tev));
1928 }
1929 
1930 static void print_open_warning(int err, bool is_kprobe)
1931 {
1932 	char sbuf[STRERR_BUFSIZE];
1933 
1934 	if (err == -ENOENT) {
1935 		const char *config;
1936 
1937 		if (!is_kprobe)
1938 			config = "CONFIG_UPROBE_EVENTS";
1939 		else
1940 			config = "CONFIG_KPROBE_EVENTS";
1941 
1942 		pr_warning("%cprobe_events file does not exist"
1943 			   " - please rebuild kernel with %s.\n",
1944 			   is_kprobe ? 'k' : 'u', config);
1945 	} else if (err == -ENOTSUP)
1946 		pr_warning("Tracefs or debugfs is not mounted.\n");
1947 	else
1948 		pr_warning("Failed to open %cprobe_events: %s\n",
1949 			   is_kprobe ? 'k' : 'u',
1950 			   strerror_r(-err, sbuf, sizeof(sbuf)));
1951 }
1952 
1953 static void print_both_open_warning(int kerr, int uerr)
1954 {
1955 	/* Both kprobes and uprobes are disabled, warn it. */
1956 	if (kerr == -ENOTSUP && uerr == -ENOTSUP)
1957 		pr_warning("Tracefs or debugfs is not mounted.\n");
1958 	else if (kerr == -ENOENT && uerr == -ENOENT)
1959 		pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1960 			   "or/and CONFIG_UPROBE_EVENTS.\n");
1961 	else {
1962 		char sbuf[STRERR_BUFSIZE];
1963 		pr_warning("Failed to open kprobe events: %s.\n",
1964 			   strerror_r(-kerr, sbuf, sizeof(sbuf)));
1965 		pr_warning("Failed to open uprobe events: %s.\n",
1966 			   strerror_r(-uerr, sbuf, sizeof(sbuf)));
1967 	}
1968 }
1969 
1970 static int open_probe_events(const char *trace_file, bool readwrite)
1971 {
1972 	char buf[PATH_MAX];
1973 	const char *__debugfs;
1974 	const char *tracing_dir = "";
1975 	int ret;
1976 
1977 	__debugfs = tracefs_find_mountpoint();
1978 	if (__debugfs == NULL) {
1979 		tracing_dir = "tracing/";
1980 
1981 		__debugfs = debugfs_find_mountpoint();
1982 		if (__debugfs == NULL)
1983 			return -ENOTSUP;
1984 	}
1985 
1986 	ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1987 			 __debugfs, tracing_dir, trace_file);
1988 	if (ret >= 0) {
1989 		pr_debug("Opening %s write=%d\n", buf, readwrite);
1990 		if (readwrite && !probe_event_dry_run)
1991 			ret = open(buf, O_RDWR | O_APPEND, 0);
1992 		else
1993 			ret = open(buf, O_RDONLY, 0);
1994 
1995 		if (ret < 0)
1996 			ret = -errno;
1997 	}
1998 	return ret;
1999 }
2000 
2001 static int open_kprobe_events(bool readwrite)
2002 {
2003 	return open_probe_events("kprobe_events", readwrite);
2004 }
2005 
2006 static int open_uprobe_events(bool readwrite)
2007 {
2008 	return open_probe_events("uprobe_events", readwrite);
2009 }
2010 
2011 /* Get raw string list of current kprobe_events  or uprobe_events */
2012 static struct strlist *get_probe_trace_command_rawlist(int fd)
2013 {
2014 	int ret, idx;
2015 	FILE *fp;
2016 	char buf[MAX_CMDLEN];
2017 	char *p;
2018 	struct strlist *sl;
2019 
2020 	sl = strlist__new(true, NULL);
2021 
2022 	fp = fdopen(dup(fd), "r");
2023 	while (!feof(fp)) {
2024 		p = fgets(buf, MAX_CMDLEN, fp);
2025 		if (!p)
2026 			break;
2027 
2028 		idx = strlen(p) - 1;
2029 		if (p[idx] == '\n')
2030 			p[idx] = '\0';
2031 		ret = strlist__add(sl, buf);
2032 		if (ret < 0) {
2033 			pr_debug("strlist__add failed (%d)\n", ret);
2034 			strlist__delete(sl);
2035 			return NULL;
2036 		}
2037 	}
2038 	fclose(fp);
2039 
2040 	return sl;
2041 }
2042 
2043 struct kprobe_blacklist_node {
2044 	struct list_head list;
2045 	unsigned long start;
2046 	unsigned long end;
2047 	char *symbol;
2048 };
2049 
2050 static void kprobe_blacklist__delete(struct list_head *blacklist)
2051 {
2052 	struct kprobe_blacklist_node *node;
2053 
2054 	while (!list_empty(blacklist)) {
2055 		node = list_first_entry(blacklist,
2056 					struct kprobe_blacklist_node, list);
2057 		list_del(&node->list);
2058 		free(node->symbol);
2059 		free(node);
2060 	}
2061 }
2062 
2063 static int kprobe_blacklist__load(struct list_head *blacklist)
2064 {
2065 	struct kprobe_blacklist_node *node;
2066 	const char *__debugfs = debugfs_find_mountpoint();
2067 	char buf[PATH_MAX], *p;
2068 	FILE *fp;
2069 	int ret;
2070 
2071 	if (__debugfs == NULL)
2072 		return -ENOTSUP;
2073 
2074 	ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2075 	if (ret < 0)
2076 		return ret;
2077 
2078 	fp = fopen(buf, "r");
2079 	if (!fp)
2080 		return -errno;
2081 
2082 	ret = 0;
2083 	while (fgets(buf, PATH_MAX, fp)) {
2084 		node = zalloc(sizeof(*node));
2085 		if (!node) {
2086 			ret = -ENOMEM;
2087 			break;
2088 		}
2089 		INIT_LIST_HEAD(&node->list);
2090 		list_add_tail(&node->list, blacklist);
2091 		if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2092 			ret = -EINVAL;
2093 			break;
2094 		}
2095 		p = strchr(buf, '\t');
2096 		if (p) {
2097 			p++;
2098 			if (p[strlen(p) - 1] == '\n')
2099 				p[strlen(p) - 1] = '\0';
2100 		} else
2101 			p = (char *)"unknown";
2102 		node->symbol = strdup(p);
2103 		if (!node->symbol) {
2104 			ret = -ENOMEM;
2105 			break;
2106 		}
2107 		pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2108 			  node->start, node->end, node->symbol);
2109 		ret++;
2110 	}
2111 	if (ret < 0)
2112 		kprobe_blacklist__delete(blacklist);
2113 	fclose(fp);
2114 
2115 	return ret;
2116 }
2117 
2118 static struct kprobe_blacklist_node *
2119 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2120 				  unsigned long address)
2121 {
2122 	struct kprobe_blacklist_node *node;
2123 
2124 	list_for_each_entry(node, blacklist, list) {
2125 		if (node->start <= address && address <= node->end)
2126 			return node;
2127 	}
2128 
2129 	return NULL;
2130 }
2131 
2132 /* Show an event */
2133 static int show_perf_probe_event(struct perf_probe_event *pev,
2134 				 const char *module)
2135 {
2136 	int i, ret;
2137 	char buf[128];
2138 	char *place;
2139 
2140 	/* Synthesize only event probe point */
2141 	place = synthesize_perf_probe_point(&pev->point);
2142 	if (!place)
2143 		return -EINVAL;
2144 
2145 	ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
2146 	if (ret < 0)
2147 		return ret;
2148 
2149 	pr_info("  %-20s (on %s", buf, place);
2150 	if (module)
2151 		pr_info(" in %s", module);
2152 
2153 	if (pev->nargs > 0) {
2154 		pr_info(" with");
2155 		for (i = 0; i < pev->nargs; i++) {
2156 			ret = synthesize_perf_probe_arg(&pev->args[i],
2157 							buf, 128);
2158 			if (ret < 0)
2159 				break;
2160 			pr_info(" %s", buf);
2161 		}
2162 	}
2163 	pr_info(")\n");
2164 	free(place);
2165 	return ret;
2166 }
2167 
2168 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2169 				     struct strfilter *filter)
2170 {
2171 	char tmp[128];
2172 
2173 	/* At first, check the event name itself */
2174 	if (strfilter__compare(filter, tev->event))
2175 		return true;
2176 
2177 	/* Next, check the combination of name and group */
2178 	if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2179 		return false;
2180 	return strfilter__compare(filter, tmp);
2181 }
2182 
2183 static int __show_perf_probe_events(int fd, bool is_kprobe,
2184 				    struct strfilter *filter)
2185 {
2186 	int ret = 0;
2187 	struct probe_trace_event tev;
2188 	struct perf_probe_event pev;
2189 	struct strlist *rawlist;
2190 	struct str_node *ent;
2191 
2192 	memset(&tev, 0, sizeof(tev));
2193 	memset(&pev, 0, sizeof(pev));
2194 
2195 	rawlist = get_probe_trace_command_rawlist(fd);
2196 	if (!rawlist)
2197 		return -ENOMEM;
2198 
2199 	strlist__for_each(ent, rawlist) {
2200 		ret = parse_probe_trace_command(ent->s, &tev);
2201 		if (ret >= 0) {
2202 			if (!filter_probe_trace_event(&tev, filter))
2203 				goto next;
2204 			ret = convert_to_perf_probe_event(&tev, &pev,
2205 								is_kprobe);
2206 			if (ret >= 0)
2207 				ret = show_perf_probe_event(&pev,
2208 							    tev.point.module);
2209 		}
2210 next:
2211 		clear_perf_probe_event(&pev);
2212 		clear_probe_trace_event(&tev);
2213 		if (ret < 0)
2214 			break;
2215 	}
2216 	strlist__delete(rawlist);
2217 
2218 	return ret;
2219 }
2220 
2221 /* List up current perf-probe events */
2222 int show_perf_probe_events(struct strfilter *filter)
2223 {
2224 	int kp_fd, up_fd, ret;
2225 
2226 	setup_pager();
2227 
2228 	ret = init_symbol_maps(false);
2229 	if (ret < 0)
2230 		return ret;
2231 
2232 	kp_fd = open_kprobe_events(false);
2233 	if (kp_fd >= 0) {
2234 		ret = __show_perf_probe_events(kp_fd, true, filter);
2235 		close(kp_fd);
2236 		if (ret < 0)
2237 			goto out;
2238 	}
2239 
2240 	up_fd = open_uprobe_events(false);
2241 	if (kp_fd < 0 && up_fd < 0) {
2242 		print_both_open_warning(kp_fd, up_fd);
2243 		ret = kp_fd;
2244 		goto out;
2245 	}
2246 
2247 	if (up_fd >= 0) {
2248 		ret = __show_perf_probe_events(up_fd, false, filter);
2249 		close(up_fd);
2250 	}
2251 out:
2252 	exit_symbol_maps();
2253 	return ret;
2254 }
2255 
2256 /* Get current perf-probe event names */
2257 static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
2258 {
2259 	char buf[128];
2260 	struct strlist *sl, *rawlist;
2261 	struct str_node *ent;
2262 	struct probe_trace_event tev;
2263 	int ret = 0;
2264 
2265 	memset(&tev, 0, sizeof(tev));
2266 	rawlist = get_probe_trace_command_rawlist(fd);
2267 	if (!rawlist)
2268 		return NULL;
2269 	sl = strlist__new(true, NULL);
2270 	strlist__for_each(ent, rawlist) {
2271 		ret = parse_probe_trace_command(ent->s, &tev);
2272 		if (ret < 0)
2273 			break;
2274 		if (include_group) {
2275 			ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2276 					tev.event);
2277 			if (ret >= 0)
2278 				ret = strlist__add(sl, buf);
2279 		} else
2280 			ret = strlist__add(sl, tev.event);
2281 		clear_probe_trace_event(&tev);
2282 		if (ret < 0)
2283 			break;
2284 	}
2285 	strlist__delete(rawlist);
2286 
2287 	if (ret < 0) {
2288 		strlist__delete(sl);
2289 		return NULL;
2290 	}
2291 	return sl;
2292 }
2293 
2294 static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
2295 {
2296 	int ret = 0;
2297 	char *buf = synthesize_probe_trace_command(tev);
2298 	char sbuf[STRERR_BUFSIZE];
2299 
2300 	if (!buf) {
2301 		pr_debug("Failed to synthesize probe trace event.\n");
2302 		return -EINVAL;
2303 	}
2304 
2305 	pr_debug("Writing event: %s\n", buf);
2306 	if (!probe_event_dry_run) {
2307 		ret = write(fd, buf, strlen(buf));
2308 		if (ret <= 0) {
2309 			ret = -errno;
2310 			pr_warning("Failed to write event: %s\n",
2311 				   strerror_r(errno, sbuf, sizeof(sbuf)));
2312 		}
2313 	}
2314 	free(buf);
2315 	return ret;
2316 }
2317 
2318 static int get_new_event_name(char *buf, size_t len, const char *base,
2319 			      struct strlist *namelist, bool allow_suffix)
2320 {
2321 	int i, ret;
2322 
2323 	if (*base == '.')
2324 		base++;
2325 
2326 	/* Try no suffix */
2327 	ret = e_snprintf(buf, len, "%s", base);
2328 	if (ret < 0) {
2329 		pr_debug("snprintf() failed: %d\n", ret);
2330 		return ret;
2331 	}
2332 	if (!strlist__has_entry(namelist, buf))
2333 		return 0;
2334 
2335 	if (!allow_suffix) {
2336 		pr_warning("Error: event \"%s\" already exists. "
2337 			   "(Use -f to force duplicates.)\n", base);
2338 		return -EEXIST;
2339 	}
2340 
2341 	/* Try to add suffix */
2342 	for (i = 1; i < MAX_EVENT_INDEX; i++) {
2343 		ret = e_snprintf(buf, len, "%s_%d", base, i);
2344 		if (ret < 0) {
2345 			pr_debug("snprintf() failed: %d\n", ret);
2346 			return ret;
2347 		}
2348 		if (!strlist__has_entry(namelist, buf))
2349 			break;
2350 	}
2351 	if (i == MAX_EVENT_INDEX) {
2352 		pr_warning("Too many events are on the same function.\n");
2353 		ret = -ERANGE;
2354 	}
2355 
2356 	return ret;
2357 }
2358 
2359 /* Warn if the current kernel's uprobe implementation is old */
2360 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2361 {
2362 	int i;
2363 	char *buf = synthesize_probe_trace_command(tev);
2364 
2365 	/* Old uprobe event doesn't support memory dereference */
2366 	if (!tev->uprobes || tev->nargs == 0 || !buf)
2367 		goto out;
2368 
2369 	for (i = 0; i < tev->nargs; i++)
2370 		if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2371 			pr_warning("Please upgrade your kernel to at least "
2372 				   "3.14 to have access to feature %s\n",
2373 				   tev->args[i].value);
2374 			break;
2375 		}
2376 out:
2377 	free(buf);
2378 }
2379 
2380 static int __add_probe_trace_events(struct perf_probe_event *pev,
2381 				     struct probe_trace_event *tevs,
2382 				     int ntevs, bool allow_suffix)
2383 {
2384 	int i, fd, ret;
2385 	struct probe_trace_event *tev = NULL;
2386 	char buf[64];
2387 	const char *event, *group;
2388 	struct strlist *namelist;
2389 	LIST_HEAD(blacklist);
2390 	struct kprobe_blacklist_node *node;
2391 
2392 	if (pev->uprobes)
2393 		fd = open_uprobe_events(true);
2394 	else
2395 		fd = open_kprobe_events(true);
2396 
2397 	if (fd < 0) {
2398 		print_open_warning(fd, !pev->uprobes);
2399 		return fd;
2400 	}
2401 
2402 	/* Get current event names */
2403 	namelist = get_probe_trace_event_names(fd, false);
2404 	if (!namelist) {
2405 		pr_debug("Failed to get current event list.\n");
2406 		ret = -ENOMEM;
2407 		goto close_out;
2408 	}
2409 	/* Get kprobe blacklist if exists */
2410 	if (!pev->uprobes) {
2411 		ret = kprobe_blacklist__load(&blacklist);
2412 		if (ret < 0)
2413 			pr_debug("No kprobe blacklist support, ignored\n");
2414 	}
2415 
2416 	ret = 0;
2417 	pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
2418 	for (i = 0; i < ntevs; i++) {
2419 		tev = &tevs[i];
2420 		/* Skip if the symbol is out of .text (marked previously) */
2421 		if (!tev->point.symbol)
2422 			continue;
2423 		/* Ensure that the address is NOT blacklisted */
2424 		node = kprobe_blacklist__find_by_address(&blacklist,
2425 							 tev->point.address);
2426 		if (node) {
2427 			pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2428 			continue;
2429 		}
2430 
2431 		if (pev->event)
2432 			event = pev->event;
2433 		else
2434 			if (pev->point.function)
2435 				event = pev->point.function;
2436 			else
2437 				event = tev->point.symbol;
2438 		if (pev->group)
2439 			group = pev->group;
2440 		else
2441 			group = PERFPROBE_GROUP;
2442 
2443 		/* Get an unused new event name */
2444 		ret = get_new_event_name(buf, 64, event,
2445 					 namelist, allow_suffix);
2446 		if (ret < 0)
2447 			break;
2448 		event = buf;
2449 
2450 		tev->event = strdup(event);
2451 		tev->group = strdup(group);
2452 		if (tev->event == NULL || tev->group == NULL) {
2453 			ret = -ENOMEM;
2454 			break;
2455 		}
2456 		ret = write_probe_trace_event(fd, tev);
2457 		if (ret < 0)
2458 			break;
2459 		/* Add added event name to namelist */
2460 		strlist__add(namelist, event);
2461 
2462 		/* Trick here - save current event/group */
2463 		event = pev->event;
2464 		group = pev->group;
2465 		pev->event = tev->event;
2466 		pev->group = tev->group;
2467 		show_perf_probe_event(pev, tev->point.module);
2468 		/* Trick here - restore current event/group */
2469 		pev->event = (char *)event;
2470 		pev->group = (char *)group;
2471 
2472 		/*
2473 		 * Probes after the first probe which comes from same
2474 		 * user input are always allowed to add suffix, because
2475 		 * there might be several addresses corresponding to
2476 		 * one code line.
2477 		 */
2478 		allow_suffix = true;
2479 	}
2480 	if (ret == -EINVAL && pev->uprobes)
2481 		warn_uprobe_event_compat(tev);
2482 
2483 	/* Note that it is possible to skip all events because of blacklist */
2484 	if (ret >= 0 && tev->event) {
2485 		/* Show how to use the event. */
2486 		pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2487 		pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2488 			 tev->event);
2489 	}
2490 
2491 	kprobe_blacklist__delete(&blacklist);
2492 	strlist__delete(namelist);
2493 close_out:
2494 	close(fd);
2495 	return ret;
2496 }
2497 
2498 static int find_probe_functions(struct map *map, char *name)
2499 {
2500 	int found = 0;
2501 	struct symbol *sym;
2502 
2503 	map__for_each_symbol_by_name(map, name, sym) {
2504 		found++;
2505 	}
2506 
2507 	return found;
2508 }
2509 
2510 #define strdup_or_goto(str, label)	\
2511 	({ char *__p = strdup(str); if (!__p) goto label; __p; })
2512 
2513 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2514 				struct probe_trace_event *tev __maybe_unused,
2515 				struct map *map __maybe_unused) { }
2516 
2517 /*
2518  * Find probe function addresses from map.
2519  * Return an error or the number of found probe_trace_event
2520  */
2521 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2522 					    struct probe_trace_event **tevs,
2523 					    int max_tevs, const char *target)
2524 {
2525 	struct map *map = NULL;
2526 	struct ref_reloc_sym *reloc_sym = NULL;
2527 	struct symbol *sym;
2528 	struct probe_trace_event *tev;
2529 	struct perf_probe_point *pp = &pev->point;
2530 	struct probe_trace_point *tp;
2531 	int num_matched_functions;
2532 	int ret, i;
2533 
2534 	map = get_target_map(target, pev->uprobes);
2535 	if (!map) {
2536 		ret = -EINVAL;
2537 		goto out;
2538 	}
2539 
2540 	/*
2541 	 * Load matched symbols: Since the different local symbols may have
2542 	 * same name but different addresses, this lists all the symbols.
2543 	 */
2544 	num_matched_functions = find_probe_functions(map, pp->function);
2545 	if (num_matched_functions == 0) {
2546 		pr_err("Failed to find symbol %s in %s\n", pp->function,
2547 			target ? : "kernel");
2548 		ret = -ENOENT;
2549 		goto out;
2550 	} else if (num_matched_functions > max_tevs) {
2551 		pr_err("Too many functions matched in %s\n",
2552 			target ? : "kernel");
2553 		ret = -E2BIG;
2554 		goto out;
2555 	}
2556 
2557 	if (!pev->uprobes && !pp->retprobe) {
2558 		reloc_sym = kernel_get_ref_reloc_sym();
2559 		if (!reloc_sym) {
2560 			pr_warning("Relocated base symbol is not found!\n");
2561 			ret = -EINVAL;
2562 			goto out;
2563 		}
2564 	}
2565 
2566 	/* Setup result trace-probe-events */
2567 	*tevs = zalloc(sizeof(*tev) * num_matched_functions);
2568 	if (!*tevs) {
2569 		ret = -ENOMEM;
2570 		goto out;
2571 	}
2572 
2573 	ret = 0;
2574 
2575 	map__for_each_symbol_by_name(map, pp->function, sym) {
2576 		tev = (*tevs) + ret;
2577 		tp = &tev->point;
2578 		if (ret == num_matched_functions) {
2579 			pr_warning("Too many symbols are listed. Skip it.\n");
2580 			break;
2581 		}
2582 		ret++;
2583 
2584 		if (pp->offset > sym->end - sym->start) {
2585 			pr_warning("Offset %ld is bigger than the size of %s\n",
2586 				   pp->offset, sym->name);
2587 			ret = -ENOENT;
2588 			goto err_out;
2589 		}
2590 		/* Add one probe point */
2591 		tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2592 		if (reloc_sym) {
2593 			tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2594 			tp->offset = tp->address - reloc_sym->addr;
2595 		} else {
2596 			tp->symbol = strdup_or_goto(sym->name, nomem_out);
2597 			tp->offset = pp->offset;
2598 		}
2599 		tp->retprobe = pp->retprobe;
2600 		if (target)
2601 			tev->point.module = strdup_or_goto(target, nomem_out);
2602 		tev->uprobes = pev->uprobes;
2603 		tev->nargs = pev->nargs;
2604 		if (tev->nargs) {
2605 			tev->args = zalloc(sizeof(struct probe_trace_arg) *
2606 					   tev->nargs);
2607 			if (tev->args == NULL)
2608 				goto nomem_out;
2609 		}
2610 		for (i = 0; i < tev->nargs; i++) {
2611 			if (pev->args[i].name)
2612 				tev->args[i].name =
2613 					strdup_or_goto(pev->args[i].name,
2614 							nomem_out);
2615 
2616 			tev->args[i].value = strdup_or_goto(pev->args[i].var,
2617 							    nomem_out);
2618 			if (pev->args[i].type)
2619 				tev->args[i].type =
2620 					strdup_or_goto(pev->args[i].type,
2621 							nomem_out);
2622 		}
2623 		arch__fix_tev_from_maps(pev, tev, map);
2624 	}
2625 
2626 out:
2627 	put_target_map(map, pev->uprobes);
2628 	return ret;
2629 
2630 nomem_out:
2631 	ret = -ENOMEM;
2632 err_out:
2633 	clear_probe_trace_events(*tevs, num_matched_functions);
2634 	zfree(tevs);
2635 	goto out;
2636 }
2637 
2638 bool __weak arch__prefers_symtab(void) { return false; }
2639 
2640 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2641 					  struct probe_trace_event **tevs,
2642 					  int max_tevs, const char *target)
2643 {
2644 	int ret;
2645 
2646 	if (pev->uprobes && !pev->group) {
2647 		/* Replace group name if not given */
2648 		ret = convert_exec_to_group(target, &pev->group);
2649 		if (ret != 0) {
2650 			pr_warning("Failed to make a group name.\n");
2651 			return ret;
2652 		}
2653 	}
2654 
2655 	if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2656 		ret = find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
2657 		if (ret > 0)
2658 			return ret; /* Found in symbol table */
2659 	}
2660 
2661 	/* Convert perf_probe_event with debuginfo */
2662 	ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
2663 	if (ret != 0)
2664 		return ret;	/* Found in debuginfo or got an error */
2665 
2666 	return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
2667 }
2668 
2669 struct __event_package {
2670 	struct perf_probe_event		*pev;
2671 	struct probe_trace_event	*tevs;
2672 	int				ntevs;
2673 };
2674 
2675 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
2676 			  int max_tevs, bool force_add)
2677 {
2678 	int i, j, ret;
2679 	struct __event_package *pkgs;
2680 
2681 	ret = 0;
2682 	pkgs = zalloc(sizeof(struct __event_package) * npevs);
2683 
2684 	if (pkgs == NULL)
2685 		return -ENOMEM;
2686 
2687 	ret = init_symbol_maps(pevs->uprobes);
2688 	if (ret < 0) {
2689 		free(pkgs);
2690 		return ret;
2691 	}
2692 
2693 	/* Loop 1: convert all events */
2694 	for (i = 0; i < npevs; i++) {
2695 		pkgs[i].pev = &pevs[i];
2696 		/* Convert with or without debuginfo */
2697 		ret  = convert_to_probe_trace_events(pkgs[i].pev,
2698 						     &pkgs[i].tevs,
2699 						     max_tevs,
2700 						     pkgs[i].pev->target);
2701 		if (ret < 0)
2702 			goto end;
2703 		pkgs[i].ntevs = ret;
2704 	}
2705 
2706 	/* Loop 2: add all events */
2707 	for (i = 0; i < npevs; i++) {
2708 		ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
2709 						pkgs[i].ntevs, force_add);
2710 		if (ret < 0)
2711 			break;
2712 	}
2713 end:
2714 	/* Loop 3: cleanup and free trace events  */
2715 	for (i = 0; i < npevs; i++) {
2716 		for (j = 0; j < pkgs[i].ntevs; j++)
2717 			clear_probe_trace_event(&pkgs[i].tevs[j]);
2718 		zfree(&pkgs[i].tevs);
2719 	}
2720 	free(pkgs);
2721 	exit_symbol_maps();
2722 
2723 	return ret;
2724 }
2725 
2726 static int __del_trace_probe_event(int fd, struct str_node *ent)
2727 {
2728 	char *p;
2729 	char buf[128];
2730 	int ret;
2731 
2732 	/* Convert from perf-probe event to trace-probe event */
2733 	ret = e_snprintf(buf, 128, "-:%s", ent->s);
2734 	if (ret < 0)
2735 		goto error;
2736 
2737 	p = strchr(buf + 2, ':');
2738 	if (!p) {
2739 		pr_debug("Internal error: %s should have ':' but not.\n",
2740 			 ent->s);
2741 		ret = -ENOTSUP;
2742 		goto error;
2743 	}
2744 	*p = '/';
2745 
2746 	pr_debug("Writing event: %s\n", buf);
2747 	ret = write(fd, buf, strlen(buf));
2748 	if (ret < 0) {
2749 		ret = -errno;
2750 		goto error;
2751 	}
2752 
2753 	pr_info("Removed event: %s\n", ent->s);
2754 	return 0;
2755 error:
2756 	pr_warning("Failed to delete event: %s\n",
2757 		   strerror_r(-ret, buf, sizeof(buf)));
2758 	return ret;
2759 }
2760 
2761 static int del_trace_probe_events(int fd, struct strfilter *filter,
2762 				  struct strlist *namelist)
2763 {
2764 	struct str_node *ent;
2765 	const char *p;
2766 	int ret = -ENOENT;
2767 
2768 	if (!namelist)
2769 		return -ENOENT;
2770 
2771 	strlist__for_each(ent, namelist) {
2772 		p = strchr(ent->s, ':');
2773 		if ((p && strfilter__compare(filter, p + 1)) ||
2774 		    strfilter__compare(filter, ent->s)) {
2775 			ret = __del_trace_probe_event(fd, ent);
2776 			if (ret < 0)
2777 				break;
2778 		}
2779 	}
2780 
2781 	return ret;
2782 }
2783 
2784 int del_perf_probe_events(struct strfilter *filter)
2785 {
2786 	int ret, ret2, ufd = -1, kfd = -1;
2787 	struct strlist *namelist = NULL, *unamelist = NULL;
2788 	char *str = strfilter__string(filter);
2789 
2790 	if (!str)
2791 		return -EINVAL;
2792 
2793 	pr_debug("Delete filter: \'%s\'\n", str);
2794 
2795 	/* Get current event names */
2796 	kfd = open_kprobe_events(true);
2797 	if (kfd >= 0)
2798 		namelist = get_probe_trace_event_names(kfd, true);
2799 
2800 	ufd = open_uprobe_events(true);
2801 	if (ufd >= 0)
2802 		unamelist = get_probe_trace_event_names(ufd, true);
2803 
2804 	if (kfd < 0 && ufd < 0) {
2805 		print_both_open_warning(kfd, ufd);
2806 		ret = kfd;
2807 		goto error;
2808 	}
2809 
2810 	ret = del_trace_probe_events(kfd, filter, namelist);
2811 	if (ret < 0 && ret != -ENOENT)
2812 		goto error;
2813 
2814 	ret2 = del_trace_probe_events(ufd, filter, unamelist);
2815 	if (ret2 < 0 && ret2 != -ENOENT)
2816 		ret = ret2;
2817 	else if (ret == -ENOENT && ret2 == -ENOENT) {
2818 		pr_debug("\"%s\" does not hit any event.\n", str);
2819 		/* Note that this is silently ignored */
2820 		ret = 0;
2821 	}
2822 
2823 error:
2824 	if (kfd >= 0) {
2825 		strlist__delete(namelist);
2826 		close(kfd);
2827 	}
2828 
2829 	if (ufd >= 0) {
2830 		strlist__delete(unamelist);
2831 		close(ufd);
2832 	}
2833 	free(str);
2834 
2835 	return ret;
2836 }
2837 
2838 /* TODO: don't use a global variable for filter ... */
2839 static struct strfilter *available_func_filter;
2840 
2841 /*
2842  * If a symbol corresponds to a function with global binding and
2843  * matches filter return 0. For all others return 1.
2844  */
2845 static int filter_available_functions(struct map *map __maybe_unused,
2846 				      struct symbol *sym)
2847 {
2848 	if (strfilter__compare(available_func_filter, sym->name))
2849 		return 0;
2850 	return 1;
2851 }
2852 
2853 int show_available_funcs(const char *target, struct strfilter *_filter,
2854 					bool user)
2855 {
2856 	struct map *map;
2857 	int ret;
2858 
2859 	ret = init_symbol_maps(user);
2860 	if (ret < 0)
2861 		return ret;
2862 
2863 	/* Get a symbol map */
2864 	if (user)
2865 		map = dso__new_map(target);
2866 	else
2867 		map = kernel_get_module_map(target);
2868 	if (!map) {
2869 		pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2870 		return -EINVAL;
2871 	}
2872 
2873 	/* Load symbols with given filter */
2874 	available_func_filter = _filter;
2875 	if (map__load(map, filter_available_functions)) {
2876 		pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2877 		goto end;
2878 	}
2879 	if (!dso__sorted_by_name(map->dso, map->type))
2880 		dso__sort_by_name(map->dso, map->type);
2881 
2882 	/* Show all (filtered) symbols */
2883 	setup_pager();
2884 	dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2885 end:
2886 	if (user) {
2887 		dso__delete(map->dso);
2888 		map__delete(map);
2889 	}
2890 	exit_symbol_maps();
2891 
2892 	return ret;
2893 }
2894 
2895