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 |
| 36 | #include "event.h" |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 37 | #include "string.h" |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 38 | #include "strlist.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 39 | #include "debug.h" |
| 40 | #include "parse-events.h" /* For debugfs_path */ |
| 41 | #include "probe-event.h" |
| 42 | |
| 43 | #define MAX_CMDLEN 256 |
| 44 | #define MAX_PROBE_ARGS 128 |
| 45 | #define PERFPROBE_GROUP "probe" |
| 46 | |
| 47 | #define semantic_error(msg ...) die("Semantic error :" msg) |
| 48 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 49 | /* If there is no space to write, returns -E2BIG. */ |
| 50 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
Masami Hiramatsu | 8498845 | 2009-12-07 12:00:53 -0500 | [diff] [blame] | 51 | __attribute__((format(printf, 3, 4))); |
| 52 | |
| 53 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 54 | { |
| 55 | int ret; |
| 56 | va_list ap; |
| 57 | va_start(ap, format); |
| 58 | ret = vsnprintf(str, size, format, ap); |
| 59 | va_end(ap); |
| 60 | if (ret >= (int)size) |
| 61 | ret = -E2BIG; |
| 62 | return ret; |
| 63 | } |
| 64 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 65 | /* Parse probepoint definition. */ |
| 66 | static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) |
| 67 | { |
| 68 | char *ptr, *tmp; |
| 69 | char c, nc = 0; |
| 70 | /* |
| 71 | * <Syntax> |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 72 | * perf probe [EVENT=]SRC:LN |
| 73 | * perf probe [EVENT=]FUNC[+OFFS|%return][@SRC] |
| 74 | * |
| 75 | * TODO:Group name support |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 76 | */ |
| 77 | |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 78 | ptr = strchr(arg, '='); |
| 79 | if (ptr) { /* Event name */ |
| 80 | *ptr = '\0'; |
| 81 | tmp = ptr + 1; |
| 82 | ptr = strchr(arg, ':'); |
| 83 | if (ptr) /* Group name is not supported yet. */ |
| 84 | semantic_error("Group name is not supported yet."); |
| 85 | pp->event = strdup(arg); |
| 86 | arg = tmp; |
| 87 | } |
| 88 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 89 | ptr = strpbrk(arg, ":+@%"); |
| 90 | if (ptr) { |
| 91 | nc = *ptr; |
| 92 | *ptr++ = '\0'; |
| 93 | } |
| 94 | |
| 95 | /* Check arg is function or file and copy it */ |
| 96 | if (strchr(arg, '.')) /* File */ |
| 97 | pp->file = strdup(arg); |
| 98 | else /* Function */ |
| 99 | pp->function = strdup(arg); |
| 100 | DIE_IF(pp->file == NULL && pp->function == NULL); |
| 101 | |
| 102 | /* Parse other options */ |
| 103 | while (ptr) { |
| 104 | arg = ptr; |
| 105 | c = nc; |
| 106 | ptr = strpbrk(arg, ":+@%"); |
| 107 | if (ptr) { |
| 108 | nc = *ptr; |
| 109 | *ptr++ = '\0'; |
| 110 | } |
| 111 | switch (c) { |
| 112 | case ':': /* Line number */ |
| 113 | pp->line = strtoul(arg, &tmp, 0); |
| 114 | if (*tmp != '\0') |
| 115 | semantic_error("There is non-digit charactor" |
| 116 | " in line number."); |
| 117 | break; |
| 118 | case '+': /* Byte offset from a symbol */ |
| 119 | pp->offset = strtoul(arg, &tmp, 0); |
| 120 | if (*tmp != '\0') |
| 121 | semantic_error("There is non-digit charactor" |
| 122 | " in offset."); |
| 123 | break; |
| 124 | case '@': /* File name */ |
| 125 | if (pp->file) |
| 126 | semantic_error("SRC@SRC is not allowed."); |
| 127 | pp->file = strdup(arg); |
| 128 | DIE_IF(pp->file == NULL); |
| 129 | if (ptr) |
| 130 | semantic_error("@SRC must be the last " |
| 131 | "option."); |
| 132 | break; |
| 133 | case '%': /* Probe places */ |
| 134 | if (strcmp(arg, "return") == 0) { |
| 135 | pp->retprobe = 1; |
| 136 | } else /* Others not supported yet */ |
| 137 | semantic_error("%%%s is not supported.", arg); |
| 138 | break; |
| 139 | default: |
| 140 | DIE_IF("Program has a bug."); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /* Exclusion check */ |
| 146 | if (pp->line && pp->offset) |
| 147 | semantic_error("Offset can't be used with line number."); |
| 148 | |
| 149 | if (!pp->line && pp->file && !pp->function) |
| 150 | semantic_error("File always requires line number."); |
| 151 | |
| 152 | if (pp->offset && !pp->function) |
| 153 | semantic_error("Offset requires an entry function."); |
| 154 | |
| 155 | if (pp->retprobe && !pp->function) |
| 156 | semantic_error("Return probe requires an entry function."); |
| 157 | |
| 158 | if ((pp->offset || pp->line) && pp->retprobe) |
| 159 | semantic_error("Offset/Line can't be used with return probe."); |
| 160 | |
| 161 | pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n", |
| 162 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe); |
| 163 | } |
| 164 | |
| 165 | /* Parse perf-probe event definition */ |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 166 | void parse_perf_probe_event(const char *str, struct probe_point *pp, |
| 167 | bool *need_dwarf) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 168 | { |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 169 | char **argv; |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 170 | int argc, i; |
| 171 | |
| 172 | *need_dwarf = false; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 173 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 174 | argv = argv_split(str, &argc); |
| 175 | if (!argv) |
| 176 | die("argv_split failed."); |
| 177 | if (argc > MAX_PROBE_ARGS + 1) |
| 178 | semantic_error("Too many arguments"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 179 | |
| 180 | /* Parse probe point */ |
| 181 | parse_perf_probe_probepoint(argv[0], pp); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 182 | if (pp->file || pp->line) |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 183 | *need_dwarf = true; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 184 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 185 | /* Copy arguments and ensure return probe has no C argument */ |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 186 | pp->nr_args = argc - 1; |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 187 | pp->args = zalloc(sizeof(char *) * pp->nr_args); |
| 188 | for (i = 0; i < pp->nr_args; i++) { |
| 189 | pp->args[i] = strdup(argv[i + 1]); |
| 190 | if (!pp->args[i]) |
| 191 | die("Failed to copy argument."); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 192 | if (is_c_varname(pp->args[i])) { |
| 193 | if (pp->retprobe) |
| 194 | semantic_error("You can't specify local" |
| 195 | " variable for kretprobe"); |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 196 | *need_dwarf = true; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 197 | } |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 198 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 199 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 200 | argv_free(argv); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 201 | } |
| 202 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 203 | /* Parse kprobe_events event into struct probe_point */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 204 | void parse_trace_kprobe_event(const char *str, struct probe_point *pp) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 205 | { |
| 206 | char pr; |
| 207 | char *p; |
| 208 | int ret, i, argc; |
| 209 | char **argv; |
| 210 | |
| 211 | pr_debug("Parsing kprobe_events: %s\n", str); |
| 212 | argv = argv_split(str, &argc); |
| 213 | if (!argv) |
| 214 | die("argv_split failed."); |
| 215 | if (argc < 2) |
| 216 | semantic_error("Too less arguments."); |
| 217 | |
| 218 | /* Scan event and group name. */ |
Liming Wang | 93aaa45 | 2009-12-02 16:42:54 +0800 | [diff] [blame] | 219 | ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 220 | &pr, (float *)(void *)&pp->group, |
| 221 | (float *)(void *)&pp->event); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 222 | if (ret != 3) |
| 223 | semantic_error("Failed to parse event name: %s", argv[0]); |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 224 | pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 225 | |
| 226 | pp->retprobe = (pr == 'r'); |
| 227 | |
| 228 | /* Scan function name and offset */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 229 | ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function, |
| 230 | &pp->offset); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 231 | if (ret == 1) |
| 232 | pp->offset = 0; |
| 233 | |
| 234 | /* kprobe_events doesn't have this information */ |
| 235 | pp->line = 0; |
| 236 | pp->file = NULL; |
| 237 | |
| 238 | pp->nr_args = argc - 2; |
| 239 | pp->args = zalloc(sizeof(char *) * pp->nr_args); |
| 240 | for (i = 0; i < pp->nr_args; i++) { |
| 241 | p = strchr(argv[i + 2], '='); |
| 242 | if (p) /* We don't need which register is assigned. */ |
| 243 | *p = '\0'; |
| 244 | pp->args[i] = strdup(argv[i + 2]); |
| 245 | if (!pp->args[i]) |
| 246 | die("Failed to copy argument."); |
| 247 | } |
| 248 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 249 | argv_free(argv); |
| 250 | } |
| 251 | |
| 252 | int synthesize_perf_probe_event(struct probe_point *pp) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 253 | { |
| 254 | char *buf; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 255 | char offs[64] = "", line[64] = ""; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 256 | int i, len, ret; |
| 257 | |
| 258 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); |
| 259 | if (!buf) |
| 260 | die("Failed to allocate memory by zalloc."); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 261 | if (pp->offset) { |
| 262 | ret = e_snprintf(offs, 64, "+%d", pp->offset); |
| 263 | if (ret <= 0) |
| 264 | goto error; |
| 265 | } |
| 266 | if (pp->line) { |
| 267 | ret = e_snprintf(line, 64, ":%d", pp->line); |
| 268 | if (ret <= 0) |
| 269 | goto error; |
| 270 | } |
| 271 | |
| 272 | if (pp->function) |
| 273 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function, |
| 274 | offs, pp->retprobe ? "%return" : "", line); |
| 275 | else |
Masami Hiramatsu | 8498845 | 2009-12-07 12:00:53 -0500 | [diff] [blame] | 276 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 277 | if (ret <= 0) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 278 | goto error; |
| 279 | len = ret; |
| 280 | |
| 281 | for (i = 0; i < pp->nr_args; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 282 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
| 283 | pp->args[i]); |
| 284 | if (ret <= 0) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 285 | goto error; |
| 286 | len += ret; |
| 287 | } |
| 288 | pp->found = 1; |
| 289 | |
| 290 | return pp->found; |
| 291 | error: |
| 292 | free(pp->probes[0]); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 297 | int synthesize_trace_kprobe_event(struct probe_point *pp) |
| 298 | { |
| 299 | char *buf; |
| 300 | int i, len, ret; |
| 301 | |
| 302 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); |
| 303 | if (!buf) |
| 304 | die("Failed to allocate memory by zalloc."); |
| 305 | ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset); |
| 306 | if (ret <= 0) |
| 307 | goto error; |
| 308 | len = ret; |
| 309 | |
| 310 | for (i = 0; i < pp->nr_args; i++) { |
| 311 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
| 312 | pp->args[i]); |
| 313 | if (ret <= 0) |
| 314 | goto error; |
| 315 | len += ret; |
| 316 | } |
| 317 | pp->found = 1; |
| 318 | |
| 319 | return pp->found; |
| 320 | error: |
| 321 | free(pp->probes[0]); |
| 322 | |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | static int open_kprobe_events(int flags, int mode) |
| 327 | { |
| 328 | char buf[PATH_MAX]; |
| 329 | int ret; |
| 330 | |
| 331 | ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path); |
| 332 | if (ret < 0) |
| 333 | die("Failed to make kprobe_events path."); |
| 334 | |
| 335 | ret = open(buf, flags, mode); |
| 336 | if (ret < 0) { |
| 337 | if (errno == ENOENT) |
| 338 | die("kprobe_events file does not exist -" |
| 339 | " please rebuild with CONFIG_KPROBE_TRACER."); |
| 340 | else |
| 341 | die("Could not open kprobe_events file: %s", |
| 342 | strerror(errno)); |
| 343 | } |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | /* Get raw string list of current kprobe_events */ |
| 348 | static struct strlist *get_trace_kprobe_event_rawlist(int fd) |
| 349 | { |
| 350 | int ret, idx; |
| 351 | FILE *fp; |
| 352 | char buf[MAX_CMDLEN]; |
| 353 | char *p; |
| 354 | struct strlist *sl; |
| 355 | |
| 356 | sl = strlist__new(true, NULL); |
| 357 | |
| 358 | fp = fdopen(dup(fd), "r"); |
| 359 | while (!feof(fp)) { |
| 360 | p = fgets(buf, MAX_CMDLEN, fp); |
| 361 | if (!p) |
| 362 | break; |
| 363 | |
| 364 | idx = strlen(p) - 1; |
| 365 | if (p[idx] == '\n') |
| 366 | p[idx] = '\0'; |
| 367 | ret = strlist__add(sl, buf); |
| 368 | if (ret < 0) |
| 369 | die("strlist__add failed: %s", strerror(-ret)); |
| 370 | } |
| 371 | fclose(fp); |
| 372 | |
| 373 | return sl; |
| 374 | } |
| 375 | |
| 376 | /* Free and zero clear probe_point */ |
| 377 | static void clear_probe_point(struct probe_point *pp) |
| 378 | { |
| 379 | int i; |
| 380 | |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 381 | if (pp->event) |
| 382 | free(pp->event); |
| 383 | if (pp->group) |
| 384 | free(pp->group); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 385 | if (pp->function) |
| 386 | free(pp->function); |
| 387 | if (pp->file) |
| 388 | free(pp->file); |
| 389 | for (i = 0; i < pp->nr_args; i++) |
| 390 | free(pp->args[i]); |
| 391 | if (pp->args) |
| 392 | free(pp->args); |
| 393 | for (i = 0; i < pp->found; i++) |
| 394 | free(pp->probes[i]); |
Julia Lawall | 5660ce3 | 2009-12-09 20:26:18 +0100 | [diff] [blame] | 395 | memset(pp, 0, sizeof(*pp)); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 398 | /* Show an event */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 399 | static void show_perf_probe_event(const char *event, const char *place, |
| 400 | struct probe_point *pp) |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 401 | { |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 402 | int i, ret; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 403 | char buf[128]; |
| 404 | |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 405 | ret = e_snprintf(buf, 128, "%s:%s", pp->group, event); |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 406 | if (ret < 0) |
| 407 | die("Failed to copy event: %s", strerror(-ret)); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 408 | printf(" %-40s (on %s", buf, place); |
| 409 | |
| 410 | if (pp->nr_args > 0) { |
| 411 | printf(" with"); |
| 412 | for (i = 0; i < pp->nr_args; i++) |
| 413 | printf(" %s", pp->args[i]); |
| 414 | } |
| 415 | printf(")\n"); |
| 416 | } |
| 417 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 418 | /* List up current perf-probe events */ |
| 419 | void show_perf_probe_events(void) |
| 420 | { |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 421 | int fd, nr; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 422 | struct probe_point pp; |
| 423 | struct strlist *rawlist; |
| 424 | struct str_node *ent; |
| 425 | |
| 426 | fd = open_kprobe_events(O_RDONLY, 0); |
| 427 | rawlist = get_trace_kprobe_event_rawlist(fd); |
| 428 | close(fd); |
| 429 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 430 | strlist__for_each(ent, rawlist) { |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 431 | parse_trace_kprobe_event(ent->s, &pp); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 432 | /* Synthesize only event probe point */ |
| 433 | nr = pp.nr_args; |
| 434 | pp.nr_args = 0; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 435 | synthesize_perf_probe_event(&pp); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 436 | pp.nr_args = nr; |
| 437 | /* Show an event */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 438 | show_perf_probe_event(pp.event, pp.probes[0], &pp); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 439 | clear_probe_point(&pp); |
| 440 | } |
| 441 | |
| 442 | strlist__delete(rawlist); |
| 443 | } |
| 444 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 445 | /* Get current perf-probe event names */ |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 446 | static struct strlist *get_perf_event_names(int fd, bool include_group) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 447 | { |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 448 | char buf[128]; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 449 | struct strlist *sl, *rawlist; |
| 450 | struct str_node *ent; |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 451 | struct probe_point pp; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 452 | |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 453 | memset(&pp, 0, sizeof(pp)); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 454 | rawlist = get_trace_kprobe_event_rawlist(fd); |
| 455 | |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 456 | sl = strlist__new(true, NULL); |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 457 | strlist__for_each(ent, rawlist) { |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 458 | parse_trace_kprobe_event(ent->s, &pp); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 459 | if (include_group) { |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 460 | if (e_snprintf(buf, 128, "%s:%s", pp.group, |
| 461 | pp.event) < 0) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 462 | die("Failed to copy group:event name."); |
| 463 | strlist__add(sl, buf); |
| 464 | } else |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 465 | strlist__add(sl, pp.event); |
| 466 | clear_probe_point(&pp); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | strlist__delete(rawlist); |
| 470 | |
| 471 | return sl; |
| 472 | } |
| 473 | |
Masami Hiramatsu | a9b495b | 2009-12-08 17:02:47 -0500 | [diff] [blame] | 474 | static void write_trace_kprobe_event(int fd, const char *buf) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 475 | { |
| 476 | int ret; |
| 477 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 478 | pr_debug("Writing event: %s\n", buf); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 479 | ret = write(fd, buf, strlen(buf)); |
| 480 | if (ret <= 0) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 481 | die("Failed to write event: %s", strerror(errno)); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 482 | } |
| 483 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 484 | static void get_new_event_name(char *buf, size_t len, const char *base, |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame^] | 485 | struct strlist *namelist, bool allow_suffix) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 486 | { |
| 487 | int i, ret; |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 488 | |
| 489 | /* Try no suffix */ |
| 490 | ret = e_snprintf(buf, len, "%s", base); |
| 491 | if (ret < 0) |
| 492 | die("snprintf() failed: %s", strerror(-ret)); |
| 493 | if (!strlist__has_entry(namelist, buf)) |
| 494 | return; |
| 495 | |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame^] | 496 | if (!allow_suffix) { |
| 497 | pr_warning("Error: event \"%s\" already exists. " |
| 498 | "(Use -f to force duplicates.)\n", base); |
| 499 | die("Can't add new event."); |
| 500 | } |
| 501 | |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 502 | /* Try to add suffix */ |
| 503 | for (i = 1; i < MAX_EVENT_INDEX; i++) { |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 504 | ret = e_snprintf(buf, len, "%s_%d", base, i); |
| 505 | if (ret < 0) |
| 506 | die("snprintf() failed: %s", strerror(-ret)); |
| 507 | if (!strlist__has_entry(namelist, buf)) |
| 508 | break; |
| 509 | } |
| 510 | if (i == MAX_EVENT_INDEX) |
| 511 | die("Too many events are on the same function."); |
| 512 | } |
| 513 | |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame^] | 514 | void add_trace_kprobe_events(struct probe_point *probes, int nr_probes, |
| 515 | bool force_add) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 516 | { |
| 517 | int i, j, fd; |
| 518 | struct probe_point *pp; |
| 519 | char buf[MAX_CMDLEN]; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 520 | char event[64]; |
| 521 | struct strlist *namelist; |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame^] | 522 | bool allow_suffix; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 523 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 524 | fd = open_kprobe_events(O_RDWR, O_APPEND); |
| 525 | /* Get current event names */ |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 526 | namelist = get_perf_event_names(fd, false); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 527 | |
| 528 | for (j = 0; j < nr_probes; j++) { |
| 529 | pp = probes + j; |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 530 | if (!pp->event) |
| 531 | pp->event = strdup(pp->function); |
| 532 | if (!pp->group) |
| 533 | pp->group = strdup(PERFPROBE_GROUP); |
| 534 | DIE_IF(!pp->event || !pp->group); |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame^] | 535 | /* If force_add is true, suffix search is allowed */ |
| 536 | allow_suffix = force_add; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 537 | for (i = 0; i < pp->found; i++) { |
| 538 | /* Get an unused new event name */ |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame^] | 539 | get_new_event_name(event, 64, pp->event, namelist, |
| 540 | allow_suffix); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 541 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n", |
| 542 | pp->retprobe ? 'r' : 'p', |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 543 | pp->group, event, |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 544 | pp->probes[i]); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 545 | write_trace_kprobe_event(fd, buf); |
Masami Hiramatsu | a9b495b | 2009-12-08 17:02:47 -0500 | [diff] [blame] | 546 | printf("Added new event:\n"); |
| 547 | /* Get the first parameter (probe-point) */ |
| 548 | sscanf(pp->probes[i], "%s", buf); |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 549 | show_perf_probe_event(event, buf, pp); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 550 | /* Add added event name to namelist */ |
| 551 | strlist__add(namelist, event); |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame^] | 552 | /* |
| 553 | * Probes after the first probe which comes from same |
| 554 | * user input are always allowed to add suffix, because |
| 555 | * there might be several addresses corresponding to |
| 556 | * one code line. |
| 557 | */ |
| 558 | allow_suffix = true; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 559 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 560 | } |
Masami Hiramatsu | a9b495b | 2009-12-08 17:02:47 -0500 | [diff] [blame] | 561 | /* Show how to use the event. */ |
| 562 | printf("\nYou can now use it on all perf tools, such as:\n\n"); |
| 563 | printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event); |
| 564 | |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 565 | strlist__delete(namelist); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 566 | close(fd); |
| 567 | } |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 568 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 569 | static void __del_trace_kprobe_event(int fd, struct str_node *ent) |
| 570 | { |
| 571 | char *p; |
| 572 | char buf[128]; |
| 573 | |
| 574 | /* Convert from perf-probe event to trace-kprobe event */ |
| 575 | if (e_snprintf(buf, 128, "-:%s", ent->s) < 0) |
| 576 | die("Failed to copy event."); |
| 577 | p = strchr(buf + 2, ':'); |
| 578 | if (!p) |
| 579 | die("Internal error: %s should have ':' but not.", ent->s); |
| 580 | *p = '/'; |
| 581 | |
| 582 | write_trace_kprobe_event(fd, buf); |
| 583 | printf("Remove event: %s\n", ent->s); |
| 584 | } |
| 585 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 586 | static void del_trace_kprobe_event(int fd, const char *group, |
| 587 | const char *event, struct strlist *namelist) |
| 588 | { |
| 589 | char buf[128]; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 590 | struct str_node *ent, *n; |
| 591 | int found = 0; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 592 | |
| 593 | if (e_snprintf(buf, 128, "%s:%s", group, event) < 0) |
| 594 | die("Failed to copy event."); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 595 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 596 | if (strpbrk(buf, "*?")) { /* Glob-exp */ |
| 597 | strlist__for_each_safe(ent, n, namelist) |
| 598 | if (strglobmatch(ent->s, buf)) { |
| 599 | found++; |
| 600 | __del_trace_kprobe_event(fd, ent); |
| 601 | strlist__remove(namelist, ent); |
| 602 | } |
| 603 | } else { |
| 604 | ent = strlist__find(namelist, buf); |
| 605 | if (ent) { |
| 606 | found++; |
| 607 | __del_trace_kprobe_event(fd, ent); |
| 608 | strlist__remove(namelist, ent); |
| 609 | } |
| 610 | } |
| 611 | if (found == 0) |
| 612 | pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | void del_trace_kprobe_events(struct strlist *dellist) |
| 616 | { |
| 617 | int fd; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 618 | const char *group, *event; |
| 619 | char *p, *str; |
| 620 | struct str_node *ent; |
| 621 | struct strlist *namelist; |
| 622 | |
| 623 | fd = open_kprobe_events(O_RDWR, O_APPEND); |
| 624 | /* Get current event names */ |
| 625 | namelist = get_perf_event_names(fd, true); |
| 626 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 627 | strlist__for_each(ent, dellist) { |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 628 | str = strdup(ent->s); |
| 629 | if (!str) |
| 630 | die("Failed to copy event."); |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 631 | pr_debug("Parsing: %s\n", str); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 632 | p = strchr(str, ':'); |
| 633 | if (p) { |
| 634 | group = str; |
| 635 | *p = '\0'; |
| 636 | event = p + 1; |
| 637 | } else { |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 638 | group = "*"; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 639 | event = str; |
| 640 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 641 | pr_debug("Group: %s, Event: %s\n", group, event); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 642 | del_trace_kprobe_event(fd, group, event, namelist); |
| 643 | free(str); |
| 644 | } |
| 645 | strlist__delete(namelist); |
| 646 | close(fd); |
| 647 | } |
| 648 | |