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 */ |
| 153 | int parse_perf_probe_event(const char *str, struct probe_point *pp) |
| 154 | { |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 155 | char **argv; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 156 | int argc, i, need_dwarf = 0; |
| 157 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 158 | argv = argv_split(str, &argc); |
| 159 | if (!argv) |
| 160 | die("argv_split failed."); |
| 161 | if (argc > MAX_PROBE_ARGS + 1) |
| 162 | semantic_error("Too many arguments"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 163 | |
| 164 | /* Parse probe point */ |
| 165 | parse_perf_probe_probepoint(argv[0], pp); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 166 | if (pp->file || pp->line) |
| 167 | need_dwarf = 1; |
| 168 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 169 | /* Copy arguments and ensure return probe has no C argument */ |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 170 | pp->nr_args = argc - 1; |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 171 | pp->args = zalloc(sizeof(char *) * pp->nr_args); |
| 172 | for (i = 0; i < pp->nr_args; i++) { |
| 173 | pp->args[i] = strdup(argv[i + 1]); |
| 174 | if (!pp->args[i]) |
| 175 | die("Failed to copy argument."); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 176 | if (is_c_varname(pp->args[i])) { |
| 177 | if (pp->retprobe) |
| 178 | semantic_error("You can't specify local" |
| 179 | " variable for kretprobe"); |
| 180 | need_dwarf = 1; |
| 181 | } |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 182 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 183 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 184 | argv_free(argv); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 185 | return need_dwarf; |
| 186 | } |
| 187 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 188 | /* Parse kprobe_events event into struct probe_point */ |
| 189 | void parse_trace_kprobe_event(const char *str, char **group, char **event, |
| 190 | struct probe_point *pp) |
| 191 | { |
| 192 | char pr; |
| 193 | char *p; |
| 194 | int ret, i, argc; |
| 195 | char **argv; |
| 196 | |
| 197 | pr_debug("Parsing kprobe_events: %s\n", str); |
| 198 | argv = argv_split(str, &argc); |
| 199 | if (!argv) |
| 200 | die("argv_split failed."); |
| 201 | if (argc < 2) |
| 202 | semantic_error("Too less arguments."); |
| 203 | |
| 204 | /* Scan event and group name. */ |
Liming Wang | 93aaa45 | 2009-12-02 16:42:54 +0800 | [diff] [blame] | 205 | ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", |
| 206 | &pr, (float *)(void *)group, (float *)(void *)event); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 207 | if (ret != 3) |
| 208 | semantic_error("Failed to parse event name: %s", argv[0]); |
| 209 | pr_debug("Group:%s Event:%s probe:%c\n", *group, *event, pr); |
| 210 | |
| 211 | if (!pp) |
| 212 | goto end; |
| 213 | |
| 214 | pp->retprobe = (pr == 'r'); |
| 215 | |
| 216 | /* Scan function name and offset */ |
Liming Wang | 93aaa45 | 2009-12-02 16:42:54 +0800 | [diff] [blame] | 217 | ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function, &pp->offset); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 218 | if (ret == 1) |
| 219 | pp->offset = 0; |
| 220 | |
| 221 | /* kprobe_events doesn't have this information */ |
| 222 | pp->line = 0; |
| 223 | pp->file = NULL; |
| 224 | |
| 225 | pp->nr_args = argc - 2; |
| 226 | pp->args = zalloc(sizeof(char *) * pp->nr_args); |
| 227 | for (i = 0; i < pp->nr_args; i++) { |
| 228 | p = strchr(argv[i + 2], '='); |
| 229 | if (p) /* We don't need which register is assigned. */ |
| 230 | *p = '\0'; |
| 231 | pp->args[i] = strdup(argv[i + 2]); |
| 232 | if (!pp->args[i]) |
| 233 | die("Failed to copy argument."); |
| 234 | } |
| 235 | |
| 236 | end: |
| 237 | argv_free(argv); |
| 238 | } |
| 239 | |
| 240 | int synthesize_perf_probe_event(struct probe_point *pp) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 241 | { |
| 242 | char *buf; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 243 | char offs[64] = "", line[64] = ""; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 244 | int i, len, ret; |
| 245 | |
| 246 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); |
| 247 | if (!buf) |
| 248 | die("Failed to allocate memory by zalloc."); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 249 | if (pp->offset) { |
| 250 | ret = e_snprintf(offs, 64, "+%d", pp->offset); |
| 251 | if (ret <= 0) |
| 252 | goto error; |
| 253 | } |
| 254 | if (pp->line) { |
| 255 | ret = e_snprintf(line, 64, ":%d", pp->line); |
| 256 | if (ret <= 0) |
| 257 | goto error; |
| 258 | } |
| 259 | |
| 260 | if (pp->function) |
| 261 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function, |
| 262 | offs, pp->retprobe ? "%return" : "", line); |
| 263 | else |
Masami Hiramatsu | 8498845 | 2009-12-07 12:00:53 -0500 | [diff] [blame] | 264 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 265 | if (ret <= 0) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 266 | goto error; |
| 267 | len = ret; |
| 268 | |
| 269 | for (i = 0; i < pp->nr_args; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 270 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
| 271 | pp->args[i]); |
| 272 | if (ret <= 0) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 273 | goto error; |
| 274 | len += ret; |
| 275 | } |
| 276 | pp->found = 1; |
| 277 | |
| 278 | return pp->found; |
| 279 | error: |
| 280 | free(pp->probes[0]); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 281 | |
| 282 | return ret; |
| 283 | } |
| 284 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 285 | int synthesize_trace_kprobe_event(struct probe_point *pp) |
| 286 | { |
| 287 | char *buf; |
| 288 | int i, len, ret; |
| 289 | |
| 290 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); |
| 291 | if (!buf) |
| 292 | die("Failed to allocate memory by zalloc."); |
| 293 | ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset); |
| 294 | if (ret <= 0) |
| 295 | goto error; |
| 296 | len = ret; |
| 297 | |
| 298 | for (i = 0; i < pp->nr_args; i++) { |
| 299 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
| 300 | pp->args[i]); |
| 301 | if (ret <= 0) |
| 302 | goto error; |
| 303 | len += ret; |
| 304 | } |
| 305 | pp->found = 1; |
| 306 | |
| 307 | return pp->found; |
| 308 | error: |
| 309 | free(pp->probes[0]); |
| 310 | |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | static int open_kprobe_events(int flags, int mode) |
| 315 | { |
| 316 | char buf[PATH_MAX]; |
| 317 | int ret; |
| 318 | |
| 319 | ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path); |
| 320 | if (ret < 0) |
| 321 | die("Failed to make kprobe_events path."); |
| 322 | |
| 323 | ret = open(buf, flags, mode); |
| 324 | if (ret < 0) { |
| 325 | if (errno == ENOENT) |
| 326 | die("kprobe_events file does not exist -" |
| 327 | " please rebuild with CONFIG_KPROBE_TRACER."); |
| 328 | else |
| 329 | die("Could not open kprobe_events file: %s", |
| 330 | strerror(errno)); |
| 331 | } |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | /* Get raw string list of current kprobe_events */ |
| 336 | static struct strlist *get_trace_kprobe_event_rawlist(int fd) |
| 337 | { |
| 338 | int ret, idx; |
| 339 | FILE *fp; |
| 340 | char buf[MAX_CMDLEN]; |
| 341 | char *p; |
| 342 | struct strlist *sl; |
| 343 | |
| 344 | sl = strlist__new(true, NULL); |
| 345 | |
| 346 | fp = fdopen(dup(fd), "r"); |
| 347 | while (!feof(fp)) { |
| 348 | p = fgets(buf, MAX_CMDLEN, fp); |
| 349 | if (!p) |
| 350 | break; |
| 351 | |
| 352 | idx = strlen(p) - 1; |
| 353 | if (p[idx] == '\n') |
| 354 | p[idx] = '\0'; |
| 355 | ret = strlist__add(sl, buf); |
| 356 | if (ret < 0) |
| 357 | die("strlist__add failed: %s", strerror(-ret)); |
| 358 | } |
| 359 | fclose(fp); |
| 360 | |
| 361 | return sl; |
| 362 | } |
| 363 | |
| 364 | /* Free and zero clear probe_point */ |
| 365 | static void clear_probe_point(struct probe_point *pp) |
| 366 | { |
| 367 | int i; |
| 368 | |
| 369 | if (pp->function) |
| 370 | free(pp->function); |
| 371 | if (pp->file) |
| 372 | free(pp->file); |
| 373 | for (i = 0; i < pp->nr_args; i++) |
| 374 | free(pp->args[i]); |
| 375 | if (pp->args) |
| 376 | free(pp->args); |
| 377 | for (i = 0; i < pp->found; i++) |
| 378 | free(pp->probes[i]); |
Julia Lawall | 5660ce3 | 2009-12-09 20:26:18 +0100 | [diff] [blame] | 379 | memset(pp, 0, sizeof(*pp)); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 380 | } |
| 381 | |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 382 | /* Show an event */ |
| 383 | static void show_perf_probe_event(const char *group, const char *event, |
| 384 | const char *place, struct probe_point *pp) |
| 385 | { |
| 386 | int i; |
| 387 | char buf[128]; |
| 388 | |
| 389 | e_snprintf(buf, 128, "%s:%s", group, event); |
| 390 | printf(" %-40s (on %s", buf, place); |
| 391 | |
| 392 | if (pp->nr_args > 0) { |
| 393 | printf(" with"); |
| 394 | for (i = 0; i < pp->nr_args; i++) |
| 395 | printf(" %s", pp->args[i]); |
| 396 | } |
| 397 | printf(")\n"); |
| 398 | } |
| 399 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 400 | /* List up current perf-probe events */ |
| 401 | void show_perf_probe_events(void) |
| 402 | { |
| 403 | unsigned int i; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 404 | int fd, nr; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 405 | char *group, *event; |
| 406 | struct probe_point pp; |
| 407 | struct strlist *rawlist; |
| 408 | struct str_node *ent; |
| 409 | |
| 410 | fd = open_kprobe_events(O_RDONLY, 0); |
| 411 | rawlist = get_trace_kprobe_event_rawlist(fd); |
| 412 | close(fd); |
| 413 | |
| 414 | for (i = 0; i < strlist__nr_entries(rawlist); i++) { |
| 415 | ent = strlist__entry(rawlist, i); |
| 416 | parse_trace_kprobe_event(ent->s, &group, &event, &pp); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 417 | /* Synthesize only event probe point */ |
| 418 | nr = pp.nr_args; |
| 419 | pp.nr_args = 0; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 420 | synthesize_perf_probe_event(&pp); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 421 | pp.nr_args = nr; |
| 422 | /* Show an event */ |
| 423 | show_perf_probe_event(group, event, pp.probes[0], &pp); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 424 | free(group); |
| 425 | free(event); |
| 426 | clear_probe_point(&pp); |
| 427 | } |
| 428 | |
| 429 | strlist__delete(rawlist); |
| 430 | } |
| 431 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 432 | /* Get current perf-probe event names */ |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 433 | static struct strlist *get_perf_event_names(int fd, bool include_group) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 434 | { |
| 435 | unsigned int i; |
| 436 | char *group, *event; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 437 | char buf[128]; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 438 | struct strlist *sl, *rawlist; |
| 439 | struct str_node *ent; |
| 440 | |
| 441 | rawlist = get_trace_kprobe_event_rawlist(fd); |
| 442 | |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 443 | sl = strlist__new(true, NULL); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 444 | for (i = 0; i < strlist__nr_entries(rawlist); i++) { |
| 445 | ent = strlist__entry(rawlist, i); |
| 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]; |
| 539 | |
| 540 | if (e_snprintf(buf, 128, "%s:%s", group, event) < 0) |
| 541 | die("Failed to copy event."); |
| 542 | if (!strlist__has_entry(namelist, buf)) { |
| 543 | pr_warning("Warning: event \"%s\" is not found.\n", buf); |
| 544 | return; |
| 545 | } |
| 546 | /* Convert from perf-probe event to trace-kprobe event */ |
| 547 | if (e_snprintf(buf, 128, "-:%s/%s", group, event) < 0) |
| 548 | die("Failed to copy event."); |
| 549 | |
| 550 | write_trace_kprobe_event(fd, buf); |
| 551 | printf("Remove event: %s:%s\n", group, event); |
| 552 | } |
| 553 | |
| 554 | void del_trace_kprobe_events(struct strlist *dellist) |
| 555 | { |
| 556 | int fd; |
| 557 | unsigned int i; |
| 558 | const char *group, *event; |
| 559 | char *p, *str; |
| 560 | struct str_node *ent; |
| 561 | struct strlist *namelist; |
| 562 | |
| 563 | fd = open_kprobe_events(O_RDWR, O_APPEND); |
| 564 | /* Get current event names */ |
| 565 | namelist = get_perf_event_names(fd, true); |
| 566 | |
| 567 | for (i = 0; i < strlist__nr_entries(dellist); i++) { |
| 568 | ent = strlist__entry(dellist, i); |
| 569 | str = strdup(ent->s); |
| 570 | if (!str) |
| 571 | die("Failed to copy event."); |
| 572 | p = strchr(str, ':'); |
| 573 | if (p) { |
| 574 | group = str; |
| 575 | *p = '\0'; |
| 576 | event = p + 1; |
| 577 | } else { |
| 578 | group = PERFPROBE_GROUP; |
| 579 | event = str; |
| 580 | } |
| 581 | del_trace_kprobe_event(fd, group, event, namelist); |
| 582 | free(str); |
| 583 | } |
| 584 | strlist__delete(namelist); |
| 585 | close(fd); |
| 586 | } |
| 587 | |