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