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 | 7ca5989 | 2010-04-14 18:39:28 -0400 | [diff] [blame] | 45 | #include "debugfs.h" |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 46 | #include "trace-event.h" /* For __unused */ |
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 | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 56 | #define semantic_error(msg ...) pr_err("Semantic error :" msg) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 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 | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 79 | static int init_vmlinux(void) |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 80 | { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 81 | struct dso *kernel; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 82 | int ret; |
| 83 | |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 84 | symbol_conf.sort_by_name = true; |
| 85 | if (symbol_conf.vmlinux_name == NULL) |
| 86 | symbol_conf.try_vmlinux_path = true; |
| 87 | else |
| 88 | pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 89 | ret = symbol__init(); |
| 90 | if (ret < 0) { |
| 91 | pr_debug("Failed to init symbol map.\n"); |
| 92 | goto out; |
| 93 | } |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 94 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 95 | kernel = dso__new_kernel(symbol_conf.vmlinux_name); |
| 96 | if (kernel == NULL) |
| 97 | die("Failed to create kernel dso."); |
| 98 | |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 99 | map_groups__init(&kmap_groups); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 100 | ret = __map_groups__create_kernel_maps(&kmap_groups, kmaps, kernel); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 101 | if (ret < 0) |
| 102 | pr_debug("Failed to create kernel maps.\n"); |
| 103 | |
| 104 | out: |
| 105 | if (ret < 0) |
| 106 | pr_warning("Failed to init vmlinux path.\n"); |
| 107 | return ret; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 110 | #ifdef DWARF_SUPPORT |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 111 | static int open_vmlinux(void) |
| 112 | { |
| 113 | if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) { |
| 114 | pr_debug("Failed to load kernel map.\n"); |
| 115 | return -EINVAL; |
| 116 | } |
| 117 | pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name); |
| 118 | return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY); |
| 119 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 120 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 121 | /* Convert trace point to probe point with debuginfo */ |
| 122 | static int convert_to_perf_probe_point(struct kprobe_trace_point *tp, |
| 123 | struct perf_probe_point *pp) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 124 | { |
| 125 | struct symbol *sym; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 126 | int fd, ret = -ENOENT; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 127 | |
| 128 | sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION], |
| 129 | tp->symbol, NULL); |
| 130 | if (sym) { |
| 131 | fd = open_vmlinux(); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 132 | if (fd >= 0) { |
| 133 | ret = find_perf_probe_point(fd, |
| 134 | sym->start + tp->offset, pp); |
| 135 | close(fd); |
| 136 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 137 | } |
| 138 | if (ret <= 0) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 139 | pr_debug("Failed to find corresponding probes from " |
| 140 | "debuginfo. Use kprobe event information.\n"); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 141 | pp->function = strdup(tp->symbol); |
| 142 | if (pp->function == NULL) |
| 143 | return -ENOMEM; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 144 | pp->offset = tp->offset; |
| 145 | } |
| 146 | pp->retprobe = tp->retprobe; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 147 | |
| 148 | return 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* Try to find perf_probe_event with debuginfo */ |
| 152 | static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev, |
| 153 | struct kprobe_trace_event **tevs) |
| 154 | { |
| 155 | bool need_dwarf = perf_probe_event_need_dwarf(pev); |
| 156 | int fd, ntevs; |
| 157 | |
| 158 | fd = open_vmlinux(); |
| 159 | if (fd < 0) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 160 | if (need_dwarf) { |
| 161 | pr_warning("Failed to open debuginfo file.\n"); |
| 162 | return fd; |
| 163 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 164 | pr_debug("Could not open vmlinux. Try to use symbols.\n"); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /* Searching trace events corresponding to probe event */ |
| 169 | ntevs = find_kprobe_trace_events(fd, pev, tevs); |
| 170 | close(fd); |
| 171 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 172 | if (ntevs > 0) { /* Succeeded to find trace events */ |
| 173 | pr_debug("find %d kprobe_trace_events.\n", ntevs); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 174 | return ntevs; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 175 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 176 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 177 | if (ntevs == 0) { /* No error but failed to find probe point. */ |
| 178 | pr_warning("Probe point '%s' not found.\n", |
| 179 | synthesize_perf_probe_point(&pev->point)); |
| 180 | return -ENOENT; |
| 181 | } |
| 182 | /* Error path : ntevs < 0 */ |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 183 | if (need_dwarf) { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 184 | if (ntevs == -EBADF) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 185 | pr_warning("No dwarf info found in the vmlinux - " |
| 186 | "please rebuild with CONFIG_DEBUG_INFO=y.\n"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 187 | return ntevs; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 188 | } |
| 189 | pr_debug("An error occurred in debuginfo analysis." |
| 190 | " Try to use symbols.\n"); |
| 191 | return 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | #define LINEBUF_SIZE 256 |
| 195 | #define NR_ADDITIONAL_LINES 2 |
| 196 | |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 197 | static int show_one_line(FILE *fp, int l, bool skip, bool show_num) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 198 | { |
| 199 | char buf[LINEBUF_SIZE]; |
| 200 | const char *color = PERF_COLOR_BLUE; |
| 201 | |
| 202 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
| 203 | goto error; |
| 204 | if (!skip) { |
| 205 | if (show_num) |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 206 | fprintf(stdout, "%7d %s", l, buf); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 207 | else |
| 208 | color_fprintf(stdout, color, " %s", buf); |
| 209 | } |
| 210 | |
| 211 | while (strlen(buf) == LINEBUF_SIZE - 1 && |
| 212 | buf[LINEBUF_SIZE - 2] != '\n') { |
| 213 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
| 214 | goto error; |
| 215 | if (!skip) { |
| 216 | if (show_num) |
| 217 | fprintf(stdout, "%s", buf); |
| 218 | else |
| 219 | color_fprintf(stdout, color, "%s", buf); |
| 220 | } |
| 221 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 222 | |
| 223 | return 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 224 | error: |
| 225 | if (feof(fp)) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 226 | pr_warning("Source file is shorter than expected.\n"); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 227 | else |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 228 | pr_warning("File read error: %s\n", strerror(errno)); |
| 229 | |
| 230 | return -1; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Show line-range always requires debuginfo to find source file and |
| 235 | * line number. |
| 236 | */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 237 | int show_line_range(struct line_range *lr) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 238 | { |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 239 | int l = 1; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 240 | struct line_node *ln; |
| 241 | FILE *fp; |
| 242 | int fd, ret; |
| 243 | |
| 244 | /* Search a line range */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 245 | ret = init_vmlinux(); |
| 246 | if (ret < 0) |
| 247 | return ret; |
| 248 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 249 | fd = open_vmlinux(); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 250 | if (fd < 0) { |
| 251 | pr_warning("Failed to open debuginfo file.\n"); |
| 252 | return fd; |
| 253 | } |
| 254 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 255 | ret = find_line_range(fd, lr); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 256 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 257 | if (ret == 0) { |
| 258 | pr_warning("Specified source line is not found.\n"); |
| 259 | return -ENOENT; |
| 260 | } else if (ret < 0) { |
| 261 | pr_warning("Debuginfo analysis failed. (%d)\n", ret); |
| 262 | return ret; |
| 263 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 264 | |
| 265 | setup_pager(); |
| 266 | |
| 267 | if (lr->function) |
| 268 | fprintf(stdout, "<%s:%d>\n", lr->function, |
| 269 | lr->start - lr->offset); |
| 270 | else |
| 271 | fprintf(stdout, "<%s:%d>\n", lr->file, lr->start); |
| 272 | |
| 273 | fp = fopen(lr->path, "r"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 274 | if (fp == NULL) { |
| 275 | pr_warning("Failed to open %s: %s\n", lr->path, |
| 276 | strerror(errno)); |
| 277 | return -errno; |
| 278 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 279 | /* Skip to starting line number */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 280 | while (l < lr->start && ret >= 0) |
| 281 | ret = show_one_line(fp, l++, true, false); |
| 282 | if (ret < 0) |
| 283 | goto end; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 284 | |
| 285 | list_for_each_entry(ln, &lr->line_list, list) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 286 | while (ln->line > l && ret >= 0) |
| 287 | ret = show_one_line(fp, (l++) - lr->offset, |
| 288 | false, false); |
| 289 | if (ret >= 0) |
| 290 | ret = show_one_line(fp, (l++) - lr->offset, |
| 291 | false, true); |
| 292 | if (ret < 0) |
| 293 | goto end; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | if (lr->end == INT_MAX) |
| 297 | lr->end = l + NR_ADDITIONAL_LINES; |
Masami Hiramatsu | dda4ab3 | 2010-04-14 18:39:50 -0400 | [diff] [blame] | 298 | while (l <= lr->end && !feof(fp) && ret >= 0) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 299 | ret = show_one_line(fp, (l++) - lr->offset, false, false); |
| 300 | end: |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 301 | fclose(fp); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 302 | return ret; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | #else /* !DWARF_SUPPORT */ |
| 306 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 307 | static int convert_to_perf_probe_point(struct kprobe_trace_point *tp, |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 308 | struct perf_probe_point *pp) |
| 309 | { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 310 | pp->function = strdup(tp->symbol); |
| 311 | if (pp->function == NULL) |
| 312 | return -ENOMEM; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 313 | pp->offset = tp->offset; |
| 314 | pp->retprobe = tp->retprobe; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 315 | |
| 316 | return 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev, |
| 320 | struct kprobe_trace_event **tevs __unused) |
| 321 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 322 | if (perf_probe_event_need_dwarf(pev)) { |
| 323 | pr_warning("Debuginfo-analysis is not supported.\n"); |
| 324 | return -ENOSYS; |
| 325 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 326 | return 0; |
| 327 | } |
| 328 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 329 | int show_line_range(struct line_range *lr __unused) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 330 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 331 | pr_warning("Debuginfo-analysis is not supported.\n"); |
| 332 | return -ENOSYS; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 333 | } |
| 334 | |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 335 | #endif |
| 336 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 337 | int parse_line_range_desc(const char *arg, struct line_range *lr) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 338 | { |
| 339 | const char *ptr; |
| 340 | char *tmp; |
| 341 | /* |
| 342 | * <Syntax> |
| 343 | * SRC:SLN[+NUM|-ELN] |
| 344 | * FUNC[:SLN[+NUM|-ELN]] |
| 345 | */ |
| 346 | ptr = strchr(arg, ':'); |
| 347 | if (ptr) { |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 348 | lr->start = (int)strtoul(ptr + 1, &tmp, 0); |
Masami Hiramatsu | dda4ab3 | 2010-04-14 18:39:50 -0400 | [diff] [blame] | 349 | if (*tmp == '+') { |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 350 | lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0); |
Masami Hiramatsu | dda4ab3 | 2010-04-14 18:39:50 -0400 | [diff] [blame] | 351 | lr->end--; /* |
| 352 | * Adjust the number of lines here. |
| 353 | * If the number of lines == 1, the |
| 354 | * the end of line should be equal to |
| 355 | * the start of line. |
| 356 | */ |
| 357 | } else if (*tmp == '-') |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 358 | lr->end = (int)strtoul(tmp + 1, &tmp, 0); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 359 | else |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 360 | lr->end = INT_MAX; |
| 361 | pr_debug("Line range is %d to %d\n", lr->start, lr->end); |
| 362 | if (lr->start > lr->end) { |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 363 | semantic_error("Start line must be smaller" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 364 | " than end line.\n"); |
| 365 | return -EINVAL; |
| 366 | } |
| 367 | if (*tmp != '\0') { |
| 368 | semantic_error("Tailing with invalid character '%d'.\n", |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 369 | *tmp); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 370 | return -EINVAL; |
| 371 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 372 | tmp = strndup(arg, (ptr - arg)); |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 373 | } else { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 374 | tmp = strdup(arg); |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 375 | lr->end = INT_MAX; |
| 376 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 377 | |
| 378 | if (tmp == NULL) |
| 379 | return -ENOMEM; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 380 | |
| 381 | if (strchr(tmp, '.')) |
| 382 | lr->file = tmp; |
| 383 | else |
| 384 | lr->function = tmp; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 385 | |
| 386 | return 0; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 387 | } |
| 388 | |
Masami Hiramatsu | b7702a2 | 2009-12-16 17:24:15 -0500 | [diff] [blame] | 389 | /* Check the name is good for event/group */ |
| 390 | static bool check_event_name(const char *name) |
| 391 | { |
| 392 | if (!isalpha(*name) && *name != '_') |
| 393 | return false; |
| 394 | while (*++name != '\0') { |
| 395 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') |
| 396 | return false; |
| 397 | } |
| 398 | return true; |
| 399 | } |
| 400 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 401 | /* Parse probepoint definition. */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 402 | static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 403 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 404 | struct perf_probe_point *pp = &pev->point; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 405 | char *ptr, *tmp; |
| 406 | char c, nc = 0; |
| 407 | /* |
| 408 | * <Syntax> |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 409 | * perf probe [EVENT=]SRC[:LN|;PTN] |
| 410 | * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 411 | * |
| 412 | * TODO:Group name support |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 413 | */ |
| 414 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 415 | ptr = strpbrk(arg, ";=@+%"); |
| 416 | if (ptr && *ptr == '=') { /* Event name */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 417 | *ptr = '\0'; |
| 418 | tmp = ptr + 1; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 419 | if (strchr(arg, ':')) { |
| 420 | semantic_error("Group name is not supported yet.\n"); |
| 421 | return -ENOTSUP; |
| 422 | } |
| 423 | if (!check_event_name(arg)) { |
Masami Hiramatsu | b7702a2 | 2009-12-16 17:24:15 -0500 | [diff] [blame] | 424 | semantic_error("%s is bad for event name -it must " |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 425 | "follow C symbol-naming rule.\n", arg); |
| 426 | return -EINVAL; |
| 427 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 428 | pev->event = strdup(arg); |
| 429 | if (pev->event == NULL) |
| 430 | return -ENOMEM; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 431 | pev->group = NULL; |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 432 | arg = tmp; |
| 433 | } |
| 434 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 435 | ptr = strpbrk(arg, ";:+@%"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 436 | if (ptr) { |
| 437 | nc = *ptr; |
| 438 | *ptr++ = '\0'; |
| 439 | } |
| 440 | |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 441 | tmp = strdup(arg); |
| 442 | if (tmp == NULL) |
| 443 | return -ENOMEM; |
| 444 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 445 | /* Check arg is function or file and copy it */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 446 | if (strchr(tmp, '.')) /* File */ |
| 447 | pp->file = tmp; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 448 | else /* Function */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 449 | pp->function = tmp; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 450 | |
| 451 | /* Parse other options */ |
| 452 | while (ptr) { |
| 453 | arg = ptr; |
| 454 | c = nc; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 455 | if (c == ';') { /* Lazy pattern must be the last part */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 456 | pp->lazy_line = strdup(arg); |
| 457 | if (pp->lazy_line == NULL) |
| 458 | return -ENOMEM; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | ptr = strpbrk(arg, ";:+@%"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 462 | if (ptr) { |
| 463 | nc = *ptr; |
| 464 | *ptr++ = '\0'; |
| 465 | } |
| 466 | switch (c) { |
| 467 | case ':': /* Line number */ |
| 468 | pp->line = strtoul(arg, &tmp, 0); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 469 | if (*tmp != '\0') { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 470 | semantic_error("There is non-digit char" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 471 | " in line number.\n"); |
| 472 | return -EINVAL; |
| 473 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 474 | break; |
| 475 | case '+': /* Byte offset from a symbol */ |
| 476 | pp->offset = strtoul(arg, &tmp, 0); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 477 | if (*tmp != '\0') { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 478 | semantic_error("There is non-digit character" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 479 | " in offset.\n"); |
| 480 | return -EINVAL; |
| 481 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 482 | break; |
| 483 | case '@': /* File name */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 484 | if (pp->file) { |
| 485 | semantic_error("SRC@SRC is not allowed.\n"); |
| 486 | return -EINVAL; |
| 487 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 488 | pp->file = strdup(arg); |
| 489 | if (pp->file == NULL) |
| 490 | return -ENOMEM; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 491 | break; |
| 492 | case '%': /* Probe places */ |
| 493 | if (strcmp(arg, "return") == 0) { |
| 494 | pp->retprobe = 1; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 495 | } else { /* Others not supported yet */ |
| 496 | semantic_error("%%%s is not supported.\n", arg); |
| 497 | return -ENOTSUP; |
| 498 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 499 | break; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 500 | default: /* Buggy case */ |
| 501 | pr_err("This program has a bug at %s:%d.\n", |
| 502 | __FILE__, __LINE__); |
| 503 | return -ENOTSUP; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 504 | break; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* Exclusion check */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 509 | if (pp->lazy_line && pp->line) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 510 | semantic_error("Lazy pattern can't be used with line number."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 511 | return -EINVAL; |
| 512 | } |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 513 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 514 | if (pp->lazy_line && pp->offset) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 515 | semantic_error("Lazy pattern can't be used with offset."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 516 | return -EINVAL; |
| 517 | } |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 518 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 519 | if (pp->line && pp->offset) { |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 520 | semantic_error("Offset can't be used with line number."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 521 | return -EINVAL; |
| 522 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 523 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 524 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 525 | semantic_error("File always requires line number or " |
| 526 | "lazy pattern."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 527 | return -EINVAL; |
| 528 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 529 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 530 | if (pp->offset && !pp->function) { |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 531 | semantic_error("Offset requires an entry function."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 532 | return -EINVAL; |
| 533 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 534 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 535 | if (pp->retprobe && !pp->function) { |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 536 | semantic_error("Return probe requires an entry function."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 537 | return -EINVAL; |
| 538 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 539 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 540 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 541 | semantic_error("Offset/Line/Lazy pattern can't be used with " |
| 542 | "return probe."); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 543 | return -EINVAL; |
| 544 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 545 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 546 | 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] | 547 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, |
| 548 | pp->lazy_line); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 549 | return 0; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 550 | } |
| 551 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 552 | /* Parse perf-probe event argument */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 553 | static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 554 | { |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 555 | char *tmp; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 556 | struct perf_probe_arg_field **fieldp; |
| 557 | |
| 558 | pr_debug("parsing arg: %s into ", str); |
| 559 | |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 560 | tmp = strchr(str, '='); |
| 561 | if (tmp) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 562 | arg->name = strndup(str, tmp - str); |
| 563 | if (arg->name == NULL) |
| 564 | return -ENOMEM; |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 565 | pr_debug("name:%s ", arg->name); |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 566 | str = tmp + 1; |
| 567 | } |
| 568 | |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 569 | tmp = strchr(str, ':'); |
| 570 | if (tmp) { /* Type setting */ |
| 571 | *tmp = '\0'; |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 572 | arg->type = strdup(tmp + 1); |
| 573 | if (arg->type == NULL) |
| 574 | return -ENOMEM; |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 575 | pr_debug("type:%s ", arg->type); |
| 576 | } |
| 577 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 578 | tmp = strpbrk(str, "-."); |
| 579 | if (!is_c_varname(str) || !tmp) { |
| 580 | /* A variable, register, symbol or special value */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 581 | arg->var = strdup(str); |
| 582 | if (arg->var == NULL) |
| 583 | return -ENOMEM; |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 584 | pr_debug("%s\n", arg->var); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 585 | return 0; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* Structure fields */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 589 | arg->var = strndup(str, tmp - str); |
| 590 | if (arg->var == NULL) |
| 591 | return -ENOMEM; |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 592 | pr_debug("%s, ", arg->var); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 593 | fieldp = &arg->field; |
| 594 | |
| 595 | do { |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 596 | *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); |
| 597 | if (*fieldp == NULL) |
| 598 | return -ENOMEM; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 599 | if (*tmp == '.') { |
| 600 | str = tmp + 1; |
| 601 | (*fieldp)->ref = false; |
| 602 | } else if (tmp[1] == '>') { |
| 603 | str = tmp + 2; |
| 604 | (*fieldp)->ref = true; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 605 | } else { |
| 606 | semantic_error("Argument parse error: %s\n", str); |
| 607 | return -EINVAL; |
| 608 | } |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 609 | |
| 610 | tmp = strpbrk(str, "-."); |
| 611 | if (tmp) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 612 | (*fieldp)->name = strndup(str, tmp - str); |
| 613 | if ((*fieldp)->name == NULL) |
| 614 | return -ENOMEM; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 615 | pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); |
| 616 | fieldp = &(*fieldp)->next; |
| 617 | } |
| 618 | } while (tmp); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 619 | (*fieldp)->name = strdup(str); |
| 620 | if ((*fieldp)->name == NULL) |
| 621 | return -ENOMEM; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 622 | pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); |
Masami Hiramatsu | df0faf4 | 2010-04-12 13:17:00 -0400 | [diff] [blame] | 623 | |
| 624 | /* If no name is specified, set the last field name */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 625 | if (!arg->name) { |
| 626 | arg->name = strdup((*fieldp)->name); |
| 627 | if (arg->name == NULL) |
| 628 | return -ENOMEM; |
| 629 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 630 | return 0; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 631 | } |
| 632 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 633 | /* Parse perf-probe event command */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 634 | int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 635 | { |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 636 | char **argv; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 637 | int argc, i, ret = 0; |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 638 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 639 | argv = argv_split(cmd, &argc); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 640 | if (!argv) { |
| 641 | pr_debug("Failed to split arguments.\n"); |
| 642 | return -ENOMEM; |
| 643 | } |
| 644 | if (argc - 1 > MAX_PROBE_ARGS) { |
| 645 | semantic_error("Too many probe arguments (%d).\n", argc - 1); |
| 646 | ret = -ERANGE; |
| 647 | goto out; |
| 648 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 649 | /* Parse probe point */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 650 | ret = parse_perf_probe_point(argv[0], pev); |
| 651 | if (ret < 0) |
| 652 | goto out; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 653 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 654 | /* Copy arguments and ensure return probe has no C argument */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 655 | pev->nargs = argc - 1; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 656 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
| 657 | if (pev->args == NULL) { |
| 658 | ret = -ENOMEM; |
| 659 | goto out; |
| 660 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 661 | for (i = 0; i < pev->nargs && ret >= 0; i++) { |
| 662 | ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); |
| 663 | if (ret >= 0 && |
| 664 | is_c_varname(pev->args[i].var) && pev->point.retprobe) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 665 | semantic_error("You can't specify local variable for" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 666 | " kretprobe.\n"); |
| 667 | ret = -EINVAL; |
| 668 | } |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 669 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 670 | out: |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 671 | argv_free(argv); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 672 | |
| 673 | return ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 674 | } |
| 675 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 676 | /* Return true if this perf_probe_event requires debuginfo */ |
| 677 | bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 678 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 679 | int i; |
| 680 | |
| 681 | if (pev->point.file || pev->point.line || pev->point.lazy_line) |
| 682 | return true; |
| 683 | |
| 684 | for (i = 0; i < pev->nargs; i++) |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 685 | if (is_c_varname(pev->args[i].var)) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 686 | return true; |
| 687 | |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | /* Parse kprobe_events event into struct probe_point */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 692 | int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 693 | { |
| 694 | struct kprobe_trace_point *tp = &tev->point; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 695 | char pr; |
| 696 | char *p; |
| 697 | int ret, i, argc; |
| 698 | char **argv; |
| 699 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 700 | pr_debug("Parsing kprobe_events: %s\n", cmd); |
| 701 | argv = argv_split(cmd, &argc); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 702 | if (!argv) { |
| 703 | pr_debug("Failed to split arguments.\n"); |
| 704 | return -ENOMEM; |
| 705 | } |
| 706 | if (argc < 2) { |
| 707 | semantic_error("Too few probe arguments.\n"); |
| 708 | ret = -ERANGE; |
| 709 | goto out; |
| 710 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 711 | |
| 712 | /* Scan event and group name. */ |
Liming Wang | 93aaa45 | 2009-12-02 16:42:54 +0800 | [diff] [blame] | 713 | ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 714 | &pr, (float *)(void *)&tev->group, |
| 715 | (float *)(void *)&tev->event); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 716 | if (ret != 3) { |
| 717 | semantic_error("Failed to parse event name: %s\n", argv[0]); |
| 718 | ret = -EINVAL; |
| 719 | goto out; |
| 720 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 721 | 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] | 722 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 723 | tp->retprobe = (pr == 'r'); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 724 | |
| 725 | /* Scan function name and offset */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 726 | ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol, |
| 727 | &tp->offset); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 728 | if (ret == 1) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 729 | tp->offset = 0; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 730 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 731 | tev->nargs = argc - 2; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 732 | tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs); |
| 733 | if (tev->args == NULL) { |
| 734 | ret = -ENOMEM; |
| 735 | goto out; |
| 736 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 737 | for (i = 0; i < tev->nargs; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 738 | p = strchr(argv[i + 2], '='); |
| 739 | if (p) /* We don't need which register is assigned. */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 740 | *p++ = '\0'; |
| 741 | else |
| 742 | p = argv[i + 2]; |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 743 | tev->args[i].name = strdup(argv[i + 2]); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 744 | /* TODO: parse regs and offset */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 745 | tev->args[i].value = strdup(p); |
| 746 | if (tev->args[i].name == NULL || tev->args[i].value == NULL) { |
| 747 | ret = -ENOMEM; |
| 748 | goto out; |
| 749 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 750 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 751 | ret = 0; |
| 752 | out: |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 753 | argv_free(argv); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 754 | return ret; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 755 | } |
| 756 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 757 | /* Compose only probe arg */ |
| 758 | int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len) |
| 759 | { |
| 760 | struct perf_probe_arg_field *field = pa->field; |
| 761 | int ret; |
| 762 | char *tmp = buf; |
| 763 | |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 764 | if (pa->name && pa->var) |
| 765 | ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var); |
| 766 | else |
| 767 | ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 768 | if (ret <= 0) |
| 769 | goto error; |
| 770 | tmp += ret; |
| 771 | len -= ret; |
| 772 | |
| 773 | while (field) { |
| 774 | ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".", |
| 775 | field->name); |
| 776 | if (ret <= 0) |
| 777 | goto error; |
| 778 | tmp += ret; |
| 779 | len -= ret; |
| 780 | field = field->next; |
| 781 | } |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 782 | |
| 783 | if (pa->type) { |
| 784 | ret = e_snprintf(tmp, len, ":%s", pa->type); |
| 785 | if (ret <= 0) |
| 786 | goto error; |
| 787 | tmp += ret; |
| 788 | len -= ret; |
| 789 | } |
| 790 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 791 | return tmp - buf; |
| 792 | error: |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 793 | pr_debug("Failed to synthesize perf probe argument: %s", |
| 794 | strerror(-ret)); |
| 795 | return ret; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 796 | } |
| 797 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 798 | /* Compose only probe point (not argument) */ |
| 799 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 800 | { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 801 | char *buf, *tmp; |
| 802 | char offs[32] = "", line[32] = "", file[32] = ""; |
| 803 | int ret, len; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 804 | |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 805 | buf = zalloc(MAX_CMDLEN); |
| 806 | if (buf == NULL) { |
| 807 | ret = -ENOMEM; |
| 808 | goto error; |
| 809 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 810 | if (pp->offset) { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 811 | ret = e_snprintf(offs, 32, "+%lu", pp->offset); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 812 | if (ret <= 0) |
| 813 | goto error; |
| 814 | } |
| 815 | if (pp->line) { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 816 | ret = e_snprintf(line, 32, ":%d", pp->line); |
| 817 | if (ret <= 0) |
| 818 | goto error; |
| 819 | } |
| 820 | if (pp->file) { |
Masami Hiramatsu | dd259c5 | 2010-04-14 18:39:35 -0400 | [diff] [blame] | 821 | len = strlen(pp->file) - 31; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 822 | if (len < 0) |
| 823 | len = 0; |
| 824 | tmp = strchr(pp->file + len, '/'); |
| 825 | if (!tmp) |
Masami Hiramatsu | dd259c5 | 2010-04-14 18:39:35 -0400 | [diff] [blame] | 826 | tmp = pp->file + len; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 827 | ret = e_snprintf(file, 32, "@%s", tmp + 1); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 828 | if (ret <= 0) |
| 829 | goto error; |
| 830 | } |
| 831 | |
| 832 | if (pp->function) |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 833 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function, |
| 834 | offs, pp->retprobe ? "%return" : "", line, |
| 835 | file); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 836 | else |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 837 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 838 | if (ret <= 0) |
| 839 | goto error; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 840 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 841 | return buf; |
| 842 | error: |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 843 | pr_debug("Failed to synthesize perf probe point: %s", |
| 844 | strerror(-ret)); |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 845 | if (buf) |
| 846 | free(buf); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 847 | return NULL; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | #if 0 |
| 851 | char *synthesize_perf_probe_command(struct perf_probe_event *pev) |
| 852 | { |
| 853 | char *buf; |
| 854 | int i, len, ret; |
| 855 | |
| 856 | buf = synthesize_perf_probe_point(&pev->point); |
| 857 | if (!buf) |
| 858 | return NULL; |
| 859 | |
| 860 | len = strlen(buf); |
| 861 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 862 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 863 | pev->args[i].name); |
| 864 | if (ret <= 0) { |
| 865 | free(buf); |
| 866 | return NULL; |
| 867 | } |
| 868 | len += ret; |
| 869 | } |
| 870 | |
| 871 | return buf; |
| 872 | } |
| 873 | #endif |
| 874 | |
| 875 | static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref, |
| 876 | char **buf, size_t *buflen, |
| 877 | int depth) |
| 878 | { |
| 879 | int ret; |
| 880 | if (ref->next) { |
| 881 | depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf, |
| 882 | buflen, depth + 1); |
| 883 | if (depth < 0) |
| 884 | goto out; |
| 885 | } |
| 886 | |
| 887 | ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset); |
| 888 | if (ret < 0) |
| 889 | depth = ret; |
| 890 | else { |
| 891 | *buf += ret; |
| 892 | *buflen -= ret; |
| 893 | } |
| 894 | out: |
| 895 | return depth; |
| 896 | |
| 897 | } |
| 898 | |
| 899 | static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg, |
| 900 | char *buf, size_t buflen) |
| 901 | { |
| 902 | int ret, depth = 0; |
| 903 | char *tmp = buf; |
| 904 | |
| 905 | /* Argument name or separator */ |
| 906 | if (arg->name) |
| 907 | ret = e_snprintf(buf, buflen, " %s=", arg->name); |
| 908 | else |
| 909 | ret = e_snprintf(buf, buflen, " "); |
| 910 | if (ret < 0) |
| 911 | return ret; |
| 912 | buf += ret; |
| 913 | buflen -= ret; |
| 914 | |
| 915 | /* Dereferencing arguments */ |
| 916 | if (arg->ref) { |
| 917 | depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf, |
| 918 | &buflen, 1); |
| 919 | if (depth < 0) |
| 920 | return depth; |
| 921 | } |
| 922 | |
| 923 | /* Print argument value */ |
| 924 | ret = e_snprintf(buf, buflen, "%s", arg->value); |
| 925 | if (ret < 0) |
| 926 | return ret; |
| 927 | buf += ret; |
| 928 | buflen -= ret; |
| 929 | |
| 930 | /* Closing */ |
| 931 | while (depth--) { |
| 932 | ret = e_snprintf(buf, buflen, ")"); |
| 933 | if (ret < 0) |
| 934 | return ret; |
| 935 | buf += ret; |
| 936 | buflen -= ret; |
| 937 | } |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 938 | /* Print argument type */ |
| 939 | if (arg->type) { |
| 940 | ret = e_snprintf(buf, buflen, ":%s", arg->type); |
| 941 | if (ret <= 0) |
| 942 | return ret; |
| 943 | buf += ret; |
| 944 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 945 | |
| 946 | return buf - tmp; |
| 947 | } |
| 948 | |
| 949 | char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev) |
| 950 | { |
| 951 | struct kprobe_trace_point *tp = &tev->point; |
| 952 | char *buf; |
| 953 | int i, len, ret; |
| 954 | |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 955 | buf = zalloc(MAX_CMDLEN); |
| 956 | if (buf == NULL) |
| 957 | return NULL; |
| 958 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 959 | len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu", |
| 960 | tp->retprobe ? 'r' : 'p', |
| 961 | tev->group, tev->event, |
| 962 | tp->symbol, tp->offset); |
| 963 | if (len <= 0) |
| 964 | goto error; |
| 965 | |
| 966 | for (i = 0; i < tev->nargs; i++) { |
| 967 | ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len, |
| 968 | MAX_CMDLEN - len); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 969 | if (ret <= 0) |
| 970 | goto error; |
| 971 | len += ret; |
| 972 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 973 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 974 | return buf; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 975 | error: |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 976 | free(buf); |
| 977 | return NULL; |
| 978 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 979 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 980 | int convert_to_perf_probe_event(struct kprobe_trace_event *tev, |
| 981 | struct perf_probe_event *pev) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 982 | { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 983 | char buf[64] = ""; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 984 | int i, ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 985 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 986 | /* Convert event/group name */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 987 | pev->event = strdup(tev->event); |
| 988 | pev->group = strdup(tev->group); |
| 989 | if (pev->event == NULL || pev->group == NULL) |
| 990 | return -ENOMEM; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 991 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 992 | /* Convert trace_point to probe_point */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 993 | ret = convert_to_perf_probe_point(&tev->point, &pev->point); |
| 994 | if (ret < 0) |
| 995 | return ret; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 996 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 997 | /* Convert trace_arg to probe_arg */ |
| 998 | pev->nargs = tev->nargs; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 999 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
| 1000 | if (pev->args == NULL) |
| 1001 | return -ENOMEM; |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1002 | for (i = 0; i < tev->nargs && ret >= 0; i++) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1003 | if (tev->args[i].name) |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1004 | pev->args[i].name = strdup(tev->args[i].name); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1005 | else { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1006 | ret = synthesize_kprobe_trace_arg(&tev->args[i], |
| 1007 | buf, 64); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1008 | pev->args[i].name = strdup(buf); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1009 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1010 | if (pev->args[i].name == NULL && ret >= 0) |
| 1011 | ret = -ENOMEM; |
| 1012 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1013 | |
| 1014 | if (ret < 0) |
| 1015 | clear_perf_probe_event(pev); |
| 1016 | |
| 1017 | return ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | void clear_perf_probe_event(struct perf_probe_event *pev) |
| 1021 | { |
| 1022 | struct perf_probe_point *pp = &pev->point; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1023 | struct perf_probe_arg_field *field, *next; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1024 | int i; |
| 1025 | |
| 1026 | if (pev->event) |
| 1027 | free(pev->event); |
| 1028 | if (pev->group) |
| 1029 | free(pev->group); |
| 1030 | if (pp->file) |
| 1031 | free(pp->file); |
| 1032 | if (pp->function) |
| 1033 | free(pp->function); |
| 1034 | if (pp->lazy_line) |
| 1035 | free(pp->lazy_line); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1036 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1037 | if (pev->args[i].name) |
| 1038 | free(pev->args[i].name); |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1039 | if (pev->args[i].var) |
| 1040 | free(pev->args[i].var); |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 1041 | if (pev->args[i].type) |
| 1042 | free(pev->args[i].type); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1043 | field = pev->args[i].field; |
| 1044 | while (field) { |
| 1045 | next = field->next; |
| 1046 | if (field->name) |
| 1047 | free(field->name); |
| 1048 | free(field); |
| 1049 | field = next; |
| 1050 | } |
| 1051 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1052 | if (pev->args) |
| 1053 | free(pev->args); |
| 1054 | memset(pev, 0, sizeof(*pev)); |
| 1055 | } |
| 1056 | |
| 1057 | void clear_kprobe_trace_event(struct kprobe_trace_event *tev) |
| 1058 | { |
| 1059 | struct kprobe_trace_arg_ref *ref, *next; |
| 1060 | int i; |
| 1061 | |
| 1062 | if (tev->event) |
| 1063 | free(tev->event); |
| 1064 | if (tev->group) |
| 1065 | free(tev->group); |
| 1066 | if (tev->point.symbol) |
| 1067 | free(tev->point.symbol); |
| 1068 | for (i = 0; i < tev->nargs; i++) { |
| 1069 | if (tev->args[i].name) |
| 1070 | free(tev->args[i].name); |
| 1071 | if (tev->args[i].value) |
| 1072 | free(tev->args[i].value); |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 1073 | if (tev->args[i].type) |
| 1074 | free(tev->args[i].type); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1075 | ref = tev->args[i].ref; |
| 1076 | while (ref) { |
| 1077 | next = ref->next; |
| 1078 | free(ref); |
| 1079 | ref = next; |
| 1080 | } |
| 1081 | } |
| 1082 | if (tev->args) |
| 1083 | free(tev->args); |
| 1084 | memset(tev, 0, sizeof(*tev)); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1085 | } |
| 1086 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1087 | static int open_kprobe_events(bool readwrite) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1088 | { |
| 1089 | char buf[PATH_MAX]; |
Masami Hiramatsu | 7ca5989 | 2010-04-14 18:39:28 -0400 | [diff] [blame] | 1090 | const char *__debugfs; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1091 | int ret; |
| 1092 | |
Masami Hiramatsu | 7ca5989 | 2010-04-14 18:39:28 -0400 | [diff] [blame] | 1093 | __debugfs = debugfs_find_mountpoint(); |
| 1094 | if (__debugfs == NULL) { |
| 1095 | pr_warning("Debugfs is not mounted.\n"); |
| 1096 | return -ENOENT; |
| 1097 | } |
| 1098 | |
| 1099 | ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1100 | if (ret >= 0) { |
Masami Hiramatsu | 7ca5989 | 2010-04-14 18:39:28 -0400 | [diff] [blame] | 1101 | pr_debug("Opening %s write=%d\n", buf, readwrite); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1102 | if (readwrite && !probe_event_dry_run) |
| 1103 | ret = open(buf, O_RDWR, O_APPEND); |
| 1104 | else |
| 1105 | ret = open(buf, O_RDONLY, 0); |
| 1106 | } |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1107 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1108 | if (ret < 0) { |
| 1109 | if (errno == ENOENT) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1110 | pr_warning("kprobe_events file does not exist - please" |
| 1111 | " rebuild kernel with CONFIG_KPROBE_EVENT.\n"); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1112 | else |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1113 | pr_warning("Failed to open kprobe_events file: %s\n", |
| 1114 | strerror(errno)); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1115 | } |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
| 1119 | /* Get raw string list of current kprobe_events */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1120 | static struct strlist *get_kprobe_trace_command_rawlist(int fd) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1121 | { |
| 1122 | int ret, idx; |
| 1123 | FILE *fp; |
| 1124 | char buf[MAX_CMDLEN]; |
| 1125 | char *p; |
| 1126 | struct strlist *sl; |
| 1127 | |
| 1128 | sl = strlist__new(true, NULL); |
| 1129 | |
| 1130 | fp = fdopen(dup(fd), "r"); |
| 1131 | while (!feof(fp)) { |
| 1132 | p = fgets(buf, MAX_CMDLEN, fp); |
| 1133 | if (!p) |
| 1134 | break; |
| 1135 | |
| 1136 | idx = strlen(p) - 1; |
| 1137 | if (p[idx] == '\n') |
| 1138 | p[idx] = '\0'; |
| 1139 | ret = strlist__add(sl, buf); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1140 | if (ret < 0) { |
| 1141 | pr_debug("strlist__add failed: %s\n", strerror(-ret)); |
| 1142 | strlist__delete(sl); |
| 1143 | return NULL; |
| 1144 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1145 | } |
| 1146 | fclose(fp); |
| 1147 | |
| 1148 | return sl; |
| 1149 | } |
| 1150 | |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1151 | /* Show an event */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1152 | static int show_perf_probe_event(struct perf_probe_event *pev) |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1153 | { |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 1154 | int i, ret; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1155 | char buf[128]; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1156 | char *place; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1157 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1158 | /* Synthesize only event probe point */ |
| 1159 | place = synthesize_perf_probe_point(&pev->point); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1160 | if (!place) |
| 1161 | return -EINVAL; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1162 | |
| 1163 | ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event); |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 1164 | if (ret < 0) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1165 | return ret; |
| 1166 | |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1167 | printf(" %-20s (on %s", buf, place); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1168 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1169 | if (pev->nargs > 0) { |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1170 | printf(" with"); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1171 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1172 | ret = synthesize_perf_probe_arg(&pev->args[i], |
| 1173 | buf, 128); |
| 1174 | if (ret < 0) |
| 1175 | break; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1176 | printf(" %s", buf); |
| 1177 | } |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1178 | } |
| 1179 | printf(")\n"); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1180 | free(place); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1181 | return ret; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 1182 | } |
| 1183 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1184 | /* List up current perf-probe events */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1185 | int show_perf_probe_events(void) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1186 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1187 | int fd, ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1188 | struct kprobe_trace_event tev; |
| 1189 | struct perf_probe_event pev; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1190 | struct strlist *rawlist; |
| 1191 | struct str_node *ent; |
| 1192 | |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 1193 | setup_pager(); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1194 | ret = init_vmlinux(); |
| 1195 | if (ret < 0) |
| 1196 | return ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1197 | |
| 1198 | memset(&tev, 0, sizeof(tev)); |
| 1199 | memset(&pev, 0, sizeof(pev)); |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 1200 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1201 | fd = open_kprobe_events(false); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1202 | if (fd < 0) |
| 1203 | return fd; |
| 1204 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1205 | rawlist = get_kprobe_trace_command_rawlist(fd); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1206 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1207 | if (!rawlist) |
| 1208 | return -ENOENT; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1209 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 1210 | strlist__for_each(ent, rawlist) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1211 | ret = parse_kprobe_trace_command(ent->s, &tev); |
| 1212 | if (ret >= 0) { |
| 1213 | ret = convert_to_perf_probe_event(&tev, &pev); |
| 1214 | if (ret >= 0) |
| 1215 | ret = show_perf_probe_event(&pev); |
| 1216 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1217 | clear_perf_probe_event(&pev); |
| 1218 | clear_kprobe_trace_event(&tev); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1219 | if (ret < 0) |
| 1220 | break; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1221 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1222 | strlist__delete(rawlist); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1223 | |
| 1224 | return ret; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1225 | } |
| 1226 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1227 | /* Get current perf-probe event names */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1228 | 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] | 1229 | { |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1230 | char buf[128]; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1231 | struct strlist *sl, *rawlist; |
| 1232 | struct str_node *ent; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1233 | struct kprobe_trace_event tev; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1234 | int ret = 0; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1235 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1236 | memset(&tev, 0, sizeof(tev)); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1237 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1238 | rawlist = get_kprobe_trace_command_rawlist(fd); |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 1239 | sl = strlist__new(true, NULL); |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 1240 | strlist__for_each(ent, rawlist) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1241 | ret = parse_kprobe_trace_command(ent->s, &tev); |
| 1242 | if (ret < 0) |
| 1243 | break; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1244 | if (include_group) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1245 | ret = e_snprintf(buf, 128, "%s:%s", tev.group, |
| 1246 | tev.event); |
| 1247 | if (ret >= 0) |
| 1248 | ret = strlist__add(sl, buf); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1249 | } else |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1250 | ret = strlist__add(sl, tev.event); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1251 | clear_kprobe_trace_event(&tev); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1252 | if (ret < 0) |
| 1253 | break; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1254 | } |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1255 | strlist__delete(rawlist); |
| 1256 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1257 | if (ret < 0) { |
| 1258 | strlist__delete(sl); |
| 1259 | return NULL; |
| 1260 | } |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1261 | return sl; |
| 1262 | } |
| 1263 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1264 | static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1265 | { |
| 1266 | int ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1267 | char *buf = synthesize_kprobe_trace_command(tev); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1268 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1269 | if (!buf) { |
| 1270 | pr_debug("Failed to synthesize kprobe trace event.\n"); |
| 1271 | return -EINVAL; |
| 1272 | } |
| 1273 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1274 | pr_debug("Writing event: %s\n", buf); |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1275 | if (!probe_event_dry_run) { |
| 1276 | ret = write(fd, buf, strlen(buf)); |
| 1277 | if (ret <= 0) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1278 | pr_warning("Failed to write event: %s\n", |
| 1279 | strerror(errno)); |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1280 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1281 | free(buf); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1282 | return ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1283 | } |
| 1284 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1285 | static int get_new_event_name(char *buf, size_t len, const char *base, |
| 1286 | struct strlist *namelist, bool allow_suffix) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1287 | { |
| 1288 | int i, ret; |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 1289 | |
| 1290 | /* Try no suffix */ |
| 1291 | ret = e_snprintf(buf, len, "%s", base); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1292 | if (ret < 0) { |
| 1293 | pr_debug("snprintf() failed: %s\n", strerror(-ret)); |
| 1294 | return ret; |
| 1295 | } |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 1296 | if (!strlist__has_entry(namelist, buf)) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1297 | return 0; |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 1298 | |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 1299 | if (!allow_suffix) { |
| 1300 | pr_warning("Error: event \"%s\" already exists. " |
| 1301 | "(Use -f to force duplicates.)\n", base); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1302 | return -EEXIST; |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 1303 | } |
| 1304 | |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 1305 | /* Try to add suffix */ |
| 1306 | for (i = 1; i < MAX_EVENT_INDEX; i++) { |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1307 | ret = e_snprintf(buf, len, "%s_%d", base, i); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1308 | if (ret < 0) { |
| 1309 | pr_debug("snprintf() failed: %s\n", strerror(-ret)); |
| 1310 | return ret; |
| 1311 | } |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1312 | if (!strlist__has_entry(namelist, buf)) |
| 1313 | break; |
| 1314 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1315 | if (i == MAX_EVENT_INDEX) { |
| 1316 | pr_warning("Too many events are on the same function.\n"); |
| 1317 | ret = -ERANGE; |
| 1318 | } |
| 1319 | |
| 1320 | return ret; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1321 | } |
| 1322 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1323 | static int __add_kprobe_trace_events(struct perf_probe_event *pev, |
| 1324 | struct kprobe_trace_event *tevs, |
| 1325 | int ntevs, bool allow_suffix) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1326 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1327 | int i, fd, ret; |
Ingo Molnar | 5563277 | 2010-03-18 16:51:16 +0100 | [diff] [blame] | 1328 | struct kprobe_trace_event *tev = NULL; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1329 | char buf[64]; |
| 1330 | const char *event, *group; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1331 | struct strlist *namelist; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1332 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1333 | fd = open_kprobe_events(true); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1334 | if (fd < 0) |
| 1335 | return fd; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 1336 | /* Get current event names */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1337 | namelist = get_kprobe_trace_event_names(fd, false); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1338 | if (!namelist) { |
| 1339 | pr_debug("Failed to get current event list.\n"); |
| 1340 | return -EIO; |
| 1341 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1342 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1343 | ret = 0; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1344 | printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":"); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1345 | for (i = 0; i < ntevs; i++) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1346 | tev = &tevs[i]; |
| 1347 | if (pev->event) |
| 1348 | event = pev->event; |
| 1349 | else |
| 1350 | if (pev->point.function) |
| 1351 | event = pev->point.function; |
| 1352 | else |
| 1353 | event = tev->point.symbol; |
| 1354 | if (pev->group) |
| 1355 | group = pev->group; |
| 1356 | else |
| 1357 | group = PERFPROBE_GROUP; |
| 1358 | |
| 1359 | /* Get an unused new event name */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1360 | ret = get_new_event_name(buf, 64, event, |
| 1361 | namelist, allow_suffix); |
| 1362 | if (ret < 0) |
| 1363 | break; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1364 | event = buf; |
| 1365 | |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1366 | tev->event = strdup(event); |
| 1367 | tev->group = strdup(group); |
| 1368 | if (tev->event == NULL || tev->group == NULL) { |
| 1369 | ret = -ENOMEM; |
| 1370 | break; |
| 1371 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1372 | ret = write_kprobe_trace_event(fd, tev); |
| 1373 | if (ret < 0) |
| 1374 | break; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1375 | /* Add added event name to namelist */ |
| 1376 | strlist__add(namelist, event); |
| 1377 | |
| 1378 | /* Trick here - save current event/group */ |
| 1379 | event = pev->event; |
| 1380 | group = pev->group; |
| 1381 | pev->event = tev->event; |
| 1382 | pev->group = tev->group; |
| 1383 | show_perf_probe_event(pev); |
| 1384 | /* Trick here - restore current event/group */ |
| 1385 | pev->event = (char *)event; |
| 1386 | pev->group = (char *)group; |
| 1387 | |
| 1388 | /* |
| 1389 | * Probes after the first probe which comes from same |
| 1390 | * user input are always allowed to add suffix, because |
| 1391 | * there might be several addresses corresponding to |
| 1392 | * one code line. |
| 1393 | */ |
| 1394 | allow_suffix = true; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1395 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1396 | |
| 1397 | if (ret >= 0) { |
| 1398 | /* Show how to use the event. */ |
| 1399 | printf("\nYou can now use it on all perf tools, such as:\n\n"); |
| 1400 | printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group, |
| 1401 | tev->event); |
| 1402 | } |
Masami Hiramatsu | a9b495b | 2009-12-08 17:02:47 -0500 | [diff] [blame] | 1403 | |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 1404 | strlist__delete(namelist); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1405 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1406 | return ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1407 | } |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1408 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1409 | static int convert_to_kprobe_trace_events(struct perf_probe_event *pev, |
| 1410 | struct kprobe_trace_event **tevs) |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1411 | { |
| 1412 | struct symbol *sym; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1413 | int ret = 0, i; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1414 | struct kprobe_trace_event *tev; |
| 1415 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 1416 | /* Convert perf_probe_event with debuginfo */ |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1417 | ret = try_to_find_kprobe_trace_events(pev, tevs); |
| 1418 | if (ret != 0) |
| 1419 | return ret; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1420 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1421 | /* Allocate trace event buffer */ |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1422 | tev = *tevs = zalloc(sizeof(struct kprobe_trace_event)); |
| 1423 | if (tev == NULL) |
| 1424 | return -ENOMEM; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1425 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1426 | /* Copy parameters */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1427 | tev->point.symbol = strdup(pev->point.function); |
| 1428 | if (tev->point.symbol == NULL) { |
| 1429 | ret = -ENOMEM; |
| 1430 | goto error; |
| 1431 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1432 | tev->point.offset = pev->point.offset; |
| 1433 | tev->nargs = pev->nargs; |
| 1434 | if (tev->nargs) { |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1435 | tev->args = zalloc(sizeof(struct kprobe_trace_arg) |
| 1436 | * tev->nargs); |
| 1437 | if (tev->args == NULL) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1438 | ret = -ENOMEM; |
| 1439 | goto error; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1440 | } |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1441 | for (i = 0; i < tev->nargs; i++) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1442 | if (pev->args[i].name) { |
| 1443 | tev->args[i].name = strdup(pev->args[i].name); |
| 1444 | if (tev->args[i].name == NULL) { |
| 1445 | ret = -ENOMEM; |
| 1446 | goto error; |
| 1447 | } |
| 1448 | } |
| 1449 | tev->args[i].value = strdup(pev->args[i].var); |
| 1450 | if (tev->args[i].value == NULL) { |
| 1451 | ret = -ENOMEM; |
| 1452 | goto error; |
| 1453 | } |
| 1454 | if (pev->args[i].type) { |
| 1455 | tev->args[i].type = strdup(pev->args[i].type); |
| 1456 | if (tev->args[i].type == NULL) { |
| 1457 | ret = -ENOMEM; |
| 1458 | goto error; |
| 1459 | } |
| 1460 | } |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1461 | } |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1462 | } |
| 1463 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1464 | /* Currently just checking function name from symbol map */ |
| 1465 | sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION], |
| 1466 | tev->point.symbol, NULL); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1467 | if (!sym) { |
| 1468 | pr_warning("Kernel symbol \'%s\' not found.\n", |
| 1469 | tev->point.symbol); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1470 | ret = -ENOENT; |
| 1471 | goto error; |
| 1472 | } |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1473 | |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1474 | return 1; |
| 1475 | error: |
| 1476 | clear_kprobe_trace_event(tev); |
| 1477 | free(tev); |
| 1478 | *tevs = NULL; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1479 | return ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | struct __event_package { |
| 1483 | struct perf_probe_event *pev; |
| 1484 | struct kprobe_trace_event *tevs; |
| 1485 | int ntevs; |
| 1486 | }; |
| 1487 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1488 | int add_perf_probe_events(struct perf_probe_event *pevs, int npevs, |
| 1489 | bool force_add) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1490 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1491 | int i, j, ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1492 | struct __event_package *pkgs; |
| 1493 | |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1494 | pkgs = zalloc(sizeof(struct __event_package) * npevs); |
| 1495 | if (pkgs == NULL) |
| 1496 | return -ENOMEM; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1497 | |
| 1498 | /* Init vmlinux path */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1499 | ret = init_vmlinux(); |
| 1500 | if (ret < 0) |
| 1501 | return ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1502 | |
| 1503 | /* Loop 1: convert all events */ |
| 1504 | for (i = 0; i < npevs; i++) { |
| 1505 | pkgs[i].pev = &pevs[i]; |
| 1506 | /* Convert with or without debuginfo */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1507 | ret = convert_to_kprobe_trace_events(pkgs[i].pev, |
| 1508 | &pkgs[i].tevs); |
| 1509 | if (ret < 0) |
| 1510 | goto end; |
| 1511 | pkgs[i].ntevs = ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | /* Loop 2: add all events */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1515 | for (i = 0; i < npevs && ret >= 0; i++) |
| 1516 | ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs, |
| 1517 | pkgs[i].ntevs, force_add); |
| 1518 | end: |
| 1519 | /* Loop 3: cleanup trace events */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1520 | for (i = 0; i < npevs; i++) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1521 | for (j = 0; j < pkgs[i].ntevs; j++) |
| 1522 | clear_kprobe_trace_event(&pkgs[i].tevs[j]); |
| 1523 | |
| 1524 | return ret; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 1525 | } |
| 1526 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1527 | static int __del_trace_kprobe_event(int fd, struct str_node *ent) |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1528 | { |
| 1529 | char *p; |
| 1530 | char buf[128]; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1531 | int ret; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1532 | |
| 1533 | /* Convert from perf-probe event to trace-kprobe event */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1534 | ret = e_snprintf(buf, 128, "-:%s", ent->s); |
| 1535 | if (ret < 0) |
| 1536 | goto error; |
| 1537 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1538 | p = strchr(buf + 2, ':'); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1539 | if (!p) { |
| 1540 | pr_debug("Internal error: %s should have ':' but not.\n", |
| 1541 | ent->s); |
| 1542 | ret = -ENOTSUP; |
| 1543 | goto error; |
| 1544 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1545 | *p = '/'; |
| 1546 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1547 | pr_debug("Writing event: %s\n", buf); |
| 1548 | ret = write(fd, buf, strlen(buf)); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1549 | if (ret < 0) |
| 1550 | goto error; |
| 1551 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1552 | printf("Remove event: %s\n", ent->s); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1553 | return 0; |
| 1554 | error: |
| 1555 | pr_warning("Failed to delete event: %s\n", strerror(-ret)); |
| 1556 | return ret; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1557 | } |
| 1558 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1559 | static int del_trace_kprobe_event(int fd, const char *group, |
| 1560 | const char *event, struct strlist *namelist) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1561 | { |
| 1562 | char buf[128]; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1563 | struct str_node *ent, *n; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1564 | int found = 0, ret = 0; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1565 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1566 | ret = e_snprintf(buf, 128, "%s:%s", group, event); |
| 1567 | if (ret < 0) { |
| 1568 | pr_err("Failed to copy event."); |
| 1569 | return ret; |
| 1570 | } |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1571 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1572 | if (strpbrk(buf, "*?")) { /* Glob-exp */ |
| 1573 | strlist__for_each_safe(ent, n, namelist) |
| 1574 | if (strglobmatch(ent->s, buf)) { |
| 1575 | found++; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1576 | ret = __del_trace_kprobe_event(fd, ent); |
| 1577 | if (ret < 0) |
| 1578 | break; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1579 | strlist__remove(namelist, ent); |
| 1580 | } |
| 1581 | } else { |
| 1582 | ent = strlist__find(namelist, buf); |
| 1583 | if (ent) { |
| 1584 | found++; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1585 | ret = __del_trace_kprobe_event(fd, ent); |
| 1586 | if (ret >= 0) |
| 1587 | strlist__remove(namelist, ent); |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1588 | } |
| 1589 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1590 | if (found == 0 && ret >= 0) |
| 1591 | pr_info("Info: Event \"%s\" does not exist.\n", buf); |
| 1592 | |
| 1593 | return ret; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1594 | } |
| 1595 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1596 | int del_perf_probe_events(struct strlist *dellist) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1597 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1598 | int fd, ret = 0; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1599 | const char *group, *event; |
| 1600 | char *p, *str; |
| 1601 | struct str_node *ent; |
| 1602 | struct strlist *namelist; |
| 1603 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1604 | fd = open_kprobe_events(true); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1605 | if (fd < 0) |
| 1606 | return fd; |
| 1607 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1608 | /* Get current event names */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1609 | namelist = get_kprobe_trace_event_names(fd, true); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1610 | if (namelist == NULL) |
| 1611 | return -EINVAL; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1612 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 1613 | strlist__for_each(ent, dellist) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1614 | str = strdup(ent->s); |
| 1615 | if (str == NULL) { |
| 1616 | ret = -ENOMEM; |
| 1617 | break; |
| 1618 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1619 | pr_debug("Parsing: %s\n", str); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1620 | p = strchr(str, ':'); |
| 1621 | if (p) { |
| 1622 | group = str; |
| 1623 | *p = '\0'; |
| 1624 | event = p + 1; |
| 1625 | } else { |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1626 | group = "*"; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1627 | event = str; |
| 1628 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 1629 | pr_debug("Group: %s, Event: %s\n", group, event); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1630 | ret = del_trace_kprobe_event(fd, group, event, namelist); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1631 | free(str); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1632 | if (ret < 0) |
| 1633 | break; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1634 | } |
| 1635 | strlist__delete(namelist); |
| 1636 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1637 | |
| 1638 | return ret; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 1639 | } |
| 1640 | |