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