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