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 | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 35 | #include "string.h" |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 36 | #include "event.h" |
| 37 | #include "debug.h" |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 38 | #include "util.h" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 39 | #include "probe-finder.h" |
| 40 | |
| 41 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 42 | /* |
| 43 | * Generic dwarf analysis helpers |
| 44 | */ |
| 45 | |
| 46 | #define X86_32_MAX_REGS 8 |
| 47 | const char *x86_32_regs_table[X86_32_MAX_REGS] = { |
| 48 | "%ax", |
| 49 | "%cx", |
| 50 | "%dx", |
| 51 | "%bx", |
| 52 | "$stack", /* Stack address instead of %sp */ |
| 53 | "%bp", |
| 54 | "%si", |
| 55 | "%di", |
| 56 | }; |
| 57 | |
| 58 | #define X86_64_MAX_REGS 16 |
| 59 | const char *x86_64_regs_table[X86_64_MAX_REGS] = { |
| 60 | "%ax", |
| 61 | "%dx", |
| 62 | "%cx", |
| 63 | "%bx", |
| 64 | "%si", |
| 65 | "%di", |
| 66 | "%bp", |
| 67 | "%sp", |
| 68 | "%r8", |
| 69 | "%r9", |
| 70 | "%r10", |
| 71 | "%r11", |
| 72 | "%r12", |
| 73 | "%r13", |
| 74 | "%r14", |
| 75 | "%r15", |
| 76 | }; |
| 77 | |
| 78 | /* TODO: switching by dwarf address size */ |
| 79 | #ifdef __x86_64__ |
| 80 | #define ARCH_MAX_REGS X86_64_MAX_REGS |
| 81 | #define arch_regs_table x86_64_regs_table |
| 82 | #else |
| 83 | #define ARCH_MAX_REGS X86_32_MAX_REGS |
| 84 | #define arch_regs_table x86_32_regs_table |
| 85 | #endif |
| 86 | |
| 87 | /* Return architecture dependent register string (for kprobe-tracer) */ |
| 88 | static const char *get_arch_regstr(unsigned int n) |
| 89 | { |
| 90 | return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Compare the tail of two strings. |
| 95 | * Return 0 if whole of either string is same as another's tail part. |
| 96 | */ |
| 97 | static int strtailcmp(const char *s1, const char *s2) |
| 98 | { |
| 99 | int i1 = strlen(s1); |
| 100 | int i2 = strlen(s2); |
Juha Leppanen | d56728b | 2009-12-07 12:00:40 -0500 | [diff] [blame] | 101 | while (--i1 >= 0 && --i2 >= 0) { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 102 | if (s1[i1] != s2[i2]) |
| 103 | return s1[i1] - s2[i2]; |
| 104 | } |
| 105 | return 0; |
| 106 | } |
| 107 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 108 | /* Line number list operations */ |
| 109 | |
| 110 | /* Add a line to line number list */ |
| 111 | static void line_list__add_line(struct list_head *head, unsigned int line) |
| 112 | { |
| 113 | struct line_node *ln; |
| 114 | struct list_head *p; |
| 115 | |
| 116 | /* Reverse search, because new line will be the last one */ |
| 117 | list_for_each_entry_reverse(ln, head, list) { |
| 118 | if (ln->line < line) { |
| 119 | p = &ln->list; |
| 120 | goto found; |
| 121 | } else if (ln->line == line) /* Already exist */ |
| 122 | return ; |
| 123 | } |
| 124 | /* List is empty, or the smallest entry */ |
| 125 | p = head; |
| 126 | found: |
| 127 | pr_debug("line list: add a line %u\n", line); |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 128 | ln = xzalloc(sizeof(struct line_node)); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 129 | ln->line = line; |
| 130 | INIT_LIST_HEAD(&ln->list); |
| 131 | list_add(&ln->list, p); |
| 132 | } |
| 133 | |
| 134 | /* Check if the line in line number list */ |
| 135 | static int line_list__has_line(struct list_head *head, unsigned int line) |
| 136 | { |
| 137 | struct line_node *ln; |
| 138 | |
| 139 | /* Reverse search, because new line will be the last one */ |
| 140 | list_for_each_entry(ln, head, list) |
| 141 | if (ln->line == line) |
| 142 | return 1; |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* Init line number list */ |
| 148 | static void line_list__init(struct list_head *head) |
| 149 | { |
| 150 | INIT_LIST_HEAD(head); |
| 151 | } |
| 152 | |
| 153 | /* Free line number list */ |
| 154 | static void line_list__free(struct list_head *head) |
| 155 | { |
| 156 | struct line_node *ln; |
| 157 | while (!list_empty(head)) { |
| 158 | ln = list_first_entry(head, struct line_node, list); |
| 159 | list_del(&ln->list); |
| 160 | free(ln); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /* Dwarf wrappers */ |
| 165 | |
| 166 | /* Find the realpath of the target file. */ |
| 167 | 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] | 168 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 169 | Dwarf_Files *files; |
| 170 | size_t nfiles, i; |
Arnaldo Carvalho de Melo | accd3cc | 2010-03-05 12:51:04 -0300 | [diff] [blame] | 171 | const char *src = NULL; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 172 | int ret; |
| 173 | |
| 174 | if (!fname) |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 175 | return NULL; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 176 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 177 | ret = dwarf_getsrcfiles(cu_die, &files, &nfiles); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 178 | if (ret != 0) |
| 179 | return NULL; |
| 180 | |
| 181 | for (i = 0; i < nfiles; i++) { |
| 182 | src = dwarf_filesrc(files, i, NULL, NULL); |
| 183 | if (strtailcmp(src, fname) == 0) |
| 184 | break; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 185 | } |
Masami Hiramatsu | c9e3858 | 2010-04-02 12:50:45 -0400 | [diff] [blame] | 186 | if (i == nfiles) |
| 187 | return NULL; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 188 | return src; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 191 | /* Compare diename and tname */ |
| 192 | static bool die_compare_name(Dwarf_Die *dw_die, const char *tname) |
| 193 | { |
| 194 | const char *name; |
| 195 | name = dwarf_diename(dw_die); |
| 196 | DIE_IF(name == NULL); |
| 197 | return strcmp(tname, name); |
| 198 | } |
| 199 | |
| 200 | /* Get entry pc(or low pc, 1st entry of ranges) of the die */ |
| 201 | static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die) |
| 202 | { |
| 203 | Dwarf_Addr epc; |
| 204 | int ret; |
| 205 | |
| 206 | ret = dwarf_entrypc(dw_die, &epc); |
| 207 | DIE_IF(ret == -1); |
| 208 | return epc; |
| 209 | } |
| 210 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 211 | /* Get type die, but skip qualifiers and typedef */ |
| 212 | static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) |
| 213 | { |
| 214 | Dwarf_Attribute attr; |
| 215 | int tag; |
| 216 | |
| 217 | do { |
| 218 | if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL || |
| 219 | dwarf_formref_die(&attr, die_mem) == NULL) |
| 220 | return NULL; |
| 221 | |
| 222 | tag = dwarf_tag(die_mem); |
| 223 | vr_die = die_mem; |
| 224 | } while (tag == DW_TAG_const_type || |
| 225 | tag == DW_TAG_restrict_type || |
| 226 | tag == DW_TAG_volatile_type || |
| 227 | tag == DW_TAG_shared_type || |
| 228 | tag == DW_TAG_typedef); |
| 229 | |
| 230 | return die_mem; |
| 231 | } |
| 232 | |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 233 | /* Return values for die_find callbacks */ |
| 234 | enum { |
| 235 | DIE_FIND_CB_FOUND = 0, /* End of Search */ |
| 236 | DIE_FIND_CB_CHILD = 1, /* Search only children */ |
| 237 | DIE_FIND_CB_SIBLING = 2, /* Search only siblings */ |
| 238 | DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */ |
| 239 | }; |
| 240 | |
| 241 | /* Search a child die */ |
| 242 | static Dwarf_Die *die_find_child(Dwarf_Die *rt_die, |
| 243 | int (*callback)(Dwarf_Die *, void *), |
| 244 | void *data, Dwarf_Die *die_mem) |
| 245 | { |
| 246 | Dwarf_Die child_die; |
| 247 | int ret; |
| 248 | |
| 249 | ret = dwarf_child(rt_die, die_mem); |
| 250 | if (ret != 0) |
| 251 | return NULL; |
| 252 | |
| 253 | do { |
| 254 | ret = callback(die_mem, data); |
| 255 | if (ret == DIE_FIND_CB_FOUND) |
| 256 | return die_mem; |
| 257 | |
| 258 | if ((ret & DIE_FIND_CB_CHILD) && |
| 259 | die_find_child(die_mem, callback, data, &child_die)) { |
| 260 | memcpy(die_mem, &child_die, sizeof(Dwarf_Die)); |
| 261 | return die_mem; |
| 262 | } |
| 263 | } while ((ret & DIE_FIND_CB_SIBLING) && |
| 264 | dwarf_siblingof(die_mem, die_mem) == 0); |
| 265 | |
| 266 | return NULL; |
| 267 | } |
| 268 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 269 | struct __addr_die_search_param { |
| 270 | Dwarf_Addr addr; |
| 271 | Dwarf_Die *die_mem; |
| 272 | }; |
| 273 | |
| 274 | static int __die_search_func_cb(Dwarf_Die *fn_die, void *data) |
| 275 | { |
| 276 | struct __addr_die_search_param *ad = data; |
| 277 | |
| 278 | if (dwarf_tag(fn_die) == DW_TAG_subprogram && |
| 279 | dwarf_haspc(fn_die, ad->addr)) { |
| 280 | memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die)); |
| 281 | return DWARF_CB_ABORT; |
| 282 | } |
| 283 | return DWARF_CB_OK; |
| 284 | } |
| 285 | |
| 286 | /* Search a real subprogram including this line, */ |
Masami Hiramatsu | 95a3e4c | 2010-03-16 18:05:51 -0400 | [diff] [blame] | 287 | static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr, |
| 288 | Dwarf_Die *die_mem) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 289 | { |
| 290 | struct __addr_die_search_param ad; |
| 291 | ad.addr = addr; |
| 292 | ad.die_mem = die_mem; |
| 293 | /* dwarf_getscopes can't find subprogram. */ |
| 294 | if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0)) |
| 295 | return NULL; |
| 296 | else |
| 297 | return die_mem; |
| 298 | } |
| 299 | |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 300 | /* die_find callback for inline function search */ |
| 301 | static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data) |
| 302 | { |
| 303 | Dwarf_Addr *addr = data; |
| 304 | |
| 305 | if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine && |
| 306 | dwarf_haspc(die_mem, *addr)) |
| 307 | return DIE_FIND_CB_FOUND; |
| 308 | |
| 309 | return DIE_FIND_CB_CONTINUE; |
| 310 | } |
| 311 | |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 312 | /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */ |
Masami Hiramatsu | 95a3e4c | 2010-03-16 18:05:51 -0400 | [diff] [blame] | 313 | static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr, |
| 314 | Dwarf_Die *die_mem) |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 315 | { |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 316 | return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem); |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 317 | } |
| 318 | |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 319 | static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 320 | { |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 321 | const char *name = data; |
| 322 | int tag; |
| 323 | |
| 324 | tag = dwarf_tag(die_mem); |
| 325 | if ((tag == DW_TAG_formal_parameter || |
| 326 | tag == DW_TAG_variable) && |
| 327 | (die_compare_name(die_mem, name) == 0)) |
| 328 | return DIE_FIND_CB_FOUND; |
| 329 | |
| 330 | return DIE_FIND_CB_CONTINUE; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 331 | } |
| 332 | |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 333 | /* Find a variable called 'name' */ |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 334 | static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name, |
| 335 | Dwarf_Die *die_mem) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 336 | { |
Masami Hiramatsu | 016f262 | 2010-03-16 18:05:58 -0400 | [diff] [blame] | 337 | return die_find_child(sp_die, __die_find_variable_cb, (void *)name, |
| 338 | die_mem); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 339 | } |
| 340 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 341 | static int __die_find_member_cb(Dwarf_Die *die_mem, void *data) |
| 342 | { |
| 343 | const char *name = data; |
| 344 | |
| 345 | if ((dwarf_tag(die_mem) == DW_TAG_member) && |
| 346 | (die_compare_name(die_mem, name) == 0)) |
| 347 | return DIE_FIND_CB_FOUND; |
| 348 | |
| 349 | return DIE_FIND_CB_SIBLING; |
| 350 | } |
| 351 | |
| 352 | /* Find a member called 'name' */ |
| 353 | static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name, |
| 354 | Dwarf_Die *die_mem) |
| 355 | { |
| 356 | return die_find_child(st_die, __die_find_member_cb, (void *)name, |
| 357 | die_mem); |
| 358 | } |
| 359 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 360 | /* |
| 361 | * Probe finder related functions |
| 362 | */ |
| 363 | |
| 364 | /* Show a location */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 365 | static void convert_location(Dwarf_Op *op, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 366 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 367 | unsigned int regn; |
| 368 | Dwarf_Word offs = 0; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 369 | bool ref = false; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 370 | const char *regs; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 371 | struct kprobe_trace_arg *tvar = pf->tvar; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 372 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 373 | /* TODO: support CFA */ |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 374 | /* If this is based on frame buffer, set the offset */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 375 | if (op->atom == DW_OP_fbreg) { |
| 376 | if (pf->fb_ops == NULL) |
| 377 | die("The attribute of frame base is not supported.\n"); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 378 | ref = true; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 379 | offs = op->number; |
| 380 | op = &pf->fb_ops[0]; |
| 381 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 382 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 383 | if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) { |
| 384 | regn = op->atom - DW_OP_breg0; |
| 385 | offs += op->number; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 386 | ref = true; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 387 | } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) { |
| 388 | regn = op->atom - DW_OP_reg0; |
| 389 | } else if (op->atom == DW_OP_bregx) { |
| 390 | regn = op->number; |
| 391 | offs += op->number2; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 392 | ref = true; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 393 | } else if (op->atom == DW_OP_regx) { |
| 394 | regn = op->number; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 395 | } else |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 396 | die("DW_OP %d is not supported.", op->atom); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 397 | |
| 398 | regs = get_arch_regstr(regn); |
| 399 | if (!regs) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 400 | die("%u exceeds max register number.", regn); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 401 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 402 | tvar->value = xstrdup(regs); |
| 403 | if (ref) { |
| 404 | tvar->ref = xzalloc(sizeof(struct kprobe_trace_arg_ref)); |
| 405 | tvar->ref->offset = (long)offs; |
| 406 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 407 | } |
| 408 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 409 | static void convert_variable_fields(Dwarf_Die *vr_die, const char *varname, |
| 410 | struct perf_probe_arg_field *field, |
| 411 | struct kprobe_trace_arg_ref **ref_ptr) |
| 412 | { |
| 413 | struct kprobe_trace_arg_ref *ref = *ref_ptr; |
| 414 | Dwarf_Attribute attr; |
| 415 | Dwarf_Die member; |
| 416 | Dwarf_Die type; |
| 417 | Dwarf_Word offs; |
| 418 | |
| 419 | pr_debug("converting %s in %s\n", field->name, varname); |
| 420 | if (die_get_real_type(vr_die, &type) == NULL) |
| 421 | die("Failed to get a type information of %s.", varname); |
| 422 | |
| 423 | /* Check the pointer and dereference */ |
| 424 | if (dwarf_tag(&type) == DW_TAG_pointer_type) { |
| 425 | if (!field->ref) |
| 426 | die("Semantic error: %s must be referred by '->'", |
| 427 | field->name); |
| 428 | /* Get the type pointed by this pointer */ |
| 429 | if (die_get_real_type(&type, &type) == NULL) |
| 430 | die("Failed to get a type information of %s.", varname); |
| 431 | |
Masami Hiramatsu | 12e5a7a | 2010-04-02 12:50:53 -0400 | [diff] [blame] | 432 | /* Verify it is a data structure */ |
| 433 | if (dwarf_tag(&type) != DW_TAG_structure_type) |
| 434 | die("%s is not a data structure.", varname); |
| 435 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 436 | ref = xzalloc(sizeof(struct kprobe_trace_arg_ref)); |
| 437 | if (*ref_ptr) |
| 438 | (*ref_ptr)->next = ref; |
| 439 | else |
| 440 | *ref_ptr = ref; |
| 441 | } else { |
Masami Hiramatsu | 12e5a7a | 2010-04-02 12:50:53 -0400 | [diff] [blame] | 442 | /* Verify it is a data structure */ |
| 443 | if (dwarf_tag(&type) != DW_TAG_structure_type) |
| 444 | die("%s is not a data structure.", varname); |
| 445 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 446 | if (field->ref) |
| 447 | die("Semantic error: %s must be referred by '.'", |
| 448 | field->name); |
| 449 | if (!ref) |
| 450 | die("Structure on a register is not supported yet."); |
| 451 | } |
| 452 | |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 453 | if (die_find_member(&type, field->name, &member) == NULL) |
| 454 | die("%s(tyep:%s) has no member %s.", varname, |
| 455 | dwarf_diename(&type), field->name); |
| 456 | |
| 457 | /* Get the offset of the field */ |
| 458 | if (dwarf_attr(&member, DW_AT_data_member_location, &attr) == NULL || |
| 459 | dwarf_formudata(&attr, &offs) != 0) |
| 460 | die("Failed to get the offset of %s.", field->name); |
| 461 | ref->offset += (long)offs; |
| 462 | |
| 463 | /* Converting next field */ |
| 464 | if (field->next) |
| 465 | convert_variable_fields(&member, field->name, field->next, |
| 466 | &ref); |
| 467 | } |
| 468 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 469 | /* Show a variables in kprobe event format */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 470 | static void convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 471 | { |
| 472 | Dwarf_Attribute attr; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 473 | Dwarf_Op *expr; |
| 474 | size_t nexpr; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 475 | int ret; |
| 476 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 477 | if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 478 | goto error; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 479 | /* TODO: handle more than 1 exprs */ |
Masami Hiramatsu | d0cb426 | 2010-03-15 13:02:35 -0400 | [diff] [blame] | 480 | ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 481 | if (ret <= 0 || nexpr == 0) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 482 | goto error; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 483 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 484 | convert_location(expr, pf); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 485 | |
| 486 | if (pf->pvar->field) |
| 487 | convert_variable_fields(vr_die, pf->pvar->name, |
| 488 | pf->pvar->field, &pf->tvar->ref); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 489 | /* *expr will be cached in libdw. Don't free it. */ |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 490 | return ; |
| 491 | error: |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 492 | /* TODO: Support const_value */ |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 493 | die("Failed to find the location of %s at this address.\n" |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 494 | " Perhaps, it has been optimized out.", pf->pvar->name); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 495 | } |
| 496 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 497 | /* Find a variable in a subprogram die */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 498 | static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 499 | { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 500 | Dwarf_Die vr_die; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 501 | char buf[128]; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 502 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 503 | /* TODO: Support struct members and arrays */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 504 | if (!is_c_varname(pf->pvar->name)) { |
| 505 | /* Copy raw parameters */ |
| 506 | pf->tvar->value = xstrdup(pf->pvar->name); |
| 507 | } else { |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 508 | synthesize_perf_probe_arg(pf->pvar, buf, 128); |
| 509 | pf->tvar->name = xstrdup(buf); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 510 | pr_debug("Searching '%s' variable in context.\n", |
| 511 | pf->pvar->name); |
| 512 | /* Search child die for local variables and parameters. */ |
| 513 | if (!die_find_variable(sp_die, pf->pvar->name, &vr_die)) |
| 514 | die("Failed to find '%s' in this function.", |
| 515 | pf->pvar->name); |
| 516 | convert_variable(&vr_die, pf); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 517 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 520 | /* Show a probe point to output buffer */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 521 | static void convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 522 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 523 | struct kprobe_trace_event *tev; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 524 | Dwarf_Addr eaddr; |
| 525 | Dwarf_Die die_mem; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 526 | const char *name; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 527 | int ret, i; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 528 | Dwarf_Attribute fb_attr; |
| 529 | size_t nops; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 530 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 531 | if (pf->ntevs == MAX_PROBES) |
| 532 | die("Too many( > %d) probe point found.\n", MAX_PROBES); |
| 533 | tev = &pf->tevs[pf->ntevs++]; |
| 534 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 535 | /* If no real subprogram, find a real one */ |
| 536 | if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) { |
Masami Hiramatsu | 95a3e4c | 2010-03-16 18:05:51 -0400 | [diff] [blame] | 537 | sp_die = die_find_real_subprogram(&pf->cu_die, |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 538 | pf->addr, &die_mem); |
| 539 | if (!sp_die) |
| 540 | die("Probe point is not found in subprograms."); |
| 541 | } |
| 542 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 543 | /* Copy the name of probe point */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 544 | name = dwarf_diename(sp_die); |
| 545 | if (name) { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 546 | dwarf_entrypc(sp_die, &eaddr); |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 547 | tev->point.symbol = xstrdup(name); |
| 548 | tev->point.offset = (unsigned long)(pf->addr - eaddr); |
| 549 | } else |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 550 | /* This function has no name. */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 551 | tev->point.offset = (unsigned long)pf->addr; |
| 552 | |
| 553 | pr_debug("Probe point found: %s+%lu\n", tev->point.symbol, |
| 554 | tev->point.offset); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 555 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 556 | /* Get the frame base attribute/ops */ |
| 557 | dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr); |
Masami Hiramatsu | d0cb426 | 2010-03-15 13:02:35 -0400 | [diff] [blame] | 558 | ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 559 | if (ret <= 0 || nops == 0) |
| 560 | pf->fb_ops = NULL; |
| 561 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 562 | /* Find each argument */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 563 | /* TODO: use dwarf_cfi_addrframe */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 564 | tev->nargs = pf->pev->nargs; |
| 565 | tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs); |
| 566 | for (i = 0; i < pf->pev->nargs; i++) { |
| 567 | pf->pvar = &pf->pev->args[i]; |
| 568 | pf->tvar = &tev->args[i]; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 569 | find_variable(sp_die, pf); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 570 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 571 | |
| 572 | /* *pf->fb_ops will be cached in libdw. Don't free it. */ |
| 573 | pf->fb_ops = NULL; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 574 | } |
| 575 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 576 | /* Find probe point from its line number */ |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 577 | static void find_probe_point_by_line(struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 578 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 579 | Dwarf_Lines *lines; |
| 580 | Dwarf_Line *line; |
| 581 | size_t nlines, i; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 582 | Dwarf_Addr addr; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 583 | int lineno; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 584 | int ret; |
| 585 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 586 | ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines); |
| 587 | DIE_IF(ret != 0); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 588 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 589 | for (i = 0; i < nlines; i++) { |
| 590 | line = dwarf_onesrcline(lines, i); |
| 591 | dwarf_lineno(line, &lineno); |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 592 | if (lineno != pf->lno) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 593 | continue; |
| 594 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 595 | /* TODO: Get fileno from line, but how? */ |
| 596 | if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0) |
| 597 | continue; |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 598 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 599 | ret = dwarf_lineaddr(line, &addr); |
| 600 | DIE_IF(ret != 0); |
| 601 | pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n", |
| 602 | (int)i, lineno, (uintmax_t)addr); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 603 | pf->addr = addr; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 604 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 605 | convert_probe_point(NULL, pf); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 606 | /* Continuing, because target line might be inlined. */ |
| 607 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 608 | } |
| 609 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 610 | /* Find lines which match lazy pattern */ |
| 611 | static int find_lazy_match_lines(struct list_head *head, |
| 612 | const char *fname, const char *pat) |
| 613 | { |
| 614 | char *fbuf, *p1, *p2; |
| 615 | int fd, line, nlines = 0; |
| 616 | struct stat st; |
| 617 | |
| 618 | fd = open(fname, O_RDONLY); |
| 619 | if (fd < 0) |
| 620 | die("failed to open %s", fname); |
| 621 | DIE_IF(fstat(fd, &st) < 0); |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 622 | fbuf = xmalloc(st.st_size + 2); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 623 | DIE_IF(read(fd, fbuf, st.st_size) < 0); |
| 624 | close(fd); |
| 625 | fbuf[st.st_size] = '\n'; /* Dummy line */ |
| 626 | fbuf[st.st_size + 1] = '\0'; |
| 627 | p1 = fbuf; |
| 628 | line = 1; |
| 629 | while ((p2 = strchr(p1, '\n')) != NULL) { |
| 630 | *p2 = '\0'; |
| 631 | if (strlazymatch(p1, pat)) { |
| 632 | line_list__add_line(head, line); |
| 633 | nlines++; |
| 634 | } |
| 635 | line++; |
| 636 | p1 = p2 + 1; |
| 637 | } |
| 638 | free(fbuf); |
| 639 | return nlines; |
| 640 | } |
| 641 | |
| 642 | /* Find probe points from lazy pattern */ |
| 643 | static void find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf) |
| 644 | { |
| 645 | Dwarf_Lines *lines; |
| 646 | Dwarf_Line *line; |
| 647 | size_t nlines, i; |
| 648 | Dwarf_Addr addr; |
| 649 | Dwarf_Die die_mem; |
| 650 | int lineno; |
| 651 | int ret; |
| 652 | |
| 653 | if (list_empty(&pf->lcache)) { |
| 654 | /* Matching lazy line pattern */ |
| 655 | ret = find_lazy_match_lines(&pf->lcache, pf->fname, |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 656 | pf->pev->point.lazy_line); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 657 | if (ret <= 0) |
| 658 | die("No matched lines found in %s.", pf->fname); |
| 659 | } |
| 660 | |
| 661 | ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines); |
| 662 | DIE_IF(ret != 0); |
| 663 | for (i = 0; i < nlines; i++) { |
| 664 | line = dwarf_onesrcline(lines, i); |
| 665 | |
| 666 | dwarf_lineno(line, &lineno); |
| 667 | if (!line_list__has_line(&pf->lcache, lineno)) |
| 668 | continue; |
| 669 | |
| 670 | /* TODO: Get fileno from line, but how? */ |
| 671 | if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0) |
| 672 | continue; |
| 673 | |
| 674 | ret = dwarf_lineaddr(line, &addr); |
| 675 | DIE_IF(ret != 0); |
| 676 | if (sp_die) { |
| 677 | /* Address filtering 1: does sp_die include addr? */ |
| 678 | if (!dwarf_haspc(sp_die, addr)) |
| 679 | continue; |
| 680 | /* Address filtering 2: No child include addr? */ |
Masami Hiramatsu | 95a3e4c | 2010-03-16 18:05:51 -0400 | [diff] [blame] | 681 | if (die_find_inlinefunc(sp_die, addr, &die_mem)) |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 682 | continue; |
| 683 | } |
| 684 | |
| 685 | pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n", |
| 686 | (int)i, lineno, (unsigned long long)addr); |
| 687 | pf->addr = addr; |
| 688 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 689 | convert_probe_point(sp_die, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 690 | /* Continuing, because target line might be inlined. */ |
| 691 | } |
| 692 | /* TODO: deallocate lines, but how? */ |
| 693 | } |
| 694 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 695 | static int probe_point_inline_cb(Dwarf_Die *in_die, void *data) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 696 | { |
| 697 | struct probe_finder *pf = (struct probe_finder *)data; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 698 | struct perf_probe_point *pp = &pf->pev->point; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 699 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 700 | if (pp->lazy_line) |
| 701 | find_probe_point_lazy(in_die, pf); |
| 702 | else { |
| 703 | /* Get probe address */ |
| 704 | pf->addr = die_get_entrypc(in_die); |
| 705 | pf->addr += pp->offset; |
| 706 | pr_debug("found inline addr: 0x%jx\n", |
| 707 | (uintmax_t)pf->addr); |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 708 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 709 | convert_probe_point(in_die, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 710 | } |
| 711 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 712 | return DWARF_CB_OK; |
| 713 | } |
| 714 | |
| 715 | /* Search function from function name */ |
| 716 | static int probe_point_search_cb(Dwarf_Die *sp_die, void *data) |
| 717 | { |
| 718 | struct probe_finder *pf = (struct probe_finder *)data; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 719 | struct perf_probe_point *pp = &pf->pev->point; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 720 | |
| 721 | /* Check tag and diename */ |
| 722 | if (dwarf_tag(sp_die) != DW_TAG_subprogram || |
| 723 | die_compare_name(sp_die, pp->function) != 0) |
| 724 | return 0; |
| 725 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 726 | pf->fname = dwarf_decl_file(sp_die); |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 727 | if (pp->line) { /* Function relative line */ |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 728 | dwarf_decl_line(sp_die, &pf->lno); |
| 729 | pf->lno += pp->line; |
| 730 | find_probe_point_by_line(pf); |
| 731 | } else if (!dwarf_func_inline(sp_die)) { |
| 732 | /* Real function */ |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 733 | if (pp->lazy_line) |
| 734 | find_probe_point_lazy(sp_die, pf); |
| 735 | else { |
| 736 | pf->addr = die_get_entrypc(sp_die); |
| 737 | pf->addr += pp->offset; |
| 738 | /* TODO: Check the address in this function */ |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 739 | convert_probe_point(sp_die, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 740 | } |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 741 | } else |
| 742 | /* Inlined function: search instances */ |
| 743 | dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf); |
| 744 | |
| 745 | return 1; /* Exit; no same symbol in this CU. */ |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 746 | } |
| 747 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 748 | static void find_probe_point_by_func(struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 749 | { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 750 | dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 751 | } |
| 752 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 753 | /* Find kprobe_trace_events specified by perf_probe_event from debuginfo */ |
| 754 | int find_kprobe_trace_events(int fd, struct perf_probe_event *pev, |
| 755 | struct kprobe_trace_event **tevs) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 756 | { |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 757 | struct probe_finder pf = {.pev = pev}; |
| 758 | struct perf_probe_point *pp = &pev->point; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 759 | Dwarf_Off off, noff; |
| 760 | size_t cuhl; |
| 761 | Dwarf_Die *diep; |
| 762 | Dwarf *dbg; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 763 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 764 | pf.tevs = xzalloc(sizeof(struct kprobe_trace_event) * MAX_PROBES); |
| 765 | *tevs = pf.tevs; |
| 766 | pf.ntevs = 0; |
| 767 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 768 | dbg = dwarf_begin(fd, DWARF_C_READ); |
| 769 | if (!dbg) |
Masami Hiramatsu | a225a1d | 2009-11-03 19:12:30 -0500 | [diff] [blame] | 770 | return -ENOENT; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 771 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 772 | off = 0; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 773 | line_list__init(&pf.lcache); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 774 | /* Loop on CUs (Compilation Unit) */ |
| 775 | while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 776 | /* Get the DIE(Debugging Information Entry) of this CU */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 777 | diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die); |
| 778 | if (!diep) |
| 779 | continue; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 780 | |
| 781 | /* Check if target file is included. */ |
| 782 | if (pp->file) |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 783 | pf.fname = cu_find_realpath(&pf.cu_die, pp->file); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 784 | else |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 785 | pf.fname = NULL; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 786 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 787 | if (!pp->file || pf.fname) { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 788 | if (pp->function) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 789 | find_probe_point_by_func(&pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 790 | else if (pp->lazy_line) |
| 791 | find_probe_point_lazy(NULL, &pf); |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 792 | else { |
| 793 | pf.lno = pp->line; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 794 | find_probe_point_by_line(&pf); |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 795 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 796 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 797 | off = noff; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 798 | } |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 799 | line_list__free(&pf.lcache); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 800 | dwarf_end(dbg); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 801 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 802 | return pf.ntevs; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 803 | } |
| 804 | |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 805 | /* Reverse search */ |
| 806 | int find_perf_probe_point(int fd, unsigned long addr, |
| 807 | struct perf_probe_point *ppt) |
| 808 | { |
| 809 | Dwarf_Die cudie, spdie, indie; |
| 810 | Dwarf *dbg; |
| 811 | Dwarf_Line *line; |
| 812 | Dwarf_Addr laddr, eaddr; |
| 813 | const char *tmp; |
| 814 | int lineno, ret = 0; |
| 815 | |
| 816 | dbg = dwarf_begin(fd, DWARF_C_READ); |
| 817 | if (!dbg) |
| 818 | return -ENOENT; |
| 819 | |
| 820 | /* Find cu die */ |
Masami Hiramatsu | 75ec5a2 | 2010-04-02 12:50:59 -0400 | [diff] [blame] | 821 | if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) { |
| 822 | ret = -EINVAL; |
| 823 | goto end; |
| 824 | } |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 825 | |
| 826 | /* Find a corresponding line */ |
| 827 | line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr); |
| 828 | if (line) { |
| 829 | dwarf_lineaddr(line, &laddr); |
| 830 | if ((Dwarf_Addr)addr == laddr) { |
| 831 | dwarf_lineno(line, &lineno); |
| 832 | ppt->line = lineno; |
| 833 | |
| 834 | tmp = dwarf_linesrc(line, NULL, NULL); |
| 835 | DIE_IF(!tmp); |
| 836 | ppt->file = xstrdup(tmp); |
| 837 | ret = 1; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | /* Find a corresponding function */ |
| 842 | if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) { |
| 843 | tmp = dwarf_diename(&spdie); |
| 844 | if (!tmp) |
| 845 | goto end; |
| 846 | |
| 847 | dwarf_entrypc(&spdie, &eaddr); |
| 848 | if (!lineno) { |
| 849 | /* We don't have a line number, let's use offset */ |
| 850 | ppt->function = xstrdup(tmp); |
| 851 | ppt->offset = addr - (unsigned long)eaddr; |
| 852 | ret = 1; |
| 853 | goto end; |
| 854 | } |
| 855 | if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr, &indie)) { |
| 856 | /* addr in an inline function */ |
| 857 | tmp = dwarf_diename(&indie); |
| 858 | if (!tmp) |
| 859 | goto end; |
| 860 | dwarf_decl_line(&indie, &lineno); |
| 861 | } else { |
| 862 | if (eaddr == addr) /* No offset: function entry */ |
| 863 | lineno = ppt->line; |
| 864 | else |
| 865 | dwarf_decl_line(&spdie, &lineno); |
| 866 | } |
| 867 | ppt->function = xstrdup(tmp); |
| 868 | ppt->line -= lineno; /* Make a relative line number */ |
| 869 | } |
| 870 | |
| 871 | end: |
| 872 | dwarf_end(dbg); |
| 873 | return ret; |
| 874 | } |
| 875 | |
| 876 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 877 | /* Find line range from its line number */ |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 878 | 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] | 879 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 880 | Dwarf_Lines *lines; |
| 881 | Dwarf_Line *line; |
| 882 | size_t nlines, i; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 883 | Dwarf_Addr addr; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 884 | int lineno; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 885 | int ret; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 886 | const char *src; |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 887 | Dwarf_Die die_mem; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 888 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 889 | line_list__init(&lf->lr->line_list); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 890 | ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines); |
| 891 | DIE_IF(ret != 0); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 892 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 893 | for (i = 0; i < nlines; i++) { |
| 894 | line = dwarf_onesrcline(lines, i); |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 895 | ret = dwarf_lineno(line, &lineno); |
| 896 | DIE_IF(ret != 0); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 897 | if (lf->lno_s > lineno || lf->lno_e < lineno) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 898 | continue; |
| 899 | |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 900 | if (sp_die) { |
| 901 | /* Address filtering 1: does sp_die include addr? */ |
| 902 | ret = dwarf_lineaddr(line, &addr); |
| 903 | DIE_IF(ret != 0); |
| 904 | if (!dwarf_haspc(sp_die, addr)) |
| 905 | continue; |
| 906 | |
| 907 | /* Address filtering 2: No child include addr? */ |
Masami Hiramatsu | 95a3e4c | 2010-03-16 18:05:51 -0400 | [diff] [blame] | 908 | if (die_find_inlinefunc(sp_die, addr, &die_mem)) |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 909 | continue; |
| 910 | } |
| 911 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 912 | /* TODO: Get fileno from line, but how? */ |
| 913 | src = dwarf_linesrc(line, NULL, NULL); |
| 914 | if (strtailcmp(src, lf->fname) != 0) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 915 | continue; |
| 916 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 917 | /* Copy real path */ |
| 918 | if (!lf->lr->path) |
Masami Hiramatsu | 31facc5 | 2010-03-16 18:05:30 -0400 | [diff] [blame] | 919 | lf->lr->path = xstrdup(src); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 920 | line_list__add_line(&lf->lr->line_list, (unsigned int)lineno); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 921 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 922 | /* Update status */ |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 923 | if (!list_empty(&lf->lr->line_list)) |
| 924 | lf->found = 1; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 925 | else { |
| 926 | free(lf->lr->path); |
| 927 | lf->lr->path = NULL; |
| 928 | } |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 929 | } |
| 930 | |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 931 | static int line_range_inline_cb(Dwarf_Die *in_die, void *data) |
| 932 | { |
| 933 | find_line_range_by_line(in_die, (struct line_finder *)data); |
| 934 | return DWARF_CB_ABORT; /* No need to find other instances */ |
| 935 | } |
| 936 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 937 | /* Search function from function name */ |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 938 | static int line_range_search_cb(Dwarf_Die *sp_die, void *data) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 939 | { |
| 940 | struct line_finder *lf = (struct line_finder *)data; |
| 941 | struct line_range *lr = lf->lr; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 942 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 943 | if (dwarf_tag(sp_die) == DW_TAG_subprogram && |
| 944 | die_compare_name(sp_die, lr->function) == 0) { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 945 | lf->fname = dwarf_decl_file(sp_die); |
| 946 | dwarf_decl_line(sp_die, &lr->offset); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 947 | pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 948 | lf->lno_s = lr->offset + lr->start; |
| 949 | if (!lr->end) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 950 | lf->lno_e = INT_MAX; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 951 | else |
| 952 | lf->lno_e = lr->offset + lr->end; |
| 953 | lr->start = lf->lno_s; |
| 954 | lr->end = lf->lno_e; |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 955 | if (dwarf_func_inline(sp_die)) |
| 956 | dwarf_func_inline_instances(sp_die, |
| 957 | line_range_inline_cb, lf); |
| 958 | else |
| 959 | find_line_range_by_line(sp_die, lf); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 960 | return 1; |
| 961 | } |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | static void find_line_range_by_func(struct line_finder *lf) |
| 966 | { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 967 | dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | int find_line_range(int fd, struct line_range *lr) |
| 971 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 972 | struct line_finder lf = {.lr = lr, .found = 0}; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 973 | int ret; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 974 | Dwarf_Off off = 0, noff; |
| 975 | size_t cuhl; |
| 976 | Dwarf_Die *diep; |
| 977 | Dwarf *dbg; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 978 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 979 | dbg = dwarf_begin(fd, DWARF_C_READ); |
| 980 | if (!dbg) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 981 | return -ENOENT; |
| 982 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 983 | /* Loop on CUs (Compilation Unit) */ |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 984 | while (!lf.found) { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 985 | ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL); |
| 986 | if (ret != 0) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 987 | break; |
| 988 | |
| 989 | /* Get the DIE(Debugging Information Entry) of this CU */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 990 | diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die); |
| 991 | if (!diep) |
| 992 | continue; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 993 | |
| 994 | /* Check if target file is included. */ |
| 995 | if (lr->file) |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 996 | lf.fname = cu_find_realpath(&lf.cu_die, lr->file); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 997 | else |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 998 | lf.fname = 0; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 999 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1000 | if (!lr->file || lf.fname) { |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1001 | if (lr->function) |
| 1002 | find_line_range_by_func(&lf); |
| 1003 | else { |
| 1004 | lf.lno_s = lr->start; |
| 1005 | if (!lr->end) |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1006 | lf.lno_e = INT_MAX; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1007 | else |
| 1008 | lf.lno_e = lr->end; |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 1009 | find_line_range_by_line(NULL, &lf); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1010 | } |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1011 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1012 | off = noff; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1013 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1014 | pr_debug("path: %lx\n", (unsigned long)lr->path); |
| 1015 | dwarf_end(dbg); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1016 | return lf.found; |
| 1017 | } |
| 1018 | |