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() failed.\n");
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.\n",
147 pr_debug("Try to open %s\n", path);
148 return open(path, O_RDONLY);
152 * Convert trace point to probe point with debuginfo
153 * Currently only handles kprobes.
155 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
156 struct perf_probe_point *pp)
163 sym = __find_kernel_function_by_name(tp->symbol, &map);
165 addr = map->unmap_ip(map, sym->start + tp->offset);
166 pr_debug("try to find %s+%ld@%llx\n", tp->symbol,
168 ret = find_perf_probe_point((unsigned long)addr, pp);
171 pr_debug("Failed to find corresponding probes from "
172 "debuginfo. Use kprobe event information.\n");
173 pp->function = strdup(tp->symbol);
174 if (pp->function == NULL)
176 pp->offset = tp->offset;
178 pp->retprobe = tp->retprobe;
183 /* Try to find perf_probe_event with debuginfo */
184 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
185 struct probe_trace_event **tevs,
186 int max_tevs, const char *module)
188 bool need_dwarf = perf_probe_event_need_dwarf(pev);
191 fd = open_vmlinux(module);
194 pr_warning("Failed to open debuginfo file.\n");
197 pr_debug("Could not open vmlinux. Try to use symbols.\n");
201 /* Searching trace events corresponding to probe event */
202 ntevs = find_probe_trace_events(fd, pev, tevs, max_tevs);
205 if (ntevs > 0) { /* Succeeded to find trace events */
206 pr_debug("find %d probe_trace_events.\n", ntevs);
210 if (ntevs == 0) { /* No error but failed to find probe point. */
211 pr_warning("Probe point '%s' not found.\n",
212 synthesize_perf_probe_point(&pev->point));
215 /* Error path : ntevs < 0 */
216 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
217 if (ntevs == -EBADF) {
218 pr_warning("Warning: No dwarf info found in the vmlinux - "
219 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
221 pr_debug("Trying to use symbols.\n");
229 * Find a src file from a DWARF tag path. Prepend optional source path prefix
230 * and chop off leading directories that do not exist. Result is passed back as
231 * a newly allocated path on success.
232 * Return 0 if file was found and readable, -errno otherwise.
234 static int get_real_path(const char *raw_path, const char *comp_dir,
237 const char *prefix = symbol_conf.source_prefix;
240 if (raw_path[0] != '/' && comp_dir)
241 /* If not an absolute path, try to use comp_dir */
244 if (access(raw_path, R_OK) == 0) {
245 *new_path = strdup(raw_path);
252 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
257 sprintf(*new_path, "%s/%s", prefix, raw_path);
259 if (access(*new_path, R_OK) == 0)
262 if (!symbol_conf.source_prefix)
263 /* In case of searching comp_dir, don't retry */
271 raw_path = strchr(++raw_path, '/');
287 #define LINEBUF_SIZE 256
288 #define NR_ADDITIONAL_LINES 2
290 static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
292 char buf[LINEBUF_SIZE];
293 const char *color = show_num ? "" : PERF_COLOR_BLUE;
294 const char *prefix = NULL;
297 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
302 prefix = show_num ? "%7d " : " ";
303 color_fprintf(stdout, color, prefix, l);
305 color_fprintf(stdout, color, "%s", buf);
307 } while (strchr(buf, '\n') == NULL);
312 pr_warning("Source file is shorter than expected.\n");
314 pr_warning("File read error: %s\n", strerror(errno));
320 * Show line-range always requires debuginfo to find source file and
323 int show_line_range(struct line_range *lr, const char *module)
326 struct line_node *ln;
331 /* Search a line range */
332 ret = init_vmlinux();
336 fd = open_vmlinux(module);
338 pr_warning("Failed to open debuginfo file.\n");
342 ret = find_line_range(fd, lr);
345 pr_warning("Specified source line is not found.\n");
347 } else if (ret < 0) {
348 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
352 /* Convert source file path */
354 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
355 free(tmp); /* Free old path */
357 pr_warning("Failed to find source file. (%d)\n", ret);
364 fprintf(stdout, "<%s:%d>\n", lr->function,
365 lr->start - lr->offset);
367 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
369 fp = fopen(lr->path, "r");
371 pr_warning("Failed to open %s: %s\n", lr->path,
375 /* Skip to starting line number */
376 while (l < lr->start) {
377 ret = show_one_line(fp, l++, true, false);
382 list_for_each_entry(ln, &lr->line_list, list) {
383 for (; ln->line > l; l++) {
384 ret = show_one_line(fp, l - lr->offset, false, false);
388 ret = show_one_line(fp, l++ - lr->offset, false, true);
393 if (lr->end == INT_MAX)
394 lr->end = l + NR_ADDITIONAL_LINES;
395 while (l <= lr->end && !feof(fp)) {
396 ret = show_one_line(fp, l++ - lr->offset, false, false);
405 static int show_available_vars_at(int fd, struct perf_probe_event *pev,
406 int max_vls, bool externs)
410 struct str_node *node;
411 struct variable_list *vls = NULL, *vl;
413 buf = synthesize_perf_probe_point(&pev->point);
416 pr_debug("Searching variables at %s\n", buf);
418 ret = find_available_vars_at(fd, pev, &vls, max_vls, externs);
420 /* Some variables were found */
421 fprintf(stdout, "Available variables at %s\n", buf);
422 for (i = 0; i < ret; i++) {
425 * A probe point might be converted to
426 * several trace points.
428 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
430 free(vl->point.symbol);
432 strlist__for_each(node, vl->vars)
433 fprintf(stdout, "\t\t%s\n", node->s);
434 strlist__delete(vl->vars);
436 fprintf(stdout, "(No variables)\n");
440 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
446 /* Show available variables on given probe point */
447 int show_available_vars(struct perf_probe_event *pevs, int npevs,
448 int max_vls, const char *module, bool externs)
452 ret = init_vmlinux();
456 fd = open_vmlinux(module);
458 pr_warning("Failed to open debug information file.\n");
464 for (i = 0; i < npevs && ret >= 0; i++)
465 ret = show_available_vars_at(fd, &pevs[i], max_vls, externs);
471 #else /* !DWARF_SUPPORT */
473 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
474 struct perf_probe_point *pp)
478 sym = __find_kernel_function_by_name(tp->symbol, NULL);
480 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
483 pp->function = strdup(tp->symbol);
484 if (pp->function == NULL)
486 pp->offset = tp->offset;
487 pp->retprobe = tp->retprobe;
492 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
493 struct probe_trace_event **tevs __unused,
494 int max_tevs __unused, const char *mod __unused)
496 if (perf_probe_event_need_dwarf(pev)) {
497 pr_warning("Debuginfo-analysis is not supported.\n");
503 int show_line_range(struct line_range *lr __unused, const char *module __unused)
505 pr_warning("Debuginfo-analysis is not supported.\n");
509 int show_available_vars(struct perf_probe_event *pevs __unused,
510 int npevs __unused, int max_vls __unused,
511 const char *module __unused, bool externs __unused)
513 pr_warning("Debuginfo-analysis is not supported.\n");
518 int parse_line_range_desc(const char *arg, struct line_range *lr)
525 * FUNC[:SLN[+NUM|-ELN]]
527 ptr = strchr(arg, ':');
529 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
531 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
533 * Adjust the number of lines here.
534 * If the number of lines == 1, the
535 * the end of line should be equal to
538 } else if (*tmp == '-')
539 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
542 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
543 if (lr->start > lr->end) {
544 semantic_error("Start line must be smaller"
545 " than end line.\n");
549 semantic_error("Tailing with invalid character '%d'.\n",
553 tmp = strndup(arg, (ptr - arg));
562 if (strchr(tmp, '.'))
570 /* Check the name is good for event/group */
571 static bool check_event_name(const char *name)
573 if (!isalpha(*name) && *name != '_')
575 while (*++name != '\0') {
576 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
582 /* Parse probepoint definition. */
583 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
585 struct perf_probe_point *pp = &pev->point;
590 * perf probe [EVENT=]SRC[:LN|;PTN]
591 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
593 * TODO:Group name support
596 ptr = strpbrk(arg, ";=@+%");
597 if (ptr && *ptr == '=') { /* Event name */
600 if (strchr(arg, ':')) {
601 semantic_error("Group name is not supported yet.\n");
604 if (!check_event_name(arg)) {
605 semantic_error("%s is bad for event name -it must "
606 "follow C symbol-naming rule.\n", arg);
609 pev->event = strdup(arg);
610 if (pev->event == NULL)
616 ptr = strpbrk(arg, ";:+@%");
626 /* Check arg is function or file and copy it */
627 if (strchr(tmp, '.')) /* File */
632 /* Parse other options */
636 if (c == ';') { /* Lazy pattern must be the last part */
637 pp->lazy_line = strdup(arg);
638 if (pp->lazy_line == NULL)
642 ptr = strpbrk(arg, ";:+@%");
648 case ':': /* Line number */
649 pp->line = strtoul(arg, &tmp, 0);
651 semantic_error("There is non-digit char"
652 " in line number.\n");
656 case '+': /* Byte offset from a symbol */
657 pp->offset = strtoul(arg, &tmp, 0);
659 semantic_error("There is non-digit character"
664 case '@': /* File name */
666 semantic_error("SRC@SRC is not allowed.\n");
669 pp->file = strdup(arg);
670 if (pp->file == NULL)
673 case '%': /* Probe places */
674 if (strcmp(arg, "return") == 0) {
676 } else { /* Others not supported yet */
677 semantic_error("%%%s is not supported.\n", arg);
681 default: /* Buggy case */
682 pr_err("This program has a bug at %s:%d.\n",
689 /* Exclusion check */
690 if (pp->lazy_line && pp->line) {
691 semantic_error("Lazy pattern can't be used with"
696 if (pp->lazy_line && pp->offset) {
697 semantic_error("Lazy pattern can't be used with offset.\n");
701 if (pp->line && pp->offset) {
702 semantic_error("Offset can't be used with line number.\n");
706 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
707 semantic_error("File always requires line number or "
712 if (pp->offset && !pp->function) {
713 semantic_error("Offset requires an entry function.\n");
717 if (pp->retprobe && !pp->function) {
718 semantic_error("Return probe requires an entry function.\n");
722 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
723 semantic_error("Offset/Line/Lazy pattern can't be used with "
728 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
729 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
734 /* Parse perf-probe event argument */
735 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
737 char *tmp, *goodname;
738 struct perf_probe_arg_field **fieldp;
740 pr_debug("parsing arg: %s into ", str);
742 tmp = strchr(str, '=');
744 arg->name = strndup(str, tmp - str);
745 if (arg->name == NULL)
747 pr_debug("name:%s ", arg->name);
751 tmp = strchr(str, ':');
752 if (tmp) { /* Type setting */
754 arg->type = strdup(tmp + 1);
755 if (arg->type == NULL)
757 pr_debug("type:%s ", arg->type);
760 tmp = strpbrk(str, "-.[");
761 if (!is_c_varname(str) || !tmp) {
762 /* A variable, register, symbol or special value */
763 arg->var = strdup(str);
764 if (arg->var == NULL)
766 pr_debug("%s\n", arg->var);
770 /* Structure fields or array element */
771 arg->var = strndup(str, tmp - str);
772 if (arg->var == NULL)
775 pr_debug("%s, ", arg->var);
776 fieldp = &arg->field;
779 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
782 if (*tmp == '[') { /* Array */
784 (*fieldp)->index = strtol(str + 1, &tmp, 0);
785 (*fieldp)->ref = true;
786 if (*tmp != ']' || tmp == str + 1) {
787 semantic_error("Array index must be a"
794 } else { /* Structure */
797 (*fieldp)->ref = false;
798 } else if (tmp[1] == '>') {
800 (*fieldp)->ref = true;
802 semantic_error("Argument parse error: %s\n",
806 tmp = strpbrk(str, "-.[");
809 (*fieldp)->name = strndup(str, tmp - str);
810 if ((*fieldp)->name == NULL)
813 goodname = (*fieldp)->name;
814 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
815 fieldp = &(*fieldp)->next;
818 (*fieldp)->name = strdup(str);
819 if ((*fieldp)->name == NULL)
822 goodname = (*fieldp)->name;
823 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
825 /* If no name is specified, set the last field name (not array index)*/
827 arg->name = strdup(goodname);
828 if (arg->name == NULL)
834 /* Parse perf-probe event command */
835 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
838 int argc, i, ret = 0;
840 argv = argv_split(cmd, &argc);
842 pr_debug("Failed to split arguments.\n");
845 if (argc - 1 > MAX_PROBE_ARGS) {
846 semantic_error("Too many probe arguments (%d).\n", argc - 1);
850 /* Parse probe point */
851 ret = parse_perf_probe_point(argv[0], pev);
855 /* Copy arguments and ensure return probe has no C argument */
856 pev->nargs = argc - 1;
857 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
858 if (pev->args == NULL) {
862 for (i = 0; i < pev->nargs && ret >= 0; i++) {
863 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
865 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
866 semantic_error("You can't specify local variable for"
877 /* Return true if this perf_probe_event requires debuginfo */
878 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
882 if (pev->point.file || pev->point.line || pev->point.lazy_line)
885 for (i = 0; i < pev->nargs; i++)
886 if (is_c_varname(pev->args[i].var))
892 /* Parse probe_events event into struct probe_point */
893 static int parse_probe_trace_command(const char *cmd,
894 struct probe_trace_event *tev)
896 struct probe_trace_point *tp = &tev->point;
902 pr_debug("Parsing probe_events: %s\n", cmd);
903 argv = argv_split(cmd, &argc);
905 pr_debug("Failed to split arguments.\n");
909 semantic_error("Too few probe arguments.\n");
914 /* Scan event and group name. */
915 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
916 &pr, (float *)(void *)&tev->group,
917 (float *)(void *)&tev->event);
919 semantic_error("Failed to parse event name: %s\n", argv[0]);
923 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
925 tp->retprobe = (pr == 'r');
927 /* Scan function name and offset */
928 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
933 tev->nargs = argc - 2;
934 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
935 if (tev->args == NULL) {
939 for (i = 0; i < tev->nargs; i++) {
940 p = strchr(argv[i + 2], '=');
941 if (p) /* We don't need which register is assigned. */
945 tev->args[i].name = strdup(argv[i + 2]);
946 /* TODO: parse regs and offset */
947 tev->args[i].value = strdup(p);
948 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
959 /* Compose only probe arg */
960 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
962 struct perf_probe_arg_field *field = pa->field;
966 if (pa->name && pa->var)
967 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
969 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
976 if (field->name[0] == '[')
977 ret = e_snprintf(tmp, len, "%s", field->name);
979 ret = e_snprintf(tmp, len, "%s%s",
980 field->ref ? "->" : ".", field->name);
989 ret = e_snprintf(tmp, len, ":%s", pa->type);
998 pr_debug("Failed to synthesize perf probe argument: %s\n",
1003 /* Compose only probe point (not argument) */
1004 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1007 char offs[32] = "", line[32] = "", file[32] = "";
1010 buf = zalloc(MAX_CMDLEN);
1016 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1021 ret = e_snprintf(line, 32, ":%d", pp->line);
1026 len = strlen(pp->file) - 31;
1029 tmp = strchr(pp->file + len, '/');
1031 tmp = pp->file + len;
1032 ret = e_snprintf(file, 32, "@%s", tmp + 1);
1038 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1039 offs, pp->retprobe ? "%return" : "", line,
1042 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1048 pr_debug("Failed to synthesize perf probe point: %s\n",
1056 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1061 buf = synthesize_perf_probe_point(&pev->point);
1066 for (i = 0; i < pev->nargs; i++) {
1067 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1080 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1081 char **buf, size_t *buflen,
1086 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1092 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1104 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1105 char *buf, size_t buflen)
1107 struct probe_trace_arg_ref *ref = arg->ref;
1111 /* Argument name or separator */
1113 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1115 ret = e_snprintf(buf, buflen, " ");
1121 /* Special case: @XXX */
1122 if (arg->value[0] == '@' && arg->ref)
1125 /* Dereferencing arguments */
1127 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1133 /* Print argument value */
1134 if (arg->value[0] == '@' && arg->ref)
1135 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1138 ret = e_snprintf(buf, buflen, "%s", arg->value);
1146 ret = e_snprintf(buf, buflen, ")");
1152 /* Print argument type */
1154 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1163 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1165 struct probe_trace_point *tp = &tev->point;
1169 buf = zalloc(MAX_CMDLEN);
1173 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1174 tp->retprobe ? 'r' : 'p',
1175 tev->group, tev->event,
1176 tp->symbol, tp->offset);
1180 for (i = 0; i < tev->nargs; i++) {
1181 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1194 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1195 struct perf_probe_event *pev)
1200 /* Convert event/group name */
1201 pev->event = strdup(tev->event);
1202 pev->group = strdup(tev->group);
1203 if (pev->event == NULL || pev->group == NULL)
1206 /* Convert trace_point to probe_point */
1207 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1211 /* Convert trace_arg to probe_arg */
1212 pev->nargs = tev->nargs;
1213 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1214 if (pev->args == NULL)
1216 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1217 if (tev->args[i].name)
1218 pev->args[i].name = strdup(tev->args[i].name);
1220 ret = synthesize_probe_trace_arg(&tev->args[i],
1222 pev->args[i].name = strdup(buf);
1224 if (pev->args[i].name == NULL && ret >= 0)
1229 clear_perf_probe_event(pev);
1234 void clear_perf_probe_event(struct perf_probe_event *pev)
1236 struct perf_probe_point *pp = &pev->point;
1237 struct perf_probe_arg_field *field, *next;
1249 free(pp->lazy_line);
1250 for (i = 0; i < pev->nargs; i++) {
1251 if (pev->args[i].name)
1252 free(pev->args[i].name);
1253 if (pev->args[i].var)
1254 free(pev->args[i].var);
1255 if (pev->args[i].type)
1256 free(pev->args[i].type);
1257 field = pev->args[i].field;
1268 memset(pev, 0, sizeof(*pev));
1271 static void clear_probe_trace_event(struct probe_trace_event *tev)
1273 struct probe_trace_arg_ref *ref, *next;
1280 if (tev->point.symbol)
1281 free(tev->point.symbol);
1282 for (i = 0; i < tev->nargs; i++) {
1283 if (tev->args[i].name)
1284 free(tev->args[i].name);
1285 if (tev->args[i].value)
1286 free(tev->args[i].value);
1287 if (tev->args[i].type)
1288 free(tev->args[i].type);
1289 ref = tev->args[i].ref;
1298 memset(tev, 0, sizeof(*tev));
1301 static int open_kprobe_events(bool readwrite)
1304 const char *__debugfs;
1307 __debugfs = debugfs_find_mountpoint();
1308 if (__debugfs == NULL) {
1309 pr_warning("Debugfs is not mounted.\n");
1313 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
1315 pr_debug("Opening %s write=%d\n", buf, readwrite);
1316 if (readwrite && !probe_event_dry_run)
1317 ret = open(buf, O_RDWR, O_APPEND);
1319 ret = open(buf, O_RDONLY, 0);
1323 if (errno == ENOENT)
1324 pr_warning("kprobe_events file does not exist - please"
1325 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1327 pr_warning("Failed to open kprobe_events file: %s\n",
1333 /* Get raw string list of current kprobe_events */
1334 static struct strlist *get_probe_trace_command_rawlist(int fd)
1338 char buf[MAX_CMDLEN];
1342 sl = strlist__new(true, NULL);
1344 fp = fdopen(dup(fd), "r");
1346 p = fgets(buf, MAX_CMDLEN, fp);
1350 idx = strlen(p) - 1;
1353 ret = strlist__add(sl, buf);
1355 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1356 strlist__delete(sl);
1366 static int show_perf_probe_event(struct perf_probe_event *pev)
1372 /* Synthesize only event probe point */
1373 place = synthesize_perf_probe_point(&pev->point);
1377 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
1381 printf(" %-20s (on %s", buf, place);
1383 if (pev->nargs > 0) {
1385 for (i = 0; i < pev->nargs; i++) {
1386 ret = synthesize_perf_probe_arg(&pev->args[i],
1398 /* List up current perf-probe events */
1399 int show_perf_probe_events(void)
1402 struct probe_trace_event tev;
1403 struct perf_probe_event pev;
1404 struct strlist *rawlist;
1405 struct str_node *ent;
1408 ret = init_vmlinux();
1412 memset(&tev, 0, sizeof(tev));
1413 memset(&pev, 0, sizeof(pev));
1415 fd = open_kprobe_events(false);
1419 rawlist = get_probe_trace_command_rawlist(fd);
1424 strlist__for_each(ent, rawlist) {
1425 ret = parse_probe_trace_command(ent->s, &tev);
1427 ret = convert_to_perf_probe_event(&tev, &pev);
1429 ret = show_perf_probe_event(&pev);
1431 clear_perf_probe_event(&pev);
1432 clear_probe_trace_event(&tev);
1436 strlist__delete(rawlist);
1441 /* Get current perf-probe event names */
1442 static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
1445 struct strlist *sl, *rawlist;
1446 struct str_node *ent;
1447 struct probe_trace_event tev;
1450 memset(&tev, 0, sizeof(tev));
1451 rawlist = get_probe_trace_command_rawlist(fd);
1452 sl = strlist__new(true, NULL);
1453 strlist__for_each(ent, rawlist) {
1454 ret = parse_probe_trace_command(ent->s, &tev);
1457 if (include_group) {
1458 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1461 ret = strlist__add(sl, buf);
1463 ret = strlist__add(sl, tev.event);
1464 clear_probe_trace_event(&tev);
1468 strlist__delete(rawlist);
1471 strlist__delete(sl);
1477 static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
1480 char *buf = synthesize_probe_trace_command(tev);
1483 pr_debug("Failed to synthesize probe trace event.\n");
1487 pr_debug("Writing event: %s\n", buf);
1488 if (!probe_event_dry_run) {
1489 ret = write(fd, buf, strlen(buf));
1491 pr_warning("Failed to write event: %s\n",
1498 static int get_new_event_name(char *buf, size_t len, const char *base,
1499 struct strlist *namelist, bool allow_suffix)
1504 ret = e_snprintf(buf, len, "%s", base);
1506 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1509 if (!strlist__has_entry(namelist, buf))
1512 if (!allow_suffix) {
1513 pr_warning("Error: event \"%s\" already exists. "
1514 "(Use -f to force duplicates.)\n", base);
1518 /* Try to add suffix */
1519 for (i = 1; i < MAX_EVENT_INDEX; i++) {
1520 ret = e_snprintf(buf, len, "%s_%d", base, i);
1522 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1525 if (!strlist__has_entry(namelist, buf))
1528 if (i == MAX_EVENT_INDEX) {
1529 pr_warning("Too many events are on the same function.\n");
1536 static int __add_probe_trace_events(struct perf_probe_event *pev,
1537 struct probe_trace_event *tevs,
1538 int ntevs, bool allow_suffix)
1541 struct probe_trace_event *tev = NULL;
1543 const char *event, *group;
1544 struct strlist *namelist;
1546 fd = open_kprobe_events(true);
1549 /* Get current event names */
1550 namelist = get_probe_trace_event_names(fd, false);
1552 pr_debug("Failed to get current event list.\n");
1557 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
1558 for (i = 0; i < ntevs; i++) {
1563 if (pev->point.function)
1564 event = pev->point.function;
1566 event = tev->point.symbol;
1570 group = PERFPROBE_GROUP;
1572 /* Get an unused new event name */
1573 ret = get_new_event_name(buf, 64, event,
1574 namelist, allow_suffix);
1579 tev->event = strdup(event);
1580 tev->group = strdup(group);
1581 if (tev->event == NULL || tev->group == NULL) {
1585 ret = write_probe_trace_event(fd, tev);
1588 /* Add added event name to namelist */
1589 strlist__add(namelist, event);
1591 /* Trick here - save current event/group */
1594 pev->event = tev->event;
1595 pev->group = tev->group;
1596 show_perf_probe_event(pev);
1597 /* Trick here - restore current event/group */
1598 pev->event = (char *)event;
1599 pev->group = (char *)group;
1602 * Probes after the first probe which comes from same
1603 * user input are always allowed to add suffix, because
1604 * there might be several addresses corresponding to
1607 allow_suffix = true;
1611 /* Show how to use the event. */
1612 printf("\nYou can now use it on all perf tools, such as:\n\n");
1613 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1617 strlist__delete(namelist);
1622 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1623 struct probe_trace_event **tevs,
1624 int max_tevs, const char *module)
1628 struct probe_trace_event *tev;
1630 /* Convert perf_probe_event with debuginfo */
1631 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
1635 /* Allocate trace event buffer */
1636 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
1640 /* Copy parameters */
1641 tev->point.symbol = strdup(pev->point.function);
1642 if (tev->point.symbol == NULL) {
1646 tev->point.offset = pev->point.offset;
1647 tev->point.retprobe = pev->point.retprobe;
1648 tev->nargs = pev->nargs;
1650 tev->args = zalloc(sizeof(struct probe_trace_arg)
1652 if (tev->args == NULL) {
1656 for (i = 0; i < tev->nargs; i++) {
1657 if (pev->args[i].name) {
1658 tev->args[i].name = strdup(pev->args[i].name);
1659 if (tev->args[i].name == NULL) {
1664 tev->args[i].value = strdup(pev->args[i].var);
1665 if (tev->args[i].value == NULL) {
1669 if (pev->args[i].type) {
1670 tev->args[i].type = strdup(pev->args[i].type);
1671 if (tev->args[i].type == NULL) {
1679 /* Currently just checking function name from symbol map */
1680 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
1682 pr_warning("Kernel symbol \'%s\' not found.\n",
1690 clear_probe_trace_event(tev);
1696 struct __event_package {
1697 struct perf_probe_event *pev;
1698 struct probe_trace_event *tevs;
1702 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1703 int max_tevs, const char *module, bool force_add)
1706 struct __event_package *pkgs;
1708 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1712 /* Init vmlinux path */
1713 ret = init_vmlinux();
1719 /* Loop 1: convert all events */
1720 for (i = 0; i < npevs; i++) {
1721 pkgs[i].pev = &pevs[i];
1722 /* Convert with or without debuginfo */
1723 ret = convert_to_probe_trace_events(pkgs[i].pev,
1729 pkgs[i].ntevs = ret;
1732 /* Loop 2: add all events */
1733 for (i = 0; i < npevs && ret >= 0; i++)
1734 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1735 pkgs[i].ntevs, force_add);
1737 /* Loop 3: cleanup and free trace events */
1738 for (i = 0; i < npevs; i++) {
1739 for (j = 0; j < pkgs[i].ntevs; j++)
1740 clear_probe_trace_event(&pkgs[i].tevs[j]);
1748 static int __del_trace_probe_event(int fd, struct str_node *ent)
1754 /* Convert from perf-probe event to trace-probe event */
1755 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1759 p = strchr(buf + 2, ':');
1761 pr_debug("Internal error: %s should have ':' but not.\n",
1768 pr_debug("Writing event: %s\n", buf);
1769 ret = write(fd, buf, strlen(buf));
1773 printf("Remove event: %s\n", ent->s);
1776 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1780 static int del_trace_probe_event(int fd, const char *group,
1781 const char *event, struct strlist *namelist)
1784 struct str_node *ent, *n;
1785 int found = 0, ret = 0;
1787 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1789 pr_err("Failed to copy event.\n");
1793 if (strpbrk(buf, "*?")) { /* Glob-exp */
1794 strlist__for_each_safe(ent, n, namelist)
1795 if (strglobmatch(ent->s, buf)) {
1797 ret = __del_trace_probe_event(fd, ent);
1800 strlist__remove(namelist, ent);
1803 ent = strlist__find(namelist, buf);
1806 ret = __del_trace_probe_event(fd, ent);
1808 strlist__remove(namelist, ent);
1811 if (found == 0 && ret >= 0)
1812 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1817 int del_perf_probe_events(struct strlist *dellist)
1820 const char *group, *event;
1822 struct str_node *ent;
1823 struct strlist *namelist;
1825 fd = open_kprobe_events(true);
1829 /* Get current event names */
1830 namelist = get_probe_trace_event_names(fd, true);
1831 if (namelist == NULL)
1834 strlist__for_each(ent, dellist) {
1835 str = strdup(ent->s);
1840 pr_debug("Parsing: %s\n", str);
1841 p = strchr(str, ':');
1850 pr_debug("Group: %s, Event: %s\n", group, event);
1851 ret = del_trace_probe_event(fd, group, event, namelist);
1856 strlist__delete(namelist);