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