Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * probe-event.c : perf-probe definition to kprobe_events format converter |
| 3 | * |
| 4 | * Written by Masami Hiramatsu <mhiramat@redhat.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #define _GNU_SOURCE |
| 23 | #include <sys/utsname.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <errno.h> |
| 28 | #include <stdio.h> |
| 29 | #include <unistd.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 32 | #include <stdarg.h> |
| 33 | #include <limits.h> |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 34 | |
| 35 | #undef _GNU_SOURCE |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 36 | #include "util.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 37 | #include "event.h" |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 38 | #include "string.h" |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 39 | #include "strlist.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 40 | #include "debug.h" |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 41 | #include "cache.h" |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 42 | #include "color.h" |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 43 | #include "symbol.h" |
| 44 | #include "thread.h" |
Masami Hiramatsu | 7ca5989 | 2010-04-14 18:39:28 -0400 | [diff] [blame] | 45 | #include "debugfs.h" |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 46 | #include "trace-event.h" /* For __unused */ |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 47 | #include "probe-event.h" |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 48 | #include "probe-finder.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 49 | |
| 50 | #define MAX_CMDLEN 256 |
| 51 | #define MAX_PROBE_ARGS 128 |
| 52 | #define PERFPROBE_GROUP "probe" |
| 53 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 54 | bool probe_event_dry_run; /* Dry run flag */ |
| 55 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 56 | #define semantic_error(msg ...) pr_err("Semantic error :" msg) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 57 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 58 | /* If there is no space to write, returns -E2BIG. */ |
| 59 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
Masami Hiramatsu | 8498845 | 2009-12-07 12:00:53 -0500 | [diff] [blame] | 60 | __attribute__((format(printf, 3, 4))); |
| 61 | |
| 62 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 63 | { |
| 64 | int ret; |
| 65 | va_list ap; |
| 66 | va_start(ap, format); |
| 67 | ret = vsnprintf(str, size, format, ap); |
| 68 | va_end(ap); |
| 69 | if (ret >= (int)size) |
| 70 | ret = -E2BIG; |
| 71 | return ret; |
| 72 | } |
| 73 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 74 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp); |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 75 | static struct machine machine; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 76 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 77 | /* Initialize symbol maps and path of vmlinux */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 78 | static int init_vmlinux(void) |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 79 | { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 80 | struct dso *kernel; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 81 | int ret; |
| 82 | |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 83 | symbol_conf.sort_by_name = true; |
| 84 | if (symbol_conf.vmlinux_name == NULL) |
| 85 | symbol_conf.try_vmlinux_path = true; |
| 86 | else |
| 87 | pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 88 | ret = symbol__init(); |
| 89 | if (ret < 0) { |
| 90 | pr_debug("Failed to init symbol map.\n"); |
| 91 | goto out; |
| 92 | } |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 93 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 94 | ret = machine__init(&machine, "/", 0); |
| 95 | if (ret < 0) |
| 96 | goto out; |
| 97 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 98 | kernel = dso__new_kernel(symbol_conf.vmlinux_name); |
| 99 | if (kernel == NULL) |
| 100 | die("Failed to create kernel dso."); |
| 101 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 102 | ret = __machine__create_kernel_maps(&machine, kernel); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 103 | if (ret < 0) |
| 104 | pr_debug("Failed to create kernel maps.\n"); |
| 105 | |
| 106 | out: |
| 107 | if (ret < 0) |
| 108 | pr_warning("Failed to init vmlinux path.\n"); |
| 109 | return ret; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 112 | #ifdef DWARF_SUPPORT |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 113 | static int open_vmlinux(void) |
| 114 | { |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 115 | if (map__load(machine.vmlinux_maps[MAP__FUNCTION], NULL) < 0) { |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 116 | pr_debug("Failed to load kernel map.\n"); |
| 117 | return -EINVAL; |
| 118 | } |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 119 | pr_debug("Try to open %s\n", machine.vmlinux_maps[MAP__FUNCTION]->dso->long_name); |
| 120 | return open(machine.vmlinux_maps[MAP__FUNCTION]->dso->long_name, O_RDONLY); |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 121 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 122 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 123 | /* Convert trace point to probe point with debuginfo */ |
| 124 | static int convert_to_perf_probe_point(struct kprobe_trace_point *tp, |
| 125 | struct perf_probe_point *pp) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 126 | { |
| 127 | struct symbol *sym; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 128 | int fd, ret = -ENOENT; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 129 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 130 | sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION], |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 131 | tp->symbol, NULL); |
| 132 | if (sym) { |
| 133 | fd = open_vmlinux(); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 134 | if (fd >= 0) { |
| 135 | ret = find_perf_probe_point(fd, |
| 136 | sym->start + tp->offset, pp); |
| 137 | close(fd); |
| 138 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 139 | } |
| 140 | if (ret <= 0) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 141 | pr_debug("Failed to find corresponding probes from " |
| 142 | "debuginfo. Use kprobe event information.\n"); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 143 | pp->function = strdup(tp->symbol); |
| 144 | if (pp->function == NULL) |
| 145 | return -ENOMEM; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 146 | pp->offset = tp->offset; |
| 147 | } |
| 148 | pp->retprobe = tp->retprobe; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 149 | |
| 150 | return 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* Try to find perf_probe_event with debuginfo */ |
| 154 | static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev, |
Masami Hiramatsu | ef4a356 | 2010-04-21 15:56:40 -0400 | [diff] [blame] | 155 | struct kprobe_trace_event **tevs, |
| 156 | int max_tevs) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 157 | { |
| 158 | bool need_dwarf = perf_probe_event_need_dwarf(pev); |
| 159 | int fd, ntevs; |
| 160 | |
| 161 | fd = open_vmlinux(); |
| 162 | if (fd < 0) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 163 | if (need_dwarf) { |
| 164 | pr_warning("Failed to open debuginfo file.\n"); |
| 165 | return fd; |
| 166 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 167 | pr_debug("Could not open vmlinux. Try to use symbols.\n"); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /* Searching trace events corresponding to probe event */ |
Masami Hiramatsu | ef4a356 | 2010-04-21 15:56:40 -0400 | [diff] [blame] | 172 | ntevs = find_kprobe_trace_events(fd, pev, tevs, max_tevs); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 173 | close(fd); |
| 174 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 175 | if (ntevs > 0) { /* Succeeded to find trace events */ |
| 176 | pr_debug("find %d kprobe_trace_events.\n", ntevs); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 177 | return ntevs; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 178 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 179 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 180 | if (ntevs == 0) { /* No error but failed to find probe point. */ |
| 181 | pr_warning("Probe point '%s' not found.\n", |
| 182 | synthesize_perf_probe_point(&pev->point)); |
| 183 | return -ENOENT; |
| 184 | } |
| 185 | /* Error path : ntevs < 0 */ |
Masami Hiramatsu | 15eca30 | 2010-04-21 15:56:24 -0400 | [diff] [blame] | 186 | pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); |
| 187 | if (ntevs == -EBADF) { |
| 188 | pr_warning("Warning: No dwarf info found in the vmlinux - " |
| 189 | "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n"); |
| 190 | if (!need_dwarf) { |
| 191 | pr_debug("Trying to use symbols.\nn"); |
| 192 | return 0; |
| 193 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 194 | } |
Masami Hiramatsu | 15eca30 | 2010-04-21 15:56:24 -0400 | [diff] [blame] | 195 | return ntevs; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | #define LINEBUF_SIZE 256 |
| 199 | #define NR_ADDITIONAL_LINES 2 |
| 200 | |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 201 | static int show_one_line(FILE *fp, int l, bool skip, bool show_num) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 202 | { |
| 203 | char buf[LINEBUF_SIZE]; |
| 204 | const char *color = PERF_COLOR_BLUE; |
| 205 | |
| 206 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
| 207 | goto error; |
| 208 | if (!skip) { |
| 209 | if (show_num) |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 210 | fprintf(stdout, "%7d %s", l, buf); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 211 | else |
| 212 | color_fprintf(stdout, color, " %s", buf); |
| 213 | } |
| 214 | |
| 215 | while (strlen(buf) == LINEBUF_SIZE - 1 && |
| 216 | buf[LINEBUF_SIZE - 2] != '\n') { |
| 217 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
| 218 | goto error; |
| 219 | if (!skip) { |
| 220 | if (show_num) |
| 221 | fprintf(stdout, "%s", buf); |
| 222 | else |
| 223 | color_fprintf(stdout, color, "%s", buf); |
| 224 | } |
| 225 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 226 | |
| 227 | return 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 228 | error: |
| 229 | if (feof(fp)) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 230 | pr_warning("Source file is shorter than expected.\n"); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 231 | else |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 232 | pr_warning("File read error: %s\n", strerror(errno)); |
| 233 | |
| 234 | return -1; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Show line-range always requires debuginfo to find source file and |
| 239 | * line number. |
| 240 | */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 241 | int show_line_range(struct line_range *lr) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 242 | { |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 243 | int l = 1; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 244 | struct line_node *ln; |
| 245 | FILE *fp; |
| 246 | int fd, ret; |
| 247 | |
| 248 | /* Search a line range */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 249 | ret = init_vmlinux(); |
| 250 | if (ret < 0) |
| 251 | return ret; |
| 252 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 253 | fd = open_vmlinux(); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 254 | if (fd < 0) { |
| 255 | pr_warning("Failed to open debuginfo file.\n"); |
| 256 | return fd; |
| 257 | } |
| 258 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 259 | ret = find_line_range(fd, lr); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 260 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 261 | if (ret == 0) { |
| 262 | pr_warning("Specified source line is not found.\n"); |
| 263 | return -ENOENT; |
| 264 | } else if (ret < 0) { |
| 265 | pr_warning("Debuginfo analysis failed. (%d)\n", ret); |
| 266 | return ret; |
| 267 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 268 | |
| 269 | setup_pager(); |
| 270 | |
| 271 | if (lr->function) |
| 272 | fprintf(stdout, "<%s:%d>\n", lr->function, |
| 273 | lr->start - lr->offset); |
| 274 | else |
| 275 | fprintf(stdout, "<%s:%d>\n", lr->file, lr->start); |
| 276 | |
| 277 | fp = fopen(lr->path, "r"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 278 | if (fp == NULL) { |
| 279 | pr_warning("Failed to open %s: %s\n", lr->path, |
| 280 | strerror(errno)); |
| 281 | return -errno; |
| 282 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 283 | /* Skip to starting line number */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 284 | while (l < lr->start && ret >= 0) |
| 285 | ret = show_one_line(fp, l++, true, false); |
| 286 | if (ret < 0) |
| 287 | goto end; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 288 | |
| 289 | list_for_each_entry(ln, &lr->line_list, list) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 290 | while (ln->line > l && ret >= 0) |
| 291 | ret = show_one_line(fp, (l++) - lr->offset, |
| 292 | false, false); |
| 293 | if (ret >= 0) |
| 294 | ret = show_one_line(fp, (l++) - lr->offset, |
| 295 | false, true); |
| 296 | if (ret < 0) |
| 297 | goto end; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (lr->end == INT_MAX) |
| 301 | lr->end = l + NR_ADDITIONAL_LINES; |
Masami Hiramatsu | dda4ab3 | 2010-04-14 18:39:50 -0400 | [diff] [blame] | 302 | while (l <= lr->end && !feof(fp) && ret >= 0) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 303 | ret = show_one_line(fp, (l++) - lr->offset, false, false); |
| 304 | end: |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 305 | fclose(fp); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 306 | return ret; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | #else /* !DWARF_SUPPORT */ |
| 310 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 311 | static int convert_to_perf_probe_point(struct kprobe_trace_point *tp, |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 312 | struct perf_probe_point *pp) |
| 313 | { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 314 | pp->function = strdup(tp->symbol); |
| 315 | if (pp->function == NULL) |
| 316 | return -ENOMEM; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 317 | pp->offset = tp->offset; |
| 318 | pp->retprobe = tp->retprobe; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 319 | |
| 320 | return 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev, |
Masami Hiramatsu | ef4a356 | 2010-04-21 15:56:40 -0400 | [diff] [blame] | 324 | struct kprobe_trace_event **tevs __unused, |
| 325 | int max_tevs __unused) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 326 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 327 | if (perf_probe_event_need_dwarf(pev)) { |
| 328 | pr_warning("Debuginfo-analysis is not supported.\n"); |
| 329 | return -ENOSYS; |
| 330 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 331 | return 0; |
| 332 | } |
| 333 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 334 | int show_line_range(struct line_range *lr __unused) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 335 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 336 | pr_warning("Debuginfo-analysis is not supported.\n"); |
| 337 | return -ENOSYS; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 338 | } |
| 339 | |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 340 | #endif |
| 341 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 342 | int parse_line_range_desc(const char *arg, struct line_range *lr) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 343 | { |
| 344 | const char *ptr; |
| 345 | char *tmp; |
| 346 | /* |
| 347 | * <Syntax> |
| 348 | * SRC:SLN[+NUM|-ELN] |
| 349 | * FUNC[:SLN[+NUM|-ELN]] |
| 350 | */ |
| 351 | ptr = strchr(arg, ':'); |
| 352 | if (ptr) { |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 353 | lr->start = (int)strtoul(ptr + 1, &tmp, 0); |
Masami Hiramatsu | dda4ab3 | 2010-04-14 18:39:50 -0400 | [diff] [blame] | 354 | if (*tmp == '+') { |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 355 | lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0); |
Masami Hiramatsu | dda4ab3 | 2010-04-14 18:39:50 -0400 | [diff] [blame] | 356 | lr->end--; /* |
| 357 | * Adjust the number of lines here. |
| 358 | * If the number of lines == 1, the |
| 359 | * the end of line should be equal to |
| 360 | * the start of line. |
| 361 | */ |
| 362 | } else if (*tmp == '-') |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 363 | lr->end = (int)strtoul(tmp + 1, &tmp, 0); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 364 | else |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 365 | lr->end = INT_MAX; |
| 366 | pr_debug("Line range is %d to %d\n", lr->start, lr->end); |
| 367 | if (lr->start > lr->end) { |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 368 | semantic_error("Start line must be smaller" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 369 | " than end line.\n"); |
| 370 | return -EINVAL; |
| 371 | } |
| 372 | if (*tmp != '\0') { |
| 373 | semantic_error("Tailing with invalid character '%d'.\n", |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 374 | *tmp); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 375 | return -EINVAL; |
| 376 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 377 | tmp = strndup(arg, (ptr - arg)); |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 378 | } else { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 379 | tmp = strdup(arg); |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 380 | lr->end = INT_MAX; |
| 381 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 382 | |
| 383 | if (tmp == NULL) |
| 384 | return -ENOMEM; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 385 | |
| 386 | if (strchr(tmp, '.')) |
| 387 | lr->file = tmp; |
| 388 | else |
| 389 | lr->function = tmp; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 390 | |
| 391 | return 0; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 392 | } |
| 393 | |
Masami Hiramatsu | b7702a2 | 2009-12-16 17:24:15 -0500 | [diff] [blame] | 394 | /* Check the name is good for event/group */ |
| 395 | static bool check_event_name(const char *name) |
| 396 | { |
| 397 | if (!isalpha(*name) && *name != '_') |
| 398 | return false; |
| 399 | while (*++name != '\0') { |
| 400 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') |
| 401 | return false; |
| 402 | } |
| 403 | return true; |
| 404 | } |
| 405 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 406 | /* Parse probepoint definition. */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 407 | static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 408 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 409 | struct perf_probe_point *pp = &pev->point; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 410 | char *ptr, *tmp; |
| 411 | char c, nc = 0; |
| 412 | /* |
| 413 | * <Syntax> |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 414 | * perf probe [EVENT=]SRC[:LN|;PTN] |
| 415 | * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 416 | * |
| 417 | * TODO:Group name support |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 418 | */ |
| 419 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 420 | ptr = strpbrk(arg, ";=@+%"); |
| 421 | if (ptr && *ptr == '=') { /* Event name */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 422 | *ptr = '\0'; |
| 423 | tmp = ptr + 1; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 424 | if (strchr(arg, ':')) { |
| 425 | semantic_error("Group name is not supported yet.\n"); |
| 426 | return -ENOTSUP; |
| 427 | } |
| 428 | if (!check_event_name(arg)) { |
Masami Hiramatsu | b7702a2 | 2009-12-16 17:24:15 -0500 | [diff] [blame] | 429 | semantic_error("%s is bad for event name -it must " |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 430 | "follow C symbol-naming rule.\n", arg); |
| 431 | return -EINVAL; |
| 432 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 433 | pev->event = strdup(arg); |
| 434 | if (pev->event == NULL) |
| 435 | return -ENOMEM; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 436 | pev->group = NULL; |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 437 | arg = tmp; |
| 438 | } |
| 439 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 440 | ptr = strpbrk(arg, ";:+@%"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 441 | if (ptr) { |
| 442 | nc = *ptr; |
| 443 | *ptr++ = '\0'; |
| 444 | } |
| 445 | |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 446 | tmp = strdup(arg); |
| 447 | if (tmp == NULL) |
| 448 | return -ENOMEM; |
| 449 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 450 | /* Check arg is function or file and copy it */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 451 | if (strchr(tmp, '.')) /* File */ |
| 452 | pp->file = tmp; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 453 | else /* Function */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 454 | pp->function = tmp; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 455 | |
| 456 | /* Parse other options */ |
| 457 | while (ptr) { |
| 458 | arg = ptr; |
| 459 | c = nc; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 460 | if (c == ';') { /* Lazy pattern must be the last part */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 461 | pp->lazy_line = strdup(arg); |
| 462 | if (pp->lazy_line == NULL) |
| 463 | return -ENOMEM; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 464 | break; |
| 465 | } |
| 466 | ptr = strpbrk(arg, ";:+@%"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 467 | if (ptr) { |
| 468 | nc = *ptr; |
| 469 | *ptr++ = '\0'; |
| 470 | } |
| 471 | switch (c) { |
| 472 | case ':': /* Line number */ |
| 473 | pp->line = strtoul(arg, &tmp, 0); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 474 | if (*tmp != '\0') { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 475 | semantic_error("There is non-digit char" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 476 | " in line number.\n"); |
| 477 | return -EINVAL; |
| 478 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 479 | break; |
| 480 | case '+': /* Byte offset from a symbol */ |
| 481 | pp->offset = strtoul(arg, &tmp, 0); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 482 | if (*tmp != '\0') { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 483 | semantic_error("There is non-digit character" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 484 | " in offset.\n"); |
| 485 | return -EINVAL; |
| 486 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 487 | break; |
| 488 | case '@': /* File name */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 489 | if (pp->file) { |
| 490 | semantic_error("SRC@SRC is not allowed.\n"); |
| 491 | return -EINVAL; |
| 492 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 493 | pp->file = strdup(arg); |
| 494 | if (pp->file == NULL) |
| 495 | return -ENOMEM; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 496 | break; |
| 497 | case '%': /* Probe places */ |
| 498 | if (strcmp(arg, "return") == 0) { |
| 499 | pp->retprobe = 1; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 500 | } else { /* Others not supported yet */ |
| 501 | semantic_error("%%%s is not supported.\n", arg); |
| 502 | return -ENOTSUP; |
| 503 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 504 | break; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 505 | default: /* Buggy case */ |
| 506 | pr_err("This program has a bug at %s:%d.\n", |
| 507 | __FILE__, __LINE__); |
| 508 | return -ENOTSUP; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 509 | break; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /* Exclusion check */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 514 | if (pp->lazy_line && pp->line) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 515 | semantic_error("Lazy pattern can't be used with line number."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 516 | return -EINVAL; |
| 517 | } |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 518 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 519 | if (pp->lazy_line && pp->offset) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 520 | semantic_error("Lazy pattern can't be used with offset."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 521 | return -EINVAL; |
| 522 | } |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 523 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 524 | if (pp->line && pp->offset) { |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 525 | semantic_error("Offset can't be used with line number."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 526 | return -EINVAL; |
| 527 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 528 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 529 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 530 | semantic_error("File always requires line number or " |
| 531 | "lazy pattern."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 532 | return -EINVAL; |
| 533 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 534 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 535 | if (pp->offset && !pp->function) { |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 536 | semantic_error("Offset requires an entry function."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 537 | return -EINVAL; |
| 538 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 539 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 540 | if (pp->retprobe && !pp->function) { |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 541 | semantic_error("Return probe requires an entry function."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 542 | return -EINVAL; |
| 543 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 544 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 545 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 546 | semantic_error("Offset/Line/Lazy pattern can't be used with " |
| 547 | "return probe."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 548 | return -EINVAL; |
| 549 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 550 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 551 | pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 552 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, |
| 553 | pp->lazy_line); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 554 | return 0; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 555 | } |
| 556 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 557 | /* Parse perf-probe event argument */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 558 | static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 559 | { |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame^] | 560 | char *tmp, *goodname; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 561 | struct perf_probe_arg_field **fieldp; |
| 562 | |
| 563 | pr_debug("parsing arg: %s into ", str); |
| 564 | |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 565 | tmp = strchr(str, '='); |
| 566 | if (tmp) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 567 | arg->name = strndup(str, tmp - str); |
| 568 | if (arg->name == NULL) |
| 569 | return -ENOMEM; |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 570 | pr_debug("name:%s ", arg->name); |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 571 | str = tmp + 1; |
| 572 | } |
| 573 | |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 574 | tmp = strchr(str, ':'); |
| 575 | if (tmp) { /* Type setting */ |
| 576 | *tmp = '\0'; |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 577 | arg->type = strdup(tmp + 1); |
| 578 | if (arg->type == NULL) |
| 579 | return -ENOMEM; |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 580 | pr_debug("type:%s ", arg->type); |
| 581 | } |
| 582 | |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame^] | 583 | tmp = strpbrk(str, "-.["); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 584 | if (!is_c_varname(str) || !tmp) { |
| 585 | /* A variable, register, symbol or special value */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 586 | arg->var = strdup(str); |
| 587 | if (arg->var == NULL) |
| 588 | return -ENOMEM; |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 589 | pr_debug("%s\n", arg->var); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 590 | return 0; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 591 | } |
| 592 | |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame^] | 593 | /* Structure fields or array element */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 594 | arg->var = strndup(str, tmp - str); |
| 595 | if (arg->var == NULL) |
| 596 | return -ENOMEM; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame^] | 597 | goodname = arg->var; |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 598 | pr_debug("%s, ", arg->var); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 599 | fieldp = &arg->field; |
| 600 | |
| 601 | do { |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 602 | *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); |
| 603 | if (*fieldp == NULL) |
| 604 | return -ENOMEM; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame^] | 605 | if (*tmp == '[') { /* Array */ |
| 606 | str = tmp; |
| 607 | (*fieldp)->index = strtol(str + 1, &tmp, 0); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 608 | (*fieldp)->ref = true; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame^] | 609 | if (*tmp != ']' || tmp == str + 1) { |
| 610 | semantic_error("Array index must be a" |
| 611 | " number.\n"); |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | tmp++; |
| 615 | if (*tmp == '\0') |
| 616 | tmp = NULL; |
| 617 | } else { /* Structure */ |
| 618 | if (*tmp == '.') { |
| 619 | str = tmp + 1; |
| 620 | (*fieldp)->ref = false; |
| 621 | } else if (tmp[1] == '>') { |
| 622 | str = tmp + 2; |
| 623 | (*fieldp)->ref = true; |
| 624 | } else { |
| 625 | semantic_error("Argument parse error: %s\n", |
| 626 | str); |
| 627 | return -EINVAL; |
| 628 | } |
| 629 | tmp = strpbrk(str, "-.["); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 630 | } |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 631 | if (tmp) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 632 | (*fieldp)->name = strndup(str, tmp - str); |
| 633 | if ((*fieldp)->name == NULL) |
| 634 | return -ENOMEM; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame^] | 635 | if (*str != '[') |
| 636 | goodname = (*fieldp)->name; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 637 | pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); |
| 638 | fieldp = &(*fieldp)->next; |
| 639 | } |
| 640 | } while (tmp); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 641 | (*fieldp)->name = strdup(str); |
| 642 | if ((*fieldp)->name == NULL) |
| 643 | return -ENOMEM; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame^] | 644 | if (*str != '[') |
| 645 | goodname = (*fieldp)->name; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 646 | pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); |
Masami Hiramatsu | df0faf4 | 2010-04-12 13:17:00 -0400 | [diff] [blame] | 647 | |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame^] | 648 | /* If no name is specified, set the last field name (not array index)*/ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 649 | if (!arg->name) { |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame^] | 650 | arg->name = strdup(goodname); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 651 | if (arg->name == NULL) |
| 652 | return -ENOMEM; |
| 653 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 654 | return 0; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 655 | } |
| 656 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 657 | /* Parse perf-probe event command */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 658 | int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 659 | { |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 660 | char **argv; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 661 | int argc, i, ret = 0; |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 662 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 663 | argv = argv_split(cmd, &argc); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 664 | if (!argv) { |
| 665 | pr_debug("Failed to split arguments.\n"); |
| 666 | return -ENOMEM; |
| 667 | } |
| 668 | if (argc - 1 > MAX_PROBE_ARGS) { |
| 669 | semantic_error("Too many probe arguments (%d).\n", argc - 1); |
| 670 | ret = -ERANGE; |
| 671 | goto out; |
| 672 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 673 | /* Parse probe point */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 674 | ret = parse_perf_probe_point(argv[0], pev); |
| 675 | if (ret < 0) |
| 676 | goto out; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 677 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 678 | /* Copy arguments and ensure return probe has no C argument */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 679 | pev->nargs = argc - 1; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 680 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
| 681 | if (pev->args == NULL) { |
| 682 | ret = -ENOMEM; |
| 683 | goto out; |
| 684 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 685 | for (i = 0; i < pev->nargs && ret >= 0; i++) { |
| 686 | ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); |
| 687 | if (ret >= 0 && |
| 688 | is_c_varname(pev->args[i].var) && pev->point.retprobe) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 689 | semantic_error("You can't specify local variable for" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 690 | " kretprobe.\n"); |
| 691 | ret = -EINVAL; |
| 692 | } |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 693 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 694 | out: |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 695 | argv_free(argv); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 696 | |
| 697 | return ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 698 | } |
| 699 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 700 | /* Return true if this perf_probe_event requires debuginfo */ |
| 701 | bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 702 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 703 | int i; |
| 704 | |
| 705 | if (pev->point.file || pev->point.line || pev->point.lazy_line) |
| 706 | return true; |
| 707 | |
| 708 | for (i = 0; i < pev->nargs; i++) |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 709 | if (is_c_varname(pev->args[i].var)) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 710 | return true; |
| 711 | |
| 712 | return false; |
| 713 | } |
| 714 | |
| 715 | /* Parse kprobe_events event into struct probe_point */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 716 | int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 717 | { |
| 718 | struct kprobe_trace_point *tp = &tev->point; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 719 | char pr; |
| 720 | char *p; |
| 721 | int ret, i, argc; |
| 722 | char **argv; |
| 723 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 724 | pr_debug("Parsing kprobe_events: %s\n", cmd); |
| 725 | argv = argv_split(cmd, &argc); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 726 | if (!argv) { |
| 727 | pr_debug("Failed to split arguments.\n"); |
| 728 | return -ENOMEM; |
| 729 | } |
| 730 | if (argc < 2) { |
| 731 | semantic_error("Too few probe arguments.\n"); |
| 732 | ret = -ERANGE; |
| 733 | goto out; |
| 734 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 735 | |
| 736 | /* Scan event and group name. */ |
Liming Wang | 93aaa45 | 2009-12-02 16:42:54 +0800 | [diff] [blame] | 737 | ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 738 | &pr, (float *)(void *)&tev->group, |
| 739 | (float *)(void *)&tev->event); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 740 | if (ret != 3) { |
| 741 | semantic_error("Failed to parse event name: %s\n", argv[0]); |
| 742 | ret = -EINVAL; |
| 743 | goto out; |
| 744 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 745 | pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 746 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 747 | tp->retprobe = (pr == 'r'); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 748 | |
| 749 | /* Scan function name and offset */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 750 | ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol, |
| 751 | &tp->offset); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 752 | if (ret == 1) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 753 | tp->offset = 0; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 754 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 755 | tev->nargs = argc - 2; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 756 | tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs); |
| 757 | if (tev->args == NULL) { |
| 758 | ret = -ENOMEM; |
| 759 | goto out; |
| 760 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 761 | for (i = 0; i < tev->nargs; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 762 | p = strchr(argv[i + 2], '='); |
| 763 | if (p) /* We don't need which register is assigned. */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 764 | *p++ = '\0'; |
| 765 | else |
| 766 | p = argv[i + 2]; |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 767 | tev->args[i].name = strdup(argv[i + 2]); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 768 | /* TODO: parse regs and offset */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 769 | tev->args[i].value = strdup(p); |
| 770 | if (tev->args[i].name == NULL || tev->args[i].value == NULL) { |
| 771 | ret = -ENOMEM; |
| 772 | goto out; |
| 773 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 774 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 775 | ret = 0; |
| 776 | out: |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 777 | argv_free(argv); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 778 | return ret; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 779 | } |
| 780 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 781 | /* Compose only probe arg */ |
| 782 | int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len) |
| 783 | { |
| 784 | struct perf_probe_arg_field *field = pa->field; |
| 785 | int ret; |
| 786 | char *tmp = buf; |
| 787 | |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 788 | if (pa->name && pa->var) |
| 789 | ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var); |
| 790 | else |
| 791 | ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 792 | if (ret <= 0) |
| 793 | goto error; |
| 794 | tmp += ret; |
| 795 | len -= ret; |
| 796 | |
| 797 | while (field) { |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame^] | 798 | if (field->name[0] == '[') |
| 799 | ret = e_snprintf(tmp, len, "%s", field->name); |
| 800 | else |
| 801 | ret = e_snprintf(tmp, len, "%s%s", |
| 802 | field->ref ? "->" : ".", field->name); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 803 | if (ret <= 0) |
| 804 | goto error; |
| 805 | tmp += ret; |
| 806 | len -= ret; |
| 807 | field = field->next; |
| 808 | } |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 809 | |
| 810 | if (pa->type) { |
| 811 | ret = e_snprintf(tmp, len, ":%s", pa->type); |
| 812 | if (ret <= 0) |
| 813 | goto error; |
| 814 | tmp += ret; |
| 815 | len -= ret; |
| 816 | } |
| 817 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 818 | return tmp - buf; |
| 819 | error: |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 820 | pr_debug("Failed to synthesize perf probe argument: %s", |
| 821 | strerror(-ret)); |
| 822 | return ret; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 823 | } |
| 824 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 825 | /* Compose only probe point (not argument) */ |
| 826 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 827 | { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 828 | char *buf, *tmp; |
| 829 | char offs[32] = "", line[32] = "", file[32] = ""; |
| 830 | int ret, len; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 831 | |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 832 | buf = zalloc(MAX_CMDLEN); |
| 833 | if (buf == NULL) { |
| 834 | ret = -ENOMEM; |
| 835 | goto error; |
| 836 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 837 | if (pp->offset) { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 838 | ret = e_snprintf(offs, 32, "+%lu", pp->offset); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 839 | if (ret <= 0) |
| 840 | goto error; |
| 841 | } |
| 842 | if (pp->line) { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 843 | ret = e_snprintf(line, 32, ":%d", pp->line); |
| 844 | if (ret <= 0) |
| 845 | goto error; |
| 846 | } |
| 847 | if (pp->file) { |
Masami Hiramatsu | dd259c5 | 2010-04-14 18:39:35 -0400 | [diff] [blame] | 848 | len = strlen(pp->file) - 31; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 849 | if (len < 0) |
| 850 | len = 0; |
| 851 | tmp = strchr(pp->file + len, '/'); |
| 852 | if (!tmp) |
Masami Hiramatsu | dd259c5 | 2010-04-14 18:39:35 -0400 | [diff] [blame] | 853 | tmp = pp->file + len; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 854 | ret = e_snprintf(file, 32, "@%s", tmp + 1); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 855 | if (ret <= 0) |
| 856 | goto error; |
| 857 | } |
| 858 | |
| 859 | if (pp->function) |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 860 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function, |
| 861 | offs, pp->retprobe ? "%return" : "", line, |
| 862 | file); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 863 | else |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 864 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 865 | if (ret <= 0) |
| 866 | goto error; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 867 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 868 | return buf; |
| 869 | error: |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 870 | pr_debug("Failed to synthesize perf probe point: %s", |
| 871 | strerror(-ret)); |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 872 | if (buf) |
| 873 | free(buf); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 874 | return NULL; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | #if 0 |
| 878 | char *synthesize_perf_probe_command(struct perf_probe_event *pev) |
| 879 | { |
| 880 | char *buf; |
| 881 | int i, len, ret; |
| 882 | |
| 883 | buf = synthesize_perf_probe_point(&pev->point); |
| 884 | if (!buf) |
| 885 | return NULL; |
| 886 | |
| 887 | len = strlen(buf); |
| 888 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 889 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 890 | pev->args[i].name); |
| 891 | if (ret <= 0) { |
| 892 | free(buf); |
| 893 | return NULL; |
| 894 | } |
| 895 | len += ret; |
| 896 | } |
| 897 | |
| 898 | return buf; |
| 899 | } |
| 900 | #endif |
| 901 | |
| 902 | static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref, |
| 903 | char **buf, size_t *buflen, |
| 904 | int depth) |
| 905 | { |
| 906 | int ret; |
| 907 | if (ref->next) { |
| 908 | depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf, |
| 909 | buflen, depth + 1); |
| 910 | if (depth < 0) |
| 911 | goto out; |
| 912 | } |
| 913 | |
| 914 | ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset); |
| 915 | if (ret < 0) |
| 916 | depth = ret; |
| 917 | else { |
| 918 | *buf += ret; |
| 919 | *buflen -= ret; |
| 920 | } |
| 921 | out: |
| 922 | return depth; |
| 923 | |
| 924 | } |
| 925 | |
| 926 | static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg, |
| 927 | char *buf, size_t buflen) |
| 928 | { |
| 929 | int ret, depth = 0; |
| 930 | char *tmp = buf; |
| 931 | |
| 932 | /* Argument name or separator */ |
| 933 | if (arg->name) |
| 934 | ret = e_snprintf(buf, buflen, " %s=", arg->name); |
| 935 | else |
| 936 | ret = e_snprintf(buf, buflen, " "); |
| 937 | if (ret < 0) |
| 938 | return ret; |
| 939 | buf += ret; |
| 940 | buflen -= ret; |
| 941 | |
| 942 | /* Dereferencing arguments */ |
| 943 | if (arg->ref) { |
| 944 | depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf, |
| 945 | &buflen, 1); |
| 946 | if (depth < 0) |
| 947 | return depth; |
| 948 | } |
| 949 | |
| 950 | /* Print argument value */ |
| 951 | ret = e_snprintf(buf, buflen, "%s", arg->value); |
| 952 | if (ret < 0) |
| 953 | return ret; |
| 954 | buf += ret; |
| 955 | buflen -= ret; |
| 956 | |
| 957 | /* Closing */ |
| 958 | while (depth--) { |
| 959 | ret = e_snprintf(buf, buflen, ")"); |
| 960 | if (ret < 0) |
| 961 | return ret; |
| 962 | buf += ret; |
| 963 | buflen -= ret; |
| 964 | } |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 965 | /* Print argument type */ |
| 966 | if (arg->type) { |
| 967 | ret = e_snprintf(buf, buflen, ":%s", arg->type); |
| 968 | if (ret <= 0) |
| 969 | return ret; |
| 970 | buf += ret; |
| 971 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 972 | |
| 973 | return buf - tmp; |
| 974 | } |
| 975 | |
| 976 | char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev) |
| 977 | { |
| 978 | struct kprobe_trace_point *tp = &tev->point; |
| 979 | char *buf; |
| 980 | int i, len, ret; |
| 981 | |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 982 | buf = zalloc(MAX_CMDLEN); |
| 983 | if (buf == NULL) |
| 984 | return NULL; |
| 985 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 986 | len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu", |
| 987 | tp->retprobe ? 'r' : 'p', |
| 988 | tev->group, tev->event, |
| 989 | tp->symbol, tp->offset); |
| 990 | if (len <= 0) |
| 991 | goto error; |
| 992 | |
| 993 | for (i = 0; i < tev->nargs; i++) { |
| 994 | ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len, |
| 995 | MAX_CMDLEN - len); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 996 | if (ret <= 0) |
| 997 | goto error; |
| 998 | len += ret; |
| 999 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1000 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1001 | return buf; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1002 | error: |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1003 | free(buf); |
| 1004 | return NULL; |
| 1005 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1006 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1007 | int convert_to_perf_probe_event(struct kprobe_trace_event *tev, |
| 1008 | struct perf_probe_event *pev) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1009 | { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1010 | char buf[64] = ""; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1011 | int i, ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1012 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 1013 | /* Convert event/group name */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1014 | pev->event = strdup(tev->event); |
| 1015 | pev->group = strdup(tev->group); |
| 1016 | if (pev->event == NULL || pev->group == NULL) |
| 1017 | return -ENOMEM; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1018 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 1019 | /* Convert trace_point to probe_point */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1020 | ret = convert_to_perf_probe_point(&tev->point, &pev->point); |
| 1021 | if (ret < 0) |
| 1022 | return ret; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 1023 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1024 | /* Convert trace_arg to probe_arg */ |
| 1025 | pev->nargs = tev->nargs; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1026 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
| 1027 | if (pev->args == NULL) |
| 1028 | return -ENOMEM; |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1029 | for (i = 0; i < tev->nargs && ret >= 0; i++) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1030 | if (tev->args[i].name) |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1031 | pev->args[i].name = strdup(tev->args[i].name); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1032 | else { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1033 | ret = synthesize_kprobe_trace_arg(&tev->args[i], |
| 1034 | buf, 64); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1035 | pev->args[i].name = strdup(buf); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1036 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1037 | if (pev->args[i].name == NULL && ret >= 0) |
| 1038 | ret = -ENOMEM; |
| 1039 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1040 | |
| 1041 | if (ret < 0) |
| 1042 | clear_perf_probe_event(pev); |
| 1043 | |
| 1044 | return ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | void clear_perf_probe_event(struct perf_probe_event *pev) |
| 1048 | { |
| 1049 | struct perf_probe_point *pp = &pev->point; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1050 | struct perf_probe_arg_field *field, *next; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1051 | int i; |
| 1052 | |
| 1053 | if (pev->event) |
| 1054 | free(pev->event); |
| 1055 | if (pev->group) |
| 1056 | free(pev->group); |
| 1057 | if (pp->file) |
| 1058 | free(pp->file); |
| 1059 | if (pp->function) |
| 1060 | free(pp->function); |
| 1061 | if (pp->lazy_line) |
| 1062 | free(pp->lazy_line); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1063 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1064 | if (pev->args[i].name) |
| 1065 | free(pev->args[i].name); |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1066 | if (pev->args[i].var) |
| 1067 | free(pev->args[i].var); |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 1068 | if (pev->args[i].type) |
| 1069 | free(pev->args[i].type); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1070 | field = pev->args[i].field; |
| 1071 | while (field) { |
| 1072 | next = field->next; |
| 1073 | if (field->name) |
| 1074 | free(field->name); |
| 1075 | free(field); |
| 1076 | field = next; |
| 1077 | } |
| 1078 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1079 | if (pev->args) |
| 1080 | free(pev->args); |
| 1081 | memset(pev, 0, sizeof(*pev)); |
| 1082 | } |
| 1083 | |
| 1084 | void clear_kprobe_trace_event(struct kprobe_trace_event *tev) |
| 1085 | { |
| 1086 | struct kprobe_trace_arg_ref *ref, *next; |
| 1087 | int i; |
| 1088 | |
| 1089 | if (tev->event) |
| 1090 | free(tev->event); |
| 1091 | if (tev->group) |
| 1092 | free(tev->group); |
| 1093 | if (tev->point.symbol) |
| 1094 | free(tev->point.symbol); |
| 1095 | for (i = 0; i < tev->nargs; i++) { |
| 1096 | if (tev->args[i].name) |
| 1097 | free(tev->args[i].name); |
| 1098 | if (tev->args[i].value) |
| 1099 | free(tev->args[i].value); |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 1100 | if (tev->args[i].type) |
| 1101 | free(tev->args[i].type); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1102 | ref = tev->args[i].ref; |
| 1103 | while (ref) { |
| 1104 | next = ref->next; |
| 1105 | free(ref); |
| 1106 | ref = next; |
| 1107 | } |
| 1108 | } |
| 1109 | if (tev->args) |
| 1110 | free(tev->args); |
| 1111 | memset(tev, 0, sizeof(*tev)); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1112 | } |
| 1113 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1114 | static int open_kprobe_events(bool readwrite) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1115 | { |
| 1116 | char buf[PATH_MAX]; |
Masami Hiramatsu | 7ca5989 | 2010-04-14 18:39:28 -0400 | [diff] [blame] | 1117 | const char *__debugfs; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1118 | int ret; |
| 1119 | |
Masami Hiramatsu | 7ca5989 | 2010-04-14 18:39:28 -0400 | [diff] [blame] | 1120 | __debugfs = debugfs_find_mountpoint(); |
| 1121 | if (__debugfs == NULL) { |
| 1122 | pr_warning("Debugfs is not mounted.\n"); |
| 1123 | return -ENOENT; |
| 1124 | } |
| 1125 | |
| 1126 | ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1127 | if (ret >= 0) { |
Masami Hiramatsu | 7ca5989 | 2010-04-14 18:39:28 -0400 | [diff] [blame] | 1128 | pr_debug("Opening %s write=%d\n", buf, readwrite); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1129 | if (readwrite && !probe_event_dry_run) |
| 1130 | ret = open(buf, O_RDWR, O_APPEND); |
| 1131 | else |
| 1132 | ret = open(buf, O_RDONLY, 0); |
| 1133 | } |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1134 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1135 | if (ret < 0) { |
| 1136 | if (errno == ENOENT) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1137 | pr_warning("kprobe_events file does not exist - please" |
| 1138 | " rebuild kernel with CONFIG_KPROBE_EVENT.\n"); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1139 | else |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1140 | pr_warning("Failed to open kprobe_events file: %s\n", |
| 1141 | strerror(errno)); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1142 | } |
| 1143 | return ret; |
| 1144 | } |
| 1145 | |
| 1146 | /* Get raw string list of current kprobe_events */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1147 | static struct strlist *get_kprobe_trace_command_rawlist(int fd) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1148 | { |
| 1149 | int ret, idx; |
| 1150 | FILE *fp; |
| 1151 | char buf[MAX_CMDLEN]; |
| 1152 | char *p; |
| 1153 | struct strlist *sl; |
| 1154 | |
| 1155 | sl = strlist__new(true, NULL); |
| 1156 | |
| 1157 | fp = fdopen(dup(fd), "r"); |
| 1158 | while (!feof(fp)) { |
| 1159 | p = fgets(buf, MAX_CMDLEN, fp); |
| 1160 | if (!p) |
| 1161 | break; |
| 1162 | |
| 1163 | idx = strlen(p) - 1; |
| 1164 | if (p[idx] == '\n') |
| 1165 | p[idx] = '\0'; |
| 1166 | ret = strlist__add(sl, buf); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1167 | if (ret < 0) { |
| 1168 | pr_debug("strlist__add failed: %s\n", strerror(-ret)); |
| 1169 | strlist__delete(sl); |
| 1170 | return NULL; |
| 1171 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1172 | } |
| 1173 | fclose(fp); |
| 1174 | |
| 1175 | return sl; |
| 1176 | } |
| 1177 | |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1178 | /* Show an event */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1179 | static int show_perf_probe_event(struct perf_probe_event *pev) |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1180 | { |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 1181 | int i, ret; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1182 | char buf[128]; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1183 | char *place; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1184 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1185 | /* Synthesize only event probe point */ |
| 1186 | place = synthesize_perf_probe_point(&pev->point); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1187 | if (!place) |
| 1188 | return -EINVAL; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1189 | |
| 1190 | ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event); |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 1191 | if (ret < 0) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1192 | return ret; |
| 1193 | |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1194 | printf(" %-20s (on %s", buf, place); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1195 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1196 | if (pev->nargs > 0) { |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1197 | printf(" with"); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1198 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1199 | ret = synthesize_perf_probe_arg(&pev->args[i], |
| 1200 | buf, 128); |
| 1201 | if (ret < 0) |
| 1202 | break; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1203 | printf(" %s", buf); |
| 1204 | } |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1205 | } |
| 1206 | printf(")\n"); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1207 | free(place); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1208 | return ret; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1209 | } |
| 1210 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1211 | /* List up current perf-probe events */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1212 | int show_perf_probe_events(void) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1213 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1214 | int fd, ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1215 | struct kprobe_trace_event tev; |
| 1216 | struct perf_probe_event pev; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1217 | struct strlist *rawlist; |
| 1218 | struct str_node *ent; |
| 1219 | |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 1220 | setup_pager(); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1221 | ret = init_vmlinux(); |
| 1222 | if (ret < 0) |
| 1223 | return ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1224 | |
| 1225 | memset(&tev, 0, sizeof(tev)); |
| 1226 | memset(&pev, 0, sizeof(pev)); |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 1227 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1228 | fd = open_kprobe_events(false); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1229 | if (fd < 0) |
| 1230 | return fd; |
| 1231 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1232 | rawlist = get_kprobe_trace_command_rawlist(fd); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1233 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1234 | if (!rawlist) |
| 1235 | return -ENOENT; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1236 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 1237 | strlist__for_each(ent, rawlist) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1238 | ret = parse_kprobe_trace_command(ent->s, &tev); |
| 1239 | if (ret >= 0) { |
| 1240 | ret = convert_to_perf_probe_event(&tev, &pev); |
| 1241 | if (ret >= 0) |
| 1242 | ret = show_perf_probe_event(&pev); |
| 1243 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1244 | clear_perf_probe_event(&pev); |
| 1245 | clear_kprobe_trace_event(&tev); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1246 | if (ret < 0) |
| 1247 | break; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1248 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1249 | strlist__delete(rawlist); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1250 | |
| 1251 | return ret; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1252 | } |
| 1253 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1254 | /* Get current perf-probe event names */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1255 | static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1256 | { |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1257 | char buf[128]; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1258 | struct strlist *sl, *rawlist; |
| 1259 | struct str_node *ent; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1260 | struct kprobe_trace_event tev; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1261 | int ret = 0; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1262 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1263 | memset(&tev, 0, sizeof(tev)); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1264 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1265 | rawlist = get_kprobe_trace_command_rawlist(fd); |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 1266 | sl = strlist__new(true, NULL); |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 1267 | strlist__for_each(ent, rawlist) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1268 | ret = parse_kprobe_trace_command(ent->s, &tev); |
| 1269 | if (ret < 0) |
| 1270 | break; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1271 | if (include_group) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1272 | ret = e_snprintf(buf, 128, "%s:%s", tev.group, |
| 1273 | tev.event); |
| 1274 | if (ret >= 0) |
| 1275 | ret = strlist__add(sl, buf); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1276 | } else |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1277 | ret = strlist__add(sl, tev.event); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1278 | clear_kprobe_trace_event(&tev); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1279 | if (ret < 0) |
| 1280 | break; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1281 | } |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1282 | strlist__delete(rawlist); |
| 1283 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1284 | if (ret < 0) { |
| 1285 | strlist__delete(sl); |
| 1286 | return NULL; |
| 1287 | } |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1288 | return sl; |
| 1289 | } |
| 1290 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1291 | static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1292 | { |
Frederic Weisbecker | 6eca8cc | 2010-04-21 02:01:05 +0200 | [diff] [blame] | 1293 | int ret = 0; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1294 | char *buf = synthesize_kprobe_trace_command(tev); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1295 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1296 | if (!buf) { |
| 1297 | pr_debug("Failed to synthesize kprobe trace event.\n"); |
| 1298 | return -EINVAL; |
| 1299 | } |
| 1300 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1301 | pr_debug("Writing event: %s\n", buf); |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1302 | if (!probe_event_dry_run) { |
| 1303 | ret = write(fd, buf, strlen(buf)); |
| 1304 | if (ret <= 0) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1305 | pr_warning("Failed to write event: %s\n", |
| 1306 | strerror(errno)); |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1307 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1308 | free(buf); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1309 | return ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1310 | } |
| 1311 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1312 | static int get_new_event_name(char *buf, size_t len, const char *base, |
| 1313 | struct strlist *namelist, bool allow_suffix) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1314 | { |
| 1315 | int i, ret; |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 1316 | |
| 1317 | /* Try no suffix */ |
| 1318 | ret = e_snprintf(buf, len, "%s", base); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1319 | if (ret < 0) { |
| 1320 | pr_debug("snprintf() failed: %s\n", strerror(-ret)); |
| 1321 | return ret; |
| 1322 | } |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 1323 | if (!strlist__has_entry(namelist, buf)) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1324 | return 0; |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 1325 | |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 1326 | if (!allow_suffix) { |
| 1327 | pr_warning("Error: event \"%s\" already exists. " |
| 1328 | "(Use -f to force duplicates.)\n", base); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1329 | return -EEXIST; |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 1330 | } |
| 1331 | |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 1332 | /* Try to add suffix */ |
| 1333 | for (i = 1; i < MAX_EVENT_INDEX; i++) { |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1334 | ret = e_snprintf(buf, len, "%s_%d", base, i); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1335 | if (ret < 0) { |
| 1336 | pr_debug("snprintf() failed: %s\n", strerror(-ret)); |
| 1337 | return ret; |
| 1338 | } |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1339 | if (!strlist__has_entry(namelist, buf)) |
| 1340 | break; |
| 1341 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1342 | if (i == MAX_EVENT_INDEX) { |
| 1343 | pr_warning("Too many events are on the same function.\n"); |
| 1344 | ret = -ERANGE; |
| 1345 | } |
| 1346 | |
| 1347 | return ret; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1348 | } |
| 1349 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1350 | static int __add_kprobe_trace_events(struct perf_probe_event *pev, |
| 1351 | struct kprobe_trace_event *tevs, |
| 1352 | int ntevs, bool allow_suffix) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1353 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1354 | int i, fd, ret; |
Ingo Molnar | 5563277 | 2010-03-18 16:51:16 +0100 | [diff] [blame] | 1355 | struct kprobe_trace_event *tev = NULL; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1356 | char buf[64]; |
| 1357 | const char *event, *group; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1358 | struct strlist *namelist; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1359 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1360 | fd = open_kprobe_events(true); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1361 | if (fd < 0) |
| 1362 | return fd; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1363 | /* Get current event names */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1364 | namelist = get_kprobe_trace_event_names(fd, false); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1365 | if (!namelist) { |
| 1366 | pr_debug("Failed to get current event list.\n"); |
| 1367 | return -EIO; |
| 1368 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1369 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1370 | ret = 0; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1371 | printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":"); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1372 | for (i = 0; i < ntevs; i++) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1373 | tev = &tevs[i]; |
| 1374 | if (pev->event) |
| 1375 | event = pev->event; |
| 1376 | else |
| 1377 | if (pev->point.function) |
| 1378 | event = pev->point.function; |
| 1379 | else |
| 1380 | event = tev->point.symbol; |
| 1381 | if (pev->group) |
| 1382 | group = pev->group; |
| 1383 | else |
| 1384 | group = PERFPROBE_GROUP; |
| 1385 | |
| 1386 | /* Get an unused new event name */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1387 | ret = get_new_event_name(buf, 64, event, |
| 1388 | namelist, allow_suffix); |
| 1389 | if (ret < 0) |
| 1390 | break; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1391 | event = buf; |
| 1392 | |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1393 | tev->event = strdup(event); |
| 1394 | tev->group = strdup(group); |
| 1395 | if (tev->event == NULL || tev->group == NULL) { |
| 1396 | ret = -ENOMEM; |
| 1397 | break; |
| 1398 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1399 | ret = write_kprobe_trace_event(fd, tev); |
| 1400 | if (ret < 0) |
| 1401 | break; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1402 | /* Add added event name to namelist */ |
| 1403 | strlist__add(namelist, event); |
| 1404 | |
| 1405 | /* Trick here - save current event/group */ |
| 1406 | event = pev->event; |
| 1407 | group = pev->group; |
| 1408 | pev->event = tev->event; |
| 1409 | pev->group = tev->group; |
| 1410 | show_perf_probe_event(pev); |
| 1411 | /* Trick here - restore current event/group */ |
| 1412 | pev->event = (char *)event; |
| 1413 | pev->group = (char *)group; |
| 1414 | |
| 1415 | /* |
| 1416 | * Probes after the first probe which comes from same |
| 1417 | * user input are always allowed to add suffix, because |
| 1418 | * there might be several addresses corresponding to |
| 1419 | * one code line. |
| 1420 | */ |
| 1421 | allow_suffix = true; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1422 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1423 | |
| 1424 | if (ret >= 0) { |
| 1425 | /* Show how to use the event. */ |
| 1426 | printf("\nYou can now use it on all perf tools, such as:\n\n"); |
| 1427 | printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group, |
| 1428 | tev->event); |
| 1429 | } |
Masami Hiramatsu | a9b495b | 2009-12-08 17:02:47 -0500 | [diff] [blame] | 1430 | |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 1431 | strlist__delete(namelist); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1432 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1433 | return ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1434 | } |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1435 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1436 | static int convert_to_kprobe_trace_events(struct perf_probe_event *pev, |
Masami Hiramatsu | ef4a356 | 2010-04-21 15:56:40 -0400 | [diff] [blame] | 1437 | struct kprobe_trace_event **tevs, |
| 1438 | int max_tevs) |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1439 | { |
| 1440 | struct symbol *sym; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1441 | int ret = 0, i; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1442 | struct kprobe_trace_event *tev; |
| 1443 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 1444 | /* Convert perf_probe_event with debuginfo */ |
Masami Hiramatsu | ef4a356 | 2010-04-21 15:56:40 -0400 | [diff] [blame] | 1445 | ret = try_to_find_kprobe_trace_events(pev, tevs, max_tevs); |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1446 | if (ret != 0) |
| 1447 | return ret; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1448 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1449 | /* Allocate trace event buffer */ |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1450 | tev = *tevs = zalloc(sizeof(struct kprobe_trace_event)); |
| 1451 | if (tev == NULL) |
| 1452 | return -ENOMEM; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1453 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1454 | /* Copy parameters */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1455 | tev->point.symbol = strdup(pev->point.function); |
| 1456 | if (tev->point.symbol == NULL) { |
| 1457 | ret = -ENOMEM; |
| 1458 | goto error; |
| 1459 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1460 | tev->point.offset = pev->point.offset; |
| 1461 | tev->nargs = pev->nargs; |
| 1462 | if (tev->nargs) { |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1463 | tev->args = zalloc(sizeof(struct kprobe_trace_arg) |
| 1464 | * tev->nargs); |
| 1465 | if (tev->args == NULL) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1466 | ret = -ENOMEM; |
| 1467 | goto error; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1468 | } |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1469 | for (i = 0; i < tev->nargs; i++) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1470 | if (pev->args[i].name) { |
| 1471 | tev->args[i].name = strdup(pev->args[i].name); |
| 1472 | if (tev->args[i].name == NULL) { |
| 1473 | ret = -ENOMEM; |
| 1474 | goto error; |
| 1475 | } |
| 1476 | } |
| 1477 | tev->args[i].value = strdup(pev->args[i].var); |
| 1478 | if (tev->args[i].value == NULL) { |
| 1479 | ret = -ENOMEM; |
| 1480 | goto error; |
| 1481 | } |
| 1482 | if (pev->args[i].type) { |
| 1483 | tev->args[i].type = strdup(pev->args[i].type); |
| 1484 | if (tev->args[i].type == NULL) { |
| 1485 | ret = -ENOMEM; |
| 1486 | goto error; |
| 1487 | } |
| 1488 | } |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1489 | } |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1490 | } |
| 1491 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1492 | /* Currently just checking function name from symbol map */ |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1493 | sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION], |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1494 | tev->point.symbol, NULL); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1495 | if (!sym) { |
| 1496 | pr_warning("Kernel symbol \'%s\' not found.\n", |
| 1497 | tev->point.symbol); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1498 | ret = -ENOENT; |
| 1499 | goto error; |
| 1500 | } |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1501 | |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1502 | return 1; |
| 1503 | error: |
| 1504 | clear_kprobe_trace_event(tev); |
| 1505 | free(tev); |
| 1506 | *tevs = NULL; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1507 | return ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | struct __event_package { |
| 1511 | struct perf_probe_event *pev; |
| 1512 | struct kprobe_trace_event *tevs; |
| 1513 | int ntevs; |
| 1514 | }; |
| 1515 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1516 | int add_perf_probe_events(struct perf_probe_event *pevs, int npevs, |
Masami Hiramatsu | ef4a356 | 2010-04-21 15:56:40 -0400 | [diff] [blame] | 1517 | bool force_add, int max_tevs) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1518 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1519 | int i, j, ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1520 | struct __event_package *pkgs; |
| 1521 | |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1522 | pkgs = zalloc(sizeof(struct __event_package) * npevs); |
| 1523 | if (pkgs == NULL) |
| 1524 | return -ENOMEM; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1525 | |
| 1526 | /* Init vmlinux path */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1527 | ret = init_vmlinux(); |
| 1528 | if (ret < 0) |
| 1529 | return ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1530 | |
| 1531 | /* Loop 1: convert all events */ |
| 1532 | for (i = 0; i < npevs; i++) { |
| 1533 | pkgs[i].pev = &pevs[i]; |
| 1534 | /* Convert with or without debuginfo */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1535 | ret = convert_to_kprobe_trace_events(pkgs[i].pev, |
Masami Hiramatsu | ef4a356 | 2010-04-21 15:56:40 -0400 | [diff] [blame] | 1536 | &pkgs[i].tevs, max_tevs); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1537 | if (ret < 0) |
| 1538 | goto end; |
| 1539 | pkgs[i].ntevs = ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | /* Loop 2: add all events */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1543 | for (i = 0; i < npevs && ret >= 0; i++) |
| 1544 | ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs, |
| 1545 | pkgs[i].ntevs, force_add); |
| 1546 | end: |
| 1547 | /* Loop 3: cleanup trace events */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1548 | for (i = 0; i < npevs; i++) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1549 | for (j = 0; j < pkgs[i].ntevs; j++) |
| 1550 | clear_kprobe_trace_event(&pkgs[i].tevs[j]); |
| 1551 | |
| 1552 | return ret; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1553 | } |
| 1554 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1555 | static int __del_trace_kprobe_event(int fd, struct str_node *ent) |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1556 | { |
| 1557 | char *p; |
| 1558 | char buf[128]; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1559 | int ret; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1560 | |
| 1561 | /* Convert from perf-probe event to trace-kprobe event */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1562 | ret = e_snprintf(buf, 128, "-:%s", ent->s); |
| 1563 | if (ret < 0) |
| 1564 | goto error; |
| 1565 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1566 | p = strchr(buf + 2, ':'); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1567 | if (!p) { |
| 1568 | pr_debug("Internal error: %s should have ':' but not.\n", |
| 1569 | ent->s); |
| 1570 | ret = -ENOTSUP; |
| 1571 | goto error; |
| 1572 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1573 | *p = '/'; |
| 1574 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1575 | pr_debug("Writing event: %s\n", buf); |
| 1576 | ret = write(fd, buf, strlen(buf)); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1577 | if (ret < 0) |
| 1578 | goto error; |
| 1579 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1580 | printf("Remove event: %s\n", ent->s); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1581 | return 0; |
| 1582 | error: |
| 1583 | pr_warning("Failed to delete event: %s\n", strerror(-ret)); |
| 1584 | return ret; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1585 | } |
| 1586 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1587 | static int del_trace_kprobe_event(int fd, const char *group, |
| 1588 | const char *event, struct strlist *namelist) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1589 | { |
| 1590 | char buf[128]; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1591 | struct str_node *ent, *n; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1592 | int found = 0, ret = 0; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1593 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1594 | ret = e_snprintf(buf, 128, "%s:%s", group, event); |
| 1595 | if (ret < 0) { |
| 1596 | pr_err("Failed to copy event."); |
| 1597 | return ret; |
| 1598 | } |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1599 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1600 | if (strpbrk(buf, "*?")) { /* Glob-exp */ |
| 1601 | strlist__for_each_safe(ent, n, namelist) |
| 1602 | if (strglobmatch(ent->s, buf)) { |
| 1603 | found++; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1604 | ret = __del_trace_kprobe_event(fd, ent); |
| 1605 | if (ret < 0) |
| 1606 | break; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1607 | strlist__remove(namelist, ent); |
| 1608 | } |
| 1609 | } else { |
| 1610 | ent = strlist__find(namelist, buf); |
| 1611 | if (ent) { |
| 1612 | found++; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1613 | ret = __del_trace_kprobe_event(fd, ent); |
| 1614 | if (ret >= 0) |
| 1615 | strlist__remove(namelist, ent); |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1616 | } |
| 1617 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1618 | if (found == 0 && ret >= 0) |
| 1619 | pr_info("Info: Event \"%s\" does not exist.\n", buf); |
| 1620 | |
| 1621 | return ret; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1622 | } |
| 1623 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1624 | int del_perf_probe_events(struct strlist *dellist) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1625 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1626 | int fd, ret = 0; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1627 | const char *group, *event; |
| 1628 | char *p, *str; |
| 1629 | struct str_node *ent; |
| 1630 | struct strlist *namelist; |
| 1631 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1632 | fd = open_kprobe_events(true); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1633 | if (fd < 0) |
| 1634 | return fd; |
| 1635 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1636 | /* Get current event names */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1637 | namelist = get_kprobe_trace_event_names(fd, true); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1638 | if (namelist == NULL) |
| 1639 | return -EINVAL; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1640 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 1641 | strlist__for_each(ent, dellist) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1642 | str = strdup(ent->s); |
| 1643 | if (str == NULL) { |
| 1644 | ret = -ENOMEM; |
| 1645 | break; |
| 1646 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1647 | pr_debug("Parsing: %s\n", str); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1648 | p = strchr(str, ':'); |
| 1649 | if (p) { |
| 1650 | group = str; |
| 1651 | *p = '\0'; |
| 1652 | event = p + 1; |
| 1653 | } else { |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1654 | group = "*"; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1655 | event = str; |
| 1656 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1657 | pr_debug("Group: %s, Event: %s\n", group, event); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1658 | ret = del_trace_kprobe_event(fd, group, event, namelist); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1659 | free(str); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1660 | if (ret < 0) |
| 1661 | break; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1662 | } |
| 1663 | strlist__delete(namelist); |
| 1664 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1665 | |
| 1666 | return ret; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1667 | } |
| 1668 | |