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