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> |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 34 | |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 35 | #include "event.h" |
| 36 | #include "debug.h" |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 37 | #include "util.h" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 38 | #include "probe-finder.h" |
| 39 | |
| 40 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 41 | /* |
| 42 | * Generic dwarf analysis helpers |
| 43 | */ |
| 44 | |
| 45 | #define X86_32_MAX_REGS 8 |
| 46 | const char *x86_32_regs_table[X86_32_MAX_REGS] = { |
| 47 | "%ax", |
| 48 | "%cx", |
| 49 | "%dx", |
| 50 | "%bx", |
| 51 | "$stack", /* Stack address instead of %sp */ |
| 52 | "%bp", |
| 53 | "%si", |
| 54 | "%di", |
| 55 | }; |
| 56 | |
| 57 | #define X86_64_MAX_REGS 16 |
| 58 | const char *x86_64_regs_table[X86_64_MAX_REGS] = { |
| 59 | "%ax", |
| 60 | "%dx", |
| 61 | "%cx", |
| 62 | "%bx", |
| 63 | "%si", |
| 64 | "%di", |
| 65 | "%bp", |
| 66 | "%sp", |
| 67 | "%r8", |
| 68 | "%r9", |
| 69 | "%r10", |
| 70 | "%r11", |
| 71 | "%r12", |
| 72 | "%r13", |
| 73 | "%r14", |
| 74 | "%r15", |
| 75 | }; |
| 76 | |
| 77 | /* TODO: switching by dwarf address size */ |
| 78 | #ifdef __x86_64__ |
| 79 | #define ARCH_MAX_REGS X86_64_MAX_REGS |
| 80 | #define arch_regs_table x86_64_regs_table |
| 81 | #else |
| 82 | #define ARCH_MAX_REGS X86_32_MAX_REGS |
| 83 | #define arch_regs_table x86_32_regs_table |
| 84 | #endif |
| 85 | |
| 86 | /* Return architecture dependent register string (for kprobe-tracer) */ |
| 87 | static const char *get_arch_regstr(unsigned int n) |
| 88 | { |
| 89 | return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Compare the tail of two strings. |
| 94 | * Return 0 if whole of either string is same as another's tail part. |
| 95 | */ |
| 96 | static int strtailcmp(const char *s1, const char *s2) |
| 97 | { |
| 98 | int i1 = strlen(s1); |
| 99 | int i2 = strlen(s2); |
Juha Leppanen | d56728b | 2009-12-07 12:00:40 -0500 | [diff] [blame] | 100 | while (--i1 >= 0 && --i2 >= 0) { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 101 | if (s1[i1] != s2[i2]) |
| 102 | return s1[i1] - s2[i2]; |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* Find the fileno of the target file. */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 108 | static int cu_find_fileno(Dwarf_Die *cu_die, const char *fname) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 109 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 110 | Dwarf_Files *files; |
| 111 | size_t nfiles, i; |
| 112 | const char *src; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 113 | int ret; |
| 114 | |
| 115 | if (!fname) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 116 | return -EINVAL; |
| 117 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 118 | ret = dwarf_getsrcfiles(cu_die, &files, &nfiles); |
| 119 | if (ret == 0) { |
| 120 | for (i = 0; i < nfiles; i++) { |
| 121 | src = dwarf_filesrc(files, i, NULL, NULL); |
| 122 | if (strtailcmp(src, fname) == 0) { |
| 123 | ret = (int)i; /*???: +1 or not?*/ |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | if (ret) |
| 128 | pr_debug("found fno: %d\n", ret); |
| 129 | } |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 130 | return ret; |
| 131 | } |
| 132 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 133 | struct __addr_die_search_param { |
| 134 | Dwarf_Addr addr; |
| 135 | Dwarf_Die *die_mem; |
| 136 | }; |
| 137 | |
| 138 | static int __die_search_func_cb(Dwarf_Die *fn_die, void *data) |
| 139 | { |
| 140 | struct __addr_die_search_param *ad = data; |
| 141 | |
| 142 | if (dwarf_tag(fn_die) == DW_TAG_subprogram && |
| 143 | dwarf_haspc(fn_die, ad->addr)) { |
| 144 | memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die)); |
| 145 | return DWARF_CB_ABORT; |
| 146 | } |
| 147 | return DWARF_CB_OK; |
| 148 | } |
| 149 | |
| 150 | /* Search a real subprogram including this line, */ |
| 151 | static Dwarf_Die *die_get_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr, |
| 152 | Dwarf_Die *die_mem) |
| 153 | { |
| 154 | struct __addr_die_search_param ad; |
| 155 | ad.addr = addr; |
| 156 | ad.die_mem = die_mem; |
| 157 | /* dwarf_getscopes can't find subprogram. */ |
| 158 | if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0)) |
| 159 | return NULL; |
| 160 | else |
| 161 | return die_mem; |
| 162 | } |
| 163 | |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame^] | 164 | /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */ |
| 165 | static Dwarf_Die *die_get_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr, |
| 166 | Dwarf_Die *die_mem) |
| 167 | { |
| 168 | Dwarf_Die child_die; |
| 169 | int ret; |
| 170 | |
| 171 | ret = dwarf_child(sp_die, die_mem); |
| 172 | if (ret != 0) |
| 173 | return NULL; |
| 174 | |
| 175 | do { |
| 176 | if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine && |
| 177 | dwarf_haspc(die_mem, addr)) |
| 178 | return die_mem; |
| 179 | |
| 180 | if (die_get_inlinefunc(die_mem, addr, &child_die)) { |
| 181 | memcpy(die_mem, &child_die, sizeof(Dwarf_Die)); |
| 182 | return die_mem; |
| 183 | } |
| 184 | } while (dwarf_siblingof(die_mem, die_mem) == 0); |
| 185 | |
| 186 | return NULL; |
| 187 | } |
| 188 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 189 | /* Compare diename and tname */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 190 | static bool die_compare_name(Dwarf_Die *dw_die, const char *tname) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 191 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 192 | const char *name; |
| 193 | name = dwarf_diename(dw_die); |
| 194 | DIE_IF(name == NULL); |
| 195 | return strcmp(tname, name); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 198 | /* Get entry pc(or low pc, 1st entry of ranges) of the die */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 199 | static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 200 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 201 | Dwarf_Addr epc; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 202 | int ret; |
| 203 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 204 | ret = dwarf_entrypc(dw_die, &epc); |
| 205 | DIE_IF(ret == -1); |
| 206 | return epc; |
| 207 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 208 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 209 | /* Get a variable die */ |
| 210 | static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name, |
| 211 | Dwarf_Die *die_mem) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 212 | { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 213 | Dwarf_Die child_die; |
| 214 | int tag; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 215 | int ret; |
| 216 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 217 | ret = dwarf_child(sp_die, die_mem); |
| 218 | if (ret != 0) |
| 219 | return NULL; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 220 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 221 | do { |
| 222 | tag = dwarf_tag(die_mem); |
| 223 | if ((tag == DW_TAG_formal_parameter || |
| 224 | tag == DW_TAG_variable) && |
| 225 | (die_compare_name(die_mem, name) == 0)) |
| 226 | return die_mem; |
| 227 | |
| 228 | if (die_find_variable(die_mem, name, &child_die)) { |
| 229 | memcpy(die_mem, &child_die, sizeof(Dwarf_Die)); |
| 230 | return die_mem; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 231 | } |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 232 | } while (dwarf_siblingof(die_mem, die_mem) == 0); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 233 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 234 | return NULL; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 235 | } |
| 236 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 237 | /* |
| 238 | * Probe finder related functions |
| 239 | */ |
| 240 | |
| 241 | /* Show a location */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 242 | static void show_location(Dwarf_Op *op, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 243 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 244 | unsigned int regn; |
| 245 | Dwarf_Word offs = 0; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 246 | int deref = 0, ret; |
| 247 | const char *regs; |
| 248 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 249 | /* TODO: support CFA */ |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 250 | /* If this is based on frame buffer, set the offset */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 251 | if (op->atom == DW_OP_fbreg) { |
| 252 | if (pf->fb_ops == NULL) |
| 253 | die("The attribute of frame base is not supported.\n"); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 254 | deref = 1; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 255 | offs = op->number; |
| 256 | op = &pf->fb_ops[0]; |
| 257 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 258 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 259 | if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) { |
| 260 | regn = op->atom - DW_OP_breg0; |
| 261 | offs += op->number; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 262 | deref = 1; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 263 | } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) { |
| 264 | regn = op->atom - DW_OP_reg0; |
| 265 | } else if (op->atom == DW_OP_bregx) { |
| 266 | regn = op->number; |
| 267 | offs += op->number2; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 268 | deref = 1; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 269 | } else if (op->atom == DW_OP_regx) { |
| 270 | regn = op->number; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 271 | } else |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 272 | die("DW_OP %d is not supported.", op->atom); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 273 | |
| 274 | regs = get_arch_regstr(regn); |
| 275 | if (!regs) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 276 | die("%u exceeds max register number.", regn); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 277 | |
| 278 | if (deref) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 279 | ret = snprintf(pf->buf, pf->len, " %s=+%ju(%s)", |
| 280 | pf->var, (uintmax_t)offs, regs); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 281 | else |
| 282 | ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs); |
Masami Hiramatsu | 9769833 | 2009-10-16 20:08:18 -0400 | [diff] [blame] | 283 | DIE_IF(ret < 0); |
| 284 | DIE_IF(ret >= pf->len); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* Show a variables in kprobe event format */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 288 | static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 289 | { |
| 290 | Dwarf_Attribute attr; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 291 | Dwarf_Op *expr; |
| 292 | size_t nexpr; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 293 | int ret; |
| 294 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 295 | if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 296 | goto error; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 297 | /* TODO: handle more than 1 exprs */ |
| 298 | ret = dwarf_getlocation_addr(&attr, (pf->addr - pf->cu_base), |
| 299 | &expr, &nexpr, 1); |
| 300 | if (ret <= 0 || nexpr == 0) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 301 | goto error; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 302 | |
| 303 | show_location(expr, pf); |
| 304 | /* *expr will be cached in libdw. Don't free it. */ |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 305 | return ; |
| 306 | error: |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 307 | /* TODO: Support const_value */ |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 308 | die("Failed to find the location of %s at this address.\n" |
Masami Hiramatsu | bbaa46f | 2010-01-05 17:47:03 -0500 | [diff] [blame] | 309 | " Perhaps, it has been optimized out.", pf->var); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 310 | } |
| 311 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 312 | /* Find a variable in a subprogram die */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 313 | static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 314 | { |
| 315 | int ret; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 316 | Dwarf_Die vr_die; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 317 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 318 | /* TODO: Support struct members and arrays */ |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 319 | if (!is_c_varname(pf->var)) { |
| 320 | /* Output raw parameters */ |
| 321 | ret = snprintf(pf->buf, pf->len, " %s", pf->var); |
Masami Hiramatsu | 9769833 | 2009-10-16 20:08:18 -0400 | [diff] [blame] | 322 | DIE_IF(ret < 0); |
| 323 | DIE_IF(ret >= pf->len); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 324 | return ; |
| 325 | } |
| 326 | |
Arnaldo Carvalho de Melo | b7cb10e | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 327 | pr_debug("Searching '%s' variable in context.\n", pf->var); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 328 | /* Search child die for local variables and parameters. */ |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 329 | if (!die_find_variable(sp_die, pf->var, &vr_die)) |
Masami Hiramatsu | bbaa46f | 2010-01-05 17:47:03 -0500 | [diff] [blame] | 330 | die("Failed to find '%s' in this function.", pf->var); |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 331 | |
| 332 | show_variable(&vr_die, pf); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 333 | } |
| 334 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 335 | /* Show a probe point to output buffer */ |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 336 | static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 337 | { |
| 338 | struct probe_point *pp = pf->pp; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 339 | Dwarf_Addr eaddr; |
| 340 | Dwarf_Die die_mem; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 341 | const char *name; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 342 | char tmp[MAX_PROBE_BUFFER]; |
| 343 | int ret, i, len; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 344 | Dwarf_Attribute fb_attr; |
| 345 | size_t nops; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 346 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 347 | /* If no real subprogram, find a real one */ |
| 348 | if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) { |
| 349 | sp_die = die_get_real_subprogram(&pf->cu_die, |
| 350 | pf->addr, &die_mem); |
| 351 | if (!sp_die) |
| 352 | die("Probe point is not found in subprograms."); |
| 353 | } |
| 354 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 355 | /* Output name of probe point */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 356 | name = dwarf_diename(sp_die); |
| 357 | if (name) { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 358 | dwarf_entrypc(sp_die, &eaddr); |
| 359 | ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%lu", name, |
| 360 | (unsigned long)(pf->addr - eaddr)); |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 361 | /* Copy the function name if possible */ |
| 362 | if (!pp->function) { |
| 363 | pp->function = strdup(name); |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 364 | pp->offset = (size_t)(pf->addr - eaddr); |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 365 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 366 | } else { |
| 367 | /* This function has no name. */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 368 | ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx", |
| 369 | (uintmax_t)pf->addr); |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 370 | if (!pp->function) { |
| 371 | /* TODO: Use _stext */ |
| 372 | pp->function = strdup(""); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 373 | pp->offset = (size_t)pf->addr; |
Masami Hiramatsu | 253977b | 2009-10-27 16:43:10 -0400 | [diff] [blame] | 374 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 375 | } |
Masami Hiramatsu | 9769833 | 2009-10-16 20:08:18 -0400 | [diff] [blame] | 376 | DIE_IF(ret < 0); |
| 377 | DIE_IF(ret >= MAX_PROBE_BUFFER); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 378 | len = ret; |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 379 | pr_debug("Probe point found: %s\n", tmp); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 380 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 381 | /* Get the frame base attribute/ops */ |
| 382 | dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr); |
| 383 | ret = dwarf_getlocation_addr(&fb_attr, (pf->addr - pf->cu_base), |
| 384 | &pf->fb_ops, &nops, 1); |
| 385 | if (ret <= 0 || nops == 0) |
| 386 | pf->fb_ops = NULL; |
| 387 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 388 | /* Find each argument */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 389 | /* TODO: use dwarf_cfi_addrframe */ |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 390 | for (i = 0; i < pp->nr_args; i++) { |
| 391 | pf->var = pp->args[i]; |
| 392 | pf->buf = &tmp[len]; |
| 393 | pf->len = MAX_PROBE_BUFFER - len; |
| 394 | find_variable(sp_die, pf); |
| 395 | len += strlen(pf->buf); |
| 396 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 397 | |
| 398 | /* *pf->fb_ops will be cached in libdw. Don't free it. */ |
| 399 | pf->fb_ops = NULL; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 400 | |
| 401 | pp->probes[pp->found] = strdup(tmp); |
| 402 | pp->found++; |
| 403 | } |
| 404 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 405 | /* Find probe point from its line number */ |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 406 | static void find_probe_point_by_line(struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 407 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 408 | Dwarf_Lines *lines; |
| 409 | Dwarf_Line *line; |
| 410 | size_t nlines, i; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 411 | Dwarf_Addr addr; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 412 | int lineno; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 413 | int ret; |
| 414 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 415 | ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines); |
| 416 | DIE_IF(ret != 0); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 417 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 418 | for (i = 0; i < nlines; i++) { |
| 419 | line = dwarf_onesrcline(lines, i); |
| 420 | dwarf_lineno(line, &lineno); |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 421 | if (lineno != pf->lno) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 422 | continue; |
| 423 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 424 | /* TODO: Get fileno from line, but how? */ |
| 425 | if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0) |
| 426 | continue; |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 427 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 428 | ret = dwarf_lineaddr(line, &addr); |
| 429 | DIE_IF(ret != 0); |
| 430 | pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n", |
| 431 | (int)i, lineno, (uintmax_t)addr); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 432 | pf->addr = addr; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 433 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 434 | show_probe_point(NULL, pf); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 435 | /* Continuing, because target line might be inlined. */ |
| 436 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 437 | } |
| 438 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 439 | static int probe_point_inline_cb(Dwarf_Die *in_die, void *data) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 440 | { |
| 441 | struct probe_finder *pf = (struct probe_finder *)data; |
| 442 | struct probe_point *pp = pf->pp; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 443 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 444 | /* Get probe address */ |
| 445 | pf->addr = die_get_entrypc(in_die); |
| 446 | pf->addr += pp->offset; |
| 447 | pr_debug("found inline addr: 0x%jx\n", (uintmax_t)pf->addr); |
| 448 | |
| 449 | show_probe_point(in_die, pf); |
| 450 | return DWARF_CB_OK; |
| 451 | } |
| 452 | |
| 453 | /* Search function from function name */ |
| 454 | static int probe_point_search_cb(Dwarf_Die *sp_die, void *data) |
| 455 | { |
| 456 | struct probe_finder *pf = (struct probe_finder *)data; |
| 457 | struct probe_point *pp = pf->pp; |
| 458 | |
| 459 | /* Check tag and diename */ |
| 460 | if (dwarf_tag(sp_die) != DW_TAG_subprogram || |
| 461 | die_compare_name(sp_die, pp->function) != 0) |
| 462 | return 0; |
| 463 | |
| 464 | if (pp->line) { /* Function relative line */ |
| 465 | pf->fname = dwarf_decl_file(sp_die); |
| 466 | dwarf_decl_line(sp_die, &pf->lno); |
| 467 | pf->lno += pp->line; |
| 468 | find_probe_point_by_line(pf); |
| 469 | } else if (!dwarf_func_inline(sp_die)) { |
| 470 | /* Real function */ |
| 471 | pf->addr = die_get_entrypc(sp_die); |
| 472 | pf->addr += pp->offset; |
| 473 | /* TODO: Check the address in this function */ |
| 474 | show_probe_point(sp_die, pf); |
| 475 | } else |
| 476 | /* Inlined function: search instances */ |
| 477 | dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf); |
| 478 | |
| 479 | return 1; /* Exit; no same symbol in this CU. */ |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 480 | } |
| 481 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 482 | static void find_probe_point_by_func(struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 483 | { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 484 | dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* Find a probe point */ |
Masami Hiramatsu | 81cb8aa | 2010-02-25 08:35:34 -0500 | [diff] [blame] | 488 | int find_probe_point(int fd, struct probe_point *pp) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 489 | { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 490 | struct probe_finder pf = {.pp = pp}; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 491 | int ret; |
| 492 | Dwarf_Off off, noff; |
| 493 | size_t cuhl; |
| 494 | Dwarf_Die *diep; |
| 495 | Dwarf *dbg; |
| 496 | int fno = 0; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 497 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 498 | dbg = dwarf_begin(fd, DWARF_C_READ); |
| 499 | if (!dbg) |
Masami Hiramatsu | a225a1d | 2009-11-03 19:12:30 -0500 | [diff] [blame] | 500 | return -ENOENT; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 501 | |
| 502 | pp->found = 0; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 503 | off = 0; |
| 504 | /* Loop on CUs (Compilation Unit) */ |
| 505 | while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 506 | /* Get the DIE(Debugging Information Entry) of this CU */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 507 | diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die); |
| 508 | if (!diep) |
| 509 | continue; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 510 | |
| 511 | /* Check if target file is included. */ |
| 512 | if (pp->file) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 513 | fno = cu_find_fileno(&pf.cu_die, pp->file); |
| 514 | else |
| 515 | fno = 0; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 516 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 517 | if (!pp->file || fno) { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 518 | /* Save CU base address (for frame_base) */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 519 | ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base); |
| 520 | if (ret != 0) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 521 | pf.cu_base = 0; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 522 | if (pp->function) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 523 | find_probe_point_by_func(&pf); |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 524 | else { |
| 525 | pf.lno = pp->line; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 526 | find_probe_point_by_line(&pf); |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 527 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 528 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 529 | off = noff; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 530 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 531 | dwarf_end(dbg); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 532 | |
| 533 | return pp->found; |
| 534 | } |
| 535 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 536 | |
| 537 | static void line_range_add_line(struct line_range *lr, unsigned int line) |
| 538 | { |
| 539 | struct line_node *ln; |
| 540 | struct list_head *p; |
| 541 | |
| 542 | /* Reverse search, because new line will be the last one */ |
| 543 | list_for_each_entry_reverse(ln, &lr->line_list, list) { |
| 544 | if (ln->line < line) { |
| 545 | p = &ln->list; |
| 546 | goto found; |
| 547 | } else if (ln->line == line) /* Already exist */ |
| 548 | return ; |
| 549 | } |
| 550 | /* List is empty, or the smallest entry */ |
| 551 | p = &lr->line_list; |
| 552 | found: |
| 553 | pr_debug("Debug: add a line %u\n", line); |
| 554 | ln = zalloc(sizeof(struct line_node)); |
| 555 | DIE_IF(ln == NULL); |
| 556 | ln->line = line; |
| 557 | INIT_LIST_HEAD(&ln->list); |
| 558 | list_add(&ln->list, p); |
| 559 | } |
| 560 | |
| 561 | /* Find line range from its line number */ |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame^] | 562 | static void 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] | 563 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 564 | Dwarf_Lines *lines; |
| 565 | Dwarf_Line *line; |
| 566 | size_t nlines, i; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 567 | Dwarf_Addr addr; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 568 | int lineno; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 569 | int ret; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 570 | const char *src; |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame^] | 571 | Dwarf_Die die_mem; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 572 | |
Masami Hiramatsu | 3cb8bc6 | 2010-02-25 08:35:27 -0500 | [diff] [blame] | 573 | INIT_LIST_HEAD(&lf->lr->line_list); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 574 | ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines); |
| 575 | DIE_IF(ret != 0); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 576 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 577 | for (i = 0; i < nlines; i++) { |
| 578 | line = dwarf_onesrcline(lines, i); |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame^] | 579 | ret = dwarf_lineno(line, &lineno); |
| 580 | DIE_IF(ret != 0); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 581 | if (lf->lno_s > lineno || lf->lno_e < lineno) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 582 | continue; |
| 583 | |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame^] | 584 | if (sp_die) { |
| 585 | /* Address filtering 1: does sp_die include addr? */ |
| 586 | ret = dwarf_lineaddr(line, &addr); |
| 587 | DIE_IF(ret != 0); |
| 588 | if (!dwarf_haspc(sp_die, addr)) |
| 589 | continue; |
| 590 | |
| 591 | /* Address filtering 2: No child include addr? */ |
| 592 | if (die_get_inlinefunc(sp_die, addr, &die_mem)) |
| 593 | continue; |
| 594 | } |
| 595 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 596 | /* TODO: Get fileno from line, but how? */ |
| 597 | src = dwarf_linesrc(line, NULL, NULL); |
| 598 | if (strtailcmp(src, lf->fname) != 0) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 599 | continue; |
| 600 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 601 | /* Copy real path */ |
| 602 | if (!lf->lr->path) |
| 603 | lf->lr->path = strdup(src); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 604 | line_range_add_line(lf->lr, (unsigned int)lineno); |
| 605 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 606 | /* Update status */ |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 607 | if (!list_empty(&lf->lr->line_list)) |
| 608 | lf->found = 1; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 609 | else { |
| 610 | free(lf->lr->path); |
| 611 | lf->lr->path = NULL; |
| 612 | } |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 613 | } |
| 614 | |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame^] | 615 | static int line_range_inline_cb(Dwarf_Die *in_die, void *data) |
| 616 | { |
| 617 | find_line_range_by_line(in_die, (struct line_finder *)data); |
| 618 | return DWARF_CB_ABORT; /* No need to find other instances */ |
| 619 | } |
| 620 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 621 | /* Search function from function name */ |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 622 | static int line_range_search_cb(Dwarf_Die *sp_die, void *data) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 623 | { |
| 624 | struct line_finder *lf = (struct line_finder *)data; |
| 625 | struct line_range *lr = lf->lr; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 626 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 627 | if (dwarf_tag(sp_die) == DW_TAG_subprogram && |
| 628 | die_compare_name(sp_die, lr->function) == 0) { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 629 | lf->fname = dwarf_decl_file(sp_die); |
| 630 | dwarf_decl_line(sp_die, &lr->offset); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 631 | pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 632 | lf->lno_s = lr->offset + lr->start; |
| 633 | if (!lr->end) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 634 | lf->lno_e = INT_MAX; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 635 | else |
| 636 | lf->lno_e = lr->offset + lr->end; |
| 637 | lr->start = lf->lno_s; |
| 638 | lr->end = lf->lno_e; |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame^] | 639 | if (dwarf_func_inline(sp_die)) |
| 640 | dwarf_func_inline_instances(sp_die, |
| 641 | line_range_inline_cb, lf); |
| 642 | else |
| 643 | find_line_range_by_line(sp_die, lf); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 644 | return 1; |
| 645 | } |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | static void find_line_range_by_func(struct line_finder *lf) |
| 650 | { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 651 | dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | int find_line_range(int fd, struct line_range *lr) |
| 655 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 656 | struct line_finder lf = {.lr = lr, .found = 0}; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 657 | int ret; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 658 | Dwarf_Off off = 0, noff; |
| 659 | size_t cuhl; |
| 660 | Dwarf_Die *diep; |
| 661 | Dwarf *dbg; |
| 662 | int fno; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 663 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 664 | dbg = dwarf_begin(fd, DWARF_C_READ); |
| 665 | if (!dbg) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 666 | return -ENOENT; |
| 667 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 668 | /* Loop on CUs (Compilation Unit) */ |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 669 | while (!lf.found) { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 670 | ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL); |
| 671 | if (ret != 0) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 672 | break; |
| 673 | |
| 674 | /* Get the DIE(Debugging Information Entry) of this CU */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 675 | diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die); |
| 676 | if (!diep) |
| 677 | continue; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 678 | |
| 679 | /* Check if target file is included. */ |
| 680 | if (lr->file) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 681 | fno = cu_find_fileno(&lf.cu_die, lr->file); |
| 682 | else |
| 683 | fno = 0; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 684 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 685 | if (!lr->file || fno) { |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 686 | if (lr->function) |
| 687 | find_line_range_by_func(&lf); |
| 688 | else { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 689 | lf.fname = lr->file; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 690 | lf.lno_s = lr->start; |
| 691 | if (!lr->end) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 692 | lf.lno_e = INT_MAX; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 693 | else |
| 694 | lf.lno_e = lr->end; |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame^] | 695 | find_line_range_by_line(NULL, &lf); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 696 | } |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 697 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 698 | off = noff; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 699 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 700 | pr_debug("path: %lx\n", (unsigned long)lr->path); |
| 701 | dwarf_end(dbg); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 702 | return lf.found; |
| 703 | } |
| 704 | |