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