Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1 | /* |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2 | * probe-event.c : perf-probe definition to probe_events format converter |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 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 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 22 | #include <sys/utsname.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <errno.h> |
| 27 | #include <stdio.h> |
| 28 | #include <unistd.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 31 | #include <stdarg.h> |
| 32 | #include <limits.h> |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 33 | #include <elf.h> |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 34 | |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 35 | #include "util.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 36 | #include "event.h" |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 37 | #include "strlist.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 38 | #include "debug.h" |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 39 | #include "cache.h" |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 40 | #include "color.h" |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 41 | #include "symbol.h" |
| 42 | #include "thread.h" |
Borislav Petkov | 553873e | 2013-12-09 17:14:23 +0100 | [diff] [blame] | 43 | #include <api/fs/debugfs.h> |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 44 | #include <api/fs/tracefs.h> |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 45 | #include "trace-event.h" /* For __maybe_unused */ |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 46 | #include "probe-event.h" |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 47 | #include "probe-finder.h" |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 48 | #include "session.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 49 | |
| 50 | #define MAX_CMDLEN 256 |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 51 | #define PERFPROBE_GROUP "probe" |
| 52 | |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 53 | bool probe_event_dry_run; /* Dry run flag */ |
| 54 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 55 | #define semantic_error(msg ...) pr_err("Semantic error :" msg) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 56 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 57 | /* If there is no space to write, returns -E2BIG. */ |
| 58 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
Masami Hiramatsu | 8498845 | 2009-12-07 12:00:53 -0500 | [diff] [blame] | 59 | __attribute__((format(printf, 3, 4))); |
| 60 | |
| 61 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 62 | { |
| 63 | int ret; |
| 64 | va_list ap; |
| 65 | va_start(ap, format); |
| 66 | ret = vsnprintf(str, size, format, ap); |
| 67 | va_end(ap); |
| 68 | if (ret >= (int)size) |
| 69 | ret = -E2BIG; |
| 70 | return ret; |
| 71 | } |
| 72 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 73 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp); |
Masami Hiramatsu | 981d05a | 2014-01-16 09:39:44 +0000 | [diff] [blame] | 74 | static void clear_probe_trace_event(struct probe_trace_event *tev); |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 75 | static struct machine *host_machine; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 76 | |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 77 | /* Initialize symbol maps and path of vmlinux/modules */ |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 78 | static int init_symbol_maps(bool user_only) |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 79 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 80 | int ret; |
| 81 | |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 82 | symbol_conf.sort_by_name = true; |
Namhyung Kim | 680d926 | 2015-03-06 16:31:27 +0900 | [diff] [blame] | 83 | symbol_conf.allow_aliases = true; |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 84 | ret = symbol__init(NULL); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 85 | if (ret < 0) { |
| 86 | pr_debug("Failed to init symbol map.\n"); |
| 87 | goto out; |
| 88 | } |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 89 | |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 90 | if (host_machine || user_only) /* already initialized */ |
| 91 | return 0; |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 92 | |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 93 | if (symbol_conf.vmlinux_name) |
| 94 | pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); |
| 95 | |
| 96 | host_machine = machine__new_host(); |
| 97 | if (!host_machine) { |
| 98 | pr_debug("machine__new_host() failed.\n"); |
| 99 | symbol__exit(); |
| 100 | ret = -1; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 101 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 102 | out: |
| 103 | if (ret < 0) |
| 104 | pr_warning("Failed to init vmlinux path.\n"); |
| 105 | return ret; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 108 | static void exit_symbol_maps(void) |
| 109 | { |
| 110 | if (host_machine) { |
| 111 | machine__delete(host_machine); |
| 112 | host_machine = NULL; |
| 113 | } |
| 114 | symbol__exit(); |
| 115 | } |
| 116 | |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 117 | static struct symbol *__find_kernel_function_by_name(const char *name, |
| 118 | struct map **mapp) |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 119 | { |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 120 | return machine__find_kernel_function_by_name(host_machine, name, mapp, |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 121 | NULL); |
| 122 | } |
| 123 | |
Masami Hiramatsu | 8f33f7d | 2014-02-06 05:32:20 +0000 | [diff] [blame] | 124 | static struct symbol *__find_kernel_function(u64 addr, struct map **mapp) |
| 125 | { |
| 126 | return machine__find_kernel_function(host_machine, addr, mapp, NULL); |
| 127 | } |
| 128 | |
| 129 | static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void) |
| 130 | { |
| 131 | /* kmap->ref_reloc_sym should be set if host_machine is initialized */ |
| 132 | struct kmap *kmap; |
| 133 | |
| 134 | if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0) |
| 135 | return NULL; |
| 136 | |
| 137 | kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]); |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 138 | if (!kmap) |
| 139 | return NULL; |
Masami Hiramatsu | 8f33f7d | 2014-02-06 05:32:20 +0000 | [diff] [blame] | 140 | return kmap->ref_reloc_sym; |
| 141 | } |
| 142 | |
| 143 | static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc) |
| 144 | { |
| 145 | struct ref_reloc_sym *reloc_sym; |
| 146 | struct symbol *sym; |
| 147 | struct map *map; |
| 148 | |
| 149 | /* ref_reloc_sym is just a label. Need a special fix*/ |
| 150 | reloc_sym = kernel_get_ref_reloc_sym(); |
| 151 | if (reloc_sym && strcmp(name, reloc_sym->name) == 0) |
| 152 | return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr; |
| 153 | else { |
| 154 | sym = __find_kernel_function_by_name(name, &map); |
| 155 | if (sym) |
| 156 | return map->unmap_ip(map, sym->start) - |
He Kuang | f56847c | 2015-02-27 18:52:53 +0800 | [diff] [blame] | 157 | ((reloc) ? 0 : map->reloc); |
Masami Hiramatsu | 8f33f7d | 2014-02-06 05:32:20 +0000 | [diff] [blame] | 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 162 | static struct map *kernel_get_module_map(const char *module) |
| 163 | { |
| 164 | struct rb_node *nd; |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 165 | struct map_groups *grp = &host_machine->kmaps; |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 166 | |
Masami Hiramatsu | 14a8fd7 | 2011-06-27 16:27:51 +0900 | [diff] [blame] | 167 | /* A file path -- this is an offline module */ |
| 168 | if (module && strchr(module, '/')) |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 169 | return machine__new_module(host_machine, 0, module); |
Masami Hiramatsu | 14a8fd7 | 2011-06-27 16:27:51 +0900 | [diff] [blame] | 170 | |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 171 | if (!module) |
| 172 | module = "kernel"; |
| 173 | |
| 174 | for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) { |
| 175 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 176 | if (strncmp(pos->dso->short_name + 1, module, |
| 177 | pos->dso->short_name_len - 2) == 0) { |
| 178 | return pos; |
| 179 | } |
| 180 | } |
| 181 | return NULL; |
| 182 | } |
| 183 | |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 184 | static struct map *get_target_map(const char *target, bool user) |
| 185 | { |
| 186 | /* Init maps of given executable or kernel */ |
| 187 | if (user) |
| 188 | return dso__new_map(target); |
| 189 | else |
| 190 | return kernel_get_module_map(target); |
| 191 | } |
| 192 | |
| 193 | static void put_target_map(struct map *map, bool user) |
| 194 | { |
| 195 | if (map && user) { |
| 196 | /* Only the user map needs to be released */ |
| 197 | dso__delete(map->dso); |
| 198 | map__delete(map); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 203 | static struct dso *kernel_get_module_dso(const char *module) |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 204 | { |
| 205 | struct dso *dso; |
Franck Bui-Huu | fd930ff | 2010-12-10 14:06:03 +0100 | [diff] [blame] | 206 | struct map *map; |
| 207 | const char *vmlinux_name; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 208 | |
| 209 | if (module) { |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 210 | list_for_each_entry(dso, &host_machine->kernel_dsos.head, |
| 211 | node) { |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 212 | if (strncmp(dso->short_name + 1, module, |
| 213 | dso->short_name_len - 2) == 0) |
| 214 | goto found; |
| 215 | } |
| 216 | pr_debug("Failed to find module %s.\n", module); |
| 217 | return NULL; |
Franck Bui-Huu | fd930ff | 2010-12-10 14:06:03 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 220 | map = host_machine->vmlinux_maps[MAP__FUNCTION]; |
Franck Bui-Huu | fd930ff | 2010-12-10 14:06:03 +0100 | [diff] [blame] | 221 | dso = map->dso; |
| 222 | |
| 223 | vmlinux_name = symbol_conf.vmlinux_name; |
| 224 | if (vmlinux_name) { |
Arnaldo Carvalho de Melo | 5230fb7 | 2013-12-10 11:58:52 -0300 | [diff] [blame] | 225 | if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0) |
Franck Bui-Huu | fd930ff | 2010-12-10 14:06:03 +0100 | [diff] [blame] | 226 | return NULL; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 227 | } else { |
Franck Bui-Huu | c3a34e0 | 2010-12-10 14:07:14 +0100 | [diff] [blame] | 228 | if (dso__load_vmlinux_path(dso, map, NULL) <= 0) { |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 229 | pr_debug("Failed to load kernel map.\n"); |
| 230 | return NULL; |
| 231 | } |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 232 | } |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 233 | found: |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 234 | return dso; |
| 235 | } |
| 236 | |
| 237 | const char *kernel_get_module_path(const char *module) |
| 238 | { |
| 239 | struct dso *dso = kernel_get_module_dso(module); |
| 240 | return (dso) ? dso->long_name : NULL; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 241 | } |
| 242 | |
Masami Hiramatsu | fb7345b | 2013-12-26 05:41:53 +0000 | [diff] [blame] | 243 | static int convert_exec_to_group(const char *exec, char **result) |
| 244 | { |
| 245 | char *ptr1, *ptr2, *exec_copy; |
| 246 | char buf[64]; |
| 247 | int ret; |
| 248 | |
| 249 | exec_copy = strdup(exec); |
| 250 | if (!exec_copy) |
| 251 | return -ENOMEM; |
| 252 | |
| 253 | ptr1 = basename(exec_copy); |
| 254 | if (!ptr1) { |
| 255 | ret = -EINVAL; |
| 256 | goto out; |
| 257 | } |
| 258 | |
| 259 | ptr2 = strpbrk(ptr1, "-._"); |
| 260 | if (ptr2) |
| 261 | *ptr2 = '\0'; |
| 262 | ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1); |
| 263 | if (ret < 0) |
| 264 | goto out; |
| 265 | |
| 266 | *result = strdup(buf); |
| 267 | ret = *result ? 0 : -ENOMEM; |
| 268 | |
| 269 | out: |
| 270 | free(exec_copy); |
| 271 | return ret; |
| 272 | } |
| 273 | |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 274 | static void clear_perf_probe_point(struct perf_probe_point *pp) |
| 275 | { |
| 276 | free(pp->file); |
| 277 | free(pp->function); |
| 278 | free(pp->lazy_line); |
| 279 | } |
| 280 | |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 281 | static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs) |
| 282 | { |
| 283 | int i; |
| 284 | |
| 285 | for (i = 0; i < ntevs; i++) |
| 286 | clear_probe_trace_event(tevs + i); |
| 287 | } |
| 288 | |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 289 | #ifdef HAVE_DWARF_SUPPORT |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 290 | /* |
| 291 | * Some binaries like glibc have special symbols which are on the symbol |
| 292 | * table, but not in the debuginfo. If we can find the address of the |
| 293 | * symbol from map, we can translate the address back to the probe point. |
| 294 | */ |
| 295 | static int find_alternative_probe_point(struct debuginfo *dinfo, |
| 296 | struct perf_probe_point *pp, |
| 297 | struct perf_probe_point *result, |
| 298 | const char *target, bool uprobes) |
| 299 | { |
| 300 | struct map *map = NULL; |
| 301 | struct symbol *sym; |
| 302 | u64 address = 0; |
| 303 | int ret = -ENOENT; |
| 304 | |
| 305 | /* This can work only for function-name based one */ |
| 306 | if (!pp->function || pp->file) |
| 307 | return -ENOTSUP; |
| 308 | |
| 309 | map = get_target_map(target, uprobes); |
| 310 | if (!map) |
| 311 | return -EINVAL; |
| 312 | |
| 313 | /* Find the address of given function */ |
| 314 | map__for_each_symbol_by_name(map, pp->function, sym) { |
Masami Hiramatsu | e6d7c91 | 2015-03-22 20:40:22 +0900 | [diff] [blame] | 315 | if (uprobes) |
| 316 | address = sym->start; |
| 317 | else |
| 318 | address = map->unmap_ip(map, sym->start); |
Namhyung Kim | e578da3 | 2015-03-06 16:31:29 +0900 | [diff] [blame] | 319 | break; |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 320 | } |
| 321 | if (!address) { |
| 322 | ret = -ENOENT; |
| 323 | goto out; |
| 324 | } |
Wang Nan | f6c1562 | 2015-04-08 02:14:34 +0000 | [diff] [blame] | 325 | pr_debug("Symbol %s address found : %" PRIx64 "\n", |
| 326 | pp->function, address); |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 327 | |
| 328 | ret = debuginfo__find_probe_point(dinfo, (unsigned long)address, |
| 329 | result); |
| 330 | if (ret <= 0) |
| 331 | ret = (!ret) ? -ENOENT : ret; |
| 332 | else { |
| 333 | result->offset += pp->offset; |
| 334 | result->line += pp->line; |
He Kuang | 9d7b45c | 2015-04-13 19:41:28 +0800 | [diff] [blame] | 335 | result->retprobe = pp->retprobe; |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 336 | ret = 0; |
| 337 | } |
| 338 | |
| 339 | out: |
| 340 | put_target_map(map, uprobes); |
| 341 | return ret; |
| 342 | |
| 343 | } |
| 344 | |
| 345 | static int get_alternative_probe_event(struct debuginfo *dinfo, |
| 346 | struct perf_probe_event *pev, |
| 347 | struct perf_probe_point *tmp, |
| 348 | const char *target) |
| 349 | { |
| 350 | int ret; |
| 351 | |
| 352 | memcpy(tmp, &pev->point, sizeof(*tmp)); |
| 353 | memset(&pev->point, 0, sizeof(pev->point)); |
| 354 | ret = find_alternative_probe_point(dinfo, tmp, &pev->point, |
| 355 | target, pev->uprobes); |
| 356 | if (ret < 0) |
| 357 | memcpy(&pev->point, tmp, sizeof(*tmp)); |
| 358 | |
| 359 | return ret; |
| 360 | } |
Masami Hiramatsu | a15ad2f | 2014-02-06 05:32:27 +0000 | [diff] [blame] | 361 | |
Masami Hiramatsu | 811dd2a | 2015-03-06 16:31:22 +0900 | [diff] [blame] | 362 | static int get_alternative_line_range(struct debuginfo *dinfo, |
| 363 | struct line_range *lr, |
| 364 | const char *target, bool user) |
| 365 | { |
David Ahern | 6d4a489 | 2015-03-11 10:36:20 -0400 | [diff] [blame] | 366 | struct perf_probe_point pp = { .function = lr->function, |
| 367 | .file = lr->file, |
| 368 | .line = lr->start }; |
| 369 | struct perf_probe_point result; |
Masami Hiramatsu | 811dd2a | 2015-03-06 16:31:22 +0900 | [diff] [blame] | 370 | int ret, len = 0; |
| 371 | |
David Ahern | 6d4a489 | 2015-03-11 10:36:20 -0400 | [diff] [blame] | 372 | memset(&result, 0, sizeof(result)); |
| 373 | |
Masami Hiramatsu | 811dd2a | 2015-03-06 16:31:22 +0900 | [diff] [blame] | 374 | if (lr->end != INT_MAX) |
| 375 | len = lr->end - lr->start; |
| 376 | ret = find_alternative_probe_point(dinfo, &pp, &result, |
| 377 | target, user); |
| 378 | if (!ret) { |
| 379 | lr->function = result.function; |
| 380 | lr->file = result.file; |
| 381 | lr->start = result.line; |
| 382 | if (lr->end != INT_MAX) |
| 383 | lr->end = lr->start + len; |
| 384 | clear_perf_probe_point(&pp); |
| 385 | } |
| 386 | return ret; |
| 387 | } |
| 388 | |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 389 | /* Open new debuginfo of given module */ |
Masami Hiramatsu | 92561cb | 2014-08-15 01:44:32 +0000 | [diff] [blame] | 390 | static struct debuginfo *open_debuginfo(const char *module, bool silent) |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 391 | { |
Masami Hiramatsu | a15ad2f | 2014-02-06 05:32:27 +0000 | [diff] [blame] | 392 | const char *path = module; |
Masami Hiramatsu | 92561cb | 2014-08-15 01:44:32 +0000 | [diff] [blame] | 393 | struct debuginfo *ret; |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 394 | |
Masami Hiramatsu | a15ad2f | 2014-02-06 05:32:27 +0000 | [diff] [blame] | 395 | if (!module || !strchr(module, '/')) { |
Masami Hiramatsu | 14a8fd7 | 2011-06-27 16:27:51 +0900 | [diff] [blame] | 396 | path = kernel_get_module_path(module); |
Masami Hiramatsu | 14a8fd7 | 2011-06-27 16:27:51 +0900 | [diff] [blame] | 397 | if (!path) { |
Masami Hiramatsu | 92561cb | 2014-08-15 01:44:32 +0000 | [diff] [blame] | 398 | if (!silent) |
| 399 | pr_err("Failed to find path of %s module.\n", |
| 400 | module ?: "kernel"); |
Masami Hiramatsu | 14a8fd7 | 2011-06-27 16:27:51 +0900 | [diff] [blame] | 401 | return NULL; |
| 402 | } |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 403 | } |
Masami Hiramatsu | 92561cb | 2014-08-15 01:44:32 +0000 | [diff] [blame] | 404 | ret = debuginfo__new(path); |
| 405 | if (!ret && !silent) { |
| 406 | pr_warning("The %s file has no debug information.\n", path); |
| 407 | if (!module || !strtailcmp(path, ".ko")) |
| 408 | pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, "); |
| 409 | else |
| 410 | pr_warning("Rebuild with -g, "); |
| 411 | pr_warning("or install an appropriate debuginfo package.\n"); |
| 412 | } |
| 413 | return ret; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 414 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 415 | |
Masami Hiramatsu | 92561cb | 2014-08-15 01:44:32 +0000 | [diff] [blame] | 416 | |
Masami Hiramatsu | 99ca423 | 2014-01-16 09:39:49 +0000 | [diff] [blame] | 417 | static int get_text_start_address(const char *exec, unsigned long *address) |
| 418 | { |
| 419 | Elf *elf; |
| 420 | GElf_Ehdr ehdr; |
| 421 | GElf_Shdr shdr; |
| 422 | int fd, ret = -ENOENT; |
| 423 | |
| 424 | fd = open(exec, O_RDONLY); |
| 425 | if (fd < 0) |
| 426 | return -errno; |
| 427 | |
| 428 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 429 | if (elf == NULL) |
| 430 | return -EINVAL; |
| 431 | |
| 432 | if (gelf_getehdr(elf, &ehdr) == NULL) |
| 433 | goto out; |
| 434 | |
| 435 | if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL)) |
| 436 | goto out; |
| 437 | |
| 438 | *address = shdr.sh_addr - shdr.sh_offset; |
| 439 | ret = 0; |
| 440 | out: |
| 441 | elf_end(elf); |
| 442 | return ret; |
| 443 | } |
| 444 | |
Masami Hiramatsu | 5a6f631 | 2014-02-06 05:32:23 +0000 | [diff] [blame] | 445 | /* |
| 446 | * Convert trace point to probe point with debuginfo |
| 447 | */ |
| 448 | static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp, |
| 449 | struct perf_probe_point *pp, |
| 450 | bool is_kprobe) |
| 451 | { |
| 452 | struct debuginfo *dinfo = NULL; |
| 453 | unsigned long stext = 0; |
| 454 | u64 addr = tp->address; |
| 455 | int ret = -ENOENT; |
| 456 | |
| 457 | /* convert the address to dwarf address */ |
| 458 | if (!is_kprobe) { |
| 459 | if (!addr) { |
| 460 | ret = -EINVAL; |
| 461 | goto error; |
| 462 | } |
| 463 | ret = get_text_start_address(tp->module, &stext); |
| 464 | if (ret < 0) |
| 465 | goto error; |
| 466 | addr += stext; |
| 467 | } else { |
| 468 | addr = kernel_get_symbol_address_by_name(tp->symbol, false); |
| 469 | if (addr == 0) |
| 470 | goto error; |
| 471 | addr += tp->offset; |
| 472 | } |
| 473 | |
| 474 | pr_debug("try to find information at %" PRIx64 " in %s\n", addr, |
| 475 | tp->module ? : "kernel"); |
| 476 | |
Masami Hiramatsu | 92561cb | 2014-08-15 01:44:32 +0000 | [diff] [blame] | 477 | dinfo = open_debuginfo(tp->module, verbose == 0); |
Masami Hiramatsu | 5a6f631 | 2014-02-06 05:32:23 +0000 | [diff] [blame] | 478 | if (dinfo) { |
| 479 | ret = debuginfo__find_probe_point(dinfo, |
| 480 | (unsigned long)addr, pp); |
| 481 | debuginfo__delete(dinfo); |
Masami Hiramatsu | 92561cb | 2014-08-15 01:44:32 +0000 | [diff] [blame] | 482 | } else |
Masami Hiramatsu | 5a6f631 | 2014-02-06 05:32:23 +0000 | [diff] [blame] | 483 | ret = -ENOENT; |
Masami Hiramatsu | 5a6f631 | 2014-02-06 05:32:23 +0000 | [diff] [blame] | 484 | |
| 485 | if (ret > 0) { |
| 486 | pp->retprobe = tp->retprobe; |
| 487 | return 0; |
| 488 | } |
| 489 | error: |
| 490 | pr_debug("Failed to find corresponding probes from debuginfo.\n"); |
| 491 | return ret ? : -ENOENT; |
| 492 | } |
| 493 | |
Masami Hiramatsu | fb7345b | 2013-12-26 05:41:53 +0000 | [diff] [blame] | 494 | static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs, |
| 495 | int ntevs, const char *exec) |
| 496 | { |
| 497 | int i, ret = 0; |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 498 | unsigned long stext = 0; |
Masami Hiramatsu | fb7345b | 2013-12-26 05:41:53 +0000 | [diff] [blame] | 499 | |
| 500 | if (!exec) |
| 501 | return 0; |
| 502 | |
| 503 | ret = get_text_start_address(exec, &stext); |
| 504 | if (ret < 0) |
| 505 | return ret; |
| 506 | |
| 507 | for (i = 0; i < ntevs && ret >= 0; i++) { |
Masami Hiramatsu | 981a237 | 2014-02-05 05:18:58 +0000 | [diff] [blame] | 508 | /* point.address is the addres of point.symbol + point.offset */ |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 509 | tevs[i].point.address -= stext; |
Masami Hiramatsu | fb7345b | 2013-12-26 05:41:53 +0000 | [diff] [blame] | 510 | tevs[i].point.module = strdup(exec); |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 511 | if (!tevs[i].point.module) { |
Masami Hiramatsu | fb7345b | 2013-12-26 05:41:53 +0000 | [diff] [blame] | 512 | ret = -ENOMEM; |
| 513 | break; |
| 514 | } |
| 515 | tevs[i].uprobes = true; |
| 516 | } |
| 517 | |
| 518 | return ret; |
| 519 | } |
| 520 | |
Masami Hiramatsu | 190b57f | 2011-06-27 16:27:45 +0900 | [diff] [blame] | 521 | static int add_module_to_probe_trace_events(struct probe_trace_event *tevs, |
| 522 | int ntevs, const char *module) |
| 523 | { |
Masami Hiramatsu | 14a8fd7 | 2011-06-27 16:27:51 +0900 | [diff] [blame] | 524 | int i, ret = 0; |
| 525 | char *tmp; |
| 526 | |
| 527 | if (!module) |
| 528 | return 0; |
| 529 | |
| 530 | tmp = strrchr(module, '/'); |
| 531 | if (tmp) { |
| 532 | /* This is a module path -- get the module name */ |
| 533 | module = strdup(tmp + 1); |
| 534 | if (!module) |
| 535 | return -ENOMEM; |
| 536 | tmp = strchr(module, '.'); |
| 537 | if (tmp) |
| 538 | *tmp = '\0'; |
| 539 | tmp = (char *)module; /* For free() */ |
| 540 | } |
| 541 | |
Masami Hiramatsu | 190b57f | 2011-06-27 16:27:45 +0900 | [diff] [blame] | 542 | for (i = 0; i < ntevs; i++) { |
| 543 | tevs[i].point.module = strdup(module); |
Masami Hiramatsu | 14a8fd7 | 2011-06-27 16:27:51 +0900 | [diff] [blame] | 544 | if (!tevs[i].point.module) { |
| 545 | ret = -ENOMEM; |
| 546 | break; |
| 547 | } |
Masami Hiramatsu | 190b57f | 2011-06-27 16:27:45 +0900 | [diff] [blame] | 548 | } |
Masami Hiramatsu | 14a8fd7 | 2011-06-27 16:27:51 +0900 | [diff] [blame] | 549 | |
Arnaldo Carvalho de Melo | f538565 | 2013-12-26 15:54:57 -0300 | [diff] [blame] | 550 | free(tmp); |
Masami Hiramatsu | 14a8fd7 | 2011-06-27 16:27:51 +0900 | [diff] [blame] | 551 | return ret; |
Masami Hiramatsu | 190b57f | 2011-06-27 16:27:45 +0900 | [diff] [blame] | 552 | } |
| 553 | |
Masami Hiramatsu | dfef99c | 2014-02-06 05:32:16 +0000 | [diff] [blame] | 554 | /* Post processing the probe events */ |
| 555 | static int post_process_probe_trace_events(struct probe_trace_event *tevs, |
| 556 | int ntevs, const char *module, |
| 557 | bool uprobe) |
| 558 | { |
| 559 | struct ref_reloc_sym *reloc_sym; |
| 560 | char *tmp; |
| 561 | int i; |
| 562 | |
| 563 | if (uprobe) |
| 564 | return add_exec_to_probe_trace_events(tevs, ntevs, module); |
| 565 | |
| 566 | /* Note that currently ref_reloc_sym based probe is not for drivers */ |
| 567 | if (module) |
| 568 | return add_module_to_probe_trace_events(tevs, ntevs, module); |
| 569 | |
Masami Hiramatsu | 8f33f7d | 2014-02-06 05:32:20 +0000 | [diff] [blame] | 570 | reloc_sym = kernel_get_ref_reloc_sym(); |
Masami Hiramatsu | dfef99c | 2014-02-06 05:32:16 +0000 | [diff] [blame] | 571 | if (!reloc_sym) { |
| 572 | pr_warning("Relocated base symbol is not found!\n"); |
| 573 | return -EINVAL; |
| 574 | } |
| 575 | |
| 576 | for (i = 0; i < ntevs; i++) { |
Namhyung Kim | 25dd917 | 2015-01-14 20:18:08 +0900 | [diff] [blame] | 577 | if (tevs[i].point.address && !tevs[i].point.retprobe) { |
Masami Hiramatsu | dfef99c | 2014-02-06 05:32:16 +0000 | [diff] [blame] | 578 | tmp = strdup(reloc_sym->name); |
| 579 | if (!tmp) |
| 580 | return -ENOMEM; |
| 581 | free(tevs[i].point.symbol); |
| 582 | tevs[i].point.symbol = tmp; |
| 583 | tevs[i].point.offset = tevs[i].point.address - |
| 584 | reloc_sym->unrelocated_addr; |
| 585 | } |
| 586 | } |
| 587 | return 0; |
| 588 | } |
| 589 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 590 | /* Try to find perf_probe_event with debuginfo */ |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 591 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
Masami Hiramatsu | 190b57f | 2011-06-27 16:27:45 +0900 | [diff] [blame] | 592 | struct probe_trace_event **tevs, |
Srikar Dronamraju | 4eced23 | 2012-02-02 19:50:40 +0530 | [diff] [blame] | 593 | int max_tevs, const char *target) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 594 | { |
| 595 | bool need_dwarf = perf_probe_event_need_dwarf(pev); |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 596 | struct perf_probe_point tmp; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 597 | struct debuginfo *dinfo; |
Masami Hiramatsu | 190b57f | 2011-06-27 16:27:45 +0900 | [diff] [blame] | 598 | int ntevs, ret = 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 599 | |
Masami Hiramatsu | 92561cb | 2014-08-15 01:44:32 +0000 | [diff] [blame] | 600 | dinfo = open_debuginfo(target, !need_dwarf); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 601 | |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 602 | if (!dinfo) { |
Masami Hiramatsu | 92561cb | 2014-08-15 01:44:32 +0000 | [diff] [blame] | 603 | if (need_dwarf) |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 604 | return -ENOENT; |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 605 | pr_debug("Could not open debuginfo. Try to use symbols.\n"); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 606 | return 0; |
| 607 | } |
| 608 | |
Masami Hiramatsu | dfef99c | 2014-02-06 05:32:16 +0000 | [diff] [blame] | 609 | pr_debug("Try to find probe point from debuginfo.\n"); |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 610 | /* Searching trace events corresponding to a probe event */ |
| 611 | ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs); |
| 612 | |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 613 | if (ntevs == 0) { /* Not found, retry with an alternative */ |
| 614 | ret = get_alternative_probe_event(dinfo, pev, &tmp, target); |
| 615 | if (!ret) { |
| 616 | ntevs = debuginfo__find_trace_events(dinfo, pev, |
| 617 | tevs, max_tevs); |
| 618 | /* |
| 619 | * Write back to the original probe_event for |
| 620 | * setting appropriate (user given) event name |
| 621 | */ |
| 622 | clear_perf_probe_point(&pev->point); |
| 623 | memcpy(&pev->point, &tmp, sizeof(tmp)); |
| 624 | } |
| 625 | } |
| 626 | |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 627 | debuginfo__delete(dinfo); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 628 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 629 | if (ntevs > 0) { /* Succeeded to find trace events */ |
Masami Hiramatsu | dfef99c | 2014-02-06 05:32:16 +0000 | [diff] [blame] | 630 | pr_debug("Found %d probe_trace_events.\n", ntevs); |
| 631 | ret = post_process_probe_trace_events(*tevs, ntevs, |
| 632 | target, pev->uprobes); |
Masami Hiramatsu | 981d05a | 2014-01-16 09:39:44 +0000 | [diff] [blame] | 633 | if (ret < 0) { |
| 634 | clear_probe_trace_events(*tevs, ntevs); |
| 635 | zfree(tevs); |
| 636 | } |
Masami Hiramatsu | 190b57f | 2011-06-27 16:27:45 +0900 | [diff] [blame] | 637 | return ret < 0 ? ret : ntevs; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 638 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 639 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 640 | if (ntevs == 0) { /* No error but failed to find probe point. */ |
Masami Hiramatsu | 0687eba | 2015-03-06 16:31:25 +0900 | [diff] [blame] | 641 | pr_warning("Probe point '%s' not found.\n", |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 642 | synthesize_perf_probe_point(&pev->point)); |
Masami Hiramatsu | 0687eba | 2015-03-06 16:31:25 +0900 | [diff] [blame] | 643 | return -ENOENT; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 644 | } |
| 645 | /* Error path : ntevs < 0 */ |
Masami Hiramatsu | 15eca30 | 2010-04-21 15:56:24 -0400 | [diff] [blame] | 646 | pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); |
| 647 | if (ntevs == -EBADF) { |
| 648 | pr_warning("Warning: No dwarf info found in the vmlinux - " |
| 649 | "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n"); |
| 650 | if (!need_dwarf) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 651 | pr_debug("Trying to use symbols.\n"); |
Masami Hiramatsu | 15eca30 | 2010-04-21 15:56:24 -0400 | [diff] [blame] | 652 | return 0; |
| 653 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 654 | } |
Masami Hiramatsu | 15eca30 | 2010-04-21 15:56:24 -0400 | [diff] [blame] | 655 | return ntevs; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | #define LINEBUF_SIZE 256 |
| 659 | #define NR_ADDITIONAL_LINES 2 |
| 660 | |
Franck Bui-Huu | fde52db | 2010-12-20 15:18:04 +0100 | [diff] [blame] | 661 | 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] | 662 | { |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 663 | char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE]; |
Franck Bui-Huu | befe341 | 2010-12-20 15:18:01 +0100 | [diff] [blame] | 664 | const char *color = show_num ? "" : PERF_COLOR_BLUE; |
| 665 | const char *prefix = NULL; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 666 | |
Franck Bui-Huu | befe341 | 2010-12-20 15:18:01 +0100 | [diff] [blame] | 667 | do { |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 668 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
| 669 | goto error; |
Franck Bui-Huu | befe341 | 2010-12-20 15:18:01 +0100 | [diff] [blame] | 670 | if (skip) |
| 671 | continue; |
| 672 | if (!prefix) { |
| 673 | prefix = show_num ? "%7d " : " "; |
| 674 | color_fprintf(stdout, color, prefix, l); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 675 | } |
Franck Bui-Huu | befe341 | 2010-12-20 15:18:01 +0100 | [diff] [blame] | 676 | color_fprintf(stdout, color, "%s", buf); |
| 677 | |
| 678 | } while (strchr(buf, '\n') == NULL); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 679 | |
Franck Bui-Huu | fde52db | 2010-12-20 15:18:04 +0100 | [diff] [blame] | 680 | return 1; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 681 | error: |
Franck Bui-Huu | fde52db | 2010-12-20 15:18:04 +0100 | [diff] [blame] | 682 | if (ferror(fp)) { |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 683 | pr_warning("File read error: %s\n", |
| 684 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Franck Bui-Huu | fde52db | 2010-12-20 15:18:04 +0100 | [diff] [blame] | 685 | return -1; |
| 686 | } |
| 687 | return 0; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 688 | } |
| 689 | |
Franck Bui-Huu | fde52db | 2010-12-20 15:18:04 +0100 | [diff] [blame] | 690 | static int _show_one_line(FILE *fp, int l, bool skip, bool show_num) |
| 691 | { |
| 692 | int rv = __show_one_line(fp, l, skip, show_num); |
| 693 | if (rv == 0) { |
| 694 | pr_warning("Source file is shorter than expected.\n"); |
| 695 | rv = -1; |
| 696 | } |
| 697 | return rv; |
| 698 | } |
| 699 | |
| 700 | #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true) |
| 701 | #define show_one_line(f,l) _show_one_line(f,l,false,false) |
| 702 | #define skip_one_line(f,l) _show_one_line(f,l,true,false) |
| 703 | #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false) |
| 704 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 705 | /* |
| 706 | * Show line-range always requires debuginfo to find source file and |
| 707 | * line number. |
| 708 | */ |
Masami Hiramatsu | 811dd2a | 2015-03-06 16:31:22 +0900 | [diff] [blame] | 709 | static int __show_line_range(struct line_range *lr, const char *module, |
| 710 | bool user) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 711 | { |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 712 | int l = 1; |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 713 | struct int_node *ln; |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 714 | struct debuginfo *dinfo; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 715 | FILE *fp; |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 716 | int ret; |
Masami Hiramatsu | 7cf0b79 | 2010-07-09 18:28:59 +0900 | [diff] [blame] | 717 | char *tmp; |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 718 | char sbuf[STRERR_BUFSIZE]; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 719 | |
| 720 | /* Search a line range */ |
Masami Hiramatsu | 92561cb | 2014-08-15 01:44:32 +0000 | [diff] [blame] | 721 | dinfo = open_debuginfo(module, false); |
| 722 | if (!dinfo) |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 723 | return -ENOENT; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 724 | |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 725 | ret = debuginfo__find_line_range(dinfo, lr); |
Masami Hiramatsu | 811dd2a | 2015-03-06 16:31:22 +0900 | [diff] [blame] | 726 | if (!ret) { /* Not found, retry with an alternative */ |
| 727 | ret = get_alternative_line_range(dinfo, lr, module, user); |
| 728 | if (!ret) |
| 729 | ret = debuginfo__find_line_range(dinfo, lr); |
| 730 | } |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 731 | debuginfo__delete(dinfo); |
Masami Hiramatsu | 5ee05b8 | 2014-06-06 07:14:06 +0000 | [diff] [blame] | 732 | if (ret == 0 || ret == -ENOENT) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 733 | pr_warning("Specified source line is not found.\n"); |
| 734 | return -ENOENT; |
| 735 | } else if (ret < 0) { |
Masami Hiramatsu | 5ee05b8 | 2014-06-06 07:14:06 +0000 | [diff] [blame] | 736 | pr_warning("Debuginfo analysis failed.\n"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 737 | return ret; |
| 738 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 739 | |
Masami Hiramatsu | 7cf0b79 | 2010-07-09 18:28:59 +0900 | [diff] [blame] | 740 | /* Convert source file path */ |
| 741 | tmp = lr->path; |
Masami Hiramatsu | 6a330a3 | 2010-07-09 18:29:11 +0900 | [diff] [blame] | 742 | ret = get_real_path(tmp, lr->comp_dir, &lr->path); |
He Kuang | a78604d | 2015-03-04 18:01:42 +0800 | [diff] [blame] | 743 | |
| 744 | /* Free old path when new path is assigned */ |
| 745 | if (tmp != lr->path) |
| 746 | free(tmp); |
| 747 | |
Masami Hiramatsu | 7cf0b79 | 2010-07-09 18:28:59 +0900 | [diff] [blame] | 748 | if (ret < 0) { |
Masami Hiramatsu | 5ee05b8 | 2014-06-06 07:14:06 +0000 | [diff] [blame] | 749 | pr_warning("Failed to find source file path.\n"); |
Masami Hiramatsu | 7cf0b79 | 2010-07-09 18:28:59 +0900 | [diff] [blame] | 750 | return ret; |
| 751 | } |
| 752 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 753 | setup_pager(); |
| 754 | |
| 755 | if (lr->function) |
Masami Hiramatsu | 8737ebd | 2011-02-10 18:08:16 +0900 | [diff] [blame] | 756 | fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path, |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 757 | lr->start - lr->offset); |
| 758 | else |
Franck Bui-Huu | 62c15fc | 2010-12-20 15:18:00 +0100 | [diff] [blame] | 759 | fprintf(stdout, "<%s:%d>\n", lr->path, lr->start); |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 760 | |
| 761 | fp = fopen(lr->path, "r"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 762 | if (fp == NULL) { |
| 763 | pr_warning("Failed to open %s: %s\n", lr->path, |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 764 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 765 | return -errno; |
| 766 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 767 | /* Skip to starting line number */ |
Franck Bui-Huu | 44b81e9 | 2010-12-20 15:18:02 +0100 | [diff] [blame] | 768 | while (l < lr->start) { |
Franck Bui-Huu | fde52db | 2010-12-20 15:18:04 +0100 | [diff] [blame] | 769 | ret = skip_one_line(fp, l++); |
Franck Bui-Huu | 44b81e9 | 2010-12-20 15:18:02 +0100 | [diff] [blame] | 770 | if (ret < 0) |
| 771 | goto end; |
| 772 | } |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 773 | |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 774 | intlist__for_each(ln, lr->line_list) { |
| 775 | for (; ln->i > l; l++) { |
Franck Bui-Huu | fde52db | 2010-12-20 15:18:04 +0100 | [diff] [blame] | 776 | ret = show_one_line(fp, l - lr->offset); |
Franck Bui-Huu | 44b81e9 | 2010-12-20 15:18:02 +0100 | [diff] [blame] | 777 | if (ret < 0) |
| 778 | goto end; |
| 779 | } |
Franck Bui-Huu | fde52db | 2010-12-20 15:18:04 +0100 | [diff] [blame] | 780 | ret = show_one_line_with_num(fp, l++ - lr->offset); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 781 | if (ret < 0) |
| 782 | goto end; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | if (lr->end == INT_MAX) |
| 786 | lr->end = l + NR_ADDITIONAL_LINES; |
Franck Bui-Huu | fde52db | 2010-12-20 15:18:04 +0100 | [diff] [blame] | 787 | while (l <= lr->end) { |
| 788 | ret = show_one_line_or_eof(fp, l++ - lr->offset); |
| 789 | if (ret <= 0) |
Franck Bui-Huu | 44b81e9 | 2010-12-20 15:18:02 +0100 | [diff] [blame] | 790 | break; |
| 791 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 792 | end: |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 793 | fclose(fp); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 794 | return ret; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 795 | } |
| 796 | |
Masami Hiramatsu | 2b394bc | 2014-09-17 08:40:54 +0000 | [diff] [blame] | 797 | int show_line_range(struct line_range *lr, const char *module, bool user) |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 798 | { |
| 799 | int ret; |
| 800 | |
Masami Hiramatsu | 2b394bc | 2014-09-17 08:40:54 +0000 | [diff] [blame] | 801 | ret = init_symbol_maps(user); |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 802 | if (ret < 0) |
| 803 | return ret; |
Masami Hiramatsu | 811dd2a | 2015-03-06 16:31:22 +0900 | [diff] [blame] | 804 | ret = __show_line_range(lr, module, user); |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 805 | exit_symbol_maps(); |
| 806 | |
| 807 | return ret; |
| 808 | } |
| 809 | |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 810 | static int show_available_vars_at(struct debuginfo *dinfo, |
| 811 | struct perf_probe_event *pev, |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 812 | int max_vls, struct strfilter *_filter, |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 813 | bool externs, const char *target) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 814 | { |
| 815 | char *buf; |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 816 | int ret, i, nvars; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 817 | struct str_node *node; |
| 818 | struct variable_list *vls = NULL, *vl; |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 819 | struct perf_probe_point tmp; |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 820 | const char *var; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 821 | |
| 822 | buf = synthesize_perf_probe_point(&pev->point); |
| 823 | if (!buf) |
| 824 | return -EINVAL; |
| 825 | pr_debug("Searching variables at %s\n", buf); |
| 826 | |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 827 | ret = debuginfo__find_available_vars_at(dinfo, pev, &vls, |
| 828 | max_vls, externs); |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 829 | if (!ret) { /* Not found, retry with an alternative */ |
| 830 | ret = get_alternative_probe_event(dinfo, pev, &tmp, target); |
| 831 | if (!ret) { |
| 832 | ret = debuginfo__find_available_vars_at(dinfo, pev, |
| 833 | &vls, max_vls, externs); |
| 834 | /* Release the old probe_point */ |
| 835 | clear_perf_probe_point(&tmp); |
| 836 | } |
| 837 | } |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 838 | if (ret <= 0) { |
Masami Hiramatsu | 69e96ea | 2014-06-06 07:13:59 +0000 | [diff] [blame] | 839 | if (ret == 0 || ret == -ENOENT) { |
| 840 | pr_err("Failed to find the address of %s\n", buf); |
| 841 | ret = -ENOENT; |
| 842 | } else |
| 843 | pr_warning("Debuginfo analysis failed.\n"); |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 844 | goto end; |
| 845 | } |
Masami Hiramatsu | 69e96ea | 2014-06-06 07:13:59 +0000 | [diff] [blame] | 846 | |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 847 | /* Some variables are found */ |
| 848 | fprintf(stdout, "Available variables at %s\n", buf); |
| 849 | for (i = 0; i < ret; i++) { |
| 850 | vl = &vls[i]; |
| 851 | /* |
| 852 | * A probe point might be converted to |
| 853 | * several trace points. |
| 854 | */ |
| 855 | fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol, |
| 856 | vl->point.offset); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 857 | zfree(&vl->point.symbol); |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 858 | nvars = 0; |
| 859 | if (vl->vars) { |
| 860 | strlist__for_each(node, vl->vars) { |
| 861 | var = strchr(node->s, '\t') + 1; |
| 862 | if (strfilter__compare(_filter, var)) { |
| 863 | fprintf(stdout, "\t\t%s\n", node->s); |
| 864 | nvars++; |
| 865 | } |
| 866 | } |
| 867 | strlist__delete(vl->vars); |
| 868 | } |
| 869 | if (nvars == 0) |
| 870 | fprintf(stdout, "\t\t(No matched variables)\n"); |
| 871 | } |
| 872 | free(vls); |
| 873 | end: |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 874 | free(buf); |
| 875 | return ret; |
| 876 | } |
| 877 | |
| 878 | /* Show available variables on given probe point */ |
| 879 | int show_available_vars(struct perf_probe_event *pevs, int npevs, |
Masami Hiramatsu | bd09d7b | 2011-01-20 23:15:39 +0900 | [diff] [blame] | 880 | int max_vls, const char *module, |
| 881 | struct strfilter *_filter, bool externs) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 882 | { |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 883 | int i, ret = 0; |
| 884 | struct debuginfo *dinfo; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 885 | |
Masami Hiramatsu | 2b394bc | 2014-09-17 08:40:54 +0000 | [diff] [blame] | 886 | ret = init_symbol_maps(pevs->uprobes); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 887 | if (ret < 0) |
| 888 | return ret; |
| 889 | |
Masami Hiramatsu | 92561cb | 2014-08-15 01:44:32 +0000 | [diff] [blame] | 890 | dinfo = open_debuginfo(module, false); |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 891 | if (!dinfo) { |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 892 | ret = -ENOENT; |
| 893 | goto out; |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 894 | } |
| 895 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 896 | setup_pager(); |
| 897 | |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 898 | for (i = 0; i < npevs && ret >= 0; i++) |
| 899 | ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter, |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 900 | externs, module); |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 901 | |
| 902 | debuginfo__delete(dinfo); |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 903 | out: |
| 904 | exit_symbol_maps(); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 905 | return ret; |
| 906 | } |
| 907 | |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 908 | #else /* !HAVE_DWARF_SUPPORT */ |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 909 | |
Masami Hiramatsu | 5a6f631 | 2014-02-06 05:32:23 +0000 | [diff] [blame] | 910 | static int |
| 911 | find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused, |
| 912 | struct perf_probe_point *pp __maybe_unused, |
| 913 | bool is_kprobe __maybe_unused) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 914 | { |
Masami Hiramatsu | 5a6f631 | 2014-02-06 05:32:23 +0000 | [diff] [blame] | 915 | return -ENOSYS; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 916 | } |
| 917 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 918 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 919 | struct probe_trace_event **tevs __maybe_unused, |
Arnaldo Carvalho de Melo | 1d027ee | 2014-01-13 15:15:25 -0300 | [diff] [blame] | 920 | int max_tevs __maybe_unused, |
| 921 | const char *target __maybe_unused) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 922 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 923 | if (perf_probe_event_need_dwarf(pev)) { |
| 924 | pr_warning("Debuginfo-analysis is not supported.\n"); |
| 925 | return -ENOSYS; |
| 926 | } |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 927 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 928 | return 0; |
| 929 | } |
| 930 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 931 | int show_line_range(struct line_range *lr __maybe_unused, |
Masami Hiramatsu | 2b394bc | 2014-09-17 08:40:54 +0000 | [diff] [blame] | 932 | const char *module __maybe_unused, |
| 933 | bool user __maybe_unused) |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 934 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 935 | pr_warning("Debuginfo-analysis is not supported.\n"); |
| 936 | return -ENOSYS; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 937 | } |
| 938 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 939 | int show_available_vars(struct perf_probe_event *pevs __maybe_unused, |
| 940 | int npevs __maybe_unused, int max_vls __maybe_unused, |
| 941 | const char *module __maybe_unused, |
| 942 | struct strfilter *filter __maybe_unused, |
| 943 | bool externs __maybe_unused) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 944 | { |
| 945 | pr_warning("Debuginfo-analysis is not supported.\n"); |
| 946 | return -ENOSYS; |
| 947 | } |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 948 | #endif |
| 949 | |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 950 | void line_range__clear(struct line_range *lr) |
| 951 | { |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 952 | free(lr->function); |
| 953 | free(lr->file); |
| 954 | free(lr->path); |
| 955 | free(lr->comp_dir); |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 956 | intlist__delete(lr->line_list); |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 957 | memset(lr, 0, sizeof(*lr)); |
| 958 | } |
| 959 | |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 960 | int line_range__init(struct line_range *lr) |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 961 | { |
| 962 | memset(lr, 0, sizeof(*lr)); |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 963 | lr->line_list = intlist__new(NULL); |
| 964 | if (!lr->line_list) |
| 965 | return -ENOMEM; |
| 966 | else |
| 967 | return 0; |
Masami Hiramatsu | e53b00d | 2014-01-16 09:39:47 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Franck Bui-Huu | 21dd9ae | 2010-12-20 15:18:05 +0100 | [diff] [blame] | 970 | static int parse_line_num(char **ptr, int *val, const char *what) |
| 971 | { |
| 972 | const char *start = *ptr; |
| 973 | |
| 974 | errno = 0; |
| 975 | *val = strtol(*ptr, ptr, 0); |
| 976 | if (errno || *ptr == start) { |
| 977 | semantic_error("'%s' is not a valid number.\n", what); |
| 978 | return -EINVAL; |
| 979 | } |
| 980 | return 0; |
| 981 | } |
| 982 | |
Franck Bui-Huu | 9d95b58 | 2010-12-20 15:18:03 +0100 | [diff] [blame] | 983 | /* |
| 984 | * Stuff 'lr' according to the line range described by 'arg'. |
| 985 | * The line range syntax is described by: |
| 986 | * |
| 987 | * SRC[:SLN[+NUM|-ELN]] |
Masami Hiramatsu | e116dfa | 2011-02-10 18:08:10 +0900 | [diff] [blame] | 988 | * FNC[@SRC][:SLN[+NUM|-ELN]] |
Franck Bui-Huu | 9d95b58 | 2010-12-20 15:18:03 +0100 | [diff] [blame] | 989 | */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 990 | int parse_line_range_desc(const char *arg, struct line_range *lr) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 991 | { |
Masami Hiramatsu | e116dfa | 2011-02-10 18:08:10 +0900 | [diff] [blame] | 992 | char *range, *file, *name = strdup(arg); |
Franck Bui-Huu | 21dd9ae | 2010-12-20 15:18:05 +0100 | [diff] [blame] | 993 | int err; |
Franck Bui-Huu | 9d95b58 | 2010-12-20 15:18:03 +0100 | [diff] [blame] | 994 | |
Franck Bui-Huu | 21dd9ae | 2010-12-20 15:18:05 +0100 | [diff] [blame] | 995 | if (!name) |
| 996 | return -ENOMEM; |
| 997 | |
| 998 | lr->start = 0; |
| 999 | lr->end = INT_MAX; |
| 1000 | |
| 1001 | range = strchr(name, ':'); |
| 1002 | if (range) { |
| 1003 | *range++ = '\0'; |
| 1004 | |
| 1005 | err = parse_line_num(&range, &lr->start, "start line"); |
| 1006 | if (err) |
| 1007 | goto err; |
| 1008 | |
| 1009 | if (*range == '+' || *range == '-') { |
| 1010 | const char c = *range++; |
| 1011 | |
| 1012 | err = parse_line_num(&range, &lr->end, "end line"); |
| 1013 | if (err) |
| 1014 | goto err; |
| 1015 | |
| 1016 | if (c == '+') { |
| 1017 | lr->end += lr->start; |
| 1018 | /* |
| 1019 | * Adjust the number of lines here. |
| 1020 | * If the number of lines == 1, the |
| 1021 | * the end of line should be equal to |
| 1022 | * the start of line. |
| 1023 | */ |
| 1024 | lr->end--; |
| 1025 | } |
| 1026 | } |
| 1027 | |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 1028 | pr_debug("Line range is %d to %d\n", lr->start, lr->end); |
Franck Bui-Huu | 21dd9ae | 2010-12-20 15:18:05 +0100 | [diff] [blame] | 1029 | |
| 1030 | err = -EINVAL; |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 1031 | if (lr->start > lr->end) { |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1032 | semantic_error("Start line must be smaller" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1033 | " than end line.\n"); |
Franck Bui-Huu | 21dd9ae | 2010-12-20 15:18:05 +0100 | [diff] [blame] | 1034 | goto err; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1035 | } |
Franck Bui-Huu | 21dd9ae | 2010-12-20 15:18:05 +0100 | [diff] [blame] | 1036 | if (*range != '\0') { |
| 1037 | semantic_error("Tailing with invalid str '%s'.\n", range); |
| 1038 | goto err; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1039 | } |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 1040 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1041 | |
Masami Hiramatsu | e116dfa | 2011-02-10 18:08:10 +0900 | [diff] [blame] | 1042 | file = strchr(name, '@'); |
| 1043 | if (file) { |
| 1044 | *file = '\0'; |
| 1045 | lr->file = strdup(++file); |
| 1046 | if (lr->file == NULL) { |
| 1047 | err = -ENOMEM; |
| 1048 | goto err; |
| 1049 | } |
| 1050 | lr->function = name; |
| 1051 | } else if (strchr(name, '.')) |
Franck Bui-Huu | 21dd9ae | 2010-12-20 15:18:05 +0100 | [diff] [blame] | 1052 | lr->file = name; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1053 | else |
Franck Bui-Huu | 21dd9ae | 2010-12-20 15:18:05 +0100 | [diff] [blame] | 1054 | lr->function = name; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1055 | |
| 1056 | return 0; |
Franck Bui-Huu | 21dd9ae | 2010-12-20 15:18:05 +0100 | [diff] [blame] | 1057 | err: |
| 1058 | free(name); |
| 1059 | return err; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1060 | } |
| 1061 | |
Masami Hiramatsu | b7702a2 | 2009-12-16 17:24:15 -0500 | [diff] [blame] | 1062 | /* Check the name is good for event/group */ |
| 1063 | static bool check_event_name(const char *name) |
| 1064 | { |
| 1065 | if (!isalpha(*name) && *name != '_') |
| 1066 | return false; |
| 1067 | while (*++name != '\0') { |
| 1068 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') |
| 1069 | return false; |
| 1070 | } |
| 1071 | return true; |
| 1072 | } |
| 1073 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1074 | /* Parse probepoint definition. */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1075 | 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] | 1076 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1077 | struct perf_probe_point *pp = &pev->point; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1078 | char *ptr, *tmp; |
| 1079 | char c, nc = 0; |
| 1080 | /* |
| 1081 | * <Syntax> |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1082 | * perf probe [EVENT=]SRC[:LN|;PTN] |
| 1083 | * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 1084 | * |
| 1085 | * TODO:Group name support |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1086 | */ |
| 1087 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1088 | ptr = strpbrk(arg, ";=@+%"); |
| 1089 | if (ptr && *ptr == '=') { /* Event name */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 1090 | *ptr = '\0'; |
| 1091 | tmp = ptr + 1; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1092 | if (strchr(arg, ':')) { |
| 1093 | semantic_error("Group name is not supported yet.\n"); |
| 1094 | return -ENOTSUP; |
| 1095 | } |
| 1096 | if (!check_event_name(arg)) { |
Masami Hiramatsu | b7702a2 | 2009-12-16 17:24:15 -0500 | [diff] [blame] | 1097 | semantic_error("%s is bad for event name -it must " |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1098 | "follow C symbol-naming rule.\n", arg); |
| 1099 | return -EINVAL; |
| 1100 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1101 | pev->event = strdup(arg); |
| 1102 | if (pev->event == NULL) |
| 1103 | return -ENOMEM; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1104 | pev->group = NULL; |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 1105 | arg = tmp; |
| 1106 | } |
| 1107 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1108 | ptr = strpbrk(arg, ";:+@%"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1109 | if (ptr) { |
| 1110 | nc = *ptr; |
| 1111 | *ptr++ = '\0'; |
| 1112 | } |
| 1113 | |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1114 | tmp = strdup(arg); |
| 1115 | if (tmp == NULL) |
| 1116 | return -ENOMEM; |
| 1117 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1118 | /* Check arg is function or file and copy it */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1119 | if (strchr(tmp, '.')) /* File */ |
| 1120 | pp->file = tmp; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1121 | else /* Function */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1122 | pp->function = tmp; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1123 | |
| 1124 | /* Parse other options */ |
| 1125 | while (ptr) { |
| 1126 | arg = ptr; |
| 1127 | c = nc; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1128 | if (c == ';') { /* Lazy pattern must be the last part */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1129 | pp->lazy_line = strdup(arg); |
| 1130 | if (pp->lazy_line == NULL) |
| 1131 | return -ENOMEM; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1132 | break; |
| 1133 | } |
| 1134 | ptr = strpbrk(arg, ";:+@%"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1135 | if (ptr) { |
| 1136 | nc = *ptr; |
| 1137 | *ptr++ = '\0'; |
| 1138 | } |
| 1139 | switch (c) { |
| 1140 | case ':': /* Line number */ |
| 1141 | pp->line = strtoul(arg, &tmp, 0); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1142 | if (*tmp != '\0') { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1143 | semantic_error("There is non-digit char" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1144 | " in line number.\n"); |
| 1145 | return -EINVAL; |
| 1146 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1147 | break; |
| 1148 | case '+': /* Byte offset from a symbol */ |
| 1149 | pp->offset = strtoul(arg, &tmp, 0); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1150 | if (*tmp != '\0') { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1151 | semantic_error("There is non-digit character" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1152 | " in offset.\n"); |
| 1153 | return -EINVAL; |
| 1154 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1155 | break; |
| 1156 | case '@': /* File name */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1157 | if (pp->file) { |
| 1158 | semantic_error("SRC@SRC is not allowed.\n"); |
| 1159 | return -EINVAL; |
| 1160 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1161 | pp->file = strdup(arg); |
| 1162 | if (pp->file == NULL) |
| 1163 | return -ENOMEM; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1164 | break; |
| 1165 | case '%': /* Probe places */ |
| 1166 | if (strcmp(arg, "return") == 0) { |
| 1167 | pp->retprobe = 1; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1168 | } else { /* Others not supported yet */ |
| 1169 | semantic_error("%%%s is not supported.\n", arg); |
| 1170 | return -ENOTSUP; |
| 1171 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1172 | break; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1173 | default: /* Buggy case */ |
| 1174 | pr_err("This program has a bug at %s:%d.\n", |
| 1175 | __FILE__, __LINE__); |
| 1176 | return -ENOTSUP; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1177 | break; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | /* Exclusion check */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1182 | if (pp->lazy_line && pp->line) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1183 | semantic_error("Lazy pattern can't be used with" |
| 1184 | " line number.\n"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1185 | return -EINVAL; |
| 1186 | } |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1187 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1188 | if (pp->lazy_line && pp->offset) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1189 | semantic_error("Lazy pattern can't be used with offset.\n"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1190 | return -EINVAL; |
| 1191 | } |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1192 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1193 | if (pp->line && pp->offset) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1194 | semantic_error("Offset can't be used with line number.\n"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1195 | return -EINVAL; |
| 1196 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1197 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1198 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1199 | semantic_error("File always requires line number or " |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1200 | "lazy pattern.\n"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1201 | return -EINVAL; |
| 1202 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1203 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1204 | if (pp->offset && !pp->function) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1205 | semantic_error("Offset requires an entry function.\n"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1206 | return -EINVAL; |
| 1207 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1208 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1209 | if (pp->retprobe && !pp->function) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1210 | semantic_error("Return probe requires an entry function.\n"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1211 | return -EINVAL; |
| 1212 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1213 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1214 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1215 | semantic_error("Offset/Line/Lazy pattern can't be used with " |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1216 | "return probe.\n"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1217 | return -EINVAL; |
| 1218 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1219 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1220 | 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] | 1221 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, |
| 1222 | pp->lazy_line); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1223 | return 0; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1224 | } |
| 1225 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1226 | /* Parse perf-probe event argument */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1227 | 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] | 1228 | { |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1229 | char *tmp, *goodname; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1230 | struct perf_probe_arg_field **fieldp; |
| 1231 | |
| 1232 | pr_debug("parsing arg: %s into ", str); |
| 1233 | |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1234 | tmp = strchr(str, '='); |
| 1235 | if (tmp) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1236 | arg->name = strndup(str, tmp - str); |
| 1237 | if (arg->name == NULL) |
| 1238 | return -ENOMEM; |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 1239 | pr_debug("name:%s ", arg->name); |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1240 | str = tmp + 1; |
| 1241 | } |
| 1242 | |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 1243 | tmp = strchr(str, ':'); |
| 1244 | if (tmp) { /* Type setting */ |
| 1245 | *tmp = '\0'; |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1246 | arg->type = strdup(tmp + 1); |
| 1247 | if (arg->type == NULL) |
| 1248 | return -ENOMEM; |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 1249 | pr_debug("type:%s ", arg->type); |
| 1250 | } |
| 1251 | |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1252 | tmp = strpbrk(str, "-.["); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1253 | if (!is_c_varname(str) || !tmp) { |
| 1254 | /* A variable, register, symbol or special value */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1255 | arg->var = strdup(str); |
| 1256 | if (arg->var == NULL) |
| 1257 | return -ENOMEM; |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1258 | pr_debug("%s\n", arg->var); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1259 | return 0; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1260 | } |
| 1261 | |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1262 | /* Structure fields or array element */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1263 | arg->var = strndup(str, tmp - str); |
| 1264 | if (arg->var == NULL) |
| 1265 | return -ENOMEM; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1266 | goodname = arg->var; |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1267 | pr_debug("%s, ", arg->var); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1268 | fieldp = &arg->field; |
| 1269 | |
| 1270 | do { |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1271 | *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); |
| 1272 | if (*fieldp == NULL) |
| 1273 | return -ENOMEM; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1274 | if (*tmp == '[') { /* Array */ |
| 1275 | str = tmp; |
| 1276 | (*fieldp)->index = strtol(str + 1, &tmp, 0); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1277 | (*fieldp)->ref = true; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1278 | if (*tmp != ']' || tmp == str + 1) { |
| 1279 | semantic_error("Array index must be a" |
| 1280 | " number.\n"); |
| 1281 | return -EINVAL; |
| 1282 | } |
| 1283 | tmp++; |
| 1284 | if (*tmp == '\0') |
| 1285 | tmp = NULL; |
| 1286 | } else { /* Structure */ |
| 1287 | if (*tmp == '.') { |
| 1288 | str = tmp + 1; |
| 1289 | (*fieldp)->ref = false; |
| 1290 | } else if (tmp[1] == '>') { |
| 1291 | str = tmp + 2; |
| 1292 | (*fieldp)->ref = true; |
| 1293 | } else { |
| 1294 | semantic_error("Argument parse error: %s\n", |
| 1295 | str); |
| 1296 | return -EINVAL; |
| 1297 | } |
| 1298 | tmp = strpbrk(str, "-.["); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1299 | } |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1300 | if (tmp) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1301 | (*fieldp)->name = strndup(str, tmp - str); |
| 1302 | if ((*fieldp)->name == NULL) |
| 1303 | return -ENOMEM; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1304 | if (*str != '[') |
| 1305 | goodname = (*fieldp)->name; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1306 | pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); |
| 1307 | fieldp = &(*fieldp)->next; |
| 1308 | } |
| 1309 | } while (tmp); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1310 | (*fieldp)->name = strdup(str); |
| 1311 | if ((*fieldp)->name == NULL) |
| 1312 | return -ENOMEM; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1313 | if (*str != '[') |
| 1314 | goodname = (*fieldp)->name; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1315 | pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); |
Masami Hiramatsu | df0faf4 | 2010-04-12 13:17:00 -0400 | [diff] [blame] | 1316 | |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1317 | /* If no name is specified, set the last field name (not array index)*/ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1318 | if (!arg->name) { |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1319 | arg->name = strdup(goodname); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1320 | if (arg->name == NULL) |
| 1321 | return -ENOMEM; |
| 1322 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1323 | return 0; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1324 | } |
| 1325 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1326 | /* Parse perf-probe event command */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1327 | 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] | 1328 | { |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 1329 | char **argv; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1330 | int argc, i, ret = 0; |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 1331 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1332 | argv = argv_split(cmd, &argc); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1333 | if (!argv) { |
| 1334 | pr_debug("Failed to split arguments.\n"); |
| 1335 | return -ENOMEM; |
| 1336 | } |
| 1337 | if (argc - 1 > MAX_PROBE_ARGS) { |
| 1338 | semantic_error("Too many probe arguments (%d).\n", argc - 1); |
| 1339 | ret = -ERANGE; |
| 1340 | goto out; |
| 1341 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1342 | /* Parse probe point */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1343 | ret = parse_perf_probe_point(argv[0], pev); |
| 1344 | if (ret < 0) |
| 1345 | goto out; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1346 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 1347 | /* Copy arguments and ensure return probe has no C argument */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1348 | pev->nargs = argc - 1; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1349 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
| 1350 | if (pev->args == NULL) { |
| 1351 | ret = -ENOMEM; |
| 1352 | goto out; |
| 1353 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1354 | for (i = 0; i < pev->nargs && ret >= 0; i++) { |
| 1355 | ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); |
| 1356 | if (ret >= 0 && |
| 1357 | is_c_varname(pev->args[i].var) && pev->point.retprobe) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1358 | semantic_error("You can't specify local variable for" |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1359 | " kretprobe.\n"); |
| 1360 | ret = -EINVAL; |
| 1361 | } |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 1362 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1363 | out: |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 1364 | argv_free(argv); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1365 | |
| 1366 | return ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1367 | } |
| 1368 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1369 | /* Return true if this perf_probe_event requires debuginfo */ |
| 1370 | bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1371 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1372 | int i; |
| 1373 | |
| 1374 | if (pev->point.file || pev->point.line || pev->point.lazy_line) |
| 1375 | return true; |
| 1376 | |
| 1377 | for (i = 0; i < pev->nargs; i++) |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1378 | if (is_c_varname(pev->args[i].var)) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1379 | return true; |
| 1380 | |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1384 | /* Parse probe_events event into struct probe_point */ |
| 1385 | static int parse_probe_trace_command(const char *cmd, |
Masami Hiramatsu | 190b57f | 2011-06-27 16:27:45 +0900 | [diff] [blame] | 1386 | struct probe_trace_event *tev) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1387 | { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1388 | struct probe_trace_point *tp = &tev->point; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1389 | char pr; |
| 1390 | char *p; |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 1391 | char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1392 | int ret, i, argc; |
| 1393 | char **argv; |
| 1394 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1395 | pr_debug("Parsing probe_events: %s\n", cmd); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1396 | argv = argv_split(cmd, &argc); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1397 | if (!argv) { |
| 1398 | pr_debug("Failed to split arguments.\n"); |
| 1399 | return -ENOMEM; |
| 1400 | } |
| 1401 | if (argc < 2) { |
| 1402 | semantic_error("Too few probe arguments.\n"); |
| 1403 | ret = -ERANGE; |
| 1404 | goto out; |
| 1405 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1406 | |
| 1407 | /* Scan event and group name. */ |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 1408 | argv0_str = strdup(argv[0]); |
| 1409 | if (argv0_str == NULL) { |
| 1410 | ret = -ENOMEM; |
| 1411 | goto out; |
| 1412 | } |
| 1413 | fmt1_str = strtok_r(argv0_str, ":", &fmt); |
| 1414 | fmt2_str = strtok_r(NULL, "/", &fmt); |
| 1415 | fmt3_str = strtok_r(NULL, " \t", &fmt); |
| 1416 | if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL |
| 1417 | || fmt3_str == NULL) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1418 | semantic_error("Failed to parse event name: %s\n", argv[0]); |
| 1419 | ret = -EINVAL; |
| 1420 | goto out; |
| 1421 | } |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 1422 | pr = fmt1_str[0]; |
| 1423 | tev->group = strdup(fmt2_str); |
| 1424 | tev->event = strdup(fmt3_str); |
| 1425 | if (tev->group == NULL || tev->event == NULL) { |
| 1426 | ret = -ENOMEM; |
| 1427 | goto out; |
| 1428 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1429 | 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] | 1430 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1431 | tp->retprobe = (pr == 'r'); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1432 | |
Masami Hiramatsu | 190b57f | 2011-06-27 16:27:45 +0900 | [diff] [blame] | 1433 | /* Scan module name(if there), function name and offset */ |
| 1434 | p = strchr(argv[1], ':'); |
| 1435 | if (p) { |
| 1436 | tp->module = strndup(argv[1], p - argv[1]); |
| 1437 | p++; |
| 1438 | } else |
| 1439 | p = argv[1]; |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 1440 | fmt1_str = strtok_r(p, "+", &fmt); |
Masami Hiramatsu | 5a6f631 | 2014-02-06 05:32:23 +0000 | [diff] [blame] | 1441 | if (fmt1_str[0] == '0') /* only the address started with 0x */ |
| 1442 | tp->address = strtoul(fmt1_str, NULL, 0); |
| 1443 | else { |
| 1444 | /* Only the symbol-based probe has offset */ |
| 1445 | tp->symbol = strdup(fmt1_str); |
| 1446 | if (tp->symbol == NULL) { |
| 1447 | ret = -ENOMEM; |
| 1448 | goto out; |
| 1449 | } |
| 1450 | fmt2_str = strtok_r(NULL, "", &fmt); |
| 1451 | if (fmt2_str == NULL) |
| 1452 | tp->offset = 0; |
| 1453 | else |
| 1454 | tp->offset = strtoul(fmt2_str, NULL, 10); |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 1455 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1456 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1457 | tev->nargs = argc - 2; |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1458 | tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1459 | if (tev->args == NULL) { |
| 1460 | ret = -ENOMEM; |
| 1461 | goto out; |
| 1462 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1463 | for (i = 0; i < tev->nargs; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1464 | p = strchr(argv[i + 2], '='); |
| 1465 | if (p) /* We don't need which register is assigned. */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1466 | *p++ = '\0'; |
| 1467 | else |
| 1468 | p = argv[i + 2]; |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1469 | tev->args[i].name = strdup(argv[i + 2]); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1470 | /* TODO: parse regs and offset */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1471 | tev->args[i].value = strdup(p); |
| 1472 | if (tev->args[i].name == NULL || tev->args[i].value == NULL) { |
| 1473 | ret = -ENOMEM; |
| 1474 | goto out; |
| 1475 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1476 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1477 | ret = 0; |
| 1478 | out: |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 1479 | free(argv0_str); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1480 | argv_free(argv); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1481 | return ret; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1482 | } |
| 1483 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1484 | /* Compose only probe arg */ |
| 1485 | int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len) |
| 1486 | { |
| 1487 | struct perf_probe_arg_field *field = pa->field; |
| 1488 | int ret; |
| 1489 | char *tmp = buf; |
| 1490 | |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1491 | if (pa->name && pa->var) |
| 1492 | ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var); |
| 1493 | else |
| 1494 | ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1495 | if (ret <= 0) |
| 1496 | goto error; |
| 1497 | tmp += ret; |
| 1498 | len -= ret; |
| 1499 | |
| 1500 | while (field) { |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1501 | if (field->name[0] == '[') |
| 1502 | ret = e_snprintf(tmp, len, "%s", field->name); |
| 1503 | else |
| 1504 | ret = e_snprintf(tmp, len, "%s%s", |
| 1505 | field->ref ? "->" : ".", field->name); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1506 | if (ret <= 0) |
| 1507 | goto error; |
| 1508 | tmp += ret; |
| 1509 | len -= ret; |
| 1510 | field = field->next; |
| 1511 | } |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 1512 | |
| 1513 | if (pa->type) { |
| 1514 | ret = e_snprintf(tmp, len, ":%s", pa->type); |
| 1515 | if (ret <= 0) |
| 1516 | goto error; |
| 1517 | tmp += ret; |
| 1518 | len -= ret; |
| 1519 | } |
| 1520 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1521 | return tmp - buf; |
| 1522 | error: |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 1523 | pr_debug("Failed to synthesize perf probe argument: %d\n", ret); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1524 | return ret; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1525 | } |
| 1526 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1527 | /* Compose only probe point (not argument) */ |
| 1528 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1529 | { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1530 | char *buf, *tmp; |
| 1531 | char offs[32] = "", line[32] = "", file[32] = ""; |
| 1532 | int ret, len; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1533 | |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1534 | buf = zalloc(MAX_CMDLEN); |
| 1535 | if (buf == NULL) { |
| 1536 | ret = -ENOMEM; |
| 1537 | goto error; |
| 1538 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1539 | if (pp->offset) { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1540 | ret = e_snprintf(offs, 32, "+%lu", pp->offset); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1541 | if (ret <= 0) |
| 1542 | goto error; |
| 1543 | } |
| 1544 | if (pp->line) { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1545 | ret = e_snprintf(line, 32, ":%d", pp->line); |
| 1546 | if (ret <= 0) |
| 1547 | goto error; |
| 1548 | } |
| 1549 | if (pp->file) { |
Franck Bui-Huu | 32ae2ad | 2010-12-23 16:04:23 +0100 | [diff] [blame] | 1550 | tmp = pp->file; |
| 1551 | len = strlen(tmp); |
| 1552 | if (len > 30) { |
| 1553 | tmp = strchr(pp->file + len - 30, '/'); |
| 1554 | tmp = tmp ? tmp + 1 : pp->file + len - 30; |
| 1555 | } |
| 1556 | ret = e_snprintf(file, 32, "@%s", tmp); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1557 | if (ret <= 0) |
| 1558 | goto error; |
| 1559 | } |
| 1560 | |
| 1561 | if (pp->function) |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1562 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function, |
| 1563 | offs, pp->retprobe ? "%return" : "", line, |
| 1564 | file); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1565 | else |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1566 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1567 | if (ret <= 0) |
| 1568 | goto error; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1569 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1570 | return buf; |
| 1571 | error: |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 1572 | pr_debug("Failed to synthesize perf probe point: %d\n", ret); |
Arnaldo Carvalho de Melo | f538565 | 2013-12-26 15:54:57 -0300 | [diff] [blame] | 1573 | free(buf); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1574 | return NULL; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | #if 0 |
| 1578 | char *synthesize_perf_probe_command(struct perf_probe_event *pev) |
| 1579 | { |
| 1580 | char *buf; |
| 1581 | int i, len, ret; |
| 1582 | |
| 1583 | buf = synthesize_perf_probe_point(&pev->point); |
| 1584 | if (!buf) |
| 1585 | return NULL; |
| 1586 | |
| 1587 | len = strlen(buf); |
| 1588 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1589 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1590 | pev->args[i].name); |
| 1591 | if (ret <= 0) { |
| 1592 | free(buf); |
| 1593 | return NULL; |
| 1594 | } |
| 1595 | len += ret; |
| 1596 | } |
| 1597 | |
| 1598 | return buf; |
| 1599 | } |
| 1600 | #endif |
| 1601 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1602 | static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref, |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1603 | char **buf, size_t *buflen, |
| 1604 | int depth) |
| 1605 | { |
| 1606 | int ret; |
| 1607 | if (ref->next) { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1608 | depth = __synthesize_probe_trace_arg_ref(ref->next, buf, |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1609 | buflen, depth + 1); |
| 1610 | if (depth < 0) |
| 1611 | goto out; |
| 1612 | } |
| 1613 | |
| 1614 | ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset); |
| 1615 | if (ret < 0) |
| 1616 | depth = ret; |
| 1617 | else { |
| 1618 | *buf += ret; |
| 1619 | *buflen -= ret; |
| 1620 | } |
| 1621 | out: |
| 1622 | return depth; |
| 1623 | |
| 1624 | } |
| 1625 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1626 | static int synthesize_probe_trace_arg(struct probe_trace_arg *arg, |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1627 | char *buf, size_t buflen) |
| 1628 | { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1629 | struct probe_trace_arg_ref *ref = arg->ref; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1630 | int ret, depth = 0; |
| 1631 | char *tmp = buf; |
| 1632 | |
| 1633 | /* Argument name or separator */ |
| 1634 | if (arg->name) |
| 1635 | ret = e_snprintf(buf, buflen, " %s=", arg->name); |
| 1636 | else |
| 1637 | ret = e_snprintf(buf, buflen, " "); |
| 1638 | if (ret < 0) |
| 1639 | return ret; |
| 1640 | buf += ret; |
| 1641 | buflen -= ret; |
| 1642 | |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 1643 | /* Special case: @XXX */ |
| 1644 | if (arg->value[0] == '@' && arg->ref) |
| 1645 | ref = ref->next; |
| 1646 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1647 | /* Dereferencing arguments */ |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 1648 | if (ref) { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1649 | depth = __synthesize_probe_trace_arg_ref(ref, &buf, |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1650 | &buflen, 1); |
| 1651 | if (depth < 0) |
| 1652 | return depth; |
| 1653 | } |
| 1654 | |
| 1655 | /* Print argument value */ |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 1656 | if (arg->value[0] == '@' && arg->ref) |
| 1657 | ret = e_snprintf(buf, buflen, "%s%+ld", arg->value, |
| 1658 | arg->ref->offset); |
| 1659 | else |
| 1660 | ret = e_snprintf(buf, buflen, "%s", arg->value); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1661 | if (ret < 0) |
| 1662 | return ret; |
| 1663 | buf += ret; |
| 1664 | buflen -= ret; |
| 1665 | |
| 1666 | /* Closing */ |
| 1667 | while (depth--) { |
| 1668 | ret = e_snprintf(buf, buflen, ")"); |
| 1669 | if (ret < 0) |
| 1670 | return ret; |
| 1671 | buf += ret; |
| 1672 | buflen -= ret; |
| 1673 | } |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 1674 | /* Print argument type */ |
| 1675 | if (arg->type) { |
| 1676 | ret = e_snprintf(buf, buflen, ":%s", arg->type); |
| 1677 | if (ret <= 0) |
| 1678 | return ret; |
| 1679 | buf += ret; |
| 1680 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1681 | |
| 1682 | return buf - tmp; |
| 1683 | } |
| 1684 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1685 | char *synthesize_probe_trace_command(struct probe_trace_event *tev) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1686 | { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1687 | struct probe_trace_point *tp = &tev->point; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1688 | char *buf; |
| 1689 | int i, len, ret; |
| 1690 | |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1691 | buf = zalloc(MAX_CMDLEN); |
| 1692 | if (buf == NULL) |
| 1693 | return NULL; |
| 1694 | |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 1695 | len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p', |
| 1696 | tev->group, tev->event); |
| 1697 | if (len <= 0) |
| 1698 | goto error; |
| 1699 | |
| 1700 | /* Uprobes must have tp->address and tp->module */ |
| 1701 | if (tev->uprobes && (!tp->address || !tp->module)) |
| 1702 | goto error; |
| 1703 | |
| 1704 | /* Use the tp->address for uprobes */ |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 1705 | if (tev->uprobes) |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 1706 | ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx", |
| 1707 | tp->module, tp->address); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 1708 | else |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 1709 | ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu", |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 1710 | tp->module ?: "", tp->module ? ":" : "", |
| 1711 | tp->symbol, tp->offset); |
| 1712 | |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 1713 | if (ret <= 0) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1714 | goto error; |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 1715 | len += ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1716 | |
| 1717 | for (i = 0; i < tev->nargs; i++) { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1718 | ret = synthesize_probe_trace_arg(&tev->args[i], buf + len, |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1719 | MAX_CMDLEN - len); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1720 | if (ret <= 0) |
| 1721 | goto error; |
| 1722 | len += ret; |
| 1723 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1724 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1725 | return buf; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1726 | error: |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1727 | free(buf); |
| 1728 | return NULL; |
| 1729 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1730 | |
Masami Hiramatsu | 5a6f631 | 2014-02-06 05:32:23 +0000 | [diff] [blame] | 1731 | static int find_perf_probe_point_from_map(struct probe_trace_point *tp, |
| 1732 | struct perf_probe_point *pp, |
| 1733 | bool is_kprobe) |
| 1734 | { |
| 1735 | struct symbol *sym = NULL; |
| 1736 | struct map *map; |
| 1737 | u64 addr; |
| 1738 | int ret = -ENOENT; |
| 1739 | |
| 1740 | if (!is_kprobe) { |
| 1741 | map = dso__new_map(tp->module); |
| 1742 | if (!map) |
| 1743 | goto out; |
| 1744 | addr = tp->address; |
| 1745 | sym = map__find_symbol(map, addr, NULL); |
| 1746 | } else { |
| 1747 | addr = kernel_get_symbol_address_by_name(tp->symbol, true); |
| 1748 | if (addr) { |
| 1749 | addr += tp->offset; |
| 1750 | sym = __find_kernel_function(addr, &map); |
| 1751 | } |
| 1752 | } |
| 1753 | if (!sym) |
| 1754 | goto out; |
| 1755 | |
| 1756 | pp->retprobe = tp->retprobe; |
| 1757 | pp->offset = addr - map->unmap_ip(map, sym->start); |
| 1758 | pp->function = strdup(sym->name); |
| 1759 | ret = pp->function ? 0 : -ENOMEM; |
| 1760 | |
| 1761 | out: |
| 1762 | if (map && !is_kprobe) { |
| 1763 | dso__delete(map->dso); |
| 1764 | map__delete(map); |
| 1765 | } |
| 1766 | |
| 1767 | return ret; |
| 1768 | } |
| 1769 | |
| 1770 | static int convert_to_perf_probe_point(struct probe_trace_point *tp, |
| 1771 | struct perf_probe_point *pp, |
| 1772 | bool is_kprobe) |
| 1773 | { |
| 1774 | char buf[128]; |
| 1775 | int ret; |
| 1776 | |
| 1777 | ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe); |
| 1778 | if (!ret) |
| 1779 | return 0; |
| 1780 | ret = find_perf_probe_point_from_map(tp, pp, is_kprobe); |
| 1781 | if (!ret) |
| 1782 | return 0; |
| 1783 | |
| 1784 | pr_debug("Failed to find probe point from both of dwarf and map.\n"); |
| 1785 | |
| 1786 | if (tp->symbol) { |
| 1787 | pp->function = strdup(tp->symbol); |
| 1788 | pp->offset = tp->offset; |
| 1789 | } else if (!tp->module && !is_kprobe) { |
| 1790 | ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address); |
| 1791 | if (ret < 0) |
| 1792 | return ret; |
| 1793 | pp->function = strdup(buf); |
| 1794 | pp->offset = 0; |
| 1795 | } |
| 1796 | if (pp->function == NULL) |
| 1797 | return -ENOMEM; |
| 1798 | |
| 1799 | pp->retprobe = tp->retprobe; |
| 1800 | |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1804 | static int convert_to_perf_probe_event(struct probe_trace_event *tev, |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 1805 | struct perf_probe_event *pev, bool is_kprobe) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1806 | { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1807 | char buf[64] = ""; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1808 | int i, ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1809 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 1810 | /* Convert event/group name */ |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1811 | pev->event = strdup(tev->event); |
| 1812 | pev->group = strdup(tev->group); |
| 1813 | if (pev->event == NULL || pev->group == NULL) |
| 1814 | return -ENOMEM; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1815 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 1816 | /* Convert trace_point to probe_point */ |
Masami Hiramatsu | 5a6f631 | 2014-02-06 05:32:23 +0000 | [diff] [blame] | 1817 | ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1818 | if (ret < 0) |
| 1819 | return ret; |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 1820 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1821 | /* Convert trace_arg to probe_arg */ |
| 1822 | pev->nargs = tev->nargs; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1823 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
| 1824 | if (pev->args == NULL) |
| 1825 | return -ENOMEM; |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1826 | for (i = 0; i < tev->nargs && ret >= 0; i++) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1827 | if (tev->args[i].name) |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1828 | pev->args[i].name = strdup(tev->args[i].name); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1829 | else { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1830 | ret = synthesize_probe_trace_arg(&tev->args[i], |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1831 | buf, 64); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1832 | pev->args[i].name = strdup(buf); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1833 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1834 | if (pev->args[i].name == NULL && ret >= 0) |
| 1835 | ret = -ENOMEM; |
| 1836 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1837 | |
| 1838 | if (ret < 0) |
| 1839 | clear_perf_probe_event(pev); |
| 1840 | |
| 1841 | return ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | void clear_perf_probe_event(struct perf_probe_event *pev) |
| 1845 | { |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1846 | struct perf_probe_arg_field *field, *next; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1847 | int i; |
| 1848 | |
Arnaldo Carvalho de Melo | f538565 | 2013-12-26 15:54:57 -0300 | [diff] [blame] | 1849 | free(pev->event); |
| 1850 | free(pev->group); |
Masami Hiramatsu | 7afb3fa | 2015-04-01 19:25:39 +0900 | [diff] [blame] | 1851 | free(pev->target); |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 1852 | clear_perf_probe_point(&pev->point); |
Arnaldo Carvalho de Melo | f538565 | 2013-12-26 15:54:57 -0300 | [diff] [blame] | 1853 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1854 | for (i = 0; i < pev->nargs; i++) { |
Arnaldo Carvalho de Melo | f538565 | 2013-12-26 15:54:57 -0300 | [diff] [blame] | 1855 | free(pev->args[i].name); |
| 1856 | free(pev->args[i].var); |
| 1857 | free(pev->args[i].type); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1858 | field = pev->args[i].field; |
| 1859 | while (field) { |
| 1860 | next = field->next; |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 1861 | zfree(&field->name); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1862 | free(field); |
| 1863 | field = next; |
| 1864 | } |
| 1865 | } |
Arnaldo Carvalho de Melo | f538565 | 2013-12-26 15:54:57 -0300 | [diff] [blame] | 1866 | free(pev->args); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1867 | memset(pev, 0, sizeof(*pev)); |
| 1868 | } |
| 1869 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1870 | static void clear_probe_trace_event(struct probe_trace_event *tev) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1871 | { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1872 | struct probe_trace_arg_ref *ref, *next; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1873 | int i; |
| 1874 | |
Arnaldo Carvalho de Melo | f538565 | 2013-12-26 15:54:57 -0300 | [diff] [blame] | 1875 | free(tev->event); |
| 1876 | free(tev->group); |
| 1877 | free(tev->point.symbol); |
| 1878 | free(tev->point.module); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1879 | for (i = 0; i < tev->nargs; i++) { |
Arnaldo Carvalho de Melo | f538565 | 2013-12-26 15:54:57 -0300 | [diff] [blame] | 1880 | free(tev->args[i].name); |
| 1881 | free(tev->args[i].value); |
| 1882 | free(tev->args[i].type); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1883 | ref = tev->args[i].ref; |
| 1884 | while (ref) { |
| 1885 | next = ref->next; |
| 1886 | free(ref); |
| 1887 | ref = next; |
| 1888 | } |
| 1889 | } |
Arnaldo Carvalho de Melo | f538565 | 2013-12-26 15:54:57 -0300 | [diff] [blame] | 1890 | free(tev->args); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1891 | memset(tev, 0, sizeof(*tev)); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1892 | } |
| 1893 | |
Masami Hiramatsu | 5e45187 | 2014-08-13 16:12:48 +0000 | [diff] [blame] | 1894 | static void print_open_warning(int err, bool is_kprobe) |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 1895 | { |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 1896 | char sbuf[STRERR_BUFSIZE]; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 1897 | |
Masami Hiramatsu | 5e45187 | 2014-08-13 16:12:48 +0000 | [diff] [blame] | 1898 | if (err == -ENOENT) { |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 1899 | const char *config; |
| 1900 | |
| 1901 | if (!is_kprobe) |
| 1902 | config = "CONFIG_UPROBE_EVENTS"; |
| 1903 | else |
| 1904 | config = "CONFIG_KPROBE_EVENTS"; |
| 1905 | |
Masami Hiramatsu | 5e45187 | 2014-08-13 16:12:48 +0000 | [diff] [blame] | 1906 | pr_warning("%cprobe_events file does not exist" |
| 1907 | " - please rebuild kernel with %s.\n", |
| 1908 | is_kprobe ? 'k' : 'u', config); |
| 1909 | } else if (err == -ENOTSUP) |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 1910 | pr_warning("Tracefs or debugfs is not mounted.\n"); |
Masami Hiramatsu | 5e45187 | 2014-08-13 16:12:48 +0000 | [diff] [blame] | 1911 | else |
| 1912 | pr_warning("Failed to open %cprobe_events: %s\n", |
| 1913 | is_kprobe ? 'k' : 'u', |
| 1914 | strerror_r(-err, sbuf, sizeof(sbuf))); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 1915 | } |
| 1916 | |
Masami Hiramatsu | 467ec08 | 2014-08-13 16:12:50 +0000 | [diff] [blame] | 1917 | static void print_both_open_warning(int kerr, int uerr) |
| 1918 | { |
| 1919 | /* Both kprobes and uprobes are disabled, warn it. */ |
| 1920 | if (kerr == -ENOTSUP && uerr == -ENOTSUP) |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 1921 | pr_warning("Tracefs or debugfs is not mounted.\n"); |
Masami Hiramatsu | 467ec08 | 2014-08-13 16:12:50 +0000 | [diff] [blame] | 1922 | else if (kerr == -ENOENT && uerr == -ENOENT) |
| 1923 | pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS " |
| 1924 | "or/and CONFIG_UPROBE_EVENTS.\n"); |
| 1925 | else { |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 1926 | char sbuf[STRERR_BUFSIZE]; |
Masami Hiramatsu | 467ec08 | 2014-08-13 16:12:50 +0000 | [diff] [blame] | 1927 | pr_warning("Failed to open kprobe events: %s.\n", |
| 1928 | strerror_r(-kerr, sbuf, sizeof(sbuf))); |
| 1929 | pr_warning("Failed to open uprobe events: %s.\n", |
| 1930 | strerror_r(-uerr, sbuf, sizeof(sbuf))); |
| 1931 | } |
| 1932 | } |
| 1933 | |
Masami Hiramatsu | 5e45187 | 2014-08-13 16:12:48 +0000 | [diff] [blame] | 1934 | static int open_probe_events(const char *trace_file, bool readwrite) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1935 | { |
| 1936 | char buf[PATH_MAX]; |
Masami Hiramatsu | 7ca5989 | 2010-04-14 18:39:28 -0400 | [diff] [blame] | 1937 | const char *__debugfs; |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 1938 | const char *tracing_dir = ""; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1939 | int ret; |
| 1940 | |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 1941 | __debugfs = tracefs_find_mountpoint(); |
| 1942 | if (__debugfs == NULL) { |
| 1943 | tracing_dir = "tracing/"; |
Masami Hiramatsu | 7ca5989 | 2010-04-14 18:39:28 -0400 | [diff] [blame] | 1944 | |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 1945 | __debugfs = debugfs_find_mountpoint(); |
| 1946 | if (__debugfs == NULL) |
| 1947 | return -ENOTSUP; |
| 1948 | } |
| 1949 | |
| 1950 | ret = e_snprintf(buf, PATH_MAX, "%s/%s%s", |
| 1951 | __debugfs, tracing_dir, trace_file); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1952 | if (ret >= 0) { |
Masami Hiramatsu | 7ca5989 | 2010-04-14 18:39:28 -0400 | [diff] [blame] | 1953 | pr_debug("Opening %s write=%d\n", buf, readwrite); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1954 | if (readwrite && !probe_event_dry_run) |
| 1955 | ret = open(buf, O_RDWR, O_APPEND); |
| 1956 | else |
| 1957 | ret = open(buf, O_RDONLY, 0); |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 1958 | |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 1959 | if (ret < 0) |
Masami Hiramatsu | 5e45187 | 2014-08-13 16:12:48 +0000 | [diff] [blame] | 1960 | ret = -errno; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1961 | } |
| 1962 | return ret; |
| 1963 | } |
| 1964 | |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 1965 | static int open_kprobe_events(bool readwrite) |
| 1966 | { |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 1967 | return open_probe_events("kprobe_events", readwrite); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | static int open_uprobe_events(bool readwrite) |
| 1971 | { |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 1972 | return open_probe_events("uprobe_events", readwrite); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | /* Get raw string list of current kprobe_events or uprobe_events */ |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1976 | static struct strlist *get_probe_trace_command_rawlist(int fd) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 1977 | { |
| 1978 | int ret, idx; |
| 1979 | FILE *fp; |
| 1980 | char buf[MAX_CMDLEN]; |
| 1981 | char *p; |
| 1982 | struct strlist *sl; |
| 1983 | |
| 1984 | sl = strlist__new(true, NULL); |
| 1985 | |
| 1986 | fp = fdopen(dup(fd), "r"); |
| 1987 | while (!feof(fp)) { |
| 1988 | p = fgets(buf, MAX_CMDLEN, fp); |
| 1989 | if (!p) |
| 1990 | break; |
| 1991 | |
| 1992 | idx = strlen(p) - 1; |
| 1993 | if (p[idx] == '\n') |
| 1994 | p[idx] = '\0'; |
| 1995 | ret = strlist__add(sl, buf); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1996 | if (ret < 0) { |
Masami Hiramatsu | 6eb0866 | 2014-08-14 02:22:30 +0000 | [diff] [blame] | 1997 | pr_debug("strlist__add failed (%d)\n", ret); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 1998 | strlist__delete(sl); |
| 1999 | return NULL; |
| 2000 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 2001 | } |
| 2002 | fclose(fp); |
| 2003 | |
| 2004 | return sl; |
| 2005 | } |
| 2006 | |
Masami Hiramatsu | 9aaf5a5 | 2015-02-19 23:31:13 +0900 | [diff] [blame] | 2007 | struct kprobe_blacklist_node { |
| 2008 | struct list_head list; |
| 2009 | unsigned long start; |
| 2010 | unsigned long end; |
| 2011 | char *symbol; |
| 2012 | }; |
| 2013 | |
| 2014 | static void kprobe_blacklist__delete(struct list_head *blacklist) |
| 2015 | { |
| 2016 | struct kprobe_blacklist_node *node; |
| 2017 | |
| 2018 | while (!list_empty(blacklist)) { |
| 2019 | node = list_first_entry(blacklist, |
| 2020 | struct kprobe_blacklist_node, list); |
| 2021 | list_del(&node->list); |
| 2022 | free(node->symbol); |
| 2023 | free(node); |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | static int kprobe_blacklist__load(struct list_head *blacklist) |
| 2028 | { |
| 2029 | struct kprobe_blacklist_node *node; |
| 2030 | const char *__debugfs = debugfs_find_mountpoint(); |
| 2031 | char buf[PATH_MAX], *p; |
| 2032 | FILE *fp; |
| 2033 | int ret; |
| 2034 | |
| 2035 | if (__debugfs == NULL) |
| 2036 | return -ENOTSUP; |
| 2037 | |
| 2038 | ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs); |
| 2039 | if (ret < 0) |
| 2040 | return ret; |
| 2041 | |
| 2042 | fp = fopen(buf, "r"); |
| 2043 | if (!fp) |
| 2044 | return -errno; |
| 2045 | |
| 2046 | ret = 0; |
| 2047 | while (fgets(buf, PATH_MAX, fp)) { |
| 2048 | node = zalloc(sizeof(*node)); |
| 2049 | if (!node) { |
| 2050 | ret = -ENOMEM; |
| 2051 | break; |
| 2052 | } |
| 2053 | INIT_LIST_HEAD(&node->list); |
| 2054 | list_add_tail(&node->list, blacklist); |
| 2055 | if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) { |
| 2056 | ret = -EINVAL; |
| 2057 | break; |
| 2058 | } |
| 2059 | p = strchr(buf, '\t'); |
| 2060 | if (p) { |
| 2061 | p++; |
| 2062 | if (p[strlen(p) - 1] == '\n') |
| 2063 | p[strlen(p) - 1] = '\0'; |
| 2064 | } else |
| 2065 | p = (char *)"unknown"; |
| 2066 | node->symbol = strdup(p); |
| 2067 | if (!node->symbol) { |
| 2068 | ret = -ENOMEM; |
| 2069 | break; |
| 2070 | } |
| 2071 | pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n", |
| 2072 | node->start, node->end, node->symbol); |
| 2073 | ret++; |
| 2074 | } |
| 2075 | if (ret < 0) |
| 2076 | kprobe_blacklist__delete(blacklist); |
| 2077 | fclose(fp); |
| 2078 | |
| 2079 | return ret; |
| 2080 | } |
| 2081 | |
| 2082 | static struct kprobe_blacklist_node * |
| 2083 | kprobe_blacklist__find_by_address(struct list_head *blacklist, |
| 2084 | unsigned long address) |
| 2085 | { |
| 2086 | struct kprobe_blacklist_node *node; |
| 2087 | |
| 2088 | list_for_each_entry(node, blacklist, list) { |
| 2089 | if (node->start <= address && address <= node->end) |
| 2090 | return node; |
| 2091 | } |
| 2092 | |
| 2093 | return NULL; |
| 2094 | } |
| 2095 | |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 2096 | /* Show an event */ |
Masami Hiramatsu | fb226cc | 2014-02-06 05:32:13 +0000 | [diff] [blame] | 2097 | static int show_perf_probe_event(struct perf_probe_event *pev, |
| 2098 | const char *module) |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 2099 | { |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 2100 | int i, ret; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 2101 | char buf[128]; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2102 | char *place; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 2103 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2104 | /* Synthesize only event probe point */ |
| 2105 | place = synthesize_perf_probe_point(&pev->point); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2106 | if (!place) |
| 2107 | return -EINVAL; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2108 | |
| 2109 | ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event); |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 2110 | if (ret < 0) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2111 | return ret; |
| 2112 | |
Masami Hiramatsu | 5e17b28 | 2014-10-27 16:31:31 -0400 | [diff] [blame] | 2113 | pr_info(" %-20s (on %s", buf, place); |
Masami Hiramatsu | fb226cc | 2014-02-06 05:32:13 +0000 | [diff] [blame] | 2114 | if (module) |
Masami Hiramatsu | 5e17b28 | 2014-10-27 16:31:31 -0400 | [diff] [blame] | 2115 | pr_info(" in %s", module); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 2116 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2117 | if (pev->nargs > 0) { |
Masami Hiramatsu | 5e17b28 | 2014-10-27 16:31:31 -0400 | [diff] [blame] | 2118 | pr_info(" with"); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 2119 | for (i = 0; i < pev->nargs; i++) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2120 | ret = synthesize_perf_probe_arg(&pev->args[i], |
| 2121 | buf, 128); |
| 2122 | if (ret < 0) |
| 2123 | break; |
Masami Hiramatsu | 5e17b28 | 2014-10-27 16:31:31 -0400 | [diff] [blame] | 2124 | pr_info(" %s", buf); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 2125 | } |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 2126 | } |
Masami Hiramatsu | 5e17b28 | 2014-10-27 16:31:31 -0400 | [diff] [blame] | 2127 | pr_info(")\n"); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2128 | free(place); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2129 | return ret; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 2130 | } |
| 2131 | |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2132 | static int __show_perf_probe_events(int fd, bool is_kprobe) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 2133 | { |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2134 | int ret = 0; |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2135 | struct probe_trace_event tev; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2136 | struct perf_probe_event pev; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 2137 | struct strlist *rawlist; |
| 2138 | struct str_node *ent; |
| 2139 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2140 | memset(&tev, 0, sizeof(tev)); |
| 2141 | memset(&pev, 0, sizeof(pev)); |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 2142 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2143 | rawlist = get_probe_trace_command_rawlist(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2144 | if (!rawlist) |
Masami Hiramatsu | 6eb0866 | 2014-08-14 02:22:30 +0000 | [diff] [blame] | 2145 | return -ENOMEM; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 2146 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 2147 | strlist__for_each(ent, rawlist) { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2148 | ret = parse_probe_trace_command(ent->s, &tev); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2149 | if (ret >= 0) { |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2150 | ret = convert_to_perf_probe_event(&tev, &pev, |
| 2151 | is_kprobe); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2152 | if (ret >= 0) |
Masami Hiramatsu | fb226cc | 2014-02-06 05:32:13 +0000 | [diff] [blame] | 2153 | ret = show_perf_probe_event(&pev, |
| 2154 | tev.point.module); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2155 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2156 | clear_perf_probe_event(&pev); |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2157 | clear_probe_trace_event(&tev); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2158 | if (ret < 0) |
| 2159 | break; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 2160 | } |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 2161 | strlist__delete(rawlist); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2162 | |
| 2163 | return ret; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 2164 | } |
| 2165 | |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2166 | /* List up current perf-probe events */ |
| 2167 | int show_perf_probe_events(void) |
| 2168 | { |
Masami Hiramatsu | 5e45187 | 2014-08-13 16:12:48 +0000 | [diff] [blame] | 2169 | int kp_fd, up_fd, ret; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2170 | |
| 2171 | setup_pager(); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2172 | |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 2173 | ret = init_symbol_maps(false); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2174 | if (ret < 0) |
| 2175 | return ret; |
| 2176 | |
Masami Hiramatsu | 5e45187 | 2014-08-13 16:12:48 +0000 | [diff] [blame] | 2177 | kp_fd = open_kprobe_events(false); |
| 2178 | if (kp_fd >= 0) { |
| 2179 | ret = __show_perf_probe_events(kp_fd, true); |
| 2180 | close(kp_fd); |
| 2181 | if (ret < 0) |
| 2182 | goto out; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2183 | } |
| 2184 | |
Masami Hiramatsu | 5e45187 | 2014-08-13 16:12:48 +0000 | [diff] [blame] | 2185 | up_fd = open_uprobe_events(false); |
| 2186 | if (kp_fd < 0 && up_fd < 0) { |
Masami Hiramatsu | 467ec08 | 2014-08-13 16:12:50 +0000 | [diff] [blame] | 2187 | print_both_open_warning(kp_fd, up_fd); |
Masami Hiramatsu | 5e45187 | 2014-08-13 16:12:48 +0000 | [diff] [blame] | 2188 | ret = kp_fd; |
| 2189 | goto out; |
| 2190 | } |
| 2191 | |
| 2192 | if (up_fd >= 0) { |
| 2193 | ret = __show_perf_probe_events(up_fd, false); |
| 2194 | close(up_fd); |
| 2195 | } |
| 2196 | out: |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 2197 | exit_symbol_maps(); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2198 | return ret; |
| 2199 | } |
| 2200 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2201 | /* Get current perf-probe event names */ |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2202 | static struct strlist *get_probe_trace_event_names(int fd, bool include_group) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2203 | { |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2204 | char buf[128]; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2205 | struct strlist *sl, *rawlist; |
| 2206 | struct str_node *ent; |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2207 | struct probe_trace_event tev; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2208 | int ret = 0; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2209 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2210 | memset(&tev, 0, sizeof(tev)); |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2211 | rawlist = get_probe_trace_command_rawlist(fd); |
Masami Hiramatsu | 6eb0866 | 2014-08-14 02:22:30 +0000 | [diff] [blame] | 2212 | if (!rawlist) |
| 2213 | return NULL; |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 2214 | sl = strlist__new(true, NULL); |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 2215 | strlist__for_each(ent, rawlist) { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2216 | ret = parse_probe_trace_command(ent->s, &tev); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2217 | if (ret < 0) |
| 2218 | break; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2219 | if (include_group) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2220 | ret = e_snprintf(buf, 128, "%s:%s", tev.group, |
| 2221 | tev.event); |
| 2222 | if (ret >= 0) |
| 2223 | ret = strlist__add(sl, buf); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2224 | } else |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2225 | ret = strlist__add(sl, tev.event); |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2226 | clear_probe_trace_event(&tev); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2227 | if (ret < 0) |
| 2228 | break; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2229 | } |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2230 | strlist__delete(rawlist); |
| 2231 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2232 | if (ret < 0) { |
| 2233 | strlist__delete(sl); |
| 2234 | return NULL; |
| 2235 | } |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2236 | return sl; |
| 2237 | } |
| 2238 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2239 | static int write_probe_trace_event(int fd, struct probe_trace_event *tev) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 2240 | { |
Frederic Weisbecker | 6eca8cc | 2010-04-21 02:01:05 +0200 | [diff] [blame] | 2241 | int ret = 0; |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2242 | char *buf = synthesize_probe_trace_command(tev); |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 2243 | char sbuf[STRERR_BUFSIZE]; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 2244 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2245 | if (!buf) { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2246 | pr_debug("Failed to synthesize probe trace event.\n"); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2247 | return -EINVAL; |
| 2248 | } |
| 2249 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2250 | pr_debug("Writing event: %s\n", buf); |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 2251 | if (!probe_event_dry_run) { |
| 2252 | ret = write(fd, buf, strlen(buf)); |
Namhyung Kim | 7949ba1 | 2015-01-10 19:33:48 +0900 | [diff] [blame] | 2253 | if (ret <= 0) { |
| 2254 | ret = -errno; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2255 | pr_warning("Failed to write event: %s\n", |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 2256 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Namhyung Kim | 7949ba1 | 2015-01-10 19:33:48 +0900 | [diff] [blame] | 2257 | } |
Masami Hiramatsu | f4d7da4 | 2010-03-16 18:06:05 -0400 | [diff] [blame] | 2258 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2259 | free(buf); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2260 | return ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 2261 | } |
| 2262 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2263 | static int get_new_event_name(char *buf, size_t len, const char *base, |
| 2264 | struct strlist *namelist, bool allow_suffix) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2265 | { |
| 2266 | int i, ret; |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 2267 | |
| 2268 | /* Try no suffix */ |
| 2269 | ret = e_snprintf(buf, len, "%s", base); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2270 | if (ret < 0) { |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 2271 | pr_debug("snprintf() failed: %d\n", ret); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2272 | return ret; |
| 2273 | } |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 2274 | if (!strlist__has_entry(namelist, buf)) |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2275 | return 0; |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 2276 | |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 2277 | if (!allow_suffix) { |
| 2278 | pr_warning("Error: event \"%s\" already exists. " |
| 2279 | "(Use -f to force duplicates.)\n", base); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2280 | return -EEXIST; |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 2281 | } |
| 2282 | |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 2283 | /* Try to add suffix */ |
| 2284 | for (i = 1; i < MAX_EVENT_INDEX; i++) { |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2285 | ret = e_snprintf(buf, len, "%s_%d", base, i); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2286 | if (ret < 0) { |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 2287 | pr_debug("snprintf() failed: %d\n", ret); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2288 | return ret; |
| 2289 | } |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2290 | if (!strlist__has_entry(namelist, buf)) |
| 2291 | break; |
| 2292 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2293 | if (i == MAX_EVENT_INDEX) { |
| 2294 | pr_warning("Too many events are on the same function.\n"); |
| 2295 | ret = -ERANGE; |
| 2296 | } |
| 2297 | |
| 2298 | return ret; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2299 | } |
| 2300 | |
Masami Hiramatsu | 79702f6 | 2015-02-28 11:53:29 +0900 | [diff] [blame] | 2301 | /* Warn if the current kernel's uprobe implementation is old */ |
| 2302 | static void warn_uprobe_event_compat(struct probe_trace_event *tev) |
| 2303 | { |
| 2304 | int i; |
| 2305 | char *buf = synthesize_probe_trace_command(tev); |
| 2306 | |
| 2307 | /* Old uprobe event doesn't support memory dereference */ |
| 2308 | if (!tev->uprobes || tev->nargs == 0 || !buf) |
| 2309 | goto out; |
| 2310 | |
| 2311 | for (i = 0; i < tev->nargs; i++) |
| 2312 | if (strglobmatch(tev->args[i].value, "[$@+-]*")) { |
| 2313 | pr_warning("Please upgrade your kernel to at least " |
| 2314 | "3.14 to have access to feature %s\n", |
| 2315 | tev->args[i].value); |
| 2316 | break; |
| 2317 | } |
| 2318 | out: |
| 2319 | free(buf); |
| 2320 | } |
| 2321 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2322 | static int __add_probe_trace_events(struct perf_probe_event *pev, |
| 2323 | struct probe_trace_event *tevs, |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2324 | int ntevs, bool allow_suffix) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 2325 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2326 | int i, fd, ret; |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2327 | struct probe_trace_event *tev = NULL; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2328 | char buf[64]; |
| 2329 | const char *event, *group; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2330 | struct strlist *namelist; |
Masami Hiramatsu | 9aaf5a5 | 2015-02-19 23:31:13 +0900 | [diff] [blame] | 2331 | LIST_HEAD(blacklist); |
| 2332 | struct kprobe_blacklist_node *node; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 2333 | |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2334 | if (pev->uprobes) |
| 2335 | fd = open_uprobe_events(true); |
| 2336 | else |
| 2337 | fd = open_kprobe_events(true); |
| 2338 | |
Masami Hiramatsu | 5e45187 | 2014-08-13 16:12:48 +0000 | [diff] [blame] | 2339 | if (fd < 0) { |
| 2340 | print_open_warning(fd, !pev->uprobes); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2341 | return fd; |
Masami Hiramatsu | 5e45187 | 2014-08-13 16:12:48 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 2344 | /* Get current event names */ |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2345 | namelist = get_probe_trace_event_names(fd, false); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2346 | if (!namelist) { |
| 2347 | pr_debug("Failed to get current event list.\n"); |
| 2348 | return -EIO; |
| 2349 | } |
Masami Hiramatsu | 9aaf5a5 | 2015-02-19 23:31:13 +0900 | [diff] [blame] | 2350 | /* Get kprobe blacklist if exists */ |
| 2351 | if (!pev->uprobes) { |
| 2352 | ret = kprobe_blacklist__load(&blacklist); |
| 2353 | if (ret < 0) |
| 2354 | pr_debug("No kprobe blacklist support, ignored\n"); |
| 2355 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 2356 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2357 | ret = 0; |
Masami Hiramatsu | 5e17b28 | 2014-10-27 16:31:31 -0400 | [diff] [blame] | 2358 | pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":"); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 2359 | for (i = 0; i < ntevs; i++) { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2360 | tev = &tevs[i]; |
Masami Hiramatsu | 9aaf5a5 | 2015-02-19 23:31:13 +0900 | [diff] [blame] | 2361 | /* Ensure that the address is NOT blacklisted */ |
| 2362 | node = kprobe_blacklist__find_by_address(&blacklist, |
| 2363 | tev->point.address); |
| 2364 | if (node) { |
| 2365 | pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol); |
| 2366 | continue; |
| 2367 | } |
| 2368 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2369 | if (pev->event) |
| 2370 | event = pev->event; |
| 2371 | else |
| 2372 | if (pev->point.function) |
| 2373 | event = pev->point.function; |
| 2374 | else |
| 2375 | event = tev->point.symbol; |
| 2376 | if (pev->group) |
| 2377 | group = pev->group; |
| 2378 | else |
| 2379 | group = PERFPROBE_GROUP; |
| 2380 | |
| 2381 | /* Get an unused new event name */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2382 | ret = get_new_event_name(buf, 64, event, |
| 2383 | namelist, allow_suffix); |
| 2384 | if (ret < 0) |
| 2385 | break; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2386 | event = buf; |
| 2387 | |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 2388 | tev->event = strdup(event); |
| 2389 | tev->group = strdup(group); |
| 2390 | if (tev->event == NULL || tev->group == NULL) { |
| 2391 | ret = -ENOMEM; |
| 2392 | break; |
| 2393 | } |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2394 | ret = write_probe_trace_event(fd, tev); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2395 | if (ret < 0) |
| 2396 | break; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2397 | /* Add added event name to namelist */ |
| 2398 | strlist__add(namelist, event); |
| 2399 | |
| 2400 | /* Trick here - save current event/group */ |
| 2401 | event = pev->event; |
| 2402 | group = pev->group; |
| 2403 | pev->event = tev->event; |
| 2404 | pev->group = tev->group; |
Masami Hiramatsu | fb226cc | 2014-02-06 05:32:13 +0000 | [diff] [blame] | 2405 | show_perf_probe_event(pev, tev->point.module); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2406 | /* Trick here - restore current event/group */ |
| 2407 | pev->event = (char *)event; |
| 2408 | pev->group = (char *)group; |
| 2409 | |
| 2410 | /* |
| 2411 | * Probes after the first probe which comes from same |
| 2412 | * user input are always allowed to add suffix, because |
| 2413 | * there might be several addresses corresponding to |
| 2414 | * one code line. |
| 2415 | */ |
| 2416 | allow_suffix = true; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 2417 | } |
Masami Hiramatsu | 79702f6 | 2015-02-28 11:53:29 +0900 | [diff] [blame] | 2418 | if (ret == -EINVAL && pev->uprobes) |
| 2419 | warn_uprobe_event_compat(tev); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2420 | |
Masami Hiramatsu | 9aaf5a5 | 2015-02-19 23:31:13 +0900 | [diff] [blame] | 2421 | /* Note that it is possible to skip all events because of blacklist */ |
| 2422 | if (ret >= 0 && tev->event) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2423 | /* Show how to use the event. */ |
Masami Hiramatsu | 5e17b28 | 2014-10-27 16:31:31 -0400 | [diff] [blame] | 2424 | pr_info("\nYou can now use it in all perf tools, such as:\n\n"); |
| 2425 | pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group, |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2426 | tev->event); |
| 2427 | } |
Masami Hiramatsu | a9b495b | 2009-12-08 17:02:47 -0500 | [diff] [blame] | 2428 | |
Masami Hiramatsu | 9aaf5a5 | 2015-02-19 23:31:13 +0900 | [diff] [blame] | 2429 | kprobe_blacklist__delete(&blacklist); |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 2430 | strlist__delete(namelist); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 2431 | close(fd); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2432 | return ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 2433 | } |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2434 | |
Namhyung Kim | 564c62a | 2015-01-14 20:18:07 +0900 | [diff] [blame] | 2435 | static int find_probe_functions(struct map *map, char *name) |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2436 | { |
Namhyung Kim | 564c62a | 2015-01-14 20:18:07 +0900 | [diff] [blame] | 2437 | int found = 0; |
Arnaldo Carvalho de Melo | 0a3873a | 2015-01-16 16:40:25 -0300 | [diff] [blame] | 2438 | struct symbol *sym; |
Namhyung Kim | 564c62a | 2015-01-14 20:18:07 +0900 | [diff] [blame] | 2439 | |
Arnaldo Carvalho de Melo | 0a3873a | 2015-01-16 16:40:25 -0300 | [diff] [blame] | 2440 | map__for_each_symbol_by_name(map, name, sym) { |
Namhyung Kim | e578da3 | 2015-03-06 16:31:29 +0900 | [diff] [blame] | 2441 | found++; |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2442 | } |
Namhyung Kim | 564c62a | 2015-01-14 20:18:07 +0900 | [diff] [blame] | 2443 | |
| 2444 | return found; |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | #define strdup_or_goto(str, label) \ |
| 2448 | ({ char *__p = strdup(str); if (!__p) goto label; __p; }) |
| 2449 | |
Naveen N. Rao | 7b6ff0b | 2015-04-28 17:35:40 +0530 | [diff] [blame^] | 2450 | void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused, |
| 2451 | struct probe_trace_event *tev __maybe_unused, |
| 2452 | struct map *map __maybe_unused) { } |
| 2453 | |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2454 | /* |
| 2455 | * Find probe function addresses from map. |
| 2456 | * Return an error or the number of found probe_trace_event |
| 2457 | */ |
| 2458 | static int find_probe_trace_events_from_map(struct perf_probe_event *pev, |
| 2459 | struct probe_trace_event **tevs, |
| 2460 | int max_tevs, const char *target) |
| 2461 | { |
| 2462 | struct map *map = NULL; |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2463 | struct ref_reloc_sym *reloc_sym = NULL; |
| 2464 | struct symbol *sym; |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2465 | struct probe_trace_event *tev; |
| 2466 | struct perf_probe_point *pp = &pev->point; |
| 2467 | struct probe_trace_point *tp; |
Namhyung Kim | 564c62a | 2015-01-14 20:18:07 +0900 | [diff] [blame] | 2468 | int num_matched_functions; |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2469 | int ret, i; |
| 2470 | |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 2471 | map = get_target_map(target, pev->uprobes); |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2472 | if (!map) { |
| 2473 | ret = -EINVAL; |
| 2474 | goto out; |
| 2475 | } |
| 2476 | |
| 2477 | /* |
| 2478 | * Load matched symbols: Since the different local symbols may have |
| 2479 | * same name but different addresses, this lists all the symbols. |
| 2480 | */ |
Namhyung Kim | 564c62a | 2015-01-14 20:18:07 +0900 | [diff] [blame] | 2481 | num_matched_functions = find_probe_functions(map, pp->function); |
| 2482 | if (num_matched_functions == 0) { |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2483 | pr_err("Failed to find symbol %s in %s\n", pp->function, |
| 2484 | target ? : "kernel"); |
| 2485 | ret = -ENOENT; |
| 2486 | goto out; |
| 2487 | } else if (num_matched_functions > max_tevs) { |
| 2488 | pr_err("Too many functions matched in %s\n", |
| 2489 | target ? : "kernel"); |
| 2490 | ret = -E2BIG; |
| 2491 | goto out; |
| 2492 | } |
| 2493 | |
Namhyung Kim | 25dd917 | 2015-01-14 20:18:08 +0900 | [diff] [blame] | 2494 | if (!pev->uprobes && !pp->retprobe) { |
He Kuang | 0560a0c | 2015-03-20 09:56:56 +0800 | [diff] [blame] | 2495 | reloc_sym = kernel_get_ref_reloc_sym(); |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2496 | if (!reloc_sym) { |
| 2497 | pr_warning("Relocated base symbol is not found!\n"); |
| 2498 | ret = -EINVAL; |
| 2499 | goto out; |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | /* Setup result trace-probe-events */ |
| 2504 | *tevs = zalloc(sizeof(*tev) * num_matched_functions); |
| 2505 | if (!*tevs) { |
| 2506 | ret = -ENOMEM; |
| 2507 | goto out; |
| 2508 | } |
| 2509 | |
| 2510 | ret = 0; |
Namhyung Kim | 564c62a | 2015-01-14 20:18:07 +0900 | [diff] [blame] | 2511 | |
Arnaldo Carvalho de Melo | 0a3873a | 2015-01-16 16:40:25 -0300 | [diff] [blame] | 2512 | map__for_each_symbol_by_name(map, pp->function, sym) { |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2513 | tev = (*tevs) + ret; |
| 2514 | tp = &tev->point; |
| 2515 | if (ret == num_matched_functions) { |
| 2516 | pr_warning("Too many symbols are listed. Skip it.\n"); |
| 2517 | break; |
| 2518 | } |
| 2519 | ret++; |
| 2520 | |
| 2521 | if (pp->offset > sym->end - sym->start) { |
| 2522 | pr_warning("Offset %ld is bigger than the size of %s\n", |
| 2523 | pp->offset, sym->name); |
| 2524 | ret = -ENOENT; |
| 2525 | goto err_out; |
| 2526 | } |
| 2527 | /* Add one probe point */ |
| 2528 | tp->address = map->unmap_ip(map, sym->start) + pp->offset; |
| 2529 | if (reloc_sym) { |
| 2530 | tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out); |
| 2531 | tp->offset = tp->address - reloc_sym->addr; |
| 2532 | } else { |
| 2533 | tp->symbol = strdup_or_goto(sym->name, nomem_out); |
| 2534 | tp->offset = pp->offset; |
| 2535 | } |
| 2536 | tp->retprobe = pp->retprobe; |
| 2537 | if (target) |
| 2538 | tev->point.module = strdup_or_goto(target, nomem_out); |
| 2539 | tev->uprobes = pev->uprobes; |
| 2540 | tev->nargs = pev->nargs; |
| 2541 | if (tev->nargs) { |
| 2542 | tev->args = zalloc(sizeof(struct probe_trace_arg) * |
| 2543 | tev->nargs); |
| 2544 | if (tev->args == NULL) |
| 2545 | goto nomem_out; |
| 2546 | } |
| 2547 | for (i = 0; i < tev->nargs; i++) { |
| 2548 | if (pev->args[i].name) |
| 2549 | tev->args[i].name = |
| 2550 | strdup_or_goto(pev->args[i].name, |
| 2551 | nomem_out); |
| 2552 | |
| 2553 | tev->args[i].value = strdup_or_goto(pev->args[i].var, |
| 2554 | nomem_out); |
| 2555 | if (pev->args[i].type) |
| 2556 | tev->args[i].type = |
| 2557 | strdup_or_goto(pev->args[i].type, |
| 2558 | nomem_out); |
| 2559 | } |
Naveen N. Rao | 7b6ff0b | 2015-04-28 17:35:40 +0530 | [diff] [blame^] | 2560 | arch__fix_tev_from_maps(pev, tev, map); |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | out: |
Masami Hiramatsu | 9b118ac | 2015-03-06 16:31:20 +0900 | [diff] [blame] | 2564 | put_target_map(map, pev->uprobes); |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2565 | return ret; |
| 2566 | |
| 2567 | nomem_out: |
| 2568 | ret = -ENOMEM; |
| 2569 | err_out: |
| 2570 | clear_probe_trace_events(*tevs, num_matched_functions); |
| 2571 | zfree(tevs); |
| 2572 | goto out; |
| 2573 | } |
| 2574 | |
Naveen N. Rao | d5c2e2c | 2015-04-28 17:35:39 +0530 | [diff] [blame] | 2575 | bool __weak arch__prefers_symtab(void) { return false; } |
| 2576 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2577 | static int convert_to_probe_trace_events(struct perf_probe_event *pev, |
| 2578 | struct probe_trace_event **tevs, |
Srikar Dronamraju | 4eced23 | 2012-02-02 19:50:40 +0530 | [diff] [blame] | 2579 | int max_tevs, const char *target) |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 2580 | { |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2581 | int ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2582 | |
Masami Hiramatsu | fb7345b | 2013-12-26 05:41:53 +0000 | [diff] [blame] | 2583 | if (pev->uprobes && !pev->group) { |
| 2584 | /* Replace group name if not given */ |
| 2585 | ret = convert_exec_to_group(target, &pev->group); |
| 2586 | if (ret != 0) { |
| 2587 | pr_warning("Failed to make a group name.\n"); |
| 2588 | return ret; |
| 2589 | } |
| 2590 | } |
| 2591 | |
Naveen N. Rao | d5c2e2c | 2015-04-28 17:35:39 +0530 | [diff] [blame] | 2592 | if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) { |
| 2593 | ret = find_probe_trace_events_from_map(pev, tevs, max_tevs, target); |
| 2594 | if (ret > 0) |
| 2595 | return ret; /* Found in symbol table */ |
| 2596 | } |
| 2597 | |
Masami Hiramatsu | 4b4da7f | 2010-03-22 13:10:26 -0300 | [diff] [blame] | 2598 | /* Convert perf_probe_event with debuginfo */ |
Srikar Dronamraju | 4eced23 | 2012-02-02 19:50:40 +0530 | [diff] [blame] | 2599 | ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target); |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 2600 | if (ret != 0) |
Masami Hiramatsu | 190b57f | 2011-06-27 16:27:45 +0900 | [diff] [blame] | 2601 | return ret; /* Found in debuginfo or got an error */ |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 2602 | |
Masami Hiramatsu | eb948e5 | 2014-02-06 05:32:25 +0000 | [diff] [blame] | 2603 | return find_probe_trace_events_from_map(pev, tevs, max_tevs, target); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | struct __event_package { |
| 2607 | struct perf_probe_event *pev; |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2608 | struct probe_trace_event *tevs; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2609 | int ntevs; |
| 2610 | }; |
| 2611 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2612 | int add_perf_probe_events(struct perf_probe_event *pevs, int npevs, |
Masami Hiramatsu | 7afb3fa | 2015-04-01 19:25:39 +0900 | [diff] [blame] | 2613 | int max_tevs, bool force_add) |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2614 | { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2615 | int i, j, ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2616 | struct __event_package *pkgs; |
| 2617 | |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2618 | ret = 0; |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 2619 | pkgs = zalloc(sizeof(struct __event_package) * npevs); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2620 | |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 2621 | if (pkgs == NULL) |
| 2622 | return -ENOMEM; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2623 | |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 2624 | ret = init_symbol_maps(pevs->uprobes); |
Masami Hiramatsu | 449e5b2 | 2010-08-03 11:11:40 +0900 | [diff] [blame] | 2625 | if (ret < 0) { |
| 2626 | free(pkgs); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2627 | return ret; |
Masami Hiramatsu | 449e5b2 | 2010-08-03 11:11:40 +0900 | [diff] [blame] | 2628 | } |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2629 | |
| 2630 | /* Loop 1: convert all events */ |
| 2631 | for (i = 0; i < npevs; i++) { |
| 2632 | pkgs[i].pev = &pevs[i]; |
| 2633 | /* Convert with or without debuginfo */ |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2634 | ret = convert_to_probe_trace_events(pkgs[i].pev, |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 2635 | &pkgs[i].tevs, |
| 2636 | max_tevs, |
Masami Hiramatsu | 7afb3fa | 2015-04-01 19:25:39 +0900 | [diff] [blame] | 2637 | pkgs[i].pev->target); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2638 | if (ret < 0) |
| 2639 | goto end; |
| 2640 | pkgs[i].ntevs = ret; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | /* Loop 2: add all events */ |
Arnaldo Carvalho de Melo | 8635bf6 | 2011-02-22 06:56:18 -0300 | [diff] [blame] | 2644 | for (i = 0; i < npevs; i++) { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2645 | ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs, |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2646 | pkgs[i].ntevs, force_add); |
Arnaldo Carvalho de Melo | fbee632 | 2011-02-21 13:23:57 -0300 | [diff] [blame] | 2647 | if (ret < 0) |
| 2648 | break; |
| 2649 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2650 | end: |
Masami Hiramatsu | 449e5b2 | 2010-08-03 11:11:40 +0900 | [diff] [blame] | 2651 | /* Loop 3: cleanup and free trace events */ |
| 2652 | for (i = 0; i < npevs; i++) { |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2653 | for (j = 0; j < pkgs[i].ntevs; j++) |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2654 | clear_probe_trace_event(&pkgs[i].tevs[j]); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 2655 | zfree(&pkgs[i].tevs); |
Masami Hiramatsu | 449e5b2 | 2010-08-03 11:11:40 +0900 | [diff] [blame] | 2656 | } |
| 2657 | free(pkgs); |
Masami Hiramatsu | ee45b6c | 2014-02-06 05:32:04 +0000 | [diff] [blame] | 2658 | exit_symbol_maps(); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2659 | |
| 2660 | return ret; |
Masami Hiramatsu | e0faa8d | 2010-03-16 18:05:37 -0400 | [diff] [blame] | 2661 | } |
| 2662 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2663 | static int __del_trace_probe_event(int fd, struct str_node *ent) |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 2664 | { |
| 2665 | char *p; |
| 2666 | char buf[128]; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2667 | int ret; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 2668 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2669 | /* Convert from perf-probe event to trace-probe event */ |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2670 | ret = e_snprintf(buf, 128, "-:%s", ent->s); |
| 2671 | if (ret < 0) |
| 2672 | goto error; |
| 2673 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 2674 | p = strchr(buf + 2, ':'); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2675 | if (!p) { |
| 2676 | pr_debug("Internal error: %s should have ':' but not.\n", |
| 2677 | ent->s); |
| 2678 | ret = -ENOTSUP; |
| 2679 | goto error; |
| 2680 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 2681 | *p = '/'; |
| 2682 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 2683 | pr_debug("Writing event: %s\n", buf); |
| 2684 | ret = write(fd, buf, strlen(buf)); |
Masami Hiramatsu | 44a5604 | 2011-10-04 19:45:04 +0900 | [diff] [blame] | 2685 | if (ret < 0) { |
| 2686 | ret = -errno; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2687 | goto error; |
Masami Hiramatsu | 44a5604 | 2011-10-04 19:45:04 +0900 | [diff] [blame] | 2688 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2689 | |
Masami Hiramatsu | 5e17b28 | 2014-10-27 16:31:31 -0400 | [diff] [blame] | 2690 | pr_info("Removed event: %s\n", ent->s); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2691 | return 0; |
| 2692 | error: |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 2693 | pr_warning("Failed to delete event: %s\n", |
| 2694 | strerror_r(-ret, buf, sizeof(buf))); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2695 | return ret; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 2696 | } |
| 2697 | |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2698 | static int del_trace_probe_event(int fd, const char *buf, |
| 2699 | struct strlist *namelist) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2700 | { |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 2701 | struct str_node *ent, *n; |
Masami Hiramatsu | 6dbe31f | 2015-04-23 22:46:14 +0900 | [diff] [blame] | 2702 | int ret = -ENOENT; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2703 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 2704 | if (strpbrk(buf, "*?")) { /* Glob-exp */ |
| 2705 | strlist__for_each_safe(ent, n, namelist) |
| 2706 | if (strglobmatch(ent->s, buf)) { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2707 | ret = __del_trace_probe_event(fd, ent); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2708 | if (ret < 0) |
| 2709 | break; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 2710 | strlist__remove(namelist, ent); |
| 2711 | } |
| 2712 | } else { |
| 2713 | ent = strlist__find(namelist, buf); |
| 2714 | if (ent) { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 2715 | ret = __del_trace_probe_event(fd, ent); |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2716 | if (ret >= 0) |
| 2717 | strlist__remove(namelist, ent); |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 2718 | } |
| 2719 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2720 | |
| 2721 | return ret; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2722 | } |
| 2723 | |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2724 | int del_perf_probe_events(struct strlist *dellist) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2725 | { |
Masami Hiramatsu | 6dbe31f | 2015-04-23 22:46:14 +0900 | [diff] [blame] | 2726 | int ret = -1, ret2, ufd = -1, kfd = -1; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2727 | char buf[128]; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2728 | const char *group, *event; |
| 2729 | char *p, *str; |
| 2730 | struct str_node *ent; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2731 | struct strlist *namelist = NULL, *unamelist = NULL; |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2732 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2733 | /* Get current event names */ |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2734 | kfd = open_kprobe_events(true); |
Masami Hiramatsu | 467ec08 | 2014-08-13 16:12:50 +0000 | [diff] [blame] | 2735 | if (kfd >= 0) |
| 2736 | namelist = get_probe_trace_event_names(kfd, true); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2737 | |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2738 | ufd = open_uprobe_events(true); |
Masami Hiramatsu | 467ec08 | 2014-08-13 16:12:50 +0000 | [diff] [blame] | 2739 | if (ufd >= 0) |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2740 | unamelist = get_probe_trace_event_names(ufd, true); |
| 2741 | |
Masami Hiramatsu | 467ec08 | 2014-08-13 16:12:50 +0000 | [diff] [blame] | 2742 | if (kfd < 0 && ufd < 0) { |
| 2743 | print_both_open_warning(kfd, ufd); |
| 2744 | goto error; |
| 2745 | } |
| 2746 | |
Masami Hiramatsu | 6dbe31f | 2015-04-23 22:46:14 +0900 | [diff] [blame] | 2747 | if (namelist == NULL && unamelist == NULL) { |
| 2748 | ret = -ENOENT; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2749 | goto error; |
Masami Hiramatsu | 6dbe31f | 2015-04-23 22:46:14 +0900 | [diff] [blame] | 2750 | } |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2751 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 2752 | strlist__for_each(ent, dellist) { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 2753 | str = strdup(ent->s); |
| 2754 | if (str == NULL) { |
| 2755 | ret = -ENOMEM; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2756 | goto error; |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 2757 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 2758 | pr_debug("Parsing: %s\n", str); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2759 | p = strchr(str, ':'); |
| 2760 | if (p) { |
| 2761 | group = str; |
| 2762 | *p = '\0'; |
| 2763 | event = p + 1; |
| 2764 | } else { |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 2765 | group = "*"; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2766 | event = str; |
| 2767 | } |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2768 | |
| 2769 | ret = e_snprintf(buf, 128, "%s:%s", group, event); |
| 2770 | if (ret < 0) { |
| 2771 | pr_err("Failed to copy event."); |
| 2772 | free(str); |
| 2773 | goto error; |
| 2774 | } |
| 2775 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 2776 | pr_debug("Group: %s, Event: %s\n", group, event); |
Masami Hiramatsu | 6dbe31f | 2015-04-23 22:46:14 +0900 | [diff] [blame] | 2777 | free(str); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2778 | |
Masami Hiramatsu | 6dbe31f | 2015-04-23 22:46:14 +0900 | [diff] [blame] | 2779 | ret = ret2 = -ENOENT; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2780 | if (namelist) |
| 2781 | ret = del_trace_probe_event(kfd, buf, namelist); |
| 2782 | |
Masami Hiramatsu | 6dbe31f | 2015-04-23 22:46:14 +0900 | [diff] [blame] | 2783 | if ((ret >= 0 || ret == -ENOENT) && unamelist) |
| 2784 | ret2 = del_trace_probe_event(ufd, buf, unamelist); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2785 | |
Masami Hiramatsu | 6dbe31f | 2015-04-23 22:46:14 +0900 | [diff] [blame] | 2786 | /* Since we can remove probes which already removed, don't check it */ |
| 2787 | if (ret == -ENOENT && ret2 == -ENOENT) |
| 2788 | pr_debug("Event \"%s\" does not exist.\n", buf); |
| 2789 | else if (ret < 0 || ret2 < 0) { |
| 2790 | if (ret >= 0) |
| 2791 | ret = ret2; |
| 2792 | break; |
| 2793 | } |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2794 | } |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2795 | |
| 2796 | error: |
| 2797 | if (kfd >= 0) { |
Srikar Dronamraju | a23c4dc | 2012-05-31 17:16:43 +0530 | [diff] [blame] | 2798 | strlist__delete(namelist); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2799 | close(kfd); |
| 2800 | } |
| 2801 | |
| 2802 | if (ufd >= 0) { |
Srikar Dronamraju | a23c4dc | 2012-05-31 17:16:43 +0530 | [diff] [blame] | 2803 | strlist__delete(unamelist); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2804 | close(ufd); |
| 2805 | } |
Masami Hiramatsu | 146a143 | 2010-04-12 13:17:42 -0400 | [diff] [blame] | 2806 | |
| 2807 | return ret; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2808 | } |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2809 | |
Masami Hiramatsu | 3c42258 | 2011-01-20 23:15:45 +0900 | [diff] [blame] | 2810 | /* TODO: don't use a global variable for filter ... */ |
| 2811 | static struct strfilter *available_func_filter; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 2812 | |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 2813 | /* |
Masami Hiramatsu | 3c42258 | 2011-01-20 23:15:45 +0900 | [diff] [blame] | 2814 | * If a symbol corresponds to a function with global binding and |
| 2815 | * matches filter return 0. For all others return 1. |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 2816 | */ |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 2817 | static int filter_available_functions(struct map *map __maybe_unused, |
Masami Hiramatsu | 3c42258 | 2011-01-20 23:15:45 +0900 | [diff] [blame] | 2818 | struct symbol *sym) |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 2819 | { |
Namhyung Kim | e578da3 | 2015-03-06 16:31:29 +0900 | [diff] [blame] | 2820 | if (strfilter__compare(available_func_filter, sym->name)) |
Masami Hiramatsu | 3c42258 | 2011-01-20 23:15:45 +0900 | [diff] [blame] | 2821 | return 0; |
| 2822 | return 1; |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 2823 | } |
| 2824 | |
Masami Hiramatsu | 2df5863 | 2014-02-06 05:32:11 +0000 | [diff] [blame] | 2825 | int show_available_funcs(const char *target, struct strfilter *_filter, |
| 2826 | bool user) |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 2827 | { |
Masami Hiramatsu | 2df5863 | 2014-02-06 05:32:11 +0000 | [diff] [blame] | 2828 | struct map *map; |
| 2829 | int ret; |
| 2830 | |
| 2831 | ret = init_symbol_maps(user); |
| 2832 | if (ret < 0) |
| 2833 | return ret; |
| 2834 | |
| 2835 | /* Get a symbol map */ |
| 2836 | if (user) |
| 2837 | map = dso__new_map(target); |
| 2838 | else |
| 2839 | map = kernel_get_module_map(target); |
| 2840 | if (!map) { |
| 2841 | pr_err("Failed to get a map for %s\n", (target) ? : "kernel"); |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 2842 | return -EINVAL; |
| 2843 | } |
Masami Hiramatsu | 2df5863 | 2014-02-06 05:32:11 +0000 | [diff] [blame] | 2844 | |
| 2845 | /* Load symbols with given filter */ |
| 2846 | available_func_filter = _filter; |
| 2847 | if (map__load(map, filter_available_functions)) { |
| 2848 | pr_err("Failed to load symbols in %s\n", (target) ? : "kernel"); |
| 2849 | goto end; |
| 2850 | } |
Masami Hiramatsu | e80711c | 2011-01-13 21:46:11 +0900 | [diff] [blame] | 2851 | if (!dso__sorted_by_name(map->dso, map->type)) |
| 2852 | dso__sort_by_name(map->dso, map->type); |
| 2853 | |
Masami Hiramatsu | 2df5863 | 2014-02-06 05:32:11 +0000 | [diff] [blame] | 2854 | /* Show all (filtered) symbols */ |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2855 | setup_pager(); |
Masami Hiramatsu | 2df5863 | 2014-02-06 05:32:11 +0000 | [diff] [blame] | 2856 | dso__fprintf_symbols_by_name(map->dso, map->type, stdout); |
| 2857 | end: |
| 2858 | if (user) { |
| 2859 | dso__delete(map->dso); |
| 2860 | map__delete(map); |
| 2861 | } |
| 2862 | exit_symbol_maps(); |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2863 | |
Masami Hiramatsu | 2df5863 | 2014-02-06 05:32:11 +0000 | [diff] [blame] | 2864 | return ret; |
Srikar Dronamraju | 225466f | 2012-04-16 17:39:09 +0530 | [diff] [blame] | 2865 | } |
| 2866 | |