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