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