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