Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * probe-finder.c : C expression to kprobe event converter |
| 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 | |
| 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 <getopt.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <stdarg.h> |
| 33 | #include <ctype.h> |
Ian Munsie | cd932c5 | 2010-04-20 16:58:32 +1000 | [diff] [blame] | 34 | #include <dwarf-regs.h> |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 35 | |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 36 | #include <linux/bitops.h> |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 37 | #include "event.h" |
| 38 | #include "debug.h" |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 39 | #include "util.h" |
Chase Douglas | 9ed7e1b | 2010-06-14 15:26:30 -0400 | [diff] [blame] | 40 | #include "symbol.h" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 41 | #include "probe-finder.h" |
| 42 | |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 43 | /* Kprobe tracer basic type is up to u64 */ |
| 44 | #define MAX_BASIC_TYPE_BITS 64 |
| 45 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 46 | /* |
| 47 | * Compare the tail of two strings. |
| 48 | * Return 0 if whole of either string is same as another's tail part. |
| 49 | */ |
| 50 | static int strtailcmp(const char *s1, const char *s2) |
| 51 | { |
| 52 | int i1 = strlen(s1); |
| 53 | int i2 = strlen(s2); |
Juha Leppanen | d56728b | 2009-12-07 12:00:40 -0500 | [diff] [blame] | 54 | while (--i1 >= 0 && --i2 >= 0) { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 55 | if (s1[i1] != s2[i2]) |
| 56 | return s1[i1] - s2[i2]; |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 61 | /* Line number list operations */ |
| 62 | |
| 63 | /* Add a line to line number list */ |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 64 | static int line_list__add_line(struct list_head *head, int line) |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 65 | { |
| 66 | struct line_node *ln; |
| 67 | struct list_head *p; |
| 68 | |
| 69 | /* Reverse search, because new line will be the last one */ |
| 70 | list_for_each_entry_reverse(ln, head, list) { |
| 71 | if (ln->line < line) { |
| 72 | p = &ln->list; |
| 73 | goto found; |
| 74 | } else if (ln->line == line) /* Already exist */ |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 75 | return 1; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 76 | } |
| 77 | /* List is empty, or the smallest entry */ |
| 78 | p = head; |
| 79 | found: |
| 80 | pr_debug("line list: add a line %u\n", line); |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 81 | ln = zalloc(sizeof(struct line_node)); |
| 82 | if (ln == NULL) |
| 83 | return -ENOMEM; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 84 | ln->line = line; |
| 85 | INIT_LIST_HEAD(&ln->list); |
| 86 | list_add(&ln->list, p); |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 87 | return 0; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* Check if the line in line number list */ |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 91 | static int line_list__has_line(struct list_head *head, int line) |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 92 | { |
| 93 | struct line_node *ln; |
| 94 | |
| 95 | /* Reverse search, because new line will be the last one */ |
| 96 | list_for_each_entry(ln, head, list) |
| 97 | if (ln->line == line) |
| 98 | return 1; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* Init line number list */ |
| 104 | static void line_list__init(struct list_head *head) |
| 105 | { |
| 106 | INIT_LIST_HEAD(head); |
| 107 | } |
| 108 | |
| 109 | /* Free line number list */ |
| 110 | static void line_list__free(struct list_head *head) |
| 111 | { |
| 112 | struct line_node *ln; |
| 113 | while (!list_empty(head)) { |
| 114 | ln = list_first_entry(head, struct line_node, list); |
| 115 | list_del(&ln->list); |
| 116 | free(ln); |
| 117 | } |
| 118 | } |
| 119 | |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 120 | /* Dwarf FL wrappers */ |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 121 | static char *debuginfo_path; /* Currently dummy */ |
| 122 | |
| 123 | static const Dwfl_Callbacks offline_callbacks = { |
| 124 | .find_debuginfo = dwfl_standard_find_debuginfo, |
| 125 | .debuginfo_path = &debuginfo_path, |
| 126 | |
| 127 | .section_address = dwfl_offline_section_address, |
| 128 | |
| 129 | /* We use this table for core files too. */ |
| 130 | .find_elf = dwfl_build_id_find_elf, |
| 131 | }; |
| 132 | |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 133 | /* Get a Dwarf from offline image */ |
| 134 | static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias) |
| 135 | { |
| 136 | Dwfl_Module *mod; |
| 137 | Dwarf *dbg = NULL; |
| 138 | |
| 139 | if (!dwflp) |
| 140 | return NULL; |
| 141 | |
| 142 | *dwflp = dwfl_begin(&offline_callbacks); |
| 143 | if (!*dwflp) |
| 144 | return NULL; |
| 145 | |
| 146 | mod = dwfl_report_offline(*dwflp, "", "", fd); |
| 147 | if (!mod) |
| 148 | goto error; |
| 149 | |
| 150 | dbg = dwfl_module_getdwarf(mod, bias); |
| 151 | if (!dbg) { |
| 152 | error: |
| 153 | dwfl_end(*dwflp); |
| 154 | *dwflp = NULL; |
| 155 | } |
| 156 | return dbg; |
| 157 | } |
| 158 | |
Masami Hiramatsu | 3b4694d | 2010-12-17 22:12:18 +0900 | [diff] [blame] | 159 | #if _ELFUTILS_PREREQ(0, 148) |
| 160 | /* This method is buggy if elfutils is older than 0.148 */ |
| 161 | static int __linux_kernel_find_elf(Dwfl_Module *mod, |
| 162 | void **userdata, |
| 163 | const char *module_name, |
| 164 | Dwarf_Addr base, |
| 165 | char **file_name, Elf **elfp) |
| 166 | { |
| 167 | int fd; |
| 168 | const char *path = kernel_get_module_path(module_name); |
| 169 | |
| 170 | pr_debug2("Use file %s for %s\n", path, module_name); |
| 171 | if (path) { |
| 172 | fd = open(path, O_RDONLY); |
| 173 | if (fd >= 0) { |
| 174 | *file_name = strdup(path); |
| 175 | return fd; |
| 176 | } |
| 177 | } |
| 178 | /* If failed, try to call standard method */ |
| 179 | return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base, |
| 180 | file_name, elfp); |
| 181 | } |
| 182 | |
| 183 | static const Dwfl_Callbacks kernel_callbacks = { |
| 184 | .find_debuginfo = dwfl_standard_find_debuginfo, |
| 185 | .debuginfo_path = &debuginfo_path, |
| 186 | |
| 187 | .find_elf = __linux_kernel_find_elf, |
| 188 | .section_address = dwfl_linux_kernel_module_section_address, |
| 189 | }; |
| 190 | |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 191 | /* Get a Dwarf from live kernel image */ |
| 192 | static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp, |
| 193 | Dwarf_Addr *bias) |
| 194 | { |
| 195 | Dwarf *dbg; |
| 196 | |
| 197 | if (!dwflp) |
| 198 | return NULL; |
| 199 | |
| 200 | *dwflp = dwfl_begin(&kernel_callbacks); |
| 201 | if (!*dwflp) |
| 202 | return NULL; |
| 203 | |
| 204 | /* Load the kernel dwarves: Don't care the result here */ |
| 205 | dwfl_linux_kernel_report_kernel(*dwflp); |
| 206 | dwfl_linux_kernel_report_modules(*dwflp); |
| 207 | |
| 208 | dbg = dwfl_addrdwarf(*dwflp, addr, bias); |
| 209 | /* Here, check whether we could get a real dwarf */ |
| 210 | if (!dbg) { |
Masami Hiramatsu | 3b4694d | 2010-12-17 22:12:18 +0900 | [diff] [blame] | 211 | pr_debug("Failed to find kernel dwarf at %lx\n", |
| 212 | (unsigned long)addr); |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 213 | dwfl_end(*dwflp); |
| 214 | *dwflp = NULL; |
| 215 | } |
| 216 | return dbg; |
| 217 | } |
Masami Hiramatsu | 3b4694d | 2010-12-17 22:12:18 +0900 | [diff] [blame] | 218 | #else |
| 219 | /* With older elfutils, this just support kernel module... */ |
| 220 | static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr __used, Dwfl **dwflp, |
| 221 | Dwarf_Addr *bias) |
| 222 | { |
| 223 | int fd; |
| 224 | const char *path = kernel_get_module_path("kernel"); |
| 225 | |
| 226 | if (!path) { |
| 227 | pr_err("Failed to find vmlinux path\n"); |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | pr_debug2("Use file %s for debuginfo\n", path); |
| 232 | fd = open(path, O_RDONLY); |
| 233 | if (fd < 0) |
| 234 | return NULL; |
| 235 | |
| 236 | return dwfl_init_offline_dwarf(fd, dwflp, bias); |
| 237 | } |
| 238 | #endif |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 239 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 240 | /* Dwarf wrappers */ |
| 241 | |
| 242 | /* Find the realpath of the target file. */ |
| 243 | static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 244 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 245 | Dwarf_Files *files; |
| 246 | size_t nfiles, i; |
Arnaldo Carvalho de Melo | accd3cc | 2010-03-05 12:51:04 -0300 | [diff] [blame] | 247 | const char *src = NULL; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 248 | int ret; |
| 249 | |
| 250 | if (!fname) |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 251 | return NULL; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 252 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 253 | ret = dwarf_getsrcfiles(cu_die, &files, &nfiles); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 254 | if (ret != 0) |
| 255 | return NULL; |
| 256 | |
| 257 | for (i = 0; i < nfiles; i++) { |
| 258 | src = dwarf_filesrc(files, i, NULL, NULL); |
| 259 | if (strtailcmp(src, fname) == 0) |
| 260 | break; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 261 | } |
Masami Hiramatsu | c9e3858 | 2010-04-02 12:50:45 -0400 | [diff] [blame] | 262 | if (i == nfiles) |
| 263 | return NULL; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 264 | return src; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 265 | } |
| 266 | |
Masami Hiramatsu | 6a330a3 | 2010-07-09 18:29:11 +0900 | [diff] [blame] | 267 | /* Get DW_AT_comp_dir (should be NULL with older gcc) */ |
| 268 | static const char *cu_get_comp_dir(Dwarf_Die *cu_die) |
| 269 | { |
| 270 | Dwarf_Attribute attr; |
| 271 | if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL) |
| 272 | return NULL; |
| 273 | return dwarf_formstring(&attr); |
| 274 | } |
| 275 | |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 276 | /* Compare diename and tname */ |
| 277 | static bool die_compare_name(Dwarf_Die *dw_die, const char *tname) |
| 278 | { |
| 279 | const char *name; |
| 280 | name = dwarf_diename(dw_die); |
Masami Hiramatsu | 8217563 | 2010-07-09 18:29:17 +0900 | [diff] [blame] | 281 | return name ? (strcmp(tname, name) == 0) : false; |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 282 | } |
| 283 | |
Masami Hiramatsu | 5069ed8 | 2011-01-13 21:46:05 +0900 | [diff] [blame] | 284 | /* Get callsite line number of inline-function instance */ |
| 285 | static int die_get_call_lineno(Dwarf_Die *in_die) |
| 286 | { |
| 287 | Dwarf_Attribute attr; |
| 288 | Dwarf_Word ret; |
| 289 | |
| 290 | if (!dwarf_attr(in_die, DW_AT_call_line, &attr)) |
| 291 | return -ENOENT; |
| 292 | |
| 293 | dwarf_formudata(&attr, &ret); |
| 294 | return (int)ret; |
| 295 | } |
| 296 | |
Masami Hiramatsu | 4046b8b | 2010-10-21 19:13:02 +0900 | [diff] [blame] | 297 | /* Get type die */ |
| 298 | static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) |
| 299 | { |
| 300 | Dwarf_Attribute attr; |
| 301 | |
| 302 | if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) && |
| 303 | dwarf_formref_die(&attr, die_mem)) |
| 304 | return die_mem; |
| 305 | else |
| 306 | return NULL; |
| 307 | } |
| 308 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 309 | /* Get a type die, but skip qualifiers */ |
| 310 | static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 311 | { |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 312 | int tag; |
| 313 | |
| 314 | do { |
Masami Hiramatsu | 4046b8b | 2010-10-21 19:13:02 +0900 | [diff] [blame] | 315 | vr_die = die_get_type(vr_die, die_mem); |
| 316 | if (!vr_die) |
| 317 | break; |
| 318 | tag = dwarf_tag(vr_die); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 319 | } while (tag == DW_TAG_const_type || |
| 320 | tag == DW_TAG_restrict_type || |
| 321 | tag == DW_TAG_volatile_type || |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 322 | tag == DW_TAG_shared_type); |
| 323 | |
| 324 | return vr_die; |
| 325 | } |
| 326 | |
| 327 | /* Get a type die, but skip qualifiers and typedef */ |
| 328 | static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) |
| 329 | { |
| 330 | do { |
| 331 | vr_die = __die_get_real_type(vr_die, die_mem); |
| 332 | } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 333 | |
Masami Hiramatsu | 4046b8b | 2010-10-21 19:13:02 +0900 | [diff] [blame] | 334 | return vr_die; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 335 | } |
| 336 | |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 337 | static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name, |
| 338 | Dwarf_Word *result) |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 339 | { |
| 340 | Dwarf_Attribute attr; |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 341 | |
| 342 | if (dwarf_attr(tp_die, attr_name, &attr) == NULL || |
| 343 | dwarf_formudata(&attr, result) != 0) |
| 344 | return -ENOENT; |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static bool die_is_signed_type(Dwarf_Die *tp_die) |
| 350 | { |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 351 | Dwarf_Word ret; |
| 352 | |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 353 | if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret)) |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 354 | return false; |
| 355 | |
| 356 | return (ret == DW_ATE_signed_char || ret == DW_ATE_signed || |
| 357 | ret == DW_ATE_signed_fixed); |
| 358 | } |
| 359 | |
| 360 | static int die_get_byte_size(Dwarf_Die *tp_die) |
| 361 | { |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 362 | Dwarf_Word ret; |
| 363 | |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 364 | if (die_get_attr_udata(tp_die, DW_AT_byte_size, &ret)) |
| 365 | return 0; |
| 366 | |
| 367 | return (int)ret; |
| 368 | } |
| 369 | |
| 370 | static int die_get_bit_size(Dwarf_Die *tp_die) |
| 371 | { |
| 372 | Dwarf_Word ret; |
| 373 | |
| 374 | if (die_get_attr_udata(tp_die, DW_AT_bit_size, &ret)) |
| 375 | return 0; |
| 376 | |
| 377 | return (int)ret; |
| 378 | } |
| 379 | |
| 380 | static int die_get_bit_offset(Dwarf_Die *tp_die) |
| 381 | { |
| 382 | Dwarf_Word ret; |
| 383 | |
| 384 | if (die_get_attr_udata(tp_die, DW_AT_bit_offset, &ret)) |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 385 | return 0; |
| 386 | |
| 387 | return (int)ret; |
| 388 | } |
| 389 | |
Masami Hiramatsu | de1439d | 2010-04-14 17:44:00 -0300 | [diff] [blame] | 390 | /* Get data_member_location offset */ |
| 391 | static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs) |
| 392 | { |
| 393 | Dwarf_Attribute attr; |
| 394 | Dwarf_Op *expr; |
| 395 | size_t nexpr; |
| 396 | int ret; |
| 397 | |
| 398 | if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL) |
| 399 | return -ENOENT; |
| 400 | |
| 401 | if (dwarf_formudata(&attr, offs) != 0) { |
| 402 | /* DW_AT_data_member_location should be DW_OP_plus_uconst */ |
| 403 | ret = dwarf_getlocation(&attr, &expr, &nexpr); |
| 404 | if (ret < 0 || nexpr == 0) |
| 405 | return -ENOENT; |
| 406 | |
| 407 | if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) { |
| 408 | pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n", |
| 409 | expr[0].atom, nexpr); |
| 410 | return -ENOTSUP; |
| 411 | } |
| 412 | *offs = (Dwarf_Word)expr[0].number; |
| 413 | } |
| 414 | return 0; |
| 415 | } |
| 416 | |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 417 | /* Return values for die_find callbacks */ |
| 418 | enum { |
| 419 | DIE_FIND_CB_FOUND = 0, /* End of Search */ |
| 420 | DIE_FIND_CB_CHILD = 1, /* Search only children */ |
| 421 | DIE_FIND_CB_SIBLING = 2, /* Search only siblings */ |
| 422 | DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */ |
| 423 | }; |
| 424 | |
| 425 | /* Search a child die */ |
| 426 | static Dwarf_Die *die_find_child(Dwarf_Die *rt_die, |
| 427 | int (*callback)(Dwarf_Die *, void *), |
| 428 | void *data, Dwarf_Die *die_mem) |
| 429 | { |
| 430 | Dwarf_Die child_die; |
| 431 | int ret; |
| 432 | |
| 433 | ret = dwarf_child(rt_die, die_mem); |
| 434 | if (ret != 0) |
| 435 | return NULL; |
| 436 | |
| 437 | do { |
| 438 | ret = callback(die_mem, data); |
| 439 | if (ret == DIE_FIND_CB_FOUND) |
| 440 | return die_mem; |
| 441 | |
| 442 | if ((ret & DIE_FIND_CB_CHILD) && |
| 443 | die_find_child(die_mem, callback, data, &child_die)) { |
| 444 | memcpy(die_mem, &child_die, sizeof(Dwarf_Die)); |
| 445 | return die_mem; |
| 446 | } |
| 447 | } while ((ret & DIE_FIND_CB_SIBLING) && |
| 448 | dwarf_siblingof(die_mem, die_mem) == 0); |
| 449 | |
| 450 | return NULL; |
| 451 | } |
| 452 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 453 | struct __addr_die_search_param { |
| 454 | Dwarf_Addr addr; |
| 455 | Dwarf_Die *die_mem; |
| 456 | }; |
| 457 | |
| 458 | static int __die_search_func_cb(Dwarf_Die *fn_die, void *data) |
| 459 | { |
| 460 | struct __addr_die_search_param *ad = data; |
| 461 | |
| 462 | if (dwarf_tag(fn_die) == DW_TAG_subprogram && |
| 463 | dwarf_haspc(fn_die, ad->addr)) { |
| 464 | memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die)); |
| 465 | return DWARF_CB_ABORT; |
| 466 | } |
| 467 | return DWARF_CB_OK; |
| 468 | } |
| 469 | |
| 470 | /* Search a real subprogram including this line, */ |
Masami Hiramatsu | 95a3e4c | 2010-03-16 18:05:51 -0400 | [diff] [blame] | 471 | static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr, |
| 472 | Dwarf_Die *die_mem) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 473 | { |
| 474 | struct __addr_die_search_param ad; |
| 475 | ad.addr = addr; |
| 476 | ad.die_mem = die_mem; |
| 477 | /* dwarf_getscopes can't find subprogram. */ |
| 478 | if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0)) |
| 479 | return NULL; |
| 480 | else |
| 481 | return die_mem; |
| 482 | } |
| 483 | |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 484 | /* die_find callback for inline function search */ |
| 485 | static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data) |
| 486 | { |
| 487 | Dwarf_Addr *addr = data; |
| 488 | |
| 489 | if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine && |
| 490 | dwarf_haspc(die_mem, *addr)) |
| 491 | return DIE_FIND_CB_FOUND; |
| 492 | |
| 493 | return DIE_FIND_CB_CONTINUE; |
| 494 | } |
| 495 | |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 496 | /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */ |
Masami Hiramatsu | 95a3e4c | 2010-03-16 18:05:51 -0400 | [diff] [blame] | 497 | static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr, |
| 498 | Dwarf_Die *die_mem) |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 499 | { |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 500 | return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem); |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 501 | } |
| 502 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 503 | /* Walker on lines (Note: line number will not be sorted) */ |
| 504 | typedef int (* line_walk_handler_t) (const char *fname, int lineno, |
| 505 | Dwarf_Addr addr, void *data); |
| 506 | |
| 507 | struct __line_walk_param { |
Masami Hiramatsu | 5069ed8 | 2011-01-13 21:46:05 +0900 | [diff] [blame] | 508 | const char *fname; |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 509 | line_walk_handler_t handler; |
| 510 | void *data; |
| 511 | int retval; |
| 512 | }; |
| 513 | |
Masami Hiramatsu | 5069ed8 | 2011-01-13 21:46:05 +0900 | [diff] [blame] | 514 | static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data) |
| 515 | { |
| 516 | struct __line_walk_param *lw = data; |
| 517 | Dwarf_Addr addr; |
| 518 | int lineno; |
| 519 | |
| 520 | if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) { |
| 521 | lineno = die_get_call_lineno(in_die); |
| 522 | if (lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) { |
| 523 | lw->retval = lw->handler(lw->fname, lineno, addr, |
| 524 | lw->data); |
| 525 | if (lw->retval != 0) |
| 526 | return DIE_FIND_CB_FOUND; |
| 527 | } |
| 528 | } |
| 529 | return DIE_FIND_CB_SIBLING; |
| 530 | } |
| 531 | |
| 532 | /* Walk on lines of blocks included in given DIE */ |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 533 | static int __die_walk_funclines(Dwarf_Die *sp_die, |
| 534 | line_walk_handler_t handler, void *data) |
| 535 | { |
Masami Hiramatsu | 5069ed8 | 2011-01-13 21:46:05 +0900 | [diff] [blame] | 536 | struct __line_walk_param lw = { |
| 537 | .handler = handler, |
| 538 | .data = data, |
| 539 | .retval = 0, |
| 540 | }; |
| 541 | Dwarf_Die die_mem; |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 542 | Dwarf_Addr addr; |
Masami Hiramatsu | 5069ed8 | 2011-01-13 21:46:05 +0900 | [diff] [blame] | 543 | int lineno; |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 544 | |
| 545 | /* Handle function declaration line */ |
Masami Hiramatsu | 5069ed8 | 2011-01-13 21:46:05 +0900 | [diff] [blame] | 546 | lw.fname = dwarf_decl_file(sp_die); |
| 547 | if (lw.fname && dwarf_decl_line(sp_die, &lineno) == 0 && |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 548 | dwarf_entrypc(sp_die, &addr) == 0) { |
Masami Hiramatsu | 5069ed8 | 2011-01-13 21:46:05 +0900 | [diff] [blame] | 549 | lw.retval = handler(lw.fname, lineno, addr, data); |
| 550 | if (lw.retval != 0) |
| 551 | goto done; |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 552 | } |
Masami Hiramatsu | 5069ed8 | 2011-01-13 21:46:05 +0900 | [diff] [blame] | 553 | die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem); |
| 554 | done: |
| 555 | return lw.retval; |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data) |
| 559 | { |
| 560 | struct __line_walk_param *lw = data; |
| 561 | |
| 562 | lw->retval = __die_walk_funclines(sp_die, lw->handler, lw->data); |
| 563 | if (lw->retval != 0) |
| 564 | return DWARF_CB_ABORT; |
| 565 | |
| 566 | return DWARF_CB_OK; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Walk on lines inside given PDIE. If the PDIE is subprogram, walk only on |
| 571 | * the lines inside the subprogram, otherwise PDIE must be a CU DIE. |
| 572 | */ |
| 573 | static int die_walk_lines(Dwarf_Die *pdie, line_walk_handler_t handler, |
| 574 | void *data) |
| 575 | { |
| 576 | Dwarf_Lines *lines; |
| 577 | Dwarf_Line *line; |
| 578 | Dwarf_Addr addr; |
| 579 | const char *fname; |
| 580 | int lineno, ret = 0; |
| 581 | Dwarf_Die die_mem, *cu_die; |
| 582 | size_t nlines, i; |
| 583 | |
| 584 | /* Get the CU die */ |
| 585 | if (dwarf_tag(pdie) == DW_TAG_subprogram) |
| 586 | cu_die = dwarf_diecu(pdie, &die_mem, NULL, NULL); |
| 587 | else |
| 588 | cu_die = pdie; |
| 589 | if (!cu_die) { |
| 590 | pr_debug2("Failed to get CU from subprogram\n"); |
| 591 | return -EINVAL; |
| 592 | } |
| 593 | |
| 594 | /* Get lines list in the CU */ |
| 595 | if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) { |
| 596 | pr_debug2("Failed to get source lines on this CU.\n"); |
| 597 | return -ENOENT; |
| 598 | } |
| 599 | pr_debug2("Get %zd lines from this CU\n", nlines); |
| 600 | |
| 601 | /* Walk on the lines on lines list */ |
| 602 | for (i = 0; i < nlines; i++) { |
| 603 | line = dwarf_onesrcline(lines, i); |
| 604 | if (line == NULL || |
| 605 | dwarf_lineno(line, &lineno) != 0 || |
| 606 | dwarf_lineaddr(line, &addr) != 0) { |
| 607 | pr_debug2("Failed to get line info. " |
| 608 | "Possible error in debuginfo.\n"); |
| 609 | continue; |
| 610 | } |
| 611 | /* Filter lines based on address */ |
| 612 | if (pdie != cu_die) |
| 613 | /* |
| 614 | * Address filtering |
| 615 | * The line is included in given function, and |
| 616 | * no inline block includes it. |
| 617 | */ |
| 618 | if (!dwarf_haspc(pdie, addr) || |
| 619 | die_find_inlinefunc(pdie, addr, &die_mem)) |
| 620 | continue; |
| 621 | /* Get source line */ |
| 622 | fname = dwarf_linesrc(line, NULL, NULL); |
| 623 | |
| 624 | ret = handler(fname, lineno, addr, data); |
| 625 | if (ret != 0) |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Dwarf lines doesn't include function declarations and inlined |
| 631 | * subroutines. We have to check functions list or given function. |
| 632 | */ |
| 633 | if (pdie != cu_die) |
| 634 | ret = __die_walk_funclines(pdie, handler, data); |
| 635 | else { |
| 636 | struct __line_walk_param param = { |
| 637 | .handler = handler, |
| 638 | .data = data, |
| 639 | .retval = 0, |
| 640 | }; |
| 641 | dwarf_getfuncs(cu_die, __die_walk_culines_cb, ¶m, 0); |
| 642 | ret = param.retval; |
| 643 | } |
| 644 | |
| 645 | return ret; |
| 646 | } |
| 647 | |
Masami Hiramatsu | 378eeaa | 2010-10-21 19:13:09 +0900 | [diff] [blame] | 648 | struct __find_variable_param { |
| 649 | const char *name; |
| 650 | Dwarf_Addr addr; |
| 651 | }; |
| 652 | |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 653 | static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 654 | { |
Masami Hiramatsu | 378eeaa | 2010-10-21 19:13:09 +0900 | [diff] [blame] | 655 | struct __find_variable_param *fvp = data; |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 656 | int tag; |
| 657 | |
| 658 | tag = dwarf_tag(die_mem); |
| 659 | if ((tag == DW_TAG_formal_parameter || |
| 660 | tag == DW_TAG_variable) && |
Masami Hiramatsu | 378eeaa | 2010-10-21 19:13:09 +0900 | [diff] [blame] | 661 | die_compare_name(die_mem, fvp->name)) |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 662 | return DIE_FIND_CB_FOUND; |
| 663 | |
Masami Hiramatsu | 378eeaa | 2010-10-21 19:13:09 +0900 | [diff] [blame] | 664 | if (dwarf_haspc(die_mem, fvp->addr)) |
| 665 | return DIE_FIND_CB_CONTINUE; |
| 666 | else |
| 667 | return DIE_FIND_CB_SIBLING; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 668 | } |
| 669 | |
Masami Hiramatsu | 378eeaa | 2010-10-21 19:13:09 +0900 | [diff] [blame] | 670 | /* Find a variable called 'name' at given address */ |
| 671 | static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name, |
| 672 | Dwarf_Addr addr, Dwarf_Die *die_mem) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 673 | { |
Masami Hiramatsu | 378eeaa | 2010-10-21 19:13:09 +0900 | [diff] [blame] | 674 | struct __find_variable_param fvp = { .name = name, .addr = addr}; |
| 675 | |
| 676 | return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp, |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 677 | die_mem); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 678 | } |
| 679 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 680 | static int __die_find_member_cb(Dwarf_Die *die_mem, void *data) |
| 681 | { |
| 682 | const char *name = data; |
| 683 | |
| 684 | if ((dwarf_tag(die_mem) == DW_TAG_member) && |
Masami Hiramatsu | 8217563 | 2010-07-09 18:29:17 +0900 | [diff] [blame] | 685 | die_compare_name(die_mem, name)) |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 686 | return DIE_FIND_CB_FOUND; |
| 687 | |
| 688 | return DIE_FIND_CB_SIBLING; |
| 689 | } |
| 690 | |
| 691 | /* Find a member called 'name' */ |
| 692 | static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name, |
| 693 | Dwarf_Die *die_mem) |
| 694 | { |
| 695 | return die_find_child(st_die, __die_find_member_cb, (void *)name, |
| 696 | die_mem); |
| 697 | } |
| 698 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 699 | /* Get the name of given variable DIE */ |
| 700 | static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len) |
| 701 | { |
| 702 | Dwarf_Die type; |
| 703 | int tag, ret, ret2; |
| 704 | const char *tmp = ""; |
| 705 | |
| 706 | if (__die_get_real_type(vr_die, &type) == NULL) |
| 707 | return -ENOENT; |
| 708 | |
| 709 | tag = dwarf_tag(&type); |
| 710 | if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type) |
| 711 | tmp = "*"; |
| 712 | else if (tag == DW_TAG_subroutine_type) { |
| 713 | /* Function pointer */ |
| 714 | ret = snprintf(buf, len, "(function_type)"); |
| 715 | return (ret >= len) ? -E2BIG : ret; |
| 716 | } else { |
| 717 | if (!dwarf_diename(&type)) |
| 718 | return -ENOENT; |
| 719 | if (tag == DW_TAG_union_type) |
| 720 | tmp = "union "; |
| 721 | else if (tag == DW_TAG_structure_type) |
| 722 | tmp = "struct "; |
| 723 | /* Write a base name */ |
| 724 | ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type)); |
| 725 | return (ret >= len) ? -E2BIG : ret; |
| 726 | } |
| 727 | ret = die_get_typename(&type, buf, len); |
| 728 | if (ret > 0) { |
| 729 | ret2 = snprintf(buf + ret, len - ret, "%s", tmp); |
| 730 | ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret; |
| 731 | } |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | /* Get the name and type of given variable DIE, stored as "type\tname" */ |
| 736 | static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len) |
| 737 | { |
| 738 | int ret, ret2; |
| 739 | |
| 740 | ret = die_get_typename(vr_die, buf, len); |
| 741 | if (ret < 0) { |
| 742 | pr_debug("Failed to get type, make it unknown.\n"); |
| 743 | ret = snprintf(buf, len, "(unknown_type)"); |
| 744 | } |
| 745 | if (ret > 0) { |
| 746 | ret2 = snprintf(buf + ret, len - ret, "\t%s", |
| 747 | dwarf_diename(vr_die)); |
| 748 | ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret; |
| 749 | } |
| 750 | return ret; |
| 751 | } |
| 752 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 753 | /* |
| 754 | * Probe finder related functions |
| 755 | */ |
| 756 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 757 | static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 758 | { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 759 | struct probe_trace_arg_ref *ref; |
| 760 | ref = zalloc(sizeof(struct probe_trace_arg_ref)); |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 761 | if (ref != NULL) |
| 762 | ref->offset = offs; |
| 763 | return ref; |
| 764 | } |
| 765 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 766 | /* |
| 767 | * Convert a location into trace_arg. |
| 768 | * If tvar == NULL, this just checks variable can be converted. |
| 769 | */ |
| 770 | static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr, |
| 771 | Dwarf_Op *fb_ops, |
| 772 | struct probe_trace_arg *tvar) |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 773 | { |
| 774 | Dwarf_Attribute attr; |
| 775 | Dwarf_Op *op; |
| 776 | size_t nops; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 777 | unsigned int regn; |
| 778 | Dwarf_Word offs = 0; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 779 | bool ref = false; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 780 | const char *regs; |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 781 | int ret; |
| 782 | |
Masami Hiramatsu | 632941c | 2010-10-21 19:13:16 +0900 | [diff] [blame] | 783 | if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL) |
| 784 | goto static_var; |
| 785 | |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 786 | /* TODO: handle more than 1 exprs */ |
| 787 | if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL || |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 788 | dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 || |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 789 | nops == 0) { |
| 790 | /* TODO: Support const_value */ |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 791 | return -ENOENT; |
| 792 | } |
| 793 | |
| 794 | if (op->atom == DW_OP_addr) { |
Masami Hiramatsu | 632941c | 2010-10-21 19:13:16 +0900 | [diff] [blame] | 795 | static_var: |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 796 | if (!tvar) |
| 797 | return 0; |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 798 | /* Static variables on memory (not stack), make @varname */ |
| 799 | ret = strlen(dwarf_diename(vr_die)); |
| 800 | tvar->value = zalloc(ret + 2); |
| 801 | if (tvar->value == NULL) |
| 802 | return -ENOMEM; |
| 803 | snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die)); |
| 804 | tvar->ref = alloc_trace_arg_ref((long)offs); |
| 805 | if (tvar->ref == NULL) |
| 806 | return -ENOMEM; |
| 807 | return 0; |
| 808 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 809 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 810 | /* If this is based on frame buffer, set the offset */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 811 | if (op->atom == DW_OP_fbreg) { |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 812 | if (fb_ops == NULL) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 813 | return -ENOTSUP; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 814 | ref = true; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 815 | offs = op->number; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 816 | op = &fb_ops[0]; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 817 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 818 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 819 | if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) { |
| 820 | regn = op->atom - DW_OP_breg0; |
| 821 | offs += op->number; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 822 | ref = true; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 823 | } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) { |
| 824 | regn = op->atom - DW_OP_reg0; |
| 825 | } else if (op->atom == DW_OP_bregx) { |
| 826 | regn = op->number; |
| 827 | offs += op->number2; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 828 | ref = true; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 829 | } else if (op->atom == DW_OP_regx) { |
| 830 | regn = op->number; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 831 | } else { |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 832 | pr_debug("DW_OP %x is not supported.\n", op->atom); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 833 | return -ENOTSUP; |
| 834 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 835 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 836 | if (!tvar) |
| 837 | return 0; |
| 838 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 839 | regs = get_arch_regstr(regn); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 840 | if (!regs) { |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 841 | /* This should be a bug in DWARF or this tool */ |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 842 | pr_warning("Mapping for the register number %u " |
| 843 | "missing on this architecture.\n", regn); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 844 | return -ERANGE; |
| 845 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 846 | |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 847 | tvar->value = strdup(regs); |
| 848 | if (tvar->value == NULL) |
| 849 | return -ENOMEM; |
| 850 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 851 | if (ref) { |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 852 | tvar->ref = alloc_trace_arg_ref((long)offs); |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 853 | if (tvar->ref == NULL) |
| 854 | return -ENOMEM; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 855 | } |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 856 | return 0; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 857 | } |
| 858 | |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 859 | #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long)) |
| 860 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 861 | static int convert_variable_type(Dwarf_Die *vr_die, |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 862 | struct probe_trace_arg *tvar, |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 863 | const char *cast) |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 864 | { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 865 | struct probe_trace_arg_ref **ref_ptr = &tvar->ref; |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 866 | Dwarf_Die type; |
| 867 | char buf[16]; |
| 868 | int ret; |
| 869 | |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 870 | /* TODO: check all types */ |
| 871 | if (cast && strcmp(cast, "string") != 0) { |
| 872 | /* Non string type is OK */ |
| 873 | tvar->type = strdup(cast); |
| 874 | return (tvar->type == NULL) ? -ENOMEM : 0; |
| 875 | } |
| 876 | |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 877 | if (die_get_bit_size(vr_die) != 0) { |
| 878 | /* This is a bitfield */ |
| 879 | ret = snprintf(buf, 16, "b%d@%d/%zd", die_get_bit_size(vr_die), |
| 880 | die_get_bit_offset(vr_die), |
| 881 | BYTES_TO_BITS(die_get_byte_size(vr_die))); |
| 882 | goto formatted; |
| 883 | } |
| 884 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 885 | if (die_get_real_type(vr_die, &type) == NULL) { |
| 886 | pr_warning("Failed to get a type information of %s.\n", |
| 887 | dwarf_diename(vr_die)); |
| 888 | return -ENOENT; |
| 889 | } |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 890 | |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 891 | pr_debug("%s type is %s.\n", |
| 892 | dwarf_diename(vr_die), dwarf_diename(&type)); |
| 893 | |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 894 | if (cast && strcmp(cast, "string") == 0) { /* String type */ |
| 895 | ret = dwarf_tag(&type); |
| 896 | if (ret != DW_TAG_pointer_type && |
| 897 | ret != DW_TAG_array_type) { |
| 898 | pr_warning("Failed to cast into string: " |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 899 | "%s(%s) is not a pointer nor array.\n", |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 900 | dwarf_diename(vr_die), dwarf_diename(&type)); |
| 901 | return -EINVAL; |
| 902 | } |
| 903 | if (ret == DW_TAG_pointer_type) { |
| 904 | if (die_get_real_type(&type, &type) == NULL) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 905 | pr_warning("Failed to get a type" |
| 906 | " information.\n"); |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 907 | return -ENOENT; |
| 908 | } |
| 909 | while (*ref_ptr) |
| 910 | ref_ptr = &(*ref_ptr)->next; |
| 911 | /* Add new reference with offset +0 */ |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 912 | *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref)); |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 913 | if (*ref_ptr == NULL) { |
| 914 | pr_warning("Out of memory error\n"); |
| 915 | return -ENOMEM; |
| 916 | } |
| 917 | } |
Masami Hiramatsu | 8217563 | 2010-07-09 18:29:17 +0900 | [diff] [blame] | 918 | if (!die_compare_name(&type, "char") && |
| 919 | !die_compare_name(&type, "unsigned char")) { |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 920 | pr_warning("Failed to cast into string: " |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 921 | "%s is not (unsigned) char *.\n", |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 922 | dwarf_diename(vr_die)); |
| 923 | return -EINVAL; |
| 924 | } |
| 925 | tvar->type = strdup(cast); |
| 926 | return (tvar->type == NULL) ? -ENOMEM : 0; |
| 927 | } |
| 928 | |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 929 | ret = BYTES_TO_BITS(die_get_byte_size(&type)); |
| 930 | if (!ret) |
| 931 | /* No size ... try to use default type */ |
| 932 | return 0; |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 933 | |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 934 | /* Check the bitwidth */ |
| 935 | if (ret > MAX_BASIC_TYPE_BITS) { |
| 936 | pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n", |
| 937 | dwarf_diename(&type), MAX_BASIC_TYPE_BITS); |
| 938 | ret = MAX_BASIC_TYPE_BITS; |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 939 | } |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 940 | ret = snprintf(buf, 16, "%c%d", |
| 941 | die_is_signed_type(&type) ? 's' : 'u', ret); |
| 942 | |
| 943 | formatted: |
| 944 | if (ret < 0 || ret >= 16) { |
| 945 | if (ret >= 16) |
| 946 | ret = -E2BIG; |
| 947 | pr_warning("Failed to convert variable type: %s\n", |
| 948 | strerror(-ret)); |
| 949 | return ret; |
| 950 | } |
| 951 | tvar->type = strdup(buf); |
| 952 | if (tvar->type == NULL) |
| 953 | return -ENOMEM; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 954 | return 0; |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 955 | } |
| 956 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 957 | static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 958 | struct perf_probe_arg_field *field, |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 959 | struct probe_trace_arg_ref **ref_ptr, |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 960 | Dwarf_Die *die_mem) |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 961 | { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 962 | struct probe_trace_arg_ref *ref = *ref_ptr; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 963 | Dwarf_Die type; |
| 964 | Dwarf_Word offs; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 965 | int ret, tag; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 966 | |
| 967 | pr_debug("converting %s in %s\n", field->name, varname); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 968 | if (die_get_real_type(vr_die, &type) == NULL) { |
| 969 | pr_warning("Failed to get the type of %s.\n", varname); |
| 970 | return -ENOENT; |
| 971 | } |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 972 | pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type)); |
| 973 | tag = dwarf_tag(&type); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 974 | |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 975 | if (field->name[0] == '[' && |
| 976 | (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) { |
| 977 | if (field->next) |
| 978 | /* Save original type for next field */ |
| 979 | memcpy(die_mem, &type, sizeof(*die_mem)); |
| 980 | /* Get the type of this array */ |
| 981 | if (die_get_real_type(&type, &type) == NULL) { |
| 982 | pr_warning("Failed to get the type of %s.\n", varname); |
| 983 | return -ENOENT; |
| 984 | } |
| 985 | pr_debug2("Array real type: (%x)\n", |
| 986 | (unsigned)dwarf_dieoffset(&type)); |
| 987 | if (tag == DW_TAG_pointer_type) { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 988 | ref = zalloc(sizeof(struct probe_trace_arg_ref)); |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 989 | if (ref == NULL) |
| 990 | return -ENOMEM; |
| 991 | if (*ref_ptr) |
| 992 | (*ref_ptr)->next = ref; |
| 993 | else |
| 994 | *ref_ptr = ref; |
| 995 | } |
| 996 | ref->offset += die_get_byte_size(&type) * field->index; |
| 997 | if (!field->next) |
| 998 | /* Save vr_die for converting types */ |
| 999 | memcpy(die_mem, vr_die, sizeof(*die_mem)); |
| 1000 | goto next; |
| 1001 | } else if (tag == DW_TAG_pointer_type) { |
| 1002 | /* Check the pointer and dereference */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1003 | if (!field->ref) { |
| 1004 | pr_err("Semantic error: %s must be referred by '->'\n", |
| 1005 | field->name); |
| 1006 | return -EINVAL; |
| 1007 | } |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1008 | /* Get the type pointed by this pointer */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1009 | if (die_get_real_type(&type, &type) == NULL) { |
| 1010 | pr_warning("Failed to get the type of %s.\n", varname); |
| 1011 | return -ENOENT; |
| 1012 | } |
Masami Hiramatsu | 12e5a7a | 2010-04-02 12:50:53 -0400 | [diff] [blame] | 1013 | /* Verify it is a data structure */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1014 | if (dwarf_tag(&type) != DW_TAG_structure_type) { |
| 1015 | pr_warning("%s is not a data structure.\n", varname); |
| 1016 | return -EINVAL; |
| 1017 | } |
Masami Hiramatsu | 12e5a7a | 2010-04-02 12:50:53 -0400 | [diff] [blame] | 1018 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 1019 | ref = zalloc(sizeof(struct probe_trace_arg_ref)); |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 1020 | if (ref == NULL) |
| 1021 | return -ENOMEM; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1022 | if (*ref_ptr) |
| 1023 | (*ref_ptr)->next = ref; |
| 1024 | else |
| 1025 | *ref_ptr = ref; |
| 1026 | } else { |
Masami Hiramatsu | 12e5a7a | 2010-04-02 12:50:53 -0400 | [diff] [blame] | 1027 | /* Verify it is a data structure */ |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1028 | if (tag != DW_TAG_structure_type) { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1029 | pr_warning("%s is not a data structure.\n", varname); |
| 1030 | return -EINVAL; |
| 1031 | } |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1032 | if (field->name[0] == '[') { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1033 | pr_err("Semantic error: %s is not a pointor" |
| 1034 | " nor array.\n", varname); |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1035 | return -EINVAL; |
| 1036 | } |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1037 | if (field->ref) { |
| 1038 | pr_err("Semantic error: %s must be referred by '.'\n", |
| 1039 | field->name); |
| 1040 | return -EINVAL; |
| 1041 | } |
| 1042 | if (!ref) { |
| 1043 | pr_warning("Structure on a register is not " |
| 1044 | "supported yet.\n"); |
| 1045 | return -ENOTSUP; |
| 1046 | } |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1047 | } |
| 1048 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1049 | if (die_find_member(&type, field->name, die_mem) == NULL) { |
| 1050 | pr_warning("%s(tyep:%s) has no member %s.\n", varname, |
| 1051 | dwarf_diename(&type), field->name); |
| 1052 | return -EINVAL; |
| 1053 | } |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1054 | |
| 1055 | /* Get the offset of the field */ |
Masami Hiramatsu | de1439d | 2010-04-14 17:44:00 -0300 | [diff] [blame] | 1056 | ret = die_get_data_member_location(die_mem, &offs); |
| 1057 | if (ret < 0) { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1058 | pr_warning("Failed to get the offset of %s.\n", field->name); |
Masami Hiramatsu | de1439d | 2010-04-14 17:44:00 -0300 | [diff] [blame] | 1059 | return ret; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1060 | } |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1061 | ref->offset += (long)offs; |
| 1062 | |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 1063 | next: |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1064 | /* Converting next field */ |
| 1065 | if (field->next) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1066 | return convert_variable_fields(die_mem, field->name, |
Masami Hiramatsu | de1439d | 2010-04-14 17:44:00 -0300 | [diff] [blame] | 1067 | field->next, &ref, die_mem); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1068 | else |
| 1069 | return 0; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 1070 | } |
| 1071 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1072 | /* Show a variables in kprobe event format */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1073 | static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1074 | { |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 1075 | Dwarf_Die die_mem; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1076 | int ret; |
| 1077 | |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 1078 | pr_debug("Converting variable %s into trace event.\n", |
| 1079 | dwarf_diename(vr_die)); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1080 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1081 | ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops, |
| 1082 | pf->tvar); |
| 1083 | if (ret == -ENOENT) |
| 1084 | pr_err("Failed to find the location of %s at this address.\n" |
| 1085 | " Perhaps, it has been optimized out.\n", pf->pvar->var); |
| 1086 | else if (ret == -ENOTSUP) |
| 1087 | pr_err("Sorry, we don't support this variable location yet.\n"); |
| 1088 | else if (pf->pvar->field) { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1089 | ret = convert_variable_fields(vr_die, pf->pvar->var, |
| 1090 | pf->pvar->field, &pf->tvar->ref, |
| 1091 | &die_mem); |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 1092 | vr_die = &die_mem; |
| 1093 | } |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 1094 | if (ret == 0) |
| 1095 | ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1096 | /* *expr will be cached in libdw. Don't free it. */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1097 | return ret; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1098 | } |
| 1099 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1100 | /* Find a variable in a subprogram die */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1101 | static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1102 | { |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 1103 | Dwarf_Die vr_die, *scopes; |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 1104 | char buf[32], *ptr; |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 1105 | int ret, nscopes; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1106 | |
Masami Hiramatsu | 367e94c | 2010-08-27 20:38:59 +0900 | [diff] [blame] | 1107 | if (!is_c_varname(pf->pvar->var)) { |
| 1108 | /* Copy raw parameters */ |
| 1109 | pf->tvar->value = strdup(pf->pvar->var); |
| 1110 | if (pf->tvar->value == NULL) |
| 1111 | return -ENOMEM; |
| 1112 | if (pf->pvar->type) { |
| 1113 | pf->tvar->type = strdup(pf->pvar->type); |
| 1114 | if (pf->tvar->type == NULL) |
| 1115 | return -ENOMEM; |
| 1116 | } |
| 1117 | if (pf->pvar->name) { |
| 1118 | pf->tvar->name = strdup(pf->pvar->name); |
| 1119 | if (pf->tvar->name == NULL) |
| 1120 | return -ENOMEM; |
| 1121 | } else |
| 1122 | pf->tvar->name = NULL; |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1126 | if (pf->pvar->name) |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1127 | pf->tvar->name = strdup(pf->pvar->name); |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1128 | else { |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1129 | ret = synthesize_perf_probe_arg(pf->pvar, buf, 32); |
| 1130 | if (ret < 0) |
| 1131 | return ret; |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 1132 | ptr = strchr(buf, ':'); /* Change type separator to _ */ |
| 1133 | if (ptr) |
| 1134 | *ptr = '_'; |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1135 | pf->tvar->name = strdup(buf); |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1136 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1137 | if (pf->tvar->name == NULL) |
| 1138 | return -ENOMEM; |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 1139 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1140 | pr_debug("Searching '%s' variable in context.\n", |
| 1141 | pf->pvar->var); |
| 1142 | /* Search child die for local variables and parameters. */ |
Masami Hiramatsu | 378eeaa | 2010-10-21 19:13:09 +0900 | [diff] [blame] | 1143 | if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die)) |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 1144 | ret = convert_variable(&vr_die, pf); |
| 1145 | else { |
| 1146 | /* Search upper class */ |
| 1147 | nscopes = dwarf_getscopes_die(sp_die, &scopes); |
Masami Hiramatsu | 632941c | 2010-10-21 19:13:16 +0900 | [diff] [blame] | 1148 | while (nscopes-- > 1) { |
| 1149 | pr_debug("Searching variables in %s\n", |
| 1150 | dwarf_diename(&scopes[nscopes])); |
| 1151 | /* We should check this scope, so give dummy address */ |
| 1152 | if (die_find_variable_at(&scopes[nscopes], |
| 1153 | pf->pvar->var, 0, |
| 1154 | &vr_die)) { |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 1155 | ret = convert_variable(&vr_die, pf); |
Masami Hiramatsu | 632941c | 2010-10-21 19:13:16 +0900 | [diff] [blame] | 1156 | goto found; |
| 1157 | } |
| 1158 | } |
| 1159 | if (scopes) |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 1160 | free(scopes); |
Masami Hiramatsu | 632941c | 2010-10-21 19:13:16 +0900 | [diff] [blame] | 1161 | ret = -ENOENT; |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 1162 | } |
Masami Hiramatsu | 632941c | 2010-10-21 19:13:16 +0900 | [diff] [blame] | 1163 | found: |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 1164 | if (ret < 0) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1165 | pr_warning("Failed to find '%s' in this function.\n", |
| 1166 | pf->pvar->var); |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 1167 | return ret; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1168 | } |
| 1169 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1170 | /* Convert subprogram DIE to trace point */ |
| 1171 | static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr, |
| 1172 | bool retprobe, struct probe_trace_point *tp) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1173 | { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1174 | Dwarf_Addr eaddr; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1175 | const char *name; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1176 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1177 | /* Copy the name of probe point */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1178 | name = dwarf_diename(sp_die); |
| 1179 | if (name) { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1180 | if (dwarf_entrypc(sp_die, &eaddr) != 0) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1181 | pr_warning("Failed to get entry address of %s\n", |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1182 | dwarf_diename(sp_die)); |
| 1183 | return -ENOENT; |
| 1184 | } |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1185 | tp->symbol = strdup(name); |
| 1186 | if (tp->symbol == NULL) |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1187 | return -ENOMEM; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1188 | tp->offset = (unsigned long)(paddr - eaddr); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1189 | } else |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1190 | /* This function has no name. */ |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1191 | tp->offset = (unsigned long)paddr; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1192 | |
Masami Hiramatsu | 04ddd04 | 2010-08-27 20:38:53 +0900 | [diff] [blame] | 1193 | /* Return probe must be on the head of a subprogram */ |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1194 | if (retprobe) { |
| 1195 | if (eaddr != paddr) { |
Masami Hiramatsu | 04ddd04 | 2010-08-27 20:38:53 +0900 | [diff] [blame] | 1196 | pr_warning("Return probe must be on the head of" |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1197 | " a real function.\n"); |
Masami Hiramatsu | 04ddd04 | 2010-08-27 20:38:53 +0900 | [diff] [blame] | 1198 | return -EINVAL; |
| 1199 | } |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1200 | tp->retprobe = true; |
Masami Hiramatsu | 04ddd04 | 2010-08-27 20:38:53 +0900 | [diff] [blame] | 1201 | } |
| 1202 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | /* Call probe_finder callback with real subprogram DIE */ |
| 1207 | static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf) |
| 1208 | { |
| 1209 | Dwarf_Die die_mem; |
| 1210 | Dwarf_Attribute fb_attr; |
| 1211 | size_t nops; |
| 1212 | int ret; |
| 1213 | |
| 1214 | /* If no real subprogram, find a real one */ |
| 1215 | if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) { |
| 1216 | sp_die = die_find_real_subprogram(&pf->cu_die, |
| 1217 | pf->addr, &die_mem); |
| 1218 | if (!sp_die) { |
| 1219 | pr_warning("Failed to find probe point in any " |
| 1220 | "functions.\n"); |
| 1221 | return -ENOENT; |
| 1222 | } |
| 1223 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1224 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1225 | /* Get the frame base attribute/ops */ |
| 1226 | dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr); |
Masami Hiramatsu | d0cb426 | 2010-03-15 13:02:35 -0400 | [diff] [blame] | 1227 | ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1); |
Masami Hiramatsu | a34a985 | 2010-04-12 13:17:29 -0400 | [diff] [blame] | 1228 | if (ret <= 0 || nops == 0) { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1229 | pf->fb_ops = NULL; |
Masami Hiramatsu | 7752f1b | 2010-05-10 13:12:07 -0400 | [diff] [blame] | 1230 | #if _ELFUTILS_PREREQ(0, 142) |
Masami Hiramatsu | a34a985 | 2010-04-12 13:17:29 -0400 | [diff] [blame] | 1231 | } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa && |
| 1232 | pf->cfi != NULL) { |
| 1233 | Dwarf_Frame *frame; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1234 | if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 || |
| 1235 | dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1236 | pr_warning("Failed to get call frame on 0x%jx\n", |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1237 | (uintmax_t)pf->addr); |
| 1238 | return -ENOENT; |
| 1239 | } |
Masami Hiramatsu | 7752f1b | 2010-05-10 13:12:07 -0400 | [diff] [blame] | 1240 | #endif |
Masami Hiramatsu | a34a985 | 2010-04-12 13:17:29 -0400 | [diff] [blame] | 1241 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1242 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1243 | /* Call finder's callback handler */ |
| 1244 | ret = pf->callback(sp_die, pf); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1245 | |
| 1246 | /* *pf->fb_ops will be cached in libdw. Don't free it. */ |
| 1247 | pf->fb_ops = NULL; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1248 | |
| 1249 | return ret; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1250 | } |
| 1251 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1252 | static int probe_point_line_walker(const char *fname, int lineno, |
| 1253 | Dwarf_Addr addr, void *data) |
| 1254 | { |
| 1255 | struct probe_finder *pf = data; |
| 1256 | int ret; |
| 1257 | |
| 1258 | if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0) |
| 1259 | return 0; |
| 1260 | |
| 1261 | pf->addr = addr; |
| 1262 | ret = call_probe_finder(NULL, pf); |
| 1263 | |
| 1264 | /* Continue if no error, because the line will be in inline function */ |
Arnaldo Carvalho de Melo | fbee632 | 2011-02-21 13:23:57 -0300 | [diff] [blame] | 1265 | return ret < 0 ? ret : 0; |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1266 | } |
| 1267 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1268 | /* Find probe point from its line number */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1269 | static int find_probe_point_by_line(struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1270 | { |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1271 | return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1272 | } |
| 1273 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1274 | /* Find lines which match lazy pattern */ |
| 1275 | static int find_lazy_match_lines(struct list_head *head, |
| 1276 | const char *fname, const char *pat) |
| 1277 | { |
Franck Bui-Huu | f50c216 | 2011-01-13 11:18:30 +0100 | [diff] [blame] | 1278 | FILE *fp; |
| 1279 | char *line = NULL; |
| 1280 | size_t line_len; |
| 1281 | ssize_t len; |
| 1282 | int count = 0, linenum = 1; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1283 | |
Franck Bui-Huu | f50c216 | 2011-01-13 11:18:30 +0100 | [diff] [blame] | 1284 | fp = fopen(fname, "r"); |
| 1285 | if (!fp) { |
| 1286 | pr_warning("Failed to open %s: %s\n", fname, strerror(errno)); |
Arnaldo Carvalho de Melo | b448c4b | 2010-05-18 23:04:28 -0300 | [diff] [blame] | 1287 | return -errno; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1288 | } |
| 1289 | |
Franck Bui-Huu | f50c216 | 2011-01-13 11:18:30 +0100 | [diff] [blame] | 1290 | while ((len = getline(&line, &line_len, fp)) > 0) { |
| 1291 | |
| 1292 | if (line[len - 1] == '\n') |
| 1293 | line[len - 1] = '\0'; |
| 1294 | |
| 1295 | if (strlazymatch(line, pat)) { |
| 1296 | line_list__add_line(head, linenum); |
| 1297 | count++; |
| 1298 | } |
| 1299 | linenum++; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1300 | } |
Arnaldo Carvalho de Melo | b448c4b | 2010-05-18 23:04:28 -0300 | [diff] [blame] | 1301 | |
Franck Bui-Huu | f50c216 | 2011-01-13 11:18:30 +0100 | [diff] [blame] | 1302 | if (ferror(fp)) |
| 1303 | count = -errno; |
| 1304 | free(line); |
| 1305 | fclose(fp); |
| 1306 | |
| 1307 | if (count == 0) |
| 1308 | pr_debug("No matched lines found in %s.\n", fname); |
| 1309 | return count; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1310 | } |
| 1311 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1312 | static int probe_point_lazy_walker(const char *fname, int lineno, |
| 1313 | Dwarf_Addr addr, void *data) |
| 1314 | { |
| 1315 | struct probe_finder *pf = data; |
| 1316 | int ret; |
| 1317 | |
| 1318 | if (!line_list__has_line(&pf->lcache, lineno) || |
| 1319 | strtailcmp(fname, pf->fname) != 0) |
| 1320 | return 0; |
| 1321 | |
| 1322 | pr_debug("Probe line found: line:%d addr:0x%llx\n", |
| 1323 | lineno, (unsigned long long)addr); |
| 1324 | pf->addr = addr; |
| 1325 | ret = call_probe_finder(NULL, pf); |
| 1326 | |
| 1327 | /* |
| 1328 | * Continue if no error, because the lazy pattern will match |
| 1329 | * to other lines |
| 1330 | */ |
Ingo Molnar | 5e814dd5 | 2011-03-15 20:51:09 +0100 | [diff] [blame] | 1331 | return ret < 0 ? ret : 0; |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1332 | } |
| 1333 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1334 | /* Find probe points from lazy pattern */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1335 | static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf) |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1336 | { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1337 | int ret = 0; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1338 | |
| 1339 | if (list_empty(&pf->lcache)) { |
| 1340 | /* Matching lazy line pattern */ |
| 1341 | ret = find_lazy_match_lines(&pf->lcache, pf->fname, |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1342 | pf->pev->point.lazy_line); |
Franck Bui-Huu | f50c216 | 2011-01-13 11:18:30 +0100 | [diff] [blame] | 1343 | if (ret <= 0) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1344 | return ret; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1345 | } |
| 1346 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1347 | return die_walk_lines(sp_die, probe_point_lazy_walker, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1348 | } |
| 1349 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1350 | /* Callback parameter with return value */ |
| 1351 | struct dwarf_callback_param { |
| 1352 | void *data; |
| 1353 | int retval; |
| 1354 | }; |
| 1355 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1356 | static int probe_point_inline_cb(Dwarf_Die *in_die, void *data) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1357 | { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1358 | struct dwarf_callback_param *param = data; |
| 1359 | struct probe_finder *pf = param->data; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1360 | struct perf_probe_point *pp = &pf->pev->point; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1361 | Dwarf_Addr addr; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1362 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1363 | if (pp->lazy_line) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1364 | param->retval = find_probe_point_lazy(in_die, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1365 | else { |
| 1366 | /* Get probe address */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1367 | if (dwarf_entrypc(in_die, &addr) != 0) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1368 | pr_warning("Failed to get entry address of %s.\n", |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1369 | dwarf_diename(in_die)); |
| 1370 | param->retval = -ENOENT; |
| 1371 | return DWARF_CB_ABORT; |
| 1372 | } |
| 1373 | pf->addr = addr; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1374 | pf->addr += pp->offset; |
| 1375 | pr_debug("found inline addr: 0x%jx\n", |
| 1376 | (uintmax_t)pf->addr); |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1377 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1378 | param->retval = call_probe_finder(in_die, pf); |
Masami Hiramatsu | 5d1ee04 | 2010-04-21 15:56:32 -0400 | [diff] [blame] | 1379 | if (param->retval < 0) |
| 1380 | return DWARF_CB_ABORT; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1381 | } |
| 1382 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1383 | return DWARF_CB_OK; |
| 1384 | } |
| 1385 | |
| 1386 | /* Search function from function name */ |
| 1387 | static int probe_point_search_cb(Dwarf_Die *sp_die, void *data) |
| 1388 | { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1389 | struct dwarf_callback_param *param = data; |
| 1390 | struct probe_finder *pf = param->data; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1391 | struct perf_probe_point *pp = &pf->pev->point; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1392 | |
| 1393 | /* Check tag and diename */ |
| 1394 | if (dwarf_tag(sp_die) != DW_TAG_subprogram || |
Masami Hiramatsu | 8217563 | 2010-07-09 18:29:17 +0900 | [diff] [blame] | 1395 | !die_compare_name(sp_die, pp->function)) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1396 | return DWARF_CB_OK; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1397 | |
Masami Hiramatsu | 7d21635 | 2011-03-30 18:25:41 +0900 | [diff] [blame] | 1398 | /* Check declared file */ |
| 1399 | if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die))) |
| 1400 | return DWARF_CB_OK; |
| 1401 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1402 | pf->fname = dwarf_decl_file(sp_die); |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1403 | if (pp->line) { /* Function relative line */ |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1404 | dwarf_decl_line(sp_die, &pf->lno); |
| 1405 | pf->lno += pp->line; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1406 | param->retval = find_probe_point_by_line(pf); |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1407 | } else if (!dwarf_func_inline(sp_die)) { |
| 1408 | /* Real function */ |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1409 | if (pp->lazy_line) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1410 | param->retval = find_probe_point_lazy(sp_die, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1411 | else { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1412 | if (dwarf_entrypc(sp_die, &pf->addr) != 0) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1413 | pr_warning("Failed to get entry address of " |
| 1414 | "%s.\n", dwarf_diename(sp_die)); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1415 | param->retval = -ENOENT; |
| 1416 | return DWARF_CB_ABORT; |
| 1417 | } |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1418 | pf->addr += pp->offset; |
| 1419 | /* TODO: Check the address in this function */ |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1420 | param->retval = call_probe_finder(sp_die, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1421 | } |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1422 | } else { |
| 1423 | struct dwarf_callback_param _param = {.data = (void *)pf, |
| 1424 | .retval = 0}; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1425 | /* Inlined function: search instances */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1426 | dwarf_func_inline_instances(sp_die, probe_point_inline_cb, |
| 1427 | &_param); |
| 1428 | param->retval = _param.retval; |
| 1429 | } |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1430 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1431 | return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */ |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1432 | } |
| 1433 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1434 | static int find_probe_point_by_func(struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1435 | { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1436 | struct dwarf_callback_param _param = {.data = (void *)pf, |
| 1437 | .retval = 0}; |
| 1438 | dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0); |
| 1439 | return _param.retval; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1440 | } |
| 1441 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1442 | /* Find probe points from debuginfo */ |
| 1443 | static int find_probes(int fd, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1444 | { |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1445 | struct perf_probe_point *pp = &pf->pev->point; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1446 | Dwarf_Off off, noff; |
| 1447 | size_t cuhl; |
| 1448 | Dwarf_Die *diep; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1449 | Dwarf *dbg = NULL; |
| 1450 | Dwfl *dwfl; |
| 1451 | Dwarf_Addr bias; /* Currently ignored */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1452 | int ret = 0; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1453 | |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1454 | dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1455 | if (!dbg) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1456 | pr_warning("No debug information found in the vmlinux - " |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1457 | "please rebuild with CONFIG_DEBUG_INFO=y.\n"); |
Masami Hiramatsu | f0c4801 | 2011-03-30 18:25:47 +0900 | [diff] [blame] | 1458 | close(fd); /* Without dwfl_end(), fd isn't closed. */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1459 | return -EBADF; |
| 1460 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1461 | |
Masami Hiramatsu | 7752f1b | 2010-05-10 13:12:07 -0400 | [diff] [blame] | 1462 | #if _ELFUTILS_PREREQ(0, 142) |
Masami Hiramatsu | a34a985 | 2010-04-12 13:17:29 -0400 | [diff] [blame] | 1463 | /* Get the call frame information from this dwarf */ |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1464 | pf->cfi = dwarf_getcfi(dbg); |
Masami Hiramatsu | 7752f1b | 2010-05-10 13:12:07 -0400 | [diff] [blame] | 1465 | #endif |
Masami Hiramatsu | a34a985 | 2010-04-12 13:17:29 -0400 | [diff] [blame] | 1466 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1467 | off = 0; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1468 | line_list__init(&pf->lcache); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1469 | /* Loop on CUs (Compilation Unit) */ |
Arnaldo Carvalho de Melo | 8635bf6 | 2011-02-22 06:56:18 -0300 | [diff] [blame] | 1470 | while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1471 | /* Get the DIE(Debugging Information Entry) of this CU */ |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1472 | diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1473 | if (!diep) |
| 1474 | continue; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1475 | |
| 1476 | /* Check if target file is included. */ |
| 1477 | if (pp->file) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1478 | pf->fname = cu_find_realpath(&pf->cu_die, pp->file); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1479 | else |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1480 | pf->fname = NULL; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1481 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1482 | if (!pp->file || pf->fname) { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1483 | if (pp->function) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1484 | ret = find_probe_point_by_func(pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1485 | else if (pp->lazy_line) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1486 | ret = find_probe_point_lazy(NULL, pf); |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 1487 | else { |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1488 | pf->lno = pp->line; |
| 1489 | ret = find_probe_point_by_line(pf); |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 1490 | } |
Arnaldo Carvalho de Melo | 8635bf6 | 2011-02-22 06:56:18 -0300 | [diff] [blame] | 1491 | if (ret < 0) |
Arnaldo Carvalho de Melo | fbee632 | 2011-02-21 13:23:57 -0300 | [diff] [blame] | 1492 | break; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1493 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1494 | off = noff; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1495 | } |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1496 | line_list__free(&pf->lcache); |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1497 | if (dwfl) |
| 1498 | dwfl_end(dwfl); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1499 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1500 | return ret; |
| 1501 | } |
| 1502 | |
| 1503 | /* Add a found probe point into trace event list */ |
| 1504 | static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf) |
| 1505 | { |
| 1506 | struct trace_event_finder *tf = |
| 1507 | container_of(pf, struct trace_event_finder, pf); |
| 1508 | struct probe_trace_event *tev; |
| 1509 | int ret, i; |
| 1510 | |
| 1511 | /* Check number of tevs */ |
| 1512 | if (tf->ntevs == tf->max_tevs) { |
| 1513 | pr_warning("Too many( > %d) probe point found.\n", |
| 1514 | tf->max_tevs); |
| 1515 | return -ERANGE; |
| 1516 | } |
| 1517 | tev = &tf->tevs[tf->ntevs++]; |
| 1518 | |
| 1519 | ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe, |
| 1520 | &tev->point); |
| 1521 | if (ret < 0) |
| 1522 | return ret; |
| 1523 | |
| 1524 | pr_debug("Probe point found: %s+%lu\n", tev->point.symbol, |
| 1525 | tev->point.offset); |
| 1526 | |
| 1527 | /* Find each argument */ |
| 1528 | tev->nargs = pf->pev->nargs; |
| 1529 | tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); |
| 1530 | if (tev->args == NULL) |
| 1531 | return -ENOMEM; |
| 1532 | for (i = 0; i < pf->pev->nargs; i++) { |
| 1533 | pf->pvar = &pf->pev->args[i]; |
| 1534 | pf->tvar = &tev->args[i]; |
| 1535 | ret = find_variable(sp_die, pf); |
| 1536 | if (ret != 0) |
| 1537 | return ret; |
| 1538 | } |
| 1539 | |
| 1540 | return 0; |
| 1541 | } |
| 1542 | |
| 1543 | /* Find probe_trace_events specified by perf_probe_event from debuginfo */ |
| 1544 | int find_probe_trace_events(int fd, struct perf_probe_event *pev, |
| 1545 | struct probe_trace_event **tevs, int max_tevs) |
| 1546 | { |
| 1547 | struct trace_event_finder tf = { |
| 1548 | .pf = {.pev = pev, .callback = add_probe_trace_event}, |
| 1549 | .max_tevs = max_tevs}; |
| 1550 | int ret; |
| 1551 | |
| 1552 | /* Allocate result tevs array */ |
| 1553 | *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs); |
| 1554 | if (*tevs == NULL) |
| 1555 | return -ENOMEM; |
| 1556 | |
| 1557 | tf.tevs = *tevs; |
| 1558 | tf.ntevs = 0; |
| 1559 | |
| 1560 | ret = find_probes(fd, &tf.pf); |
| 1561 | if (ret < 0) { |
| 1562 | free(*tevs); |
| 1563 | *tevs = NULL; |
| 1564 | return ret; |
| 1565 | } |
| 1566 | |
| 1567 | return (ret < 0) ? ret : tf.ntevs; |
| 1568 | } |
| 1569 | |
| 1570 | #define MAX_VAR_LEN 64 |
| 1571 | |
| 1572 | /* Collect available variables in this scope */ |
| 1573 | static int collect_variables_cb(Dwarf_Die *die_mem, void *data) |
| 1574 | { |
| 1575 | struct available_var_finder *af = data; |
| 1576 | struct variable_list *vl; |
| 1577 | char buf[MAX_VAR_LEN]; |
| 1578 | int tag, ret; |
| 1579 | |
| 1580 | vl = &af->vls[af->nvls - 1]; |
| 1581 | |
| 1582 | tag = dwarf_tag(die_mem); |
| 1583 | if (tag == DW_TAG_formal_parameter || |
| 1584 | tag == DW_TAG_variable) { |
| 1585 | ret = convert_variable_location(die_mem, af->pf.addr, |
| 1586 | af->pf.fb_ops, NULL); |
| 1587 | if (ret == 0) { |
| 1588 | ret = die_get_varname(die_mem, buf, MAX_VAR_LEN); |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1589 | pr_debug2("Add new var: %s\n", buf); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1590 | if (ret > 0) |
| 1591 | strlist__add(vl->vars, buf); |
| 1592 | } |
| 1593 | } |
| 1594 | |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1595 | if (af->child && dwarf_haspc(die_mem, af->pf.addr)) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1596 | return DIE_FIND_CB_CONTINUE; |
| 1597 | else |
| 1598 | return DIE_FIND_CB_SIBLING; |
| 1599 | } |
| 1600 | |
| 1601 | /* Add a found vars into available variables list */ |
| 1602 | static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf) |
| 1603 | { |
| 1604 | struct available_var_finder *af = |
| 1605 | container_of(pf, struct available_var_finder, pf); |
| 1606 | struct variable_list *vl; |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1607 | Dwarf_Die die_mem, *scopes = NULL; |
| 1608 | int ret, nscopes; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1609 | |
| 1610 | /* Check number of tevs */ |
| 1611 | if (af->nvls == af->max_vls) { |
| 1612 | pr_warning("Too many( > %d) probe point found.\n", af->max_vls); |
| 1613 | return -ERANGE; |
| 1614 | } |
| 1615 | vl = &af->vls[af->nvls++]; |
| 1616 | |
| 1617 | ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe, |
| 1618 | &vl->point); |
| 1619 | if (ret < 0) |
| 1620 | return ret; |
| 1621 | |
| 1622 | pr_debug("Probe point found: %s+%lu\n", vl->point.symbol, |
| 1623 | vl->point.offset); |
| 1624 | |
| 1625 | /* Find local variables */ |
| 1626 | vl->vars = strlist__new(true, NULL); |
| 1627 | if (vl->vars == NULL) |
| 1628 | return -ENOMEM; |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1629 | af->child = true; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1630 | die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem); |
| 1631 | |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1632 | /* Find external variables */ |
| 1633 | if (!af->externs) |
| 1634 | goto out; |
| 1635 | /* Don't need to search child DIE for externs. */ |
| 1636 | af->child = false; |
| 1637 | nscopes = dwarf_getscopes_die(sp_die, &scopes); |
| 1638 | while (nscopes-- > 1) |
| 1639 | die_find_child(&scopes[nscopes], collect_variables_cb, |
| 1640 | (void *)af, &die_mem); |
| 1641 | if (scopes) |
| 1642 | free(scopes); |
| 1643 | |
| 1644 | out: |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1645 | if (strlist__empty(vl->vars)) { |
| 1646 | strlist__delete(vl->vars); |
| 1647 | vl->vars = NULL; |
| 1648 | } |
| 1649 | |
| 1650 | return ret; |
| 1651 | } |
| 1652 | |
| 1653 | /* Find available variables at given probe point */ |
| 1654 | int find_available_vars_at(int fd, struct perf_probe_event *pev, |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1655 | struct variable_list **vls, int max_vls, |
| 1656 | bool externs) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1657 | { |
| 1658 | struct available_var_finder af = { |
| 1659 | .pf = {.pev = pev, .callback = add_available_vars}, |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1660 | .max_vls = max_vls, .externs = externs}; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1661 | int ret; |
| 1662 | |
| 1663 | /* Allocate result vls array */ |
| 1664 | *vls = zalloc(sizeof(struct variable_list) * max_vls); |
| 1665 | if (*vls == NULL) |
| 1666 | return -ENOMEM; |
| 1667 | |
| 1668 | af.vls = *vls; |
| 1669 | af.nvls = 0; |
| 1670 | |
| 1671 | ret = find_probes(fd, &af.pf); |
| 1672 | if (ret < 0) { |
| 1673 | /* Free vlist for error */ |
| 1674 | while (af.nvls--) { |
| 1675 | if (af.vls[af.nvls].point.symbol) |
| 1676 | free(af.vls[af.nvls].point.symbol); |
| 1677 | if (af.vls[af.nvls].vars) |
| 1678 | strlist__delete(af.vls[af.nvls].vars); |
| 1679 | } |
| 1680 | free(af.vls); |
| 1681 | *vls = NULL; |
| 1682 | return ret; |
| 1683 | } |
| 1684 | |
| 1685 | return (ret < 0) ? ret : af.nvls; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1686 | } |
| 1687 | |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1688 | /* Reverse search */ |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1689 | int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt) |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1690 | { |
| 1691 | Dwarf_Die cudie, spdie, indie; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1692 | Dwarf *dbg = NULL; |
| 1693 | Dwfl *dwfl = NULL; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1694 | Dwarf_Line *line; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1695 | Dwarf_Addr laddr, eaddr, bias = 0; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1696 | const char *tmp; |
| 1697 | int lineno, ret = 0; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1698 | bool found = false; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1699 | |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1700 | /* Open the live linux kernel */ |
| 1701 | dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias); |
| 1702 | if (!dbg) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1703 | pr_warning("No debug information found in the vmlinux - " |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1704 | "please rebuild with CONFIG_DEBUG_INFO=y.\n"); |
| 1705 | ret = -EINVAL; |
| 1706 | goto end; |
| 1707 | } |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1708 | |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1709 | /* Adjust address with bias */ |
| 1710 | addr += bias; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1711 | /* Find cu die */ |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1712 | if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1713 | pr_warning("Failed to find debug information for address %lx\n", |
| 1714 | addr); |
Masami Hiramatsu | 75ec5a2 | 2010-04-02 12:50:59 -0400 | [diff] [blame] | 1715 | ret = -EINVAL; |
| 1716 | goto end; |
| 1717 | } |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1718 | |
| 1719 | /* Find a corresponding line */ |
| 1720 | line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr); |
| 1721 | if (line) { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1722 | if (dwarf_lineaddr(line, &laddr) == 0 && |
| 1723 | (Dwarf_Addr)addr == laddr && |
| 1724 | dwarf_lineno(line, &lineno) == 0) { |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1725 | tmp = dwarf_linesrc(line, NULL, NULL); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1726 | if (tmp) { |
| 1727 | ppt->line = lineno; |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1728 | ppt->file = strdup(tmp); |
| 1729 | if (ppt->file == NULL) { |
| 1730 | ret = -ENOMEM; |
| 1731 | goto end; |
| 1732 | } |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1733 | found = true; |
| 1734 | } |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | /* Find a corresponding function */ |
| 1739 | if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) { |
| 1740 | tmp = dwarf_diename(&spdie); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1741 | if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0) |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1742 | goto end; |
| 1743 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1744 | if (ppt->line) { |
| 1745 | if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr, |
| 1746 | &indie)) { |
| 1747 | /* addr in an inline function */ |
| 1748 | tmp = dwarf_diename(&indie); |
| 1749 | if (!tmp) |
| 1750 | goto end; |
| 1751 | ret = dwarf_decl_line(&indie, &lineno); |
| 1752 | } else { |
| 1753 | if (eaddr == addr) { /* Function entry */ |
| 1754 | lineno = ppt->line; |
| 1755 | ret = 0; |
| 1756 | } else |
| 1757 | ret = dwarf_decl_line(&spdie, &lineno); |
| 1758 | } |
| 1759 | if (ret == 0) { |
| 1760 | /* Make a relative line number */ |
| 1761 | ppt->line -= lineno; |
| 1762 | goto found; |
| 1763 | } |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1764 | } |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1765 | /* We don't have a line number, let's use offset */ |
| 1766 | ppt->offset = addr - (unsigned long)eaddr; |
| 1767 | found: |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1768 | ppt->function = strdup(tmp); |
| 1769 | if (ppt->function == NULL) { |
| 1770 | ret = -ENOMEM; |
| 1771 | goto end; |
| 1772 | } |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1773 | found = true; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | end: |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1777 | if (dwfl) |
| 1778 | dwfl_end(dwfl); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1779 | if (ret >= 0) |
| 1780 | ret = found ? 1 : 0; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1781 | return ret; |
| 1782 | } |
| 1783 | |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1784 | /* Add a line and store the src path */ |
| 1785 | static int line_range_add_line(const char *src, unsigned int lineno, |
| 1786 | struct line_range *lr) |
| 1787 | { |
Masami Hiramatsu | 7cf0b79 | 2010-07-09 18:28:59 +0900 | [diff] [blame] | 1788 | /* Copy source path */ |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1789 | if (!lr->path) { |
Masami Hiramatsu | 7cf0b79 | 2010-07-09 18:28:59 +0900 | [diff] [blame] | 1790 | lr->path = strdup(src); |
| 1791 | if (lr->path == NULL) |
| 1792 | return -ENOMEM; |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1793 | } |
| 1794 | return line_list__add_line(&lr->line_list, lineno); |
| 1795 | } |
| 1796 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1797 | static int line_range_walk_cb(const char *fname, int lineno, |
| 1798 | Dwarf_Addr addr __used, |
| 1799 | void *data) |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1800 | { |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1801 | struct line_finder *lf = data; |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1802 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1803 | if ((strtailcmp(fname, lf->fname) != 0) || |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1804 | (lf->lno_s > lineno || lf->lno_e < lineno)) |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1805 | return 0; |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1806 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1807 | if (line_range_add_line(fname, lineno, lf->lr) < 0) |
| 1808 | return -EINVAL; |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1809 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1810 | return 0; |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1811 | } |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1812 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1813 | /* Find line range from its line number */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1814 | static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1815 | { |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1816 | int ret; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1817 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1818 | ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf); |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1819 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1820 | /* Update status */ |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1821 | if (ret >= 0) |
| 1822 | if (!list_empty(&lf->lr->line_list)) |
| 1823 | ret = lf->found = 1; |
| 1824 | else |
| 1825 | ret = 0; /* Lines are not found */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1826 | else { |
| 1827 | free(lf->lr->path); |
| 1828 | lf->lr->path = NULL; |
| 1829 | } |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1830 | return ret; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1831 | } |
| 1832 | |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 1833 | static int line_range_inline_cb(Dwarf_Die *in_die, void *data) |
| 1834 | { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1835 | struct dwarf_callback_param *param = data; |
| 1836 | |
| 1837 | param->retval = find_line_range_by_line(in_die, param->data); |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 1838 | return DWARF_CB_ABORT; /* No need to find other instances */ |
| 1839 | } |
| 1840 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1841 | /* Search function from function name */ |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1842 | static int line_range_search_cb(Dwarf_Die *sp_die, void *data) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1843 | { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1844 | struct dwarf_callback_param *param = data; |
| 1845 | struct line_finder *lf = param->data; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1846 | struct line_range *lr = lf->lr; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1847 | |
Masami Hiramatsu | 7d21635 | 2011-03-30 18:25:41 +0900 | [diff] [blame] | 1848 | /* Check declared file */ |
| 1849 | if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die))) |
| 1850 | return DWARF_CB_OK; |
| 1851 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1852 | if (dwarf_tag(sp_die) == DW_TAG_subprogram && |
Masami Hiramatsu | 8217563 | 2010-07-09 18:29:17 +0900 | [diff] [blame] | 1853 | die_compare_name(sp_die, lr->function)) { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1854 | lf->fname = dwarf_decl_file(sp_die); |
| 1855 | dwarf_decl_line(sp_die, &lr->offset); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1856 | pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1857 | lf->lno_s = lr->offset + lr->start; |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 1858 | if (lf->lno_s < 0) /* Overflow */ |
| 1859 | lf->lno_s = INT_MAX; |
| 1860 | lf->lno_e = lr->offset + lr->end; |
| 1861 | if (lf->lno_e < 0) /* Overflow */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1862 | lf->lno_e = INT_MAX; |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 1863 | pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1864 | lr->start = lf->lno_s; |
| 1865 | lr->end = lf->lno_e; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1866 | if (dwarf_func_inline(sp_die)) { |
| 1867 | struct dwarf_callback_param _param; |
| 1868 | _param.data = (void *)lf; |
| 1869 | _param.retval = 0; |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 1870 | dwarf_func_inline_instances(sp_die, |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1871 | line_range_inline_cb, |
| 1872 | &_param); |
| 1873 | param->retval = _param.retval; |
| 1874 | } else |
| 1875 | param->retval = find_line_range_by_line(sp_die, lf); |
| 1876 | return DWARF_CB_ABORT; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1877 | } |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1878 | return DWARF_CB_OK; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1879 | } |
| 1880 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1881 | static int find_line_range_by_func(struct line_finder *lf) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1882 | { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1883 | struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0}; |
| 1884 | dwarf_getfuncs(&lf->cu_die, line_range_search_cb, ¶m, 0); |
| 1885 | return param.retval; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | int find_line_range(int fd, struct line_range *lr) |
| 1889 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1890 | struct line_finder lf = {.lr = lr, .found = 0}; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1891 | int ret = 0; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1892 | Dwarf_Off off = 0, noff; |
| 1893 | size_t cuhl; |
| 1894 | Dwarf_Die *diep; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1895 | Dwarf *dbg = NULL; |
| 1896 | Dwfl *dwfl; |
| 1897 | Dwarf_Addr bias; /* Currently ignored */ |
Masami Hiramatsu | 6a330a3 | 2010-07-09 18:29:11 +0900 | [diff] [blame] | 1898 | const char *comp_dir; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1899 | |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1900 | dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1901 | if (!dbg) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1902 | pr_warning("No debug information found in the vmlinux - " |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1903 | "please rebuild with CONFIG_DEBUG_INFO=y.\n"); |
Masami Hiramatsu | f0c4801 | 2011-03-30 18:25:47 +0900 | [diff] [blame] | 1904 | close(fd); /* Without dwfl_end(), fd isn't closed. */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1905 | return -EBADF; |
| 1906 | } |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1907 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1908 | /* Loop on CUs (Compilation Unit) */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1909 | while (!lf.found && ret >= 0) { |
| 1910 | if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1911 | break; |
| 1912 | |
| 1913 | /* Get the DIE(Debugging Information Entry) of this CU */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1914 | diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die); |
| 1915 | if (!diep) |
| 1916 | continue; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1917 | |
| 1918 | /* Check if target file is included. */ |
| 1919 | if (lr->file) |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1920 | lf.fname = cu_find_realpath(&lf.cu_die, lr->file); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1921 | else |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1922 | lf.fname = 0; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1923 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1924 | if (!lr->file || lf.fname) { |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1925 | if (lr->function) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1926 | ret = find_line_range_by_func(&lf); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1927 | else { |
| 1928 | lf.lno_s = lr->start; |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 1929 | lf.lno_e = lr->end; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1930 | ret = find_line_range_by_line(NULL, &lf); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1931 | } |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1932 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1933 | off = noff; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1934 | } |
Masami Hiramatsu | 6a330a3 | 2010-07-09 18:29:11 +0900 | [diff] [blame] | 1935 | |
| 1936 | /* Store comp_dir */ |
| 1937 | if (lf.found) { |
| 1938 | comp_dir = cu_get_comp_dir(&lf.cu_die); |
| 1939 | if (comp_dir) { |
| 1940 | lr->comp_dir = strdup(comp_dir); |
| 1941 | if (!lr->comp_dir) |
| 1942 | ret = -ENOMEM; |
| 1943 | } |
| 1944 | } |
| 1945 | |
Masami Hiramatsu | 7cf0b79 | 2010-07-09 18:28:59 +0900 | [diff] [blame] | 1946 | pr_debug("path: %s\n", lr->path); |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 1947 | dwfl_end(dwfl); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1948 | return (ret < 0) ? ret : lf.found; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1949 | } |
| 1950 | |