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