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