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