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