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.
23 #include <sys/utsname.h>
24 #include <sys/types.h>
46 #include "trace-event.h" /* For __unused */
47 #include "probe-event.h"
48 #include "probe-finder.h"
50 #define MAX_CMDLEN 256
51 #define MAX_PROBE_ARGS 128
52 #define PERFPROBE_GROUP "probe"
54 bool probe_event_dry_run; /* Dry run flag */
56 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
58 /* If there is no space to write, returns -E2BIG. */
59 static int e_snprintf(char *str, size_t size, const char *format, ...)
60 __attribute__((format(printf, 3, 4)));
62 static int e_snprintf(char *str, size_t size, const char *format, ...)
67 ret = vsnprintf(str, size, format, ap);
74 static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
75 static struct machine machine;
77 /* Initialize symbol maps and path of vmlinux/modules */
78 static int init_vmlinux(void)
82 symbol_conf.sort_by_name = true;
83 if (symbol_conf.vmlinux_name == NULL)
84 symbol_conf.try_vmlinux_path = true;
86 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
89 pr_debug("Failed to init symbol map.\n");
93 ret = machine__init(&machine, "", HOST_KERNEL_ID);
97 if (machine__create_kernel_maps(&machine) < 0) {
98 pr_debug("machine__create_kernel_maps ");
103 pr_warning("Failed to init vmlinux path.\n");
107 static struct symbol *__find_kernel_function_by_name(const char *name,
110 return machine__find_kernel_function_by_name(&machine, name, mapp,
114 const char *kernel_get_module_path(const char *module)
119 list_for_each_entry(dso, &machine.kernel_dsos, node) {
120 if (strncmp(dso->short_name + 1, module,
121 dso->short_name_len - 2) == 0)
124 pr_debug("Failed to find module %s.\n", module);
127 dso = machine.vmlinux_maps[MAP__FUNCTION]->dso;
128 if (dso__load_vmlinux_path(dso,
129 machine.vmlinux_maps[MAP__FUNCTION], NULL) < 0) {
130 pr_debug("Failed to load kernel map.\n");
135 return dso->long_name;
139 static int open_vmlinux(const char *module)
141 const char *path = kernel_get_module_path(module);
143 pr_err("Failed to find path of %s module", module ?: "kernel");
146 pr_debug("Try to open %s\n", path);
147 return open(path, O_RDONLY);
151 * Convert trace point to probe point with debuginfo
152 * Currently only handles kprobes.
154 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
155 struct perf_probe_point *pp)
162 sym = __find_kernel_function_by_name(tp->symbol, &map);
164 addr = map->unmap_ip(map, sym->start + tp->offset);
165 pr_debug("try to find %s+%ld@%llx\n", tp->symbol,
167 ret = find_perf_probe_point((unsigned long)addr, pp);
170 pr_debug("Failed to find corresponding probes from "
171 "debuginfo. Use kprobe event information.\n");
172 pp->function = strdup(tp->symbol);
173 if (pp->function == NULL)
175 pp->offset = tp->offset;
177 pp->retprobe = tp->retprobe;
182 /* Try to find perf_probe_event with debuginfo */
183 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
184 struct probe_trace_event **tevs,
185 int max_tevs, const char *module)
187 bool need_dwarf = perf_probe_event_need_dwarf(pev);
190 fd = open_vmlinux(module);
193 pr_warning("Failed to open debuginfo file.\n");
196 pr_debug("Could not open vmlinux. Try to use symbols.\n");
200 /* Searching trace events corresponding to probe event */
201 ntevs = find_probe_trace_events(fd, pev, tevs, max_tevs);
204 if (ntevs > 0) { /* Succeeded to find trace events */
205 pr_debug("find %d probe_trace_events.\n", ntevs);
209 if (ntevs == 0) { /* No error but failed to find probe point. */
210 pr_warning("Probe point '%s' not found.\n",
211 synthesize_perf_probe_point(&pev->point));
214 /* Error path : ntevs < 0 */
215 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
216 if (ntevs == -EBADF) {
217 pr_warning("Warning: No dwarf info found in the vmlinux - "
218 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
220 pr_debug("Trying to use symbols.\nn");
228 * Find a src file from a DWARF tag path. Prepend optional source path prefix
229 * and chop off leading directories that do not exist. Result is passed back as
230 * a newly allocated path on success.
231 * Return 0 if file was found and readable, -errno otherwise.
233 static int get_real_path(const char *raw_path, const char *comp_dir,
236 const char *prefix = symbol_conf.source_prefix;
239 if (raw_path[0] != '/' && comp_dir)
240 /* If not an absolute path, try to use comp_dir */
243 if (access(raw_path, R_OK) == 0) {
244 *new_path = strdup(raw_path);
251 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
256 sprintf(*new_path, "%s/%s", prefix, raw_path);
258 if (access(*new_path, R_OK) == 0)
261 if (!symbol_conf.source_prefix)
262 /* In case of searching comp_dir, don't retry */
270 raw_path = strchr(++raw_path, '/');
286 #define LINEBUF_SIZE 256
287 #define NR_ADDITIONAL_LINES 2
289 static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
291 char buf[LINEBUF_SIZE];
292 const char *color = PERF_COLOR_BLUE;
294 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
298 fprintf(stdout, "%7d %s", l, buf);
300 color_fprintf(stdout, color, " %s", buf);
303 while (strlen(buf) == LINEBUF_SIZE - 1 &&
304 buf[LINEBUF_SIZE - 2] != '\n') {
305 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
309 fprintf(stdout, "%s", buf);
311 color_fprintf(stdout, color, "%s", buf);
318 pr_warning("Source file is shorter than expected.\n");
320 pr_warning("File read error: %s\n", strerror(errno));
326 * Show line-range always requires debuginfo to find source file and
329 int show_line_range(struct line_range *lr, const char *module)
332 struct line_node *ln;
337 /* Search a line range */
338 ret = init_vmlinux();
342 fd = open_vmlinux(module);
344 pr_warning("Failed to open debuginfo file.\n");
348 ret = find_line_range(fd, lr);
351 pr_warning("Specified source line is not found.\n");
353 } else if (ret < 0) {
354 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
358 /* Convert source file path */
360 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
361 free(tmp); /* Free old path */
363 pr_warning("Failed to find source file. (%d)\n", ret);
370 fprintf(stdout, "<%s:%d>\n", lr->function,
371 lr->start - lr->offset);
373 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
375 fp = fopen(lr->path, "r");
377 pr_warning("Failed to open %s: %s\n", lr->path,
381 /* Skip to starting line number */
382 while (l < lr->start && ret >= 0)
383 ret = show_one_line(fp, l++, true, false);
387 list_for_each_entry(ln, &lr->line_list, list) {
388 while (ln->line > l && ret >= 0)
389 ret = show_one_line(fp, (l++) - lr->offset,
392 ret = show_one_line(fp, (l++) - lr->offset,
398 if (lr->end == INT_MAX)
399 lr->end = l + NR_ADDITIONAL_LINES;
400 while (l <= lr->end && !feof(fp) && ret >= 0)
401 ret = show_one_line(fp, (l++) - lr->offset, false, false);
407 static int show_available_vars_at(int fd, struct perf_probe_event *pev,
408 int max_vls, bool externs)
412 struct str_node *node;
413 struct variable_list *vls = NULL, *vl;
415 buf = synthesize_perf_probe_point(&pev->point);
418 pr_debug("Searching variables at %s\n", buf);
420 ret = find_available_vars_at(fd, pev, &vls, max_vls, externs);
422 /* Some variables were found */
423 fprintf(stdout, "Available variables at %s\n", buf);
424 for (i = 0; i < ret; i++) {
427 * A probe point might be converted to
428 * several trace points.
430 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
432 free(vl->point.symbol);
434 strlist__for_each(node, vl->vars)
435 fprintf(stdout, "\t\t%s\n", node->s);
436 strlist__delete(vl->vars);
438 fprintf(stdout, "(No variables)\n");
442 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
448 /* Show available variables on given probe point */
449 int show_available_vars(struct perf_probe_event *pevs, int npevs,
450 int max_vls, const char *module, bool externs)
454 ret = init_vmlinux();
458 fd = open_vmlinux(module);
460 pr_warning("Failed to open debuginfo file.\n");
466 for (i = 0; i < npevs && ret >= 0; i++)
467 ret = show_available_vars_at(fd, &pevs[i], max_vls, externs);
473 #else /* !DWARF_SUPPORT */
475 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
476 struct perf_probe_point *pp)
480 sym = __find_kernel_function_by_name(tp->symbol, NULL);
482 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
485 pp->function = strdup(tp->symbol);
486 if (pp->function == NULL)
488 pp->offset = tp->offset;
489 pp->retprobe = tp->retprobe;
494 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
495 struct probe_trace_event **tevs __unused,
496 int max_tevs __unused, const char *mod __unused)
498 if (perf_probe_event_need_dwarf(pev)) {
499 pr_warning("Debuginfo-analysis is not supported.\n");
505 int show_line_range(struct line_range *lr __unused, const char *module __unused)
507 pr_warning("Debuginfo-analysis is not supported.\n");
511 int show_available_vars(struct perf_probe_event *pevs __unused,
512 int npevs __unused, int max_vls __unused,
513 const char *module __unused, bool externs __unused)
515 pr_warning("Debuginfo-analysis is not supported.\n");
520 int parse_line_range_desc(const char *arg, struct line_range *lr)
527 * FUNC[:SLN[+NUM|-ELN]]
529 ptr = strchr(arg, ':');
531 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
533 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
535 * Adjust the number of lines here.
536 * If the number of lines == 1, the
537 * the end of line should be equal to
540 } else if (*tmp == '-')
541 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
544 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
545 if (lr->start > lr->end) {
546 semantic_error("Start line must be smaller"
547 " than end line.\n");
551 semantic_error("Tailing with invalid character '%d'.\n",
555 tmp = strndup(arg, (ptr - arg));
564 if (strchr(tmp, '.'))
572 /* Check the name is good for event/group */
573 static bool check_event_name(const char *name)
575 if (!isalpha(*name) && *name != '_')
577 while (*++name != '\0') {
578 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
584 /* Parse probepoint definition. */
585 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
587 struct perf_probe_point *pp = &pev->point;
592 * perf probe [EVENT=]SRC[:LN|;PTN]
593 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
595 * TODO:Group name support
598 ptr = strpbrk(arg, ";=@+%");
599 if (ptr && *ptr == '=') { /* Event name */
602 if (strchr(arg, ':')) {
603 semantic_error("Group name is not supported yet.\n");
606 if (!check_event_name(arg)) {
607 semantic_error("%s is bad for event name -it must "
608 "follow C symbol-naming rule.\n", arg);
611 pev->event = strdup(arg);
612 if (pev->event == NULL)
618 ptr = strpbrk(arg, ";:+@%");
628 /* Check arg is function or file and copy it */
629 if (strchr(tmp, '.')) /* File */
634 /* Parse other options */
638 if (c == ';') { /* Lazy pattern must be the last part */
639 pp->lazy_line = strdup(arg);
640 if (pp->lazy_line == NULL)
644 ptr = strpbrk(arg, ";:+@%");
650 case ':': /* Line number */
651 pp->line = strtoul(arg, &tmp, 0);
653 semantic_error("There is non-digit char"
654 " in line number.\n");
658 case '+': /* Byte offset from a symbol */
659 pp->offset = strtoul(arg, &tmp, 0);
661 semantic_error("There is non-digit character"
666 case '@': /* File name */
668 semantic_error("SRC@SRC is not allowed.\n");
671 pp->file = strdup(arg);
672 if (pp->file == NULL)
675 case '%': /* Probe places */
676 if (strcmp(arg, "return") == 0) {
678 } else { /* Others not supported yet */
679 semantic_error("%%%s is not supported.\n", arg);
683 default: /* Buggy case */
684 pr_err("This program has a bug at %s:%d.\n",
691 /* Exclusion check */
692 if (pp->lazy_line && pp->line) {
693 semantic_error("Lazy pattern can't be used with line number.");
697 if (pp->lazy_line && pp->offset) {
698 semantic_error("Lazy pattern can't be used with offset.");
702 if (pp->line && pp->offset) {
703 semantic_error("Offset can't be used with line number.");
707 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
708 semantic_error("File always requires line number or "
713 if (pp->offset && !pp->function) {
714 semantic_error("Offset requires an entry function.");
718 if (pp->retprobe && !pp->function) {
719 semantic_error("Return probe requires an entry function.");
723 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
724 semantic_error("Offset/Line/Lazy pattern can't be used with "
729 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
730 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
735 /* Parse perf-probe event argument */
736 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
738 char *tmp, *goodname;
739 struct perf_probe_arg_field **fieldp;
741 pr_debug("parsing arg: %s into ", str);
743 tmp = strchr(str, '=');
745 arg->name = strndup(str, tmp - str);
746 if (arg->name == NULL)
748 pr_debug("name:%s ", arg->name);
752 tmp = strchr(str, ':');
753 if (tmp) { /* Type setting */
755 arg->type = strdup(tmp + 1);
756 if (arg->type == NULL)
758 pr_debug("type:%s ", arg->type);
761 tmp = strpbrk(str, "-.[");
762 if (!is_c_varname(str) || !tmp) {
763 /* A variable, register, symbol or special value */
764 arg->var = strdup(str);
765 if (arg->var == NULL)
767 pr_debug("%s\n", arg->var);
771 /* Structure fields or array element */
772 arg->var = strndup(str, tmp - str);
773 if (arg->var == NULL)
776 pr_debug("%s, ", arg->var);
777 fieldp = &arg->field;
780 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
783 if (*tmp == '[') { /* Array */
785 (*fieldp)->index = strtol(str + 1, &tmp, 0);
786 (*fieldp)->ref = true;
787 if (*tmp != ']' || tmp == str + 1) {
788 semantic_error("Array index must be a"
795 } else { /* Structure */
798 (*fieldp)->ref = false;
799 } else if (tmp[1] == '>') {
801 (*fieldp)->ref = true;
803 semantic_error("Argument parse error: %s\n",
807 tmp = strpbrk(str, "-.[");
810 (*fieldp)->name = strndup(str, tmp - str);
811 if ((*fieldp)->name == NULL)
814 goodname = (*fieldp)->name;
815 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
816 fieldp = &(*fieldp)->next;
819 (*fieldp)->name = strdup(str);
820 if ((*fieldp)->name == NULL)
823 goodname = (*fieldp)->name;
824 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
826 /* If no name is specified, set the last field name (not array index)*/
828 arg->name = strdup(goodname);
829 if (arg->name == NULL)
835 /* Parse perf-probe event command */
836 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
839 int argc, i, ret = 0;
841 argv = argv_split(cmd, &argc);
843 pr_debug("Failed to split arguments.\n");
846 if (argc - 1 > MAX_PROBE_ARGS) {
847 semantic_error("Too many probe arguments (%d).\n", argc - 1);
851 /* Parse probe point */
852 ret = parse_perf_probe_point(argv[0], pev);
856 /* Copy arguments and ensure return probe has no C argument */
857 pev->nargs = argc - 1;
858 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
859 if (pev->args == NULL) {
863 for (i = 0; i < pev->nargs && ret >= 0; i++) {
864 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
866 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
867 semantic_error("You can't specify local variable for"
878 /* Return true if this perf_probe_event requires debuginfo */
879 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
883 if (pev->point.file || pev->point.line || pev->point.lazy_line)
886 for (i = 0; i < pev->nargs; i++)
887 if (is_c_varname(pev->args[i].var))
893 /* Parse probe_events event into struct probe_point */
894 static int parse_probe_trace_command(const char *cmd,
895 struct probe_trace_event *tev)
897 struct probe_trace_point *tp = &tev->point;
903 pr_debug("Parsing probe_events: %s\n", cmd);
904 argv = argv_split(cmd, &argc);
906 pr_debug("Failed to split arguments.\n");
910 semantic_error("Too few probe arguments.\n");
915 /* Scan event and group name. */
916 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
917 &pr, (float *)(void *)&tev->group,
918 (float *)(void *)&tev->event);
920 semantic_error("Failed to parse event name: %s\n", argv[0]);
924 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
926 tp->retprobe = (pr == 'r');
928 /* Scan function name and offset */
929 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
934 tev->nargs = argc - 2;
935 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
936 if (tev->args == NULL) {
940 for (i = 0; i < tev->nargs; i++) {
941 p = strchr(argv[i + 2], '=');
942 if (p) /* We don't need which register is assigned. */
946 tev->args[i].name = strdup(argv[i + 2]);
947 /* TODO: parse regs and offset */
948 tev->args[i].value = strdup(p);
949 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
960 /* Compose only probe arg */
961 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
963 struct perf_probe_arg_field *field = pa->field;
967 if (pa->name && pa->var)
968 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
970 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
977 if (field->name[0] == '[')
978 ret = e_snprintf(tmp, len, "%s", field->name);
980 ret = e_snprintf(tmp, len, "%s%s",
981 field->ref ? "->" : ".", field->name);
990 ret = e_snprintf(tmp, len, ":%s", pa->type);
999 pr_debug("Failed to synthesize perf probe argument: %s",
1004 /* Compose only probe point (not argument) */
1005 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1008 char offs[32] = "", line[32] = "", file[32] = "";
1011 buf = zalloc(MAX_CMDLEN);
1017 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1022 ret = e_snprintf(line, 32, ":%d", pp->line);
1027 len = strlen(pp->file) - 31;
1030 tmp = strchr(pp->file + len, '/');
1032 tmp = pp->file + len;
1033 ret = e_snprintf(file, 32, "@%s", tmp + 1);
1039 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1040 offs, pp->retprobe ? "%return" : "", line,
1043 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1049 pr_debug("Failed to synthesize perf probe point: %s",
1057 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1062 buf = synthesize_perf_probe_point(&pev->point);
1067 for (i = 0; i < pev->nargs; i++) {
1068 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1081 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1082 char **buf, size_t *buflen,
1087 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1093 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1105 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1106 char *buf, size_t buflen)
1108 struct probe_trace_arg_ref *ref = arg->ref;
1112 /* Argument name or separator */
1114 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1116 ret = e_snprintf(buf, buflen, " ");
1122 /* Special case: @XXX */
1123 if (arg->value[0] == '@' && arg->ref)
1126 /* Dereferencing arguments */
1128 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1134 /* Print argument value */
1135 if (arg->value[0] == '@' && arg->ref)
1136 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1139 ret = e_snprintf(buf, buflen, "%s", arg->value);
1147 ret = e_snprintf(buf, buflen, ")");
1153 /* Print argument type */
1155 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1164 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1166 struct probe_trace_point *tp = &tev->point;
1170 buf = zalloc(MAX_CMDLEN);
1174 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1175 tp->retprobe ? 'r' : 'p',
1176 tev->group, tev->event,
1177 tp->symbol, tp->offset);
1181 for (i = 0; i < tev->nargs; i++) {
1182 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1195 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1196 struct perf_probe_event *pev)
1201 /* Convert event/group name */
1202 pev->event = strdup(tev->event);
1203 pev->group = strdup(tev->group);
1204 if (pev->event == NULL || pev->group == NULL)
1207 /* Convert trace_point to probe_point */
1208 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1212 /* Convert trace_arg to probe_arg */
1213 pev->nargs = tev->nargs;
1214 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1215 if (pev->args == NULL)
1217 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1218 if (tev->args[i].name)
1219 pev->args[i].name = strdup(tev->args[i].name);
1221 ret = synthesize_probe_trace_arg(&tev->args[i],
1223 pev->args[i].name = strdup(buf);
1225 if (pev->args[i].name == NULL && ret >= 0)
1230 clear_perf_probe_event(pev);
1235 void clear_perf_probe_event(struct perf_probe_event *pev)
1237 struct perf_probe_point *pp = &pev->point;
1238 struct perf_probe_arg_field *field, *next;
1250 free(pp->lazy_line);
1251 for (i = 0; i < pev->nargs; i++) {
1252 if (pev->args[i].name)
1253 free(pev->args[i].name);
1254 if (pev->args[i].var)
1255 free(pev->args[i].var);
1256 if (pev->args[i].type)
1257 free(pev->args[i].type);
1258 field = pev->args[i].field;
1269 memset(pev, 0, sizeof(*pev));
1272 static void clear_probe_trace_event(struct probe_trace_event *tev)
1274 struct probe_trace_arg_ref *ref, *next;
1281 if (tev->point.symbol)
1282 free(tev->point.symbol);
1283 for (i = 0; i < tev->nargs; i++) {
1284 if (tev->args[i].name)
1285 free(tev->args[i].name);
1286 if (tev->args[i].value)
1287 free(tev->args[i].value);
1288 if (tev->args[i].type)
1289 free(tev->args[i].type);
1290 ref = tev->args[i].ref;
1299 memset(tev, 0, sizeof(*tev));
1302 static int open_kprobe_events(bool readwrite)
1305 const char *__debugfs;
1308 __debugfs = debugfs_find_mountpoint();
1309 if (__debugfs == NULL) {
1310 pr_warning("Debugfs is not mounted.\n");
1314 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
1316 pr_debug("Opening %s write=%d\n", buf, readwrite);
1317 if (readwrite && !probe_event_dry_run)
1318 ret = open(buf, O_RDWR, O_APPEND);
1320 ret = open(buf, O_RDONLY, 0);
1324 if (errno == ENOENT)
1325 pr_warning("kprobe_events file does not exist - please"
1326 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1328 pr_warning("Failed to open kprobe_events file: %s\n",
1334 /* Get raw string list of current kprobe_events */
1335 static struct strlist *get_probe_trace_command_rawlist(int fd)
1339 char buf[MAX_CMDLEN];
1343 sl = strlist__new(true, NULL);
1345 fp = fdopen(dup(fd), "r");
1347 p = fgets(buf, MAX_CMDLEN, fp);
1351 idx = strlen(p) - 1;
1354 ret = strlist__add(sl, buf);
1356 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1357 strlist__delete(sl);
1367 static int show_perf_probe_event(struct perf_probe_event *pev)
1373 /* Synthesize only event probe point */
1374 place = synthesize_perf_probe_point(&pev->point);
1378 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
1382 printf(" %-20s (on %s", buf, place);
1384 if (pev->nargs > 0) {
1386 for (i = 0; i < pev->nargs; i++) {
1387 ret = synthesize_perf_probe_arg(&pev->args[i],
1399 /* List up current perf-probe events */
1400 int show_perf_probe_events(void)
1403 struct probe_trace_event tev;
1404 struct perf_probe_event pev;
1405 struct strlist *rawlist;
1406 struct str_node *ent;
1409 ret = init_vmlinux();
1413 memset(&tev, 0, sizeof(tev));
1414 memset(&pev, 0, sizeof(pev));
1416 fd = open_kprobe_events(false);
1420 rawlist = get_probe_trace_command_rawlist(fd);
1425 strlist__for_each(ent, rawlist) {
1426 ret = parse_probe_trace_command(ent->s, &tev);
1428 ret = convert_to_perf_probe_event(&tev, &pev);
1430 ret = show_perf_probe_event(&pev);
1432 clear_perf_probe_event(&pev);
1433 clear_probe_trace_event(&tev);
1437 strlist__delete(rawlist);
1442 /* Get current perf-probe event names */
1443 static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
1446 struct strlist *sl, *rawlist;
1447 struct str_node *ent;
1448 struct probe_trace_event tev;
1451 memset(&tev, 0, sizeof(tev));
1452 rawlist = get_probe_trace_command_rawlist(fd);
1453 sl = strlist__new(true, NULL);
1454 strlist__for_each(ent, rawlist) {
1455 ret = parse_probe_trace_command(ent->s, &tev);
1458 if (include_group) {
1459 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1462 ret = strlist__add(sl, buf);
1464 ret = strlist__add(sl, tev.event);
1465 clear_probe_trace_event(&tev);
1469 strlist__delete(rawlist);
1472 strlist__delete(sl);
1478 static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
1481 char *buf = synthesize_probe_trace_command(tev);
1484 pr_debug("Failed to synthesize probe trace event.\n");
1488 pr_debug("Writing event: %s\n", buf);
1489 if (!probe_event_dry_run) {
1490 ret = write(fd, buf, strlen(buf));
1492 pr_warning("Failed to write event: %s\n",
1499 static int get_new_event_name(char *buf, size_t len, const char *base,
1500 struct strlist *namelist, bool allow_suffix)
1505 ret = e_snprintf(buf, len, "%s", base);
1507 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1510 if (!strlist__has_entry(namelist, buf))
1513 if (!allow_suffix) {
1514 pr_warning("Error: event \"%s\" already exists. "
1515 "(Use -f to force duplicates.)\n", base);
1519 /* Try to add suffix */
1520 for (i = 1; i < MAX_EVENT_INDEX; i++) {
1521 ret = e_snprintf(buf, len, "%s_%d", base, i);
1523 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1526 if (!strlist__has_entry(namelist, buf))
1529 if (i == MAX_EVENT_INDEX) {
1530 pr_warning("Too many events are on the same function.\n");
1537 static int __add_probe_trace_events(struct perf_probe_event *pev,
1538 struct probe_trace_event *tevs,
1539 int ntevs, bool allow_suffix)
1542 struct probe_trace_event *tev = NULL;
1544 const char *event, *group;
1545 struct strlist *namelist;
1547 fd = open_kprobe_events(true);
1550 /* Get current event names */
1551 namelist = get_probe_trace_event_names(fd, false);
1553 pr_debug("Failed to get current event list.\n");
1558 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
1559 for (i = 0; i < ntevs; i++) {
1564 if (pev->point.function)
1565 event = pev->point.function;
1567 event = tev->point.symbol;
1571 group = PERFPROBE_GROUP;
1573 /* Get an unused new event name */
1574 ret = get_new_event_name(buf, 64, event,
1575 namelist, allow_suffix);
1580 tev->event = strdup(event);
1581 tev->group = strdup(group);
1582 if (tev->event == NULL || tev->group == NULL) {
1586 ret = write_probe_trace_event(fd, tev);
1589 /* Add added event name to namelist */
1590 strlist__add(namelist, event);
1592 /* Trick here - save current event/group */
1595 pev->event = tev->event;
1596 pev->group = tev->group;
1597 show_perf_probe_event(pev);
1598 /* Trick here - restore current event/group */
1599 pev->event = (char *)event;
1600 pev->group = (char *)group;
1603 * Probes after the first probe which comes from same
1604 * user input are always allowed to add suffix, because
1605 * there might be several addresses corresponding to
1608 allow_suffix = true;
1612 /* Show how to use the event. */
1613 printf("\nYou can now use it on all perf tools, such as:\n\n");
1614 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1618 strlist__delete(namelist);
1623 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1624 struct probe_trace_event **tevs,
1625 int max_tevs, const char *module)
1629 struct probe_trace_event *tev;
1631 /* Convert perf_probe_event with debuginfo */
1632 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
1636 /* Allocate trace event buffer */
1637 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
1641 /* Copy parameters */
1642 tev->point.symbol = strdup(pev->point.function);
1643 if (tev->point.symbol == NULL) {
1647 tev->point.offset = pev->point.offset;
1648 tev->point.retprobe = pev->point.retprobe;
1649 tev->nargs = pev->nargs;
1651 tev->args = zalloc(sizeof(struct probe_trace_arg)
1653 if (tev->args == NULL) {
1657 for (i = 0; i < tev->nargs; i++) {
1658 if (pev->args[i].name) {
1659 tev->args[i].name = strdup(pev->args[i].name);
1660 if (tev->args[i].name == NULL) {
1665 tev->args[i].value = strdup(pev->args[i].var);
1666 if (tev->args[i].value == NULL) {
1670 if (pev->args[i].type) {
1671 tev->args[i].type = strdup(pev->args[i].type);
1672 if (tev->args[i].type == NULL) {
1680 /* Currently just checking function name from symbol map */
1681 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
1683 pr_warning("Kernel symbol \'%s\' not found.\n",
1691 clear_probe_trace_event(tev);
1697 struct __event_package {
1698 struct perf_probe_event *pev;
1699 struct probe_trace_event *tevs;
1703 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1704 int max_tevs, const char *module, bool force_add)
1707 struct __event_package *pkgs;
1709 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1713 /* Init vmlinux path */
1714 ret = init_vmlinux();
1720 /* Loop 1: convert all events */
1721 for (i = 0; i < npevs; i++) {
1722 pkgs[i].pev = &pevs[i];
1723 /* Convert with or without debuginfo */
1724 ret = convert_to_probe_trace_events(pkgs[i].pev,
1730 pkgs[i].ntevs = ret;
1733 /* Loop 2: add all events */
1734 for (i = 0; i < npevs && ret >= 0; i++)
1735 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1736 pkgs[i].ntevs, force_add);
1738 /* Loop 3: cleanup and free trace events */
1739 for (i = 0; i < npevs; i++) {
1740 for (j = 0; j < pkgs[i].ntevs; j++)
1741 clear_probe_trace_event(&pkgs[i].tevs[j]);
1749 static int __del_trace_probe_event(int fd, struct str_node *ent)
1755 /* Convert from perf-probe event to trace-probe event */
1756 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1760 p = strchr(buf + 2, ':');
1762 pr_debug("Internal error: %s should have ':' but not.\n",
1769 pr_debug("Writing event: %s\n", buf);
1770 ret = write(fd, buf, strlen(buf));
1774 printf("Remove event: %s\n", ent->s);
1777 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1781 static int del_trace_probe_event(int fd, const char *group,
1782 const char *event, struct strlist *namelist)
1785 struct str_node *ent, *n;
1786 int found = 0, ret = 0;
1788 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1790 pr_err("Failed to copy event.");
1794 if (strpbrk(buf, "*?")) { /* Glob-exp */
1795 strlist__for_each_safe(ent, n, namelist)
1796 if (strglobmatch(ent->s, buf)) {
1798 ret = __del_trace_probe_event(fd, ent);
1801 strlist__remove(namelist, ent);
1804 ent = strlist__find(namelist, buf);
1807 ret = __del_trace_probe_event(fd, ent);
1809 strlist__remove(namelist, ent);
1812 if (found == 0 && ret >= 0)
1813 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1818 int del_perf_probe_events(struct strlist *dellist)
1821 const char *group, *event;
1823 struct str_node *ent;
1824 struct strlist *namelist;
1826 fd = open_kprobe_events(true);
1830 /* Get current event names */
1831 namelist = get_probe_trace_event_names(fd, true);
1832 if (namelist == NULL)
1835 strlist__for_each(ent, dellist) {
1836 str = strdup(ent->s);
1841 pr_debug("Parsing: %s\n", str);
1842 p = strchr(str, ':');
1851 pr_debug("Group: %s, Event: %s\n", group, event);
1852 ret = del_trace_probe_event(fd, group, event, namelist);
1857 strlist__delete(namelist);