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