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