1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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"
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;
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
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)
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
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
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
157 if (module && strchr(module, '/'))
158 return dso__new_map(module);
159
160 if (!module)
161 module = "kernel";
162
163 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
164
165 if (strncmp(pos->dso->short_name + 1, module,
166 pos->dso->short_name_len - 2) == 0 &&
167 module[pos->dso->short_name_len - 2] == '\0') {
168 return map__get(pos);
169 }
170 }
171 return NULL;
172}
173
174struct map *get_target_map(const char *target, bool user)
175{
176
177 if (user)
178 return dso__new_map(target);
179 else
180 return kernel_get_module_map(target);
181}
182
183static int convert_exec_to_group(const char *exec, char **result)
184{
185 char *ptr1, *ptr2, *exec_copy;
186 char buf[64];
187 int ret;
188
189 exec_copy = strdup(exec);
190 if (!exec_copy)
191 return -ENOMEM;
192
193 ptr1 = basename(exec_copy);
194 if (!ptr1) {
195 ret = -EINVAL;
196 goto out;
197 }
198
199 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
200 if (!isalnum(*ptr2) && *ptr2 != '_') {
201 *ptr2 = '\0';
202 break;
203 }
204 }
205
206 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
207 if (ret < 0)
208 goto out;
209
210 *result = strdup(buf);
211 ret = *result ? 0 : -ENOMEM;
212
213out:
214 free(exec_copy);
215 return ret;
216}
217
218static void clear_perf_probe_point(struct perf_probe_point *pp)
219{
220 free(pp->file);
221 free(pp->function);
222 free(pp->lazy_line);
223}
224
225static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
226{
227 int i;
228
229 for (i = 0; i < ntevs; i++)
230 clear_probe_trace_event(tevs + i);
231}
232
233static bool kprobe_blacklist__listed(unsigned long address);
234static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
235{
236 u64 etext_addr = 0;
237 int ret;
238
239
240 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
241 false, false);
242
243 if (ret == 0 && etext_addr < address)
244 pr_warning("%s is out of .text, skip it.\n", symbol);
245 else if (kprobe_blacklist__listed(address))
246 pr_warning("%s is blacklisted function, skip it.\n", symbol);
247 else
248 return false;
249
250 return true;
251}
252
253
254
255
256
257
258static char *find_module_name(const char *module)
259{
260 int fd;
261 Elf *elf;
262 GElf_Ehdr ehdr;
263 GElf_Shdr shdr;
264 Elf_Data *data;
265 Elf_Scn *sec;
266 char *mod_name = NULL;
267 int name_offset;
268
269 fd = open(module, O_RDONLY);
270 if (fd < 0)
271 return NULL;
272
273 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
274 if (elf == NULL)
275 goto elf_err;
276
277 if (gelf_getehdr(elf, &ehdr) == NULL)
278 goto ret_err;
279
280 sec = elf_section_by_name(elf, &ehdr, &shdr,
281 ".gnu.linkonce.this_module", NULL);
282 if (!sec)
283 goto ret_err;
284
285 data = elf_getdata(sec, NULL);
286 if (!data || !data->d_buf)
287 goto ret_err;
288
289
290
291
292
293
294
295
296
297
298 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
299 name_offset = 12;
300 else
301 name_offset = 24;
302
303 mod_name = strdup((char *)data->d_buf + name_offset);
304
305ret_err:
306 elf_end(elf);
307elf_err:
308 close(fd);
309 return mod_name;
310}
311
312#ifdef HAVE_DWARF_SUPPORT
313
314static int kernel_get_module_dso(const char *module, struct dso **pdso)
315{
316 struct dso *dso;
317 struct map *map;
318 const char *vmlinux_name;
319 int ret = 0;
320
321 if (module) {
322 char module_name[128];
323
324 snprintf(module_name, sizeof(module_name), "[%s]", module);
325 map = map_groups__find_by_name(&host_machine->kmaps, module_name);
326 if (map) {
327 dso = map->dso;
328 goto found;
329 }
330 pr_debug("Failed to find module %s.\n", module);
331 return -ENOENT;
332 }
333
334 map = machine__kernel_map(host_machine);
335 dso = map->dso;
336
337 vmlinux_name = symbol_conf.vmlinux_name;
338 dso->load_errno = 0;
339 if (vmlinux_name)
340 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
341 else
342 ret = dso__load_vmlinux_path(dso, map);
343found:
344 *pdso = dso;
345 return ret;
346}
347
348
349
350
351
352
353static int find_alternative_probe_point(struct debuginfo *dinfo,
354 struct perf_probe_point *pp,
355 struct perf_probe_point *result,
356 const char *target, bool uprobes)
357{
358 struct map *map = NULL;
359 struct symbol *sym;
360 u64 address = 0;
361 int ret = -ENOENT;
362
363
364 if (!pp->function || pp->file)
365 return -ENOTSUP;
366
367 map = get_target_map(target, uprobes);
368 if (!map)
369 return -EINVAL;
370
371
372 map__for_each_symbol_by_name(map, pp->function, sym) {
373 if (uprobes)
374 address = sym->start;
375 else
376 address = map->unmap_ip(map, sym->start) - map->reloc;
377 break;
378 }
379 if (!address) {
380 ret = -ENOENT;
381 goto out;
382 }
383 pr_debug("Symbol %s address found : %" PRIx64 "\n",
384 pp->function, address);
385
386 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
387 result);
388 if (ret <= 0)
389 ret = (!ret) ? -ENOENT : ret;
390 else {
391 result->offset += pp->offset;
392 result->line += pp->line;
393 result->retprobe = pp->retprobe;
394 ret = 0;
395 }
396
397out:
398 map__put(map);
399 return ret;
400
401}
402
403static int get_alternative_probe_event(struct debuginfo *dinfo,
404 struct perf_probe_event *pev,
405 struct perf_probe_point *tmp)
406{
407 int ret;
408
409 memcpy(tmp, &pev->point, sizeof(*tmp));
410 memset(&pev->point, 0, sizeof(pev->point));
411 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
412 pev->target, pev->uprobes);
413 if (ret < 0)
414 memcpy(&pev->point, tmp, sizeof(*tmp));
415
416 return ret;
417}
418
419static int get_alternative_line_range(struct debuginfo *dinfo,
420 struct line_range *lr,
421 const char *target, bool user)
422{
423 struct perf_probe_point pp = { .function = lr->function,
424 .file = lr->file,
425 .line = lr->start };
426 struct perf_probe_point result;
427 int ret, len = 0;
428
429 memset(&result, 0, sizeof(result));
430
431 if (lr->end != INT_MAX)
432 len = lr->end - lr->start;
433 ret = find_alternative_probe_point(dinfo, &pp, &result,
434 target, user);
435 if (!ret) {
436 lr->function = result.function;
437 lr->file = result.file;
438 lr->start = result.line;
439 if (lr->end != INT_MAX)
440 lr->end = lr->start + len;
441 clear_perf_probe_point(&pp);
442 }
443 return ret;
444}
445
446
447static struct debuginfo *open_debuginfo(const char *module, bool silent)
448{
449 const char *path = module;
450 char reason[STRERR_BUFSIZE];
451 struct debuginfo *ret = NULL;
452 struct dso *dso = NULL;
453 int err;
454
455 if (!module || !strchr(module, '/')) {
456 err = kernel_get_module_dso(module, &dso);
457 if (err < 0) {
458 if (!dso || dso->load_errno == 0) {
459 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
460 strcpy(reason, "(unknown)");
461 } else
462 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
463 if (!silent)
464 pr_err("Failed to find the path for %s: %s\n",
465 module ?: "kernel", reason);
466 return NULL;
467 }
468 path = dso->long_name;
469 }
470 ret = debuginfo__new(path);
471 if (!ret && !silent) {
472 pr_warning("The %s file has no debug information.\n", path);
473 if (!module || !strtailcmp(path, ".ko"))
474 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
475 else
476 pr_warning("Rebuild with -g, ");
477 pr_warning("or install an appropriate debuginfo package.\n");
478 }
479 return ret;
480}
481
482
483static struct debuginfo *debuginfo_cache;
484static char *debuginfo_cache_path;
485
486static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
487{
488 const char *path = module;
489
490
491 if (!module)
492 path = "kernel";
493
494 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
495 goto out;
496
497
498 free(debuginfo_cache_path);
499 debuginfo_cache_path = strdup(path);
500 if (!debuginfo_cache_path) {
501 debuginfo__delete(debuginfo_cache);
502 debuginfo_cache = NULL;
503 goto out;
504 }
505
506 debuginfo_cache = open_debuginfo(module, silent);
507 if (!debuginfo_cache)
508 zfree(&debuginfo_cache_path);
509out:
510 return debuginfo_cache;
511}
512
513static void debuginfo_cache__exit(void)
514{
515 debuginfo__delete(debuginfo_cache);
516 debuginfo_cache = NULL;
517 zfree(&debuginfo_cache_path);
518}
519
520
521static int get_text_start_address(const char *exec, unsigned long *address)
522{
523 Elf *elf;
524 GElf_Ehdr ehdr;
525 GElf_Shdr shdr;
526 int fd, ret = -ENOENT;
527
528 fd = open(exec, O_RDONLY);
529 if (fd < 0)
530 return -errno;
531
532 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
533 if (elf == NULL) {
534 ret = -EINVAL;
535 goto out_close;
536 }
537
538 if (gelf_getehdr(elf, &ehdr) == NULL)
539 goto out;
540
541 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
542 goto out;
543
544 *address = shdr.sh_addr - shdr.sh_offset;
545 ret = 0;
546out:
547 elf_end(elf);
548out_close:
549 close(fd);
550
551 return ret;
552}
553
554
555
556
557static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
558 struct perf_probe_point *pp,
559 bool is_kprobe)
560{
561 struct debuginfo *dinfo = NULL;
562 unsigned long stext = 0;
563 u64 addr = tp->address;
564 int ret = -ENOENT;
565
566
567 if (!is_kprobe) {
568 if (!addr) {
569 ret = -EINVAL;
570 goto error;
571 }
572 ret = get_text_start_address(tp->module, &stext);
573 if (ret < 0)
574 goto error;
575 addr += stext;
576 } else if (tp->symbol) {
577
578 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
579 false, !!tp->module);
580 if (ret != 0)
581 goto error;
582 addr += tp->offset;
583 }
584
585 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
586 tp->module ? : "kernel");
587
588 dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
589 if (dinfo)
590 ret = debuginfo__find_probe_point(dinfo,
591 (unsigned long)addr, pp);
592 else
593 ret = -ENOENT;
594
595 if (ret > 0) {
596 pp->retprobe = tp->retprobe;
597 return 0;
598 }
599error:
600 pr_debug("Failed to find corresponding probes from debuginfo.\n");
601 return ret ? : -ENOENT;
602}
603
604
605static int post_process_probe_trace_point(struct probe_trace_point *tp,
606 struct map *map, unsigned long offs)
607{
608 struct symbol *sym;
609 u64 addr = tp->address - offs;
610
611 sym = map__find_symbol(map, addr);
612 if (!sym)
613 return -ENOENT;
614
615 if (strcmp(sym->name, tp->symbol)) {
616
617 if (!tp->realname)
618 tp->realname = tp->symbol;
619 else
620 free(tp->symbol);
621 tp->symbol = strdup(sym->name);
622 if (!tp->symbol)
623 return -ENOMEM;
624 }
625 tp->offset = addr - sym->start;
626 tp->address -= offs;
627
628 return 0;
629}
630
631
632
633
634
635
636
637
638
639static int
640post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
641 int ntevs, const char *pathname)
642{
643 struct map *map;
644 unsigned long stext = 0;
645 int i, ret = 0;
646
647
648 map = dso__new_map(pathname);
649 if (!map || get_text_start_address(pathname, &stext) < 0) {
650 pr_warning("Failed to get ELF symbols for %s\n", pathname);
651 return -EINVAL;
652 }
653
654 for (i = 0; i < ntevs; i++) {
655 ret = post_process_probe_trace_point(&tevs[i].point,
656 map, stext);
657 if (ret < 0)
658 break;
659 }
660 map__put(map);
661
662 return ret;
663}
664
665static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
666 int ntevs, const char *exec)
667{
668 int i, ret = 0;
669 unsigned long stext = 0;
670
671 if (!exec)
672 return 0;
673
674 ret = get_text_start_address(exec, &stext);
675 if (ret < 0)
676 return ret;
677
678 for (i = 0; i < ntevs && ret >= 0; i++) {
679
680 tevs[i].point.address -= stext;
681 tevs[i].point.module = strdup(exec);
682 if (!tevs[i].point.module) {
683 ret = -ENOMEM;
684 break;
685 }
686 tevs[i].uprobes = true;
687 }
688
689 return ret;
690}
691
692static int
693post_process_module_probe_trace_events(struct probe_trace_event *tevs,
694 int ntevs, const char *module,
695 struct debuginfo *dinfo)
696{
697 Dwarf_Addr text_offs = 0;
698 int i, ret = 0;
699 char *mod_name = NULL;
700 struct map *map;
701
702 if (!module)
703 return 0;
704
705 map = get_target_map(module, false);
706 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
707 pr_warning("Failed to get ELF symbols for %s\n", module);
708 return -EINVAL;
709 }
710
711 mod_name = find_module_name(module);
712 for (i = 0; i < ntevs; i++) {
713 ret = post_process_probe_trace_point(&tevs[i].point,
714 map, (unsigned long)text_offs);
715 if (ret < 0)
716 break;
717 tevs[i].point.module =
718 strdup(mod_name ? mod_name : module);
719 if (!tevs[i].point.module) {
720 ret = -ENOMEM;
721 break;
722 }
723 }
724
725 free(mod_name);
726 map__put(map);
727
728 return ret;
729}
730
731static int
732post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
733 int ntevs)
734{
735 struct ref_reloc_sym *reloc_sym;
736 char *tmp;
737 int i, skipped = 0;
738
739
740 if (symbol_conf.ignore_vmlinux_buildid)
741 return post_process_offline_probe_trace_events(tevs, ntevs,
742 symbol_conf.vmlinux_name);
743
744 reloc_sym = kernel_get_ref_reloc_sym();
745 if (!reloc_sym) {
746 pr_warning("Relocated base symbol is not found!\n");
747 return -EINVAL;
748 }
749
750 for (i = 0; i < ntevs; i++) {
751 if (!tevs[i].point.address)
752 continue;
753 if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
754 continue;
755
756 if (kprobe_warn_out_range(tevs[i].point.symbol,
757 tevs[i].point.address)) {
758 tmp = NULL;
759 skipped++;
760 } else {
761 tmp = strdup(reloc_sym->name);
762 if (!tmp)
763 return -ENOMEM;
764 }
765
766 if (!tevs[i].point.realname)
767 tevs[i].point.realname = tevs[i].point.symbol;
768 else
769 free(tevs[i].point.symbol);
770 tevs[i].point.symbol = tmp;
771 tevs[i].point.offset = tevs[i].point.address -
772 reloc_sym->unrelocated_addr;
773 }
774 return skipped;
775}
776
777void __weak
778arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
779 int ntevs __maybe_unused)
780{
781}
782
783
784static int post_process_probe_trace_events(struct perf_probe_event *pev,
785 struct probe_trace_event *tevs,
786 int ntevs, const char *module,
787 bool uprobe, struct debuginfo *dinfo)
788{
789 int ret;
790
791 if (uprobe)
792 ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
793 else if (module)
794
795 ret = post_process_module_probe_trace_events(tevs, ntevs,
796 module, dinfo);
797 else
798 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
799
800 if (ret >= 0)
801 arch__post_process_probe_trace_events(pev, ntevs);
802
803 return ret;
804}
805
806
807static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
808 struct probe_trace_event **tevs)
809{
810 bool need_dwarf = perf_probe_event_need_dwarf(pev);
811 struct perf_probe_point tmp;
812 struct debuginfo *dinfo;
813 int ntevs, ret = 0;
814
815 dinfo = open_debuginfo(pev->target, !need_dwarf);
816 if (!dinfo) {
817 if (need_dwarf)
818 return -ENOENT;
819 pr_debug("Could not open debuginfo. Try to use symbols.\n");
820 return 0;
821 }
822
823 pr_debug("Try to find probe point from debuginfo.\n");
824
825 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
826
827 if (ntevs == 0) {
828 ret = get_alternative_probe_event(dinfo, pev, &tmp);
829 if (!ret) {
830 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
831
832
833
834
835 clear_perf_probe_point(&pev->point);
836 memcpy(&pev->point, &tmp, sizeof(tmp));
837 }
838 }
839
840 if (ntevs > 0) {
841 pr_debug("Found %d probe_trace_events.\n", ntevs);
842 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
843 pev->target, pev->uprobes, dinfo);
844 if (ret < 0 || ret == ntevs) {
845 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
846 clear_probe_trace_events(*tevs, ntevs);
847 zfree(tevs);
848 ntevs = 0;
849 }
850 }
851
852 debuginfo__delete(dinfo);
853
854 if (ntevs == 0) {
855 pr_warning("Probe point '%s' not found.\n",
856 synthesize_perf_probe_point(&pev->point));
857 return -ENOENT;
858 } else if (ntevs < 0) {
859
860 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
861 if (ntevs == -EBADF)
862 pr_warning("Warning: No dwarf info found in the vmlinux - "
863 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
864 if (!need_dwarf) {
865 pr_debug("Trying to use symbols.\n");
866 return 0;
867 }
868 }
869 return ntevs;
870}
871
872#define LINEBUF_SIZE 256
873#define NR_ADDITIONAL_LINES 2
874
875static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
876{
877 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
878 const char *color = show_num ? "" : PERF_COLOR_BLUE;
879 const char *prefix = NULL;
880
881 do {
882 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
883 goto error;
884 if (skip)
885 continue;
886 if (!prefix) {
887 prefix = show_num ? "%7d " : " ";
888 color_fprintf(stdout, color, prefix, l);
889 }
890 color_fprintf(stdout, color, "%s", buf);
891
892 } while (strchr(buf, '\n') == NULL);
893
894 return 1;
895error:
896 if (ferror(fp)) {
897 pr_warning("File read error: %s\n",
898 str_error_r(errno, sbuf, sizeof(sbuf)));
899 return -1;
900 }
901 return 0;
902}
903
904static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
905{
906 int rv = __show_one_line(fp, l, skip, show_num);
907 if (rv == 0) {
908 pr_warning("Source file is shorter than expected.\n");
909 rv = -1;
910 }
911 return rv;
912}
913
914#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
915#define show_one_line(f,l) _show_one_line(f,l,false,false)
916#define skip_one_line(f,l) _show_one_line(f,l,true,false)
917#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
918
919
920
921
922
923static int __show_line_range(struct line_range *lr, const char *module,
924 bool user)
925{
926 int l = 1;
927 struct int_node *ln;
928 struct debuginfo *dinfo;
929 FILE *fp;
930 int ret;
931 char *tmp;
932 char sbuf[STRERR_BUFSIZE];
933
934
935 dinfo = open_debuginfo(module, false);
936 if (!dinfo)
937 return -ENOENT;
938
939 ret = debuginfo__find_line_range(dinfo, lr);
940 if (!ret) {
941 ret = get_alternative_line_range(dinfo, lr, module, user);
942 if (!ret)
943 ret = debuginfo__find_line_range(dinfo, lr);
944 }
945 debuginfo__delete(dinfo);
946 if (ret == 0 || ret == -ENOENT) {
947 pr_warning("Specified source line is not found.\n");
948 return -ENOENT;
949 } else if (ret < 0) {
950 pr_warning("Debuginfo analysis failed.\n");
951 return ret;
952 }
953
954
955 tmp = lr->path;
956 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
957
958
959 if (tmp != lr->path)
960 free(tmp);
961
962 if (ret < 0) {
963 pr_warning("Failed to find source file path.\n");
964 return ret;
965 }
966
967 setup_pager();
968
969 if (lr->function)
970 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
971 lr->start - lr->offset);
972 else
973 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
974
975 fp = fopen(lr->path, "r");
976 if (fp == NULL) {
977 pr_warning("Failed to open %s: %s\n", lr->path,
978 str_error_r(errno, sbuf, sizeof(sbuf)));
979 return -errno;
980 }
981
982 while (l < lr->start) {
983 ret = skip_one_line(fp, l++);
984 if (ret < 0)
985 goto end;
986 }
987
988 intlist__for_each_entry(ln, lr->line_list) {
989 for (; ln->i > l; l++) {
990 ret = show_one_line(fp, l - lr->offset);
991 if (ret < 0)
992 goto end;
993 }
994 ret = show_one_line_with_num(fp, l++ - lr->offset);
995 if (ret < 0)
996 goto end;
997 }
998
999 if (lr->end == INT_MAX)
1000 lr->end = l + NR_ADDITIONAL_LINES;
1001 while (l <= lr->end) {
1002 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1003 if (ret <= 0)
1004 break;
1005 }
1006end:
1007 fclose(fp);
1008 return ret;
1009}
1010
1011int show_line_range(struct line_range *lr, const char *module, bool user)
1012{
1013 int ret;
1014
1015 ret = init_probe_symbol_maps(user);
1016 if (ret < 0)
1017 return ret;
1018 ret = __show_line_range(lr, module, user);
1019 exit_probe_symbol_maps();
1020
1021 return ret;
1022}
1023
1024static int show_available_vars_at(struct debuginfo *dinfo,
1025 struct perf_probe_event *pev,
1026 struct strfilter *_filter)
1027{
1028 char *buf;
1029 int ret, i, nvars;
1030 struct str_node *node;
1031 struct variable_list *vls = NULL, *vl;
1032 struct perf_probe_point tmp;
1033 const char *var;
1034
1035 buf = synthesize_perf_probe_point(&pev->point);
1036 if (!buf)
1037 return -EINVAL;
1038 pr_debug("Searching variables at %s\n", buf);
1039
1040 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1041 if (!ret) {
1042 ret = get_alternative_probe_event(dinfo, pev, &tmp);
1043 if (!ret) {
1044 ret = debuginfo__find_available_vars_at(dinfo, pev,
1045 &vls);
1046
1047 clear_perf_probe_point(&tmp);
1048 }
1049 }
1050 if (ret <= 0) {
1051 if (ret == 0 || ret == -ENOENT) {
1052 pr_err("Failed to find the address of %s\n", buf);
1053 ret = -ENOENT;
1054 } else
1055 pr_warning("Debuginfo analysis failed.\n");
1056 goto end;
1057 }
1058
1059
1060 fprintf(stdout, "Available variables at %s\n", buf);
1061 for (i = 0; i < ret; i++) {
1062 vl = &vls[i];
1063
1064
1065
1066
1067 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1068 vl->point.offset);
1069 zfree(&vl->point.symbol);
1070 nvars = 0;
1071 if (vl->vars) {
1072 strlist__for_each_entry(node, vl->vars) {
1073 var = strchr(node->s, '\t') + 1;
1074 if (strfilter__compare(_filter, var)) {
1075 fprintf(stdout, "\t\t%s\n", node->s);
1076 nvars++;
1077 }
1078 }
1079 strlist__delete(vl->vars);
1080 }
1081 if (nvars == 0)
1082 fprintf(stdout, "\t\t(No matched variables)\n");
1083 }
1084 free(vls);
1085end:
1086 free(buf);
1087 return ret;
1088}
1089
1090
1091int show_available_vars(struct perf_probe_event *pevs, int npevs,
1092 struct strfilter *_filter)
1093{
1094 int i, ret = 0;
1095 struct debuginfo *dinfo;
1096
1097 ret = init_probe_symbol_maps(pevs->uprobes);
1098 if (ret < 0)
1099 return ret;
1100
1101 dinfo = open_debuginfo(pevs->target, false);
1102 if (!dinfo) {
1103 ret = -ENOENT;
1104 goto out;
1105 }
1106
1107 setup_pager();
1108
1109 for (i = 0; i < npevs && ret >= 0; i++)
1110 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1111
1112 debuginfo__delete(dinfo);
1113out:
1114 exit_probe_symbol_maps();
1115 return ret;
1116}
1117
1118#else
1119
1120static void debuginfo_cache__exit(void)
1121{
1122}
1123
1124static int
1125find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1126 struct perf_probe_point *pp __maybe_unused,
1127 bool is_kprobe __maybe_unused)
1128{
1129 return -ENOSYS;
1130}
1131
1132static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1133 struct probe_trace_event **tevs __maybe_unused)
1134{
1135 if (perf_probe_event_need_dwarf(pev)) {
1136 pr_warning("Debuginfo-analysis is not supported.\n");
1137 return -ENOSYS;
1138 }
1139
1140 return 0;
1141}
1142
1143int show_line_range(struct line_range *lr __maybe_unused,
1144 const char *module __maybe_unused,
1145 bool user __maybe_unused)
1146{
1147 pr_warning("Debuginfo-analysis is not supported.\n");
1148 return -ENOSYS;
1149}
1150
1151int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1152 int npevs __maybe_unused,
1153 struct strfilter *filter __maybe_unused)
1154{
1155 pr_warning("Debuginfo-analysis is not supported.\n");
1156 return -ENOSYS;
1157}
1158#endif
1159
1160void line_range__clear(struct line_range *lr)
1161{
1162 free(lr->function);
1163 free(lr->file);
1164 free(lr->path);
1165 free(lr->comp_dir);
1166 intlist__delete(lr->line_list);
1167 memset(lr, 0, sizeof(*lr));
1168}
1169
1170int line_range__init(struct line_range *lr)
1171{
1172 memset(lr, 0, sizeof(*lr));
1173 lr->line_list = intlist__new(NULL);
1174 if (!lr->line_list)
1175 return -ENOMEM;
1176 else
1177 return 0;
1178}
1179
1180static int parse_line_num(char **ptr, int *val, const char *what)
1181{
1182 const char *start = *ptr;
1183
1184 errno = 0;
1185 *val = strtol(*ptr, ptr, 0);
1186 if (errno || *ptr == start) {
1187 semantic_error("'%s' is not a valid number.\n", what);
1188 return -EINVAL;
1189 }
1190 return 0;
1191}
1192
1193
1194static bool is_c_func_name(const char *name)
1195{
1196 if (!isalpha(*name) && *name != '_')
1197 return false;
1198 while (*++name != '\0') {
1199 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1200 return false;
1201 }
1202 return true;
1203}
1204
1205
1206
1207
1208
1209
1210
1211
1212int parse_line_range_desc(const char *arg, struct line_range *lr)
1213{
1214 char *range, *file, *name = strdup(arg);
1215 int err;
1216
1217 if (!name)
1218 return -ENOMEM;
1219
1220 lr->start = 0;
1221 lr->end = INT_MAX;
1222
1223 range = strchr(name, ':');
1224 if (range) {
1225 *range++ = '\0';
1226
1227 err = parse_line_num(&range, &lr->start, "start line");
1228 if (err)
1229 goto err;
1230
1231 if (*range == '+' || *range == '-') {
1232 const char c = *range++;
1233
1234 err = parse_line_num(&range, &lr->end, "end line");
1235 if (err)
1236 goto err;
1237
1238 if (c == '+') {
1239 lr->end += lr->start;
1240
1241
1242
1243
1244
1245
1246 lr->end--;
1247 }
1248 }
1249
1250 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1251
1252 err = -EINVAL;
1253 if (lr->start > lr->end) {
1254 semantic_error("Start line must be smaller"
1255 " than end line.\n");
1256 goto err;
1257 }
1258 if (*range != '\0') {
1259 semantic_error("Tailing with invalid str '%s'.\n", range);
1260 goto err;
1261 }
1262 }
1263
1264 file = strchr(name, '@');
1265 if (file) {
1266 *file = '\0';
1267 lr->file = strdup(++file);
1268 if (lr->file == NULL) {
1269 err = -ENOMEM;
1270 goto err;
1271 }
1272 lr->function = name;
1273 } else if (strchr(name, '/') || strchr(name, '.'))
1274 lr->file = name;
1275 else if (is_c_func_name(name))
1276 lr->function = name;
1277 else {
1278 semantic_error("'%s' is not a valid function name.\n", name);
1279 err = -EINVAL;
1280 goto err;
1281 }
1282
1283 return 0;
1284err:
1285 free(name);
1286 return err;
1287}
1288
1289static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1290{
1291 char *ptr;
1292
1293 ptr = strpbrk_esc(*arg, ":");
1294 if (ptr) {
1295 *ptr = '\0';
1296 if (!pev->sdt && !is_c_func_name(*arg))
1297 goto ng_name;
1298 pev->group = strdup_esc(*arg);
1299 if (!pev->group)
1300 return -ENOMEM;
1301 *arg = ptr + 1;
1302 } else
1303 pev->group = NULL;
1304
1305 pev->event = strdup_esc(*arg);
1306 if (pev->event == NULL)
1307 return -ENOMEM;
1308
1309 if (!pev->sdt && !is_c_func_name(pev->event)) {
1310 zfree(&pev->event);
1311ng_name:
1312 zfree(&pev->group);
1313 semantic_error("%s is bad for event name -it must "
1314 "follow C symbol-naming rule.\n", *arg);
1315 return -EINVAL;
1316 }
1317 return 0;
1318}
1319
1320
1321static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1322{
1323 struct perf_probe_point *pp = &pev->point;
1324 char *ptr, *tmp;
1325 char c, nc = 0;
1326 bool file_spec = false;
1327 int ret;
1328
1329
1330
1331
1332
1333
1334
1335 if (!arg)
1336 return -EINVAL;
1337
1338 if (is_sdt_event(arg)) {
1339 pev->sdt = true;
1340 if (arg[0] == '%')
1341 arg++;
1342 }
1343
1344 ptr = strpbrk_esc(arg, ";=@+%");
1345 if (pev->sdt) {
1346 if (ptr) {
1347 if (*ptr != '@') {
1348 semantic_error("%s must be an SDT name.\n",
1349 arg);
1350 return -EINVAL;
1351 }
1352
1353 tmp = build_id_cache__complement(ptr + 1);
1354 if (tmp) {
1355 pev->target = build_id_cache__origname(tmp);
1356 free(tmp);
1357 } else
1358 pev->target = strdup_esc(ptr + 1);
1359 if (!pev->target)
1360 return -ENOMEM;
1361 *ptr = '\0';
1362 }
1363 ret = parse_perf_probe_event_name(&arg, pev);
1364 if (ret == 0) {
1365 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1366 ret = -errno;
1367 }
1368 return ret;
1369 }
1370
1371 if (ptr && *ptr == '=') {
1372 *ptr = '\0';
1373 tmp = ptr + 1;
1374 ret = parse_perf_probe_event_name(&arg, pev);
1375 if (ret < 0)
1376 return ret;
1377
1378 arg = tmp;
1379 }
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392 if (!strpbrk_esc(arg, "+@%")) {
1393 ptr = strpbrk_esc(arg, ";:");
1394
1395 if (ptr && memchr(arg, '.', ptr - arg))
1396 file_spec = true;
1397 }
1398
1399 ptr = strpbrk_esc(arg, ";:+@%");
1400 if (ptr) {
1401 nc = *ptr;
1402 *ptr++ = '\0';
1403 }
1404
1405 if (arg[0] == '\0')
1406 tmp = NULL;
1407 else {
1408 tmp = strdup_esc(arg);
1409 if (tmp == NULL)
1410 return -ENOMEM;
1411 }
1412
1413 if (file_spec)
1414 pp->file = tmp;
1415 else {
1416 pp->function = tmp;
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427 if (tmp && !strncmp(tmp, "0x", 2)) {
1428 pp->abs_address = strtoul(pp->function, &tmp, 0);
1429 if (*tmp != '\0') {
1430 semantic_error("Invalid absolute address.\n");
1431 return -EINVAL;
1432 }
1433 }
1434 }
1435
1436
1437 while (ptr) {
1438 arg = ptr;
1439 c = nc;
1440 if (c == ';') {
1441 pp->lazy_line = strdup(arg);
1442 if (pp->lazy_line == NULL)
1443 return -ENOMEM;
1444 break;
1445 }
1446 ptr = strpbrk_esc(arg, ";:+@%");
1447 if (ptr) {
1448 nc = *ptr;
1449 *ptr++ = '\0';
1450 }
1451 switch (c) {
1452 case ':':
1453 pp->line = strtoul(arg, &tmp, 0);
1454 if (*tmp != '\0') {
1455 semantic_error("There is non-digit char"
1456 " in line number.\n");
1457 return -EINVAL;
1458 }
1459 break;
1460 case '+':
1461 pp->offset = strtoul(arg, &tmp, 0);
1462 if (*tmp != '\0') {
1463 semantic_error("There is non-digit character"
1464 " in offset.\n");
1465 return -EINVAL;
1466 }
1467 break;
1468 case '@':
1469 if (pp->file) {
1470 semantic_error("SRC@SRC is not allowed.\n");
1471 return -EINVAL;
1472 }
1473 pp->file = strdup_esc(arg);
1474 if (pp->file == NULL)
1475 return -ENOMEM;
1476 break;
1477 case '%':
1478 if (strcmp(arg, "return") == 0) {
1479 pp->retprobe = 1;
1480 } else {
1481 semantic_error("%%%s is not supported.\n", arg);
1482 return -ENOTSUP;
1483 }
1484 break;
1485 default:
1486 pr_err("This program has a bug at %s:%d.\n",
1487 __FILE__, __LINE__);
1488 return -ENOTSUP;
1489 break;
1490 }
1491 }
1492
1493
1494 if (pp->lazy_line && pp->line) {
1495 semantic_error("Lazy pattern can't be used with"
1496 " line number.\n");
1497 return -EINVAL;
1498 }
1499
1500 if (pp->lazy_line && pp->offset) {
1501 semantic_error("Lazy pattern can't be used with offset.\n");
1502 return -EINVAL;
1503 }
1504
1505 if (pp->line && pp->offset) {
1506 semantic_error("Offset can't be used with line number.\n");
1507 return -EINVAL;
1508 }
1509
1510 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1511 semantic_error("File always requires line number or "
1512 "lazy pattern.\n");
1513 return -EINVAL;
1514 }
1515
1516 if (pp->offset && !pp->function) {
1517 semantic_error("Offset requires an entry function.\n");
1518 return -EINVAL;
1519 }
1520
1521 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1522 semantic_error("Offset/Line/Lazy pattern can't be used with "
1523 "return probe.\n");
1524 return -EINVAL;
1525 }
1526
1527 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1528 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1529 pp->lazy_line);
1530 return 0;
1531}
1532
1533
1534static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1535{
1536 char *tmp, *goodname;
1537 struct perf_probe_arg_field **fieldp;
1538
1539 pr_debug("parsing arg: %s into ", str);
1540
1541 tmp = strchr(str, '=');
1542 if (tmp) {
1543 arg->name = strndup(str, tmp - str);
1544 if (arg->name == NULL)
1545 return -ENOMEM;
1546 pr_debug("name:%s ", arg->name);
1547 str = tmp + 1;
1548 }
1549
1550 tmp = strchr(str, ':');
1551 if (tmp) {
1552 *tmp = '\0';
1553 arg->type = strdup(tmp + 1);
1554 if (arg->type == NULL)
1555 return -ENOMEM;
1556 pr_debug("type:%s ", arg->type);
1557 }
1558
1559 tmp = strpbrk(str, "-.[");
1560 if (!is_c_varname(str) || !tmp) {
1561
1562 arg->var = strdup(str);
1563 if (arg->var == NULL)
1564 return -ENOMEM;
1565 pr_debug("%s\n", arg->var);
1566 return 0;
1567 }
1568
1569
1570 arg->var = strndup(str, tmp - str);
1571 if (arg->var == NULL)
1572 return -ENOMEM;
1573 goodname = arg->var;
1574 pr_debug("%s, ", arg->var);
1575 fieldp = &arg->field;
1576
1577 do {
1578 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1579 if (*fieldp == NULL)
1580 return -ENOMEM;
1581 if (*tmp == '[') {
1582 str = tmp;
1583 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1584 (*fieldp)->ref = true;
1585 if (*tmp != ']' || tmp == str + 1) {
1586 semantic_error("Array index must be a"
1587 " number.\n");
1588 return -EINVAL;
1589 }
1590 tmp++;
1591 if (*tmp == '\0')
1592 tmp = NULL;
1593 } else {
1594 if (*tmp == '.') {
1595 str = tmp + 1;
1596 (*fieldp)->ref = false;
1597 } else if (tmp[1] == '>') {
1598 str = tmp + 2;
1599 (*fieldp)->ref = true;
1600 } else {
1601 semantic_error("Argument parse error: %s\n",
1602 str);
1603 return -EINVAL;
1604 }
1605 tmp = strpbrk(str, "-.[");
1606 }
1607 if (tmp) {
1608 (*fieldp)->name = strndup(str, tmp - str);
1609 if ((*fieldp)->name == NULL)
1610 return -ENOMEM;
1611 if (*str != '[')
1612 goodname = (*fieldp)->name;
1613 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1614 fieldp = &(*fieldp)->next;
1615 }
1616 } while (tmp);
1617 (*fieldp)->name = strdup(str);
1618 if ((*fieldp)->name == NULL)
1619 return -ENOMEM;
1620 if (*str != '[')
1621 goodname = (*fieldp)->name;
1622 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1623
1624
1625 if (!arg->name) {
1626 arg->name = strdup(goodname);
1627 if (arg->name == NULL)
1628 return -ENOMEM;
1629 }
1630 return 0;
1631}
1632
1633
1634int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1635{
1636 char **argv;
1637 int argc, i, ret = 0;
1638
1639 argv = argv_split(cmd, &argc);
1640 if (!argv) {
1641 pr_debug("Failed to split arguments.\n");
1642 return -ENOMEM;
1643 }
1644 if (argc - 1 > MAX_PROBE_ARGS) {
1645 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1646 ret = -ERANGE;
1647 goto out;
1648 }
1649
1650 ret = parse_perf_probe_point(argv[0], pev);
1651 if (ret < 0)
1652 goto out;
1653
1654
1655 pev->nargs = argc - 1;
1656 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1657 if (pev->args == NULL) {
1658 ret = -ENOMEM;
1659 goto out;
1660 }
1661 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1662 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1663 if (ret >= 0 &&
1664 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1665 semantic_error("You can't specify local variable for"
1666 " kretprobe.\n");
1667 ret = -EINVAL;
1668 }
1669 }
1670out:
1671 argv_free(argv);
1672
1673 return ret;
1674}
1675
1676
1677bool perf_probe_with_var(struct perf_probe_event *pev)
1678{
1679 int i = 0;
1680
1681 for (i = 0; i < pev->nargs; i++)
1682 if (is_c_varname(pev->args[i].var) ||
1683 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1684 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1685 return true;
1686 return false;
1687}
1688
1689
1690bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1691{
1692 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1693 return true;
1694
1695 if (perf_probe_with_var(pev))
1696 return true;
1697
1698 return false;
1699}
1700
1701
1702int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1703{
1704 struct probe_trace_point *tp = &tev->point;
1705 char pr;
1706 char *p;
1707 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1708 int ret, i, argc;
1709 char **argv;
1710
1711 pr_debug("Parsing probe_events: %s\n", cmd);
1712 argv = argv_split(cmd, &argc);
1713 if (!argv) {
1714 pr_debug("Failed to split arguments.\n");
1715 return -ENOMEM;
1716 }
1717 if (argc < 2) {
1718 semantic_error("Too few probe arguments.\n");
1719 ret = -ERANGE;
1720 goto out;
1721 }
1722
1723
1724 argv0_str = strdup(argv[0]);
1725 if (argv0_str == NULL) {
1726 ret = -ENOMEM;
1727 goto out;
1728 }
1729 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1730 fmt2_str = strtok_r(NULL, "/", &fmt);
1731 fmt3_str = strtok_r(NULL, " \t", &fmt);
1732 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1733 || fmt3_str == NULL) {
1734 semantic_error("Failed to parse event name: %s\n", argv[0]);
1735 ret = -EINVAL;
1736 goto out;
1737 }
1738 pr = fmt1_str[0];
1739 tev->group = strdup(fmt2_str);
1740 tev->event = strdup(fmt3_str);
1741 if (tev->group == NULL || tev->event == NULL) {
1742 ret = -ENOMEM;
1743 goto out;
1744 }
1745 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1746
1747 tp->retprobe = (pr == 'r');
1748
1749
1750 p = strchr(argv[1], ':');
1751 if (p) {
1752 tp->module = strndup(argv[1], p - argv[1]);
1753 if (!tp->module) {
1754 ret = -ENOMEM;
1755 goto out;
1756 }
1757 tev->uprobes = (tp->module[0] == '/');
1758 p++;
1759 } else
1760 p = argv[1];
1761 fmt1_str = strtok_r(p, "+", &fmt);
1762
1763 if (fmt1_str[0] == '0') {
1764
1765
1766
1767
1768
1769
1770
1771 if (strcmp(fmt1_str, "0x") == 0) {
1772 if (!argv[2] || strcmp(argv[2], "(null)")) {
1773 ret = -EINVAL;
1774 goto out;
1775 }
1776 tp->address = 0;
1777
1778 free(argv[2]);
1779 for (i = 2; argv[i + 1] != NULL; i++)
1780 argv[i] = argv[i + 1];
1781
1782 argv[i] = NULL;
1783 argc -= 1;
1784 } else
1785 tp->address = strtoul(fmt1_str, NULL, 0);
1786 } else {
1787
1788 tp->symbol = strdup(fmt1_str);
1789 if (tp->symbol == NULL) {
1790 ret = -ENOMEM;
1791 goto out;
1792 }
1793 fmt2_str = strtok_r(NULL, "", &fmt);
1794 if (fmt2_str == NULL)
1795 tp->offset = 0;
1796 else
1797 tp->offset = strtoul(fmt2_str, NULL, 10);
1798 }
1799
1800 if (tev->uprobes) {
1801 fmt2_str = strchr(p, '(');
1802 if (fmt2_str)
1803 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1804 }
1805
1806 tev->nargs = argc - 2;
1807 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1808 if (tev->args == NULL) {
1809 ret = -ENOMEM;
1810 goto out;
1811 }
1812 for (i = 0; i < tev->nargs; i++) {
1813 p = strchr(argv[i + 2], '=');
1814 if (p)
1815 *p++ = '\0';
1816 else
1817 p = argv[i + 2];
1818 tev->args[i].name = strdup(argv[i + 2]);
1819
1820 tev->args[i].value = strdup(p);
1821 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1822 ret = -ENOMEM;
1823 goto out;
1824 }
1825 }
1826 ret = 0;
1827out:
1828 free(argv0_str);
1829 argv_free(argv);
1830 return ret;
1831}
1832
1833
1834char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1835{
1836 struct perf_probe_arg_field *field = pa->field;
1837 struct strbuf buf;
1838 char *ret = NULL;
1839 int err;
1840
1841 if (strbuf_init(&buf, 64) < 0)
1842 return NULL;
1843
1844 if (pa->name && pa->var)
1845 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1846 else
1847 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1848 if (err)
1849 goto out;
1850
1851 while (field) {
1852 if (field->name[0] == '[')
1853 err = strbuf_addstr(&buf, field->name);
1854 else
1855 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1856 field->name);
1857 field = field->next;
1858 if (err)
1859 goto out;
1860 }
1861
1862 if (pa->type)
1863 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1864 goto out;
1865
1866 ret = strbuf_detach(&buf, NULL);
1867out:
1868 strbuf_release(&buf);
1869 return ret;
1870}
1871
1872
1873char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1874{
1875 struct strbuf buf;
1876 char *tmp, *ret = NULL;
1877 int len, err = 0;
1878
1879 if (strbuf_init(&buf, 64) < 0)
1880 return NULL;
1881
1882 if (pp->function) {
1883 if (strbuf_addstr(&buf, pp->function) < 0)
1884 goto out;
1885 if (pp->offset)
1886 err = strbuf_addf(&buf, "+%lu", pp->offset);
1887 else if (pp->line)
1888 err = strbuf_addf(&buf, ":%d", pp->line);
1889 else if (pp->retprobe)
1890 err = strbuf_addstr(&buf, "%return");
1891 if (err)
1892 goto out;
1893 }
1894 if (pp->file) {
1895 tmp = pp->file;
1896 len = strlen(tmp);
1897 if (len > 30) {
1898 tmp = strchr(pp->file + len - 30, '/');
1899 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1900 }
1901 err = strbuf_addf(&buf, "@%s", tmp);
1902 if (!err && !pp->function && pp->line)
1903 err = strbuf_addf(&buf, ":%d", pp->line);
1904 }
1905 if (!err)
1906 ret = strbuf_detach(&buf, NULL);
1907out:
1908 strbuf_release(&buf);
1909 return ret;
1910}
1911
1912char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1913{
1914 struct strbuf buf;
1915 char *tmp, *ret = NULL;
1916 int i;
1917
1918 if (strbuf_init(&buf, 64))
1919 return NULL;
1920 if (pev->event)
1921 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1922 pev->event) < 0)
1923 goto out;
1924
1925 tmp = synthesize_perf_probe_point(&pev->point);
1926 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1927 goto out;
1928 free(tmp);
1929
1930 for (i = 0; i < pev->nargs; i++) {
1931 tmp = synthesize_perf_probe_arg(pev->args + i);
1932 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1933 goto out;
1934 free(tmp);
1935 }
1936
1937 ret = strbuf_detach(&buf, NULL);
1938out:
1939 strbuf_release(&buf);
1940 return ret;
1941}
1942
1943static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1944 struct strbuf *buf, int depth)
1945{
1946 int err;
1947 if (ref->next) {
1948 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1949 depth + 1);
1950 if (depth < 0)
1951 return depth;
1952 }
1953 err = strbuf_addf(buf, "%+ld(", ref->offset);
1954 return (err < 0) ? err : depth;
1955}
1956
1957static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1958 struct strbuf *buf)
1959{
1960 struct probe_trace_arg_ref *ref = arg->ref;
1961 int depth = 0, err;
1962
1963
1964 if (arg->name)
1965 err = strbuf_addf(buf, " %s=", arg->name);
1966 else
1967 err = strbuf_addch(buf, ' ');
1968 if (err)
1969 return err;
1970
1971
1972 if (arg->value[0] == '@' && arg->ref)
1973 ref = ref->next;
1974
1975
1976 if (ref) {
1977 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
1978 if (depth < 0)
1979 return depth;
1980 }
1981
1982
1983 if (arg->value[0] == '@' && arg->ref)
1984 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
1985 else
1986 err = strbuf_addstr(buf, arg->value);
1987
1988
1989 while (!err && depth--)
1990 err = strbuf_addch(buf, ')');
1991
1992
1993 if (!err && arg->type)
1994 err = strbuf_addf(buf, ":%s", arg->type);
1995
1996 return err;
1997}
1998
1999static int
2000synthesize_uprobe_trace_def(struct probe_trace_event *tev, struct strbuf *buf)
2001{
2002 struct probe_trace_point *tp = &tev->point;
2003 int err;
2004
2005 err = strbuf_addf(buf, "%s:0x%lx", tp->module, tp->address);
2006
2007 if (err >= 0 && tp->ref_ctr_offset) {
2008 if (!uprobe_ref_ctr_is_supported())
2009 return -1;
2010 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2011 }
2012 return err >= 0 ? 0 : -1;
2013}
2014
2015char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2016{
2017 struct probe_trace_point *tp = &tev->point;
2018 struct strbuf buf;
2019 char *ret = NULL;
2020 int i, err;
2021
2022
2023 if (tev->uprobes && !tp->module)
2024 return NULL;
2025
2026 if (strbuf_init(&buf, 32) < 0)
2027 return NULL;
2028
2029 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2030 tev->group, tev->event) < 0)
2031 goto error;
2032
2033
2034
2035
2036
2037
2038 if (tev->uprobes && !tp->address) {
2039 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2040 goto error;
2041 }
2042
2043
2044 if (tev->uprobes) {
2045 err = synthesize_uprobe_trace_def(tev, &buf);
2046 } else if (!strncmp(tp->symbol, "0x", 2)) {
2047
2048 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2049 tp->module ? ":" : "", tp->address);
2050 } else {
2051 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2052 tp->module ? ":" : "", tp->symbol, tp->offset);
2053 }
2054
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
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
2157 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2158 if (ret < 0)
2159 return ret;
2160
2161
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
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
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
2486 if (strfilter__compare(filter, tev->event))
2487 return true;
2488
2489
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
2532 debuginfo_cache__exit();
2533
2534 return ret;
2535}
2536
2537
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
2582 p = strpbrk(nbase, ".@");
2583 if (p && p != nbase)
2584 *p = '\0';
2585
2586
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
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
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
2634static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2635{
2636 int i;
2637 char *buf = synthesize_probe_trace_command(tev);
2638 struct probe_trace_point *tp = &tev->point;
2639
2640 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2641 pr_warning("A semaphore is associated with %s:%s and "
2642 "seems your kernel doesn't support it.\n",
2643 tev->group, tev->event);
2644 }
2645
2646
2647 if (!tev->uprobes || tev->nargs == 0 || !buf)
2648 goto out;
2649
2650 for (i = 0; i < tev->nargs; i++)
2651 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2652 pr_warning("Please upgrade your kernel to at least "
2653 "3.14 to have access to feature %s\n",
2654 tev->args[i].value);
2655 break;
2656 }
2657out:
2658 free(buf);
2659}
2660
2661
2662static int probe_trace_event__set_name(struct probe_trace_event *tev,
2663 struct perf_probe_event *pev,
2664 struct strlist *namelist,
2665 bool allow_suffix)
2666{
2667 const char *event, *group;
2668 char buf[64];
2669 int ret;
2670
2671
2672 if (pev->event && !pev->sdt)
2673 event = pev->event;
2674 else if (tev->event)
2675 event = tev->event;
2676 else {
2677
2678 if (pev->point.function &&
2679 (strncmp(pev->point.function, "0x", 2) != 0) &&
2680 !strisglob(pev->point.function))
2681 event = pev->point.function;
2682 else
2683 event = tev->point.realname;
2684 }
2685 if (pev->group && !pev->sdt)
2686 group = pev->group;
2687 else if (tev->group)
2688 group = tev->group;
2689 else
2690 group = PERFPROBE_GROUP;
2691
2692
2693 ret = get_new_event_name(buf, 64, event, namelist,
2694 tev->point.retprobe, allow_suffix);
2695 if (ret < 0)
2696 return ret;
2697
2698 event = buf;
2699
2700 tev->event = strdup(event);
2701 tev->group = strdup(group);
2702 if (tev->event == NULL || tev->group == NULL)
2703 return -ENOMEM;
2704
2705
2706 strlist__add(namelist, event);
2707 return 0;
2708}
2709
2710static int __open_probe_file_and_namelist(bool uprobe,
2711 struct strlist **namelist)
2712{
2713 int fd;
2714
2715 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2716 if (fd < 0)
2717 return fd;
2718
2719
2720 *namelist = probe_file__get_namelist(fd);
2721 if (!(*namelist)) {
2722 pr_debug("Failed to get current event list.\n");
2723 close(fd);
2724 return -ENOMEM;
2725 }
2726 return fd;
2727}
2728
2729static int __add_probe_trace_events(struct perf_probe_event *pev,
2730 struct probe_trace_event *tevs,
2731 int ntevs, bool allow_suffix)
2732{
2733 int i, fd[2] = {-1, -1}, up, ret;
2734 struct probe_trace_event *tev = NULL;
2735 struct probe_cache *cache = NULL;
2736 struct strlist *namelist[2] = {NULL, NULL};
2737
2738 up = pev->uprobes ? 1 : 0;
2739 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2740 if (fd[up] < 0)
2741 return fd[up];
2742
2743 ret = 0;
2744 for (i = 0; i < ntevs; i++) {
2745 tev = &tevs[i];
2746 up = tev->uprobes ? 1 : 0;
2747 if (fd[up] == -1) {
2748 fd[up] = __open_probe_file_and_namelist(up,
2749 &namelist[up]);
2750 if (fd[up] < 0)
2751 goto close_out;
2752 }
2753
2754 if (!tev->point.symbol && !pev->uprobes)
2755 continue;
2756
2757
2758 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2759 allow_suffix);
2760 if (ret < 0)
2761 break;
2762
2763 ret = probe_file__add_event(fd[up], tev);
2764 if (ret < 0)
2765 break;
2766
2767
2768
2769
2770
2771
2772
2773 allow_suffix = true;
2774 }
2775 if (ret == -EINVAL && pev->uprobes)
2776 warn_uprobe_event_compat(tev);
2777 if (ret == 0 && probe_conf.cache) {
2778 cache = probe_cache__new(pev->target);
2779 if (!cache ||
2780 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2781 probe_cache__commit(cache) < 0)
2782 pr_warning("Failed to add event to probe cache\n");
2783 probe_cache__delete(cache);
2784 }
2785
2786close_out:
2787 for (up = 0; up < 2; up++) {
2788 strlist__delete(namelist[up]);
2789 if (fd[up] >= 0)
2790 close(fd[up]);
2791 }
2792 return ret;
2793}
2794
2795static int find_probe_functions(struct map *map, char *name,
2796 struct symbol **syms)
2797{
2798 int found = 0;
2799 struct symbol *sym;
2800 struct rb_node *tmp;
2801 const char *norm, *ver;
2802 char *buf = NULL;
2803 bool cut_version = true;
2804
2805 if (map__load(map) < 0)
2806 return 0;
2807
2808
2809 if (strchr(name, '@'))
2810 cut_version = false;
2811
2812 map__for_each_symbol(map, sym, tmp) {
2813 norm = arch__normalize_symbol_name(sym->name);
2814 if (!norm)
2815 continue;
2816
2817 if (cut_version) {
2818
2819 ver = strchr(norm, '@');
2820 if (ver) {
2821 buf = strndup(norm, ver - norm);
2822 if (!buf)
2823 return -ENOMEM;
2824 norm = buf;
2825 }
2826 }
2827
2828 if (strglobmatch(norm, name)) {
2829 found++;
2830 if (syms && found < probe_conf.max_probes)
2831 syms[found - 1] = sym;
2832 }
2833 if (buf)
2834 zfree(&buf);
2835 }
2836
2837 return found;
2838}
2839
2840void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2841 struct probe_trace_event *tev __maybe_unused,
2842 struct map *map __maybe_unused,
2843 struct symbol *sym __maybe_unused) { }
2844
2845
2846
2847
2848
2849static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2850 struct probe_trace_event **tevs)
2851{
2852 struct map *map = NULL;
2853 struct ref_reloc_sym *reloc_sym = NULL;
2854 struct symbol *sym;
2855 struct symbol **syms = NULL;
2856 struct probe_trace_event *tev;
2857 struct perf_probe_point *pp = &pev->point;
2858 struct probe_trace_point *tp;
2859 int num_matched_functions;
2860 int ret, i, j, skipped = 0;
2861 char *mod_name;
2862
2863 map = get_target_map(pev->target, pev->uprobes);
2864 if (!map) {
2865 ret = -EINVAL;
2866 goto out;
2867 }
2868
2869 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2870 if (!syms) {
2871 ret = -ENOMEM;
2872 goto out;
2873 }
2874
2875
2876
2877
2878
2879 num_matched_functions = find_probe_functions(map, pp->function, syms);
2880 if (num_matched_functions <= 0) {
2881 pr_err("Failed to find symbol %s in %s\n", pp->function,
2882 pev->target ? : "kernel");
2883 ret = -ENOENT;
2884 goto out;
2885 } else if (num_matched_functions > probe_conf.max_probes) {
2886 pr_err("Too many functions matched in %s\n",
2887 pev->target ? : "kernel");
2888 ret = -E2BIG;
2889 goto out;
2890 }
2891
2892
2893 if (!pev->uprobes && !pev->target &&
2894 (!pp->retprobe || kretprobe_offset_is_supported())) {
2895 reloc_sym = kernel_get_ref_reloc_sym();
2896 if (!reloc_sym) {
2897 pr_warning("Relocated base symbol is not found!\n");
2898 ret = -EINVAL;
2899 goto out;
2900 }
2901 }
2902
2903
2904 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2905 if (!*tevs) {
2906 ret = -ENOMEM;
2907 goto out;
2908 }
2909
2910 ret = 0;
2911
2912 for (j = 0; j < num_matched_functions; j++) {
2913 sym = syms[j];
2914
2915 tev = (*tevs) + ret;
2916 tp = &tev->point;
2917 if (ret == num_matched_functions) {
2918 pr_warning("Too many symbols are listed. Skip it.\n");
2919 break;
2920 }
2921 ret++;
2922
2923 if (pp->offset > sym->end - sym->start) {
2924 pr_warning("Offset %ld is bigger than the size of %s\n",
2925 pp->offset, sym->name);
2926 ret = -ENOENT;
2927 goto err_out;
2928 }
2929
2930 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2931
2932
2933 if (!pev->uprobes && !pev->target &&
2934 kprobe_warn_out_range(sym->name, tp->address)) {
2935 tp->symbol = NULL;
2936 skipped++;
2937 } else if (reloc_sym) {
2938 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2939 tp->offset = tp->address - reloc_sym->addr;
2940 } else {
2941 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2942 tp->offset = pp->offset;
2943 }
2944 tp->realname = strdup_or_goto(sym->name, nomem_out);
2945
2946 tp->retprobe = pp->retprobe;
2947 if (pev->target) {
2948 if (pev->uprobes) {
2949 tev->point.module = strdup_or_goto(pev->target,
2950 nomem_out);
2951 } else {
2952 mod_name = find_module_name(pev->target);
2953 tev->point.module =
2954 strdup(mod_name ? mod_name : pev->target);
2955 free(mod_name);
2956 if (!tev->point.module)
2957 goto nomem_out;
2958 }
2959 }
2960 tev->uprobes = pev->uprobes;
2961 tev->nargs = pev->nargs;
2962 if (tev->nargs) {
2963 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2964 tev->nargs);
2965 if (tev->args == NULL)
2966 goto nomem_out;
2967 }
2968 for (i = 0; i < tev->nargs; i++) {
2969 if (pev->args[i].name)
2970 tev->args[i].name =
2971 strdup_or_goto(pev->args[i].name,
2972 nomem_out);
2973
2974 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2975 nomem_out);
2976 if (pev->args[i].type)
2977 tev->args[i].type =
2978 strdup_or_goto(pev->args[i].type,
2979 nomem_out);
2980 }
2981 arch__fix_tev_from_maps(pev, tev, map, sym);
2982 }
2983 if (ret == skipped) {
2984 ret = -ENOENT;
2985 goto err_out;
2986 }
2987
2988out:
2989 map__put(map);
2990 free(syms);
2991 return ret;
2992
2993nomem_out:
2994 ret = -ENOMEM;
2995err_out:
2996 clear_probe_trace_events(*tevs, num_matched_functions);
2997 zfree(tevs);
2998 goto out;
2999}
3000
3001static int try_to_find_absolute_address(struct perf_probe_event *pev,
3002 struct probe_trace_event **tevs)
3003{
3004 struct perf_probe_point *pp = &pev->point;
3005 struct probe_trace_event *tev;
3006 struct probe_trace_point *tp;
3007 int i, err;
3008
3009 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3010 return -EINVAL;
3011 if (perf_probe_event_need_dwarf(pev))
3012 return -EINVAL;
3013
3014
3015
3016
3017
3018
3019
3020 *tevs = zalloc(sizeof(*tev));
3021 if (!*tevs)
3022 return -ENOMEM;
3023
3024 tev = *tevs;
3025 tp = &tev->point;
3026
3027
3028
3029
3030
3031
3032 tp->address = pev->point.abs_address;
3033 tp->retprobe = pp->retprobe;
3034 tev->uprobes = pev->uprobes;
3035
3036 err = -ENOMEM;
3037
3038
3039
3040
3041
3042 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
3043 goto errout;
3044
3045
3046 if ((!tev->uprobes) &&
3047 (kprobe_warn_out_range(tev->point.symbol,
3048 tev->point.address))) {
3049 err = -EACCES;
3050 goto errout;
3051 }
3052
3053 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3054 goto errout;
3055
3056 if (pev->target) {
3057 tp->module = strdup(pev->target);
3058 if (!tp->module)
3059 goto errout;
3060 }
3061
3062 if (tev->group) {
3063 tev->group = strdup(pev->group);
3064 if (!tev->group)
3065 goto errout;
3066 }
3067
3068 if (pev->event) {
3069 tev->event = strdup(pev->event);
3070 if (!tev->event)
3071 goto errout;
3072 }
3073
3074 tev->nargs = pev->nargs;
3075 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3076 if (!tev->args)
3077 goto errout;
3078
3079 for (i = 0; i < tev->nargs; i++)
3080 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3081
3082 return 1;
3083
3084errout:
3085 clear_probe_trace_events(*tevs, 1);
3086 *tevs = NULL;
3087 return err;
3088}
3089
3090
3091static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3092{
3093 void *ret;
3094
3095 ret = malloc(sz_a + sz_b);
3096 if (ret) {
3097 memcpy(ret, a, sz_a);
3098 memcpy(ret + sz_a, b, sz_b);
3099 }
3100 return ret;
3101}
3102
3103static int
3104concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3105 struct probe_trace_event **tevs2, int ntevs2)
3106{
3107 struct probe_trace_event *new_tevs;
3108 int ret = 0;
3109
3110 if (*ntevs == 0) {
3111 *tevs = *tevs2;
3112 *ntevs = ntevs2;
3113 *tevs2 = NULL;
3114 return 0;
3115 }
3116
3117 if (*ntevs + ntevs2 > probe_conf.max_probes)
3118 ret = -E2BIG;
3119 else {
3120
3121 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3122 *tevs2, ntevs2 * sizeof(**tevs2));
3123 if (!new_tevs)
3124 ret = -ENOMEM;
3125 else {
3126 free(*tevs);
3127 *tevs = new_tevs;
3128 *ntevs += ntevs2;
3129 }
3130 }
3131 if (ret < 0)
3132 clear_probe_trace_events(*tevs2, ntevs2);
3133 zfree(tevs2);
3134
3135 return ret;
3136}
3137
3138
3139
3140
3141
3142static int find_cached_events(struct perf_probe_event *pev,
3143 struct probe_trace_event **tevs,
3144 const char *target)
3145{
3146 struct probe_cache *cache;
3147 struct probe_cache_entry *entry;
3148 struct probe_trace_event *tmp_tevs = NULL;
3149 int ntevs = 0;
3150 int ret = 0;
3151
3152 cache = probe_cache__new(target);
3153
3154 if (!cache)
3155 return 0;
3156
3157 for_each_probe_cache_entry(entry, cache) {
3158
3159 if (!entry->pev.event || !entry->pev.group)
3160 continue;
3161 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3162 strglobmatch(entry->pev.event, pev->event)) {
3163 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3164 if (ret > 0)
3165 ret = concat_probe_trace_events(tevs, &ntevs,
3166 &tmp_tevs, ret);
3167 if (ret < 0)
3168 break;
3169 }
3170 }
3171 probe_cache__delete(cache);
3172 if (ret < 0) {
3173 clear_probe_trace_events(*tevs, ntevs);
3174 zfree(tevs);
3175 } else {
3176 ret = ntevs;
3177 if (ntevs > 0 && target && target[0] == '/')
3178 pev->uprobes = true;
3179 }
3180
3181 return ret;
3182}
3183
3184
3185static int find_cached_events_all(struct perf_probe_event *pev,
3186 struct probe_trace_event **tevs)
3187{
3188 struct probe_trace_event *tmp_tevs = NULL;
3189 struct strlist *bidlist;
3190 struct str_node *nd;
3191 char *pathname;
3192 int ntevs = 0;
3193 int ret;
3194
3195
3196 bidlist = build_id_cache__list_all(true);
3197 if (!bidlist) {
3198 ret = -errno;
3199 pr_debug("Failed to get buildids: %d\n", ret);
3200 return ret;
3201 }
3202
3203 ret = 0;
3204 strlist__for_each_entry(nd, bidlist) {
3205 pathname = build_id_cache__origname(nd->s);
3206 ret = find_cached_events(pev, &tmp_tevs, pathname);
3207
3208 if (ret > 0)
3209 ret = concat_probe_trace_events(tevs, &ntevs,
3210 &tmp_tevs, ret);
3211 free(pathname);
3212 if (ret < 0)
3213 break;
3214 }
3215 strlist__delete(bidlist);
3216
3217 if (ret < 0) {
3218 clear_probe_trace_events(*tevs, ntevs);
3219 zfree(tevs);
3220 } else
3221 ret = ntevs;
3222
3223 return ret;
3224}
3225
3226static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3227 struct probe_trace_event **tevs)
3228{
3229 struct probe_cache *cache;
3230 struct probe_cache_entry *entry;
3231 struct probe_trace_event *tev;
3232 struct str_node *node;
3233 int ret, i;
3234
3235 if (pev->sdt) {
3236
3237 if (!pev->target)
3238 return find_cached_events_all(pev, tevs);
3239 else
3240 return find_cached_events(pev, tevs, pev->target);
3241 }
3242 cache = probe_cache__new(pev->target);
3243 if (!cache)
3244 return 0;
3245
3246 entry = probe_cache__find(cache, pev);
3247 if (!entry) {
3248
3249 ret = pev->sdt ? -ENOENT : 0;
3250 goto out;
3251 }
3252
3253 ret = strlist__nr_entries(entry->tevlist);
3254 if (ret > probe_conf.max_probes) {
3255 pr_debug("Too many entries matched in the cache of %s\n",
3256 pev->target ? : "kernel");
3257 ret = -E2BIG;
3258 goto out;
3259 }
3260
3261 *tevs = zalloc(ret * sizeof(*tev));
3262 if (!*tevs) {
3263 ret = -ENOMEM;
3264 goto out;
3265 }
3266
3267 i = 0;
3268 strlist__for_each_entry(node, entry->tevlist) {
3269 tev = &(*tevs)[i++];
3270 ret = parse_probe_trace_command(node->s, tev);
3271 if (ret < 0)
3272 goto out;
3273
3274 tev->uprobes = pev->uprobes;
3275 }
3276 ret = i;
3277
3278out:
3279 probe_cache__delete(cache);
3280 return ret;
3281}
3282
3283static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3284 struct probe_trace_event **tevs)
3285{
3286 int ret;
3287
3288 if (!pev->group && !pev->sdt) {
3289
3290 if (!pev->uprobes) {
3291 pev->group = strdup(PERFPROBE_GROUP);
3292 ret = pev->group ? 0 : -ENOMEM;
3293 } else
3294 ret = convert_exec_to_group(pev->target, &pev->group);
3295 if (ret != 0) {
3296 pr_warning("Failed to make a group name.\n");
3297 return ret;
3298 }
3299 }
3300
3301 ret = try_to_find_absolute_address(pev, tevs);
3302 if (ret > 0)
3303 return ret;
3304
3305
3306 ret = find_probe_trace_events_from_cache(pev, tevs);
3307 if (ret > 0 || pev->sdt)
3308 return ret == 0 ? -ENOENT : ret;
3309
3310
3311 ret = try_to_find_probe_trace_events(pev, tevs);
3312 if (ret != 0)
3313 return ret;
3314
3315 return find_probe_trace_events_from_map(pev, tevs);
3316}
3317
3318int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3319{
3320 int i, ret;
3321
3322
3323 for (i = 0; i < npevs; i++) {
3324
3325 if (!pevs[i].uprobes)
3326 kprobe_blacklist__init();
3327
3328 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3329 if (ret < 0)
3330 return ret;
3331 pevs[i].ntevs = ret;
3332 }
3333
3334 kprobe_blacklist__release();
3335
3336 return 0;
3337}
3338
3339static int show_probe_trace_event(struct probe_trace_event *tev)
3340{
3341 char *buf = synthesize_probe_trace_command(tev);
3342
3343 if (!buf) {
3344 pr_debug("Failed to synthesize probe trace event.\n");
3345 return -EINVAL;
3346 }
3347
3348
3349 printf("%s\n", buf);
3350 free(buf);
3351
3352 return 0;
3353}
3354
3355int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3356{
3357 struct strlist *namelist = strlist__new(NULL, NULL);
3358 struct probe_trace_event *tev;
3359 struct perf_probe_event *pev;
3360 int i, j, ret = 0;
3361
3362 if (!namelist)
3363 return -ENOMEM;
3364
3365 for (j = 0; j < npevs && !ret; j++) {
3366 pev = &pevs[j];
3367 for (i = 0; i < pev->ntevs && !ret; i++) {
3368 tev = &pev->tevs[i];
3369
3370 if (!tev->point.symbol && !pev->uprobes)
3371 continue;
3372
3373
3374 ret = probe_trace_event__set_name(tev, pev,
3375 namelist, true);
3376 if (!ret)
3377 ret = show_probe_trace_event(tev);
3378 }
3379 }
3380 strlist__delete(namelist);
3381
3382 return ret;
3383}
3384
3385int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3386{
3387 int i, ret = 0;
3388
3389
3390 for (i = 0; i < npevs; i++) {
3391 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3392 pevs[i].ntevs,
3393 probe_conf.force_add);
3394 if (ret < 0)
3395 break;
3396 }
3397 return ret;
3398}
3399
3400void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3401{
3402 int i, j;
3403
3404
3405 for (i = 0; i < npevs; i++) {
3406 for (j = 0; j < pevs[i].ntevs; j++)
3407 clear_probe_trace_event(&pevs[i].tevs[j]);
3408 zfree(&pevs[i].tevs);
3409 pevs[i].ntevs = 0;
3410 clear_perf_probe_event(&pevs[i]);
3411 }
3412}
3413
3414int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3415{
3416 int ret;
3417
3418 ret = init_probe_symbol_maps(pevs->uprobes);
3419 if (ret < 0)
3420 return ret;
3421
3422 ret = convert_perf_probe_events(pevs, npevs);
3423 if (ret == 0)
3424 ret = apply_perf_probe_events(pevs, npevs);
3425
3426 cleanup_perf_probe_events(pevs, npevs);
3427
3428 exit_probe_symbol_maps();
3429 return ret;
3430}
3431
3432int del_perf_probe_events(struct strfilter *filter)
3433{
3434 int ret, ret2, ufd = -1, kfd = -1;
3435 char *str = strfilter__string(filter);
3436
3437 if (!str)
3438 return -EINVAL;
3439
3440
3441 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3442 if (ret < 0)
3443 goto out;
3444
3445 ret = probe_file__del_events(kfd, filter);
3446 if (ret < 0 && ret != -ENOENT)
3447 goto error;
3448
3449 ret2 = probe_file__del_events(ufd, filter);
3450 if (ret2 < 0 && ret2 != -ENOENT) {
3451 ret = ret2;
3452 goto error;
3453 }
3454 ret = 0;
3455
3456error:
3457 if (kfd >= 0)
3458 close(kfd);
3459 if (ufd >= 0)
3460 close(ufd);
3461out:
3462 free(str);
3463
3464 return ret;
3465}
3466
3467int show_available_funcs(const char *target, struct strfilter *_filter,
3468 bool user)
3469{
3470 struct rb_node *nd;
3471 struct map *map;
3472 int ret;
3473
3474 ret = init_probe_symbol_maps(user);
3475 if (ret < 0)
3476 return ret;
3477
3478
3479 map = get_target_map(target, user);
3480 if (!map) {
3481 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3482 return -EINVAL;
3483 }
3484
3485 ret = map__load(map);
3486 if (ret) {
3487 if (ret == -2) {
3488 char *str = strfilter__string(_filter);
3489 pr_err("Failed to find symbols matched to \"%s\"\n",
3490 str);
3491 free(str);
3492 } else
3493 pr_err("Failed to load symbols in %s\n",
3494 (target) ? : "kernel");
3495 goto end;
3496 }
3497 if (!dso__sorted_by_name(map->dso))
3498 dso__sort_by_name(map->dso);
3499
3500
3501 setup_pager();
3502
3503 for (nd = rb_first(&map->dso->symbol_names); nd; nd = rb_next(nd)) {
3504 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3505
3506 if (strfilter__compare(_filter, pos->sym.name))
3507 printf("%s\n", pos->sym.name);
3508 }
3509end:
3510 map__put(map);
3511 exit_probe_symbol_maps();
3512
3513 return ret;
3514}
3515
3516int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3517 struct perf_probe_arg *pvar)
3518{
3519 tvar->value = strdup(pvar->var);
3520 if (tvar->value == NULL)
3521 return -ENOMEM;
3522 if (pvar->type) {
3523 tvar->type = strdup(pvar->type);
3524 if (tvar->type == NULL)
3525 return -ENOMEM;
3526 }
3527 if (pvar->name) {
3528 tvar->name = strdup(pvar->name);
3529 if (tvar->name == NULL)
3530 return -ENOMEM;
3531 } else
3532 tvar->name = NULL;
3533 return 0;
3534}
3535