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