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