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