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