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