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