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