Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * probe-event.c : perf-probe definition to kprobe_events format converter |
| 3 | * |
| 4 | * Written by Masami Hiramatsu <mhiramat@redhat.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #define _GNU_SOURCE |
| 23 | #include <sys/utsname.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <errno.h> |
| 28 | #include <stdio.h> |
| 29 | #include <unistd.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 32 | #include <stdarg.h> |
| 33 | #include <limits.h> |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 34 | |
| 35 | #undef _GNU_SOURCE |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 36 | #include "util.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 37 | #include "event.h" |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 38 | #include "string.h" |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 39 | #include "strlist.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 40 | #include "debug.h" |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 41 | #include "cache.h" |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 42 | #include "color.h" |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 43 | #include "symbol.h" |
| 44 | #include "thread.h" |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 45 | #include "trace-event.h" /* For __unused */ |
| 46 | #include "parse-events.h" /* For debugfs_path */ |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 47 | #include "probe-event.h" |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 48 | #include "probe-finder.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 49 | |
| 50 | #define MAX_CMDLEN 256 |
| 51 | #define MAX_PROBE_ARGS 128 |
| 52 | #define PERFPROBE_GROUP "probe" |
| 53 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 54 | bool probe_event_dry_run; /* Dry run flag */ |
| 55 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 56 | #define semantic_error(msg ...) pr_err("Semantic error :" msg) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 57 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 58 | /* If there is no space to write, returns -E2BIG. */ |
| 59 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
Masami Hiramatsu | 8498845 | 2009-12-07 12:00:53 -0500 | [diff] [blame] | 60 | __attribute__((format(printf, 3, 4))); |
| 61 | |
| 62 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 63 | { |
| 64 | int ret; |
| 65 | va_list ap; |
| 66 | va_start(ap, format); |
| 67 | ret = vsnprintf(str, size, format, ap); |
| 68 | va_end(ap); |
| 69 | if (ret >= (int)size) |
| 70 | ret = -E2BIG; |
| 71 | return ret; |
| 72 | } |
| 73 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 74 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp); |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 75 | static struct map_groups kmap_groups; |
| 76 | static struct map *kmaps[MAP__NR_TYPES]; |
| 77 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 78 | /* Initialize symbol maps and path of vmlinux */ |
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 | |
| 94 | map_groups__init(&kmap_groups); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 95 | ret = map_groups__create_kernel_maps(&kmap_groups, kmaps); |
| 96 | if (ret < 0) |
| 97 | pr_debug("Failed to create kernel maps.\n"); |
| 98 | |
| 99 | out: |
| 100 | if (ret < 0) |
| 101 | pr_warning("Failed to init vmlinux path.\n"); |
| 102 | return ret; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 105 | #ifdef DWARF_SUPPORT |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 106 | static int open_vmlinux(void) |
| 107 | { |
| 108 | if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) { |
| 109 | pr_debug("Failed to load kernel map.\n"); |
| 110 | return -EINVAL; |
| 111 | } |
| 112 | pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name); |
| 113 | return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY); |
| 114 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 115 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 116 | /* Convert trace point to probe point with debuginfo */ |
| 117 | static int convert_to_perf_probe_point(struct kprobe_trace_point *tp, |
| 118 | struct perf_probe_point *pp) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 119 | { |
| 120 | struct symbol *sym; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 121 | int fd, ret = -ENOENT; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 122 | |
| 123 | sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION], |
| 124 | tp->symbol, NULL); |
| 125 | if (sym) { |
| 126 | fd = open_vmlinux(); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 127 | if (fd >= 0) { |
| 128 | ret = find_perf_probe_point(fd, |
| 129 | sym->start + tp->offset, pp); |
| 130 | close(fd); |
| 131 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 132 | } |
| 133 | if (ret <= 0) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 134 | pr_debug("Failed to find corresponding probes from " |
| 135 | "debuginfo. Use kprobe event information.\n"); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 136 | pp->function = xstrdup(tp->symbol); |
| 137 | pp->offset = tp->offset; |
| 138 | } |
| 139 | pp->retprobe = tp->retprobe; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 140 | |
| 141 | return 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* Try to find perf_probe_event with debuginfo */ |
| 145 | static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev, |
| 146 | struct kprobe_trace_event **tevs) |
| 147 | { |
| 148 | bool need_dwarf = perf_probe_event_need_dwarf(pev); |
| 149 | int fd, ntevs; |
| 150 | |
| 151 | fd = open_vmlinux(); |
| 152 | if (fd < 0) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 153 | if (need_dwarf) { |
| 154 | pr_warning("Failed to open debuginfo file.\n"); |
| 155 | return fd; |
| 156 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 157 | pr_debug("Could not open vmlinux. Try to use symbols.\n"); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* Searching trace events corresponding to probe event */ |
| 162 | ntevs = find_kprobe_trace_events(fd, pev, tevs); |
| 163 | close(fd); |
| 164 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 165 | if (ntevs > 0) { /* Succeeded to find trace events */ |
| 166 | pr_debug("find %d kprobe_trace_events.\n", ntevs); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 167 | return ntevs; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 168 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 169 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 170 | if (ntevs == 0) { /* No error but failed to find probe point. */ |
| 171 | pr_warning("Probe point '%s' not found.\n", |
| 172 | synthesize_perf_probe_point(&pev->point)); |
| 173 | return -ENOENT; |
| 174 | } |
| 175 | /* Error path : ntevs < 0 */ |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 176 | if (need_dwarf) { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 177 | if (ntevs == -EBADF) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 178 | pr_warning("No dwarf info found in the vmlinux - " |
| 179 | "please rebuild with CONFIG_DEBUG_INFO=y.\n"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 180 | return ntevs; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 181 | } |
| 182 | pr_debug("An error occurred in debuginfo analysis." |
| 183 | " Try to use symbols.\n"); |
| 184 | return 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | #define LINEBUF_SIZE 256 |
| 188 | #define NR_ADDITIONAL_LINES 2 |
| 189 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 190 | static int show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 191 | { |
| 192 | char buf[LINEBUF_SIZE]; |
| 193 | const char *color = PERF_COLOR_BLUE; |
| 194 | |
| 195 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
| 196 | goto error; |
| 197 | if (!skip) { |
| 198 | if (show_num) |
| 199 | fprintf(stdout, "%7u %s", l, buf); |
| 200 | else |
| 201 | color_fprintf(stdout, color, " %s", buf); |
| 202 | } |
| 203 | |
| 204 | while (strlen(buf) == LINEBUF_SIZE - 1 && |
| 205 | buf[LINEBUF_SIZE - 2] != '\n') { |
| 206 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
| 207 | goto error; |
| 208 | if (!skip) { |
| 209 | if (show_num) |
| 210 | fprintf(stdout, "%s", buf); |
| 211 | else |
| 212 | color_fprintf(stdout, color, "%s", buf); |
| 213 | } |
| 214 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 215 | |
| 216 | return 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 217 | error: |
| 218 | if (feof(fp)) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 219 | pr_warning("Source file is shorter than expected.\n"); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 220 | else |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 221 | pr_warning("File read error: %s\n", strerror(errno)); |
| 222 | |
| 223 | return -1; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Show line-range always requires debuginfo to find source file and |
| 228 | * line number. |
| 229 | */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 230 | int show_line_range(struct line_range *lr) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 231 | { |
| 232 | unsigned int l = 1; |
| 233 | struct line_node *ln; |
| 234 | FILE *fp; |
| 235 | int fd, ret; |
| 236 | |
| 237 | /* Search a line range */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 238 | ret = init_vmlinux(); |
| 239 | if (ret < 0) |
| 240 | return ret; |
| 241 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 242 | fd = open_vmlinux(); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 243 | if (fd < 0) { |
| 244 | pr_warning("Failed to open debuginfo file.\n"); |
| 245 | return fd; |
| 246 | } |
| 247 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 248 | ret = find_line_range(fd, lr); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 249 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 250 | if (ret == 0) { |
| 251 | pr_warning("Specified source line is not found.\n"); |
| 252 | return -ENOENT; |
| 253 | } else if (ret < 0) { |
| 254 | pr_warning("Debuginfo analysis failed. (%d)\n", ret); |
| 255 | return ret; |
| 256 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 257 | |
| 258 | setup_pager(); |
| 259 | |
| 260 | if (lr->function) |
| 261 | fprintf(stdout, "<%s:%d>\n", lr->function, |
| 262 | lr->start - lr->offset); |
| 263 | else |
| 264 | fprintf(stdout, "<%s:%d>\n", lr->file, lr->start); |
| 265 | |
| 266 | fp = fopen(lr->path, "r"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 267 | if (fp == NULL) { |
| 268 | pr_warning("Failed to open %s: %s\n", lr->path, |
| 269 | strerror(errno)); |
| 270 | return -errno; |
| 271 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 272 | /* Skip to starting line number */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 273 | while (l < lr->start && ret >= 0) |
| 274 | ret = show_one_line(fp, l++, true, false); |
| 275 | if (ret < 0) |
| 276 | goto end; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 277 | |
| 278 | list_for_each_entry(ln, &lr->line_list, list) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 279 | while (ln->line > l && ret >= 0) |
| 280 | ret = show_one_line(fp, (l++) - lr->offset, |
| 281 | false, false); |
| 282 | if (ret >= 0) |
| 283 | ret = show_one_line(fp, (l++) - lr->offset, |
| 284 | false, true); |
| 285 | if (ret < 0) |
| 286 | goto end; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | if (lr->end == INT_MAX) |
| 290 | lr->end = l + NR_ADDITIONAL_LINES; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 291 | while (l < lr->end && !feof(fp) && ret >= 0) |
| 292 | ret = show_one_line(fp, (l++) - lr->offset, false, false); |
| 293 | end: |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 294 | fclose(fp); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 295 | return ret; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | #else /* !DWARF_SUPPORT */ |
| 299 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 300 | static int convert_to_perf_probe_point(struct kprobe_trace_point *tp, |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 301 | struct perf_probe_point *pp) |
| 302 | { |
| 303 | pp->function = xstrdup(tp->symbol); |
| 304 | pp->offset = tp->offset; |
| 305 | pp->retprobe = tp->retprobe; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 306 | |
| 307 | return 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev, |
| 311 | struct kprobe_trace_event **tevs __unused) |
| 312 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 313 | if (perf_probe_event_need_dwarf(pev)) { |
| 314 | pr_warning("Debuginfo-analysis is not supported.\n"); |
| 315 | return -ENOSYS; |
| 316 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 320 | int show_line_range(struct line_range *lr __unused) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 321 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 322 | pr_warning("Debuginfo-analysis is not supported.\n"); |
| 323 | return -ENOSYS; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 324 | } |
| 325 | |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 326 | #endif |
| 327 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 328 | int parse_line_range_desc(const char *arg, struct line_range *lr) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 329 | { |
| 330 | const char *ptr; |
| 331 | char *tmp; |
| 332 | /* |
| 333 | * <Syntax> |
| 334 | * SRC:SLN[+NUM|-ELN] |
| 335 | * FUNC[:SLN[+NUM|-ELN]] |
| 336 | */ |
| 337 | ptr = strchr(arg, ':'); |
| 338 | if (ptr) { |
| 339 | lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0); |
| 340 | if (*tmp == '+') |
| 341 | lr->end = lr->start + (unsigned int)strtoul(tmp + 1, |
| 342 | &tmp, 0); |
| 343 | else if (*tmp == '-') |
| 344 | lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0); |
| 345 | else |
| 346 | lr->end = 0; |
| 347 | pr_debug("Line range is %u to %u\n", lr->start, lr->end); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 348 | if (lr->end && lr->start > lr->end) { |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 349 | semantic_error("Start line must be smaller" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 350 | " than end line.\n"); |
| 351 | return -EINVAL; |
| 352 | } |
| 353 | if (*tmp != '\0') { |
| 354 | semantic_error("Tailing with invalid character '%d'.\n", |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 355 | *tmp); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 356 | return -EINVAL; |
| 357 | } |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 358 | tmp = xstrndup(arg, (ptr - arg)); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 359 | } else |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 360 | tmp = xstrdup(arg); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 361 | |
| 362 | if (strchr(tmp, '.')) |
| 363 | lr->file = tmp; |
| 364 | else |
| 365 | lr->function = tmp; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 366 | |
| 367 | return 0; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 368 | } |
| 369 | |
Masami Hiramatsu | b7702a2 | 2009-12-16 17:24:15 -0500 | [diff] [blame] | 370 | /* Check the name is good for event/group */ |
| 371 | static bool check_event_name(const char *name) |
| 372 | { |
| 373 | if (!isalpha(*name) && *name != '_') |
| 374 | return false; |
| 375 | while (*++name != '\0') { |
| 376 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') |
| 377 | return false; |
| 378 | } |
| 379 | return true; |
| 380 | } |
| 381 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 382 | /* Parse probepoint definition. */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 383 | 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] | 384 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 385 | struct perf_probe_point *pp = &pev->point; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 386 | char *ptr, *tmp; |
| 387 | char c, nc = 0; |
| 388 | /* |
| 389 | * <Syntax> |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 390 | * perf probe [EVENT=]SRC[:LN|;PTN] |
| 391 | * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 392 | * |
| 393 | * TODO:Group name support |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 394 | */ |
| 395 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 396 | ptr = strpbrk(arg, ";=@+%"); |
| 397 | if (ptr && *ptr == '=') { /* Event name */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 398 | *ptr = '\0'; |
| 399 | tmp = ptr + 1; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 400 | if (strchr(arg, ':')) { |
| 401 | semantic_error("Group name is not supported yet.\n"); |
| 402 | return -ENOTSUP; |
| 403 | } |
| 404 | if (!check_event_name(arg)) { |
Masami Hiramatsu | b7702a2 | 2009-12-16 17:24:15 -0500 | [diff] [blame] | 405 | semantic_error("%s is bad for event name -it must " |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 406 | "follow C symbol-naming rule.\n", arg); |
| 407 | return -EINVAL; |
| 408 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 409 | pev->event = xstrdup(arg); |
| 410 | pev->group = NULL; |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 411 | arg = tmp; |
| 412 | } |
| 413 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 414 | ptr = strpbrk(arg, ";:+@%"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 415 | if (ptr) { |
| 416 | nc = *ptr; |
| 417 | *ptr++ = '\0'; |
| 418 | } |
| 419 | |
| 420 | /* Check arg is function or file and copy it */ |
| 421 | if (strchr(arg, '.')) /* File */ |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 422 | pp->file = xstrdup(arg); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 423 | else /* Function */ |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 424 | pp->function = xstrdup(arg); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 425 | |
| 426 | /* Parse other options */ |
| 427 | while (ptr) { |
| 428 | arg = ptr; |
| 429 | c = nc; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 430 | if (c == ';') { /* Lazy pattern must be the last part */ |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 431 | pp->lazy_line = xstrdup(arg); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 432 | break; |
| 433 | } |
| 434 | ptr = strpbrk(arg, ";:+@%"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 435 | if (ptr) { |
| 436 | nc = *ptr; |
| 437 | *ptr++ = '\0'; |
| 438 | } |
| 439 | switch (c) { |
| 440 | case ':': /* Line number */ |
| 441 | pp->line = strtoul(arg, &tmp, 0); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 442 | if (*tmp != '\0') { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 443 | semantic_error("There is non-digit char" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 444 | " in line number.\n"); |
| 445 | return -EINVAL; |
| 446 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 447 | break; |
| 448 | case '+': /* Byte offset from a symbol */ |
| 449 | pp->offset = strtoul(arg, &tmp, 0); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 450 | if (*tmp != '\0') { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 451 | semantic_error("There is non-digit character" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 452 | " in offset.\n"); |
| 453 | return -EINVAL; |
| 454 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 455 | break; |
| 456 | case '@': /* File name */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 457 | if (pp->file) { |
| 458 | semantic_error("SRC@SRC is not allowed.\n"); |
| 459 | return -EINVAL; |
| 460 | } |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 461 | pp->file = xstrdup(arg); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 462 | break; |
| 463 | case '%': /* Probe places */ |
| 464 | if (strcmp(arg, "return") == 0) { |
| 465 | pp->retprobe = 1; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 466 | } else { /* Others not supported yet */ |
| 467 | semantic_error("%%%s is not supported.\n", arg); |
| 468 | return -ENOTSUP; |
| 469 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 470 | break; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 471 | default: /* Buggy case */ |
| 472 | pr_err("This program has a bug at %s:%d.\n", |
| 473 | __FILE__, __LINE__); |
| 474 | return -ENOTSUP; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /* Exclusion check */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 480 | if (pp->lazy_line && pp->line) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 481 | semantic_error("Lazy pattern can't be used with line number."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 482 | return -EINVAL; |
| 483 | } |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 484 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 485 | if (pp->lazy_line && pp->offset) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 486 | semantic_error("Lazy pattern can't be used with offset."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 487 | return -EINVAL; |
| 488 | } |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 489 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 490 | if (pp->line && pp->offset) { |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 491 | semantic_error("Offset can't be used with line number."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 492 | return -EINVAL; |
| 493 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 494 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 495 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 496 | semantic_error("File always requires line number or " |
| 497 | "lazy pattern."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 498 | return -EINVAL; |
| 499 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 500 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 501 | if (pp->offset && !pp->function) { |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 502 | semantic_error("Offset requires an entry function."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 503 | return -EINVAL; |
| 504 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 505 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 506 | if (pp->retprobe && !pp->function) { |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 507 | semantic_error("Return probe requires an entry function."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 508 | return -EINVAL; |
| 509 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 510 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 511 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 512 | semantic_error("Offset/Line/Lazy pattern can't be used with " |
| 513 | "return probe."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 514 | return -EINVAL; |
| 515 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 516 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 517 | 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] | 518 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, |
| 519 | pp->lazy_line); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 520 | return 0; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 521 | } |
| 522 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 523 | /* Parse perf-probe event argument */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 524 | 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] | 525 | { |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 526 | char *tmp; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 527 | struct perf_probe_arg_field **fieldp; |
| 528 | |
| 529 | pr_debug("parsing arg: %s into ", str); |
| 530 | |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 531 | tmp = strchr(str, '='); |
| 532 | if (tmp) { |
| 533 | arg->name = xstrndup(str, tmp - str); |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 534 | pr_debug("name:%s ", arg->name); |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 535 | str = tmp + 1; |
| 536 | } |
| 537 | |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 538 | tmp = strchr(str, ':'); |
| 539 | if (tmp) { /* Type setting */ |
| 540 | *tmp = '\0'; |
| 541 | arg->type = xstrdup(tmp + 1); |
| 542 | pr_debug("type:%s ", arg->type); |
| 543 | } |
| 544 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 545 | tmp = strpbrk(str, "-."); |
| 546 | if (!is_c_varname(str) || !tmp) { |
| 547 | /* A variable, register, symbol or special value */ |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 548 | arg->var = xstrdup(str); |
| 549 | pr_debug("%s\n", arg->var); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 550 | return 0; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /* Structure fields */ |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 554 | arg->var = xstrndup(str, tmp - str); |
| 555 | pr_debug("%s, ", arg->var); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 556 | fieldp = &arg->field; |
| 557 | |
| 558 | do { |
| 559 | *fieldp = xzalloc(sizeof(struct perf_probe_arg_field)); |
| 560 | if (*tmp == '.') { |
| 561 | str = tmp + 1; |
| 562 | (*fieldp)->ref = false; |
| 563 | } else if (tmp[1] == '>') { |
| 564 | str = tmp + 2; |
| 565 | (*fieldp)->ref = true; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 566 | } else { |
| 567 | semantic_error("Argument parse error: %s\n", str); |
| 568 | return -EINVAL; |
| 569 | } |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 570 | |
| 571 | tmp = strpbrk(str, "-."); |
| 572 | if (tmp) { |
| 573 | (*fieldp)->name = xstrndup(str, tmp - str); |
| 574 | pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); |
| 575 | fieldp = &(*fieldp)->next; |
| 576 | } |
| 577 | } while (tmp); |
| 578 | (*fieldp)->name = xstrdup(str); |
| 579 | pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); |
Masami Hiramatsu | df0faf4 | 2010-04-12 13:17:00 -0400 | [diff] [blame] | 580 | |
| 581 | /* If no name is specified, set the last field name */ |
| 582 | if (!arg->name) |
| 583 | arg->name = xstrdup((*fieldp)->name); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 584 | |
| 585 | return 0; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 586 | } |
| 587 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 588 | /* Parse perf-probe event command */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 589 | 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] | 590 | { |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 591 | char **argv; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 592 | int argc, i, ret = 0; |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 593 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 594 | argv = argv_split(cmd, &argc); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 595 | if (!argv) { |
| 596 | pr_debug("Failed to split arguments.\n"); |
| 597 | return -ENOMEM; |
| 598 | } |
| 599 | if (argc - 1 > MAX_PROBE_ARGS) { |
| 600 | semantic_error("Too many probe arguments (%d).\n", argc - 1); |
| 601 | ret = -ERANGE; |
| 602 | goto out; |
| 603 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 604 | /* Parse probe point */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 605 | ret = parse_perf_probe_point(argv[0], pev); |
| 606 | if (ret < 0) |
| 607 | goto out; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 608 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 609 | /* Copy arguments and ensure return probe has no C argument */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 610 | pev->nargs = argc - 1; |
| 611 | pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 612 | for (i = 0; i < pev->nargs && ret >= 0; i++) { |
| 613 | ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); |
| 614 | if (ret >= 0 && |
| 615 | is_c_varname(pev->args[i].var) && pev->point.retprobe) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 616 | semantic_error("You can't specify local variable for" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 617 | " kretprobe.\n"); |
| 618 | ret = -EINVAL; |
| 619 | } |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 620 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 621 | out: |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 622 | argv_free(argv); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 623 | |
| 624 | return ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 625 | } |
| 626 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 627 | /* Return true if this perf_probe_event requires debuginfo */ |
| 628 | bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 629 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 630 | int i; |
| 631 | |
| 632 | if (pev->point.file || pev->point.line || pev->point.lazy_line) |
| 633 | return true; |
| 634 | |
| 635 | for (i = 0; i < pev->nargs; i++) |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 636 | if (is_c_varname(pev->args[i].var)) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 637 | return true; |
| 638 | |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | /* Parse kprobe_events event into struct probe_point */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 643 | int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 644 | { |
| 645 | struct kprobe_trace_point *tp = &tev->point; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 646 | char pr; |
| 647 | char *p; |
| 648 | int ret, i, argc; |
| 649 | char **argv; |
| 650 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 651 | pr_debug("Parsing kprobe_events: %s\n", cmd); |
| 652 | argv = argv_split(cmd, &argc); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 653 | if (!argv) { |
| 654 | pr_debug("Failed to split arguments.\n"); |
| 655 | return -ENOMEM; |
| 656 | } |
| 657 | if (argc < 2) { |
| 658 | semantic_error("Too few probe arguments.\n"); |
| 659 | ret = -ERANGE; |
| 660 | goto out; |
| 661 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 662 | |
| 663 | /* Scan event and group name. */ |
Liming Wang | 93aaa45 | 2009-12-02 16:42:54 +0800 | [diff] [blame] | 664 | ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 665 | &pr, (float *)(void *)&tev->group, |
| 666 | (float *)(void *)&tev->event); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 667 | if (ret != 3) { |
| 668 | semantic_error("Failed to parse event name: %s\n", argv[0]); |
| 669 | ret = -EINVAL; |
| 670 | goto out; |
| 671 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 672 | 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] | 673 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 674 | tp->retprobe = (pr == 'r'); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 675 | |
| 676 | /* Scan function name and offset */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 677 | ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol, |
| 678 | &tp->offset); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 679 | if (ret == 1) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 680 | tp->offset = 0; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 681 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 682 | tev->nargs = argc - 2; |
| 683 | tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs); |
| 684 | for (i = 0; i < tev->nargs; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 685 | p = strchr(argv[i + 2], '='); |
| 686 | if (p) /* We don't need which register is assigned. */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 687 | *p++ = '\0'; |
| 688 | else |
| 689 | p = argv[i + 2]; |
| 690 | tev->args[i].name = xstrdup(argv[i + 2]); |
| 691 | /* TODO: parse regs and offset */ |
| 692 | tev->args[i].value = xstrdup(p); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 693 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 694 | ret = 0; |
| 695 | out: |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 696 | argv_free(argv); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 697 | return ret; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 698 | } |
| 699 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 700 | /* Compose only probe arg */ |
| 701 | int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len) |
| 702 | { |
| 703 | struct perf_probe_arg_field *field = pa->field; |
| 704 | int ret; |
| 705 | char *tmp = buf; |
| 706 | |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 707 | if (pa->name && pa->var) |
| 708 | ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var); |
| 709 | else |
| 710 | ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 711 | if (ret <= 0) |
| 712 | goto error; |
| 713 | tmp += ret; |
| 714 | len -= ret; |
| 715 | |
| 716 | while (field) { |
| 717 | ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".", |
| 718 | field->name); |
| 719 | if (ret <= 0) |
| 720 | goto error; |
| 721 | tmp += ret; |
| 722 | len -= ret; |
| 723 | field = field->next; |
| 724 | } |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 725 | |
| 726 | if (pa->type) { |
| 727 | ret = e_snprintf(tmp, len, ":%s", pa->type); |
| 728 | if (ret <= 0) |
| 729 | goto error; |
| 730 | tmp += ret; |
| 731 | len -= ret; |
| 732 | } |
| 733 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 734 | return tmp - buf; |
| 735 | error: |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 736 | pr_debug("Failed to synthesize perf probe argument: %s", |
| 737 | strerror(-ret)); |
| 738 | return ret; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 739 | } |
| 740 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 741 | /* Compose only probe point (not argument) */ |
| 742 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 743 | { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 744 | char *buf, *tmp; |
| 745 | char offs[32] = "", line[32] = "", file[32] = ""; |
| 746 | int ret, len; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 747 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 748 | buf = xzalloc(MAX_CMDLEN); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 749 | if (pp->offset) { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 750 | ret = e_snprintf(offs, 32, "+%lu", pp->offset); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 751 | if (ret <= 0) |
| 752 | goto error; |
| 753 | } |
| 754 | if (pp->line) { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 755 | ret = e_snprintf(line, 32, ":%d", pp->line); |
| 756 | if (ret <= 0) |
| 757 | goto error; |
| 758 | } |
| 759 | if (pp->file) { |
| 760 | len = strlen(pp->file) - 32; |
| 761 | if (len < 0) |
| 762 | len = 0; |
| 763 | tmp = strchr(pp->file + len, '/'); |
| 764 | if (!tmp) |
| 765 | tmp = pp->file + len - 1; |
| 766 | ret = e_snprintf(file, 32, "@%s", tmp + 1); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 767 | if (ret <= 0) |
| 768 | goto error; |
| 769 | } |
| 770 | |
| 771 | if (pp->function) |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 772 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function, |
| 773 | offs, pp->retprobe ? "%return" : "", line, |
| 774 | file); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 775 | else |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 776 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 777 | if (ret <= 0) |
| 778 | goto error; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 779 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 780 | return buf; |
| 781 | error: |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 782 | pr_debug("Failed to synthesize perf probe point: %s", |
| 783 | strerror(-ret)); |
| 784 | free(buf); |
| 785 | return NULL; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | #if 0 |
| 789 | char *synthesize_perf_probe_command(struct perf_probe_event *pev) |
| 790 | { |
| 791 | char *buf; |
| 792 | int i, len, ret; |
| 793 | |
| 794 | buf = synthesize_perf_probe_point(&pev->point); |
| 795 | if (!buf) |
| 796 | return NULL; |
| 797 | |
| 798 | len = strlen(buf); |
| 799 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 800 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 801 | pev->args[i].name); |
| 802 | if (ret <= 0) { |
| 803 | free(buf); |
| 804 | return NULL; |
| 805 | } |
| 806 | len += ret; |
| 807 | } |
| 808 | |
| 809 | return buf; |
| 810 | } |
| 811 | #endif |
| 812 | |
| 813 | static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref, |
| 814 | char **buf, size_t *buflen, |
| 815 | int depth) |
| 816 | { |
| 817 | int ret; |
| 818 | if (ref->next) { |
| 819 | depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf, |
| 820 | buflen, depth + 1); |
| 821 | if (depth < 0) |
| 822 | goto out; |
| 823 | } |
| 824 | |
| 825 | ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset); |
| 826 | if (ret < 0) |
| 827 | depth = ret; |
| 828 | else { |
| 829 | *buf += ret; |
| 830 | *buflen -= ret; |
| 831 | } |
| 832 | out: |
| 833 | return depth; |
| 834 | |
| 835 | } |
| 836 | |
| 837 | static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg, |
| 838 | char *buf, size_t buflen) |
| 839 | { |
| 840 | int ret, depth = 0; |
| 841 | char *tmp = buf; |
| 842 | |
| 843 | /* Argument name or separator */ |
| 844 | if (arg->name) |
| 845 | ret = e_snprintf(buf, buflen, " %s=", arg->name); |
| 846 | else |
| 847 | ret = e_snprintf(buf, buflen, " "); |
| 848 | if (ret < 0) |
| 849 | return ret; |
| 850 | buf += ret; |
| 851 | buflen -= ret; |
| 852 | |
| 853 | /* Dereferencing arguments */ |
| 854 | if (arg->ref) { |
| 855 | depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf, |
| 856 | &buflen, 1); |
| 857 | if (depth < 0) |
| 858 | return depth; |
| 859 | } |
| 860 | |
| 861 | /* Print argument value */ |
| 862 | ret = e_snprintf(buf, buflen, "%s", arg->value); |
| 863 | if (ret < 0) |
| 864 | return ret; |
| 865 | buf += ret; |
| 866 | buflen -= ret; |
| 867 | |
| 868 | /* Closing */ |
| 869 | while (depth--) { |
| 870 | ret = e_snprintf(buf, buflen, ")"); |
| 871 | if (ret < 0) |
| 872 | return ret; |
| 873 | buf += ret; |
| 874 | buflen -= ret; |
| 875 | } |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 876 | /* Print argument type */ |
| 877 | if (arg->type) { |
| 878 | ret = e_snprintf(buf, buflen, ":%s", arg->type); |
| 879 | if (ret <= 0) |
| 880 | return ret; |
| 881 | buf += ret; |
| 882 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 883 | |
| 884 | return buf - tmp; |
| 885 | } |
| 886 | |
| 887 | char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev) |
| 888 | { |
| 889 | struct kprobe_trace_point *tp = &tev->point; |
| 890 | char *buf; |
| 891 | int i, len, ret; |
| 892 | |
| 893 | buf = xzalloc(MAX_CMDLEN); |
| 894 | len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu", |
| 895 | tp->retprobe ? 'r' : 'p', |
| 896 | tev->group, tev->event, |
| 897 | tp->symbol, tp->offset); |
| 898 | if (len <= 0) |
| 899 | goto error; |
| 900 | |
| 901 | for (i = 0; i < tev->nargs; i++) { |
| 902 | ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len, |
| 903 | MAX_CMDLEN - len); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 904 | if (ret <= 0) |
| 905 | goto error; |
| 906 | len += ret; |
| 907 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 908 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 909 | return buf; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 910 | error: |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 911 | free(buf); |
| 912 | return NULL; |
| 913 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 914 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 915 | int convert_to_perf_probe_event(struct kprobe_trace_event *tev, |
| 916 | struct perf_probe_event *pev) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 917 | { |
| 918 | char buf[64]; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 919 | int i, ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 920 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 921 | /* Convert event/group name */ |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 922 | pev->event = xstrdup(tev->event); |
| 923 | pev->group = xstrdup(tev->group); |
| 924 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 925 | /* Convert trace_point to probe_point */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 926 | ret = convert_to_perf_probe_point(&tev->point, &pev->point); |
| 927 | if (ret < 0) |
| 928 | return ret; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 929 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 930 | /* Convert trace_arg to probe_arg */ |
| 931 | pev->nargs = tev->nargs; |
| 932 | pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 933 | for (i = 0; i < tev->nargs && ret >= 0; i++) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 934 | if (tev->args[i].name) |
| 935 | pev->args[i].name = xstrdup(tev->args[i].name); |
| 936 | else { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 937 | ret = synthesize_kprobe_trace_arg(&tev->args[i], |
| 938 | buf, 64); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 939 | pev->args[i].name = xstrdup(buf); |
| 940 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 941 | |
| 942 | if (ret < 0) |
| 943 | clear_perf_probe_event(pev); |
| 944 | |
| 945 | return ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | void clear_perf_probe_event(struct perf_probe_event *pev) |
| 949 | { |
| 950 | struct perf_probe_point *pp = &pev->point; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 951 | struct perf_probe_arg_field *field, *next; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 952 | int i; |
| 953 | |
| 954 | if (pev->event) |
| 955 | free(pev->event); |
| 956 | if (pev->group) |
| 957 | free(pev->group); |
| 958 | if (pp->file) |
| 959 | free(pp->file); |
| 960 | if (pp->function) |
| 961 | free(pp->function); |
| 962 | if (pp->lazy_line) |
| 963 | free(pp->lazy_line); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 964 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 965 | if (pev->args[i].name) |
| 966 | free(pev->args[i].name); |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 967 | if (pev->args[i].var) |
| 968 | free(pev->args[i].var); |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 969 | if (pev->args[i].type) |
| 970 | free(pev->args[i].type); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 971 | field = pev->args[i].field; |
| 972 | while (field) { |
| 973 | next = field->next; |
| 974 | if (field->name) |
| 975 | free(field->name); |
| 976 | free(field); |
| 977 | field = next; |
| 978 | } |
| 979 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 980 | if (pev->args) |
| 981 | free(pev->args); |
| 982 | memset(pev, 0, sizeof(*pev)); |
| 983 | } |
| 984 | |
| 985 | void clear_kprobe_trace_event(struct kprobe_trace_event *tev) |
| 986 | { |
| 987 | struct kprobe_trace_arg_ref *ref, *next; |
| 988 | int i; |
| 989 | |
| 990 | if (tev->event) |
| 991 | free(tev->event); |
| 992 | if (tev->group) |
| 993 | free(tev->group); |
| 994 | if (tev->point.symbol) |
| 995 | free(tev->point.symbol); |
| 996 | for (i = 0; i < tev->nargs; i++) { |
| 997 | if (tev->args[i].name) |
| 998 | free(tev->args[i].name); |
| 999 | if (tev->args[i].value) |
| 1000 | free(tev->args[i].value); |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 1001 | if (tev->args[i].type) |
| 1002 | free(tev->args[i].type); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1003 | ref = tev->args[i].ref; |
| 1004 | while (ref) { |
| 1005 | next = ref->next; |
| 1006 | free(ref); |
| 1007 | ref = next; |
| 1008 | } |
| 1009 | } |
| 1010 | if (tev->args) |
| 1011 | free(tev->args); |
| 1012 | memset(tev, 0, sizeof(*tev)); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1013 | } |
| 1014 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1015 | static int open_kprobe_events(bool readwrite) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1016 | { |
| 1017 | char buf[PATH_MAX]; |
| 1018 | int ret; |
| 1019 | |
| 1020 | ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1021 | if (ret >= 0) { |
| 1022 | if (readwrite && !probe_event_dry_run) |
| 1023 | ret = open(buf, O_RDWR, O_APPEND); |
| 1024 | else |
| 1025 | ret = open(buf, O_RDONLY, 0); |
| 1026 | } |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1027 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1028 | if (ret < 0) { |
| 1029 | if (errno == ENOENT) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1030 | pr_warning("kprobe_events file does not exist - please" |
| 1031 | " rebuild kernel with CONFIG_KPROBE_EVENT.\n"); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1032 | else |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1033 | pr_warning("Failed to open kprobe_events file: %s\n", |
| 1034 | strerror(errno)); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1035 | } |
| 1036 | return ret; |
| 1037 | } |
| 1038 | |
| 1039 | /* Get raw string list of current kprobe_events */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1040 | static struct strlist *get_kprobe_trace_command_rawlist(int fd) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1041 | { |
| 1042 | int ret, idx; |
| 1043 | FILE *fp; |
| 1044 | char buf[MAX_CMDLEN]; |
| 1045 | char *p; |
| 1046 | struct strlist *sl; |
| 1047 | |
| 1048 | sl = strlist__new(true, NULL); |
| 1049 | |
| 1050 | fp = fdopen(dup(fd), "r"); |
| 1051 | while (!feof(fp)) { |
| 1052 | p = fgets(buf, MAX_CMDLEN, fp); |
| 1053 | if (!p) |
| 1054 | break; |
| 1055 | |
| 1056 | idx = strlen(p) - 1; |
| 1057 | if (p[idx] == '\n') |
| 1058 | p[idx] = '\0'; |
| 1059 | ret = strlist__add(sl, buf); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1060 | if (ret < 0) { |
| 1061 | pr_debug("strlist__add failed: %s\n", strerror(-ret)); |
| 1062 | strlist__delete(sl); |
| 1063 | return NULL; |
| 1064 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1065 | } |
| 1066 | fclose(fp); |
| 1067 | |
| 1068 | return sl; |
| 1069 | } |
| 1070 | |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1071 | /* Show an event */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1072 | static int show_perf_probe_event(struct perf_probe_event *pev) |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1073 | { |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 1074 | int i, ret; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1075 | char buf[128]; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1076 | char *place; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1077 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1078 | /* Synthesize only event probe point */ |
| 1079 | place = synthesize_perf_probe_point(&pev->point); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1080 | if (!place) |
| 1081 | return -EINVAL; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1082 | |
| 1083 | ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event); |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 1084 | if (ret < 0) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1085 | return ret; |
| 1086 | |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1087 | printf(" %-20s (on %s", buf, place); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1088 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1089 | if (pev->nargs > 0) { |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1090 | printf(" with"); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1091 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1092 | ret = synthesize_perf_probe_arg(&pev->args[i], |
| 1093 | buf, 128); |
| 1094 | if (ret < 0) |
| 1095 | break; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1096 | printf(" %s", buf); |
| 1097 | } |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1098 | } |
| 1099 | printf(")\n"); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1100 | free(place); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1101 | return ret; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1102 | } |
| 1103 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1104 | /* List up current perf-probe events */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1105 | int show_perf_probe_events(void) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1106 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1107 | int fd, ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1108 | struct kprobe_trace_event tev; |
| 1109 | struct perf_probe_event pev; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1110 | struct strlist *rawlist; |
| 1111 | struct str_node *ent; |
| 1112 | |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 1113 | setup_pager(); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1114 | ret = init_vmlinux(); |
| 1115 | if (ret < 0) |
| 1116 | return ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1117 | |
| 1118 | memset(&tev, 0, sizeof(tev)); |
| 1119 | memset(&pev, 0, sizeof(pev)); |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 1120 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1121 | fd = open_kprobe_events(false); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1122 | if (fd < 0) |
| 1123 | return fd; |
| 1124 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1125 | rawlist = get_kprobe_trace_command_rawlist(fd); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1126 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1127 | if (!rawlist) |
| 1128 | return -ENOENT; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1129 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 1130 | strlist__for_each(ent, rawlist) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1131 | ret = parse_kprobe_trace_command(ent->s, &tev); |
| 1132 | if (ret >= 0) { |
| 1133 | ret = convert_to_perf_probe_event(&tev, &pev); |
| 1134 | if (ret >= 0) |
| 1135 | ret = show_perf_probe_event(&pev); |
| 1136 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1137 | clear_perf_probe_event(&pev); |
| 1138 | clear_kprobe_trace_event(&tev); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1139 | if (ret < 0) |
| 1140 | break; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1141 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1142 | strlist__delete(rawlist); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1143 | |
| 1144 | return ret; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1145 | } |
| 1146 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1147 | /* Get current perf-probe event names */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1148 | static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1149 | { |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1150 | char buf[128]; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1151 | struct strlist *sl, *rawlist; |
| 1152 | struct str_node *ent; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1153 | struct kprobe_trace_event tev; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1154 | int ret = 0; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1155 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1156 | memset(&tev, 0, sizeof(tev)); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1157 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1158 | rawlist = get_kprobe_trace_command_rawlist(fd); |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 1159 | sl = strlist__new(true, NULL); |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 1160 | strlist__for_each(ent, rawlist) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1161 | ret = parse_kprobe_trace_command(ent->s, &tev); |
| 1162 | if (ret < 0) |
| 1163 | break; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1164 | if (include_group) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1165 | ret = e_snprintf(buf, 128, "%s:%s", tev.group, |
| 1166 | tev.event); |
| 1167 | if (ret >= 0) |
| 1168 | ret = strlist__add(sl, buf); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1169 | } else |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1170 | ret = strlist__add(sl, tev.event); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1171 | clear_kprobe_trace_event(&tev); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1172 | if (ret < 0) |
| 1173 | break; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1174 | } |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1175 | strlist__delete(rawlist); |
| 1176 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1177 | if (ret < 0) { |
| 1178 | strlist__delete(sl); |
| 1179 | return NULL; |
| 1180 | } |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1181 | return sl; |
| 1182 | } |
| 1183 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1184 | static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1185 | { |
| 1186 | int ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1187 | char *buf = synthesize_kprobe_trace_command(tev); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1188 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1189 | if (!buf) { |
| 1190 | pr_debug("Failed to synthesize kprobe trace event.\n"); |
| 1191 | return -EINVAL; |
| 1192 | } |
| 1193 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1194 | pr_debug("Writing event: %s\n", buf); |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1195 | if (!probe_event_dry_run) { |
| 1196 | ret = write(fd, buf, strlen(buf)); |
| 1197 | if (ret <= 0) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1198 | pr_warning("Failed to write event: %s\n", |
| 1199 | strerror(errno)); |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1200 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1201 | free(buf); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1202 | return ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1203 | } |
| 1204 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1205 | static int get_new_event_name(char *buf, size_t len, const char *base, |
| 1206 | struct strlist *namelist, bool allow_suffix) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1207 | { |
| 1208 | int i, ret; |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 1209 | |
| 1210 | /* Try no suffix */ |
| 1211 | ret = e_snprintf(buf, len, "%s", base); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1212 | if (ret < 0) { |
| 1213 | pr_debug("snprintf() failed: %s\n", strerror(-ret)); |
| 1214 | return ret; |
| 1215 | } |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 1216 | if (!strlist__has_entry(namelist, buf)) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1217 | return 0; |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 1218 | |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 1219 | if (!allow_suffix) { |
| 1220 | pr_warning("Error: event \"%s\" already exists. " |
| 1221 | "(Use -f to force duplicates.)\n", base); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1222 | return -EEXIST; |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 1223 | } |
| 1224 | |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 1225 | /* Try to add suffix */ |
| 1226 | for (i = 1; i < MAX_EVENT_INDEX; i++) { |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1227 | ret = e_snprintf(buf, len, "%s_%d", base, i); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1228 | if (ret < 0) { |
| 1229 | pr_debug("snprintf() failed: %s\n", strerror(-ret)); |
| 1230 | return ret; |
| 1231 | } |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1232 | if (!strlist__has_entry(namelist, buf)) |
| 1233 | break; |
| 1234 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1235 | if (i == MAX_EVENT_INDEX) { |
| 1236 | pr_warning("Too many events are on the same function.\n"); |
| 1237 | ret = -ERANGE; |
| 1238 | } |
| 1239 | |
| 1240 | return ret; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1241 | } |
| 1242 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1243 | static int __add_kprobe_trace_events(struct perf_probe_event *pev, |
| 1244 | struct kprobe_trace_event *tevs, |
| 1245 | int ntevs, bool allow_suffix) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1246 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1247 | int i, fd, ret; |
Ingo Molnar | 5563277 | 2010-03-18 16:51:16 +0100 | [diff] [blame] | 1248 | struct kprobe_trace_event *tev = NULL; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1249 | char buf[64]; |
| 1250 | const char *event, *group; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1251 | struct strlist *namelist; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1252 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1253 | fd = open_kprobe_events(true); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1254 | if (fd < 0) |
| 1255 | return fd; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1256 | /* Get current event names */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1257 | namelist = get_kprobe_trace_event_names(fd, false); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1258 | if (!namelist) { |
| 1259 | pr_debug("Failed to get current event list.\n"); |
| 1260 | return -EIO; |
| 1261 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1262 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1263 | ret = 0; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1264 | printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1265 | for (i = 0; i < ntevs && ret >= 0; i++) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1266 | tev = &tevs[i]; |
| 1267 | if (pev->event) |
| 1268 | event = pev->event; |
| 1269 | else |
| 1270 | if (pev->point.function) |
| 1271 | event = pev->point.function; |
| 1272 | else |
| 1273 | event = tev->point.symbol; |
| 1274 | if (pev->group) |
| 1275 | group = pev->group; |
| 1276 | else |
| 1277 | group = PERFPROBE_GROUP; |
| 1278 | |
| 1279 | /* Get an unused new event name */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1280 | ret = get_new_event_name(buf, 64, event, |
| 1281 | namelist, allow_suffix); |
| 1282 | if (ret < 0) |
| 1283 | break; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1284 | event = buf; |
| 1285 | |
| 1286 | tev->event = xstrdup(event); |
| 1287 | tev->group = xstrdup(group); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1288 | ret = write_kprobe_trace_event(fd, tev); |
| 1289 | if (ret < 0) |
| 1290 | break; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1291 | /* Add added event name to namelist */ |
| 1292 | strlist__add(namelist, event); |
| 1293 | |
| 1294 | /* Trick here - save current event/group */ |
| 1295 | event = pev->event; |
| 1296 | group = pev->group; |
| 1297 | pev->event = tev->event; |
| 1298 | pev->group = tev->group; |
| 1299 | show_perf_probe_event(pev); |
| 1300 | /* Trick here - restore current event/group */ |
| 1301 | pev->event = (char *)event; |
| 1302 | pev->group = (char *)group; |
| 1303 | |
| 1304 | /* |
| 1305 | * Probes after the first probe which comes from same |
| 1306 | * user input are always allowed to add suffix, because |
| 1307 | * there might be several addresses corresponding to |
| 1308 | * one code line. |
| 1309 | */ |
| 1310 | allow_suffix = true; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1311 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1312 | |
| 1313 | if (ret >= 0) { |
| 1314 | /* Show how to use the event. */ |
| 1315 | printf("\nYou can now use it on all perf tools, such as:\n\n"); |
| 1316 | printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group, |
| 1317 | tev->event); |
| 1318 | } |
Masami Hiramatsu | a9b495b | 2009-12-08 17:02:47 -0500 | [diff] [blame] | 1319 | |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 1320 | strlist__delete(namelist); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1321 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1322 | return ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1323 | } |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1324 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1325 | static int convert_to_kprobe_trace_events(struct perf_probe_event *pev, |
| 1326 | struct kprobe_trace_event **tevs) |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1327 | { |
| 1328 | struct symbol *sym; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1329 | int ntevs = 0, i; |
| 1330 | struct kprobe_trace_event *tev; |
| 1331 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 1332 | /* Convert perf_probe_event with debuginfo */ |
| 1333 | ntevs = try_to_find_kprobe_trace_events(pev, tevs); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1334 | if (ntevs != 0) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 1335 | return ntevs; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1336 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1337 | /* Allocate trace event buffer */ |
| 1338 | ntevs = 1; |
| 1339 | tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event)); |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1340 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1341 | /* Copy parameters */ |
| 1342 | tev->point.symbol = xstrdup(pev->point.function); |
| 1343 | tev->point.offset = pev->point.offset; |
| 1344 | tev->nargs = pev->nargs; |
| 1345 | if (tev->nargs) { |
| 1346 | tev->args = xzalloc(sizeof(struct kprobe_trace_arg) |
| 1347 | * tev->nargs); |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1348 | for (i = 0; i < tev->nargs; i++) { |
| 1349 | if (pev->args[i].name) |
| 1350 | tev->args[i].name = xstrdup(pev->args[i].name); |
| 1351 | tev->args[i].value = xstrdup(pev->args[i].var); |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 1352 | if (pev->args[i].type) |
| 1353 | tev->args[i].type = xstrdup(pev->args[i].type); |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1354 | } |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1355 | } |
| 1356 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1357 | /* Currently just checking function name from symbol map */ |
| 1358 | sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION], |
| 1359 | tev->point.symbol, NULL); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1360 | if (!sym) { |
| 1361 | pr_warning("Kernel symbol \'%s\' not found.\n", |
| 1362 | tev->point.symbol); |
| 1363 | return -ENOENT; |
| 1364 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1365 | return ntevs; |
| 1366 | } |
| 1367 | |
| 1368 | struct __event_package { |
| 1369 | struct perf_probe_event *pev; |
| 1370 | struct kprobe_trace_event *tevs; |
| 1371 | int ntevs; |
| 1372 | }; |
| 1373 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1374 | int add_perf_probe_events(struct perf_probe_event *pevs, int npevs, |
| 1375 | bool force_add) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1376 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1377 | int i, j, ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1378 | struct __event_package *pkgs; |
| 1379 | |
| 1380 | pkgs = xzalloc(sizeof(struct __event_package) * npevs); |
| 1381 | |
| 1382 | /* Init vmlinux path */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1383 | ret = init_vmlinux(); |
| 1384 | if (ret < 0) |
| 1385 | return ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1386 | |
| 1387 | /* Loop 1: convert all events */ |
| 1388 | for (i = 0; i < npevs; i++) { |
| 1389 | pkgs[i].pev = &pevs[i]; |
| 1390 | /* Convert with or without debuginfo */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1391 | ret = convert_to_kprobe_trace_events(pkgs[i].pev, |
| 1392 | &pkgs[i].tevs); |
| 1393 | if (ret < 0) |
| 1394 | goto end; |
| 1395 | pkgs[i].ntevs = ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | /* Loop 2: add all events */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1399 | for (i = 0; i < npevs && ret >= 0; i++) |
| 1400 | ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs, |
| 1401 | pkgs[i].ntevs, force_add); |
| 1402 | end: |
| 1403 | /* Loop 3: cleanup trace events */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1404 | for (i = 0; i < npevs; i++) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1405 | for (j = 0; j < pkgs[i].ntevs; j++) |
| 1406 | clear_kprobe_trace_event(&pkgs[i].tevs[j]); |
| 1407 | |
| 1408 | return ret; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1409 | } |
| 1410 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1411 | static int __del_trace_kprobe_event(int fd, struct str_node *ent) |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1412 | { |
| 1413 | char *p; |
| 1414 | char buf[128]; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1415 | int ret; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1416 | |
| 1417 | /* Convert from perf-probe event to trace-kprobe event */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1418 | ret = e_snprintf(buf, 128, "-:%s", ent->s); |
| 1419 | if (ret < 0) |
| 1420 | goto error; |
| 1421 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1422 | p = strchr(buf + 2, ':'); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1423 | if (!p) { |
| 1424 | pr_debug("Internal error: %s should have ':' but not.\n", |
| 1425 | ent->s); |
| 1426 | ret = -ENOTSUP; |
| 1427 | goto error; |
| 1428 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1429 | *p = '/'; |
| 1430 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1431 | pr_debug("Writing event: %s\n", buf); |
| 1432 | ret = write(fd, buf, strlen(buf)); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1433 | if (ret < 0) |
| 1434 | goto error; |
| 1435 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1436 | printf("Remove event: %s\n", ent->s); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1437 | return 0; |
| 1438 | error: |
| 1439 | pr_warning("Failed to delete event: %s\n", strerror(-ret)); |
| 1440 | return ret; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1441 | } |
| 1442 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1443 | static int del_trace_kprobe_event(int fd, const char *group, |
| 1444 | const char *event, struct strlist *namelist) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1445 | { |
| 1446 | char buf[128]; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1447 | struct str_node *ent, *n; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1448 | int found = 0, ret = 0; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1449 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1450 | ret = e_snprintf(buf, 128, "%s:%s", group, event); |
| 1451 | if (ret < 0) { |
| 1452 | pr_err("Failed to copy event."); |
| 1453 | return ret; |
| 1454 | } |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1455 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1456 | if (strpbrk(buf, "*?")) { /* Glob-exp */ |
| 1457 | strlist__for_each_safe(ent, n, namelist) |
| 1458 | if (strglobmatch(ent->s, buf)) { |
| 1459 | found++; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1460 | ret = __del_trace_kprobe_event(fd, ent); |
| 1461 | if (ret < 0) |
| 1462 | break; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1463 | strlist__remove(namelist, ent); |
| 1464 | } |
| 1465 | } else { |
| 1466 | ent = strlist__find(namelist, buf); |
| 1467 | if (ent) { |
| 1468 | found++; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1469 | ret = __del_trace_kprobe_event(fd, ent); |
| 1470 | if (ret >= 0) |
| 1471 | strlist__remove(namelist, ent); |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1472 | } |
| 1473 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1474 | if (found == 0 && ret >= 0) |
| 1475 | pr_info("Info: Event \"%s\" does not exist.\n", buf); |
| 1476 | |
| 1477 | return ret; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1478 | } |
| 1479 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1480 | int del_perf_probe_events(struct strlist *dellist) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1481 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1482 | int fd, ret = 0; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1483 | const char *group, *event; |
| 1484 | char *p, *str; |
| 1485 | struct str_node *ent; |
| 1486 | struct strlist *namelist; |
| 1487 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1488 | fd = open_kprobe_events(true); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1489 | if (fd < 0) |
| 1490 | return fd; |
| 1491 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1492 | /* Get current event names */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1493 | namelist = get_kprobe_trace_event_names(fd, true); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1494 | if (namelist == NULL) |
| 1495 | return -EINVAL; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1496 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 1497 | strlist__for_each(ent, dellist) { |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 1498 | str = xstrdup(ent->s); |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1499 | pr_debug("Parsing: %s\n", str); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1500 | p = strchr(str, ':'); |
| 1501 | if (p) { |
| 1502 | group = str; |
| 1503 | *p = '\0'; |
| 1504 | event = p + 1; |
| 1505 | } else { |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1506 | group = "*"; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1507 | event = str; |
| 1508 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1509 | pr_debug("Group: %s, Event: %s\n", group, event); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1510 | ret = del_trace_kprobe_event(fd, group, event, namelist); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1511 | free(str); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1512 | if (ret < 0) |
| 1513 | break; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1514 | } |
| 1515 | strlist__delete(namelist); |
| 1516 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame^] | 1517 | |
| 1518 | return ret; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1519 | } |
| 1520 | |