• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * probe-event.c : perf-probe definition to probe_events format converter
4  *
5  * Written by Masami Hiramatsu <mhiramat@redhat.com>
6  */
7 
8 #include <inttypes.h>
9 #include <sys/utsname.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <errno.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdarg.h>
19 #include <limits.h>
20 #include <elf.h>
21 
22 #include "build-id.h"
23 #include "event.h"
24 #include "namespaces.h"
25 #include "strlist.h"
26 #include "strfilter.h"
27 #include "debug.h"
28 #include "dso.h"
29 #include "color.h"
30 #include "map.h"
31 #include "maps.h"
32 #include "symbol.h"
33 #include <api/fs/fs.h>
34 #include "trace-event.h"	/* For __maybe_unused */
35 #include "probe-event.h"
36 #include "probe-finder.h"
37 #include "probe-file.h"
38 #include "session.h"
39 #include "string2.h"
40 #include "strbuf.h"
41 
42 #include <subcmd/pager.h>
43 #include <linux/ctype.h>
44 #include <linux/zalloc.h>
45 
46 #ifdef HAVE_DEBUGINFOD_SUPPORT
47 #include <elfutils/debuginfod.h>
48 #endif
49 
50 #define PERFPROBE_GROUP "probe"
51 
52 bool probe_event_dry_run;	/* Dry run flag */
53 struct probe_conf probe_conf = { .magic_num = DEFAULT_PROBE_MAGIC_NUM };
54 
55 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
56 
e_snprintf(char * str,size_t size,const char * format,...)57 int e_snprintf(char *str, size_t size, const char *format, ...)
58 {
59 	int ret;
60 	va_list ap;
61 	va_start(ap, format);
62 	ret = vsnprintf(str, size, format, ap);
63 	va_end(ap);
64 	if (ret >= (int)size)
65 		ret = -E2BIG;
66 	return ret;
67 }
68 
69 static struct machine *host_machine;
70 
71 /* Initialize symbol maps and path of vmlinux/modules */
init_probe_symbol_maps(bool user_only)72 int init_probe_symbol_maps(bool user_only)
73 {
74 	int ret;
75 
76 	symbol_conf.sort_by_name = true;
77 	symbol_conf.allow_aliases = true;
78 	ret = symbol__init(NULL);
79 	if (ret < 0) {
80 		pr_debug("Failed to init symbol map.\n");
81 		goto out;
82 	}
83 
84 	if (host_machine || user_only)	/* already initialized */
85 		return 0;
86 
87 	if (symbol_conf.vmlinux_name)
88 		pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
89 
90 	host_machine = machine__new_host();
91 	if (!host_machine) {
92 		pr_debug("machine__new_host() failed.\n");
93 		symbol__exit();
94 		ret = -1;
95 	}
96 out:
97 	if (ret < 0)
98 		pr_warning("Failed to init vmlinux path.\n");
99 	return ret;
100 }
101 
exit_probe_symbol_maps(void)102 void exit_probe_symbol_maps(void)
103 {
104 	machine__delete(host_machine);
105 	host_machine = NULL;
106 	symbol__exit();
107 }
108 
kernel_get_ref_reloc_sym(struct map ** pmap)109 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(struct map **pmap)
110 {
111 	/* kmap->ref_reloc_sym should be set if host_machine is initialized */
112 	struct kmap *kmap;
113 	struct map *map = machine__kernel_map(host_machine);
114 
115 	if (map__load(map) < 0)
116 		return NULL;
117 
118 	kmap = map__kmap(map);
119 	if (!kmap)
120 		return NULL;
121 
122 	if (pmap)
123 		*pmap = map;
124 
125 	return kmap->ref_reloc_sym;
126 }
127 
kernel_get_symbol_address_by_name(const char * name,u64 * addr,bool reloc,bool reladdr)128 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
129 					     bool reloc, bool reladdr)
130 {
131 	struct ref_reloc_sym *reloc_sym;
132 	struct symbol *sym;
133 	struct map *map;
134 
135 	/* ref_reloc_sym is just a label. Need a special fix*/
136 	reloc_sym = kernel_get_ref_reloc_sym(&map);
137 	if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
138 		*addr = (!map->reloc || reloc) ? reloc_sym->addr :
139 			reloc_sym->unrelocated_addr;
140 	else {
141 		sym = machine__find_kernel_symbol_by_name(host_machine, name, &map);
142 		if (!sym)
143 			return -ENOENT;
144 		*addr = map->unmap_ip(map, sym->start) -
145 			((reloc) ? 0 : map->reloc) -
146 			((reladdr) ? map->start : 0);
147 	}
148 	return 0;
149 }
150 
kernel_get_module_map(const char * module)151 static struct map *kernel_get_module_map(const char *module)
152 {
153 	struct maps *maps = machine__kernel_maps(host_machine);
154 	struct map *pos;
155 
156 	/* A file path -- this is an offline module */
157 	if (module && strchr(module, '/'))
158 		return dso__new_map(module);
159 
160 	if (!module) {
161 		pos = machine__kernel_map(host_machine);
162 		return map__get(pos);
163 	}
164 
165 	maps__for_each_entry(maps, pos) {
166 		/* short_name is "[module]" */
167 		if (strncmp(pos->dso->short_name + 1, module,
168 			    pos->dso->short_name_len - 2) == 0 &&
169 		    module[pos->dso->short_name_len - 2] == '\0') {
170 			return map__get(pos);
171 		}
172 	}
173 	return NULL;
174 }
175 
get_target_map(const char * target,struct nsinfo * nsi,bool user)176 struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user)
177 {
178 	/* Init maps of given executable or kernel */
179 	if (user) {
180 		struct map *map;
181 
182 		map = dso__new_map(target);
183 		if (map && map->dso) {
184 			nsinfo__put(map->dso->nsinfo);
185 			map->dso->nsinfo = nsinfo__get(nsi);
186 		}
187 		return map;
188 	} else {
189 		return kernel_get_module_map(target);
190 	}
191 }
192 
convert_exec_to_group(const char * exec,char ** result)193 static int convert_exec_to_group(const char *exec, char **result)
194 {
195 	char *ptr1, *ptr2, *exec_copy;
196 	char buf[64];
197 	int ret;
198 
199 	exec_copy = strdup(exec);
200 	if (!exec_copy)
201 		return -ENOMEM;
202 
203 	ptr1 = basename(exec_copy);
204 	if (!ptr1) {
205 		ret = -EINVAL;
206 		goto out;
207 	}
208 
209 	for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
210 		if (!isalnum(*ptr2) && *ptr2 != '_') {
211 			*ptr2 = '\0';
212 			break;
213 		}
214 	}
215 
216 	ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
217 	if (ret < 0)
218 		goto out;
219 
220 	*result = strdup(buf);
221 	ret = *result ? 0 : -ENOMEM;
222 
223 out:
224 	free(exec_copy);
225 	return ret;
226 }
227 
clear_perf_probe_point(struct perf_probe_point * pp)228 static void clear_perf_probe_point(struct perf_probe_point *pp)
229 {
230 	zfree(&pp->file);
231 	zfree(&pp->function);
232 	zfree(&pp->lazy_line);
233 }
234 
clear_probe_trace_events(struct probe_trace_event * tevs,int ntevs)235 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
236 {
237 	int i;
238 
239 	for (i = 0; i < ntevs; i++)
240 		clear_probe_trace_event(tevs + i);
241 }
242 
243 static bool kprobe_blacklist__listed(unsigned long address);
kprobe_warn_out_range(const char * symbol,unsigned long address)244 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
245 {
246 	struct map *map;
247 	bool ret = false;
248 
249 	map = kernel_get_module_map(NULL);
250 	if (map) {
251 		ret = address <= map->start || map->end < address;
252 		if (ret)
253 			pr_warning("%s is out of .text, skip it.\n", symbol);
254 		map__put(map);
255 	}
256 	if (!ret && kprobe_blacklist__listed(address)) {
257 		pr_warning("%s is blacklisted function, skip it.\n", symbol);
258 		ret = true;
259 	}
260 
261 	return ret;
262 }
263 
264 /*
265  * @module can be module name of module file path. In case of path,
266  * inspect elf and find out what is actual module name.
267  * Caller has to free mod_name after using it.
268  */
find_module_name(const char * module)269 static char *find_module_name(const char *module)
270 {
271 	int fd;
272 	Elf *elf;
273 	GElf_Ehdr ehdr;
274 	GElf_Shdr shdr;
275 	Elf_Data *data;
276 	Elf_Scn *sec;
277 	char *mod_name = NULL;
278 	int name_offset;
279 
280 	fd = open(module, O_RDONLY);
281 	if (fd < 0)
282 		return NULL;
283 
284 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
285 	if (elf == NULL)
286 		goto elf_err;
287 
288 	if (gelf_getehdr(elf, &ehdr) == NULL)
289 		goto ret_err;
290 
291 	sec = elf_section_by_name(elf, &ehdr, &shdr,
292 			".gnu.linkonce.this_module", NULL);
293 	if (!sec)
294 		goto ret_err;
295 
296 	data = elf_getdata(sec, NULL);
297 	if (!data || !data->d_buf)
298 		goto ret_err;
299 
300 	/*
301 	 * NOTE:
302 	 * '.gnu.linkonce.this_module' section of kernel module elf directly
303 	 * maps to 'struct module' from linux/module.h. This section contains
304 	 * actual module name which will be used by kernel after loading it.
305 	 * But, we cannot use 'struct module' here since linux/module.h is not
306 	 * exposed to user-space. Offset of 'name' has remained same from long
307 	 * time, so hardcoding it here.
308 	 */
309 	if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
310 		name_offset = 12;
311 	else	/* expect ELFCLASS64 by default */
312 		name_offset = 24;
313 
314 	mod_name = strdup((char *)data->d_buf + name_offset);
315 
316 ret_err:
317 	elf_end(elf);
318 elf_err:
319 	close(fd);
320 	return mod_name;
321 }
322 
323 #ifdef HAVE_DWARF_SUPPORT
324 
kernel_get_module_dso(const char * module,struct dso ** pdso)325 static int kernel_get_module_dso(const char *module, struct dso **pdso)
326 {
327 	struct dso *dso;
328 	struct map *map;
329 	const char *vmlinux_name;
330 	int ret = 0;
331 
332 	if (module) {
333 		char module_name[128];
334 
335 		snprintf(module_name, sizeof(module_name), "[%s]", module);
336 		map = maps__find_by_name(&host_machine->kmaps, module_name);
337 		if (map) {
338 			dso = map->dso;
339 			goto found;
340 		}
341 		pr_debug("Failed to find module %s.\n", module);
342 		return -ENOENT;
343 	}
344 
345 	map = machine__kernel_map(host_machine);
346 	dso = map->dso;
347 	if (!dso->has_build_id)
348 		dso__read_running_kernel_build_id(dso, host_machine);
349 
350 	vmlinux_name = symbol_conf.vmlinux_name;
351 	dso->load_errno = 0;
352 	if (vmlinux_name)
353 		ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
354 	else
355 		ret = dso__load_vmlinux_path(dso, map);
356 found:
357 	*pdso = dso;
358 	return ret;
359 }
360 
361 /*
362  * Some binaries like glibc have special symbols which are on the symbol
363  * table, but not in the debuginfo. If we can find the address of the
364  * symbol from map, we can translate the address back to the probe point.
365  */
find_alternative_probe_point(struct debuginfo * dinfo,struct perf_probe_point * pp,struct perf_probe_point * result,const char * target,struct nsinfo * nsi,bool uprobes)366 static int find_alternative_probe_point(struct debuginfo *dinfo,
367 					struct perf_probe_point *pp,
368 					struct perf_probe_point *result,
369 					const char *target, struct nsinfo *nsi,
370 					bool uprobes)
371 {
372 	struct map *map = NULL;
373 	struct symbol *sym;
374 	u64 address = 0;
375 	int ret = -ENOENT;
376 
377 	/* This can work only for function-name based one */
378 	if (!pp->function || pp->file)
379 		return -ENOTSUP;
380 
381 	map = get_target_map(target, nsi, uprobes);
382 	if (!map)
383 		return -EINVAL;
384 
385 	/* Find the address of given function */
386 	map__for_each_symbol_by_name(map, pp->function, sym) {
387 		if (uprobes) {
388 			address = sym->start;
389 			if (sym->type == STT_GNU_IFUNC)
390 				pr_warning("Warning: The probe function (%s) is a GNU indirect function.\n"
391 					   "Consider identifying the final function used at run time and set the probe directly on that.\n",
392 					   pp->function);
393 		} else
394 			address = map->unmap_ip(map, sym->start) - map->reloc;
395 		break;
396 	}
397 	if (!address) {
398 		ret = -ENOENT;
399 		goto out;
400 	}
401 	pr_debug("Symbol %s address found : %" PRIx64 "\n",
402 			pp->function, address);
403 
404 	ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
405 					  result);
406 	if (ret <= 0)
407 		ret = (!ret) ? -ENOENT : ret;
408 	else {
409 		result->offset += pp->offset;
410 		result->line += pp->line;
411 		result->retprobe = pp->retprobe;
412 		ret = 0;
413 	}
414 
415 out:
416 	map__put(map);
417 	return ret;
418 
419 }
420 
get_alternative_probe_event(struct debuginfo * dinfo,struct perf_probe_event * pev,struct perf_probe_point * tmp)421 static int get_alternative_probe_event(struct debuginfo *dinfo,
422 				       struct perf_probe_event *pev,
423 				       struct perf_probe_point *tmp)
424 {
425 	int ret;
426 
427 	memcpy(tmp, &pev->point, sizeof(*tmp));
428 	memset(&pev->point, 0, sizeof(pev->point));
429 	ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target,
430 					   pev->nsi, pev->uprobes);
431 	if (ret < 0)
432 		memcpy(&pev->point, tmp, sizeof(*tmp));
433 
434 	return ret;
435 }
436 
get_alternative_line_range(struct debuginfo * dinfo,struct line_range * lr,const char * target,bool user)437 static int get_alternative_line_range(struct debuginfo *dinfo,
438 				      struct line_range *lr,
439 				      const char *target, bool user)
440 {
441 	struct perf_probe_point pp = { .function = lr->function,
442 				       .file = lr->file,
443 				       .line = lr->start };
444 	struct perf_probe_point result;
445 	int ret, len = 0;
446 
447 	memset(&result, 0, sizeof(result));
448 
449 	if (lr->end != INT_MAX)
450 		len = lr->end - lr->start;
451 	ret = find_alternative_probe_point(dinfo, &pp, &result,
452 					   target, NULL, user);
453 	if (!ret) {
454 		lr->function = result.function;
455 		lr->file = result.file;
456 		lr->start = result.line;
457 		if (lr->end != INT_MAX)
458 			lr->end = lr->start + len;
459 		clear_perf_probe_point(&pp);
460 	}
461 	return ret;
462 }
463 
464 #ifdef HAVE_DEBUGINFOD_SUPPORT
open_from_debuginfod(struct dso * dso,struct nsinfo * nsi,bool silent)465 static struct debuginfo *open_from_debuginfod(struct dso *dso, struct nsinfo *nsi,
466 					      bool silent)
467 {
468 	debuginfod_client *c = debuginfod_begin();
469 	char sbuild_id[SBUILD_ID_SIZE + 1];
470 	struct debuginfo *ret = NULL;
471 	struct nscookie nsc;
472 	char *path;
473 	int fd;
474 
475 	if (!c)
476 		return NULL;
477 
478 	build_id__sprintf(&dso->bid, sbuild_id);
479 	fd = debuginfod_find_debuginfo(c, (const unsigned char *)sbuild_id,
480 					0, &path);
481 	if (fd >= 0)
482 		close(fd);
483 	debuginfod_end(c);
484 	if (fd < 0) {
485 		if (!silent)
486 			pr_debug("Failed to find debuginfo in debuginfod.\n");
487 		return NULL;
488 	}
489 	if (!silent)
490 		pr_debug("Load debuginfo from debuginfod (%s)\n", path);
491 
492 	nsinfo__mountns_enter(nsi, &nsc);
493 	ret = debuginfo__new((const char *)path);
494 	nsinfo__mountns_exit(&nsc);
495 	return ret;
496 }
497 #else
498 static inline
open_from_debuginfod(struct dso * dso __maybe_unused,struct nsinfo * nsi __maybe_unused,bool silent __maybe_unused)499 struct debuginfo *open_from_debuginfod(struct dso *dso __maybe_unused,
500 				       struct nsinfo *nsi __maybe_unused,
501 				       bool silent __maybe_unused)
502 {
503 	return NULL;
504 }
505 #endif
506 
507 /* Open new debuginfo of given module */
open_debuginfo(const char * module,struct nsinfo * nsi,bool silent)508 static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi,
509 					bool silent)
510 {
511 	const char *path = module;
512 	char reason[STRERR_BUFSIZE];
513 	struct debuginfo *ret = NULL;
514 	struct dso *dso = NULL;
515 	struct nscookie nsc;
516 	int err;
517 
518 	if (!module || !strchr(module, '/')) {
519 		err = kernel_get_module_dso(module, &dso);
520 		if (err < 0) {
521 			if (!dso || dso->load_errno == 0) {
522 				if (!str_error_r(-err, reason, STRERR_BUFSIZE))
523 					strcpy(reason, "(unknown)");
524 			} else
525 				dso__strerror_load(dso, reason, STRERR_BUFSIZE);
526 			if (dso)
527 				ret = open_from_debuginfod(dso, nsi, silent);
528 			if (ret)
529 				return ret;
530 			if (!silent) {
531 				if (module)
532 					pr_err("Module %s is not loaded, please specify its full path name.\n", module);
533 				else
534 					pr_err("Failed to find the path for the kernel: %s\n", reason);
535 			}
536 			return NULL;
537 		}
538 		path = dso->long_name;
539 	}
540 	nsinfo__mountns_enter(nsi, &nsc);
541 	ret = debuginfo__new(path);
542 	if (!ret && !silent) {
543 		pr_warning("The %s file has no debug information.\n", path);
544 		if (!module || !strtailcmp(path, ".ko"))
545 			pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
546 		else
547 			pr_warning("Rebuild with -g, ");
548 		pr_warning("or install an appropriate debuginfo package.\n");
549 	}
550 	nsinfo__mountns_exit(&nsc);
551 	return ret;
552 }
553 
554 /* For caching the last debuginfo */
555 static struct debuginfo *debuginfo_cache;
556 static char *debuginfo_cache_path;
557 
debuginfo_cache__open(const char * module,bool silent)558 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
559 {
560 	const char *path = module;
561 
562 	/* If the module is NULL, it should be the kernel. */
563 	if (!module)
564 		path = "kernel";
565 
566 	if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
567 		goto out;
568 
569 	/* Copy module path */
570 	free(debuginfo_cache_path);
571 	debuginfo_cache_path = strdup(path);
572 	if (!debuginfo_cache_path) {
573 		debuginfo__delete(debuginfo_cache);
574 		debuginfo_cache = NULL;
575 		goto out;
576 	}
577 
578 	debuginfo_cache = open_debuginfo(module, NULL, silent);
579 	if (!debuginfo_cache)
580 		zfree(&debuginfo_cache_path);
581 out:
582 	return debuginfo_cache;
583 }
584 
debuginfo_cache__exit(void)585 static void debuginfo_cache__exit(void)
586 {
587 	debuginfo__delete(debuginfo_cache);
588 	debuginfo_cache = NULL;
589 	zfree(&debuginfo_cache_path);
590 }
591 
592 
get_text_start_address(const char * exec,unsigned long * address,struct nsinfo * nsi)593 static int get_text_start_address(const char *exec, unsigned long *address,
594 				  struct nsinfo *nsi)
595 {
596 	Elf *elf;
597 	GElf_Ehdr ehdr;
598 	GElf_Shdr shdr;
599 	int fd, ret = -ENOENT;
600 	struct nscookie nsc;
601 
602 	nsinfo__mountns_enter(nsi, &nsc);
603 	fd = open(exec, O_RDONLY);
604 	nsinfo__mountns_exit(&nsc);
605 	if (fd < 0)
606 		return -errno;
607 
608 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
609 	if (elf == NULL) {
610 		ret = -EINVAL;
611 		goto out_close;
612 	}
613 
614 	if (gelf_getehdr(elf, &ehdr) == NULL)
615 		goto out;
616 
617 	if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
618 		goto out;
619 
620 	*address = shdr.sh_addr - shdr.sh_offset;
621 	ret = 0;
622 out:
623 	elf_end(elf);
624 out_close:
625 	close(fd);
626 
627 	return ret;
628 }
629 
630 /*
631  * Convert trace point to probe point with debuginfo
632  */
find_perf_probe_point_from_dwarf(struct probe_trace_point * tp,struct perf_probe_point * pp,bool is_kprobe)633 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
634 					    struct perf_probe_point *pp,
635 					    bool is_kprobe)
636 {
637 	struct debuginfo *dinfo = NULL;
638 	unsigned long stext = 0;
639 	u64 addr = tp->address;
640 	int ret = -ENOENT;
641 
642 	/* convert the address to dwarf address */
643 	if (!is_kprobe) {
644 		if (!addr) {
645 			ret = -EINVAL;
646 			goto error;
647 		}
648 		ret = get_text_start_address(tp->module, &stext, NULL);
649 		if (ret < 0)
650 			goto error;
651 		addr += stext;
652 	} else if (tp->symbol) {
653 		/* If the module is given, this returns relative address */
654 		ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
655 							false, !!tp->module);
656 		if (ret != 0)
657 			goto error;
658 		addr += tp->offset;
659 	}
660 
661 	pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
662 		 tp->module ? : "kernel");
663 
664 	dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
665 	if (dinfo)
666 		ret = debuginfo__find_probe_point(dinfo,
667 						 (unsigned long)addr, pp);
668 	else
669 		ret = -ENOENT;
670 
671 	if (ret > 0) {
672 		pp->retprobe = tp->retprobe;
673 		return 0;
674 	}
675 error:
676 	pr_debug("Failed to find corresponding probes from debuginfo.\n");
677 	return ret ? : -ENOENT;
678 }
679 
680 /* Adjust symbol name and address */
post_process_probe_trace_point(struct probe_trace_point * tp,struct map * map,unsigned long offs)681 static int post_process_probe_trace_point(struct probe_trace_point *tp,
682 					   struct map *map, unsigned long offs)
683 {
684 	struct symbol *sym;
685 	u64 addr = tp->address - offs;
686 
687 	sym = map__find_symbol(map, addr);
688 	if (!sym)
689 		return -ENOENT;
690 
691 	if (strcmp(sym->name, tp->symbol)) {
692 		/* If we have no realname, use symbol for it */
693 		if (!tp->realname)
694 			tp->realname = tp->symbol;
695 		else
696 			free(tp->symbol);
697 		tp->symbol = strdup(sym->name);
698 		if (!tp->symbol)
699 			return -ENOMEM;
700 	}
701 	tp->offset = addr - sym->start;
702 	tp->address -= offs;
703 
704 	return 0;
705 }
706 
707 /*
708  * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
709  * and generate new symbols with suffixes such as .constprop.N or .isra.N
710  * etc. Since those symbols are not recorded in DWARF, we have to find
711  * correct generated symbols from offline ELF binary.
712  * For online kernel or uprobes we don't need this because those are
713  * rebased on _text, or already a section relative address.
714  */
715 static int
post_process_offline_probe_trace_events(struct probe_trace_event * tevs,int ntevs,const char * pathname)716 post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
717 					int ntevs, const char *pathname)
718 {
719 	struct map *map;
720 	unsigned long stext = 0;
721 	int i, ret = 0;
722 
723 	/* Prepare a map for offline binary */
724 	map = dso__new_map(pathname);
725 	if (!map || get_text_start_address(pathname, &stext, NULL) < 0) {
726 		pr_warning("Failed to get ELF symbols for %s\n", pathname);
727 		return -EINVAL;
728 	}
729 
730 	for (i = 0; i < ntevs; i++) {
731 		ret = post_process_probe_trace_point(&tevs[i].point,
732 						     map, stext);
733 		if (ret < 0)
734 			break;
735 	}
736 	map__put(map);
737 
738 	return ret;
739 }
740 
add_exec_to_probe_trace_events(struct probe_trace_event * tevs,int ntevs,const char * exec,struct nsinfo * nsi)741 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
742 					  int ntevs, const char *exec,
743 					  struct nsinfo *nsi)
744 {
745 	int i, ret = 0;
746 	unsigned long stext = 0;
747 
748 	if (!exec)
749 		return 0;
750 
751 	ret = get_text_start_address(exec, &stext, nsi);
752 	if (ret < 0)
753 		return ret;
754 
755 	for (i = 0; i < ntevs && ret >= 0; i++) {
756 		/* point.address is the address of point.symbol + point.offset */
757 		tevs[i].point.address -= stext;
758 		tevs[i].point.module = strdup(exec);
759 		if (!tevs[i].point.module) {
760 			ret = -ENOMEM;
761 			break;
762 		}
763 		tevs[i].uprobes = true;
764 	}
765 
766 	return ret;
767 }
768 
769 static int
post_process_module_probe_trace_events(struct probe_trace_event * tevs,int ntevs,const char * module,struct debuginfo * dinfo)770 post_process_module_probe_trace_events(struct probe_trace_event *tevs,
771 				       int ntevs, const char *module,
772 				       struct debuginfo *dinfo)
773 {
774 	Dwarf_Addr text_offs = 0;
775 	int i, ret = 0;
776 	char *mod_name = NULL;
777 	struct map *map;
778 
779 	if (!module)
780 		return 0;
781 
782 	map = get_target_map(module, NULL, false);
783 	if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
784 		pr_warning("Failed to get ELF symbols for %s\n", module);
785 		return -EINVAL;
786 	}
787 
788 	mod_name = find_module_name(module);
789 	for (i = 0; i < ntevs; i++) {
790 		ret = post_process_probe_trace_point(&tevs[i].point,
791 						map, (unsigned long)text_offs);
792 		if (ret < 0)
793 			break;
794 		tevs[i].point.module =
795 			strdup(mod_name ? mod_name : module);
796 		if (!tevs[i].point.module) {
797 			ret = -ENOMEM;
798 			break;
799 		}
800 	}
801 
802 	free(mod_name);
803 	map__put(map);
804 
805 	return ret;
806 }
807 
808 static int
post_process_kernel_probe_trace_events(struct probe_trace_event * tevs,int ntevs)809 post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
810 				       int ntevs)
811 {
812 	struct ref_reloc_sym *reloc_sym;
813 	struct map *map;
814 	char *tmp;
815 	int i, skipped = 0;
816 
817 	/* Skip post process if the target is an offline kernel */
818 	if (symbol_conf.ignore_vmlinux_buildid)
819 		return post_process_offline_probe_trace_events(tevs, ntevs,
820 						symbol_conf.vmlinux_name);
821 
822 	reloc_sym = kernel_get_ref_reloc_sym(&map);
823 	if (!reloc_sym) {
824 		pr_warning("Relocated base symbol is not found!\n");
825 		return -EINVAL;
826 	}
827 
828 	for (i = 0; i < ntevs; i++) {
829 		if (!tevs[i].point.address)
830 			continue;
831 		if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
832 			continue;
833 		/*
834 		 * If we found a wrong one, mark it by NULL symbol.
835 		 * Since addresses in debuginfo is same as objdump, we need
836 		 * to convert it to addresses on memory.
837 		 */
838 		if (kprobe_warn_out_range(tevs[i].point.symbol,
839 			map__objdump_2mem(map, tevs[i].point.address))) {
840 			tmp = NULL;
841 			skipped++;
842 		} else {
843 			tmp = strdup(reloc_sym->name);
844 			if (!tmp)
845 				return -ENOMEM;
846 		}
847 		/* If we have no realname, use symbol for it */
848 		if (!tevs[i].point.realname)
849 			tevs[i].point.realname = tevs[i].point.symbol;
850 		else
851 			free(tevs[i].point.symbol);
852 		tevs[i].point.symbol = tmp;
853 		tevs[i].point.offset = tevs[i].point.address -
854 			(map->reloc ? reloc_sym->unrelocated_addr :
855 				      reloc_sym->addr);
856 	}
857 	return skipped;
858 }
859 
860 void __weak
arch__post_process_probe_trace_events(struct perf_probe_event * pev __maybe_unused,int ntevs __maybe_unused)861 arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
862 				      int ntevs __maybe_unused)
863 {
864 }
865 
866 /* Post processing the probe events */
post_process_probe_trace_events(struct perf_probe_event * pev,struct probe_trace_event * tevs,int ntevs,const char * module,bool uprobe,struct debuginfo * dinfo)867 static int post_process_probe_trace_events(struct perf_probe_event *pev,
868 					   struct probe_trace_event *tevs,
869 					   int ntevs, const char *module,
870 					   bool uprobe, struct debuginfo *dinfo)
871 {
872 	int ret;
873 
874 	if (uprobe)
875 		ret = add_exec_to_probe_trace_events(tevs, ntevs, module,
876 						     pev->nsi);
877 	else if (module)
878 		/* Currently ref_reloc_sym based probe is not for drivers */
879 		ret = post_process_module_probe_trace_events(tevs, ntevs,
880 							     module, dinfo);
881 	else
882 		ret = post_process_kernel_probe_trace_events(tevs, ntevs);
883 
884 	if (ret >= 0)
885 		arch__post_process_probe_trace_events(pev, ntevs);
886 
887 	return ret;
888 }
889 
890 /* Try to find perf_probe_event with debuginfo */
try_to_find_probe_trace_events(struct perf_probe_event * pev,struct probe_trace_event ** tevs)891 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
892 					  struct probe_trace_event **tevs)
893 {
894 	bool need_dwarf = perf_probe_event_need_dwarf(pev);
895 	struct perf_probe_point tmp;
896 	struct debuginfo *dinfo;
897 	int ntevs, ret = 0;
898 
899 	dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf);
900 	if (!dinfo) {
901 		if (need_dwarf)
902 			return -ENOENT;
903 		pr_debug("Could not open debuginfo. Try to use symbols.\n");
904 		return 0;
905 	}
906 
907 	pr_debug("Try to find probe point from debuginfo.\n");
908 	/* Searching trace events corresponding to a probe event */
909 	ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
910 
911 	if (ntevs == 0)	{  /* Not found, retry with an alternative */
912 		ret = get_alternative_probe_event(dinfo, pev, &tmp);
913 		if (!ret) {
914 			ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
915 			/*
916 			 * Write back to the original probe_event for
917 			 * setting appropriate (user given) event name
918 			 */
919 			clear_perf_probe_point(&pev->point);
920 			memcpy(&pev->point, &tmp, sizeof(tmp));
921 		}
922 	}
923 
924 	if (ntevs > 0) {	/* Succeeded to find trace events */
925 		pr_debug("Found %d probe_trace_events.\n", ntevs);
926 		ret = post_process_probe_trace_events(pev, *tevs, ntevs,
927 					pev->target, pev->uprobes, dinfo);
928 		if (ret < 0 || ret == ntevs) {
929 			pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
930 			clear_probe_trace_events(*tevs, ntevs);
931 			zfree(tevs);
932 			ntevs = 0;
933 		}
934 	}
935 
936 	debuginfo__delete(dinfo);
937 
938 	if (ntevs == 0)	{	/* No error but failed to find probe point. */
939 		pr_warning("Probe point '%s' not found.\n",
940 			   synthesize_perf_probe_point(&pev->point));
941 		return -ENOENT;
942 	} else if (ntevs < 0) {
943 		/* Error path : ntevs < 0 */
944 		pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
945 		if (ntevs == -EBADF)
946 			pr_warning("Warning: No dwarf info found in the vmlinux - "
947 				"please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
948 		if (!need_dwarf) {
949 			pr_debug("Trying to use symbols.\n");
950 			return 0;
951 		}
952 	}
953 	return ntevs;
954 }
955 
956 #define LINEBUF_SIZE 256
957 #define NR_ADDITIONAL_LINES 2
958 
__show_one_line(FILE * fp,int l,bool skip,bool show_num)959 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
960 {
961 	char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
962 	const char *color = show_num ? "" : PERF_COLOR_BLUE;
963 	const char *prefix = NULL;
964 
965 	do {
966 		if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
967 			goto error;
968 		if (skip)
969 			continue;
970 		if (!prefix) {
971 			prefix = show_num ? "%7d  " : "         ";
972 			color_fprintf(stdout, color, prefix, l);
973 		}
974 		color_fprintf(stdout, color, "%s", buf);
975 
976 	} while (strchr(buf, '\n') == NULL);
977 
978 	return 1;
979 error:
980 	if (ferror(fp)) {
981 		pr_warning("File read error: %s\n",
982 			   str_error_r(errno, sbuf, sizeof(sbuf)));
983 		return -1;
984 	}
985 	return 0;
986 }
987 
_show_one_line(FILE * fp,int l,bool skip,bool show_num)988 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
989 {
990 	int rv = __show_one_line(fp, l, skip, show_num);
991 	if (rv == 0) {
992 		pr_warning("Source file is shorter than expected.\n");
993 		rv = -1;
994 	}
995 	return rv;
996 }
997 
998 #define show_one_line_with_num(f,l)	_show_one_line(f,l,false,true)
999 #define show_one_line(f,l)		_show_one_line(f,l,false,false)
1000 #define skip_one_line(f,l)		_show_one_line(f,l,true,false)
1001 #define show_one_line_or_eof(f,l)	__show_one_line(f,l,false,false)
1002 
1003 /*
1004  * Show line-range always requires debuginfo to find source file and
1005  * line number.
1006  */
__show_line_range(struct line_range * lr,const char * module,bool user)1007 static int __show_line_range(struct line_range *lr, const char *module,
1008 			     bool user)
1009 {
1010 	struct build_id bid;
1011 	int l = 1;
1012 	struct int_node *ln;
1013 	struct debuginfo *dinfo;
1014 	FILE *fp;
1015 	int ret;
1016 	char *tmp;
1017 	char sbuf[STRERR_BUFSIZE];
1018 	char sbuild_id[SBUILD_ID_SIZE] = "";
1019 
1020 	/* Search a line range */
1021 	dinfo = open_debuginfo(module, NULL, false);
1022 	if (!dinfo)
1023 		return -ENOENT;
1024 
1025 	ret = debuginfo__find_line_range(dinfo, lr);
1026 	if (!ret) {	/* Not found, retry with an alternative */
1027 		ret = get_alternative_line_range(dinfo, lr, module, user);
1028 		if (!ret)
1029 			ret = debuginfo__find_line_range(dinfo, lr);
1030 	}
1031 	if (dinfo->build_id) {
1032 		build_id__init(&bid, dinfo->build_id, BUILD_ID_SIZE);
1033 		build_id__sprintf(&bid, sbuild_id);
1034 	}
1035 	debuginfo__delete(dinfo);
1036 	if (ret == 0 || ret == -ENOENT) {
1037 		pr_warning("Specified source line is not found.\n");
1038 		return -ENOENT;
1039 	} else if (ret < 0) {
1040 		pr_warning("Debuginfo analysis failed.\n");
1041 		return ret;
1042 	}
1043 
1044 	/* Convert source file path */
1045 	tmp = lr->path;
1046 	ret = find_source_path(tmp, sbuild_id, lr->comp_dir, &lr->path);
1047 
1048 	/* Free old path when new path is assigned */
1049 	if (tmp != lr->path)
1050 		free(tmp);
1051 
1052 	if (ret < 0) {
1053 		pr_warning("Failed to find source file path.\n");
1054 		return ret;
1055 	}
1056 
1057 	setup_pager();
1058 
1059 	if (lr->function)
1060 		fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
1061 			lr->start - lr->offset);
1062 	else
1063 		fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
1064 
1065 	fp = fopen(lr->path, "r");
1066 	if (fp == NULL) {
1067 		pr_warning("Failed to open %s: %s\n", lr->path,
1068 			   str_error_r(errno, sbuf, sizeof(sbuf)));
1069 		return -errno;
1070 	}
1071 	/* Skip to starting line number */
1072 	while (l < lr->start) {
1073 		ret = skip_one_line(fp, l++);
1074 		if (ret < 0)
1075 			goto end;
1076 	}
1077 
1078 	intlist__for_each_entry(ln, lr->line_list) {
1079 		for (; ln->i > l; l++) {
1080 			ret = show_one_line(fp, l - lr->offset);
1081 			if (ret < 0)
1082 				goto end;
1083 		}
1084 		ret = show_one_line_with_num(fp, l++ - lr->offset);
1085 		if (ret < 0)
1086 			goto end;
1087 	}
1088 
1089 	if (lr->end == INT_MAX)
1090 		lr->end = l + NR_ADDITIONAL_LINES;
1091 	while (l <= lr->end) {
1092 		ret = show_one_line_or_eof(fp, l++ - lr->offset);
1093 		if (ret <= 0)
1094 			break;
1095 	}
1096 end:
1097 	fclose(fp);
1098 	return ret;
1099 }
1100 
show_line_range(struct line_range * lr,const char * module,struct nsinfo * nsi,bool user)1101 int show_line_range(struct line_range *lr, const char *module,
1102 		    struct nsinfo *nsi, bool user)
1103 {
1104 	int ret;
1105 	struct nscookie nsc;
1106 
1107 	ret = init_probe_symbol_maps(user);
1108 	if (ret < 0)
1109 		return ret;
1110 	nsinfo__mountns_enter(nsi, &nsc);
1111 	ret = __show_line_range(lr, module, user);
1112 	nsinfo__mountns_exit(&nsc);
1113 	exit_probe_symbol_maps();
1114 
1115 	return ret;
1116 }
1117 
show_available_vars_at(struct debuginfo * dinfo,struct perf_probe_event * pev,struct strfilter * _filter)1118 static int show_available_vars_at(struct debuginfo *dinfo,
1119 				  struct perf_probe_event *pev,
1120 				  struct strfilter *_filter)
1121 {
1122 	char *buf;
1123 	int ret, i, nvars;
1124 	struct str_node *node;
1125 	struct variable_list *vls = NULL, *vl;
1126 	struct perf_probe_point tmp;
1127 	const char *var;
1128 
1129 	buf = synthesize_perf_probe_point(&pev->point);
1130 	if (!buf)
1131 		return -EINVAL;
1132 	pr_debug("Searching variables at %s\n", buf);
1133 
1134 	ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1135 	if (!ret) {  /* Not found, retry with an alternative */
1136 		ret = get_alternative_probe_event(dinfo, pev, &tmp);
1137 		if (!ret) {
1138 			ret = debuginfo__find_available_vars_at(dinfo, pev,
1139 								&vls);
1140 			/* Release the old probe_point */
1141 			clear_perf_probe_point(&tmp);
1142 		}
1143 	}
1144 	if (ret <= 0) {
1145 		if (ret == 0 || ret == -ENOENT) {
1146 			pr_err("Failed to find the address of %s\n", buf);
1147 			ret = -ENOENT;
1148 		} else
1149 			pr_warning("Debuginfo analysis failed.\n");
1150 		goto end;
1151 	}
1152 
1153 	/* Some variables are found */
1154 	fprintf(stdout, "Available variables at %s\n", buf);
1155 	for (i = 0; i < ret; i++) {
1156 		vl = &vls[i];
1157 		/*
1158 		 * A probe point might be converted to
1159 		 * several trace points.
1160 		 */
1161 		fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1162 			vl->point.offset);
1163 		zfree(&vl->point.symbol);
1164 		nvars = 0;
1165 		if (vl->vars) {
1166 			strlist__for_each_entry(node, vl->vars) {
1167 				var = strchr(node->s, '\t') + 1;
1168 				if (strfilter__compare(_filter, var)) {
1169 					fprintf(stdout, "\t\t%s\n", node->s);
1170 					nvars++;
1171 				}
1172 			}
1173 			strlist__delete(vl->vars);
1174 		}
1175 		if (nvars == 0)
1176 			fprintf(stdout, "\t\t(No matched variables)\n");
1177 	}
1178 	free(vls);
1179 end:
1180 	free(buf);
1181 	return ret;
1182 }
1183 
1184 /* Show available variables on given probe point */
show_available_vars(struct perf_probe_event * pevs,int npevs,struct strfilter * _filter)1185 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1186 			struct strfilter *_filter)
1187 {
1188 	int i, ret = 0;
1189 	struct debuginfo *dinfo;
1190 
1191 	ret = init_probe_symbol_maps(pevs->uprobes);
1192 	if (ret < 0)
1193 		return ret;
1194 
1195 	dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
1196 	if (!dinfo) {
1197 		ret = -ENOENT;
1198 		goto out;
1199 	}
1200 
1201 	setup_pager();
1202 
1203 	for (i = 0; i < npevs && ret >= 0; i++)
1204 		ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1205 
1206 	debuginfo__delete(dinfo);
1207 out:
1208 	exit_probe_symbol_maps();
1209 	return ret;
1210 }
1211 
1212 #else	/* !HAVE_DWARF_SUPPORT */
1213 
debuginfo_cache__exit(void)1214 static void debuginfo_cache__exit(void)
1215 {
1216 }
1217 
1218 static int
find_perf_probe_point_from_dwarf(struct probe_trace_point * tp __maybe_unused,struct perf_probe_point * pp __maybe_unused,bool is_kprobe __maybe_unused)1219 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1220 				 struct perf_probe_point *pp __maybe_unused,
1221 				 bool is_kprobe __maybe_unused)
1222 {
1223 	return -ENOSYS;
1224 }
1225 
try_to_find_probe_trace_events(struct perf_probe_event * pev,struct probe_trace_event ** tevs __maybe_unused)1226 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1227 				struct probe_trace_event **tevs __maybe_unused)
1228 {
1229 	if (perf_probe_event_need_dwarf(pev)) {
1230 		pr_warning("Debuginfo-analysis is not supported.\n");
1231 		return -ENOSYS;
1232 	}
1233 
1234 	return 0;
1235 }
1236 
show_line_range(struct line_range * lr __maybe_unused,const char * module __maybe_unused,struct nsinfo * nsi __maybe_unused,bool user __maybe_unused)1237 int show_line_range(struct line_range *lr __maybe_unused,
1238 		    const char *module __maybe_unused,
1239 		    struct nsinfo *nsi __maybe_unused,
1240 		    bool user __maybe_unused)
1241 {
1242 	pr_warning("Debuginfo-analysis is not supported.\n");
1243 	return -ENOSYS;
1244 }
1245 
show_available_vars(struct perf_probe_event * pevs __maybe_unused,int npevs __maybe_unused,struct strfilter * filter __maybe_unused)1246 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1247 			int npevs __maybe_unused,
1248 			struct strfilter *filter __maybe_unused)
1249 {
1250 	pr_warning("Debuginfo-analysis is not supported.\n");
1251 	return -ENOSYS;
1252 }
1253 #endif
1254 
line_range__clear(struct line_range * lr)1255 void line_range__clear(struct line_range *lr)
1256 {
1257 	zfree(&lr->function);
1258 	zfree(&lr->file);
1259 	zfree(&lr->path);
1260 	zfree(&lr->comp_dir);
1261 	intlist__delete(lr->line_list);
1262 }
1263 
line_range__init(struct line_range * lr)1264 int line_range__init(struct line_range *lr)
1265 {
1266 	memset(lr, 0, sizeof(*lr));
1267 	lr->line_list = intlist__new(NULL);
1268 	if (!lr->line_list)
1269 		return -ENOMEM;
1270 	else
1271 		return 0;
1272 }
1273 
parse_line_num(char ** ptr,int * val,const char * what)1274 static int parse_line_num(char **ptr, int *val, const char *what)
1275 {
1276 	const char *start = *ptr;
1277 
1278 	errno = 0;
1279 	*val = strtol(*ptr, ptr, 0);
1280 	if (errno || *ptr == start) {
1281 		semantic_error("'%s' is not a valid number.\n", what);
1282 		return -EINVAL;
1283 	}
1284 	return 0;
1285 }
1286 
1287 /* Check the name is good for event, group or function */
is_c_func_name(const char * name)1288 static bool is_c_func_name(const char *name)
1289 {
1290 	if (!isalpha(*name) && *name != '_')
1291 		return false;
1292 	while (*++name != '\0') {
1293 		if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1294 			return false;
1295 	}
1296 	return true;
1297 }
1298 
1299 /*
1300  * Stuff 'lr' according to the line range described by 'arg'.
1301  * The line range syntax is described by:
1302  *
1303  *         SRC[:SLN[+NUM|-ELN]]
1304  *         FNC[@SRC][:SLN[+NUM|-ELN]]
1305  */
parse_line_range_desc(const char * arg,struct line_range * lr)1306 int parse_line_range_desc(const char *arg, struct line_range *lr)
1307 {
1308 	char *range, *file, *name = strdup(arg);
1309 	int err;
1310 
1311 	if (!name)
1312 		return -ENOMEM;
1313 
1314 	lr->start = 0;
1315 	lr->end = INT_MAX;
1316 
1317 	range = strchr(name, ':');
1318 	if (range) {
1319 		*range++ = '\0';
1320 
1321 		err = parse_line_num(&range, &lr->start, "start line");
1322 		if (err)
1323 			goto err;
1324 
1325 		if (*range == '+' || *range == '-') {
1326 			const char c = *range++;
1327 
1328 			err = parse_line_num(&range, &lr->end, "end line");
1329 			if (err)
1330 				goto err;
1331 
1332 			if (c == '+') {
1333 				lr->end += lr->start;
1334 				/*
1335 				 * Adjust the number of lines here.
1336 				 * If the number of lines == 1, the
1337 				 * the end of line should be equal to
1338 				 * the start of line.
1339 				 */
1340 				lr->end--;
1341 			}
1342 		}
1343 
1344 		pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1345 
1346 		err = -EINVAL;
1347 		if (lr->start > lr->end) {
1348 			semantic_error("Start line must be smaller"
1349 				       " than end line.\n");
1350 			goto err;
1351 		}
1352 		if (*range != '\0') {
1353 			semantic_error("Tailing with invalid str '%s'.\n", range);
1354 			goto err;
1355 		}
1356 	}
1357 
1358 	file = strchr(name, '@');
1359 	if (file) {
1360 		*file = '\0';
1361 		lr->file = strdup(++file);
1362 		if (lr->file == NULL) {
1363 			err = -ENOMEM;
1364 			goto err;
1365 		}
1366 		lr->function = name;
1367 	} else if (strchr(name, '/') || strchr(name, '.'))
1368 		lr->file = name;
1369 	else if (is_c_func_name(name))/* We reuse it for checking funcname */
1370 		lr->function = name;
1371 	else {	/* Invalid name */
1372 		semantic_error("'%s' is not a valid function name.\n", name);
1373 		err = -EINVAL;
1374 		goto err;
1375 	}
1376 
1377 	return 0;
1378 err:
1379 	free(name);
1380 	return err;
1381 }
1382 
parse_perf_probe_event_name(char ** arg,struct perf_probe_event * pev)1383 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1384 {
1385 	char *ptr;
1386 
1387 	ptr = strpbrk_esc(*arg, ":");
1388 	if (ptr) {
1389 		*ptr = '\0';
1390 		if (!pev->sdt && !is_c_func_name(*arg))
1391 			goto ng_name;
1392 		pev->group = strdup_esc(*arg);
1393 		if (!pev->group)
1394 			return -ENOMEM;
1395 		*arg = ptr + 1;
1396 	} else
1397 		pev->group = NULL;
1398 
1399 	pev->event = strdup_esc(*arg);
1400 	if (pev->event == NULL)
1401 		return -ENOMEM;
1402 
1403 	if (!pev->sdt && !is_c_func_name(pev->event)) {
1404 		zfree(&pev->event);
1405 ng_name:
1406 		zfree(&pev->group);
1407 		semantic_error("%s is bad for event name -it must "
1408 			       "follow C symbol-naming rule.\n", *arg);
1409 		return -EINVAL;
1410 	}
1411 	return 0;
1412 }
1413 
1414 /* Parse probepoint definition. */
parse_perf_probe_point(char * arg,struct perf_probe_event * pev)1415 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1416 {
1417 	struct perf_probe_point *pp = &pev->point;
1418 	char *ptr, *tmp;
1419 	char c, nc = 0;
1420 	bool file_spec = false;
1421 	int ret;
1422 
1423 	/*
1424 	 * <Syntax>
1425 	 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1426 	 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1427 	 * perf probe %[GRP:]SDT_EVENT
1428 	 */
1429 	if (!arg)
1430 		return -EINVAL;
1431 
1432 	if (is_sdt_event(arg)) {
1433 		pev->sdt = true;
1434 		if (arg[0] == '%')
1435 			arg++;
1436 	}
1437 
1438 	ptr = strpbrk_esc(arg, ";=@+%");
1439 	if (pev->sdt) {
1440 		if (ptr) {
1441 			if (*ptr != '@') {
1442 				semantic_error("%s must be an SDT name.\n",
1443 					       arg);
1444 				return -EINVAL;
1445 			}
1446 			/* This must be a target file name or build id */
1447 			tmp = build_id_cache__complement(ptr + 1);
1448 			if (tmp) {
1449 				pev->target = build_id_cache__origname(tmp);
1450 				free(tmp);
1451 			} else
1452 				pev->target = strdup_esc(ptr + 1);
1453 			if (!pev->target)
1454 				return -ENOMEM;
1455 			*ptr = '\0';
1456 		}
1457 		ret = parse_perf_probe_event_name(&arg, pev);
1458 		if (ret == 0) {
1459 			if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1460 				ret = -errno;
1461 		}
1462 		return ret;
1463 	}
1464 
1465 	if (ptr && *ptr == '=') {	/* Event name */
1466 		*ptr = '\0';
1467 		tmp = ptr + 1;
1468 		ret = parse_perf_probe_event_name(&arg, pev);
1469 		if (ret < 0)
1470 			return ret;
1471 
1472 		arg = tmp;
1473 	}
1474 
1475 	/*
1476 	 * Check arg is function or file name and copy it.
1477 	 *
1478 	 * We consider arg to be a file spec if and only if it satisfies
1479 	 * all of the below criteria::
1480 	 * - it does not include any of "+@%",
1481 	 * - it includes one of ":;", and
1482 	 * - it has a period '.' in the name.
1483 	 *
1484 	 * Otherwise, we consider arg to be a function specification.
1485 	 */
1486 	if (!strpbrk_esc(arg, "+@%")) {
1487 		ptr = strpbrk_esc(arg, ";:");
1488 		/* This is a file spec if it includes a '.' before ; or : */
1489 		if (ptr && memchr(arg, '.', ptr - arg))
1490 			file_spec = true;
1491 	}
1492 
1493 	ptr = strpbrk_esc(arg, ";:+@%");
1494 	if (ptr) {
1495 		nc = *ptr;
1496 		*ptr++ = '\0';
1497 	}
1498 
1499 	if (arg[0] == '\0')
1500 		tmp = NULL;
1501 	else {
1502 		tmp = strdup_esc(arg);
1503 		if (tmp == NULL)
1504 			return -ENOMEM;
1505 	}
1506 
1507 	if (file_spec)
1508 		pp->file = tmp;
1509 	else {
1510 		pp->function = tmp;
1511 
1512 		/*
1513 		 * Keep pp->function even if this is absolute address,
1514 		 * so it can mark whether abs_address is valid.
1515 		 * Which make 'perf probe lib.bin 0x0' possible.
1516 		 *
1517 		 * Note that checking length of tmp is not needed
1518 		 * because when we access tmp[1] we know tmp[0] is '0',
1519 		 * so tmp[1] should always valid (but could be '\0').
1520 		 */
1521 		if (tmp && !strncmp(tmp, "0x", 2)) {
1522 			pp->abs_address = strtoul(pp->function, &tmp, 0);
1523 			if (*tmp != '\0') {
1524 				semantic_error("Invalid absolute address.\n");
1525 				return -EINVAL;
1526 			}
1527 		}
1528 	}
1529 
1530 	/* Parse other options */
1531 	while (ptr) {
1532 		arg = ptr;
1533 		c = nc;
1534 		if (c == ';') {	/* Lazy pattern must be the last part */
1535 			pp->lazy_line = strdup(arg); /* let leave escapes */
1536 			if (pp->lazy_line == NULL)
1537 				return -ENOMEM;
1538 			break;
1539 		}
1540 		ptr = strpbrk_esc(arg, ";:+@%");
1541 		if (ptr) {
1542 			nc = *ptr;
1543 			*ptr++ = '\0';
1544 		}
1545 		switch (c) {
1546 		case ':':	/* Line number */
1547 			pp->line = strtoul(arg, &tmp, 0);
1548 			if (*tmp != '\0') {
1549 				semantic_error("There is non-digit char"
1550 					       " in line number.\n");
1551 				return -EINVAL;
1552 			}
1553 			break;
1554 		case '+':	/* Byte offset from a symbol */
1555 			pp->offset = strtoul(arg, &tmp, 0);
1556 			if (*tmp != '\0') {
1557 				semantic_error("There is non-digit character"
1558 						" in offset.\n");
1559 				return -EINVAL;
1560 			}
1561 			break;
1562 		case '@':	/* File name */
1563 			if (pp->file) {
1564 				semantic_error("SRC@SRC is not allowed.\n");
1565 				return -EINVAL;
1566 			}
1567 			pp->file = strdup_esc(arg);
1568 			if (pp->file == NULL)
1569 				return -ENOMEM;
1570 			break;
1571 		case '%':	/* Probe places */
1572 			if (strcmp(arg, "return") == 0) {
1573 				pp->retprobe = 1;
1574 			} else {	/* Others not supported yet */
1575 				semantic_error("%%%s is not supported.\n", arg);
1576 				return -ENOTSUP;
1577 			}
1578 			break;
1579 		default:	/* Buggy case */
1580 			pr_err("This program has a bug at %s:%d.\n",
1581 				__FILE__, __LINE__);
1582 			return -ENOTSUP;
1583 			break;
1584 		}
1585 	}
1586 
1587 	/* Exclusion check */
1588 	if (pp->lazy_line && pp->line) {
1589 		semantic_error("Lazy pattern can't be used with"
1590 			       " line number.\n");
1591 		return -EINVAL;
1592 	}
1593 
1594 	if (pp->lazy_line && pp->offset) {
1595 		semantic_error("Lazy pattern can't be used with offset.\n");
1596 		return -EINVAL;
1597 	}
1598 
1599 	if (pp->line && pp->offset) {
1600 		semantic_error("Offset can't be used with line number.\n");
1601 		return -EINVAL;
1602 	}
1603 
1604 	if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1605 		semantic_error("File always requires line number or "
1606 			       "lazy pattern.\n");
1607 		return -EINVAL;
1608 	}
1609 
1610 	if (pp->offset && !pp->function) {
1611 		semantic_error("Offset requires an entry function.\n");
1612 		return -EINVAL;
1613 	}
1614 
1615 	if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1616 		semantic_error("Offset/Line/Lazy pattern can't be used with "
1617 			       "return probe.\n");
1618 		return -EINVAL;
1619 	}
1620 
1621 	pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1622 		 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1623 		 pp->lazy_line);
1624 	return 0;
1625 }
1626 
1627 /* Parse perf-probe event argument */
parse_perf_probe_arg(char * str,struct perf_probe_arg * arg)1628 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1629 {
1630 	char *tmp, *goodname;
1631 	struct perf_probe_arg_field **fieldp;
1632 
1633 	pr_debug("parsing arg: %s into ", str);
1634 
1635 	tmp = strchr(str, '=');
1636 	if (tmp) {
1637 		arg->name = strndup(str, tmp - str);
1638 		if (arg->name == NULL)
1639 			return -ENOMEM;
1640 		pr_debug("name:%s ", arg->name);
1641 		str = tmp + 1;
1642 	}
1643 
1644 	tmp = strchr(str, '@');
1645 	if (tmp && tmp != str && !strcmp(tmp + 1, "user")) { /* user attr */
1646 		if (!user_access_is_supported()) {
1647 			semantic_error("ftrace does not support user access\n");
1648 			return -EINVAL;
1649 		}
1650 		*tmp = '\0';
1651 		arg->user_access = true;
1652 		pr_debug("user_access ");
1653 	}
1654 
1655 	tmp = strchr(str, ':');
1656 	if (tmp) {	/* Type setting */
1657 		*tmp = '\0';
1658 		arg->type = strdup(tmp + 1);
1659 		if (arg->type == NULL)
1660 			return -ENOMEM;
1661 		pr_debug("type:%s ", arg->type);
1662 	}
1663 
1664 	tmp = strpbrk(str, "-.[");
1665 	if (!is_c_varname(str) || !tmp) {
1666 		/* A variable, register, symbol or special value */
1667 		arg->var = strdup(str);
1668 		if (arg->var == NULL)
1669 			return -ENOMEM;
1670 		pr_debug("%s\n", arg->var);
1671 		return 0;
1672 	}
1673 
1674 	/* Structure fields or array element */
1675 	arg->var = strndup(str, tmp - str);
1676 	if (arg->var == NULL)
1677 		return -ENOMEM;
1678 	goodname = arg->var;
1679 	pr_debug("%s, ", arg->var);
1680 	fieldp = &arg->field;
1681 
1682 	do {
1683 		*fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1684 		if (*fieldp == NULL)
1685 			return -ENOMEM;
1686 		if (*tmp == '[') {	/* Array */
1687 			str = tmp;
1688 			(*fieldp)->index = strtol(str + 1, &tmp, 0);
1689 			(*fieldp)->ref = true;
1690 			if (*tmp != ']' || tmp == str + 1) {
1691 				semantic_error("Array index must be a"
1692 						" number.\n");
1693 				return -EINVAL;
1694 			}
1695 			tmp++;
1696 			if (*tmp == '\0')
1697 				tmp = NULL;
1698 		} else {		/* Structure */
1699 			if (*tmp == '.') {
1700 				str = tmp + 1;
1701 				(*fieldp)->ref = false;
1702 			} else if (tmp[1] == '>') {
1703 				str = tmp + 2;
1704 				(*fieldp)->ref = true;
1705 			} else {
1706 				semantic_error("Argument parse error: %s\n",
1707 					       str);
1708 				return -EINVAL;
1709 			}
1710 			tmp = strpbrk(str, "-.[");
1711 		}
1712 		if (tmp) {
1713 			(*fieldp)->name = strndup(str, tmp - str);
1714 			if ((*fieldp)->name == NULL)
1715 				return -ENOMEM;
1716 			if (*str != '[')
1717 				goodname = (*fieldp)->name;
1718 			pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1719 			fieldp = &(*fieldp)->next;
1720 		}
1721 	} while (tmp);
1722 	(*fieldp)->name = strdup(str);
1723 	if ((*fieldp)->name == NULL)
1724 		return -ENOMEM;
1725 	if (*str != '[')
1726 		goodname = (*fieldp)->name;
1727 	pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1728 
1729 	/* If no name is specified, set the last field name (not array index)*/
1730 	if (!arg->name) {
1731 		arg->name = strdup(goodname);
1732 		if (arg->name == NULL)
1733 			return -ENOMEM;
1734 	}
1735 	return 0;
1736 }
1737 
1738 /* Parse perf-probe event command */
parse_perf_probe_command(const char * cmd,struct perf_probe_event * pev)1739 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1740 {
1741 	char **argv;
1742 	int argc, i, ret = 0;
1743 
1744 	argv = argv_split(cmd, &argc);
1745 	if (!argv) {
1746 		pr_debug("Failed to split arguments.\n");
1747 		return -ENOMEM;
1748 	}
1749 	if (argc - 1 > MAX_PROBE_ARGS) {
1750 		semantic_error("Too many probe arguments (%d).\n", argc - 1);
1751 		ret = -ERANGE;
1752 		goto out;
1753 	}
1754 	/* Parse probe point */
1755 	ret = parse_perf_probe_point(argv[0], pev);
1756 	if (ret < 0)
1757 		goto out;
1758 
1759 	/* Generate event name if needed */
1760 	if (!pev->event && pev->point.function && pev->point.line
1761 			&& !pev->point.lazy_line && !pev->point.offset) {
1762 		if (asprintf(&pev->event, "%s_L%d", pev->point.function,
1763 			pev->point.line) < 0) {
1764 			ret = -ENOMEM;
1765 			goto out;
1766 		}
1767 	}
1768 
1769 	/* Copy arguments and ensure return probe has no C argument */
1770 	pev->nargs = argc - 1;
1771 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1772 	if (pev->args == NULL) {
1773 		ret = -ENOMEM;
1774 		goto out;
1775 	}
1776 	for (i = 0; i < pev->nargs && ret >= 0; i++) {
1777 		ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1778 		if (ret >= 0 &&
1779 		    is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1780 			semantic_error("You can't specify local variable for"
1781 				       " kretprobe.\n");
1782 			ret = -EINVAL;
1783 		}
1784 	}
1785 out:
1786 	argv_free(argv);
1787 
1788 	return ret;
1789 }
1790 
1791 /* Returns true if *any* ARG is either C variable, $params or $vars. */
perf_probe_with_var(struct perf_probe_event * pev)1792 bool perf_probe_with_var(struct perf_probe_event *pev)
1793 {
1794 	int i = 0;
1795 
1796 	for (i = 0; i < pev->nargs; i++)
1797 		if (is_c_varname(pev->args[i].var)              ||
1798 		    !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1799 		    !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1800 			return true;
1801 	return false;
1802 }
1803 
1804 /* Return true if this perf_probe_event requires debuginfo */
perf_probe_event_need_dwarf(struct perf_probe_event * pev)1805 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1806 {
1807 	if (pev->point.file || pev->point.line || pev->point.lazy_line)
1808 		return true;
1809 
1810 	if (perf_probe_with_var(pev))
1811 		return true;
1812 
1813 	return false;
1814 }
1815 
1816 /* Parse probe_events event into struct probe_point */
parse_probe_trace_command(const char * cmd,struct probe_trace_event * tev)1817 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1818 {
1819 	struct probe_trace_point *tp = &tev->point;
1820 	char pr;
1821 	char *p;
1822 	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1823 	int ret, i, argc;
1824 	char **argv;
1825 
1826 	pr_debug("Parsing probe_events: %s\n", cmd);
1827 	argv = argv_split(cmd, &argc);
1828 	if (!argv) {
1829 		pr_debug("Failed to split arguments.\n");
1830 		return -ENOMEM;
1831 	}
1832 	if (argc < 2) {
1833 		semantic_error("Too few probe arguments.\n");
1834 		ret = -ERANGE;
1835 		goto out;
1836 	}
1837 
1838 	/* Scan event and group name. */
1839 	argv0_str = strdup(argv[0]);
1840 	if (argv0_str == NULL) {
1841 		ret = -ENOMEM;
1842 		goto out;
1843 	}
1844 	fmt1_str = strtok_r(argv0_str, ":", &fmt);
1845 	fmt2_str = strtok_r(NULL, "/", &fmt);
1846 	fmt3_str = strtok_r(NULL, " \t", &fmt);
1847 	if (fmt1_str == NULL || fmt2_str == NULL || fmt3_str == NULL) {
1848 		semantic_error("Failed to parse event name: %s\n", argv[0]);
1849 		ret = -EINVAL;
1850 		goto out;
1851 	}
1852 	pr = fmt1_str[0];
1853 	tev->group = strdup(fmt2_str);
1854 	tev->event = strdup(fmt3_str);
1855 	if (tev->group == NULL || tev->event == NULL) {
1856 		ret = -ENOMEM;
1857 		goto out;
1858 	}
1859 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1860 
1861 	tp->retprobe = (pr == 'r');
1862 
1863 	/* Scan module name(if there), function name and offset */
1864 	p = strchr(argv[1], ':');
1865 	if (p) {
1866 		tp->module = strndup(argv[1], p - argv[1]);
1867 		if (!tp->module) {
1868 			ret = -ENOMEM;
1869 			goto out;
1870 		}
1871 		tev->uprobes = (tp->module[0] == '/');
1872 		p++;
1873 	} else
1874 		p = argv[1];
1875 	fmt1_str = strtok_r(p, "+", &fmt);
1876 	/* only the address started with 0x */
1877 	if (fmt1_str[0] == '0')	{
1878 		/*
1879 		 * Fix a special case:
1880 		 * if address == 0, kernel reports something like:
1881 		 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x          (null) arg1=%ax
1882 		 * Newer kernel may fix that, but we want to
1883 		 * support old kernel also.
1884 		 */
1885 		if (strcmp(fmt1_str, "0x") == 0) {
1886 			if (!argv[2] || strcmp(argv[2], "(null)")) {
1887 				ret = -EINVAL;
1888 				goto out;
1889 			}
1890 			tp->address = 0;
1891 
1892 			free(argv[2]);
1893 			for (i = 2; argv[i + 1] != NULL; i++)
1894 				argv[i] = argv[i + 1];
1895 
1896 			argv[i] = NULL;
1897 			argc -= 1;
1898 		} else
1899 			tp->address = strtoul(fmt1_str, NULL, 0);
1900 	} else {
1901 		/* Only the symbol-based probe has offset */
1902 		tp->symbol = strdup(fmt1_str);
1903 		if (tp->symbol == NULL) {
1904 			ret = -ENOMEM;
1905 			goto out;
1906 		}
1907 		fmt2_str = strtok_r(NULL, "", &fmt);
1908 		if (fmt2_str == NULL)
1909 			tp->offset = 0;
1910 		else
1911 			tp->offset = strtoul(fmt2_str, NULL, 10);
1912 	}
1913 
1914 	if (tev->uprobes) {
1915 		fmt2_str = strchr(p, '(');
1916 		if (fmt2_str)
1917 			tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1918 	}
1919 
1920 	tev->nargs = argc - 2;
1921 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1922 	if (tev->args == NULL) {
1923 		ret = -ENOMEM;
1924 		goto out;
1925 	}
1926 	for (i = 0; i < tev->nargs; i++) {
1927 		p = strchr(argv[i + 2], '=');
1928 		if (p)	/* We don't need which register is assigned. */
1929 			*p++ = '\0';
1930 		else
1931 			p = argv[i + 2];
1932 		tev->args[i].name = strdup(argv[i + 2]);
1933 		/* TODO: parse regs and offset */
1934 		tev->args[i].value = strdup(p);
1935 		if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1936 			ret = -ENOMEM;
1937 			goto out;
1938 		}
1939 	}
1940 	ret = 0;
1941 out:
1942 	free(argv0_str);
1943 	argv_free(argv);
1944 	return ret;
1945 }
1946 
1947 /* Compose only probe arg */
synthesize_perf_probe_arg(struct perf_probe_arg * pa)1948 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1949 {
1950 	struct perf_probe_arg_field *field = pa->field;
1951 	struct strbuf buf;
1952 	char *ret = NULL;
1953 	int err;
1954 
1955 	if (strbuf_init(&buf, 64) < 0)
1956 		return NULL;
1957 
1958 	if (pa->name && pa->var)
1959 		err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1960 	else
1961 		err = strbuf_addstr(&buf, pa->name ?: pa->var);
1962 	if (err)
1963 		goto out;
1964 
1965 	while (field) {
1966 		if (field->name[0] == '[')
1967 			err = strbuf_addstr(&buf, field->name);
1968 		else
1969 			err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1970 					  field->name);
1971 		field = field->next;
1972 		if (err)
1973 			goto out;
1974 	}
1975 
1976 	if (pa->type)
1977 		if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1978 			goto out;
1979 
1980 	ret = strbuf_detach(&buf, NULL);
1981 out:
1982 	strbuf_release(&buf);
1983 	return ret;
1984 }
1985 
1986 /* Compose only probe point (not argument) */
synthesize_perf_probe_point(struct perf_probe_point * pp)1987 char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1988 {
1989 	struct strbuf buf;
1990 	char *tmp, *ret = NULL;
1991 	int len, err = 0;
1992 
1993 	if (strbuf_init(&buf, 64) < 0)
1994 		return NULL;
1995 
1996 	if (pp->function) {
1997 		if (strbuf_addstr(&buf, pp->function) < 0)
1998 			goto out;
1999 		if (pp->offset)
2000 			err = strbuf_addf(&buf, "+%lu", pp->offset);
2001 		else if (pp->line)
2002 			err = strbuf_addf(&buf, ":%d", pp->line);
2003 		else if (pp->retprobe)
2004 			err = strbuf_addstr(&buf, "%return");
2005 		if (err)
2006 			goto out;
2007 	}
2008 	if (pp->file) {
2009 		tmp = pp->file;
2010 		len = strlen(tmp);
2011 		if (len > 30) {
2012 			tmp = strchr(pp->file + len - 30, '/');
2013 			tmp = tmp ? tmp + 1 : pp->file + len - 30;
2014 		}
2015 		err = strbuf_addf(&buf, "@%s", tmp);
2016 		if (!err && !pp->function && pp->line)
2017 			err = strbuf_addf(&buf, ":%d", pp->line);
2018 	}
2019 	if (!err)
2020 		ret = strbuf_detach(&buf, NULL);
2021 out:
2022 	strbuf_release(&buf);
2023 	return ret;
2024 }
2025 
synthesize_perf_probe_command(struct perf_probe_event * pev)2026 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
2027 {
2028 	struct strbuf buf;
2029 	char *tmp, *ret = NULL;
2030 	int i;
2031 
2032 	if (strbuf_init(&buf, 64))
2033 		return NULL;
2034 	if (pev->event)
2035 		if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
2036 				pev->event) < 0)
2037 			goto out;
2038 
2039 	tmp = synthesize_perf_probe_point(&pev->point);
2040 	if (!tmp || strbuf_addstr(&buf, tmp) < 0)
2041 		goto out;
2042 	free(tmp);
2043 
2044 	for (i = 0; i < pev->nargs; i++) {
2045 		tmp = synthesize_perf_probe_arg(pev->args + i);
2046 		if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
2047 			goto out;
2048 		free(tmp);
2049 	}
2050 
2051 	ret = strbuf_detach(&buf, NULL);
2052 out:
2053 	strbuf_release(&buf);
2054 	return ret;
2055 }
2056 
__synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref * ref,struct strbuf * buf,int depth)2057 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
2058 					    struct strbuf *buf, int depth)
2059 {
2060 	int err;
2061 	if (ref->next) {
2062 		depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
2063 							 depth + 1);
2064 		if (depth < 0)
2065 			return depth;
2066 	}
2067 	if (ref->user_access)
2068 		err = strbuf_addf(buf, "%s%ld(", "+u", ref->offset);
2069 	else
2070 		err = strbuf_addf(buf, "%+ld(", ref->offset);
2071 	return (err < 0) ? err : depth;
2072 }
2073 
synthesize_probe_trace_arg(struct probe_trace_arg * arg,struct strbuf * buf)2074 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
2075 				      struct strbuf *buf)
2076 {
2077 	struct probe_trace_arg_ref *ref = arg->ref;
2078 	int depth = 0, err;
2079 
2080 	/* Argument name or separator */
2081 	if (arg->name)
2082 		err = strbuf_addf(buf, " %s=", arg->name);
2083 	else
2084 		err = strbuf_addch(buf, ' ');
2085 	if (err)
2086 		return err;
2087 
2088 	/* Special case: @XXX */
2089 	if (arg->value[0] == '@' && arg->ref)
2090 			ref = ref->next;
2091 
2092 	/* Dereferencing arguments */
2093 	if (ref) {
2094 		depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
2095 		if (depth < 0)
2096 			return depth;
2097 	}
2098 
2099 	/* Print argument value */
2100 	if (arg->value[0] == '@' && arg->ref)
2101 		err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
2102 	else
2103 		err = strbuf_addstr(buf, arg->value);
2104 
2105 	/* Closing */
2106 	while (!err && depth--)
2107 		err = strbuf_addch(buf, ')');
2108 
2109 	/* Print argument type */
2110 	if (!err && arg->type)
2111 		err = strbuf_addf(buf, ":%s", arg->type);
2112 
2113 	return err;
2114 }
2115 
2116 static int
synthesize_uprobe_trace_def(struct probe_trace_event * tev,struct strbuf * buf)2117 synthesize_uprobe_trace_def(struct probe_trace_event *tev, struct strbuf *buf)
2118 {
2119 	struct probe_trace_point *tp = &tev->point;
2120 	int err;
2121 
2122 	err = strbuf_addf(buf, "%s:0x%lx", tp->module, tp->address);
2123 
2124 	if (err >= 0 && tp->ref_ctr_offset) {
2125 		if (!uprobe_ref_ctr_is_supported())
2126 			return -1;
2127 		err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2128 	}
2129 	return err >= 0 ? 0 : -1;
2130 }
2131 
synthesize_probe_trace_command(struct probe_trace_event * tev)2132 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2133 {
2134 	struct probe_trace_point *tp = &tev->point;
2135 	struct strbuf buf;
2136 	char *ret = NULL;
2137 	int i, err;
2138 
2139 	/* Uprobes must have tp->module */
2140 	if (tev->uprobes && !tp->module)
2141 		return NULL;
2142 
2143 	if (strbuf_init(&buf, 32) < 0)
2144 		return NULL;
2145 
2146 	if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2147 			tev->group, tev->event) < 0)
2148 		goto error;
2149 	/*
2150 	 * If tp->address == 0, then this point must be a
2151 	 * absolute address uprobe.
2152 	 * try_to_find_absolute_address() should have made
2153 	 * tp->symbol to "0x0".
2154 	 */
2155 	if (tev->uprobes && !tp->address) {
2156 		if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2157 			goto error;
2158 	}
2159 
2160 	/* Use the tp->address for uprobes */
2161 	if (tev->uprobes) {
2162 		err = synthesize_uprobe_trace_def(tev, &buf);
2163 	} else if (!strncmp(tp->symbol, "0x", 2)) {
2164 		/* Absolute address. See try_to_find_absolute_address() */
2165 		err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2166 				  tp->module ? ":" : "", tp->address);
2167 	} else {
2168 		err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2169 				tp->module ? ":" : "", tp->symbol, tp->offset);
2170 	}
2171 
2172 	if (err)
2173 		goto error;
2174 
2175 	for (i = 0; i < tev->nargs; i++)
2176 		if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
2177 			goto error;
2178 
2179 	ret = strbuf_detach(&buf, NULL);
2180 error:
2181 	strbuf_release(&buf);
2182 	return ret;
2183 }
2184 
find_perf_probe_point_from_map(struct probe_trace_point * tp,struct perf_probe_point * pp,bool is_kprobe)2185 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2186 					  struct perf_probe_point *pp,
2187 					  bool is_kprobe)
2188 {
2189 	struct symbol *sym = NULL;
2190 	struct map *map = NULL;
2191 	u64 addr = tp->address;
2192 	int ret = -ENOENT;
2193 
2194 	if (!is_kprobe) {
2195 		map = dso__new_map(tp->module);
2196 		if (!map)
2197 			goto out;
2198 		sym = map__find_symbol(map, addr);
2199 	} else {
2200 		if (tp->symbol && !addr) {
2201 			if (kernel_get_symbol_address_by_name(tp->symbol,
2202 						&addr, true, false) < 0)
2203 				goto out;
2204 		}
2205 		if (addr) {
2206 			addr += tp->offset;
2207 			sym = machine__find_kernel_symbol(host_machine, addr, &map);
2208 		}
2209 	}
2210 
2211 	if (!sym)
2212 		goto out;
2213 
2214 	pp->retprobe = tp->retprobe;
2215 	pp->offset = addr - map->unmap_ip(map, sym->start);
2216 	pp->function = strdup(sym->name);
2217 	ret = pp->function ? 0 : -ENOMEM;
2218 
2219 out:
2220 	if (map && !is_kprobe) {
2221 		map__put(map);
2222 	}
2223 
2224 	return ret;
2225 }
2226 
convert_to_perf_probe_point(struct probe_trace_point * tp,struct perf_probe_point * pp,bool is_kprobe)2227 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2228 				       struct perf_probe_point *pp,
2229 				       bool is_kprobe)
2230 {
2231 	char buf[128];
2232 	int ret;
2233 
2234 	ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2235 	if (!ret)
2236 		return 0;
2237 	ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2238 	if (!ret)
2239 		return 0;
2240 
2241 	pr_debug("Failed to find probe point from both of dwarf and map.\n");
2242 
2243 	if (tp->symbol) {
2244 		pp->function = strdup(tp->symbol);
2245 		pp->offset = tp->offset;
2246 	} else {
2247 		ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2248 		if (ret < 0)
2249 			return ret;
2250 		pp->function = strdup(buf);
2251 		pp->offset = 0;
2252 	}
2253 	if (pp->function == NULL)
2254 		return -ENOMEM;
2255 
2256 	pp->retprobe = tp->retprobe;
2257 
2258 	return 0;
2259 }
2260 
convert_to_perf_probe_event(struct probe_trace_event * tev,struct perf_probe_event * pev,bool is_kprobe)2261 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2262 			       struct perf_probe_event *pev, bool is_kprobe)
2263 {
2264 	struct strbuf buf = STRBUF_INIT;
2265 	int i, ret;
2266 
2267 	/* Convert event/group name */
2268 	pev->event = strdup(tev->event);
2269 	pev->group = strdup(tev->group);
2270 	if (pev->event == NULL || pev->group == NULL)
2271 		return -ENOMEM;
2272 
2273 	/* Convert trace_point to probe_point */
2274 	ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2275 	if (ret < 0)
2276 		return ret;
2277 
2278 	/* Convert trace_arg to probe_arg */
2279 	pev->nargs = tev->nargs;
2280 	pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2281 	if (pev->args == NULL)
2282 		return -ENOMEM;
2283 	for (i = 0; i < tev->nargs && ret >= 0; i++) {
2284 		if (tev->args[i].name)
2285 			pev->args[i].name = strdup(tev->args[i].name);
2286 		else {
2287 			if ((ret = strbuf_init(&buf, 32)) < 0)
2288 				goto error;
2289 			ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2290 			pev->args[i].name = strbuf_detach(&buf, NULL);
2291 		}
2292 		if (pev->args[i].name == NULL && ret >= 0)
2293 			ret = -ENOMEM;
2294 	}
2295 error:
2296 	if (ret < 0)
2297 		clear_perf_probe_event(pev);
2298 
2299 	return ret;
2300 }
2301 
clear_perf_probe_event(struct perf_probe_event * pev)2302 void clear_perf_probe_event(struct perf_probe_event *pev)
2303 {
2304 	struct perf_probe_arg_field *field, *next;
2305 	int i;
2306 
2307 	zfree(&pev->event);
2308 	zfree(&pev->group);
2309 	zfree(&pev->target);
2310 	clear_perf_probe_point(&pev->point);
2311 
2312 	for (i = 0; i < pev->nargs; i++) {
2313 		zfree(&pev->args[i].name);
2314 		zfree(&pev->args[i].var);
2315 		zfree(&pev->args[i].type);
2316 		field = pev->args[i].field;
2317 		while (field) {
2318 			next = field->next;
2319 			zfree(&field->name);
2320 			free(field);
2321 			field = next;
2322 		}
2323 	}
2324 	pev->nargs = 0;
2325 	zfree(&pev->args);
2326 }
2327 
2328 #define strdup_or_goto(str, label)	\
2329 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2330 
perf_probe_point__copy(struct perf_probe_point * dst,struct perf_probe_point * src)2331 static int perf_probe_point__copy(struct perf_probe_point *dst,
2332 				  struct perf_probe_point *src)
2333 {
2334 	dst->file = strdup_or_goto(src->file, out_err);
2335 	dst->function = strdup_or_goto(src->function, out_err);
2336 	dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2337 	dst->line = src->line;
2338 	dst->retprobe = src->retprobe;
2339 	dst->offset = src->offset;
2340 	return 0;
2341 
2342 out_err:
2343 	clear_perf_probe_point(dst);
2344 	return -ENOMEM;
2345 }
2346 
perf_probe_arg__copy(struct perf_probe_arg * dst,struct perf_probe_arg * src)2347 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2348 				struct perf_probe_arg *src)
2349 {
2350 	struct perf_probe_arg_field *field, **ppfield;
2351 
2352 	dst->name = strdup_or_goto(src->name, out_err);
2353 	dst->var = strdup_or_goto(src->var, out_err);
2354 	dst->type = strdup_or_goto(src->type, out_err);
2355 
2356 	field = src->field;
2357 	ppfield = &(dst->field);
2358 	while (field) {
2359 		*ppfield = zalloc(sizeof(*field));
2360 		if (!*ppfield)
2361 			goto out_err;
2362 		(*ppfield)->name = strdup_or_goto(field->name, out_err);
2363 		(*ppfield)->index = field->index;
2364 		(*ppfield)->ref = field->ref;
2365 		field = field->next;
2366 		ppfield = &((*ppfield)->next);
2367 	}
2368 	return 0;
2369 out_err:
2370 	return -ENOMEM;
2371 }
2372 
perf_probe_event__copy(struct perf_probe_event * dst,struct perf_probe_event * src)2373 int perf_probe_event__copy(struct perf_probe_event *dst,
2374 			   struct perf_probe_event *src)
2375 {
2376 	int i;
2377 
2378 	dst->event = strdup_or_goto(src->event, out_err);
2379 	dst->group = strdup_or_goto(src->group, out_err);
2380 	dst->target = strdup_or_goto(src->target, out_err);
2381 	dst->uprobes = src->uprobes;
2382 
2383 	if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2384 		goto out_err;
2385 
2386 	dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2387 	if (!dst->args)
2388 		goto out_err;
2389 	dst->nargs = src->nargs;
2390 
2391 	for (i = 0; i < src->nargs; i++)
2392 		if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2393 			goto out_err;
2394 	return 0;
2395 
2396 out_err:
2397 	clear_perf_probe_event(dst);
2398 	return -ENOMEM;
2399 }
2400 
clear_probe_trace_event(struct probe_trace_event * tev)2401 void clear_probe_trace_event(struct probe_trace_event *tev)
2402 {
2403 	struct probe_trace_arg_ref *ref, *next;
2404 	int i;
2405 
2406 	zfree(&tev->event);
2407 	zfree(&tev->group);
2408 	zfree(&tev->point.symbol);
2409 	zfree(&tev->point.realname);
2410 	zfree(&tev->point.module);
2411 	for (i = 0; i < tev->nargs; i++) {
2412 		zfree(&tev->args[i].name);
2413 		zfree(&tev->args[i].value);
2414 		zfree(&tev->args[i].type);
2415 		ref = tev->args[i].ref;
2416 		while (ref) {
2417 			next = ref->next;
2418 			free(ref);
2419 			ref = next;
2420 		}
2421 	}
2422 	zfree(&tev->args);
2423 	tev->nargs = 0;
2424 }
2425 
2426 struct kprobe_blacklist_node {
2427 	struct list_head list;
2428 	unsigned long start;
2429 	unsigned long end;
2430 	char *symbol;
2431 };
2432 
kprobe_blacklist__delete(struct list_head * blacklist)2433 static void kprobe_blacklist__delete(struct list_head *blacklist)
2434 {
2435 	struct kprobe_blacklist_node *node;
2436 
2437 	while (!list_empty(blacklist)) {
2438 		node = list_first_entry(blacklist,
2439 					struct kprobe_blacklist_node, list);
2440 		list_del_init(&node->list);
2441 		zfree(&node->symbol);
2442 		free(node);
2443 	}
2444 }
2445 
kprobe_blacklist__load(struct list_head * blacklist)2446 static int kprobe_blacklist__load(struct list_head *blacklist)
2447 {
2448 	struct kprobe_blacklist_node *node;
2449 	const char *__debugfs = debugfs__mountpoint();
2450 	char buf[PATH_MAX], *p;
2451 	FILE *fp;
2452 	int ret;
2453 
2454 	if (__debugfs == NULL)
2455 		return -ENOTSUP;
2456 
2457 	ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2458 	if (ret < 0)
2459 		return ret;
2460 
2461 	fp = fopen(buf, "r");
2462 	if (!fp)
2463 		return -errno;
2464 
2465 	ret = 0;
2466 	while (fgets(buf, PATH_MAX, fp)) {
2467 		node = zalloc(sizeof(*node));
2468 		if (!node) {
2469 			ret = -ENOMEM;
2470 			break;
2471 		}
2472 		INIT_LIST_HEAD(&node->list);
2473 		list_add_tail(&node->list, blacklist);
2474 		if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2475 			ret = -EINVAL;
2476 			break;
2477 		}
2478 		p = strchr(buf, '\t');
2479 		if (p) {
2480 			p++;
2481 			if (p[strlen(p) - 1] == '\n')
2482 				p[strlen(p) - 1] = '\0';
2483 		} else
2484 			p = (char *)"unknown";
2485 		node->symbol = strdup(p);
2486 		if (!node->symbol) {
2487 			ret = -ENOMEM;
2488 			break;
2489 		}
2490 		pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2491 			  node->start, node->end, node->symbol);
2492 		ret++;
2493 	}
2494 	if (ret < 0)
2495 		kprobe_blacklist__delete(blacklist);
2496 	fclose(fp);
2497 
2498 	return ret;
2499 }
2500 
2501 static struct kprobe_blacklist_node *
kprobe_blacklist__find_by_address(struct list_head * blacklist,unsigned long address)2502 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2503 				  unsigned long address)
2504 {
2505 	struct kprobe_blacklist_node *node;
2506 
2507 	list_for_each_entry(node, blacklist, list) {
2508 		if (node->start <= address && address < node->end)
2509 			return node;
2510 	}
2511 
2512 	return NULL;
2513 }
2514 
2515 static LIST_HEAD(kprobe_blacklist);
2516 
kprobe_blacklist__init(void)2517 static void kprobe_blacklist__init(void)
2518 {
2519 	if (!list_empty(&kprobe_blacklist))
2520 		return;
2521 
2522 	if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2523 		pr_debug("No kprobe blacklist support, ignored\n");
2524 }
2525 
kprobe_blacklist__release(void)2526 static void kprobe_blacklist__release(void)
2527 {
2528 	kprobe_blacklist__delete(&kprobe_blacklist);
2529 }
2530 
kprobe_blacklist__listed(unsigned long address)2531 static bool kprobe_blacklist__listed(unsigned long address)
2532 {
2533 	return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2534 }
2535 
perf_probe_event__sprintf(const char * group,const char * event,struct perf_probe_event * pev,const char * module,struct strbuf * result)2536 static int perf_probe_event__sprintf(const char *group, const char *event,
2537 				     struct perf_probe_event *pev,
2538 				     const char *module,
2539 				     struct strbuf *result)
2540 {
2541 	int i, ret;
2542 	char *buf;
2543 
2544 	if (asprintf(&buf, "%s:%s", group, event) < 0)
2545 		return -errno;
2546 	ret = strbuf_addf(result, "  %-20s (on ", buf);
2547 	free(buf);
2548 	if (ret)
2549 		return ret;
2550 
2551 	/* Synthesize only event probe point */
2552 	buf = synthesize_perf_probe_point(&pev->point);
2553 	if (!buf)
2554 		return -ENOMEM;
2555 	ret = strbuf_addstr(result, buf);
2556 	free(buf);
2557 
2558 	if (!ret && module)
2559 		ret = strbuf_addf(result, " in %s", module);
2560 
2561 	if (!ret && pev->nargs > 0) {
2562 		ret = strbuf_add(result, " with", 5);
2563 		for (i = 0; !ret && i < pev->nargs; i++) {
2564 			buf = synthesize_perf_probe_arg(&pev->args[i]);
2565 			if (!buf)
2566 				return -ENOMEM;
2567 			ret = strbuf_addf(result, " %s", buf);
2568 			free(buf);
2569 		}
2570 	}
2571 	if (!ret)
2572 		ret = strbuf_addch(result, ')');
2573 
2574 	return ret;
2575 }
2576 
2577 /* Show an event */
show_perf_probe_event(const char * group,const char * event,struct perf_probe_event * pev,const char * module,bool use_stdout)2578 int show_perf_probe_event(const char *group, const char *event,
2579 			  struct perf_probe_event *pev,
2580 			  const char *module, bool use_stdout)
2581 {
2582 	struct strbuf buf = STRBUF_INIT;
2583 	int ret;
2584 
2585 	ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2586 	if (ret >= 0) {
2587 		if (use_stdout)
2588 			printf("%s\n", buf.buf);
2589 		else
2590 			pr_info("%s\n", buf.buf);
2591 	}
2592 	strbuf_release(&buf);
2593 
2594 	return ret;
2595 }
2596 
filter_probe_trace_event(struct probe_trace_event * tev,struct strfilter * filter)2597 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2598 				     struct strfilter *filter)
2599 {
2600 	char tmp[128];
2601 
2602 	/* At first, check the event name itself */
2603 	if (strfilter__compare(filter, tev->event))
2604 		return true;
2605 
2606 	/* Next, check the combination of name and group */
2607 	if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2608 		return false;
2609 	return strfilter__compare(filter, tmp);
2610 }
2611 
__show_perf_probe_events(int fd,bool is_kprobe,struct strfilter * filter)2612 static int __show_perf_probe_events(int fd, bool is_kprobe,
2613 				    struct strfilter *filter)
2614 {
2615 	int ret = 0;
2616 	struct probe_trace_event tev;
2617 	struct perf_probe_event pev;
2618 	struct strlist *rawlist;
2619 	struct str_node *ent;
2620 
2621 	memset(&tev, 0, sizeof(tev));
2622 	memset(&pev, 0, sizeof(pev));
2623 
2624 	rawlist = probe_file__get_rawlist(fd);
2625 	if (!rawlist)
2626 		return -ENOMEM;
2627 
2628 	strlist__for_each_entry(ent, rawlist) {
2629 		ret = parse_probe_trace_command(ent->s, &tev);
2630 		if (ret >= 0) {
2631 			if (!filter_probe_trace_event(&tev, filter))
2632 				goto next;
2633 			ret = convert_to_perf_probe_event(&tev, &pev,
2634 								is_kprobe);
2635 			if (ret < 0)
2636 				goto next;
2637 			ret = show_perf_probe_event(pev.group, pev.event,
2638 						    &pev, tev.point.module,
2639 						    true);
2640 		}
2641 next:
2642 		clear_perf_probe_event(&pev);
2643 		clear_probe_trace_event(&tev);
2644 		if (ret < 0)
2645 			break;
2646 	}
2647 	strlist__delete(rawlist);
2648 	/* Cleanup cached debuginfo if needed */
2649 	debuginfo_cache__exit();
2650 
2651 	return ret;
2652 }
2653 
2654 /* List up current perf-probe events */
show_perf_probe_events(struct strfilter * filter)2655 int show_perf_probe_events(struct strfilter *filter)
2656 {
2657 	int kp_fd, up_fd, ret;
2658 
2659 	setup_pager();
2660 
2661 	if (probe_conf.cache)
2662 		return probe_cache__show_all_caches(filter);
2663 
2664 	ret = init_probe_symbol_maps(false);
2665 	if (ret < 0)
2666 		return ret;
2667 
2668 	ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2669 	if (ret < 0)
2670 		return ret;
2671 
2672 	if (kp_fd >= 0)
2673 		ret = __show_perf_probe_events(kp_fd, true, filter);
2674 	if (up_fd >= 0 && ret >= 0)
2675 		ret = __show_perf_probe_events(up_fd, false, filter);
2676 	if (kp_fd > 0)
2677 		close(kp_fd);
2678 	if (up_fd > 0)
2679 		close(up_fd);
2680 	exit_probe_symbol_maps();
2681 
2682 	return ret;
2683 }
2684 
get_new_event_name(char * buf,size_t len,const char * base,struct strlist * namelist,bool ret_event,bool allow_suffix)2685 static int get_new_event_name(char *buf, size_t len, const char *base,
2686 			      struct strlist *namelist, bool ret_event,
2687 			      bool allow_suffix)
2688 {
2689 	int i, ret;
2690 	char *p, *nbase;
2691 
2692 	if (*base == '.')
2693 		base++;
2694 	nbase = strdup(base);
2695 	if (!nbase)
2696 		return -ENOMEM;
2697 
2698 	/* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2699 	p = strpbrk(nbase, ".@");
2700 	if (p && p != nbase)
2701 		*p = '\0';
2702 
2703 	/* Try no suffix number */
2704 	ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
2705 	if (ret < 0) {
2706 		pr_debug("snprintf() failed: %d\n", ret);
2707 		goto out;
2708 	}
2709 	if (!strlist__has_entry(namelist, buf))
2710 		goto out;
2711 
2712 	if (!allow_suffix) {
2713 		pr_warning("Error: event \"%s\" already exists.\n"
2714 			   " Hint: Remove existing event by 'perf probe -d'\n"
2715 			   "       or force duplicates by 'perf probe -f'\n"
2716 			   "       or set 'force=yes' in BPF source.\n",
2717 			   buf);
2718 		ret = -EEXIST;
2719 		goto out;
2720 	}
2721 
2722 	/* Try to add suffix */
2723 	for (i = 1; i < MAX_EVENT_INDEX; i++) {
2724 		ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2725 		if (ret < 0) {
2726 			pr_debug("snprintf() failed: %d\n", ret);
2727 			goto out;
2728 		}
2729 		if (!strlist__has_entry(namelist, buf))
2730 			break;
2731 	}
2732 	if (i == MAX_EVENT_INDEX) {
2733 		pr_warning("Too many events are on the same function.\n");
2734 		ret = -ERANGE;
2735 	}
2736 
2737 out:
2738 	free(nbase);
2739 
2740 	/* Final validation */
2741 	if (ret >= 0 && !is_c_func_name(buf)) {
2742 		pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2743 			   buf);
2744 		ret = -EINVAL;
2745 	}
2746 
2747 	return ret;
2748 }
2749 
2750 /* Warn if the current kernel's uprobe implementation is old */
warn_uprobe_event_compat(struct probe_trace_event * tev)2751 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2752 {
2753 	int i;
2754 	char *buf = synthesize_probe_trace_command(tev);
2755 	struct probe_trace_point *tp = &tev->point;
2756 
2757 	if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2758 		pr_warning("A semaphore is associated with %s:%s and "
2759 			   "seems your kernel doesn't support it.\n",
2760 			   tev->group, tev->event);
2761 	}
2762 
2763 	/* Old uprobe event doesn't support memory dereference */
2764 	if (!tev->uprobes || tev->nargs == 0 || !buf)
2765 		goto out;
2766 
2767 	for (i = 0; i < tev->nargs; i++)
2768 		if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2769 			pr_warning("Please upgrade your kernel to at least "
2770 				   "3.14 to have access to feature %s\n",
2771 				   tev->args[i].value);
2772 			break;
2773 		}
2774 out:
2775 	free(buf);
2776 }
2777 
2778 /* Set new name from original perf_probe_event and namelist */
probe_trace_event__set_name(struct probe_trace_event * tev,struct perf_probe_event * pev,struct strlist * namelist,bool allow_suffix)2779 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2780 				       struct perf_probe_event *pev,
2781 				       struct strlist *namelist,
2782 				       bool allow_suffix)
2783 {
2784 	const char *event, *group;
2785 	char buf[64];
2786 	int ret;
2787 
2788 	/* If probe_event or trace_event already have the name, reuse it */
2789 	if (pev->event && !pev->sdt)
2790 		event = pev->event;
2791 	else if (tev->event)
2792 		event = tev->event;
2793 	else {
2794 		/* Or generate new one from probe point */
2795 		if (pev->point.function &&
2796 			(strncmp(pev->point.function, "0x", 2) != 0) &&
2797 			!strisglob(pev->point.function))
2798 			event = pev->point.function;
2799 		else
2800 			event = tev->point.realname;
2801 	}
2802 	if (pev->group && !pev->sdt)
2803 		group = pev->group;
2804 	else if (tev->group)
2805 		group = tev->group;
2806 	else
2807 		group = PERFPROBE_GROUP;
2808 
2809 	/* Get an unused new event name */
2810 	ret = get_new_event_name(buf, 64, event, namelist,
2811 				 tev->point.retprobe, allow_suffix);
2812 	if (ret < 0)
2813 		return ret;
2814 
2815 	event = buf;
2816 
2817 	tev->event = strdup(event);
2818 	tev->group = strdup(group);
2819 	if (tev->event == NULL || tev->group == NULL)
2820 		return -ENOMEM;
2821 
2822 	/*
2823 	 * Add new event name to namelist if multiprobe event is NOT
2824 	 * supported, since we have to use new event name for following
2825 	 * probes in that case.
2826 	 */
2827 	if (!multiprobe_event_is_supported())
2828 		strlist__add(namelist, event);
2829 	return 0;
2830 }
2831 
__open_probe_file_and_namelist(bool uprobe,struct strlist ** namelist)2832 static int __open_probe_file_and_namelist(bool uprobe,
2833 					  struct strlist **namelist)
2834 {
2835 	int fd;
2836 
2837 	fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2838 	if (fd < 0)
2839 		return fd;
2840 
2841 	/* Get current event names */
2842 	*namelist = probe_file__get_namelist(fd);
2843 	if (!(*namelist)) {
2844 		pr_debug("Failed to get current event list.\n");
2845 		close(fd);
2846 		return -ENOMEM;
2847 	}
2848 	return fd;
2849 }
2850 
__add_probe_trace_events(struct perf_probe_event * pev,struct probe_trace_event * tevs,int ntevs,bool allow_suffix)2851 static int __add_probe_trace_events(struct perf_probe_event *pev,
2852 				     struct probe_trace_event *tevs,
2853 				     int ntevs, bool allow_suffix)
2854 {
2855 	int i, fd[2] = {-1, -1}, up, ret;
2856 	struct probe_trace_event *tev = NULL;
2857 	struct probe_cache *cache = NULL;
2858 	struct strlist *namelist[2] = {NULL, NULL};
2859 	struct nscookie nsc;
2860 
2861 	up = pev->uprobes ? 1 : 0;
2862 	fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2863 	if (fd[up] < 0)
2864 		return fd[up];
2865 
2866 	ret = 0;
2867 	for (i = 0; i < ntevs; i++) {
2868 		tev = &tevs[i];
2869 		up = tev->uprobes ? 1 : 0;
2870 		if (fd[up] == -1) {	/* Open the kprobe/uprobe_events */
2871 			fd[up] = __open_probe_file_and_namelist(up,
2872 								&namelist[up]);
2873 			if (fd[up] < 0)
2874 				goto close_out;
2875 		}
2876 		/* Skip if the symbol is out of .text or blacklisted */
2877 		if (!tev->point.symbol && !pev->uprobes)
2878 			continue;
2879 
2880 		/* Set new name for tev (and update namelist) */
2881 		ret = probe_trace_event__set_name(tev, pev, namelist[up],
2882 						  allow_suffix);
2883 		if (ret < 0)
2884 			break;
2885 
2886 		nsinfo__mountns_enter(pev->nsi, &nsc);
2887 		ret = probe_file__add_event(fd[up], tev);
2888 		nsinfo__mountns_exit(&nsc);
2889 		if (ret < 0)
2890 			break;
2891 
2892 		/*
2893 		 * Probes after the first probe which comes from same
2894 		 * user input are always allowed to add suffix, because
2895 		 * there might be several addresses corresponding to
2896 		 * one code line.
2897 		 */
2898 		allow_suffix = true;
2899 	}
2900 	if (ret == -EINVAL && pev->uprobes)
2901 		warn_uprobe_event_compat(tev);
2902 	if (ret == 0 && probe_conf.cache) {
2903 		cache = probe_cache__new(pev->target, pev->nsi);
2904 		if (!cache ||
2905 		    probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2906 		    probe_cache__commit(cache) < 0)
2907 			pr_warning("Failed to add event to probe cache\n");
2908 		probe_cache__delete(cache);
2909 	}
2910 
2911 close_out:
2912 	for (up = 0; up < 2; up++) {
2913 		strlist__delete(namelist[up]);
2914 		if (fd[up] >= 0)
2915 			close(fd[up]);
2916 	}
2917 	return ret;
2918 }
2919 
find_probe_functions(struct map * map,char * name,struct symbol ** syms)2920 static int find_probe_functions(struct map *map, char *name,
2921 				struct symbol **syms)
2922 {
2923 	int found = 0;
2924 	struct symbol *sym;
2925 	struct rb_node *tmp;
2926 	const char *norm, *ver;
2927 	char *buf = NULL;
2928 	bool cut_version = true;
2929 
2930 	if (map__load(map) < 0)
2931 		return 0;
2932 
2933 	/* If user gives a version, don't cut off the version from symbols */
2934 	if (strchr(name, '@'))
2935 		cut_version = false;
2936 
2937 	map__for_each_symbol(map, sym, tmp) {
2938 		norm = arch__normalize_symbol_name(sym->name);
2939 		if (!norm)
2940 			continue;
2941 
2942 		if (cut_version) {
2943 			/* We don't care about default symbol or not */
2944 			ver = strchr(norm, '@');
2945 			if (ver) {
2946 				buf = strndup(norm, ver - norm);
2947 				if (!buf)
2948 					return -ENOMEM;
2949 				norm = buf;
2950 			}
2951 		}
2952 
2953 		if (strglobmatch(norm, name)) {
2954 			found++;
2955 			if (syms && found < probe_conf.max_probes)
2956 				syms[found - 1] = sym;
2957 		}
2958 		if (buf)
2959 			zfree(&buf);
2960 	}
2961 
2962 	return found;
2963 }
2964 
arch__fix_tev_from_maps(struct perf_probe_event * pev __maybe_unused,struct probe_trace_event * tev __maybe_unused,struct map * map __maybe_unused,struct symbol * sym __maybe_unused)2965 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2966 				struct probe_trace_event *tev __maybe_unused,
2967 				struct map *map __maybe_unused,
2968 				struct symbol *sym __maybe_unused) { }
2969 
2970 /*
2971  * Find probe function addresses from map.
2972  * Return an error or the number of found probe_trace_event
2973  */
find_probe_trace_events_from_map(struct perf_probe_event * pev,struct probe_trace_event ** tevs)2974 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2975 					    struct probe_trace_event **tevs)
2976 {
2977 	struct map *map = NULL;
2978 	struct ref_reloc_sym *reloc_sym = NULL;
2979 	struct symbol *sym;
2980 	struct symbol **syms = NULL;
2981 	struct probe_trace_event *tev;
2982 	struct perf_probe_point *pp = &pev->point;
2983 	struct probe_trace_point *tp;
2984 	int num_matched_functions;
2985 	int ret, i, j, skipped = 0;
2986 	char *mod_name;
2987 
2988 	map = get_target_map(pev->target, pev->nsi, pev->uprobes);
2989 	if (!map) {
2990 		ret = -EINVAL;
2991 		goto out;
2992 	}
2993 
2994 	syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2995 	if (!syms) {
2996 		ret = -ENOMEM;
2997 		goto out;
2998 	}
2999 
3000 	/*
3001 	 * Load matched symbols: Since the different local symbols may have
3002 	 * same name but different addresses, this lists all the symbols.
3003 	 */
3004 	num_matched_functions = find_probe_functions(map, pp->function, syms);
3005 	if (num_matched_functions <= 0) {
3006 		pr_err("Failed to find symbol %s in %s\n", pp->function,
3007 			pev->target ? : "kernel");
3008 		ret = -ENOENT;
3009 		goto out;
3010 	} else if (num_matched_functions > probe_conf.max_probes) {
3011 		pr_err("Too many functions matched in %s\n",
3012 			pev->target ? : "kernel");
3013 		ret = -E2BIG;
3014 		goto out;
3015 	}
3016 
3017 	/* Note that the symbols in the kmodule are not relocated */
3018 	if (!pev->uprobes && !pev->target &&
3019 			(!pp->retprobe || kretprobe_offset_is_supported())) {
3020 		reloc_sym = kernel_get_ref_reloc_sym(NULL);
3021 		if (!reloc_sym) {
3022 			pr_warning("Relocated base symbol is not found!\n");
3023 			ret = -EINVAL;
3024 			goto out;
3025 		}
3026 	}
3027 
3028 	/* Setup result trace-probe-events */
3029 	*tevs = zalloc(sizeof(*tev) * num_matched_functions);
3030 	if (!*tevs) {
3031 		ret = -ENOMEM;
3032 		goto out;
3033 	}
3034 
3035 	ret = 0;
3036 
3037 	for (j = 0; j < num_matched_functions; j++) {
3038 		sym = syms[j];
3039 
3040 		if (sym->type != STT_FUNC)
3041 			continue;
3042 
3043 		/* There can be duplicated symbols in the map */
3044 		for (i = 0; i < j; i++)
3045 			if (sym->start == syms[i]->start) {
3046 				pr_debug("Found duplicated symbol %s @ %" PRIx64 "\n",
3047 					 sym->name, sym->start);
3048 				break;
3049 			}
3050 		if (i != j)
3051 			continue;
3052 
3053 		tev = (*tevs) + ret;
3054 		tp = &tev->point;
3055 		if (ret == num_matched_functions) {
3056 			pr_warning("Too many symbols are listed. Skip it.\n");
3057 			break;
3058 		}
3059 		ret++;
3060 
3061 		if (pp->offset > sym->end - sym->start) {
3062 			pr_warning("Offset %ld is bigger than the size of %s\n",
3063 				   pp->offset, sym->name);
3064 			ret = -ENOENT;
3065 			goto err_out;
3066 		}
3067 		/* Add one probe point */
3068 		tp->address = map->unmap_ip(map, sym->start) + pp->offset;
3069 
3070 		/* Check the kprobe (not in module) is within .text  */
3071 		if (!pev->uprobes && !pev->target &&
3072 		    kprobe_warn_out_range(sym->name, tp->address)) {
3073 			tp->symbol = NULL;	/* Skip it */
3074 			skipped++;
3075 		} else if (reloc_sym) {
3076 			tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
3077 			tp->offset = tp->address - reloc_sym->addr;
3078 		} else {
3079 			tp->symbol = strdup_or_goto(sym->name, nomem_out);
3080 			tp->offset = pp->offset;
3081 		}
3082 		tp->realname = strdup_or_goto(sym->name, nomem_out);
3083 
3084 		tp->retprobe = pp->retprobe;
3085 		if (pev->target) {
3086 			if (pev->uprobes) {
3087 				tev->point.module = strdup_or_goto(pev->target,
3088 								   nomem_out);
3089 			} else {
3090 				mod_name = find_module_name(pev->target);
3091 				tev->point.module =
3092 					strdup(mod_name ? mod_name : pev->target);
3093 				free(mod_name);
3094 				if (!tev->point.module)
3095 					goto nomem_out;
3096 			}
3097 		}
3098 		tev->uprobes = pev->uprobes;
3099 		tev->nargs = pev->nargs;
3100 		if (tev->nargs) {
3101 			tev->args = zalloc(sizeof(struct probe_trace_arg) *
3102 					   tev->nargs);
3103 			if (tev->args == NULL)
3104 				goto nomem_out;
3105 		}
3106 		for (i = 0; i < tev->nargs; i++) {
3107 			if (pev->args[i].name)
3108 				tev->args[i].name =
3109 					strdup_or_goto(pev->args[i].name,
3110 							nomem_out);
3111 
3112 			tev->args[i].value = strdup_or_goto(pev->args[i].var,
3113 							    nomem_out);
3114 			if (pev->args[i].type)
3115 				tev->args[i].type =
3116 					strdup_or_goto(pev->args[i].type,
3117 							nomem_out);
3118 		}
3119 		arch__fix_tev_from_maps(pev, tev, map, sym);
3120 	}
3121 	if (ret == skipped) {
3122 		ret = -ENOENT;
3123 		goto err_out;
3124 	}
3125 
3126 out:
3127 	map__put(map);
3128 	free(syms);
3129 	return ret;
3130 
3131 nomem_out:
3132 	ret = -ENOMEM;
3133 err_out:
3134 	clear_probe_trace_events(*tevs, num_matched_functions);
3135 	zfree(tevs);
3136 	goto out;
3137 }
3138 
try_to_find_absolute_address(struct perf_probe_event * pev,struct probe_trace_event ** tevs)3139 static int try_to_find_absolute_address(struct perf_probe_event *pev,
3140 					struct probe_trace_event **tevs)
3141 {
3142 	struct perf_probe_point *pp = &pev->point;
3143 	struct probe_trace_event *tev;
3144 	struct probe_trace_point *tp;
3145 	int i, err;
3146 
3147 	if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3148 		return -EINVAL;
3149 	if (perf_probe_event_need_dwarf(pev))
3150 		return -EINVAL;
3151 
3152 	/*
3153 	 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3154 	 * absolute address.
3155 	 *
3156 	 * Only one tev can be generated by this.
3157 	 */
3158 	*tevs = zalloc(sizeof(*tev));
3159 	if (!*tevs)
3160 		return -ENOMEM;
3161 
3162 	tev = *tevs;
3163 	tp = &tev->point;
3164 
3165 	/*
3166 	 * Don't use tp->offset, use address directly, because
3167 	 * in synthesize_probe_trace_command() address cannot be
3168 	 * zero.
3169 	 */
3170 	tp->address = pev->point.abs_address;
3171 	tp->retprobe = pp->retprobe;
3172 	tev->uprobes = pev->uprobes;
3173 
3174 	err = -ENOMEM;
3175 	/*
3176 	 * Give it a '0x' leading symbol name.
3177 	 * In __add_probe_trace_events, a NULL symbol is interpreted as
3178 	 * invalid.
3179 	 */
3180 	if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
3181 		goto errout;
3182 
3183 	/* For kprobe, check range */
3184 	if ((!tev->uprobes) &&
3185 	    (kprobe_warn_out_range(tev->point.symbol,
3186 				   tev->point.address))) {
3187 		err = -EACCES;
3188 		goto errout;
3189 	}
3190 
3191 	if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3192 		goto errout;
3193 
3194 	if (pev->target) {
3195 		tp->module = strdup(pev->target);
3196 		if (!tp->module)
3197 			goto errout;
3198 	}
3199 
3200 	if (tev->group) {
3201 		tev->group = strdup(pev->group);
3202 		if (!tev->group)
3203 			goto errout;
3204 	}
3205 
3206 	if (pev->event) {
3207 		tev->event = strdup(pev->event);
3208 		if (!tev->event)
3209 			goto errout;
3210 	}
3211 
3212 	tev->nargs = pev->nargs;
3213 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3214 	if (!tev->args)
3215 		goto errout;
3216 
3217 	for (i = 0; i < tev->nargs; i++)
3218 		copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3219 
3220 	return 1;
3221 
3222 errout:
3223 	clear_probe_trace_events(*tevs, 1);
3224 	*tevs = NULL;
3225 	return err;
3226 }
3227 
3228 /* Concatinate two arrays */
memcat(void * a,size_t sz_a,void * b,size_t sz_b)3229 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3230 {
3231 	void *ret;
3232 
3233 	ret = malloc(sz_a + sz_b);
3234 	if (ret) {
3235 		memcpy(ret, a, sz_a);
3236 		memcpy(ret + sz_a, b, sz_b);
3237 	}
3238 	return ret;
3239 }
3240 
3241 static int
concat_probe_trace_events(struct probe_trace_event ** tevs,int * ntevs,struct probe_trace_event ** tevs2,int ntevs2)3242 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3243 			  struct probe_trace_event **tevs2, int ntevs2)
3244 {
3245 	struct probe_trace_event *new_tevs;
3246 	int ret = 0;
3247 
3248 	if (*ntevs == 0) {
3249 		*tevs = *tevs2;
3250 		*ntevs = ntevs2;
3251 		*tevs2 = NULL;
3252 		return 0;
3253 	}
3254 
3255 	if (*ntevs + ntevs2 > probe_conf.max_probes)
3256 		ret = -E2BIG;
3257 	else {
3258 		/* Concatinate the array of probe_trace_event */
3259 		new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3260 				  *tevs2, ntevs2 * sizeof(**tevs2));
3261 		if (!new_tevs)
3262 			ret = -ENOMEM;
3263 		else {
3264 			free(*tevs);
3265 			*tevs = new_tevs;
3266 			*ntevs += ntevs2;
3267 		}
3268 	}
3269 	if (ret < 0)
3270 		clear_probe_trace_events(*tevs2, ntevs2);
3271 	zfree(tevs2);
3272 
3273 	return ret;
3274 }
3275 
3276 /*
3277  * Try to find probe_trace_event from given probe caches. Return the number
3278  * of cached events found, if an error occurs return the error.
3279  */
find_cached_events(struct perf_probe_event * pev,struct probe_trace_event ** tevs,const char * target)3280 static int find_cached_events(struct perf_probe_event *pev,
3281 			      struct probe_trace_event **tevs,
3282 			      const char *target)
3283 {
3284 	struct probe_cache *cache;
3285 	struct probe_cache_entry *entry;
3286 	struct probe_trace_event *tmp_tevs = NULL;
3287 	int ntevs = 0;
3288 	int ret = 0;
3289 
3290 	cache = probe_cache__new(target, pev->nsi);
3291 	/* Return 0 ("not found") if the target has no probe cache. */
3292 	if (!cache)
3293 		return 0;
3294 
3295 	for_each_probe_cache_entry(entry, cache) {
3296 		/* Skip the cache entry which has no name */
3297 		if (!entry->pev.event || !entry->pev.group)
3298 			continue;
3299 		if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3300 		    strglobmatch(entry->pev.event, pev->event)) {
3301 			ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3302 			if (ret > 0)
3303 				ret = concat_probe_trace_events(tevs, &ntevs,
3304 								&tmp_tevs, ret);
3305 			if (ret < 0)
3306 				break;
3307 		}
3308 	}
3309 	probe_cache__delete(cache);
3310 	if (ret < 0) {
3311 		clear_probe_trace_events(*tevs, ntevs);
3312 		zfree(tevs);
3313 	} else {
3314 		ret = ntevs;
3315 		if (ntevs > 0 && target && target[0] == '/')
3316 			pev->uprobes = true;
3317 	}
3318 
3319 	return ret;
3320 }
3321 
3322 /* Try to find probe_trace_event from all probe caches */
find_cached_events_all(struct perf_probe_event * pev,struct probe_trace_event ** tevs)3323 static int find_cached_events_all(struct perf_probe_event *pev,
3324 				   struct probe_trace_event **tevs)
3325 {
3326 	struct probe_trace_event *tmp_tevs = NULL;
3327 	struct strlist *bidlist;
3328 	struct str_node *nd;
3329 	char *pathname;
3330 	int ntevs = 0;
3331 	int ret;
3332 
3333 	/* Get the buildid list of all valid caches */
3334 	bidlist = build_id_cache__list_all(true);
3335 	if (!bidlist) {
3336 		ret = -errno;
3337 		pr_debug("Failed to get buildids: %d\n", ret);
3338 		return ret;
3339 	}
3340 
3341 	ret = 0;
3342 	strlist__for_each_entry(nd, bidlist) {
3343 		pathname = build_id_cache__origname(nd->s);
3344 		ret = find_cached_events(pev, &tmp_tevs, pathname);
3345 		/* In the case of cnt == 0, we just skip it */
3346 		if (ret > 0)
3347 			ret = concat_probe_trace_events(tevs, &ntevs,
3348 							&tmp_tevs, ret);
3349 		free(pathname);
3350 		if (ret < 0)
3351 			break;
3352 	}
3353 	strlist__delete(bidlist);
3354 
3355 	if (ret < 0) {
3356 		clear_probe_trace_events(*tevs, ntevs);
3357 		zfree(tevs);
3358 	} else
3359 		ret = ntevs;
3360 
3361 	return ret;
3362 }
3363 
find_probe_trace_events_from_cache(struct perf_probe_event * pev,struct probe_trace_event ** tevs)3364 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3365 					      struct probe_trace_event **tevs)
3366 {
3367 	struct probe_cache *cache;
3368 	struct probe_cache_entry *entry;
3369 	struct probe_trace_event *tev;
3370 	struct str_node *node;
3371 	int ret, i;
3372 
3373 	if (pev->sdt) {
3374 		/* For SDT/cached events, we use special search functions */
3375 		if (!pev->target)
3376 			return find_cached_events_all(pev, tevs);
3377 		else
3378 			return find_cached_events(pev, tevs, pev->target);
3379 	}
3380 	cache = probe_cache__new(pev->target, pev->nsi);
3381 	if (!cache)
3382 		return 0;
3383 
3384 	entry = probe_cache__find(cache, pev);
3385 	if (!entry) {
3386 		/* SDT must be in the cache */
3387 		ret = pev->sdt ? -ENOENT : 0;
3388 		goto out;
3389 	}
3390 
3391 	ret = strlist__nr_entries(entry->tevlist);
3392 	if (ret > probe_conf.max_probes) {
3393 		pr_debug("Too many entries matched in the cache of %s\n",
3394 			 pev->target ? : "kernel");
3395 		ret = -E2BIG;
3396 		goto out;
3397 	}
3398 
3399 	*tevs = zalloc(ret * sizeof(*tev));
3400 	if (!*tevs) {
3401 		ret = -ENOMEM;
3402 		goto out;
3403 	}
3404 
3405 	i = 0;
3406 	strlist__for_each_entry(node, entry->tevlist) {
3407 		tev = &(*tevs)[i++];
3408 		ret = parse_probe_trace_command(node->s, tev);
3409 		if (ret < 0)
3410 			goto out;
3411 		/* Set the uprobes attribute as same as original */
3412 		tev->uprobes = pev->uprobes;
3413 	}
3414 	ret = i;
3415 
3416 out:
3417 	probe_cache__delete(cache);
3418 	return ret;
3419 }
3420 
convert_to_probe_trace_events(struct perf_probe_event * pev,struct probe_trace_event ** tevs)3421 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3422 					 struct probe_trace_event **tevs)
3423 {
3424 	int ret;
3425 
3426 	if (!pev->group && !pev->sdt) {
3427 		/* Set group name if not given */
3428 		if (!pev->uprobes) {
3429 			pev->group = strdup(PERFPROBE_GROUP);
3430 			ret = pev->group ? 0 : -ENOMEM;
3431 		} else
3432 			ret = convert_exec_to_group(pev->target, &pev->group);
3433 		if (ret != 0) {
3434 			pr_warning("Failed to make a group name.\n");
3435 			return ret;
3436 		}
3437 	}
3438 
3439 	ret = try_to_find_absolute_address(pev, tevs);
3440 	if (ret > 0)
3441 		return ret;
3442 
3443 	/* At first, we need to lookup cache entry */
3444 	ret = find_probe_trace_events_from_cache(pev, tevs);
3445 	if (ret > 0 || pev->sdt)	/* SDT can be found only in the cache */
3446 		return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3447 
3448 	/* Convert perf_probe_event with debuginfo */
3449 	ret = try_to_find_probe_trace_events(pev, tevs);
3450 	if (ret != 0)
3451 		return ret;	/* Found in debuginfo or got an error */
3452 
3453 	return find_probe_trace_events_from_map(pev, tevs);
3454 }
3455 
convert_perf_probe_events(struct perf_probe_event * pevs,int npevs)3456 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3457 {
3458 	int i, ret;
3459 
3460 	/* Loop 1: convert all events */
3461 	for (i = 0; i < npevs; i++) {
3462 		/* Init kprobe blacklist if needed */
3463 		if (!pevs[i].uprobes)
3464 			kprobe_blacklist__init();
3465 		/* Convert with or without debuginfo */
3466 		ret  = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3467 		if (ret < 0)
3468 			return ret;
3469 		pevs[i].ntevs = ret;
3470 	}
3471 	/* This just release blacklist only if allocated */
3472 	kprobe_blacklist__release();
3473 
3474 	return 0;
3475 }
3476 
show_probe_trace_event(struct probe_trace_event * tev)3477 static int show_probe_trace_event(struct probe_trace_event *tev)
3478 {
3479 	char *buf = synthesize_probe_trace_command(tev);
3480 
3481 	if (!buf) {
3482 		pr_debug("Failed to synthesize probe trace event.\n");
3483 		return -EINVAL;
3484 	}
3485 
3486 	/* Showing definition always go stdout */
3487 	printf("%s\n", buf);
3488 	free(buf);
3489 
3490 	return 0;
3491 }
3492 
show_probe_trace_events(struct perf_probe_event * pevs,int npevs)3493 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3494 {
3495 	struct strlist *namelist = strlist__new(NULL, NULL);
3496 	struct probe_trace_event *tev;
3497 	struct perf_probe_event *pev;
3498 	int i, j, ret = 0;
3499 
3500 	if (!namelist)
3501 		return -ENOMEM;
3502 
3503 	for (j = 0; j < npevs && !ret; j++) {
3504 		pev = &pevs[j];
3505 		for (i = 0; i < pev->ntevs && !ret; i++) {
3506 			tev = &pev->tevs[i];
3507 			/* Skip if the symbol is out of .text or blacklisted */
3508 			if (!tev->point.symbol && !pev->uprobes)
3509 				continue;
3510 
3511 			/* Set new name for tev (and update namelist) */
3512 			ret = probe_trace_event__set_name(tev, pev,
3513 							  namelist, true);
3514 			if (!ret)
3515 				ret = show_probe_trace_event(tev);
3516 		}
3517 	}
3518 	strlist__delete(namelist);
3519 
3520 	return ret;
3521 }
3522 
apply_perf_probe_events(struct perf_probe_event * pevs,int npevs)3523 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3524 {
3525 	int i, ret = 0;
3526 
3527 	/* Loop 2: add all events */
3528 	for (i = 0; i < npevs; i++) {
3529 		ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3530 					       pevs[i].ntevs,
3531 					       probe_conf.force_add);
3532 		if (ret < 0)
3533 			break;
3534 	}
3535 	return ret;
3536 }
3537 
cleanup_perf_probe_events(struct perf_probe_event * pevs,int npevs)3538 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3539 {
3540 	int i, j;
3541 	struct perf_probe_event *pev;
3542 
3543 	/* Loop 3: cleanup and free trace events  */
3544 	for (i = 0; i < npevs; i++) {
3545 		pev = &pevs[i];
3546 		for (j = 0; j < pevs[i].ntevs; j++)
3547 			clear_probe_trace_event(&pevs[i].tevs[j]);
3548 		zfree(&pevs[i].tevs);
3549 		pevs[i].ntevs = 0;
3550 		nsinfo__zput(pev->nsi);
3551 		clear_perf_probe_event(&pevs[i]);
3552 	}
3553 }
3554 
add_perf_probe_events(struct perf_probe_event * pevs,int npevs)3555 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3556 {
3557 	int ret;
3558 
3559 	ret = init_probe_symbol_maps(pevs->uprobes);
3560 	if (ret < 0)
3561 		return ret;
3562 
3563 	ret = convert_perf_probe_events(pevs, npevs);
3564 	if (ret == 0)
3565 		ret = apply_perf_probe_events(pevs, npevs);
3566 
3567 	cleanup_perf_probe_events(pevs, npevs);
3568 
3569 	exit_probe_symbol_maps();
3570 	return ret;
3571 }
3572 
del_perf_probe_events(struct strfilter * filter)3573 int del_perf_probe_events(struct strfilter *filter)
3574 {
3575 	int ret, ret2, ufd = -1, kfd = -1;
3576 	char *str = strfilter__string(filter);
3577 
3578 	if (!str)
3579 		return -EINVAL;
3580 
3581 	/* Get current event names */
3582 	ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3583 	if (ret < 0)
3584 		goto out;
3585 
3586 	ret = probe_file__del_events(kfd, filter);
3587 	if (ret < 0 && ret != -ENOENT)
3588 		goto error;
3589 
3590 	ret2 = probe_file__del_events(ufd, filter);
3591 	if (ret2 < 0 && ret2 != -ENOENT) {
3592 		ret = ret2;
3593 		goto error;
3594 	}
3595 	ret = 0;
3596 
3597 error:
3598 	if (kfd >= 0)
3599 		close(kfd);
3600 	if (ufd >= 0)
3601 		close(ufd);
3602 out:
3603 	free(str);
3604 
3605 	return ret;
3606 }
3607 
show_available_funcs(const char * target,struct nsinfo * nsi,struct strfilter * _filter,bool user)3608 int show_available_funcs(const char *target, struct nsinfo *nsi,
3609 			 struct strfilter *_filter, bool user)
3610 {
3611         struct rb_node *nd;
3612 	struct map *map;
3613 	int ret;
3614 
3615 	ret = init_probe_symbol_maps(user);
3616 	if (ret < 0)
3617 		return ret;
3618 
3619 	/* Get a symbol map */
3620 	map = get_target_map(target, nsi, user);
3621 	if (!map) {
3622 		pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3623 		return -EINVAL;
3624 	}
3625 
3626 	ret = map__load(map);
3627 	if (ret) {
3628 		if (ret == -2) {
3629 			char *str = strfilter__string(_filter);
3630 			pr_err("Failed to find symbols matched to \"%s\"\n",
3631 			       str);
3632 			free(str);
3633 		} else
3634 			pr_err("Failed to load symbols in %s\n",
3635 			       (target) ? : "kernel");
3636 		goto end;
3637 	}
3638 	if (!dso__sorted_by_name(map->dso))
3639 		dso__sort_by_name(map->dso);
3640 
3641 	/* Show all (filtered) symbols */
3642 	setup_pager();
3643 
3644 	for (nd = rb_first_cached(&map->dso->symbol_names); nd;
3645 	     nd = rb_next(nd)) {
3646 		struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3647 
3648 		if (strfilter__compare(_filter, pos->sym.name))
3649 			printf("%s\n", pos->sym.name);
3650 	}
3651 end:
3652 	map__put(map);
3653 	exit_probe_symbol_maps();
3654 
3655 	return ret;
3656 }
3657 
copy_to_probe_trace_arg(struct probe_trace_arg * tvar,struct perf_probe_arg * pvar)3658 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3659 			    struct perf_probe_arg *pvar)
3660 {
3661 	tvar->value = strdup(pvar->var);
3662 	if (tvar->value == NULL)
3663 		return -ENOMEM;
3664 	if (pvar->type) {
3665 		tvar->type = strdup(pvar->type);
3666 		if (tvar->type == NULL)
3667 			return -ENOMEM;
3668 	}
3669 	if (pvar->name) {
3670 		tvar->name = strdup(pvar->name);
3671 		if (tvar->name == NULL)
3672 			return -ENOMEM;
3673 	} else
3674 		tvar->name = NULL;
3675 	return 0;
3676 }
3677