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