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