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