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