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