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