Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * builtin-probe.c |
| 3 | * |
| 4 | * Builtin probe command: Set up probe events by C expression |
| 5 | * |
| 6 | * Written by Masami Hiramatsu <mhiramat@redhat.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 | * |
| 22 | */ |
| 23 | #define _GNU_SOURCE |
| 24 | #include <sys/utsname.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <errno.h> |
| 29 | #include <stdio.h> |
| 30 | #include <unistd.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | #undef _GNU_SOURCE |
| 35 | #include "perf.h" |
| 36 | #include "builtin.h" |
| 37 | #include "util/util.h" |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 38 | #include "util/event.h" |
| 39 | #include "util/debug.h" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 40 | #include "util/parse-options.h" |
| 41 | #include "util/parse-events.h" /* For debugfs_path */ |
| 42 | #include "util/probe-finder.h" |
| 43 | |
| 44 | /* Default vmlinux search paths */ |
| 45 | #define NR_SEARCH_PATH 3 |
| 46 | const char *default_search_path[NR_SEARCH_PATH] = { |
| 47 | "/lib/modules/%s/build/vmlinux", /* Custom build kernel */ |
| 48 | "/usr/lib/debug/lib/modules/%s/vmlinux", /* Red Hat debuginfo */ |
| 49 | "/boot/vmlinux-debug-%s", /* Ubuntu */ |
| 50 | }; |
| 51 | |
| 52 | #define MAX_PATH_LEN 256 |
| 53 | #define MAX_PROBES 128 |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 54 | #define MAX_PROBE_ARGS 128 |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 55 | #define PERFPROBE_GROUP "perfprobe" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 56 | |
| 57 | /* Session management structure */ |
| 58 | static struct { |
| 59 | char *vmlinux; |
| 60 | char *release; |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 61 | int need_dwarf; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 62 | int nr_probe; |
| 63 | struct probe_point probes[MAX_PROBES]; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 64 | } session; |
| 65 | |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 66 | #define semantic_error(msg ...) die("Semantic error :" msg) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 67 | |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 68 | /* Parse probe point. Return 1 if return probe */ |
| 69 | static void parse_probe_point(char *arg, struct probe_point *pp) |
| 70 | { |
| 71 | char *ptr, *tmp; |
Arnaldo Carvalho de Melo | 12e4db4 | 2009-11-03 11:29:07 -0200 | [diff] [blame^] | 72 | char c, nc = 0; |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 73 | /* |
| 74 | * <Syntax> |
| 75 | * perf probe SRC:LN |
| 76 | * perf probe FUNC[+OFFS|%return][@SRC] |
| 77 | */ |
| 78 | |
| 79 | ptr = strpbrk(arg, ":+@%"); |
| 80 | if (ptr) { |
| 81 | nc = *ptr; |
| 82 | *ptr++ = '\0'; |
| 83 | } |
| 84 | |
| 85 | /* Check arg is function or file and copy it */ |
| 86 | if (strchr(arg, '.')) /* File */ |
| 87 | pp->file = strdup(arg); |
| 88 | else /* Function */ |
| 89 | pp->function = strdup(arg); |
| 90 | DIE_IF(pp->file == NULL && pp->function == NULL); |
| 91 | |
| 92 | /* Parse other options */ |
| 93 | while (ptr) { |
| 94 | arg = ptr; |
| 95 | c = nc; |
| 96 | ptr = strpbrk(arg, ":+@%"); |
| 97 | if (ptr) { |
| 98 | nc = *ptr; |
| 99 | *ptr++ = '\0'; |
| 100 | } |
| 101 | switch (c) { |
| 102 | case ':': /* Line number */ |
| 103 | pp->line = strtoul(arg, &tmp, 0); |
| 104 | if (*tmp != '\0') |
| 105 | semantic_error("There is non-digit charactor" |
| 106 | " in line number."); |
| 107 | break; |
| 108 | case '+': /* Byte offset from a symbol */ |
| 109 | pp->offset = strtoul(arg, &tmp, 0); |
| 110 | if (*tmp != '\0') |
| 111 | semantic_error("There is non-digit charactor" |
| 112 | " in offset."); |
| 113 | break; |
| 114 | case '@': /* File name */ |
| 115 | if (pp->file) |
| 116 | semantic_error("SRC@SRC is not allowed."); |
| 117 | pp->file = strdup(arg); |
| 118 | DIE_IF(pp->file == NULL); |
| 119 | if (ptr) |
| 120 | semantic_error("@SRC must be the last " |
| 121 | "option."); |
| 122 | break; |
| 123 | case '%': /* Probe places */ |
| 124 | if (strcmp(arg, "return") == 0) { |
| 125 | pp->retprobe = 1; |
| 126 | } else /* Others not supported yet */ |
| 127 | semantic_error("%%%s is not supported.", arg); |
| 128 | break; |
| 129 | default: |
| 130 | DIE_IF("Program has a bug."); |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* Exclusion check */ |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 136 | if (pp->line && pp->offset) |
| 137 | semantic_error("Offset can't be used with line number."); |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 138 | if (!pp->line && pp->file && !pp->function) |
| 139 | semantic_error("File always requires line number."); |
| 140 | if (pp->offset && !pp->function) |
| 141 | semantic_error("Offset requires an entry function."); |
| 142 | if (pp->retprobe && !pp->function) |
| 143 | semantic_error("Return probe requires an entry function."); |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 144 | if ((pp->offset || pp->line) && pp->retprobe) |
| 145 | semantic_error("Offset/Line can't be used with return probe."); |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 146 | |
| 147 | pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n", |
| 148 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe); |
| 149 | } |
| 150 | |
| 151 | /* Parse an event definition. Note that any error must die. */ |
| 152 | static void parse_probe_event(const char *str) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 153 | { |
| 154 | char *argv[MAX_PROBE_ARGS + 2]; /* Event + probe + args */ |
| 155 | int argc, i; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 156 | struct probe_point *pp = &session.probes[session.nr_probe]; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 157 | |
Arnaldo Carvalho de Melo | b7cb10e | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 158 | pr_debug("probe-definition(%d): %s\n", session.nr_probe, str); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 159 | if (++session.nr_probe == MAX_PROBES) |
| 160 | semantic_error("Too many probes"); |
| 161 | |
| 162 | /* Separate arguments, similar to argv_split */ |
| 163 | argc = 0; |
| 164 | do { |
| 165 | /* Skip separators */ |
| 166 | while (isspace(*str)) |
| 167 | str++; |
| 168 | |
| 169 | /* Add an argument */ |
| 170 | if (*str != '\0') { |
| 171 | const char *s = str; |
| 172 | |
| 173 | /* Skip the argument */ |
| 174 | while (!isspace(*str) && *str != '\0') |
| 175 | str++; |
| 176 | |
| 177 | /* Duplicate the argument */ |
| 178 | argv[argc] = strndup(s, str - s); |
| 179 | if (argv[argc] == NULL) |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 180 | die("strndup"); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 181 | if (++argc == MAX_PROBE_ARGS) |
| 182 | semantic_error("Too many arguments"); |
Arnaldo Carvalho de Melo | b7cb10e | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 183 | pr_debug("argv[%d]=%s\n", argc, argv[argc - 1]); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 184 | } |
| 185 | } while (*str != '\0'); |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 186 | if (!argc) |
| 187 | semantic_error("An empty argument."); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 188 | |
| 189 | /* Parse probe point */ |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 190 | parse_probe_point(argv[0], pp); |
| 191 | free(argv[0]); |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 192 | if (pp->file) |
| 193 | session.need_dwarf = 1; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 194 | |
| 195 | /* Copy arguments */ |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 196 | pp->nr_args = argc - 1; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 197 | if (pp->nr_args > 0) { |
| 198 | pp->args = (char **)malloc(sizeof(char *) * pp->nr_args); |
| 199 | if (!pp->args) |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 200 | die("malloc"); |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 201 | memcpy(pp->args, &argv[1], sizeof(char *) * pp->nr_args); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* Ensure return probe has no C argument */ |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 205 | for (i = 0; i < pp->nr_args; i++) |
| 206 | if (is_c_varname(pp->args[i])) { |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 207 | if (pp->retprobe) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 208 | semantic_error("You can't specify local" |
| 209 | " variable for kretprobe"); |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 210 | session.need_dwarf = 1; |
| 211 | } |
| 212 | |
Arnaldo Carvalho de Melo | b7cb10e | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 213 | pr_debug("%d arguments\n", pp->nr_args); |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 216 | static int opt_add_probe_event(const struct option *opt __used, |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 217 | const char *str, int unset __used) |
| 218 | { |
| 219 | if (str) |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 220 | parse_probe_event(str); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 224 | #ifndef NO_LIBDWARF |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 225 | static int open_default_vmlinux(void) |
| 226 | { |
| 227 | struct utsname uts; |
| 228 | char fname[MAX_PATH_LEN]; |
| 229 | int fd, ret, i; |
| 230 | |
| 231 | ret = uname(&uts); |
| 232 | if (ret) { |
Arnaldo Carvalho de Melo | b7cb10e | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 233 | pr_debug("uname() failed.\n"); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 234 | return -errno; |
| 235 | } |
| 236 | session.release = uts.release; |
| 237 | for (i = 0; i < NR_SEARCH_PATH; i++) { |
| 238 | ret = snprintf(fname, MAX_PATH_LEN, |
| 239 | default_search_path[i], session.release); |
| 240 | if (ret >= MAX_PATH_LEN || ret < 0) { |
Arnaldo Carvalho de Melo | b7cb10e | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 241 | pr_debug("Filename(%d,%s) is too long.\n", i, |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 242 | uts.release); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 243 | errno = E2BIG; |
| 244 | return -E2BIG; |
| 245 | } |
Arnaldo Carvalho de Melo | b7cb10e | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 246 | pr_debug("try to open %s\n", fname); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 247 | fd = open(fname, O_RDONLY); |
| 248 | if (fd >= 0) |
| 249 | break; |
| 250 | } |
| 251 | return fd; |
| 252 | } |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 253 | #endif |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 254 | |
| 255 | static const char * const probe_usage[] = { |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 256 | "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]", |
| 257 | "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]", |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 258 | NULL |
| 259 | }; |
| 260 | |
| 261 | static const struct option options[] = { |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 262 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 263 | "be more verbose (show parsed arguments, etc)"), |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 264 | #ifndef NO_LIBDWARF |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 265 | OPT_STRING('k', "vmlinux", &session.vmlinux, "file", |
| 266 | "vmlinux/module pathname"), |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 267 | #endif |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 268 | OPT_CALLBACK('a', "add", NULL, |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 269 | #ifdef NO_LIBDWARF |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 270 | "FUNC[+OFFS|%return] [ARG ...]", |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 271 | #else |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 272 | "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]", |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 273 | #endif |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 274 | "probe point definition, where\n" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 275 | "\t\tGRP:\tGroup name (optional)\n" |
| 276 | "\t\tNAME:\tEvent name\n" |
| 277 | "\t\tFUNC:\tFunction name\n" |
| 278 | "\t\tOFFS:\tOffset from function entry (in byte)\n" |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 279 | "\t\t%return:\tPut the probe at function return\n" |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 280 | #ifdef NO_LIBDWARF |
| 281 | "\t\tARG:\tProbe argument (only \n" |
| 282 | #else |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 283 | "\t\tSRC:\tSource code path\n" |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 284 | "\t\tRLN:\tRelative line number from function entry.\n" |
| 285 | "\t\tALN:\tAbsolute line number in file.\n" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 286 | "\t\tARG:\tProbe argument (local variable name or\n" |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 287 | #endif |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 288 | "\t\t\tkprobe-tracer argument format is supported.)\n", |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 289 | opt_add_probe_event), |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 290 | OPT_END() |
| 291 | }; |
| 292 | |
| 293 | static int write_new_event(int fd, const char *buf) |
| 294 | { |
| 295 | int ret; |
| 296 | |
| 297 | printf("Adding new event: %s\n", buf); |
| 298 | ret = write(fd, buf, strlen(buf)); |
| 299 | if (ret <= 0) |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 300 | die("failed to create event."); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 301 | |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | #define MAX_CMDLEN 256 |
| 306 | |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 307 | static int synthesize_probe_event(struct probe_point *pp) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 308 | { |
| 309 | char *buf; |
| 310 | int i, len, ret; |
| 311 | pp->probes[0] = buf = (char *)calloc(MAX_CMDLEN, sizeof(char)); |
| 312 | if (!buf) |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 313 | die("calloc"); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 314 | ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset); |
| 315 | if (ret <= 0 || ret >= MAX_CMDLEN) |
| 316 | goto error; |
| 317 | len = ret; |
| 318 | |
| 319 | for (i = 0; i < pp->nr_args; i++) { |
| 320 | ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
| 321 | pp->args[i]); |
| 322 | if (ret <= 0 || ret >= MAX_CMDLEN - len) |
| 323 | goto error; |
| 324 | len += ret; |
| 325 | } |
| 326 | pp->found = 1; |
| 327 | return pp->found; |
| 328 | error: |
| 329 | free(pp->probes[0]); |
| 330 | if (ret > 0) |
| 331 | ret = -E2BIG; |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | int cmd_probe(int argc, const char **argv, const char *prefix __used) |
| 336 | { |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 337 | int i, j, fd, ret; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 338 | struct probe_point *pp; |
| 339 | char buf[MAX_CMDLEN]; |
| 340 | |
| 341 | argc = parse_options(argc, argv, options, probe_usage, |
Masami Hiramatsu | 46ab492 | 2009-10-27 16:43:02 -0400 | [diff] [blame] | 342 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 343 | for (i = 0; i < argc; i++) |
| 344 | parse_probe_event(argv[i]); |
| 345 | |
| 346 | if (session.nr_probe == 0) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 347 | usage_with_options(probe_usage, options); |
| 348 | |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 349 | #ifdef NO_LIBDWARF |
| 350 | if (session.need_dwarf) |
| 351 | semantic_error("Dwarf-analysis is not supported"); |
| 352 | #endif |
| 353 | |
| 354 | /* Synthesize probes without dwarf */ |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 355 | for (j = 0; j < session.nr_probe; j++) { |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 356 | #ifndef NO_LIBDWARF |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 357 | if (!session.probes[j].retprobe) { |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 358 | session.need_dwarf = 1; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 359 | continue; |
| 360 | } |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 361 | #endif |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 362 | ret = synthesize_probe_event(&session.probes[j]); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 363 | if (ret == -E2BIG) |
| 364 | semantic_error("probe point is too long."); |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 365 | else if (ret < 0) |
| 366 | die("snprintf"); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 367 | } |
| 368 | |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 369 | #ifndef NO_LIBDWARF |
| 370 | if (!session.need_dwarf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 371 | goto setup_probes; |
| 372 | |
| 373 | if (session.vmlinux) |
| 374 | fd = open(session.vmlinux, O_RDONLY); |
| 375 | else |
| 376 | fd = open_default_vmlinux(); |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 377 | if (fd < 0) |
| 378 | die("vmlinux/module file open"); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 379 | |
| 380 | /* Searching probe points */ |
| 381 | for (j = 0; j < session.nr_probe; j++) { |
| 382 | pp = &session.probes[j]; |
| 383 | if (pp->found) |
| 384 | continue; |
| 385 | |
| 386 | lseek(fd, SEEK_SET, 0); |
| 387 | ret = find_probepoint(fd, pp); |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 388 | if (ret <= 0) |
| 389 | die("No probe point found.\n"); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 390 | } |
| 391 | close(fd); |
| 392 | |
| 393 | setup_probes: |
Masami Hiramatsu | 23e8ec0 | 2009-10-07 18:28:30 -0400 | [diff] [blame] | 394 | #endif /* !NO_LIBDWARF */ |
| 395 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 396 | /* Settng up probe points */ |
| 397 | snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path); |
| 398 | fd = open(buf, O_WRONLY, O_APPEND); |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 399 | if (fd < 0) |
| 400 | die("kprobe_events open"); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 401 | for (j = 0; j < session.nr_probe; j++) { |
| 402 | pp = &session.probes[j]; |
| 403 | if (pp->found == 1) { |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 404 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x %s\n", |
| 405 | pp->retprobe ? 'r' : 'p', PERFPROBE_GROUP, |
| 406 | pp->function, pp->offset, pp->probes[0]); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 407 | write_new_event(fd, buf); |
| 408 | } else |
| 409 | for (i = 0; i < pp->found; i++) { |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 410 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x_%d %s\n", |
| 411 | pp->retprobe ? 'r' : 'p', |
| 412 | PERFPROBE_GROUP, |
| 413 | pp->function, pp->offset, i, |
| 414 | pp->probes[0]); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 415 | write_new_event(fd, buf); |
| 416 | } |
| 417 | } |
| 418 | close(fd); |
| 419 | return 0; |
| 420 | } |
| 421 | |