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