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 |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 36 | #include "util.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 37 | #include "event.h" |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 38 | #include "string.h" |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 39 | #include "strlist.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 40 | #include "debug.h" |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 41 | #include "cache.h" |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 42 | #include "color.h" |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 43 | #include "symbol.h" |
| 44 | #include "thread.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 45 | #include "parse-events.h" /* For debugfs_path */ |
| 46 | #include "probe-event.h" |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 47 | #include "probe-finder.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 48 | |
| 49 | #define MAX_CMDLEN 256 |
| 50 | #define MAX_PROBE_ARGS 128 |
| 51 | #define PERFPROBE_GROUP "probe" |
| 52 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 53 | bool probe_event_dry_run; /* Dry run flag */ |
| 54 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 55 | #define semantic_error(msg ...) die("Semantic error :" msg) |
| 56 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 57 | /* If there is no space to write, returns -E2BIG. */ |
| 58 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
Masami Hiramatsu | 8498845 | 2009-12-07 12:00:53 -0500 | [diff] [blame] | 59 | __attribute__((format(printf, 3, 4))); |
| 60 | |
| 61 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 62 | { |
| 63 | int ret; |
| 64 | va_list ap; |
| 65 | va_start(ap, format); |
| 66 | ret = vsnprintf(str, size, format, ap); |
| 67 | va_end(ap); |
| 68 | if (ret >= (int)size) |
| 69 | ret = -E2BIG; |
| 70 | return ret; |
| 71 | } |
| 72 | |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 73 | static struct map_groups kmap_groups; |
| 74 | static struct map *kmaps[MAP__NR_TYPES]; |
| 75 | |
| 76 | /* Initialize symbol maps for vmlinux */ |
| 77 | static void init_vmlinux(void) |
| 78 | { |
| 79 | symbol_conf.sort_by_name = true; |
| 80 | if (symbol_conf.vmlinux_name == NULL) |
| 81 | symbol_conf.try_vmlinux_path = true; |
| 82 | else |
| 83 | pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); |
| 84 | if (symbol__init() < 0) |
| 85 | die("Failed to init symbol map."); |
| 86 | |
| 87 | map_groups__init(&kmap_groups); |
| 88 | if (map_groups__create_kernel_maps(&kmap_groups, kmaps) < 0) |
| 89 | die("Failed to create kernel maps."); |
| 90 | } |
| 91 | |
| 92 | #ifndef NO_DWARF_SUPPORT |
| 93 | static int open_vmlinux(void) |
| 94 | { |
| 95 | if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) { |
| 96 | pr_debug("Failed to load kernel map.\n"); |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name); |
| 100 | return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY); |
| 101 | } |
| 102 | #endif |
| 103 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 104 | void parse_line_range_desc(const char *arg, struct line_range *lr) |
| 105 | { |
| 106 | const char *ptr; |
| 107 | char *tmp; |
| 108 | /* |
| 109 | * <Syntax> |
| 110 | * SRC:SLN[+NUM|-ELN] |
| 111 | * FUNC[:SLN[+NUM|-ELN]] |
| 112 | */ |
| 113 | ptr = strchr(arg, ':'); |
| 114 | if (ptr) { |
| 115 | lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0); |
| 116 | if (*tmp == '+') |
| 117 | lr->end = lr->start + (unsigned int)strtoul(tmp + 1, |
| 118 | &tmp, 0); |
| 119 | else if (*tmp == '-') |
| 120 | lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0); |
| 121 | else |
| 122 | lr->end = 0; |
| 123 | pr_debug("Line range is %u to %u\n", lr->start, lr->end); |
| 124 | if (lr->end && lr->start > lr->end) |
| 125 | semantic_error("Start line must be smaller" |
| 126 | " than end line."); |
| 127 | if (*tmp != '\0') |
| 128 | semantic_error("Tailing with invalid character '%d'.", |
| 129 | *tmp); |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 130 | tmp = xstrndup(arg, (ptr - arg)); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 131 | } else |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 132 | tmp = xstrdup(arg); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 133 | |
| 134 | if (strchr(tmp, '.')) |
| 135 | lr->file = tmp; |
| 136 | else |
| 137 | lr->function = tmp; |
| 138 | } |
| 139 | |
Masami Hiramatsu | b7702a2 | 2009-12-16 17:24:15 -0500 | [diff] [blame] | 140 | /* Check the name is good for event/group */ |
| 141 | static bool check_event_name(const char *name) |
| 142 | { |
| 143 | if (!isalpha(*name) && *name != '_') |
| 144 | return false; |
| 145 | while (*++name != '\0') { |
| 146 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') |
| 147 | return false; |
| 148 | } |
| 149 | return true; |
| 150 | } |
| 151 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 152 | /* Parse probepoint definition. */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 153 | static void parse_perf_probe_point(char *arg, struct perf_probe_event *pev) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 154 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 155 | struct perf_probe_point *pp = &pev->point; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 156 | char *ptr, *tmp; |
| 157 | char c, nc = 0; |
| 158 | /* |
| 159 | * <Syntax> |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 160 | * perf probe [EVENT=]SRC[:LN|;PTN] |
| 161 | * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 162 | * |
| 163 | * TODO:Group name support |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 164 | */ |
| 165 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 166 | ptr = strpbrk(arg, ";=@+%"); |
| 167 | if (ptr && *ptr == '=') { /* Event name */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 168 | *ptr = '\0'; |
| 169 | tmp = ptr + 1; |
| 170 | ptr = strchr(arg, ':'); |
| 171 | if (ptr) /* Group name is not supported yet. */ |
| 172 | semantic_error("Group name is not supported yet."); |
Masami Hiramatsu | b7702a2 | 2009-12-16 17:24:15 -0500 | [diff] [blame] | 173 | if (!check_event_name(arg)) |
| 174 | semantic_error("%s is bad for event name -it must " |
| 175 | "follow C symbol-naming rule.", arg); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 176 | pev->event = xstrdup(arg); |
| 177 | pev->group = NULL; |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 178 | arg = tmp; |
| 179 | } |
| 180 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 181 | ptr = strpbrk(arg, ";:+@%"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 182 | if (ptr) { |
| 183 | nc = *ptr; |
| 184 | *ptr++ = '\0'; |
| 185 | } |
| 186 | |
| 187 | /* Check arg is function or file and copy it */ |
| 188 | if (strchr(arg, '.')) /* File */ |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 189 | pp->file = xstrdup(arg); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 190 | else /* Function */ |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 191 | pp->function = xstrdup(arg); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 192 | |
| 193 | /* Parse other options */ |
| 194 | while (ptr) { |
| 195 | arg = ptr; |
| 196 | c = nc; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 197 | if (c == ';') { /* Lazy pattern must be the last part */ |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 198 | pp->lazy_line = xstrdup(arg); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 199 | break; |
| 200 | } |
| 201 | ptr = strpbrk(arg, ";:+@%"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 202 | if (ptr) { |
| 203 | nc = *ptr; |
| 204 | *ptr++ = '\0'; |
| 205 | } |
| 206 | switch (c) { |
| 207 | case ':': /* Line number */ |
| 208 | pp->line = strtoul(arg, &tmp, 0); |
| 209 | if (*tmp != '\0') |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 210 | semantic_error("There is non-digit char" |
| 211 | " in line number."); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 212 | break; |
| 213 | case '+': /* Byte offset from a symbol */ |
| 214 | pp->offset = strtoul(arg, &tmp, 0); |
| 215 | if (*tmp != '\0') |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 216 | semantic_error("There is non-digit character" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 217 | " in offset."); |
| 218 | break; |
| 219 | case '@': /* File name */ |
| 220 | if (pp->file) |
| 221 | semantic_error("SRC@SRC is not allowed."); |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 222 | pp->file = xstrdup(arg); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 223 | break; |
| 224 | case '%': /* Probe places */ |
| 225 | if (strcmp(arg, "return") == 0) { |
| 226 | pp->retprobe = 1; |
| 227 | } else /* Others not supported yet */ |
| 228 | semantic_error("%%%s is not supported.", arg); |
| 229 | break; |
| 230 | default: |
| 231 | DIE_IF("Program has a bug."); |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /* Exclusion check */ |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 237 | if (pp->lazy_line && pp->line) |
| 238 | semantic_error("Lazy pattern can't be used with line number."); |
| 239 | |
| 240 | if (pp->lazy_line && pp->offset) |
| 241 | semantic_error("Lazy pattern can't be used with offset."); |
| 242 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 243 | if (pp->line && pp->offset) |
| 244 | semantic_error("Offset can't be used with line number."); |
| 245 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 246 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) |
| 247 | semantic_error("File always requires line number or " |
| 248 | "lazy pattern."); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 249 | |
| 250 | if (pp->offset && !pp->function) |
| 251 | semantic_error("Offset requires an entry function."); |
| 252 | |
| 253 | if (pp->retprobe && !pp->function) |
| 254 | semantic_error("Return probe requires an entry function."); |
| 255 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 256 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) |
| 257 | semantic_error("Offset/Line/Lazy pattern can't be used with " |
| 258 | "return probe."); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 259 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 260 | pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 261 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, |
| 262 | pp->lazy_line); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 263 | } |
| 264 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame^] | 265 | /* Parse perf-probe event argument */ |
| 266 | static void parse_perf_probe_arg(const char *str, struct perf_probe_arg *arg) |
| 267 | { |
| 268 | const char *tmp; |
| 269 | struct perf_probe_arg_field **fieldp; |
| 270 | |
| 271 | pr_debug("parsing arg: %s into ", str); |
| 272 | |
| 273 | tmp = strpbrk(str, "-."); |
| 274 | if (!is_c_varname(str) || !tmp) { |
| 275 | /* A variable, register, symbol or special value */ |
| 276 | arg->name = xstrdup(str); |
| 277 | pr_debug("%s\n", arg->name); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | /* Structure fields */ |
| 282 | arg->name = xstrndup(str, tmp - str); |
| 283 | pr_debug("%s, ", arg->name); |
| 284 | fieldp = &arg->field; |
| 285 | |
| 286 | do { |
| 287 | *fieldp = xzalloc(sizeof(struct perf_probe_arg_field)); |
| 288 | if (*tmp == '.') { |
| 289 | str = tmp + 1; |
| 290 | (*fieldp)->ref = false; |
| 291 | } else if (tmp[1] == '>') { |
| 292 | str = tmp + 2; |
| 293 | (*fieldp)->ref = true; |
| 294 | } else |
| 295 | semantic_error("Argument parse error: %s", str); |
| 296 | |
| 297 | tmp = strpbrk(str, "-."); |
| 298 | if (tmp) { |
| 299 | (*fieldp)->name = xstrndup(str, tmp - str); |
| 300 | pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); |
| 301 | fieldp = &(*fieldp)->next; |
| 302 | } |
| 303 | } while (tmp); |
| 304 | (*fieldp)->name = xstrdup(str); |
| 305 | pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); |
| 306 | } |
| 307 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 308 | /* Parse perf-probe event command */ |
| 309 | void parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 310 | { |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 311 | char **argv; |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 312 | int argc, i; |
| 313 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 314 | argv = argv_split(cmd, &argc); |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 315 | if (!argv) |
| 316 | die("argv_split failed."); |
| 317 | if (argc > MAX_PROBE_ARGS + 1) |
| 318 | semantic_error("Too many arguments"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 319 | |
| 320 | /* Parse probe point */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 321 | parse_perf_probe_point(argv[0], pev); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 322 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 323 | /* Copy arguments and ensure return probe has no C argument */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 324 | pev->nargs = argc - 1; |
| 325 | pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
| 326 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame^] | 327 | parse_perf_probe_arg(argv[i + 1], &pev->args[i]); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 328 | if (is_c_varname(pev->args[i].name) && pev->point.retprobe) |
| 329 | semantic_error("You can't specify local variable for" |
| 330 | " kretprobe"); |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 331 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 332 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 333 | argv_free(argv); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 336 | /* Return true if this perf_probe_event requires debuginfo */ |
| 337 | bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 338 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 339 | int i; |
| 340 | |
| 341 | if (pev->point.file || pev->point.line || pev->point.lazy_line) |
| 342 | return true; |
| 343 | |
| 344 | for (i = 0; i < pev->nargs; i++) |
| 345 | if (is_c_varname(pev->args[i].name)) |
| 346 | return true; |
| 347 | |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | /* Parse kprobe_events event into struct probe_point */ |
| 352 | void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev) |
| 353 | { |
| 354 | struct kprobe_trace_point *tp = &tev->point; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 355 | char pr; |
| 356 | char *p; |
| 357 | int ret, i, argc; |
| 358 | char **argv; |
| 359 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 360 | pr_debug("Parsing kprobe_events: %s\n", cmd); |
| 361 | argv = argv_split(cmd, &argc); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 362 | if (!argv) |
| 363 | die("argv_split failed."); |
| 364 | if (argc < 2) |
| 365 | semantic_error("Too less arguments."); |
| 366 | |
| 367 | /* Scan event and group name. */ |
Liming Wang | 93aaa45 | 2009-12-02 16:42:54 +0800 | [diff] [blame] | 368 | ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 369 | &pr, (float *)(void *)&tev->group, |
| 370 | (float *)(void *)&tev->event); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 371 | if (ret != 3) |
| 372 | semantic_error("Failed to parse event name: %s", argv[0]); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 373 | pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 374 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 375 | tp->retprobe = (pr == 'r'); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 376 | |
| 377 | /* Scan function name and offset */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 378 | ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol, |
| 379 | &tp->offset); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 380 | if (ret == 1) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 381 | tp->offset = 0; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 382 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 383 | tev->nargs = argc - 2; |
| 384 | tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs); |
| 385 | for (i = 0; i < tev->nargs; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 386 | p = strchr(argv[i + 2], '='); |
| 387 | if (p) /* We don't need which register is assigned. */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 388 | *p++ = '\0'; |
| 389 | else |
| 390 | p = argv[i + 2]; |
| 391 | tev->args[i].name = xstrdup(argv[i + 2]); |
| 392 | /* TODO: parse regs and offset */ |
| 393 | tev->args[i].value = xstrdup(p); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 394 | } |
| 395 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 396 | argv_free(argv); |
| 397 | } |
| 398 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame^] | 399 | /* Compose only probe arg */ |
| 400 | int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len) |
| 401 | { |
| 402 | struct perf_probe_arg_field *field = pa->field; |
| 403 | int ret; |
| 404 | char *tmp = buf; |
| 405 | |
| 406 | ret = e_snprintf(tmp, len, "%s", pa->name); |
| 407 | if (ret <= 0) |
| 408 | goto error; |
| 409 | tmp += ret; |
| 410 | len -= ret; |
| 411 | |
| 412 | while (field) { |
| 413 | ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".", |
| 414 | field->name); |
| 415 | if (ret <= 0) |
| 416 | goto error; |
| 417 | tmp += ret; |
| 418 | len -= ret; |
| 419 | field = field->next; |
| 420 | } |
| 421 | return tmp - buf; |
| 422 | error: |
| 423 | die("Failed to synthesize perf probe argument: %s", strerror(-ret)); |
| 424 | } |
| 425 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 426 | /* Compose only probe point (not argument) */ |
| 427 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 428 | { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 429 | char *buf, *tmp; |
| 430 | char offs[32] = "", line[32] = "", file[32] = ""; |
| 431 | int ret, len; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 432 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 433 | buf = xzalloc(MAX_CMDLEN); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 434 | if (pp->offset) { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 435 | ret = e_snprintf(offs, 32, "+%lu", pp->offset); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 436 | if (ret <= 0) |
| 437 | goto error; |
| 438 | } |
| 439 | if (pp->line) { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 440 | ret = e_snprintf(line, 32, ":%d", pp->line); |
| 441 | if (ret <= 0) |
| 442 | goto error; |
| 443 | } |
| 444 | if (pp->file) { |
| 445 | len = strlen(pp->file) - 32; |
| 446 | if (len < 0) |
| 447 | len = 0; |
| 448 | tmp = strchr(pp->file + len, '/'); |
| 449 | if (!tmp) |
| 450 | tmp = pp->file + len - 1; |
| 451 | ret = e_snprintf(file, 32, "@%s", tmp + 1); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 452 | if (ret <= 0) |
| 453 | goto error; |
| 454 | } |
| 455 | |
| 456 | if (pp->function) |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 457 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function, |
| 458 | offs, pp->retprobe ? "%return" : "", line, |
| 459 | file); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 460 | else |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 461 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 462 | if (ret <= 0) |
| 463 | goto error; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 464 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 465 | return buf; |
| 466 | error: |
| 467 | die("Failed to synthesize perf probe point: %s", strerror(-ret)); |
| 468 | } |
| 469 | |
| 470 | #if 0 |
| 471 | char *synthesize_perf_probe_command(struct perf_probe_event *pev) |
| 472 | { |
| 473 | char *buf; |
| 474 | int i, len, ret; |
| 475 | |
| 476 | buf = synthesize_perf_probe_point(&pev->point); |
| 477 | if (!buf) |
| 478 | return NULL; |
| 479 | |
| 480 | len = strlen(buf); |
| 481 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 482 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 483 | pev->args[i].name); |
| 484 | if (ret <= 0) { |
| 485 | free(buf); |
| 486 | return NULL; |
| 487 | } |
| 488 | len += ret; |
| 489 | } |
| 490 | |
| 491 | return buf; |
| 492 | } |
| 493 | #endif |
| 494 | |
| 495 | static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref, |
| 496 | char **buf, size_t *buflen, |
| 497 | int depth) |
| 498 | { |
| 499 | int ret; |
| 500 | if (ref->next) { |
| 501 | depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf, |
| 502 | buflen, depth + 1); |
| 503 | if (depth < 0) |
| 504 | goto out; |
| 505 | } |
| 506 | |
| 507 | ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset); |
| 508 | if (ret < 0) |
| 509 | depth = ret; |
| 510 | else { |
| 511 | *buf += ret; |
| 512 | *buflen -= ret; |
| 513 | } |
| 514 | out: |
| 515 | return depth; |
| 516 | |
| 517 | } |
| 518 | |
| 519 | static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg, |
| 520 | char *buf, size_t buflen) |
| 521 | { |
| 522 | int ret, depth = 0; |
| 523 | char *tmp = buf; |
| 524 | |
| 525 | /* Argument name or separator */ |
| 526 | if (arg->name) |
| 527 | ret = e_snprintf(buf, buflen, " %s=", arg->name); |
| 528 | else |
| 529 | ret = e_snprintf(buf, buflen, " "); |
| 530 | if (ret < 0) |
| 531 | return ret; |
| 532 | buf += ret; |
| 533 | buflen -= ret; |
| 534 | |
| 535 | /* Dereferencing arguments */ |
| 536 | if (arg->ref) { |
| 537 | depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf, |
| 538 | &buflen, 1); |
| 539 | if (depth < 0) |
| 540 | return depth; |
| 541 | } |
| 542 | |
| 543 | /* Print argument value */ |
| 544 | ret = e_snprintf(buf, buflen, "%s", arg->value); |
| 545 | if (ret < 0) |
| 546 | return ret; |
| 547 | buf += ret; |
| 548 | buflen -= ret; |
| 549 | |
| 550 | /* Closing */ |
| 551 | while (depth--) { |
| 552 | ret = e_snprintf(buf, buflen, ")"); |
| 553 | if (ret < 0) |
| 554 | return ret; |
| 555 | buf += ret; |
| 556 | buflen -= ret; |
| 557 | } |
| 558 | |
| 559 | return buf - tmp; |
| 560 | } |
| 561 | |
| 562 | char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev) |
| 563 | { |
| 564 | struct kprobe_trace_point *tp = &tev->point; |
| 565 | char *buf; |
| 566 | int i, len, ret; |
| 567 | |
| 568 | buf = xzalloc(MAX_CMDLEN); |
| 569 | len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu", |
| 570 | tp->retprobe ? 'r' : 'p', |
| 571 | tev->group, tev->event, |
| 572 | tp->symbol, tp->offset); |
| 573 | if (len <= 0) |
| 574 | goto error; |
| 575 | |
| 576 | for (i = 0; i < tev->nargs; i++) { |
| 577 | ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len, |
| 578 | MAX_CMDLEN - len); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 579 | if (ret <= 0) |
| 580 | goto error; |
| 581 | len += ret; |
| 582 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 583 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 584 | return buf; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 585 | error: |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 586 | free(buf); |
| 587 | return NULL; |
| 588 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 589 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 590 | void convert_to_perf_probe_event(struct kprobe_trace_event *tev, |
| 591 | struct perf_probe_event *pev) |
| 592 | { |
| 593 | char buf[64]; |
| 594 | int i; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 595 | #ifndef NO_DWARF_SUPPORT |
| 596 | struct symbol *sym; |
| 597 | int fd, ret = 0; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 598 | |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 599 | sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION], |
| 600 | tev->point.symbol, NULL); |
| 601 | if (sym) { |
| 602 | fd = open_vmlinux(); |
| 603 | ret = find_perf_probe_point(fd, sym->start + tev->point.offset, |
| 604 | &pev->point); |
| 605 | close(fd); |
| 606 | } |
| 607 | if (ret <= 0) { |
| 608 | pev->point.function = xstrdup(tev->point.symbol); |
| 609 | pev->point.offset = tev->point.offset; |
| 610 | } |
| 611 | #else |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 612 | /* Convert trace_point to probe_point */ |
| 613 | pev->point.function = xstrdup(tev->point.symbol); |
| 614 | pev->point.offset = tev->point.offset; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 615 | #endif |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 616 | pev->point.retprobe = tev->point.retprobe; |
| 617 | |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 618 | pev->event = xstrdup(tev->event); |
| 619 | pev->group = xstrdup(tev->group); |
| 620 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 621 | /* Convert trace_arg to probe_arg */ |
| 622 | pev->nargs = tev->nargs; |
| 623 | pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
| 624 | for (i = 0; i < tev->nargs; i++) |
| 625 | if (tev->args[i].name) |
| 626 | pev->args[i].name = xstrdup(tev->args[i].name); |
| 627 | else { |
| 628 | synthesize_kprobe_trace_arg(&tev->args[i], buf, 64); |
| 629 | pev->args[i].name = xstrdup(buf); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | void clear_perf_probe_event(struct perf_probe_event *pev) |
| 634 | { |
| 635 | struct perf_probe_point *pp = &pev->point; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame^] | 636 | struct perf_probe_arg_field *field, *next; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 637 | int i; |
| 638 | |
| 639 | if (pev->event) |
| 640 | free(pev->event); |
| 641 | if (pev->group) |
| 642 | free(pev->group); |
| 643 | if (pp->file) |
| 644 | free(pp->file); |
| 645 | if (pp->function) |
| 646 | free(pp->function); |
| 647 | if (pp->lazy_line) |
| 648 | free(pp->lazy_line); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame^] | 649 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 650 | if (pev->args[i].name) |
| 651 | free(pev->args[i].name); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame^] | 652 | field = pev->args[i].field; |
| 653 | while (field) { |
| 654 | next = field->next; |
| 655 | if (field->name) |
| 656 | free(field->name); |
| 657 | free(field); |
| 658 | field = next; |
| 659 | } |
| 660 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 661 | if (pev->args) |
| 662 | free(pev->args); |
| 663 | memset(pev, 0, sizeof(*pev)); |
| 664 | } |
| 665 | |
| 666 | void clear_kprobe_trace_event(struct kprobe_trace_event *tev) |
| 667 | { |
| 668 | struct kprobe_trace_arg_ref *ref, *next; |
| 669 | int i; |
| 670 | |
| 671 | if (tev->event) |
| 672 | free(tev->event); |
| 673 | if (tev->group) |
| 674 | free(tev->group); |
| 675 | if (tev->point.symbol) |
| 676 | free(tev->point.symbol); |
| 677 | for (i = 0; i < tev->nargs; i++) { |
| 678 | if (tev->args[i].name) |
| 679 | free(tev->args[i].name); |
| 680 | if (tev->args[i].value) |
| 681 | free(tev->args[i].value); |
| 682 | ref = tev->args[i].ref; |
| 683 | while (ref) { |
| 684 | next = ref->next; |
| 685 | free(ref); |
| 686 | ref = next; |
| 687 | } |
| 688 | } |
| 689 | if (tev->args) |
| 690 | free(tev->args); |
| 691 | memset(tev, 0, sizeof(*tev)); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 692 | } |
| 693 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 694 | static int open_kprobe_events(bool readwrite) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 695 | { |
| 696 | char buf[PATH_MAX]; |
| 697 | int ret; |
| 698 | |
| 699 | ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path); |
| 700 | if (ret < 0) |
| 701 | die("Failed to make kprobe_events path."); |
| 702 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 703 | if (readwrite && !probe_event_dry_run) |
| 704 | ret = open(buf, O_RDWR, O_APPEND); |
| 705 | else |
| 706 | ret = open(buf, O_RDONLY, 0); |
| 707 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 708 | if (ret < 0) { |
| 709 | if (errno == ENOENT) |
| 710 | die("kprobe_events file does not exist -" |
Liming Wang | 63bbd5e | 2009-12-29 16:37:09 +0800 | [diff] [blame] | 711 | " please rebuild with CONFIG_KPROBE_EVENT."); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 712 | else |
| 713 | die("Could not open kprobe_events file: %s", |
| 714 | strerror(errno)); |
| 715 | } |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | /* Get raw string list of current kprobe_events */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 720 | static struct strlist *get_kprobe_trace_command_rawlist(int fd) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 721 | { |
| 722 | int ret, idx; |
| 723 | FILE *fp; |
| 724 | char buf[MAX_CMDLEN]; |
| 725 | char *p; |
| 726 | struct strlist *sl; |
| 727 | |
| 728 | sl = strlist__new(true, NULL); |
| 729 | |
| 730 | fp = fdopen(dup(fd), "r"); |
| 731 | while (!feof(fp)) { |
| 732 | p = fgets(buf, MAX_CMDLEN, fp); |
| 733 | if (!p) |
| 734 | break; |
| 735 | |
| 736 | idx = strlen(p) - 1; |
| 737 | if (p[idx] == '\n') |
| 738 | p[idx] = '\0'; |
| 739 | ret = strlist__add(sl, buf); |
| 740 | if (ret < 0) |
| 741 | die("strlist__add failed: %s", strerror(-ret)); |
| 742 | } |
| 743 | fclose(fp); |
| 744 | |
| 745 | return sl; |
| 746 | } |
| 747 | |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 748 | /* Show an event */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 749 | static void show_perf_probe_event(struct perf_probe_event *pev) |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 750 | { |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 751 | int i, ret; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 752 | char buf[128]; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 753 | char *place; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 754 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 755 | /* Synthesize only event probe point */ |
| 756 | place = synthesize_perf_probe_point(&pev->point); |
| 757 | |
| 758 | ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event); |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 759 | if (ret < 0) |
| 760 | die("Failed to copy event: %s", strerror(-ret)); |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 761 | printf(" %-20s (on %s", buf, place); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 762 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 763 | if (pev->nargs > 0) { |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 764 | printf(" with"); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame^] | 765 | for (i = 0; i < pev->nargs; i++) { |
| 766 | synthesize_perf_probe_arg(&pev->args[i], buf, 128); |
| 767 | printf(" %s", buf); |
| 768 | } |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 769 | } |
| 770 | printf(")\n"); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 771 | free(place); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 772 | } |
| 773 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 774 | /* List up current perf-probe events */ |
| 775 | void show_perf_probe_events(void) |
| 776 | { |
Masami Hiramatsu | 7ef17aa | 2009-12-15 10:32:47 -0500 | [diff] [blame] | 777 | int fd; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 778 | struct kprobe_trace_event tev; |
| 779 | struct perf_probe_event pev; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 780 | struct strlist *rawlist; |
| 781 | struct str_node *ent; |
| 782 | |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 783 | setup_pager(); |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 784 | init_vmlinux(); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 785 | |
| 786 | memset(&tev, 0, sizeof(tev)); |
| 787 | memset(&pev, 0, sizeof(pev)); |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 788 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 789 | fd = open_kprobe_events(false); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 790 | rawlist = get_kprobe_trace_command_rawlist(fd); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 791 | close(fd); |
| 792 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 793 | strlist__for_each(ent, rawlist) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 794 | parse_kprobe_trace_command(ent->s, &tev); |
| 795 | convert_to_perf_probe_event(&tev, &pev); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 796 | /* Show an event */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 797 | show_perf_probe_event(&pev); |
| 798 | clear_perf_probe_event(&pev); |
| 799 | clear_kprobe_trace_event(&tev); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | strlist__delete(rawlist); |
| 803 | } |
| 804 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 805 | /* Get current perf-probe event names */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 806 | static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 807 | { |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 808 | char buf[128]; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 809 | struct strlist *sl, *rawlist; |
| 810 | struct str_node *ent; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 811 | struct kprobe_trace_event tev; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 812 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 813 | memset(&tev, 0, sizeof(tev)); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 814 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 815 | rawlist = get_kprobe_trace_command_rawlist(fd); |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 816 | sl = strlist__new(true, NULL); |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 817 | strlist__for_each(ent, rawlist) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 818 | parse_kprobe_trace_command(ent->s, &tev); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 819 | if (include_group) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 820 | if (e_snprintf(buf, 128, "%s:%s", tev.group, |
| 821 | tev.event) < 0) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 822 | die("Failed to copy group:event name."); |
| 823 | strlist__add(sl, buf); |
| 824 | } else |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 825 | strlist__add(sl, tev.event); |
| 826 | clear_kprobe_trace_event(&tev); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | strlist__delete(rawlist); |
| 830 | |
| 831 | return sl; |
| 832 | } |
| 833 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 834 | static void write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 835 | { |
| 836 | int ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 837 | char *buf = synthesize_kprobe_trace_command(tev); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 838 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 839 | pr_debug("Writing event: %s\n", buf); |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 840 | if (!probe_event_dry_run) { |
| 841 | ret = write(fd, buf, strlen(buf)); |
| 842 | if (ret <= 0) |
| 843 | die("Failed to write event: %s", strerror(errno)); |
| 844 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 845 | free(buf); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 846 | } |
| 847 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 848 | 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] | 849 | struct strlist *namelist, bool allow_suffix) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 850 | { |
| 851 | int i, ret; |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 852 | |
| 853 | /* Try no suffix */ |
| 854 | ret = e_snprintf(buf, len, "%s", base); |
| 855 | if (ret < 0) |
| 856 | die("snprintf() failed: %s", strerror(-ret)); |
| 857 | if (!strlist__has_entry(namelist, buf)) |
| 858 | return; |
| 859 | |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 860 | if (!allow_suffix) { |
| 861 | pr_warning("Error: event \"%s\" already exists. " |
| 862 | "(Use -f to force duplicates.)\n", base); |
| 863 | die("Can't add new event."); |
| 864 | } |
| 865 | |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 866 | /* Try to add suffix */ |
| 867 | for (i = 1; i < MAX_EVENT_INDEX; i++) { |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 868 | ret = e_snprintf(buf, len, "%s_%d", base, i); |
| 869 | if (ret < 0) |
| 870 | die("snprintf() failed: %s", strerror(-ret)); |
| 871 | if (!strlist__has_entry(namelist, buf)) |
| 872 | break; |
| 873 | } |
| 874 | if (i == MAX_EVENT_INDEX) |
| 875 | die("Too many events are on the same function."); |
| 876 | } |
| 877 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 878 | static void __add_kprobe_trace_events(struct perf_probe_event *pev, |
| 879 | struct kprobe_trace_event *tevs, |
| 880 | int ntevs, bool allow_suffix) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 881 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 882 | int i, fd; |
| 883 | struct kprobe_trace_event *tev; |
| 884 | char buf[64]; |
| 885 | const char *event, *group; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 886 | struct strlist *namelist; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 887 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 888 | fd = open_kprobe_events(true); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 889 | /* Get current event names */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 890 | namelist = get_kprobe_trace_event_names(fd, false); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 891 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 892 | printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":"); |
| 893 | for (i = 0; i < ntevs; i++) { |
| 894 | tev = &tevs[i]; |
| 895 | if (pev->event) |
| 896 | event = pev->event; |
| 897 | else |
| 898 | if (pev->point.function) |
| 899 | event = pev->point.function; |
| 900 | else |
| 901 | event = tev->point.symbol; |
| 902 | if (pev->group) |
| 903 | group = pev->group; |
| 904 | else |
| 905 | group = PERFPROBE_GROUP; |
| 906 | |
| 907 | /* Get an unused new event name */ |
| 908 | get_new_event_name(buf, 64, event, namelist, allow_suffix); |
| 909 | event = buf; |
| 910 | |
| 911 | tev->event = xstrdup(event); |
| 912 | tev->group = xstrdup(group); |
| 913 | write_kprobe_trace_event(fd, tev); |
| 914 | /* Add added event name to namelist */ |
| 915 | strlist__add(namelist, event); |
| 916 | |
| 917 | /* Trick here - save current event/group */ |
| 918 | event = pev->event; |
| 919 | group = pev->group; |
| 920 | pev->event = tev->event; |
| 921 | pev->group = tev->group; |
| 922 | show_perf_probe_event(pev); |
| 923 | /* Trick here - restore current event/group */ |
| 924 | pev->event = (char *)event; |
| 925 | pev->group = (char *)group; |
| 926 | |
| 927 | /* |
| 928 | * Probes after the first probe which comes from same |
| 929 | * user input are always allowed to add suffix, because |
| 930 | * there might be several addresses corresponding to |
| 931 | * one code line. |
| 932 | */ |
| 933 | allow_suffix = true; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 934 | } |
Masami Hiramatsu | a9b495b | 2009-12-08 17:02:47 -0500 | [diff] [blame] | 935 | /* Show how to use the event. */ |
| 936 | printf("\nYou can now use it on all perf tools, such as:\n\n"); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 937 | printf("\tperf record -e %s:%s -a sleep 1\n\n", tev->group, tev->event); |
Masami Hiramatsu | a9b495b | 2009-12-08 17:02:47 -0500 | [diff] [blame] | 938 | |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 939 | strlist__delete(namelist); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 940 | close(fd); |
| 941 | } |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 942 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 943 | static int convert_to_kprobe_trace_events(struct perf_probe_event *pev, |
| 944 | struct kprobe_trace_event **tevs) |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 945 | { |
| 946 | struct symbol *sym; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 947 | bool need_dwarf; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 948 | #ifndef NO_DWARF_SUPPORT |
| 949 | int fd; |
| 950 | #endif |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 951 | int ntevs = 0, i; |
| 952 | struct kprobe_trace_event *tev; |
| 953 | |
| 954 | need_dwarf = perf_probe_event_need_dwarf(pev); |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 955 | |
| 956 | if (need_dwarf) |
| 957 | #ifdef NO_DWARF_SUPPORT |
| 958 | die("Debuginfo-analysis is not supported"); |
| 959 | #else /* !NO_DWARF_SUPPORT */ |
| 960 | pr_debug("Some probes require debuginfo.\n"); |
| 961 | |
| 962 | fd = open_vmlinux(); |
| 963 | if (fd < 0) { |
| 964 | if (need_dwarf) |
| 965 | die("Could not open debuginfo file."); |
| 966 | |
| 967 | pr_debug("Could not open vmlinux/module file." |
| 968 | " Try to use symbols.\n"); |
| 969 | goto end_dwarf; |
| 970 | } |
| 971 | |
| 972 | /* Searching probe points */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 973 | ntevs = find_kprobe_trace_events(fd, pev, tevs); |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 974 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 975 | if (ntevs > 0) /* Found */ |
| 976 | goto found; |
| 977 | |
| 978 | if (ntevs == 0) /* No error but failed to find probe point. */ |
| 979 | die("Probe point '%s' not found. - probe not added.", |
| 980 | synthesize_perf_probe_point(&pev->point)); |
| 981 | |
| 982 | /* Error path */ |
| 983 | if (need_dwarf) { |
| 984 | if (ntevs == -ENOENT) |
| 985 | pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n"); |
| 986 | die("Could not analyze debuginfo."); |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 987 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 988 | pr_debug("An error occurred in debuginfo analysis." |
| 989 | " Try to use symbols.\n"); |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 990 | |
| 991 | end_dwarf: |
| 992 | #endif /* !NO_DWARF_SUPPORT */ |
| 993 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 994 | /* Allocate trace event buffer */ |
| 995 | ntevs = 1; |
| 996 | tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event)); |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 997 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 998 | /* Copy parameters */ |
| 999 | tev->point.symbol = xstrdup(pev->point.function); |
| 1000 | tev->point.offset = pev->point.offset; |
| 1001 | tev->nargs = pev->nargs; |
| 1002 | if (tev->nargs) { |
| 1003 | tev->args = xzalloc(sizeof(struct kprobe_trace_arg) |
| 1004 | * tev->nargs); |
| 1005 | for (i = 0; i < tev->nargs; i++) |
| 1006 | tev->args[i].value = xstrdup(pev->args[i].name); |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1007 | } |
| 1008 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1009 | /* Currently just checking function name from symbol map */ |
| 1010 | sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION], |
| 1011 | tev->point.symbol, NULL); |
| 1012 | if (!sym) |
| 1013 | die("Kernel symbol \'%s\' not found - probe not added.", |
| 1014 | tev->point.symbol); |
| 1015 | found: |
| 1016 | close(fd); |
| 1017 | return ntevs; |
| 1018 | } |
| 1019 | |
| 1020 | struct __event_package { |
| 1021 | struct perf_probe_event *pev; |
| 1022 | struct kprobe_trace_event *tevs; |
| 1023 | int ntevs; |
| 1024 | }; |
| 1025 | |
| 1026 | void add_perf_probe_events(struct perf_probe_event *pevs, int npevs, |
| 1027 | bool force_add) |
| 1028 | { |
| 1029 | int i; |
| 1030 | struct __event_package *pkgs; |
| 1031 | |
| 1032 | pkgs = xzalloc(sizeof(struct __event_package) * npevs); |
| 1033 | |
| 1034 | /* Init vmlinux path */ |
| 1035 | init_vmlinux(); |
| 1036 | |
| 1037 | /* Loop 1: convert all events */ |
| 1038 | for (i = 0; i < npevs; i++) { |
| 1039 | pkgs[i].pev = &pevs[i]; |
| 1040 | /* Convert with or without debuginfo */ |
| 1041 | pkgs[i].ntevs = convert_to_kprobe_trace_events(pkgs[i].pev, |
| 1042 | &pkgs[i].tevs); |
| 1043 | } |
| 1044 | |
| 1045 | /* Loop 2: add all events */ |
| 1046 | for (i = 0; i < npevs; i++) |
| 1047 | __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs, |
| 1048 | pkgs[i].ntevs, force_add); |
| 1049 | /* TODO: cleanup all trace events? */ |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1050 | } |
| 1051 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1052 | static void __del_trace_kprobe_event(int fd, struct str_node *ent) |
| 1053 | { |
| 1054 | char *p; |
| 1055 | char buf[128]; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1056 | int ret; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1057 | |
| 1058 | /* Convert from perf-probe event to trace-kprobe event */ |
| 1059 | if (e_snprintf(buf, 128, "-:%s", ent->s) < 0) |
| 1060 | die("Failed to copy event."); |
| 1061 | p = strchr(buf + 2, ':'); |
| 1062 | if (!p) |
| 1063 | die("Internal error: %s should have ':' but not.", ent->s); |
| 1064 | *p = '/'; |
| 1065 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1066 | pr_debug("Writing event: %s\n", buf); |
| 1067 | ret = write(fd, buf, strlen(buf)); |
| 1068 | if (ret <= 0) |
| 1069 | die("Failed to write event: %s", strerror(errno)); |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1070 | printf("Remove event: %s\n", ent->s); |
| 1071 | } |
| 1072 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1073 | static void del_trace_kprobe_event(int fd, const char *group, |
| 1074 | const char *event, struct strlist *namelist) |
| 1075 | { |
| 1076 | char buf[128]; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1077 | struct str_node *ent, *n; |
| 1078 | int found = 0; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1079 | |
| 1080 | if (e_snprintf(buf, 128, "%s:%s", group, event) < 0) |
| 1081 | die("Failed to copy event."); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1082 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1083 | if (strpbrk(buf, "*?")) { /* Glob-exp */ |
| 1084 | strlist__for_each_safe(ent, n, namelist) |
| 1085 | if (strglobmatch(ent->s, buf)) { |
| 1086 | found++; |
| 1087 | __del_trace_kprobe_event(fd, ent); |
| 1088 | strlist__remove(namelist, ent); |
| 1089 | } |
| 1090 | } else { |
| 1091 | ent = strlist__find(namelist, buf); |
| 1092 | if (ent) { |
| 1093 | found++; |
| 1094 | __del_trace_kprobe_event(fd, ent); |
| 1095 | strlist__remove(namelist, ent); |
| 1096 | } |
| 1097 | } |
| 1098 | if (found == 0) |
| 1099 | 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] | 1100 | } |
| 1101 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1102 | void del_perf_probe_events(struct strlist *dellist) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1103 | { |
| 1104 | int fd; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1105 | const char *group, *event; |
| 1106 | char *p, *str; |
| 1107 | struct str_node *ent; |
| 1108 | struct strlist *namelist; |
| 1109 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1110 | fd = open_kprobe_events(true); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1111 | /* Get current event names */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1112 | namelist = get_kprobe_trace_event_names(fd, true); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1113 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 1114 | strlist__for_each(ent, dellist) { |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 1115 | str = xstrdup(ent->s); |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1116 | pr_debug("Parsing: %s\n", str); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1117 | p = strchr(str, ':'); |
| 1118 | if (p) { |
| 1119 | group = str; |
| 1120 | *p = '\0'; |
| 1121 | event = p + 1; |
| 1122 | } else { |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1123 | group = "*"; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1124 | event = str; |
| 1125 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1126 | pr_debug("Group: %s, Event: %s\n", group, event); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1127 | del_trace_kprobe_event(fd, group, event, namelist); |
| 1128 | free(str); |
| 1129 | } |
| 1130 | strlist__delete(namelist); |
| 1131 | close(fd); |
| 1132 | } |
| 1133 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1134 | #define LINEBUF_SIZE 256 |
Masami Hiramatsu | 5c8d1cb | 2010-02-25 08:36:04 -0500 | [diff] [blame] | 1135 | #define NR_ADDITIONAL_LINES 2 |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1136 | |
| 1137 | static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num) |
| 1138 | { |
| 1139 | char buf[LINEBUF_SIZE]; |
| 1140 | const char *color = PERF_COLOR_BLUE; |
| 1141 | |
| 1142 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
| 1143 | goto error; |
| 1144 | if (!skip) { |
| 1145 | if (show_num) |
| 1146 | fprintf(stdout, "%7u %s", l, buf); |
| 1147 | else |
| 1148 | color_fprintf(stdout, color, " %s", buf); |
| 1149 | } |
| 1150 | |
| 1151 | while (strlen(buf) == LINEBUF_SIZE - 1 && |
| 1152 | buf[LINEBUF_SIZE - 2] != '\n') { |
| 1153 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
| 1154 | goto error; |
| 1155 | if (!skip) { |
| 1156 | if (show_num) |
| 1157 | fprintf(stdout, "%s", buf); |
| 1158 | else |
| 1159 | color_fprintf(stdout, color, "%s", buf); |
| 1160 | } |
| 1161 | } |
| 1162 | return; |
| 1163 | error: |
| 1164 | if (feof(fp)) |
| 1165 | die("Source file is shorter than expected."); |
| 1166 | else |
| 1167 | die("File read error: %s", strerror(errno)); |
| 1168 | } |
| 1169 | |
| 1170 | void show_line_range(struct line_range *lr) |
| 1171 | { |
| 1172 | unsigned int l = 1; |
| 1173 | struct line_node *ln; |
| 1174 | FILE *fp; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1175 | int fd, ret; |
| 1176 | |
| 1177 | /* Search a line range */ |
| 1178 | init_vmlinux(); |
| 1179 | fd = open_vmlinux(); |
| 1180 | if (fd < 0) |
| 1181 | die("Could not open debuginfo file."); |
| 1182 | ret = find_line_range(fd, lr); |
| 1183 | if (ret <= 0) |
| 1184 | die("Source line is not found.\n"); |
| 1185 | close(fd); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1186 | |
| 1187 | setup_pager(); |
| 1188 | |
| 1189 | if (lr->function) |
| 1190 | fprintf(stdout, "<%s:%d>\n", lr->function, |
| 1191 | lr->start - lr->offset); |
| 1192 | else |
| 1193 | fprintf(stdout, "<%s:%d>\n", lr->file, lr->start); |
| 1194 | |
| 1195 | fp = fopen(lr->path, "r"); |
| 1196 | if (fp == NULL) |
| 1197 | die("Failed to open %s: %s", lr->path, strerror(errno)); |
| 1198 | /* Skip to starting line number */ |
| 1199 | while (l < lr->start) |
| 1200 | show_one_line(fp, l++, true, false); |
| 1201 | |
| 1202 | list_for_each_entry(ln, &lr->line_list, list) { |
| 1203 | while (ln->line > l) |
| 1204 | show_one_line(fp, (l++) - lr->offset, false, false); |
| 1205 | show_one_line(fp, (l++) - lr->offset, false, true); |
| 1206 | } |
Masami Hiramatsu | 5c8d1cb | 2010-02-25 08:36:04 -0500 | [diff] [blame] | 1207 | |
| 1208 | if (lr->end == INT_MAX) |
| 1209 | lr->end = l + NR_ADDITIONAL_LINES; |
| 1210 | while (l < lr->end && !feof(fp)) |
| 1211 | show_one_line(fp, (l++) - lr->offset, false, false); |
| 1212 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1213 | fclose(fp); |
| 1214 | } |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1215 | |
| 1216 | |