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