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