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