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