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