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> |
| 32 | |
| 33 | #undef _GNU_SOURCE |
| 34 | #include "event.h" |
| 35 | #include "debug.h" |
| 36 | #include "parse-events.h" /* For debugfs_path */ |
| 37 | #include "probe-event.h" |
| 38 | |
| 39 | #define MAX_CMDLEN 256 |
| 40 | #define MAX_PROBE_ARGS 128 |
| 41 | #define PERFPROBE_GROUP "probe" |
| 42 | |
| 43 | #define semantic_error(msg ...) die("Semantic error :" msg) |
| 44 | |
| 45 | /* Parse probepoint definition. */ |
| 46 | static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) |
| 47 | { |
| 48 | char *ptr, *tmp; |
| 49 | char c, nc = 0; |
| 50 | /* |
| 51 | * <Syntax> |
| 52 | * perf probe SRC:LN |
| 53 | * perf probe FUNC[+OFFS|%return][@SRC] |
| 54 | */ |
| 55 | |
| 56 | ptr = strpbrk(arg, ":+@%"); |
| 57 | if (ptr) { |
| 58 | nc = *ptr; |
| 59 | *ptr++ = '\0'; |
| 60 | } |
| 61 | |
| 62 | /* Check arg is function or file and copy it */ |
| 63 | if (strchr(arg, '.')) /* File */ |
| 64 | pp->file = strdup(arg); |
| 65 | else /* Function */ |
| 66 | pp->function = strdup(arg); |
| 67 | DIE_IF(pp->file == NULL && pp->function == NULL); |
| 68 | |
| 69 | /* Parse other options */ |
| 70 | while (ptr) { |
| 71 | arg = ptr; |
| 72 | c = nc; |
| 73 | ptr = strpbrk(arg, ":+@%"); |
| 74 | if (ptr) { |
| 75 | nc = *ptr; |
| 76 | *ptr++ = '\0'; |
| 77 | } |
| 78 | switch (c) { |
| 79 | case ':': /* Line number */ |
| 80 | pp->line = strtoul(arg, &tmp, 0); |
| 81 | if (*tmp != '\0') |
| 82 | semantic_error("There is non-digit charactor" |
| 83 | " in line number."); |
| 84 | break; |
| 85 | case '+': /* Byte offset from a symbol */ |
| 86 | pp->offset = strtoul(arg, &tmp, 0); |
| 87 | if (*tmp != '\0') |
| 88 | semantic_error("There is non-digit charactor" |
| 89 | " in offset."); |
| 90 | break; |
| 91 | case '@': /* File name */ |
| 92 | if (pp->file) |
| 93 | semantic_error("SRC@SRC is not allowed."); |
| 94 | pp->file = strdup(arg); |
| 95 | DIE_IF(pp->file == NULL); |
| 96 | if (ptr) |
| 97 | semantic_error("@SRC must be the last " |
| 98 | "option."); |
| 99 | break; |
| 100 | case '%': /* Probe places */ |
| 101 | if (strcmp(arg, "return") == 0) { |
| 102 | pp->retprobe = 1; |
| 103 | } else /* Others not supported yet */ |
| 104 | semantic_error("%%%s is not supported.", arg); |
| 105 | break; |
| 106 | default: |
| 107 | DIE_IF("Program has a bug."); |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* Exclusion check */ |
| 113 | if (pp->line && pp->offset) |
| 114 | semantic_error("Offset can't be used with line number."); |
| 115 | |
| 116 | if (!pp->line && pp->file && !pp->function) |
| 117 | semantic_error("File always requires line number."); |
| 118 | |
| 119 | if (pp->offset && !pp->function) |
| 120 | semantic_error("Offset requires an entry function."); |
| 121 | |
| 122 | if (pp->retprobe && !pp->function) |
| 123 | semantic_error("Return probe requires an entry function."); |
| 124 | |
| 125 | if ((pp->offset || pp->line) && pp->retprobe) |
| 126 | semantic_error("Offset/Line can't be used with return probe."); |
| 127 | |
| 128 | pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n", |
| 129 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe); |
| 130 | } |
| 131 | |
| 132 | /* Parse perf-probe event definition */ |
| 133 | int parse_perf_probe_event(const char *str, struct probe_point *pp) |
| 134 | { |
| 135 | char *argv[MAX_PROBE_ARGS + 1]; /* probe + args */ |
| 136 | int argc, i, need_dwarf = 0; |
| 137 | |
| 138 | /* Separate arguments, similar to argv_split */ |
| 139 | argc = 0; |
| 140 | do { |
| 141 | /* Skip separators */ |
| 142 | while (isspace(*str)) |
| 143 | str++; |
| 144 | |
| 145 | /* Add an argument */ |
| 146 | if (*str != '\0') { |
| 147 | const char *s = str; |
| 148 | /* Check the limit number of arguments */ |
| 149 | if (argc == MAX_PROBE_ARGS + 1) |
| 150 | semantic_error("Too many arguments"); |
| 151 | |
| 152 | /* Skip the argument */ |
| 153 | while (!isspace(*str) && *str != '\0') |
| 154 | str++; |
| 155 | |
| 156 | /* Duplicate the argument */ |
| 157 | argv[argc] = strndup(s, str - s); |
| 158 | if (argv[argc] == NULL) |
| 159 | die("strndup"); |
| 160 | pr_debug("argv[%d]=%s\n", argc, argv[argc]); |
| 161 | argc++; |
| 162 | } |
| 163 | } while (*str != '\0'); |
| 164 | if (!argc) |
| 165 | semantic_error("An empty argument."); |
| 166 | |
| 167 | /* Parse probe point */ |
| 168 | parse_perf_probe_probepoint(argv[0], pp); |
| 169 | free(argv[0]); |
| 170 | if (pp->file || pp->line) |
| 171 | need_dwarf = 1; |
| 172 | |
| 173 | /* Copy arguments */ |
| 174 | pp->nr_args = argc - 1; |
| 175 | if (pp->nr_args > 0) { |
| 176 | pp->args = (char **)malloc(sizeof(char *) * pp->nr_args); |
| 177 | if (!pp->args) |
| 178 | die("malloc"); |
| 179 | memcpy(pp->args, &argv[1], sizeof(char *) * pp->nr_args); |
| 180 | } |
| 181 | |
| 182 | /* Ensure return probe has no C argument */ |
| 183 | for (i = 0; i < pp->nr_args; i++) |
| 184 | if (is_c_varname(pp->args[i])) { |
| 185 | if (pp->retprobe) |
| 186 | semantic_error("You can't specify local" |
| 187 | " variable for kretprobe"); |
| 188 | need_dwarf = 1; |
| 189 | } |
| 190 | |
| 191 | return need_dwarf; |
| 192 | } |
| 193 | |
| 194 | int synthesize_trace_kprobe_event(struct probe_point *pp) |
| 195 | { |
| 196 | char *buf; |
| 197 | int i, len, ret; |
| 198 | |
| 199 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); |
| 200 | if (!buf) |
| 201 | die("Failed to allocate memory by zalloc."); |
| 202 | ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset); |
| 203 | if (ret <= 0 || ret >= MAX_CMDLEN) |
| 204 | goto error; |
| 205 | len = ret; |
| 206 | |
| 207 | for (i = 0; i < pp->nr_args; i++) { |
| 208 | ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
| 209 | pp->args[i]); |
| 210 | if (ret <= 0 || ret >= MAX_CMDLEN - len) |
| 211 | goto error; |
| 212 | len += ret; |
| 213 | } |
| 214 | pp->found = 1; |
| 215 | |
| 216 | return pp->found; |
| 217 | error: |
| 218 | free(pp->probes[0]); |
| 219 | if (ret > 0) |
| 220 | ret = -E2BIG; |
| 221 | |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | static int write_trace_kprobe_event(int fd, const char *buf) |
| 226 | { |
| 227 | int ret; |
| 228 | |
| 229 | ret = write(fd, buf, strlen(buf)); |
| 230 | if (ret <= 0) |
| 231 | die("Failed to create event."); |
| 232 | else |
| 233 | printf("Added new event: %s\n", buf); |
| 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | void add_trace_kprobe_events(struct probe_point *probes, int nr_probes) |
| 239 | { |
| 240 | int i, j, fd; |
| 241 | struct probe_point *pp; |
| 242 | char buf[MAX_CMDLEN]; |
| 243 | |
| 244 | snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path); |
| 245 | fd = open(buf, O_WRONLY, O_APPEND); |
| 246 | if (fd < 0) { |
| 247 | if (errno == ENOENT) |
| 248 | die("kprobe_events file does not exist -" |
| 249 | " please rebuild with CONFIG_KPROBE_TRACER."); |
| 250 | else |
| 251 | die("Could not open kprobe_events file: %s", |
| 252 | strerror(errno)); |
| 253 | } |
| 254 | |
| 255 | for (j = 0; j < nr_probes; j++) { |
| 256 | pp = probes + j; |
| 257 | if (pp->found == 1) { |
| 258 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x %s\n", |
| 259 | pp->retprobe ? 'r' : 'p', PERFPROBE_GROUP, |
| 260 | pp->function, pp->offset, pp->probes[0]); |
| 261 | write_trace_kprobe_event(fd, buf); |
| 262 | } else |
| 263 | for (i = 0; i < pp->found; i++) { |
| 264 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x_%d %s\n", |
| 265 | pp->retprobe ? 'r' : 'p', |
| 266 | PERFPROBE_GROUP, |
| 267 | pp->function, pp->offset, i, |
| 268 | pp->probes[i]); |
| 269 | write_trace_kprobe_event(fd, buf); |
| 270 | } |
| 271 | } |
| 272 | close(fd); |
| 273 | } |