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