Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / tools / perf / util / probe-event.c
blobe33554a562b36fb59c95d935258d525a0e810dac
1 /*
2 * probe-event.c : perf-probe definition to probe_events format converter
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
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.
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.
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.
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>
35 #include "util.h"
36 #include "event.h"
37 #include "string.h"
38 #include "strlist.h"
39 #include "debug.h"
40 #include "cache.h"
41 #include "color.h"
42 #include "symbol.h"
43 #include "thread.h"
44 #include "debugfs.h"
45 #include "trace-event.h" /* For __unused */
46 #include "probe-event.h"
47 #include "probe-finder.h"
49 #define MAX_CMDLEN 256
50 #define MAX_PROBE_ARGS 128
51 #define PERFPROBE_GROUP "probe"
53 bool probe_event_dry_run; /* Dry run flag */
55 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
57 /* If there is no space to write, returns -E2BIG. */
58 static int e_snprintf(char *str, size_t size, const char *format, ...)
59 __attribute__((format(printf, 3, 4)));
61 static int e_snprintf(char *str, size_t size, const char *format, ...)
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;
73 static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
74 static struct machine machine;
76 /* Initialize symbol maps and path of vmlinux/modules */
77 static int init_vmlinux(void)
79 int ret;
81 symbol_conf.sort_by_name = true;
82 if (symbol_conf.vmlinux_name == NULL)
83 symbol_conf.try_vmlinux_path = true;
84 else
85 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
86 ret = symbol__init();
87 if (ret < 0) {
88 pr_debug("Failed to init symbol map.\n");
89 goto out;
92 ret = machine__init(&machine, "", HOST_KERNEL_ID);
93 if (ret < 0)
94 goto out;
96 if (machine__create_kernel_maps(&machine) < 0) {
97 pr_debug("machine__create_kernel_maps() failed.\n");
98 goto out;
100 out:
101 if (ret < 0)
102 pr_warning("Failed to init vmlinux path.\n");
103 return ret;
106 static struct symbol *__find_kernel_function_by_name(const char *name,
107 struct map **mapp)
109 return machine__find_kernel_function_by_name(&machine, name, mapp,
110 NULL);
113 static struct map *kernel_get_module_map(const char *module)
115 struct rb_node *nd;
116 struct map_groups *grp = &machine.kmaps;
118 /* A file path -- this is an offline module */
119 if (module && strchr(module, '/'))
120 return machine__new_module(&machine, 0, module);
122 if (!module)
123 module = "kernel";
125 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
126 struct map *pos = rb_entry(nd, struct map, rb_node);
127 if (strncmp(pos->dso->short_name + 1, module,
128 pos->dso->short_name_len - 2) == 0) {
129 return pos;
132 return NULL;
135 static struct dso *kernel_get_module_dso(const char *module)
137 struct dso *dso;
138 struct map *map;
139 const char *vmlinux_name;
141 if (module) {
142 list_for_each_entry(dso, &machine.kernel_dsos, node) {
143 if (strncmp(dso->short_name + 1, module,
144 dso->short_name_len - 2) == 0)
145 goto found;
147 pr_debug("Failed to find module %s.\n", module);
148 return NULL;
151 map = machine.vmlinux_maps[MAP__FUNCTION];
152 dso = map->dso;
154 vmlinux_name = symbol_conf.vmlinux_name;
155 if (vmlinux_name) {
156 if (dso__load_vmlinux(dso, map, vmlinux_name, NULL) <= 0)
157 return NULL;
158 } else {
159 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
160 pr_debug("Failed to load kernel map.\n");
161 return NULL;
164 found:
165 return dso;
168 const char *kernel_get_module_path(const char *module)
170 struct dso *dso = kernel_get_module_dso(module);
171 return (dso) ? dso->long_name : NULL;
174 #ifdef DWARF_SUPPORT
175 /* Open new debuginfo of given module */
176 static struct debuginfo *open_debuginfo(const char *module)
178 const char *path;
180 /* A file path -- this is an offline module */
181 if (module && strchr(module, '/'))
182 path = module;
183 else {
184 path = kernel_get_module_path(module);
186 if (!path) {
187 pr_err("Failed to find path of %s module.\n",
188 module ?: "kernel");
189 return NULL;
192 return debuginfo__new(path);
196 * Convert trace point to probe point with debuginfo
197 * Currently only handles kprobes.
199 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
200 struct perf_probe_point *pp)
202 struct symbol *sym;
203 struct map *map;
204 u64 addr;
205 int ret = -ENOENT;
206 struct debuginfo *dinfo;
208 sym = __find_kernel_function_by_name(tp->symbol, &map);
209 if (sym) {
210 addr = map->unmap_ip(map, sym->start + tp->offset);
211 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
212 tp->offset, addr);
214 dinfo = debuginfo__new_online_kernel(addr);
215 if (dinfo) {
216 ret = debuginfo__find_probe_point(dinfo,
217 (unsigned long)addr, pp);
218 debuginfo__delete(dinfo);
219 } else {
220 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
221 addr);
222 ret = -ENOENT;
225 if (ret <= 0) {
226 pr_debug("Failed to find corresponding probes from "
227 "debuginfo. Use kprobe event information.\n");
228 pp->function = strdup(tp->symbol);
229 if (pp->function == NULL)
230 return -ENOMEM;
231 pp->offset = tp->offset;
233 pp->retprobe = tp->retprobe;
235 return 0;
238 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
239 int ntevs, const char *module)
241 int i, ret = 0;
242 char *tmp;
244 if (!module)
245 return 0;
247 tmp = strrchr(module, '/');
248 if (tmp) {
249 /* This is a module path -- get the module name */
250 module = strdup(tmp + 1);
251 if (!module)
252 return -ENOMEM;
253 tmp = strchr(module, '.');
254 if (tmp)
255 *tmp = '\0';
256 tmp = (char *)module; /* For free() */
259 for (i = 0; i < ntevs; i++) {
260 tevs[i].point.module = strdup(module);
261 if (!tevs[i].point.module) {
262 ret = -ENOMEM;
263 break;
267 if (tmp)
268 free(tmp);
270 return ret;
273 /* Try to find perf_probe_event with debuginfo */
274 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
275 struct probe_trace_event **tevs,
276 int max_tevs, const char *module)
278 bool need_dwarf = perf_probe_event_need_dwarf(pev);
279 struct debuginfo *dinfo = open_debuginfo(module);
280 int ntevs, ret = 0;
282 if (!dinfo) {
283 if (need_dwarf) {
284 pr_warning("Failed to open debuginfo file.\n");
285 return -ENOENT;
287 pr_debug("Could not open debuginfo. Try to use symbols.\n");
288 return 0;
291 /* Searching trace events corresponding to a probe event */
292 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
294 debuginfo__delete(dinfo);
296 if (ntevs > 0) { /* Succeeded to find trace events */
297 pr_debug("find %d probe_trace_events.\n", ntevs);
298 if (module)
299 ret = add_module_to_probe_trace_events(*tevs, ntevs,
300 module);
301 return ret < 0 ? ret : ntevs;
304 if (ntevs == 0) { /* No error but failed to find probe point. */
305 pr_warning("Probe point '%s' not found.\n",
306 synthesize_perf_probe_point(&pev->point));
307 return -ENOENT;
309 /* Error path : ntevs < 0 */
310 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
311 if (ntevs == -EBADF) {
312 pr_warning("Warning: No dwarf info found in the vmlinux - "
313 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
314 if (!need_dwarf) {
315 pr_debug("Trying to use symbols.\n");
316 return 0;
319 return ntevs;
323 * Find a src file from a DWARF tag path. Prepend optional source path prefix
324 * and chop off leading directories that do not exist. Result is passed back as
325 * a newly allocated path on success.
326 * Return 0 if file was found and readable, -errno otherwise.
328 static int get_real_path(const char *raw_path, const char *comp_dir,
329 char **new_path)
331 const char *prefix = symbol_conf.source_prefix;
333 if (!prefix) {
334 if (raw_path[0] != '/' && comp_dir)
335 /* If not an absolute path, try to use comp_dir */
336 prefix = comp_dir;
337 else {
338 if (access(raw_path, R_OK) == 0) {
339 *new_path = strdup(raw_path);
340 return 0;
341 } else
342 return -errno;
346 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
347 if (!*new_path)
348 return -ENOMEM;
350 for (;;) {
351 sprintf(*new_path, "%s/%s", prefix, raw_path);
353 if (access(*new_path, R_OK) == 0)
354 return 0;
356 if (!symbol_conf.source_prefix)
357 /* In case of searching comp_dir, don't retry */
358 return -errno;
360 switch (errno) {
361 case ENAMETOOLONG:
362 case ENOENT:
363 case EROFS:
364 case EFAULT:
365 raw_path = strchr(++raw_path, '/');
366 if (!raw_path) {
367 free(*new_path);
368 *new_path = NULL;
369 return -ENOENT;
371 continue;
373 default:
374 free(*new_path);
375 *new_path = NULL;
376 return -errno;
381 #define LINEBUF_SIZE 256
382 #define NR_ADDITIONAL_LINES 2
384 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
386 char buf[LINEBUF_SIZE];
387 const char *color = show_num ? "" : PERF_COLOR_BLUE;
388 const char *prefix = NULL;
390 do {
391 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
392 goto error;
393 if (skip)
394 continue;
395 if (!prefix) {
396 prefix = show_num ? "%7d " : " ";
397 color_fprintf(stdout, color, prefix, l);
399 color_fprintf(stdout, color, "%s", buf);
401 } while (strchr(buf, '\n') == NULL);
403 return 1;
404 error:
405 if (ferror(fp)) {
406 pr_warning("File read error: %s\n", strerror(errno));
407 return -1;
409 return 0;
412 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
414 int rv = __show_one_line(fp, l, skip, show_num);
415 if (rv == 0) {
416 pr_warning("Source file is shorter than expected.\n");
417 rv = -1;
419 return rv;
422 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
423 #define show_one_line(f,l) _show_one_line(f,l,false,false)
424 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
425 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
428 * Show line-range always requires debuginfo to find source file and
429 * line number.
431 int show_line_range(struct line_range *lr, const char *module)
433 int l = 1;
434 struct line_node *ln;
435 struct debuginfo *dinfo;
436 FILE *fp;
437 int ret;
438 char *tmp;
440 /* Search a line range */
441 ret = init_vmlinux();
442 if (ret < 0)
443 return ret;
445 dinfo = open_debuginfo(module);
446 if (!dinfo) {
447 pr_warning("Failed to open debuginfo file.\n");
448 return -ENOENT;
451 ret = debuginfo__find_line_range(dinfo, lr);
452 debuginfo__delete(dinfo);
453 if (ret == 0) {
454 pr_warning("Specified source line is not found.\n");
455 return -ENOENT;
456 } else if (ret < 0) {
457 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
458 return ret;
461 /* Convert source file path */
462 tmp = lr->path;
463 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
464 free(tmp); /* Free old path */
465 if (ret < 0) {
466 pr_warning("Failed to find source file. (%d)\n", ret);
467 return ret;
470 setup_pager();
472 if (lr->function)
473 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
474 lr->start - lr->offset);
475 else
476 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
478 fp = fopen(lr->path, "r");
479 if (fp == NULL) {
480 pr_warning("Failed to open %s: %s\n", lr->path,
481 strerror(errno));
482 return -errno;
484 /* Skip to starting line number */
485 while (l < lr->start) {
486 ret = skip_one_line(fp, l++);
487 if (ret < 0)
488 goto end;
491 list_for_each_entry(ln, &lr->line_list, list) {
492 for (; ln->line > l; l++) {
493 ret = show_one_line(fp, l - lr->offset);
494 if (ret < 0)
495 goto end;
497 ret = show_one_line_with_num(fp, l++ - lr->offset);
498 if (ret < 0)
499 goto end;
502 if (lr->end == INT_MAX)
503 lr->end = l + NR_ADDITIONAL_LINES;
504 while (l <= lr->end) {
505 ret = show_one_line_or_eof(fp, l++ - lr->offset);
506 if (ret <= 0)
507 break;
509 end:
510 fclose(fp);
511 return ret;
514 static int show_available_vars_at(struct debuginfo *dinfo,
515 struct perf_probe_event *pev,
516 int max_vls, struct strfilter *_filter,
517 bool externs)
519 char *buf;
520 int ret, i, nvars;
521 struct str_node *node;
522 struct variable_list *vls = NULL, *vl;
523 const char *var;
525 buf = synthesize_perf_probe_point(&pev->point);
526 if (!buf)
527 return -EINVAL;
528 pr_debug("Searching variables at %s\n", buf);
530 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
531 max_vls, externs);
532 if (ret <= 0) {
533 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
534 goto end;
536 /* Some variables are found */
537 fprintf(stdout, "Available variables at %s\n", buf);
538 for (i = 0; i < ret; i++) {
539 vl = &vls[i];
541 * A probe point might be converted to
542 * several trace points.
544 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
545 vl->point.offset);
546 free(vl->point.symbol);
547 nvars = 0;
548 if (vl->vars) {
549 strlist__for_each(node, vl->vars) {
550 var = strchr(node->s, '\t') + 1;
551 if (strfilter__compare(_filter, var)) {
552 fprintf(stdout, "\t\t%s\n", node->s);
553 nvars++;
556 strlist__delete(vl->vars);
558 if (nvars == 0)
559 fprintf(stdout, "\t\t(No matched variables)\n");
561 free(vls);
562 end:
563 free(buf);
564 return ret;
567 /* Show available variables on given probe point */
568 int show_available_vars(struct perf_probe_event *pevs, int npevs,
569 int max_vls, const char *module,
570 struct strfilter *_filter, bool externs)
572 int i, ret = 0;
573 struct debuginfo *dinfo;
575 ret = init_vmlinux();
576 if (ret < 0)
577 return ret;
579 dinfo = open_debuginfo(module);
580 if (!dinfo) {
581 pr_warning("Failed to open debuginfo file.\n");
582 return -ENOENT;
585 setup_pager();
587 for (i = 0; i < npevs && ret >= 0; i++)
588 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
589 externs);
591 debuginfo__delete(dinfo);
592 return ret;
595 #else /* !DWARF_SUPPORT */
597 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
598 struct perf_probe_point *pp)
600 struct symbol *sym;
602 sym = __find_kernel_function_by_name(tp->symbol, NULL);
603 if (!sym) {
604 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
605 return -ENOENT;
607 pp->function = strdup(tp->symbol);
608 if (pp->function == NULL)
609 return -ENOMEM;
610 pp->offset = tp->offset;
611 pp->retprobe = tp->retprobe;
613 return 0;
616 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
617 struct probe_trace_event **tevs __unused,
618 int max_tevs __unused, const char *mod __unused)
620 if (perf_probe_event_need_dwarf(pev)) {
621 pr_warning("Debuginfo-analysis is not supported.\n");
622 return -ENOSYS;
624 return 0;
627 int show_line_range(struct line_range *lr __unused, const char *module __unused)
629 pr_warning("Debuginfo-analysis is not supported.\n");
630 return -ENOSYS;
633 int show_available_vars(struct perf_probe_event *pevs __unused,
634 int npevs __unused, int max_vls __unused,
635 const char *module __unused,
636 struct strfilter *filter __unused,
637 bool externs __unused)
639 pr_warning("Debuginfo-analysis is not supported.\n");
640 return -ENOSYS;
642 #endif
644 static int parse_line_num(char **ptr, int *val, const char *what)
646 const char *start = *ptr;
648 errno = 0;
649 *val = strtol(*ptr, ptr, 0);
650 if (errno || *ptr == start) {
651 semantic_error("'%s' is not a valid number.\n", what);
652 return -EINVAL;
654 return 0;
658 * Stuff 'lr' according to the line range described by 'arg'.
659 * The line range syntax is described by:
661 * SRC[:SLN[+NUM|-ELN]]
662 * FNC[@SRC][:SLN[+NUM|-ELN]]
664 int parse_line_range_desc(const char *arg, struct line_range *lr)
666 char *range, *file, *name = strdup(arg);
667 int err;
669 if (!name)
670 return -ENOMEM;
672 lr->start = 0;
673 lr->end = INT_MAX;
675 range = strchr(name, ':');
676 if (range) {
677 *range++ = '\0';
679 err = parse_line_num(&range, &lr->start, "start line");
680 if (err)
681 goto err;
683 if (*range == '+' || *range == '-') {
684 const char c = *range++;
686 err = parse_line_num(&range, &lr->end, "end line");
687 if (err)
688 goto err;
690 if (c == '+') {
691 lr->end += lr->start;
693 * Adjust the number of lines here.
694 * If the number of lines == 1, the
695 * the end of line should be equal to
696 * the start of line.
698 lr->end--;
702 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
704 err = -EINVAL;
705 if (lr->start > lr->end) {
706 semantic_error("Start line must be smaller"
707 " than end line.\n");
708 goto err;
710 if (*range != '\0') {
711 semantic_error("Tailing with invalid str '%s'.\n", range);
712 goto err;
716 file = strchr(name, '@');
717 if (file) {
718 *file = '\0';
719 lr->file = strdup(++file);
720 if (lr->file == NULL) {
721 err = -ENOMEM;
722 goto err;
724 lr->function = name;
725 } else if (strchr(name, '.'))
726 lr->file = name;
727 else
728 lr->function = name;
730 return 0;
731 err:
732 free(name);
733 return err;
736 /* Check the name is good for event/group */
737 static bool check_event_name(const char *name)
739 if (!isalpha(*name) && *name != '_')
740 return false;
741 while (*++name != '\0') {
742 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
743 return false;
745 return true;
748 /* Parse probepoint definition. */
749 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
751 struct perf_probe_point *pp = &pev->point;
752 char *ptr, *tmp;
753 char c, nc = 0;
755 * <Syntax>
756 * perf probe [EVENT=]SRC[:LN|;PTN]
757 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
759 * TODO:Group name support
762 ptr = strpbrk(arg, ";=@+%");
763 if (ptr && *ptr == '=') { /* Event name */
764 *ptr = '\0';
765 tmp = ptr + 1;
766 if (strchr(arg, ':')) {
767 semantic_error("Group name is not supported yet.\n");
768 return -ENOTSUP;
770 if (!check_event_name(arg)) {
771 semantic_error("%s is bad for event name -it must "
772 "follow C symbol-naming rule.\n", arg);
773 return -EINVAL;
775 pev->event = strdup(arg);
776 if (pev->event == NULL)
777 return -ENOMEM;
778 pev->group = NULL;
779 arg = tmp;
782 ptr = strpbrk(arg, ";:+@%");
783 if (ptr) {
784 nc = *ptr;
785 *ptr++ = '\0';
788 tmp = strdup(arg);
789 if (tmp == NULL)
790 return -ENOMEM;
792 /* Check arg is function or file and copy it */
793 if (strchr(tmp, '.')) /* File */
794 pp->file = tmp;
795 else /* Function */
796 pp->function = tmp;
798 /* Parse other options */
799 while (ptr) {
800 arg = ptr;
801 c = nc;
802 if (c == ';') { /* Lazy pattern must be the last part */
803 pp->lazy_line = strdup(arg);
804 if (pp->lazy_line == NULL)
805 return -ENOMEM;
806 break;
808 ptr = strpbrk(arg, ";:+@%");
809 if (ptr) {
810 nc = *ptr;
811 *ptr++ = '\0';
813 switch (c) {
814 case ':': /* Line number */
815 pp->line = strtoul(arg, &tmp, 0);
816 if (*tmp != '\0') {
817 semantic_error("There is non-digit char"
818 " in line number.\n");
819 return -EINVAL;
821 break;
822 case '+': /* Byte offset from a symbol */
823 pp->offset = strtoul(arg, &tmp, 0);
824 if (*tmp != '\0') {
825 semantic_error("There is non-digit character"
826 " in offset.\n");
827 return -EINVAL;
829 break;
830 case '@': /* File name */
831 if (pp->file) {
832 semantic_error("SRC@SRC is not allowed.\n");
833 return -EINVAL;
835 pp->file = strdup(arg);
836 if (pp->file == NULL)
837 return -ENOMEM;
838 break;
839 case '%': /* Probe places */
840 if (strcmp(arg, "return") == 0) {
841 pp->retprobe = 1;
842 } else { /* Others not supported yet */
843 semantic_error("%%%s is not supported.\n", arg);
844 return -ENOTSUP;
846 break;
847 default: /* Buggy case */
848 pr_err("This program has a bug at %s:%d.\n",
849 __FILE__, __LINE__);
850 return -ENOTSUP;
851 break;
855 /* Exclusion check */
856 if (pp->lazy_line && pp->line) {
857 semantic_error("Lazy pattern can't be used with"
858 " line number.\n");
859 return -EINVAL;
862 if (pp->lazy_line && pp->offset) {
863 semantic_error("Lazy pattern can't be used with offset.\n");
864 return -EINVAL;
867 if (pp->line && pp->offset) {
868 semantic_error("Offset can't be used with line number.\n");
869 return -EINVAL;
872 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
873 semantic_error("File always requires line number or "
874 "lazy pattern.\n");
875 return -EINVAL;
878 if (pp->offset && !pp->function) {
879 semantic_error("Offset requires an entry function.\n");
880 return -EINVAL;
883 if (pp->retprobe && !pp->function) {
884 semantic_error("Return probe requires an entry function.\n");
885 return -EINVAL;
888 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
889 semantic_error("Offset/Line/Lazy pattern can't be used with "
890 "return probe.\n");
891 return -EINVAL;
894 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
895 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
896 pp->lazy_line);
897 return 0;
900 /* Parse perf-probe event argument */
901 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
903 char *tmp, *goodname;
904 struct perf_probe_arg_field **fieldp;
906 pr_debug("parsing arg: %s into ", str);
908 tmp = strchr(str, '=');
909 if (tmp) {
910 arg->name = strndup(str, tmp - str);
911 if (arg->name == NULL)
912 return -ENOMEM;
913 pr_debug("name:%s ", arg->name);
914 str = tmp + 1;
917 tmp = strchr(str, ':');
918 if (tmp) { /* Type setting */
919 *tmp = '\0';
920 arg->type = strdup(tmp + 1);
921 if (arg->type == NULL)
922 return -ENOMEM;
923 pr_debug("type:%s ", arg->type);
926 tmp = strpbrk(str, "-.[");
927 if (!is_c_varname(str) || !tmp) {
928 /* A variable, register, symbol or special value */
929 arg->var = strdup(str);
930 if (arg->var == NULL)
931 return -ENOMEM;
932 pr_debug("%s\n", arg->var);
933 return 0;
936 /* Structure fields or array element */
937 arg->var = strndup(str, tmp - str);
938 if (arg->var == NULL)
939 return -ENOMEM;
940 goodname = arg->var;
941 pr_debug("%s, ", arg->var);
942 fieldp = &arg->field;
944 do {
945 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
946 if (*fieldp == NULL)
947 return -ENOMEM;
948 if (*tmp == '[') { /* Array */
949 str = tmp;
950 (*fieldp)->index = strtol(str + 1, &tmp, 0);
951 (*fieldp)->ref = true;
952 if (*tmp != ']' || tmp == str + 1) {
953 semantic_error("Array index must be a"
954 " number.\n");
955 return -EINVAL;
957 tmp++;
958 if (*tmp == '\0')
959 tmp = NULL;
960 } else { /* Structure */
961 if (*tmp == '.') {
962 str = tmp + 1;
963 (*fieldp)->ref = false;
964 } else if (tmp[1] == '>') {
965 str = tmp + 2;
966 (*fieldp)->ref = true;
967 } else {
968 semantic_error("Argument parse error: %s\n",
969 str);
970 return -EINVAL;
972 tmp = strpbrk(str, "-.[");
974 if (tmp) {
975 (*fieldp)->name = strndup(str, tmp - str);
976 if ((*fieldp)->name == NULL)
977 return -ENOMEM;
978 if (*str != '[')
979 goodname = (*fieldp)->name;
980 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
981 fieldp = &(*fieldp)->next;
983 } while (tmp);
984 (*fieldp)->name = strdup(str);
985 if ((*fieldp)->name == NULL)
986 return -ENOMEM;
987 if (*str != '[')
988 goodname = (*fieldp)->name;
989 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
991 /* If no name is specified, set the last field name (not array index)*/
992 if (!arg->name) {
993 arg->name = strdup(goodname);
994 if (arg->name == NULL)
995 return -ENOMEM;
997 return 0;
1000 /* Parse perf-probe event command */
1001 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1003 char **argv;
1004 int argc, i, ret = 0;
1006 argv = argv_split(cmd, &argc);
1007 if (!argv) {
1008 pr_debug("Failed to split arguments.\n");
1009 return -ENOMEM;
1011 if (argc - 1 > MAX_PROBE_ARGS) {
1012 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1013 ret = -ERANGE;
1014 goto out;
1016 /* Parse probe point */
1017 ret = parse_perf_probe_point(argv[0], pev);
1018 if (ret < 0)
1019 goto out;
1021 /* Copy arguments and ensure return probe has no C argument */
1022 pev->nargs = argc - 1;
1023 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1024 if (pev->args == NULL) {
1025 ret = -ENOMEM;
1026 goto out;
1028 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1029 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1030 if (ret >= 0 &&
1031 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1032 semantic_error("You can't specify local variable for"
1033 " kretprobe.\n");
1034 ret = -EINVAL;
1037 out:
1038 argv_free(argv);
1040 return ret;
1043 /* Return true if this perf_probe_event requires debuginfo */
1044 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1046 int i;
1048 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1049 return true;
1051 for (i = 0; i < pev->nargs; i++)
1052 if (is_c_varname(pev->args[i].var))
1053 return true;
1055 return false;
1058 /* Parse probe_events event into struct probe_point */
1059 static int parse_probe_trace_command(const char *cmd,
1060 struct probe_trace_event *tev)
1062 struct probe_trace_point *tp = &tev->point;
1063 char pr;
1064 char *p;
1065 int ret, i, argc;
1066 char **argv;
1068 pr_debug("Parsing probe_events: %s\n", cmd);
1069 argv = argv_split(cmd, &argc);
1070 if (!argv) {
1071 pr_debug("Failed to split arguments.\n");
1072 return -ENOMEM;
1074 if (argc < 2) {
1075 semantic_error("Too few probe arguments.\n");
1076 ret = -ERANGE;
1077 goto out;
1080 /* Scan event and group name. */
1081 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
1082 &pr, (float *)(void *)&tev->group,
1083 (float *)(void *)&tev->event);
1084 if (ret != 3) {
1085 semantic_error("Failed to parse event name: %s\n", argv[0]);
1086 ret = -EINVAL;
1087 goto out;
1089 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1091 tp->retprobe = (pr == 'r');
1093 /* Scan module name(if there), function name and offset */
1094 p = strchr(argv[1], ':');
1095 if (p) {
1096 tp->module = strndup(argv[1], p - argv[1]);
1097 p++;
1098 } else
1099 p = argv[1];
1100 ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol,
1101 &tp->offset);
1102 if (ret == 1)
1103 tp->offset = 0;
1105 tev->nargs = argc - 2;
1106 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1107 if (tev->args == NULL) {
1108 ret = -ENOMEM;
1109 goto out;
1111 for (i = 0; i < tev->nargs; i++) {
1112 p = strchr(argv[i + 2], '=');
1113 if (p) /* We don't need which register is assigned. */
1114 *p++ = '\0';
1115 else
1116 p = argv[i + 2];
1117 tev->args[i].name = strdup(argv[i + 2]);
1118 /* TODO: parse regs and offset */
1119 tev->args[i].value = strdup(p);
1120 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1121 ret = -ENOMEM;
1122 goto out;
1125 ret = 0;
1126 out:
1127 argv_free(argv);
1128 return ret;
1131 /* Compose only probe arg */
1132 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1134 struct perf_probe_arg_field *field = pa->field;
1135 int ret;
1136 char *tmp = buf;
1138 if (pa->name && pa->var)
1139 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1140 else
1141 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1142 if (ret <= 0)
1143 goto error;
1144 tmp += ret;
1145 len -= ret;
1147 while (field) {
1148 if (field->name[0] == '[')
1149 ret = e_snprintf(tmp, len, "%s", field->name);
1150 else
1151 ret = e_snprintf(tmp, len, "%s%s",
1152 field->ref ? "->" : ".", field->name);
1153 if (ret <= 0)
1154 goto error;
1155 tmp += ret;
1156 len -= ret;
1157 field = field->next;
1160 if (pa->type) {
1161 ret = e_snprintf(tmp, len, ":%s", pa->type);
1162 if (ret <= 0)
1163 goto error;
1164 tmp += ret;
1165 len -= ret;
1168 return tmp - buf;
1169 error:
1170 pr_debug("Failed to synthesize perf probe argument: %s\n",
1171 strerror(-ret));
1172 return ret;
1175 /* Compose only probe point (not argument) */
1176 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1178 char *buf, *tmp;
1179 char offs[32] = "", line[32] = "", file[32] = "";
1180 int ret, len;
1182 buf = zalloc(MAX_CMDLEN);
1183 if (buf == NULL) {
1184 ret = -ENOMEM;
1185 goto error;
1187 if (pp->offset) {
1188 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1189 if (ret <= 0)
1190 goto error;
1192 if (pp->line) {
1193 ret = e_snprintf(line, 32, ":%d", pp->line);
1194 if (ret <= 0)
1195 goto error;
1197 if (pp->file) {
1198 tmp = pp->file;
1199 len = strlen(tmp);
1200 if (len > 30) {
1201 tmp = strchr(pp->file + len - 30, '/');
1202 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1204 ret = e_snprintf(file, 32, "@%s", tmp);
1205 if (ret <= 0)
1206 goto error;
1209 if (pp->function)
1210 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1211 offs, pp->retprobe ? "%return" : "", line,
1212 file);
1213 else
1214 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1215 if (ret <= 0)
1216 goto error;
1218 return buf;
1219 error:
1220 pr_debug("Failed to synthesize perf probe point: %s\n",
1221 strerror(-ret));
1222 if (buf)
1223 free(buf);
1224 return NULL;
1227 #if 0
1228 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1230 char *buf;
1231 int i, len, ret;
1233 buf = synthesize_perf_probe_point(&pev->point);
1234 if (!buf)
1235 return NULL;
1237 len = strlen(buf);
1238 for (i = 0; i < pev->nargs; i++) {
1239 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1240 pev->args[i].name);
1241 if (ret <= 0) {
1242 free(buf);
1243 return NULL;
1245 len += ret;
1248 return buf;
1250 #endif
1252 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1253 char **buf, size_t *buflen,
1254 int depth)
1256 int ret;
1257 if (ref->next) {
1258 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1259 buflen, depth + 1);
1260 if (depth < 0)
1261 goto out;
1264 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1265 if (ret < 0)
1266 depth = ret;
1267 else {
1268 *buf += ret;
1269 *buflen -= ret;
1271 out:
1272 return depth;
1276 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1277 char *buf, size_t buflen)
1279 struct probe_trace_arg_ref *ref = arg->ref;
1280 int ret, depth = 0;
1281 char *tmp = buf;
1283 /* Argument name or separator */
1284 if (arg->name)
1285 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1286 else
1287 ret = e_snprintf(buf, buflen, " ");
1288 if (ret < 0)
1289 return ret;
1290 buf += ret;
1291 buflen -= ret;
1293 /* Special case: @XXX */
1294 if (arg->value[0] == '@' && arg->ref)
1295 ref = ref->next;
1297 /* Dereferencing arguments */
1298 if (ref) {
1299 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1300 &buflen, 1);
1301 if (depth < 0)
1302 return depth;
1305 /* Print argument value */
1306 if (arg->value[0] == '@' && arg->ref)
1307 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1308 arg->ref->offset);
1309 else
1310 ret = e_snprintf(buf, buflen, "%s", arg->value);
1311 if (ret < 0)
1312 return ret;
1313 buf += ret;
1314 buflen -= ret;
1316 /* Closing */
1317 while (depth--) {
1318 ret = e_snprintf(buf, buflen, ")");
1319 if (ret < 0)
1320 return ret;
1321 buf += ret;
1322 buflen -= ret;
1324 /* Print argument type */
1325 if (arg->type) {
1326 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1327 if (ret <= 0)
1328 return ret;
1329 buf += ret;
1332 return buf - tmp;
1335 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1337 struct probe_trace_point *tp = &tev->point;
1338 char *buf;
1339 int i, len, ret;
1341 buf = zalloc(MAX_CMDLEN);
1342 if (buf == NULL)
1343 return NULL;
1345 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
1346 tp->retprobe ? 'r' : 'p',
1347 tev->group, tev->event,
1348 tp->module ?: "", tp->module ? ":" : "",
1349 tp->symbol, tp->offset);
1350 if (len <= 0)
1351 goto error;
1353 for (i = 0; i < tev->nargs; i++) {
1354 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1355 MAX_CMDLEN - len);
1356 if (ret <= 0)
1357 goto error;
1358 len += ret;
1361 return buf;
1362 error:
1363 free(buf);
1364 return NULL;
1367 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1368 struct perf_probe_event *pev)
1370 char buf[64] = "";
1371 int i, ret;
1373 /* Convert event/group name */
1374 pev->event = strdup(tev->event);
1375 pev->group = strdup(tev->group);
1376 if (pev->event == NULL || pev->group == NULL)
1377 return -ENOMEM;
1379 /* Convert trace_point to probe_point */
1380 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1381 if (ret < 0)
1382 return ret;
1384 /* Convert trace_arg to probe_arg */
1385 pev->nargs = tev->nargs;
1386 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1387 if (pev->args == NULL)
1388 return -ENOMEM;
1389 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1390 if (tev->args[i].name)
1391 pev->args[i].name = strdup(tev->args[i].name);
1392 else {
1393 ret = synthesize_probe_trace_arg(&tev->args[i],
1394 buf, 64);
1395 pev->args[i].name = strdup(buf);
1397 if (pev->args[i].name == NULL && ret >= 0)
1398 ret = -ENOMEM;
1401 if (ret < 0)
1402 clear_perf_probe_event(pev);
1404 return ret;
1407 void clear_perf_probe_event(struct perf_probe_event *pev)
1409 struct perf_probe_point *pp = &pev->point;
1410 struct perf_probe_arg_field *field, *next;
1411 int i;
1413 if (pev->event)
1414 free(pev->event);
1415 if (pev->group)
1416 free(pev->group);
1417 if (pp->file)
1418 free(pp->file);
1419 if (pp->function)
1420 free(pp->function);
1421 if (pp->lazy_line)
1422 free(pp->lazy_line);
1423 for (i = 0; i < pev->nargs; i++) {
1424 if (pev->args[i].name)
1425 free(pev->args[i].name);
1426 if (pev->args[i].var)
1427 free(pev->args[i].var);
1428 if (pev->args[i].type)
1429 free(pev->args[i].type);
1430 field = pev->args[i].field;
1431 while (field) {
1432 next = field->next;
1433 if (field->name)
1434 free(field->name);
1435 free(field);
1436 field = next;
1439 if (pev->args)
1440 free(pev->args);
1441 memset(pev, 0, sizeof(*pev));
1444 static void clear_probe_trace_event(struct probe_trace_event *tev)
1446 struct probe_trace_arg_ref *ref, *next;
1447 int i;
1449 if (tev->event)
1450 free(tev->event);
1451 if (tev->group)
1452 free(tev->group);
1453 if (tev->point.symbol)
1454 free(tev->point.symbol);
1455 if (tev->point.module)
1456 free(tev->point.module);
1457 for (i = 0; i < tev->nargs; i++) {
1458 if (tev->args[i].name)
1459 free(tev->args[i].name);
1460 if (tev->args[i].value)
1461 free(tev->args[i].value);
1462 if (tev->args[i].type)
1463 free(tev->args[i].type);
1464 ref = tev->args[i].ref;
1465 while (ref) {
1466 next = ref->next;
1467 free(ref);
1468 ref = next;
1471 if (tev->args)
1472 free(tev->args);
1473 memset(tev, 0, sizeof(*tev));
1476 static int open_kprobe_events(bool readwrite)
1478 char buf[PATH_MAX];
1479 const char *__debugfs;
1480 int ret;
1482 __debugfs = debugfs_find_mountpoint();
1483 if (__debugfs == NULL) {
1484 pr_warning("Debugfs is not mounted.\n");
1485 return -ENOENT;
1488 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
1489 if (ret >= 0) {
1490 pr_debug("Opening %s write=%d\n", buf, readwrite);
1491 if (readwrite && !probe_event_dry_run)
1492 ret = open(buf, O_RDWR, O_APPEND);
1493 else
1494 ret = open(buf, O_RDONLY, 0);
1497 if (ret < 0) {
1498 if (errno == ENOENT)
1499 pr_warning("kprobe_events file does not exist - please"
1500 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1501 else
1502 pr_warning("Failed to open kprobe_events file: %s\n",
1503 strerror(errno));
1505 return ret;
1508 /* Get raw string list of current kprobe_events */
1509 static struct strlist *get_probe_trace_command_rawlist(int fd)
1511 int ret, idx;
1512 FILE *fp;
1513 char buf[MAX_CMDLEN];
1514 char *p;
1515 struct strlist *sl;
1517 sl = strlist__new(true, NULL);
1519 fp = fdopen(dup(fd), "r");
1520 while (!feof(fp)) {
1521 p = fgets(buf, MAX_CMDLEN, fp);
1522 if (!p)
1523 break;
1525 idx = strlen(p) - 1;
1526 if (p[idx] == '\n')
1527 p[idx] = '\0';
1528 ret = strlist__add(sl, buf);
1529 if (ret < 0) {
1530 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1531 strlist__delete(sl);
1532 return NULL;
1535 fclose(fp);
1537 return sl;
1540 /* Show an event */
1541 static int show_perf_probe_event(struct perf_probe_event *pev)
1543 int i, ret;
1544 char buf[128];
1545 char *place;
1547 /* Synthesize only event probe point */
1548 place = synthesize_perf_probe_point(&pev->point);
1549 if (!place)
1550 return -EINVAL;
1552 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
1553 if (ret < 0)
1554 return ret;
1556 printf(" %-20s (on %s", buf, place);
1558 if (pev->nargs > 0) {
1559 printf(" with");
1560 for (i = 0; i < pev->nargs; i++) {
1561 ret = synthesize_perf_probe_arg(&pev->args[i],
1562 buf, 128);
1563 if (ret < 0)
1564 break;
1565 printf(" %s", buf);
1568 printf(")\n");
1569 free(place);
1570 return ret;
1573 /* List up current perf-probe events */
1574 int show_perf_probe_events(void)
1576 int fd, ret;
1577 struct probe_trace_event tev;
1578 struct perf_probe_event pev;
1579 struct strlist *rawlist;
1580 struct str_node *ent;
1582 setup_pager();
1583 ret = init_vmlinux();
1584 if (ret < 0)
1585 return ret;
1587 memset(&tev, 0, sizeof(tev));
1588 memset(&pev, 0, sizeof(pev));
1590 fd = open_kprobe_events(false);
1591 if (fd < 0)
1592 return fd;
1594 rawlist = get_probe_trace_command_rawlist(fd);
1595 close(fd);
1596 if (!rawlist)
1597 return -ENOENT;
1599 strlist__for_each(ent, rawlist) {
1600 ret = parse_probe_trace_command(ent->s, &tev);
1601 if (ret >= 0) {
1602 ret = convert_to_perf_probe_event(&tev, &pev);
1603 if (ret >= 0)
1604 ret = show_perf_probe_event(&pev);
1606 clear_perf_probe_event(&pev);
1607 clear_probe_trace_event(&tev);
1608 if (ret < 0)
1609 break;
1611 strlist__delete(rawlist);
1613 return ret;
1616 /* Get current perf-probe event names */
1617 static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
1619 char buf[128];
1620 struct strlist *sl, *rawlist;
1621 struct str_node *ent;
1622 struct probe_trace_event tev;
1623 int ret = 0;
1625 memset(&tev, 0, sizeof(tev));
1626 rawlist = get_probe_trace_command_rawlist(fd);
1627 sl = strlist__new(true, NULL);
1628 strlist__for_each(ent, rawlist) {
1629 ret = parse_probe_trace_command(ent->s, &tev);
1630 if (ret < 0)
1631 break;
1632 if (include_group) {
1633 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1634 tev.event);
1635 if (ret >= 0)
1636 ret = strlist__add(sl, buf);
1637 } else
1638 ret = strlist__add(sl, tev.event);
1639 clear_probe_trace_event(&tev);
1640 if (ret < 0)
1641 break;
1643 strlist__delete(rawlist);
1645 if (ret < 0) {
1646 strlist__delete(sl);
1647 return NULL;
1649 return sl;
1652 static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
1654 int ret = 0;
1655 char *buf = synthesize_probe_trace_command(tev);
1657 if (!buf) {
1658 pr_debug("Failed to synthesize probe trace event.\n");
1659 return -EINVAL;
1662 pr_debug("Writing event: %s\n", buf);
1663 if (!probe_event_dry_run) {
1664 ret = write(fd, buf, strlen(buf));
1665 if (ret <= 0)
1666 pr_warning("Failed to write event: %s\n",
1667 strerror(errno));
1669 free(buf);
1670 return ret;
1673 static int get_new_event_name(char *buf, size_t len, const char *base,
1674 struct strlist *namelist, bool allow_suffix)
1676 int i, ret;
1678 /* Try no suffix */
1679 ret = e_snprintf(buf, len, "%s", base);
1680 if (ret < 0) {
1681 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1682 return ret;
1684 if (!strlist__has_entry(namelist, buf))
1685 return 0;
1687 if (!allow_suffix) {
1688 pr_warning("Error: event \"%s\" already exists. "
1689 "(Use -f to force duplicates.)\n", base);
1690 return -EEXIST;
1693 /* Try to add suffix */
1694 for (i = 1; i < MAX_EVENT_INDEX; i++) {
1695 ret = e_snprintf(buf, len, "%s_%d", base, i);
1696 if (ret < 0) {
1697 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1698 return ret;
1700 if (!strlist__has_entry(namelist, buf))
1701 break;
1703 if (i == MAX_EVENT_INDEX) {
1704 pr_warning("Too many events are on the same function.\n");
1705 ret = -ERANGE;
1708 return ret;
1711 static int __add_probe_trace_events(struct perf_probe_event *pev,
1712 struct probe_trace_event *tevs,
1713 int ntevs, bool allow_suffix)
1715 int i, fd, ret;
1716 struct probe_trace_event *tev = NULL;
1717 char buf[64];
1718 const char *event, *group;
1719 struct strlist *namelist;
1721 fd = open_kprobe_events(true);
1722 if (fd < 0)
1723 return fd;
1724 /* Get current event names */
1725 namelist = get_probe_trace_event_names(fd, false);
1726 if (!namelist) {
1727 pr_debug("Failed to get current event list.\n");
1728 return -EIO;
1731 ret = 0;
1732 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
1733 for (i = 0; i < ntevs; i++) {
1734 tev = &tevs[i];
1735 if (pev->event)
1736 event = pev->event;
1737 else
1738 if (pev->point.function)
1739 event = pev->point.function;
1740 else
1741 event = tev->point.symbol;
1742 if (pev->group)
1743 group = pev->group;
1744 else
1745 group = PERFPROBE_GROUP;
1747 /* Get an unused new event name */
1748 ret = get_new_event_name(buf, 64, event,
1749 namelist, allow_suffix);
1750 if (ret < 0)
1751 break;
1752 event = buf;
1754 tev->event = strdup(event);
1755 tev->group = strdup(group);
1756 if (tev->event == NULL || tev->group == NULL) {
1757 ret = -ENOMEM;
1758 break;
1760 ret = write_probe_trace_event(fd, tev);
1761 if (ret < 0)
1762 break;
1763 /* Add added event name to namelist */
1764 strlist__add(namelist, event);
1766 /* Trick here - save current event/group */
1767 event = pev->event;
1768 group = pev->group;
1769 pev->event = tev->event;
1770 pev->group = tev->group;
1771 show_perf_probe_event(pev);
1772 /* Trick here - restore current event/group */
1773 pev->event = (char *)event;
1774 pev->group = (char *)group;
1777 * Probes after the first probe which comes from same
1778 * user input are always allowed to add suffix, because
1779 * there might be several addresses corresponding to
1780 * one code line.
1782 allow_suffix = true;
1785 if (ret >= 0) {
1786 /* Show how to use the event. */
1787 printf("\nYou can now use it on all perf tools, such as:\n\n");
1788 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1789 tev->event);
1792 strlist__delete(namelist);
1793 close(fd);
1794 return ret;
1797 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1798 struct probe_trace_event **tevs,
1799 int max_tevs, const char *module)
1801 struct symbol *sym;
1802 int ret = 0, i;
1803 struct probe_trace_event *tev;
1805 /* Convert perf_probe_event with debuginfo */
1806 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
1807 if (ret != 0)
1808 return ret; /* Found in debuginfo or got an error */
1810 /* Allocate trace event buffer */
1811 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
1812 if (tev == NULL)
1813 return -ENOMEM;
1815 /* Copy parameters */
1816 tev->point.symbol = strdup(pev->point.function);
1817 if (tev->point.symbol == NULL) {
1818 ret = -ENOMEM;
1819 goto error;
1822 if (module) {
1823 tev->point.module = strdup(module);
1824 if (tev->point.module == NULL) {
1825 ret = -ENOMEM;
1826 goto error;
1830 tev->point.offset = pev->point.offset;
1831 tev->point.retprobe = pev->point.retprobe;
1832 tev->nargs = pev->nargs;
1833 if (tev->nargs) {
1834 tev->args = zalloc(sizeof(struct probe_trace_arg)
1835 * tev->nargs);
1836 if (tev->args == NULL) {
1837 ret = -ENOMEM;
1838 goto error;
1840 for (i = 0; i < tev->nargs; i++) {
1841 if (pev->args[i].name) {
1842 tev->args[i].name = strdup(pev->args[i].name);
1843 if (tev->args[i].name == NULL) {
1844 ret = -ENOMEM;
1845 goto error;
1848 tev->args[i].value = strdup(pev->args[i].var);
1849 if (tev->args[i].value == NULL) {
1850 ret = -ENOMEM;
1851 goto error;
1853 if (pev->args[i].type) {
1854 tev->args[i].type = strdup(pev->args[i].type);
1855 if (tev->args[i].type == NULL) {
1856 ret = -ENOMEM;
1857 goto error;
1863 /* Currently just checking function name from symbol map */
1864 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
1865 if (!sym) {
1866 pr_warning("Kernel symbol \'%s\' not found.\n",
1867 tev->point.symbol);
1868 ret = -ENOENT;
1869 goto error;
1870 } else if (tev->point.offset > sym->end - sym->start) {
1871 pr_warning("Offset specified is greater than size of %s\n",
1872 tev->point.symbol);
1873 ret = -ENOENT;
1874 goto error;
1878 return 1;
1879 error:
1880 clear_probe_trace_event(tev);
1881 free(tev);
1882 *tevs = NULL;
1883 return ret;
1886 struct __event_package {
1887 struct perf_probe_event *pev;
1888 struct probe_trace_event *tevs;
1889 int ntevs;
1892 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1893 int max_tevs, const char *module, bool force_add)
1895 int i, j, ret;
1896 struct __event_package *pkgs;
1898 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1899 if (pkgs == NULL)
1900 return -ENOMEM;
1902 /* Init vmlinux path */
1903 ret = init_vmlinux();
1904 if (ret < 0) {
1905 free(pkgs);
1906 return ret;
1909 /* Loop 1: convert all events */
1910 for (i = 0; i < npevs; i++) {
1911 pkgs[i].pev = &pevs[i];
1912 /* Convert with or without debuginfo */
1913 ret = convert_to_probe_trace_events(pkgs[i].pev,
1914 &pkgs[i].tevs,
1915 max_tevs,
1916 module);
1917 if (ret < 0)
1918 goto end;
1919 pkgs[i].ntevs = ret;
1922 /* Loop 2: add all events */
1923 for (i = 0; i < npevs; i++) {
1924 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1925 pkgs[i].ntevs, force_add);
1926 if (ret < 0)
1927 break;
1929 end:
1930 /* Loop 3: cleanup and free trace events */
1931 for (i = 0; i < npevs; i++) {
1932 for (j = 0; j < pkgs[i].ntevs; j++)
1933 clear_probe_trace_event(&pkgs[i].tevs[j]);
1934 free(pkgs[i].tevs);
1936 free(pkgs);
1938 return ret;
1941 static int __del_trace_probe_event(int fd, struct str_node *ent)
1943 char *p;
1944 char buf[128];
1945 int ret;
1947 /* Convert from perf-probe event to trace-probe event */
1948 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1949 if (ret < 0)
1950 goto error;
1952 p = strchr(buf + 2, ':');
1953 if (!p) {
1954 pr_debug("Internal error: %s should have ':' but not.\n",
1955 ent->s);
1956 ret = -ENOTSUP;
1957 goto error;
1959 *p = '/';
1961 pr_debug("Writing event: %s\n", buf);
1962 ret = write(fd, buf, strlen(buf));
1963 if (ret < 0) {
1964 ret = -errno;
1965 goto error;
1968 printf("Remove event: %s\n", ent->s);
1969 return 0;
1970 error:
1971 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1972 return ret;
1975 static int del_trace_probe_event(int fd, const char *group,
1976 const char *event, struct strlist *namelist)
1978 char buf[128];
1979 struct str_node *ent, *n;
1980 int found = 0, ret = 0;
1982 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1983 if (ret < 0) {
1984 pr_err("Failed to copy event.\n");
1985 return ret;
1988 if (strpbrk(buf, "*?")) { /* Glob-exp */
1989 strlist__for_each_safe(ent, n, namelist)
1990 if (strglobmatch(ent->s, buf)) {
1991 found++;
1992 ret = __del_trace_probe_event(fd, ent);
1993 if (ret < 0)
1994 break;
1995 strlist__remove(namelist, ent);
1997 } else {
1998 ent = strlist__find(namelist, buf);
1999 if (ent) {
2000 found++;
2001 ret = __del_trace_probe_event(fd, ent);
2002 if (ret >= 0)
2003 strlist__remove(namelist, ent);
2006 if (found == 0 && ret >= 0)
2007 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2009 return ret;
2012 int del_perf_probe_events(struct strlist *dellist)
2014 int fd, ret = 0;
2015 const char *group, *event;
2016 char *p, *str;
2017 struct str_node *ent;
2018 struct strlist *namelist;
2020 fd = open_kprobe_events(true);
2021 if (fd < 0)
2022 return fd;
2024 /* Get current event names */
2025 namelist = get_probe_trace_event_names(fd, true);
2026 if (namelist == NULL)
2027 return -EINVAL;
2029 strlist__for_each(ent, dellist) {
2030 str = strdup(ent->s);
2031 if (str == NULL) {
2032 ret = -ENOMEM;
2033 break;
2035 pr_debug("Parsing: %s\n", str);
2036 p = strchr(str, ':');
2037 if (p) {
2038 group = str;
2039 *p = '\0';
2040 event = p + 1;
2041 } else {
2042 group = "*";
2043 event = str;
2045 pr_debug("Group: %s, Event: %s\n", group, event);
2046 ret = del_trace_probe_event(fd, group, event, namelist);
2047 free(str);
2048 if (ret < 0)
2049 break;
2051 strlist__delete(namelist);
2052 close(fd);
2054 return ret;
2056 /* TODO: don't use a global variable for filter ... */
2057 static struct strfilter *available_func_filter;
2060 * If a symbol corresponds to a function with global binding and
2061 * matches filter return 0. For all others return 1.
2063 static int filter_available_functions(struct map *map __unused,
2064 struct symbol *sym)
2066 if (sym->binding == STB_GLOBAL &&
2067 strfilter__compare(available_func_filter, sym->name))
2068 return 0;
2069 return 1;
2072 int show_available_funcs(const char *module, struct strfilter *_filter)
2074 struct map *map;
2075 int ret;
2077 setup_pager();
2079 ret = init_vmlinux();
2080 if (ret < 0)
2081 return ret;
2083 map = kernel_get_module_map(module);
2084 if (!map) {
2085 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
2086 return -EINVAL;
2088 available_func_filter = _filter;
2089 if (map__load(map, filter_available_functions)) {
2090 pr_err("Failed to load map.\n");
2091 return -EINVAL;
2093 if (!dso__sorted_by_name(map->dso, map->type))
2094 dso__sort_by_name(map->dso, map->type);
2096 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2097 return 0;