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