1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdarg.h>
32#include <limits.h>
33#include <elf.h>
34
35#include "util.h"
36#include "event.h"
37#include "strlist.h"
38#include "debug.h"
39#include "cache.h"
40#include "color.h"
41#include "symbol.h"
42#include "thread.h"
43#include <api/fs/debugfs.h>
44#include <api/fs/tracefs.h>
45#include "trace-event.h"
46#include "probe-event.h"
47#include "probe-finder.h"
48#include "probe-file.h"
49#include "session.h"
50
51#define MAX_CMDLEN 256
52#define PERFPROBE_GROUP "probe"
53
54bool probe_event_dry_run;
55struct probe_conf probe_conf;
56
57#define semantic_error(msg ...) pr_err("Semantic error :" msg)
58
59int e_snprintf(char *str, size_t size, const char *format, ...)
60{
61 int ret;
62 va_list ap;
63 va_start(ap, format);
64 ret = vsnprintf(str, size, format, ap);
65 va_end(ap);
66 if (ret >= (int)size)
67 ret = -E2BIG;
68 return ret;
69}
70
71static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
72static struct machine *host_machine;
73
74
75static int init_symbol_maps(bool user_only)
76{
77 int ret;
78
79 symbol_conf.sort_by_name = true;
80 symbol_conf.allow_aliases = true;
81 ret = symbol__init(NULL);
82 if (ret < 0) {
83 pr_debug("Failed to init symbol map.\n");
84 goto out;
85 }
86
87 if (host_machine || user_only)
88 return 0;
89
90 if (symbol_conf.vmlinux_name)
91 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
92
93 host_machine = machine__new_host();
94 if (!host_machine) {
95 pr_debug("machine__new_host() failed.\n");
96 symbol__exit();
97 ret = -1;
98 }
99out:
100 if (ret < 0)
101 pr_warning("Failed to init vmlinux path.\n");
102 return ret;
103}
104
105static void exit_symbol_maps(void)
106{
107 if (host_machine) {
108 machine__delete(host_machine);
109 host_machine = NULL;
110 }
111 symbol__exit();
112}
113
114static struct symbol *__find_kernel_function_by_name(const char *name,
115 struct map **mapp)
116{
117 return machine__find_kernel_function_by_name(host_machine, name, mapp,
118 NULL);
119}
120
121static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
122{
123 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
124}
125
126static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
127{
128
129 struct kmap *kmap;
130
131 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
132 return NULL;
133
134 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
135 if (!kmap)
136 return NULL;
137 return kmap->ref_reloc_sym;
138}
139
140static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
141{
142 struct ref_reloc_sym *reloc_sym;
143 struct symbol *sym;
144 struct map *map;
145
146
147 reloc_sym = kernel_get_ref_reloc_sym();
148 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
149 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
150 else {
151 sym = __find_kernel_function_by_name(name, &map);
152 if (sym)
153 return map->unmap_ip(map, sym->start) -
154 ((reloc) ? 0 : map->reloc);
155 }
156 return 0;
157}
158
159static struct map *kernel_get_module_map(const char *module)
160{
161 struct map_groups *grp = &host_machine->kmaps;
162 struct maps *maps = &grp->maps[MAP__FUNCTION];
163 struct map *pos;
164
165
166 if (module && strchr(module, '/'))
167 return machine__findnew_module_map(host_machine, 0, module);
168
169 if (!module)
170 module = "kernel";
171
172 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
173 if (strncmp(pos->dso->short_name + 1, module,
174 pos->dso->short_name_len - 2) == 0) {
175 return pos;
176 }
177 }
178 return NULL;
179}
180
181static struct map *get_target_map(const char *target, bool user)
182{
183
184 if (user)
185 return dso__new_map(target);
186 else
187 return kernel_get_module_map(target);
188}
189
190static void put_target_map(struct map *map, bool user)
191{
192 if (map && user) {
193
194 map__put(map);
195 }
196}
197
198
199static int convert_exec_to_group(const char *exec, char **result)
200{
201 char *ptr1, *ptr2, *exec_copy;
202 char buf[64];
203 int ret;
204
205 exec_copy = strdup(exec);
206 if (!exec_copy)
207 return -ENOMEM;
208
209 ptr1 = basename(exec_copy);
210 if (!ptr1) {
211 ret = -EINVAL;
212 goto out;
213 }
214
215 ptr2 = strpbrk(ptr1, "-._");
216 if (ptr2)
217 *ptr2 = '\0';
218 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
219 if (ret < 0)
220 goto out;
221
222 *result = strdup(buf);
223 ret = *result ? 0 : -ENOMEM;
224
225out:
226 free(exec_copy);
227 return ret;
228}
229
230static void clear_perf_probe_point(struct perf_probe_point *pp)
231{
232 free(pp->file);
233 free(pp->function);
234 free(pp->lazy_line);
235}
236
237static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
238{
239 int i;
240
241 for (i = 0; i < ntevs; i++)
242 clear_probe_trace_event(tevs + i);
243}
244
245static bool kprobe_blacklist__listed(unsigned long address);
246static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
247{
248 u64 etext_addr;
249
250
251 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
252
253 if (etext_addr != 0 && etext_addr < address)
254 pr_warning("%s is out of .text, skip it.\n", symbol);
255 else if (kprobe_blacklist__listed(address))
256 pr_warning("%s is blacklisted function, skip it.\n", symbol);
257 else
258 return false;
259
260 return true;
261}
262
263#ifdef HAVE_DWARF_SUPPORT
264
265static int kernel_get_module_dso(const char *module, struct dso **pdso)
266{
267 struct dso *dso;
268 struct map *map;
269 const char *vmlinux_name;
270 int ret = 0;
271
272 if (module) {
273 char module_name[128];
274
275 snprintf(module_name, sizeof(module_name), "[%s]", module);
276 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
277 if (map) {
278 dso = map->dso;
279 goto found;
280 }
281 pr_debug("Failed to find module %s.\n", module);
282 return -ENOENT;
283 }
284
285 map = host_machine->vmlinux_maps[MAP__FUNCTION];
286 dso = map->dso;
287
288 vmlinux_name = symbol_conf.vmlinux_name;
289 dso->load_errno = 0;
290 if (vmlinux_name)
291 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
292 else
293 ret = dso__load_vmlinux_path(dso, map, NULL);
294found:
295 *pdso = dso;
296 return ret;
297}
298
299
300
301
302
303
304static int find_alternative_probe_point(struct debuginfo *dinfo,
305 struct perf_probe_point *pp,
306 struct perf_probe_point *result,
307 const char *target, bool uprobes)
308{
309 struct map *map = NULL;
310 struct symbol *sym;
311 u64 address = 0;
312 int ret = -ENOENT;
313
314
315 if (!pp->function || pp->file)
316 return -ENOTSUP;
317
318 map = get_target_map(target, uprobes);
319 if (!map)
320 return -EINVAL;
321
322
323 map__for_each_symbol_by_name(map, pp->function, sym) {
324 if (uprobes)
325 address = sym->start;
326 else
327 address = map->unmap_ip(map, sym->start);
328 break;
329 }
330 if (!address) {
331 ret = -ENOENT;
332 goto out;
333 }
334 pr_debug("Symbol %s address found : %" PRIx64 "\n",
335 pp->function, address);
336
337 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
338 result);
339 if (ret <= 0)
340 ret = (!ret) ? -ENOENT : ret;
341 else {
342 result->offset += pp->offset;
343 result->line += pp->line;
344 result->retprobe = pp->retprobe;
345 ret = 0;
346 }
347
348out:
349 put_target_map(map, uprobes);
350 return ret;
351
352}
353
354static int get_alternative_probe_event(struct debuginfo *dinfo,
355 struct perf_probe_event *pev,
356 struct perf_probe_point *tmp)
357{
358 int ret;
359
360 memcpy(tmp, &pev->point, sizeof(*tmp));
361 memset(&pev->point, 0, sizeof(pev->point));
362 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
363 pev->target, pev->uprobes);
364 if (ret < 0)
365 memcpy(&pev->point, tmp, sizeof(*tmp));
366
367 return ret;
368}
369
370static int get_alternative_line_range(struct debuginfo *dinfo,
371 struct line_range *lr,
372 const char *target, bool user)
373{
374 struct perf_probe_point pp = { .function = lr->function,
375 .file = lr->file,
376 .line = lr->start };
377 struct perf_probe_point result;
378 int ret, len = 0;
379
380 memset(&result, 0, sizeof(result));
381
382 if (lr->end != INT_MAX)
383 len = lr->end - lr->start;
384 ret = find_alternative_probe_point(dinfo, &pp, &result,
385 target, user);
386 if (!ret) {
387 lr->function = result.function;
388 lr->file = result.file;
389 lr->start = result.line;
390 if (lr->end != INT_MAX)
391 lr->end = lr->start + len;
392 clear_perf_probe_point(&pp);
393 }
394 return ret;
395}
396
397
398static struct debuginfo *open_debuginfo(const char *module, bool silent)
399{
400 const char *path = module;
401 char reason[STRERR_BUFSIZE];
402 struct debuginfo *ret = NULL;
403 struct dso *dso = NULL;
404 int err;
405
406 if (!module || !strchr(module, '/')) {
407 err = kernel_get_module_dso(module, &dso);
408 if (err < 0) {
409 if (!dso || dso->load_errno == 0) {
410 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
411 strcpy(reason, "(unknown)");
412 } else
413 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
414 if (!silent)
415 pr_err("Failed to find the path for %s: %s\n",
416 module ?: "kernel", reason);
417 return NULL;
418 }
419 path = dso->long_name;
420 }
421 ret = debuginfo__new(path);
422 if (!ret && !silent) {
423 pr_warning("The %s file has no debug information.\n", path);
424 if (!module || !strtailcmp(path, ".ko"))
425 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
426 else
427 pr_warning("Rebuild with -g, ");
428 pr_warning("or install an appropriate debuginfo package.\n");
429 }
430 return ret;
431}
432
433
434static struct debuginfo *debuginfo_cache;
435static char *debuginfo_cache_path;
436
437static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
438{
439 if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
440 (!debuginfo_cache_path && !module && debuginfo_cache))
441 goto out;
442
443
444 free(debuginfo_cache_path);
445 if (module) {
446 debuginfo_cache_path = strdup(module);
447 if (!debuginfo_cache_path) {
448 debuginfo__delete(debuginfo_cache);
449 debuginfo_cache = NULL;
450 goto out;
451 }
452 }
453
454 debuginfo_cache = open_debuginfo(module, silent);
455 if (!debuginfo_cache)
456 zfree(&debuginfo_cache_path);
457out:
458 return debuginfo_cache;
459}
460
461static void debuginfo_cache__exit(void)
462{
463 debuginfo__delete(debuginfo_cache);
464 debuginfo_cache = NULL;
465 zfree(&debuginfo_cache_path);
466}
467
468
469static int get_text_start_address(const char *exec, unsigned long *address)
470{
471 Elf *elf;
472 GElf_Ehdr ehdr;
473 GElf_Shdr shdr;
474 int fd, ret = -ENOENT;
475
476 fd = open(exec, O_RDONLY);
477 if (fd < 0)
478 return -errno;
479
480 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
481 if (elf == NULL)
482 return -EINVAL;
483
484 if (gelf_getehdr(elf, &ehdr) == NULL)
485 goto out;
486
487 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
488 goto out;
489
490 *address = shdr.sh_addr - shdr.sh_offset;
491 ret = 0;
492out:
493 elf_end(elf);
494 return ret;
495}
496
497
498
499
500static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
501 struct perf_probe_point *pp,
502 bool is_kprobe)
503{
504 struct debuginfo *dinfo = NULL;
505 unsigned long stext = 0;
506 u64 addr = tp->address;
507 int ret = -ENOENT;
508
509
510 if (!is_kprobe) {
511 if (!addr) {
512 ret = -EINVAL;
513 goto error;
514 }
515 ret = get_text_start_address(tp->module, &stext);
516 if (ret < 0)
517 goto error;
518 addr += stext;
519 } else if (tp->symbol) {
520 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
521 if (addr == 0)
522 goto error;
523 addr += tp->offset;
524 }
525
526 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
527 tp->module ? : "kernel");
528
529 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
530 if (dinfo)
531 ret = debuginfo__find_probe_point(dinfo,
532 (unsigned long)addr, pp);
533 else
534 ret = -ENOENT;
535
536 if (ret > 0) {
537 pp->retprobe = tp->retprobe;
538 return 0;
539 }
540error:
541 pr_debug("Failed to find corresponding probes from debuginfo.\n");
542 return ret ? : -ENOENT;
543}
544
545static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
546 int ntevs, const char *exec)
547{
548 int i, ret = 0;
549 unsigned long stext = 0;
550
551 if (!exec)
552 return 0;
553
554 ret = get_text_start_address(exec, &stext);
555 if (ret < 0)
556 return ret;
557
558 for (i = 0; i < ntevs && ret >= 0; i++) {
559
560 tevs[i].point.address -= stext;
561 tevs[i].point.module = strdup(exec);
562 if (!tevs[i].point.module) {
563 ret = -ENOMEM;
564 break;
565 }
566 tevs[i].uprobes = true;
567 }
568
569 return ret;
570}
571
572static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
573 int ntevs, const char *module)
574{
575 int i, ret = 0;
576 char *tmp;
577
578 if (!module)
579 return 0;
580
581 tmp = strrchr(module, '/');
582 if (tmp) {
583
584 module = strdup(tmp + 1);
585 if (!module)
586 return -ENOMEM;
587 tmp = strchr(module, '.');
588 if (tmp)
589 *tmp = '\0';
590 tmp = (char *)module;
591 }
592
593 for (i = 0; i < ntevs; i++) {
594 tevs[i].point.module = strdup(module);
595 if (!tevs[i].point.module) {
596 ret = -ENOMEM;
597 break;
598 }
599 }
600
601 free(tmp);
602 return ret;
603}
604
605
606static int post_process_probe_trace_events(struct probe_trace_event *tevs,
607 int ntevs, const char *module,
608 bool uprobe)
609{
610 struct ref_reloc_sym *reloc_sym;
611 char *tmp;
612 int i, skipped = 0;
613
614 if (uprobe)
615 return add_exec_to_probe_trace_events(tevs, ntevs, module);
616
617
618 if (module)
619 return add_module_to_probe_trace_events(tevs, ntevs, module);
620
621 reloc_sym = kernel_get_ref_reloc_sym();
622 if (!reloc_sym) {
623 pr_warning("Relocated base symbol is not found!\n");
624 return -EINVAL;
625 }
626
627 for (i = 0; i < ntevs; i++) {
628 if (!tevs[i].point.address || tevs[i].point.retprobe)
629 continue;
630
631 if (kprobe_warn_out_range(tevs[i].point.symbol,
632 tevs[i].point.address)) {
633 tmp = NULL;
634 skipped++;
635 } else {
636 tmp = strdup(reloc_sym->name);
637 if (!tmp)
638 return -ENOMEM;
639 }
640
641 if (!tevs[i].point.realname)
642 tevs[i].point.realname = tevs[i].point.symbol;
643 else
644 free(tevs[i].point.symbol);
645 tevs[i].point.symbol = tmp;
646 tevs[i].point.offset = tevs[i].point.address -
647 reloc_sym->unrelocated_addr;
648 }
649 return skipped;
650}
651
652
653static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
654 struct probe_trace_event **tevs)
655{
656 bool need_dwarf = perf_probe_event_need_dwarf(pev);
657 struct perf_probe_point tmp;
658 struct debuginfo *dinfo;
659 int ntevs, ret = 0;
660
661 dinfo = open_debuginfo(pev->target, !need_dwarf);
662 if (!dinfo) {
663 if (need_dwarf)
664 return -ENOENT;
665 pr_debug("Could not open debuginfo. Try to use symbols.\n");
666 return 0;
667 }
668
669 pr_debug("Try to find probe point from debuginfo.\n");
670
671 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
672
673 if (ntevs == 0) {
674 ret = get_alternative_probe_event(dinfo, pev, &tmp);
675 if (!ret) {
676 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
677
678
679
680
681 clear_perf_probe_point(&pev->point);
682 memcpy(&pev->point, &tmp, sizeof(tmp));
683 }
684 }
685
686 debuginfo__delete(dinfo);
687
688 if (ntevs > 0) {
689 pr_debug("Found %d probe_trace_events.\n", ntevs);
690 ret = post_process_probe_trace_events(*tevs, ntevs,
691 pev->target, pev->uprobes);
692 if (ret < 0 || ret == ntevs) {
693 clear_probe_trace_events(*tevs, ntevs);
694 zfree(tevs);
695 }
696 if (ret != ntevs)
697 return ret < 0 ? ret : ntevs;
698 ntevs = 0;
699
700 }
701
702 if (ntevs == 0) {
703 pr_warning("Probe point '%s' not found.\n",
704 synthesize_perf_probe_point(&pev->point));
705 return -ENOENT;
706 }
707
708 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
709 if (ntevs < 0) {
710 if (ntevs == -EBADF)
711 pr_warning("Warning: No dwarf info found in the vmlinux - "
712 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
713 if (!need_dwarf) {
714 pr_debug("Trying to use symbols.\n");
715 return 0;
716 }
717 }
718 return ntevs;
719}
720
721#define LINEBUF_SIZE 256
722#define NR_ADDITIONAL_LINES 2
723
724static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
725{
726 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
727 const char *color = show_num ? "" : PERF_COLOR_BLUE;
728 const char *prefix = NULL;
729
730 do {
731 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
732 goto error;
733 if (skip)
734 continue;
735 if (!prefix) {
736 prefix = show_num ? "%7d " : " ";
737 color_fprintf(stdout, color, prefix, l);
738 }
739 color_fprintf(stdout, color, "%s", buf);
740
741 } while (strchr(buf, '\n') == NULL);
742
743 return 1;
744error:
745 if (ferror(fp)) {
746 pr_warning("File read error: %s\n",
747 strerror_r(errno, sbuf, sizeof(sbuf)));
748 return -1;
749 }
750 return 0;
751}
752
753static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
754{
755 int rv = __show_one_line(fp, l, skip, show_num);
756 if (rv == 0) {
757 pr_warning("Source file is shorter than expected.\n");
758 rv = -1;
759 }
760 return rv;
761}
762
763#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
764#define show_one_line(f,l) _show_one_line(f,l,false,false)
765#define skip_one_line(f,l) _show_one_line(f,l,true,false)
766#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
767
768
769
770
771
772static int __show_line_range(struct line_range *lr, const char *module,
773 bool user)
774{
775 int l = 1;
776 struct int_node *ln;
777 struct debuginfo *dinfo;
778 FILE *fp;
779 int ret;
780 char *tmp;
781 char sbuf[STRERR_BUFSIZE];
782
783
784 dinfo = open_debuginfo(module, false);
785 if (!dinfo)
786 return -ENOENT;
787
788 ret = debuginfo__find_line_range(dinfo, lr);
789 if (!ret) {
790 ret = get_alternative_line_range(dinfo, lr, module, user);
791 if (!ret)
792 ret = debuginfo__find_line_range(dinfo, lr);
793 }
794 debuginfo__delete(dinfo);
795 if (ret == 0 || ret == -ENOENT) {
796 pr_warning("Specified source line is not found.\n");
797 return -ENOENT;
798 } else if (ret < 0) {
799 pr_warning("Debuginfo analysis failed.\n");
800 return ret;
801 }
802
803
804 tmp = lr->path;
805 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
806
807
808 if (tmp != lr->path)
809 free(tmp);
810
811 if (ret < 0) {
812 pr_warning("Failed to find source file path.\n");
813 return ret;
814 }
815
816 setup_pager();
817
818 if (lr->function)
819 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
820 lr->start - lr->offset);
821 else
822 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
823
824 fp = fopen(lr->path, "r");
825 if (fp == NULL) {
826 pr_warning("Failed to open %s: %s\n", lr->path,
827 strerror_r(errno, sbuf, sizeof(sbuf)));
828 return -errno;
829 }
830
831 while (l < lr->start) {
832 ret = skip_one_line(fp, l++);
833 if (ret < 0)
834 goto end;
835 }
836
837 intlist__for_each(ln, lr->line_list) {
838 for (; ln->i > l; l++) {
839 ret = show_one_line(fp, l - lr->offset);
840 if (ret < 0)
841 goto end;
842 }
843 ret = show_one_line_with_num(fp, l++ - lr->offset);
844 if (ret < 0)
845 goto end;
846 }
847
848 if (lr->end == INT_MAX)
849 lr->end = l + NR_ADDITIONAL_LINES;
850 while (l <= lr->end) {
851 ret = show_one_line_or_eof(fp, l++ - lr->offset);
852 if (ret <= 0)
853 break;
854 }
855end:
856 fclose(fp);
857 return ret;
858}
859
860int show_line_range(struct line_range *lr, const char *module, bool user)
861{
862 int ret;
863
864 ret = init_symbol_maps(user);
865 if (ret < 0)
866 return ret;
867 ret = __show_line_range(lr, module, user);
868 exit_symbol_maps();
869
870 return ret;
871}
872
873static int show_available_vars_at(struct debuginfo *dinfo,
874 struct perf_probe_event *pev,
875 struct strfilter *_filter)
876{
877 char *buf;
878 int ret, i, nvars;
879 struct str_node *node;
880 struct variable_list *vls = NULL, *vl;
881 struct perf_probe_point tmp;
882 const char *var;
883
884 buf = synthesize_perf_probe_point(&pev->point);
885 if (!buf)
886 return -EINVAL;
887 pr_debug("Searching variables at %s\n", buf);
888
889 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
890 if (!ret) {
891 ret = get_alternative_probe_event(dinfo, pev, &tmp);
892 if (!ret) {
893 ret = debuginfo__find_available_vars_at(dinfo, pev,
894 &vls);
895
896 clear_perf_probe_point(&tmp);
897 }
898 }
899 if (ret <= 0) {
900 if (ret == 0 || ret == -ENOENT) {
901 pr_err("Failed to find the address of %s\n", buf);
902 ret = -ENOENT;
903 } else
904 pr_warning("Debuginfo analysis failed.\n");
905 goto end;
906 }
907
908
909 fprintf(stdout, "Available variables at %s\n", buf);
910 for (i = 0; i < ret; i++) {
911 vl = &vls[i];
912
913
914
915
916 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
917 vl->point.offset);
918 zfree(&vl->point.symbol);
919 nvars = 0;
920 if (vl->vars) {
921 strlist__for_each(node, vl->vars) {
922 var = strchr(node->s, '\t') + 1;
923 if (strfilter__compare(_filter, var)) {
924 fprintf(stdout, "\t\t%s\n", node->s);
925 nvars++;
926 }
927 }
928 strlist__delete(vl->vars);
929 }
930 if (nvars == 0)
931 fprintf(stdout, "\t\t(No matched variables)\n");
932 }
933 free(vls);
934end:
935 free(buf);
936 return ret;
937}
938
939
940int show_available_vars(struct perf_probe_event *pevs, int npevs,
941 struct strfilter *_filter)
942{
943 int i, ret = 0;
944 struct debuginfo *dinfo;
945
946 ret = init_symbol_maps(pevs->uprobes);
947 if (ret < 0)
948 return ret;
949
950 dinfo = open_debuginfo(pevs->target, false);
951 if (!dinfo) {
952 ret = -ENOENT;
953 goto out;
954 }
955
956 setup_pager();
957
958 for (i = 0; i < npevs && ret >= 0; i++)
959 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
960
961 debuginfo__delete(dinfo);
962out:
963 exit_symbol_maps();
964 return ret;
965}
966
967#else
968
969static void debuginfo_cache__exit(void)
970{
971}
972
973static int
974find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
975 struct perf_probe_point *pp __maybe_unused,
976 bool is_kprobe __maybe_unused)
977{
978 return -ENOSYS;
979}
980
981static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
982 struct probe_trace_event **tevs __maybe_unused)
983{
984 if (perf_probe_event_need_dwarf(pev)) {
985 pr_warning("Debuginfo-analysis is not supported.\n");
986 return -ENOSYS;
987 }
988
989 return 0;
990}
991
992int show_line_range(struct line_range *lr __maybe_unused,
993 const char *module __maybe_unused,
994 bool user __maybe_unused)
995{
996 pr_warning("Debuginfo-analysis is not supported.\n");
997 return -ENOSYS;
998}
999
1000int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1001 int npevs __maybe_unused,
1002 struct strfilter *filter __maybe_unused)
1003{
1004 pr_warning("Debuginfo-analysis is not supported.\n");
1005 return -ENOSYS;
1006}
1007#endif
1008
1009void line_range__clear(struct line_range *lr)
1010{
1011 free(lr->function);
1012 free(lr->file);
1013 free(lr->path);
1014 free(lr->comp_dir);
1015 intlist__delete(lr->line_list);
1016 memset(lr, 0, sizeof(*lr));
1017}
1018
1019int line_range__init(struct line_range *lr)
1020{
1021 memset(lr, 0, sizeof(*lr));
1022 lr->line_list = intlist__new(NULL);
1023 if (!lr->line_list)
1024 return -ENOMEM;
1025 else
1026 return 0;
1027}
1028
1029static int parse_line_num(char **ptr, int *val, const char *what)
1030{
1031 const char *start = *ptr;
1032
1033 errno = 0;
1034 *val = strtol(*ptr, ptr, 0);
1035 if (errno || *ptr == start) {
1036 semantic_error("'%s' is not a valid number.\n", what);
1037 return -EINVAL;
1038 }
1039 return 0;
1040}
1041
1042
1043static bool is_c_func_name(const char *name)
1044{
1045 if (!isalpha(*name) && *name != '_')
1046 return false;
1047 while (*++name != '\0') {
1048 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1049 return false;
1050 }
1051 return true;
1052}
1053
1054
1055
1056
1057
1058
1059
1060
1061int parse_line_range_desc(const char *arg, struct line_range *lr)
1062{
1063 char *range, *file, *name = strdup(arg);
1064 int err;
1065
1066 if (!name)
1067 return -ENOMEM;
1068
1069 lr->start = 0;
1070 lr->end = INT_MAX;
1071
1072 range = strchr(name, ':');
1073 if (range) {
1074 *range++ = '\0';
1075
1076 err = parse_line_num(&range, &lr->start, "start line");
1077 if (err)
1078 goto err;
1079
1080 if (*range == '+' || *range == '-') {
1081 const char c = *range++;
1082
1083 err = parse_line_num(&range, &lr->end, "end line");
1084 if (err)
1085 goto err;
1086
1087 if (c == '+') {
1088 lr->end += lr->start;
1089
1090
1091
1092
1093
1094
1095 lr->end--;
1096 }
1097 }
1098
1099 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1100
1101 err = -EINVAL;
1102 if (lr->start > lr->end) {
1103 semantic_error("Start line must be smaller"
1104 " than end line.\n");
1105 goto err;
1106 }
1107 if (*range != '\0') {
1108 semantic_error("Tailing with invalid str '%s'.\n", range);
1109 goto err;
1110 }
1111 }
1112
1113 file = strchr(name, '@');
1114 if (file) {
1115 *file = '\0';
1116 lr->file = strdup(++file);
1117 if (lr->file == NULL) {
1118 err = -ENOMEM;
1119 goto err;
1120 }
1121 lr->function = name;
1122 } else if (strchr(name, '/') || strchr(name, '.'))
1123 lr->file = name;
1124 else if (is_c_func_name(name))
1125 lr->function = name;
1126 else {
1127 semantic_error("'%s' is not a valid function name.\n", name);
1128 err = -EINVAL;
1129 goto err;
1130 }
1131
1132 return 0;
1133err:
1134 free(name);
1135 return err;
1136}
1137
1138
1139static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1140{
1141 struct perf_probe_point *pp = &pev->point;
1142 char *ptr, *tmp;
1143 char c, nc = 0;
1144 bool file_spec = false;
1145
1146
1147
1148
1149
1150
1151
1152 if (!arg)
1153 return -EINVAL;
1154
1155 ptr = strpbrk(arg, ";=@+%");
1156 if (ptr && *ptr == '=') {
1157 *ptr = '\0';
1158 tmp = ptr + 1;
1159 if (strchr(arg, ':')) {
1160 semantic_error("Group name is not supported yet.\n");
1161 return -ENOTSUP;
1162 }
1163 if (!is_c_func_name(arg)) {
1164 semantic_error("%s is bad for event name -it must "
1165 "follow C symbol-naming rule.\n", arg);
1166 return -EINVAL;
1167 }
1168 pev->event = strdup(arg);
1169 if (pev->event == NULL)
1170 return -ENOMEM;
1171 pev->group = NULL;
1172 arg = tmp;
1173 }
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1187
1188 if (memchr(arg, '.', ptr - arg))
1189 file_spec = true;
1190 }
1191
1192 ptr = strpbrk(arg, ";:+@%");
1193 if (ptr) {
1194 nc = *ptr;
1195 *ptr++ = '\0';
1196 }
1197
1198 if (arg[0] == '\0')
1199 tmp = NULL;
1200 else {
1201 tmp = strdup(arg);
1202 if (tmp == NULL)
1203 return -ENOMEM;
1204 }
1205
1206 if (file_spec)
1207 pp->file = tmp;
1208 else {
1209 pp->function = tmp;
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220 if (tmp && !strncmp(tmp, "0x", 2)) {
1221 pp->abs_address = strtoul(pp->function, &tmp, 0);
1222 if (*tmp != '\0') {
1223 semantic_error("Invalid absolute address.\n");
1224 return -EINVAL;
1225 }
1226 }
1227 }
1228
1229
1230 while (ptr) {
1231 arg = ptr;
1232 c = nc;
1233 if (c == ';') {
1234 pp->lazy_line = strdup(arg);
1235 if (pp->lazy_line == NULL)
1236 return -ENOMEM;
1237 break;
1238 }
1239 ptr = strpbrk(arg, ";:+@%");
1240 if (ptr) {
1241 nc = *ptr;
1242 *ptr++ = '\0';
1243 }
1244 switch (c) {
1245 case ':':
1246 pp->line = strtoul(arg, &tmp, 0);
1247 if (*tmp != '\0') {
1248 semantic_error("There is non-digit char"
1249 " in line number.\n");
1250 return -EINVAL;
1251 }
1252 break;
1253 case '+':
1254 pp->offset = strtoul(arg, &tmp, 0);
1255 if (*tmp != '\0') {
1256 semantic_error("There is non-digit character"
1257 " in offset.\n");
1258 return -EINVAL;
1259 }
1260 break;
1261 case '@':
1262 if (pp->file) {
1263 semantic_error("SRC@SRC is not allowed.\n");
1264 return -EINVAL;
1265 }
1266 pp->file = strdup(arg);
1267 if (pp->file == NULL)
1268 return -ENOMEM;
1269 break;
1270 case '%':
1271 if (strcmp(arg, "return") == 0) {
1272 pp->retprobe = 1;
1273 } else {
1274 semantic_error("%%%s is not supported.\n", arg);
1275 return -ENOTSUP;
1276 }
1277 break;
1278 default:
1279 pr_err("This program has a bug at %s:%d.\n",
1280 __FILE__, __LINE__);
1281 return -ENOTSUP;
1282 break;
1283 }
1284 }
1285
1286
1287 if (pp->lazy_line && pp->line) {
1288 semantic_error("Lazy pattern can't be used with"
1289 " line number.\n");
1290 return -EINVAL;
1291 }
1292
1293 if (pp->lazy_line && pp->offset) {
1294 semantic_error("Lazy pattern can't be used with offset.\n");
1295 return -EINVAL;
1296 }
1297
1298 if (pp->line && pp->offset) {
1299 semantic_error("Offset can't be used with line number.\n");
1300 return -EINVAL;
1301 }
1302
1303 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1304 semantic_error("File always requires line number or "
1305 "lazy pattern.\n");
1306 return -EINVAL;
1307 }
1308
1309 if (pp->offset && !pp->function) {
1310 semantic_error("Offset requires an entry function.\n");
1311 return -EINVAL;
1312 }
1313
1314 if (pp->retprobe && !pp->function) {
1315 semantic_error("Return probe requires an entry function.\n");
1316 return -EINVAL;
1317 }
1318
1319 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1320 semantic_error("Offset/Line/Lazy pattern can't be used with "
1321 "return probe.\n");
1322 return -EINVAL;
1323 }
1324
1325 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1326 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1327 pp->lazy_line);
1328 return 0;
1329}
1330
1331
1332static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1333{
1334 char *tmp, *goodname;
1335 struct perf_probe_arg_field **fieldp;
1336
1337 pr_debug("parsing arg: %s into ", str);
1338
1339 tmp = strchr(str, '=');
1340 if (tmp) {
1341 arg->name = strndup(str, tmp - str);
1342 if (arg->name == NULL)
1343 return -ENOMEM;
1344 pr_debug("name:%s ", arg->name);
1345 str = tmp + 1;
1346 }
1347
1348 tmp = strchr(str, ':');
1349 if (tmp) {
1350 *tmp = '\0';
1351 arg->type = strdup(tmp + 1);
1352 if (arg->type == NULL)
1353 return -ENOMEM;
1354 pr_debug("type:%s ", arg->type);
1355 }
1356
1357 tmp = strpbrk(str, "-.[");
1358 if (!is_c_varname(str) || !tmp) {
1359
1360 arg->var = strdup(str);
1361 if (arg->var == NULL)
1362 return -ENOMEM;
1363 pr_debug("%s\n", arg->var);
1364 return 0;
1365 }
1366
1367
1368 arg->var = strndup(str, tmp - str);
1369 if (arg->var == NULL)
1370 return -ENOMEM;
1371 goodname = arg->var;
1372 pr_debug("%s, ", arg->var);
1373 fieldp = &arg->field;
1374
1375 do {
1376 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1377 if (*fieldp == NULL)
1378 return -ENOMEM;
1379 if (*tmp == '[') {
1380 str = tmp;
1381 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1382 (*fieldp)->ref = true;
1383 if (*tmp != ']' || tmp == str + 1) {
1384 semantic_error("Array index must be a"
1385 " number.\n");
1386 return -EINVAL;
1387 }
1388 tmp++;
1389 if (*tmp == '\0')
1390 tmp = NULL;
1391 } else {
1392 if (*tmp == '.') {
1393 str = tmp + 1;
1394 (*fieldp)->ref = false;
1395 } else if (tmp[1] == '>') {
1396 str = tmp + 2;
1397 (*fieldp)->ref = true;
1398 } else {
1399 semantic_error("Argument parse error: %s\n",
1400 str);
1401 return -EINVAL;
1402 }
1403 tmp = strpbrk(str, "-.[");
1404 }
1405 if (tmp) {
1406 (*fieldp)->name = strndup(str, tmp - str);
1407 if ((*fieldp)->name == NULL)
1408 return -ENOMEM;
1409 if (*str != '[')
1410 goodname = (*fieldp)->name;
1411 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1412 fieldp = &(*fieldp)->next;
1413 }
1414 } while (tmp);
1415 (*fieldp)->name = strdup(str);
1416 if ((*fieldp)->name == NULL)
1417 return -ENOMEM;
1418 if (*str != '[')
1419 goodname = (*fieldp)->name;
1420 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1421
1422
1423 if (!arg->name) {
1424 arg->name = strdup(goodname);
1425 if (arg->name == NULL)
1426 return -ENOMEM;
1427 }
1428 return 0;
1429}
1430
1431
1432int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1433{
1434 char **argv;
1435 int argc, i, ret = 0;
1436
1437 argv = argv_split(cmd, &argc);
1438 if (!argv) {
1439 pr_debug("Failed to split arguments.\n");
1440 return -ENOMEM;
1441 }
1442 if (argc - 1 > MAX_PROBE_ARGS) {
1443 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1444 ret = -ERANGE;
1445 goto out;
1446 }
1447
1448 ret = parse_perf_probe_point(argv[0], pev);
1449 if (ret < 0)
1450 goto out;
1451
1452
1453 pev->nargs = argc - 1;
1454 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1455 if (pev->args == NULL) {
1456 ret = -ENOMEM;
1457 goto out;
1458 }
1459 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1460 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1461 if (ret >= 0 &&
1462 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1463 semantic_error("You can't specify local variable for"
1464 " kretprobe.\n");
1465 ret = -EINVAL;
1466 }
1467 }
1468out:
1469 argv_free(argv);
1470
1471 return ret;
1472}
1473
1474
1475bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1476{
1477 int i;
1478
1479 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1480 return true;
1481
1482 for (i = 0; i < pev->nargs; i++)
1483 if (is_c_varname(pev->args[i].var))
1484 return true;
1485
1486 return false;
1487}
1488
1489
1490int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1491{
1492 struct probe_trace_point *tp = &tev->point;
1493 char pr;
1494 char *p;
1495 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1496 int ret, i, argc;
1497 char **argv;
1498
1499 pr_debug("Parsing probe_events: %s\n", cmd);
1500 argv = argv_split(cmd, &argc);
1501 if (!argv) {
1502 pr_debug("Failed to split arguments.\n");
1503 return -ENOMEM;
1504 }
1505 if (argc < 2) {
1506 semantic_error("Too few probe arguments.\n");
1507 ret = -ERANGE;
1508 goto out;
1509 }
1510
1511
1512 argv0_str = strdup(argv[0]);
1513 if (argv0_str == NULL) {
1514 ret = -ENOMEM;
1515 goto out;
1516 }
1517 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1518 fmt2_str = strtok_r(NULL, "/", &fmt);
1519 fmt3_str = strtok_r(NULL, " \t", &fmt);
1520 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1521 || fmt3_str == NULL) {
1522 semantic_error("Failed to parse event name: %s\n", argv[0]);
1523 ret = -EINVAL;
1524 goto out;
1525 }
1526 pr = fmt1_str[0];
1527 tev->group = strdup(fmt2_str);
1528 tev->event = strdup(fmt3_str);
1529 if (tev->group == NULL || tev->event == NULL) {
1530 ret = -ENOMEM;
1531 goto out;
1532 }
1533 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1534
1535 tp->retprobe = (pr == 'r');
1536
1537
1538 p = strchr(argv[1], ':');
1539 if (p) {
1540 tp->module = strndup(argv[1], p - argv[1]);
1541 p++;
1542 } else
1543 p = argv[1];
1544 fmt1_str = strtok_r(p, "+", &fmt);
1545
1546 if (fmt1_str[0] == '0') {
1547
1548
1549
1550
1551
1552
1553
1554 if (strcmp(fmt1_str, "0x") == 0) {
1555 if (!argv[2] || strcmp(argv[2], "(null)")) {
1556 ret = -EINVAL;
1557 goto out;
1558 }
1559 tp->address = 0;
1560
1561 free(argv[2]);
1562 for (i = 2; argv[i + 1] != NULL; i++)
1563 argv[i] = argv[i + 1];
1564
1565 argv[i] = NULL;
1566 argc -= 1;
1567 } else
1568 tp->address = strtoul(fmt1_str, NULL, 0);
1569 } else {
1570
1571 tp->symbol = strdup(fmt1_str);
1572 if (tp->symbol == NULL) {
1573 ret = -ENOMEM;
1574 goto out;
1575 }
1576 fmt2_str = strtok_r(NULL, "", &fmt);
1577 if (fmt2_str == NULL)
1578 tp->offset = 0;
1579 else
1580 tp->offset = strtoul(fmt2_str, NULL, 10);
1581 }
1582
1583 tev->nargs = argc - 2;
1584 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1585 if (tev->args == NULL) {
1586 ret = -ENOMEM;
1587 goto out;
1588 }
1589 for (i = 0; i < tev->nargs; i++) {
1590 p = strchr(argv[i + 2], '=');
1591 if (p)
1592 *p++ = '\0';
1593 else
1594 p = argv[i + 2];
1595 tev->args[i].name = strdup(argv[i + 2]);
1596
1597 tev->args[i].value = strdup(p);
1598 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1599 ret = -ENOMEM;
1600 goto out;
1601 }
1602 }
1603 ret = 0;
1604out:
1605 free(argv0_str);
1606 argv_free(argv);
1607 return ret;
1608}
1609
1610
1611int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1612{
1613 struct perf_probe_arg_field *field = pa->field;
1614 int ret;
1615 char *tmp = buf;
1616
1617 if (pa->name && pa->var)
1618 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1619 else
1620 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1621 if (ret <= 0)
1622 goto error;
1623 tmp += ret;
1624 len -= ret;
1625
1626 while (field) {
1627 if (field->name[0] == '[')
1628 ret = e_snprintf(tmp, len, "%s", field->name);
1629 else
1630 ret = e_snprintf(tmp, len, "%s%s",
1631 field->ref ? "->" : ".", field->name);
1632 if (ret <= 0)
1633 goto error;
1634 tmp += ret;
1635 len -= ret;
1636 field = field->next;
1637 }
1638
1639 if (pa->type) {
1640 ret = e_snprintf(tmp, len, ":%s", pa->type);
1641 if (ret <= 0)
1642 goto error;
1643 tmp += ret;
1644 len -= ret;
1645 }
1646
1647 return tmp - buf;
1648error:
1649 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1650 return ret;
1651}
1652
1653
1654static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1655{
1656 char *buf, *tmp;
1657 char offs[32] = "", line[32] = "", file[32] = "";
1658 int ret, len;
1659
1660 buf = zalloc(MAX_CMDLEN);
1661 if (buf == NULL) {
1662 ret = -ENOMEM;
1663 goto error;
1664 }
1665 if (pp->offset) {
1666 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1667 if (ret <= 0)
1668 goto error;
1669 }
1670 if (pp->line) {
1671 ret = e_snprintf(line, 32, ":%d", pp->line);
1672 if (ret <= 0)
1673 goto error;
1674 }
1675 if (pp->file) {
1676 tmp = pp->file;
1677 len = strlen(tmp);
1678 if (len > 30) {
1679 tmp = strchr(pp->file + len - 30, '/');
1680 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1681 }
1682 ret = e_snprintf(file, 32, "@%s", tmp);
1683 if (ret <= 0)
1684 goto error;
1685 }
1686
1687 if (pp->function)
1688 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1689 offs, pp->retprobe ? "%return" : "", line,
1690 file);
1691 else
1692 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1693 if (ret <= 0)
1694 goto error;
1695
1696 return buf;
1697error:
1698 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1699 free(buf);
1700 return NULL;
1701}
1702
1703#if 0
1704char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1705{
1706 char *buf;
1707 int i, len, ret;
1708
1709 buf = synthesize_perf_probe_point(&pev->point);
1710 if (!buf)
1711 return NULL;
1712
1713 len = strlen(buf);
1714 for (i = 0; i < pev->nargs; i++) {
1715 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1716 pev->args[i].name);
1717 if (ret <= 0) {
1718 free(buf);
1719 return NULL;
1720 }
1721 len += ret;
1722 }
1723
1724 return buf;
1725}
1726#endif
1727
1728static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1729 char **buf, size_t *buflen,
1730 int depth)
1731{
1732 int ret;
1733 if (ref->next) {
1734 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1735 buflen, depth + 1);
1736 if (depth < 0)
1737 goto out;
1738 }
1739
1740 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1741 if (ret < 0)
1742 depth = ret;
1743 else {
1744 *buf += ret;
1745 *buflen -= ret;
1746 }
1747out:
1748 return depth;
1749
1750}
1751
1752static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1753 char *buf, size_t buflen)
1754{
1755 struct probe_trace_arg_ref *ref = arg->ref;
1756 int ret, depth = 0;
1757 char *tmp = buf;
1758
1759
1760 if (arg->name)
1761 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1762 else
1763 ret = e_snprintf(buf, buflen, " ");
1764 if (ret < 0)
1765 return ret;
1766 buf += ret;
1767 buflen -= ret;
1768
1769
1770 if (arg->value[0] == '@' && arg->ref)
1771 ref = ref->next;
1772
1773
1774 if (ref) {
1775 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1776 &buflen, 1);
1777 if (depth < 0)
1778 return depth;
1779 }
1780
1781
1782 if (arg->value[0] == '@' && arg->ref)
1783 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1784 arg->ref->offset);
1785 else
1786 ret = e_snprintf(buf, buflen, "%s", arg->value);
1787 if (ret < 0)
1788 return ret;
1789 buf += ret;
1790 buflen -= ret;
1791
1792
1793 while (depth--) {
1794 ret = e_snprintf(buf, buflen, ")");
1795 if (ret < 0)
1796 return ret;
1797 buf += ret;
1798 buflen -= ret;
1799 }
1800
1801 if (arg->type) {
1802 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1803 if (ret <= 0)
1804 return ret;
1805 buf += ret;
1806 }
1807
1808 return buf - tmp;
1809}
1810
1811char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1812{
1813 struct probe_trace_point *tp = &tev->point;
1814 char *buf;
1815 int i, len, ret;
1816
1817 buf = zalloc(MAX_CMDLEN);
1818 if (buf == NULL)
1819 return NULL;
1820
1821 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1822 tev->group, tev->event);
1823 if (len <= 0)
1824 goto error;
1825
1826
1827 if (tev->uprobes && !tp->module)
1828 goto error;
1829
1830
1831
1832
1833
1834
1835 if (tev->uprobes && !tp->address) {
1836 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1837 goto error;
1838 }
1839
1840
1841 if (tev->uprobes)
1842 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1843 tp->module, tp->address);
1844 else if (!strncmp(tp->symbol, "0x", 2))
1845
1846 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s0x%lx",
1847 tp->module ?: "", tp->module ? ":" : "",
1848 tp->address);
1849 else
1850 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1851 tp->module ?: "", tp->module ? ":" : "",
1852 tp->symbol, tp->offset);
1853
1854 if (ret <= 0)
1855 goto error;
1856 len += ret;
1857
1858 for (i = 0; i < tev->nargs; i++) {
1859 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1860 MAX_CMDLEN - len);
1861 if (ret <= 0)
1862 goto error;
1863 len += ret;
1864 }
1865
1866 return buf;
1867error:
1868 free(buf);
1869 return NULL;
1870}
1871
1872static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1873 struct perf_probe_point *pp,
1874 bool is_kprobe)
1875{
1876 struct symbol *sym = NULL;
1877 struct map *map;
1878 u64 addr = tp->address;
1879 int ret = -ENOENT;
1880
1881 if (!is_kprobe) {
1882 map = dso__new_map(tp->module);
1883 if (!map)
1884 goto out;
1885 sym = map__find_symbol(map, addr, NULL);
1886 } else {
1887 if (tp->symbol)
1888 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1889 if (addr) {
1890 addr += tp->offset;
1891 sym = __find_kernel_function(addr, &map);
1892 }
1893 }
1894 if (!sym)
1895 goto out;
1896
1897 pp->retprobe = tp->retprobe;
1898 pp->offset = addr - map->unmap_ip(map, sym->start);
1899 pp->function = strdup(sym->name);
1900 ret = pp->function ? 0 : -ENOMEM;
1901
1902out:
1903 if (map && !is_kprobe) {
1904 map__put(map);
1905 }
1906
1907 return ret;
1908}
1909
1910static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1911 struct perf_probe_point *pp,
1912 bool is_kprobe)
1913{
1914 char buf[128];
1915 int ret;
1916
1917 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1918 if (!ret)
1919 return 0;
1920 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1921 if (!ret)
1922 return 0;
1923
1924 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1925
1926 if (tp->symbol) {
1927 pp->function = strdup(tp->symbol);
1928 pp->offset = tp->offset;
1929 } else {
1930 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1931 if (ret < 0)
1932 return ret;
1933 pp->function = strdup(buf);
1934 pp->offset = 0;
1935 }
1936 if (pp->function == NULL)
1937 return -ENOMEM;
1938
1939 pp->retprobe = tp->retprobe;
1940
1941 return 0;
1942}
1943
1944static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1945 struct perf_probe_event *pev, bool is_kprobe)
1946{
1947 char buf[64] = "";
1948 int i, ret;
1949
1950
1951 pev->event = strdup(tev->event);
1952 pev->group = strdup(tev->group);
1953 if (pev->event == NULL || pev->group == NULL)
1954 return -ENOMEM;
1955
1956
1957 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1958 if (ret < 0)
1959 return ret;
1960
1961
1962 pev->nargs = tev->nargs;
1963 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1964 if (pev->args == NULL)
1965 return -ENOMEM;
1966 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1967 if (tev->args[i].name)
1968 pev->args[i].name = strdup(tev->args[i].name);
1969 else {
1970 ret = synthesize_probe_trace_arg(&tev->args[i],
1971 buf, 64);
1972 pev->args[i].name = strdup(buf);
1973 }
1974 if (pev->args[i].name == NULL && ret >= 0)
1975 ret = -ENOMEM;
1976 }
1977
1978 if (ret < 0)
1979 clear_perf_probe_event(pev);
1980
1981 return ret;
1982}
1983
1984void clear_perf_probe_event(struct perf_probe_event *pev)
1985{
1986 struct perf_probe_arg_field *field, *next;
1987 int i;
1988
1989 free(pev->event);
1990 free(pev->group);
1991 free(pev->target);
1992 clear_perf_probe_point(&pev->point);
1993
1994 for (i = 0; i < pev->nargs; i++) {
1995 free(pev->args[i].name);
1996 free(pev->args[i].var);
1997 free(pev->args[i].type);
1998 field = pev->args[i].field;
1999 while (field) {
2000 next = field->next;
2001 zfree(&field->name);
2002 free(field);
2003 field = next;
2004 }
2005 }
2006 free(pev->args);
2007 memset(pev, 0, sizeof(*pev));
2008}
2009
2010void clear_probe_trace_event(struct probe_trace_event *tev)
2011{
2012 struct probe_trace_arg_ref *ref, *next;
2013 int i;
2014
2015 free(tev->event);
2016 free(tev->group);
2017 free(tev->point.symbol);
2018 free(tev->point.realname);
2019 free(tev->point.module);
2020 for (i = 0; i < tev->nargs; i++) {
2021 free(tev->args[i].name);
2022 free(tev->args[i].value);
2023 free(tev->args[i].type);
2024 ref = tev->args[i].ref;
2025 while (ref) {
2026 next = ref->next;
2027 free(ref);
2028 ref = next;
2029 }
2030 }
2031 free(tev->args);
2032 memset(tev, 0, sizeof(*tev));
2033}
2034
2035struct kprobe_blacklist_node {
2036 struct list_head list;
2037 unsigned long start;
2038 unsigned long end;
2039 char *symbol;
2040};
2041
2042static void kprobe_blacklist__delete(struct list_head *blacklist)
2043{
2044 struct kprobe_blacklist_node *node;
2045
2046 while (!list_empty(blacklist)) {
2047 node = list_first_entry(blacklist,
2048 struct kprobe_blacklist_node, list);
2049 list_del(&node->list);
2050 free(node->symbol);
2051 free(node);
2052 }
2053}
2054
2055static int kprobe_blacklist__load(struct list_head *blacklist)
2056{
2057 struct kprobe_blacklist_node *node;
2058 const char *__debugfs = debugfs_find_mountpoint();
2059 char buf[PATH_MAX], *p;
2060 FILE *fp;
2061 int ret;
2062
2063 if (__debugfs == NULL)
2064 return -ENOTSUP;
2065
2066 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2067 if (ret < 0)
2068 return ret;
2069
2070 fp = fopen(buf, "r");
2071 if (!fp)
2072 return -errno;
2073
2074 ret = 0;
2075 while (fgets(buf, PATH_MAX, fp)) {
2076 node = zalloc(sizeof(*node));
2077 if (!node) {
2078 ret = -ENOMEM;
2079 break;
2080 }
2081 INIT_LIST_HEAD(&node->list);
2082 list_add_tail(&node->list, blacklist);
2083 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2084 ret = -EINVAL;
2085 break;
2086 }
2087 p = strchr(buf, '\t');
2088 if (p) {
2089 p++;
2090 if (p[strlen(p) - 1] == '\n')
2091 p[strlen(p) - 1] = '\0';
2092 } else
2093 p = (char *)"unknown";
2094 node->symbol = strdup(p);
2095 if (!node->symbol) {
2096 ret = -ENOMEM;
2097 break;
2098 }
2099 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2100 node->start, node->end, node->symbol);
2101 ret++;
2102 }
2103 if (ret < 0)
2104 kprobe_blacklist__delete(blacklist);
2105 fclose(fp);
2106
2107 return ret;
2108}
2109
2110static struct kprobe_blacklist_node *
2111kprobe_blacklist__find_by_address(struct list_head *blacklist,
2112 unsigned long address)
2113{
2114 struct kprobe_blacklist_node *node;
2115
2116 list_for_each_entry(node, blacklist, list) {
2117 if (node->start <= address && address <= node->end)
2118 return node;
2119 }
2120
2121 return NULL;
2122}
2123
2124static LIST_HEAD(kprobe_blacklist);
2125
2126static void kprobe_blacklist__init(void)
2127{
2128 if (!list_empty(&kprobe_blacklist))
2129 return;
2130
2131 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2132 pr_debug("No kprobe blacklist support, ignored\n");
2133}
2134
2135static void kprobe_blacklist__release(void)
2136{
2137 kprobe_blacklist__delete(&kprobe_blacklist);
2138}
2139
2140static bool kprobe_blacklist__listed(unsigned long address)
2141{
2142 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2143}
2144
2145static int perf_probe_event__sprintf(const char *group, const char *event,
2146 struct perf_probe_event *pev,
2147 const char *module,
2148 struct strbuf *result)
2149{
2150 int i, ret;
2151 char buf[128];
2152 char *place;
2153
2154
2155 place = synthesize_perf_probe_point(&pev->point);
2156 if (!place)
2157 return -EINVAL;
2158
2159 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2160 if (ret < 0)
2161 goto out;
2162
2163 strbuf_addf(result, " %-20s (on %s", buf, place);
2164 if (module)
2165 strbuf_addf(result, " in %s", module);
2166
2167 if (pev->nargs > 0) {
2168 strbuf_addstr(result, " with");
2169 for (i = 0; i < pev->nargs; i++) {
2170 ret = synthesize_perf_probe_arg(&pev->args[i],
2171 buf, 128);
2172 if (ret < 0)
2173 goto out;
2174 strbuf_addf(result, " %s", buf);
2175 }
2176 }
2177 strbuf_addch(result, ')');
2178out:
2179 free(place);
2180 return ret;
2181}
2182
2183
2184static int show_perf_probe_event(const char *group, const char *event,
2185 struct perf_probe_event *pev,
2186 const char *module, bool use_stdout)
2187{
2188 struct strbuf buf = STRBUF_INIT;
2189 int ret;
2190
2191 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2192 if (ret >= 0) {
2193 if (use_stdout)
2194 printf("%s\n", buf.buf);
2195 else
2196 pr_info("%s\n", buf.buf);
2197 }
2198 strbuf_release(&buf);
2199
2200 return ret;
2201}
2202
2203static bool filter_probe_trace_event(struct probe_trace_event *tev,
2204 struct strfilter *filter)
2205{
2206 char tmp[128];
2207
2208
2209 if (strfilter__compare(filter, tev->event))
2210 return true;
2211
2212
2213 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2214 return false;
2215 return strfilter__compare(filter, tmp);
2216}
2217
2218static int __show_perf_probe_events(int fd, bool is_kprobe,
2219 struct strfilter *filter)
2220{
2221 int ret = 0;
2222 struct probe_trace_event tev;
2223 struct perf_probe_event pev;
2224 struct strlist *rawlist;
2225 struct str_node *ent;
2226
2227 memset(&tev, 0, sizeof(tev));
2228 memset(&pev, 0, sizeof(pev));
2229
2230 rawlist = probe_file__get_rawlist(fd);
2231 if (!rawlist)
2232 return -ENOMEM;
2233
2234 strlist__for_each(ent, rawlist) {
2235 ret = parse_probe_trace_command(ent->s, &tev);
2236 if (ret >= 0) {
2237 if (!filter_probe_trace_event(&tev, filter))
2238 goto next;
2239 ret = convert_to_perf_probe_event(&tev, &pev,
2240 is_kprobe);
2241 if (ret < 0)
2242 goto next;
2243 ret = show_perf_probe_event(pev.group, pev.event,
2244 &pev, tev.point.module,
2245 true);
2246 }
2247next:
2248 clear_perf_probe_event(&pev);
2249 clear_probe_trace_event(&tev);
2250 if (ret < 0)
2251 break;
2252 }
2253 strlist__delete(rawlist);
2254
2255 debuginfo_cache__exit();
2256
2257 return ret;
2258}
2259
2260
2261int show_perf_probe_events(struct strfilter *filter)
2262{
2263 int kp_fd, up_fd, ret;
2264
2265 setup_pager();
2266
2267 ret = init_symbol_maps(false);
2268 if (ret < 0)
2269 return ret;
2270
2271 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2272 if (ret < 0)
2273 return ret;
2274
2275 if (kp_fd >= 0)
2276 ret = __show_perf_probe_events(kp_fd, true, filter);
2277 if (up_fd >= 0 && ret >= 0)
2278 ret = __show_perf_probe_events(up_fd, false, filter);
2279 if (kp_fd > 0)
2280 close(kp_fd);
2281 if (up_fd > 0)
2282 close(up_fd);
2283 exit_symbol_maps();
2284
2285 return ret;
2286}
2287
2288static int get_new_event_name(char *buf, size_t len, const char *base,
2289 struct strlist *namelist, bool allow_suffix)
2290{
2291 int i, ret;
2292 char *p;
2293
2294 if (*base == '.')
2295 base++;
2296
2297
2298 ret = e_snprintf(buf, len, "%s", base);
2299 if (ret < 0) {
2300 pr_debug("snprintf() failed: %d\n", ret);
2301 return ret;
2302 }
2303
2304 p = strchr(buf, '.');
2305 if (p && p != buf)
2306 *p = '\0';
2307 if (!strlist__has_entry(namelist, buf))
2308 return 0;
2309
2310 if (!allow_suffix) {
2311 pr_warning("Error: event \"%s\" already exists. "
2312 "(Use -f to force duplicates.)\n", base);
2313 return -EEXIST;
2314 }
2315
2316
2317 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2318 ret = e_snprintf(buf, len, "%s_%d", base, i);
2319 if (ret < 0) {
2320 pr_debug("snprintf() failed: %d\n", ret);
2321 return ret;
2322 }
2323 if (!strlist__has_entry(namelist, buf))
2324 break;
2325 }
2326 if (i == MAX_EVENT_INDEX) {
2327 pr_warning("Too many events are on the same function.\n");
2328 ret = -ERANGE;
2329 }
2330
2331 return ret;
2332}
2333
2334
2335static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2336{
2337 int i;
2338 char *buf = synthesize_probe_trace_command(tev);
2339
2340
2341 if (!tev->uprobes || tev->nargs == 0 || !buf)
2342 goto out;
2343
2344 for (i = 0; i < tev->nargs; i++)
2345 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2346 pr_warning("Please upgrade your kernel to at least "
2347 "3.14 to have access to feature %s\n",
2348 tev->args[i].value);
2349 break;
2350 }
2351out:
2352 free(buf);
2353}
2354
2355
2356static int probe_trace_event__set_name(struct probe_trace_event *tev,
2357 struct perf_probe_event *pev,
2358 struct strlist *namelist,
2359 bool allow_suffix)
2360{
2361 const char *event, *group;
2362 char buf[64];
2363 int ret;
2364
2365 if (pev->event)
2366 event = pev->event;
2367 else
2368 if (pev->point.function &&
2369 (strncmp(pev->point.function, "0x", 2) != 0) &&
2370 !strisglob(pev->point.function))
2371 event = pev->point.function;
2372 else
2373 event = tev->point.realname;
2374 if (pev->group)
2375 group = pev->group;
2376 else
2377 group = PERFPROBE_GROUP;
2378
2379
2380 ret = get_new_event_name(buf, 64, event,
2381 namelist, allow_suffix);
2382 if (ret < 0)
2383 return ret;
2384
2385 event = buf;
2386
2387 tev->event = strdup(event);
2388 tev->group = strdup(group);
2389 if (tev->event == NULL || tev->group == NULL)
2390 return -ENOMEM;
2391
2392
2393 strlist__add(namelist, event);
2394 return 0;
2395}
2396
2397static int __add_probe_trace_events(struct perf_probe_event *pev,
2398 struct probe_trace_event *tevs,
2399 int ntevs, bool allow_suffix)
2400{
2401 int i, fd, ret;
2402 struct probe_trace_event *tev = NULL;
2403 const char *event = NULL, *group = NULL;
2404 struct strlist *namelist;
2405
2406 fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
2407 if (fd < 0)
2408 return fd;
2409
2410
2411 namelist = probe_file__get_namelist(fd);
2412 if (!namelist) {
2413 pr_debug("Failed to get current event list.\n");
2414 ret = -ENOMEM;
2415 goto close_out;
2416 }
2417
2418 ret = 0;
2419 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
2420 for (i = 0; i < ntevs; i++) {
2421 tev = &tevs[i];
2422
2423 if (!tev->point.symbol)
2424 continue;
2425
2426
2427 ret = probe_trace_event__set_name(tev, pev, namelist,
2428 allow_suffix);
2429 if (ret < 0)
2430 break;
2431
2432 ret = probe_file__add_event(fd, tev);
2433 if (ret < 0)
2434 break;
2435
2436
2437 show_perf_probe_event(tev->group, tev->event, pev,
2438 tev->point.module, false);
2439
2440 event = tev->event;
2441 group = tev->group;
2442
2443
2444
2445
2446
2447
2448
2449 allow_suffix = true;
2450 }
2451 if (ret == -EINVAL && pev->uprobes)
2452 warn_uprobe_event_compat(tev);
2453
2454
2455 if (ret >= 0 && event) {
2456
2457 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2458 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group, event);
2459 }
2460
2461 strlist__delete(namelist);
2462close_out:
2463 close(fd);
2464 return ret;
2465}
2466
2467static int find_probe_functions(struct map *map, char *name,
2468 struct symbol **syms)
2469{
2470 int found = 0;
2471 struct symbol *sym;
2472 struct rb_node *tmp;
2473
2474 if (map__load(map, NULL) < 0)
2475 return 0;
2476
2477 map__for_each_symbol(map, sym, tmp) {
2478 if (strglobmatch(sym->name, name)) {
2479 found++;
2480 if (syms && found < probe_conf.max_probes)
2481 syms[found - 1] = sym;
2482 }
2483 }
2484
2485 return found;
2486}
2487
2488#define strdup_or_goto(str, label) \
2489 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2490
2491void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2492 struct probe_trace_event *tev __maybe_unused,
2493 struct map *map __maybe_unused) { }
2494
2495
2496
2497
2498
2499static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2500 struct probe_trace_event **tevs)
2501{
2502 struct map *map = NULL;
2503 struct ref_reloc_sym *reloc_sym = NULL;
2504 struct symbol *sym;
2505 struct symbol **syms = NULL;
2506 struct probe_trace_event *tev;
2507 struct perf_probe_point *pp = &pev->point;
2508 struct probe_trace_point *tp;
2509 int num_matched_functions;
2510 int ret, i, j, skipped = 0;
2511
2512 map = get_target_map(pev->target, pev->uprobes);
2513 if (!map) {
2514 ret = -EINVAL;
2515 goto out;
2516 }
2517
2518 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2519 if (!syms) {
2520 ret = -ENOMEM;
2521 goto out;
2522 }
2523
2524
2525
2526
2527
2528 num_matched_functions = find_probe_functions(map, pp->function, syms);
2529 if (num_matched_functions == 0) {
2530 pr_err("Failed to find symbol %s in %s\n", pp->function,
2531 pev->target ? : "kernel");
2532 ret = -ENOENT;
2533 goto out;
2534 } else if (num_matched_functions > probe_conf.max_probes) {
2535 pr_err("Too many functions matched in %s\n",
2536 pev->target ? : "kernel");
2537 ret = -E2BIG;
2538 goto out;
2539 }
2540
2541 if (!pev->uprobes && !pp->retprobe) {
2542 reloc_sym = kernel_get_ref_reloc_sym();
2543 if (!reloc_sym) {
2544 pr_warning("Relocated base symbol is not found!\n");
2545 ret = -EINVAL;
2546 goto out;
2547 }
2548 }
2549
2550
2551 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2552 if (!*tevs) {
2553 ret = -ENOMEM;
2554 goto out;
2555 }
2556
2557 ret = 0;
2558
2559 for (j = 0; j < num_matched_functions; j++) {
2560 sym = syms[j];
2561
2562 tev = (*tevs) + ret;
2563 tp = &tev->point;
2564 if (ret == num_matched_functions) {
2565 pr_warning("Too many symbols are listed. Skip it.\n");
2566 break;
2567 }
2568 ret++;
2569
2570 if (pp->offset > sym->end - sym->start) {
2571 pr_warning("Offset %ld is bigger than the size of %s\n",
2572 pp->offset, sym->name);
2573 ret = -ENOENT;
2574 goto err_out;
2575 }
2576
2577 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2578
2579 if (!pev->uprobes &&
2580 kprobe_warn_out_range(sym->name, tp->address)) {
2581 tp->symbol = NULL;
2582 skipped++;
2583 } else if (reloc_sym) {
2584 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2585 tp->offset = tp->address - reloc_sym->addr;
2586 } else {
2587 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2588 tp->offset = pp->offset;
2589 }
2590 tp->realname = strdup_or_goto(sym->name, nomem_out);
2591
2592 tp->retprobe = pp->retprobe;
2593 if (pev->target)
2594 tev->point.module = strdup_or_goto(pev->target,
2595 nomem_out);
2596 tev->uprobes = pev->uprobes;
2597 tev->nargs = pev->nargs;
2598 if (tev->nargs) {
2599 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2600 tev->nargs);
2601 if (tev->args == NULL)
2602 goto nomem_out;
2603 }
2604 for (i = 0; i < tev->nargs; i++) {
2605 if (pev->args[i].name)
2606 tev->args[i].name =
2607 strdup_or_goto(pev->args[i].name,
2608 nomem_out);
2609
2610 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2611 nomem_out);
2612 if (pev->args[i].type)
2613 tev->args[i].type =
2614 strdup_or_goto(pev->args[i].type,
2615 nomem_out);
2616 }
2617 arch__fix_tev_from_maps(pev, tev, map);
2618 }
2619 if (ret == skipped) {
2620 ret = -ENOENT;
2621 goto err_out;
2622 }
2623
2624out:
2625 put_target_map(map, pev->uprobes);
2626 free(syms);
2627 return ret;
2628
2629nomem_out:
2630 ret = -ENOMEM;
2631err_out:
2632 clear_probe_trace_events(*tevs, num_matched_functions);
2633 zfree(tevs);
2634 goto out;
2635}
2636
2637static int try_to_find_absolute_address(struct perf_probe_event *pev,
2638 struct probe_trace_event **tevs)
2639{
2640 struct perf_probe_point *pp = &pev->point;
2641 struct probe_trace_event *tev;
2642 struct probe_trace_point *tp;
2643 int i, err;
2644
2645 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2646 return -EINVAL;
2647 if (perf_probe_event_need_dwarf(pev))
2648 return -EINVAL;
2649
2650
2651
2652
2653
2654
2655
2656 *tevs = zalloc(sizeof(*tev));
2657 if (!*tevs)
2658 return -ENOMEM;
2659
2660 tev = *tevs;
2661 tp = &tev->point;
2662
2663
2664
2665
2666
2667
2668 tp->address = pev->point.abs_address;
2669 tp->retprobe = pp->retprobe;
2670 tev->uprobes = pev->uprobes;
2671
2672 err = -ENOMEM;
2673
2674
2675
2676
2677
2678 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2679 goto errout;
2680
2681
2682 if ((!tev->uprobes) &&
2683 (kprobe_warn_out_range(tev->point.symbol,
2684 tev->point.address))) {
2685 err = -EACCES;
2686 goto errout;
2687 }
2688
2689 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2690 goto errout;
2691
2692 if (pev->target) {
2693 tp->module = strdup(pev->target);
2694 if (!tp->module)
2695 goto errout;
2696 }
2697
2698 if (tev->group) {
2699 tev->group = strdup(pev->group);
2700 if (!tev->group)
2701 goto errout;
2702 }
2703
2704 if (pev->event) {
2705 tev->event = strdup(pev->event);
2706 if (!tev->event)
2707 goto errout;
2708 }
2709
2710 tev->nargs = pev->nargs;
2711 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2712 if (!tev->args) {
2713 err = -ENOMEM;
2714 goto errout;
2715 }
2716 for (i = 0; i < tev->nargs; i++)
2717 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2718
2719 return 1;
2720
2721errout:
2722 if (*tevs) {
2723 clear_probe_trace_events(*tevs, 1);
2724 *tevs = NULL;
2725 }
2726 return err;
2727}
2728
2729bool __weak arch__prefers_symtab(void) { return false; }
2730
2731static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2732 struct probe_trace_event **tevs)
2733{
2734 int ret;
2735
2736 if (pev->uprobes && !pev->group) {
2737
2738 ret = convert_exec_to_group(pev->target, &pev->group);
2739 if (ret != 0) {
2740 pr_warning("Failed to make a group name.\n");
2741 return ret;
2742 }
2743 }
2744
2745 ret = try_to_find_absolute_address(pev, tevs);
2746 if (ret > 0)
2747 return ret;
2748
2749 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2750 ret = find_probe_trace_events_from_map(pev, tevs);
2751 if (ret > 0)
2752 return ret;
2753 }
2754
2755
2756 ret = try_to_find_probe_trace_events(pev, tevs);
2757 if (ret != 0)
2758 return ret;
2759
2760 return find_probe_trace_events_from_map(pev, tevs);
2761}
2762
2763struct __event_package {
2764 struct perf_probe_event *pev;
2765 struct probe_trace_event *tevs;
2766 int ntevs;
2767};
2768
2769int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2770{
2771 int i, j, ret;
2772 struct __event_package *pkgs;
2773
2774 ret = 0;
2775 pkgs = zalloc(sizeof(struct __event_package) * npevs);
2776
2777 if (pkgs == NULL)
2778 return -ENOMEM;
2779
2780 ret = init_symbol_maps(pevs->uprobes);
2781 if (ret < 0) {
2782 free(pkgs);
2783 return ret;
2784 }
2785
2786
2787 for (i = 0; i < npevs; i++) {
2788 pkgs[i].pev = &pevs[i];
2789
2790 if (!pkgs[i].pev->uprobes)
2791 kprobe_blacklist__init();
2792
2793 ret = convert_to_probe_trace_events(pkgs[i].pev,
2794 &pkgs[i].tevs);
2795 if (ret < 0)
2796 goto end;
2797 pkgs[i].ntevs = ret;
2798 }
2799
2800 kprobe_blacklist__release();
2801
2802
2803 for (i = 0; i < npevs; i++) {
2804 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
2805 pkgs[i].ntevs,
2806 probe_conf.force_add);
2807 if (ret < 0)
2808 break;
2809 }
2810end:
2811
2812 for (i = 0; i < npevs; i++) {
2813 for (j = 0; j < pkgs[i].ntevs; j++)
2814 clear_probe_trace_event(&pkgs[i].tevs[j]);
2815 zfree(&pkgs[i].tevs);
2816 }
2817 free(pkgs);
2818 exit_symbol_maps();
2819
2820 return ret;
2821}
2822
2823int del_perf_probe_events(struct strfilter *filter)
2824{
2825 int ret, ret2, ufd = -1, kfd = -1;
2826 char *str = strfilter__string(filter);
2827
2828 if (!str)
2829 return -EINVAL;
2830
2831 pr_debug("Delete filter: \'%s\'\n", str);
2832
2833
2834 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2835 if (ret < 0)
2836 goto out;
2837
2838 ret = probe_file__del_events(kfd, filter);
2839 if (ret < 0 && ret != -ENOENT)
2840 goto error;
2841
2842 ret2 = probe_file__del_events(ufd, filter);
2843 if (ret2 < 0 && ret2 != -ENOENT) {
2844 ret = ret2;
2845 goto error;
2846 }
2847 if (ret == -ENOENT && ret2 == -ENOENT)
2848 pr_debug("\"%s\" does not hit any event.\n", str);
2849
2850 ret = 0;
2851
2852error:
2853 if (kfd >= 0)
2854 close(kfd);
2855 if (ufd >= 0)
2856 close(ufd);
2857out:
2858 free(str);
2859
2860 return ret;
2861}
2862
2863
2864static struct strfilter *available_func_filter;
2865
2866
2867
2868
2869
2870static int filter_available_functions(struct map *map __maybe_unused,
2871 struct symbol *sym)
2872{
2873 if (strfilter__compare(available_func_filter, sym->name))
2874 return 0;
2875 return 1;
2876}
2877
2878int show_available_funcs(const char *target, struct strfilter *_filter,
2879 bool user)
2880{
2881 struct map *map;
2882 int ret;
2883
2884 ret = init_symbol_maps(user);
2885 if (ret < 0)
2886 return ret;
2887
2888
2889 if (user)
2890 map = dso__new_map(target);
2891 else
2892 map = kernel_get_module_map(target);
2893 if (!map) {
2894 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2895 return -EINVAL;
2896 }
2897
2898
2899 available_func_filter = _filter;
2900 if (map__load(map, filter_available_functions)) {
2901 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2902 goto end;
2903 }
2904 if (!dso__sorted_by_name(map->dso, map->type))
2905 dso__sort_by_name(map->dso, map->type);
2906
2907
2908 setup_pager();
2909 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2910end:
2911 if (user) {
2912 map__put(map);
2913 }
2914 exit_symbol_maps();
2915
2916 return ret;
2917}
2918
2919int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
2920 struct perf_probe_arg *pvar)
2921{
2922 tvar->value = strdup(pvar->var);
2923 if (tvar->value == NULL)
2924 return -ENOMEM;
2925 if (pvar->type) {
2926 tvar->type = strdup(pvar->type);
2927 if (tvar->type == NULL)
2928 return -ENOMEM;
2929 }
2930 if (pvar->name) {
2931 tvar->name = strdup(pvar->name);
2932 if (tvar->name == NULL)
2933 return -ENOMEM;
2934 } else
2935 tvar->name = NULL;
2936 return 0;
2937}
2938