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