1*e0d153c6SMasami Hiramatsu /* 2*e0d153c6SMasami Hiramatsu * dwarf-aux.c : libdw auxiliary interfaces 3*e0d153c6SMasami Hiramatsu * 4*e0d153c6SMasami Hiramatsu * This program is free software; you can redistribute it and/or modify 5*e0d153c6SMasami Hiramatsu * it under the terms of the GNU General Public License as published by 6*e0d153c6SMasami Hiramatsu * the Free Software Foundation; either version 2 of the License, or 7*e0d153c6SMasami Hiramatsu * (at your option) any later version. 8*e0d153c6SMasami Hiramatsu * 9*e0d153c6SMasami Hiramatsu * This program is distributed in the hope that it will be useful, 10*e0d153c6SMasami Hiramatsu * but WITHOUT ANY WARRANTY; without even the implied warranty of 11*e0d153c6SMasami Hiramatsu * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12*e0d153c6SMasami Hiramatsu * GNU General Public License for more details. 13*e0d153c6SMasami Hiramatsu * 14*e0d153c6SMasami Hiramatsu * You should have received a copy of the GNU General Public License 15*e0d153c6SMasami Hiramatsu * along with this program; if not, write to the Free Software 16*e0d153c6SMasami Hiramatsu * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17*e0d153c6SMasami Hiramatsu * 18*e0d153c6SMasami Hiramatsu */ 19*e0d153c6SMasami Hiramatsu 20*e0d153c6SMasami Hiramatsu #include <stdbool.h> 21*e0d153c6SMasami Hiramatsu #include "util.h" 22*e0d153c6SMasami Hiramatsu #include "debug.h" 23*e0d153c6SMasami Hiramatsu #include "dwarf-aux.h" 24*e0d153c6SMasami Hiramatsu 25*e0d153c6SMasami Hiramatsu /** 26*e0d153c6SMasami Hiramatsu * cu_find_realpath - Find the realpath of the target file 27*e0d153c6SMasami Hiramatsu * @cu_die: A DIE(dwarf information entry) of CU(compilation Unit) 28*e0d153c6SMasami Hiramatsu * @fname: The tail filename of the target file 29*e0d153c6SMasami Hiramatsu * 30*e0d153c6SMasami Hiramatsu * Find the real(long) path of @fname in @cu_die. 31*e0d153c6SMasami Hiramatsu */ 32*e0d153c6SMasami Hiramatsu const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname) 33*e0d153c6SMasami Hiramatsu { 34*e0d153c6SMasami Hiramatsu Dwarf_Files *files; 35*e0d153c6SMasami Hiramatsu size_t nfiles, i; 36*e0d153c6SMasami Hiramatsu const char *src = NULL; 37*e0d153c6SMasami Hiramatsu int ret; 38*e0d153c6SMasami Hiramatsu 39*e0d153c6SMasami Hiramatsu if (!fname) 40*e0d153c6SMasami Hiramatsu return NULL; 41*e0d153c6SMasami Hiramatsu 42*e0d153c6SMasami Hiramatsu ret = dwarf_getsrcfiles(cu_die, &files, &nfiles); 43*e0d153c6SMasami Hiramatsu if (ret != 0) 44*e0d153c6SMasami Hiramatsu return NULL; 45*e0d153c6SMasami Hiramatsu 46*e0d153c6SMasami Hiramatsu for (i = 0; i < nfiles; i++) { 47*e0d153c6SMasami Hiramatsu src = dwarf_filesrc(files, i, NULL, NULL); 48*e0d153c6SMasami Hiramatsu if (strtailcmp(src, fname) == 0) 49*e0d153c6SMasami Hiramatsu break; 50*e0d153c6SMasami Hiramatsu } 51*e0d153c6SMasami Hiramatsu if (i == nfiles) 52*e0d153c6SMasami Hiramatsu return NULL; 53*e0d153c6SMasami Hiramatsu return src; 54*e0d153c6SMasami Hiramatsu } 55*e0d153c6SMasami Hiramatsu 56*e0d153c6SMasami Hiramatsu /** 57*e0d153c6SMasami Hiramatsu * cu_get_comp_dir - Get the path of compilation directory 58*e0d153c6SMasami Hiramatsu * @cu_die: a CU DIE 59*e0d153c6SMasami Hiramatsu * 60*e0d153c6SMasami Hiramatsu * Get the path of compilation directory of given @cu_die. 61*e0d153c6SMasami Hiramatsu * Since this depends on DW_AT_comp_dir, older gcc will not 62*e0d153c6SMasami Hiramatsu * embedded it. In that case, this returns NULL. 63*e0d153c6SMasami Hiramatsu */ 64*e0d153c6SMasami Hiramatsu const char *cu_get_comp_dir(Dwarf_Die *cu_die) 65*e0d153c6SMasami Hiramatsu { 66*e0d153c6SMasami Hiramatsu Dwarf_Attribute attr; 67*e0d153c6SMasami Hiramatsu if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL) 68*e0d153c6SMasami Hiramatsu return NULL; 69*e0d153c6SMasami Hiramatsu return dwarf_formstring(&attr); 70*e0d153c6SMasami Hiramatsu } 71*e0d153c6SMasami Hiramatsu 72*e0d153c6SMasami Hiramatsu /** 73*e0d153c6SMasami Hiramatsu * cu_find_lineinfo - Get a line number and file name for given address 74*e0d153c6SMasami Hiramatsu * @cu_die: a CU DIE 75*e0d153c6SMasami Hiramatsu * @addr: An address 76*e0d153c6SMasami Hiramatsu * @fname: a pointer which returns the file name string 77*e0d153c6SMasami Hiramatsu * @lineno: a pointer which returns the line number 78*e0d153c6SMasami Hiramatsu * 79*e0d153c6SMasami Hiramatsu * Find a line number and file name for @addr in @cu_die. 80*e0d153c6SMasami Hiramatsu */ 81*e0d153c6SMasami Hiramatsu int cu_find_lineinfo(Dwarf_Die *cu_die, unsigned long addr, 82*e0d153c6SMasami Hiramatsu const char **fname, int *lineno) 83*e0d153c6SMasami Hiramatsu { 84*e0d153c6SMasami Hiramatsu Dwarf_Line *line; 85*e0d153c6SMasami Hiramatsu Dwarf_Addr laddr; 86*e0d153c6SMasami Hiramatsu 87*e0d153c6SMasami Hiramatsu line = dwarf_getsrc_die(cu_die, (Dwarf_Addr)addr); 88*e0d153c6SMasami Hiramatsu if (line && dwarf_lineaddr(line, &laddr) == 0 && 89*e0d153c6SMasami Hiramatsu addr == (unsigned long)laddr && dwarf_lineno(line, lineno) == 0) { 90*e0d153c6SMasami Hiramatsu *fname = dwarf_linesrc(line, NULL, NULL); 91*e0d153c6SMasami Hiramatsu if (!*fname) 92*e0d153c6SMasami Hiramatsu /* line number is useless without filename */ 93*e0d153c6SMasami Hiramatsu *lineno = 0; 94*e0d153c6SMasami Hiramatsu } 95*e0d153c6SMasami Hiramatsu 96*e0d153c6SMasami Hiramatsu return *lineno ?: -ENOENT; 97*e0d153c6SMasami Hiramatsu } 98*e0d153c6SMasami Hiramatsu 99*e0d153c6SMasami Hiramatsu /** 100*e0d153c6SMasami Hiramatsu * die_compare_name - Compare diename and tname 101*e0d153c6SMasami Hiramatsu * @dw_die: a DIE 102*e0d153c6SMasami Hiramatsu * @tname: a string of target name 103*e0d153c6SMasami Hiramatsu * 104*e0d153c6SMasami Hiramatsu * Compare the name of @dw_die and @tname. Return false if @dw_die has no name. 105*e0d153c6SMasami Hiramatsu */ 106*e0d153c6SMasami Hiramatsu bool die_compare_name(Dwarf_Die *dw_die, const char *tname) 107*e0d153c6SMasami Hiramatsu { 108*e0d153c6SMasami Hiramatsu const char *name; 109*e0d153c6SMasami Hiramatsu name = dwarf_diename(dw_die); 110*e0d153c6SMasami Hiramatsu return name ? (strcmp(tname, name) == 0) : false; 111*e0d153c6SMasami Hiramatsu } 112*e0d153c6SMasami Hiramatsu 113*e0d153c6SMasami Hiramatsu /** 114*e0d153c6SMasami Hiramatsu * die_get_call_lineno - Get callsite line number of inline-function instance 115*e0d153c6SMasami Hiramatsu * @in_die: a DIE of an inlined function instance 116*e0d153c6SMasami Hiramatsu * 117*e0d153c6SMasami Hiramatsu * Get call-site line number of @in_die. This means from where the inline 118*e0d153c6SMasami Hiramatsu * function is called. 119*e0d153c6SMasami Hiramatsu */ 120*e0d153c6SMasami Hiramatsu int die_get_call_lineno(Dwarf_Die *in_die) 121*e0d153c6SMasami Hiramatsu { 122*e0d153c6SMasami Hiramatsu Dwarf_Attribute attr; 123*e0d153c6SMasami Hiramatsu Dwarf_Word ret; 124*e0d153c6SMasami Hiramatsu 125*e0d153c6SMasami Hiramatsu if (!dwarf_attr(in_die, DW_AT_call_line, &attr)) 126*e0d153c6SMasami Hiramatsu return -ENOENT; 127*e0d153c6SMasami Hiramatsu 128*e0d153c6SMasami Hiramatsu dwarf_formudata(&attr, &ret); 129*e0d153c6SMasami Hiramatsu return (int)ret; 130*e0d153c6SMasami Hiramatsu } 131*e0d153c6SMasami Hiramatsu 132*e0d153c6SMasami Hiramatsu /** 133*e0d153c6SMasami Hiramatsu * die_get_type - Get type DIE 134*e0d153c6SMasami Hiramatsu * @vr_die: a DIE of a variable 135*e0d153c6SMasami Hiramatsu * @die_mem: where to store a type DIE 136*e0d153c6SMasami Hiramatsu * 137*e0d153c6SMasami Hiramatsu * Get a DIE of the type of given variable (@vr_die), and store 138*e0d153c6SMasami Hiramatsu * it to die_mem. Return NULL if fails to get a type DIE. 139*e0d153c6SMasami Hiramatsu */ 140*e0d153c6SMasami Hiramatsu Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) 141*e0d153c6SMasami Hiramatsu { 142*e0d153c6SMasami Hiramatsu Dwarf_Attribute attr; 143*e0d153c6SMasami Hiramatsu 144*e0d153c6SMasami Hiramatsu if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) && 145*e0d153c6SMasami Hiramatsu dwarf_formref_die(&attr, die_mem)) 146*e0d153c6SMasami Hiramatsu return die_mem; 147*e0d153c6SMasami Hiramatsu else 148*e0d153c6SMasami Hiramatsu return NULL; 149*e0d153c6SMasami Hiramatsu } 150*e0d153c6SMasami Hiramatsu 151*e0d153c6SMasami Hiramatsu /* Get a type die, but skip qualifiers */ 152*e0d153c6SMasami Hiramatsu static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) 153*e0d153c6SMasami Hiramatsu { 154*e0d153c6SMasami Hiramatsu int tag; 155*e0d153c6SMasami Hiramatsu 156*e0d153c6SMasami Hiramatsu do { 157*e0d153c6SMasami Hiramatsu vr_die = die_get_type(vr_die, die_mem); 158*e0d153c6SMasami Hiramatsu if (!vr_die) 159*e0d153c6SMasami Hiramatsu break; 160*e0d153c6SMasami Hiramatsu tag = dwarf_tag(vr_die); 161*e0d153c6SMasami Hiramatsu } while (tag == DW_TAG_const_type || 162*e0d153c6SMasami Hiramatsu tag == DW_TAG_restrict_type || 163*e0d153c6SMasami Hiramatsu tag == DW_TAG_volatile_type || 164*e0d153c6SMasami Hiramatsu tag == DW_TAG_shared_type); 165*e0d153c6SMasami Hiramatsu 166*e0d153c6SMasami Hiramatsu return vr_die; 167*e0d153c6SMasami Hiramatsu } 168*e0d153c6SMasami Hiramatsu 169*e0d153c6SMasami Hiramatsu /** 170*e0d153c6SMasami Hiramatsu * die_get_real_type - Get a type die, but skip qualifiers and typedef 171*e0d153c6SMasami Hiramatsu * @vr_die: a DIE of a variable 172*e0d153c6SMasami Hiramatsu * @die_mem: where to store a type DIE 173*e0d153c6SMasami Hiramatsu * 174*e0d153c6SMasami Hiramatsu * Get a DIE of the type of given variable (@vr_die), and store 175*e0d153c6SMasami Hiramatsu * it to die_mem. Return NULL if fails to get a type DIE. 176*e0d153c6SMasami Hiramatsu * If the type is qualifiers (e.g. const) or typedef, this skips it 177*e0d153c6SMasami Hiramatsu * and tries to find real type (structure or basic types, e.g. int). 178*e0d153c6SMasami Hiramatsu */ 179*e0d153c6SMasami Hiramatsu Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) 180*e0d153c6SMasami Hiramatsu { 181*e0d153c6SMasami Hiramatsu do { 182*e0d153c6SMasami Hiramatsu vr_die = __die_get_real_type(vr_die, die_mem); 183*e0d153c6SMasami Hiramatsu } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef); 184*e0d153c6SMasami Hiramatsu 185*e0d153c6SMasami Hiramatsu return vr_die; 186*e0d153c6SMasami Hiramatsu } 187*e0d153c6SMasami Hiramatsu 188*e0d153c6SMasami Hiramatsu /* Get attribute and translate it as a udata */ 189*e0d153c6SMasami Hiramatsu static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name, 190*e0d153c6SMasami Hiramatsu Dwarf_Word *result) 191*e0d153c6SMasami Hiramatsu { 192*e0d153c6SMasami Hiramatsu Dwarf_Attribute attr; 193*e0d153c6SMasami Hiramatsu 194*e0d153c6SMasami Hiramatsu if (dwarf_attr(tp_die, attr_name, &attr) == NULL || 195*e0d153c6SMasami Hiramatsu dwarf_formudata(&attr, result) != 0) 196*e0d153c6SMasami Hiramatsu return -ENOENT; 197*e0d153c6SMasami Hiramatsu 198*e0d153c6SMasami Hiramatsu return 0; 199*e0d153c6SMasami Hiramatsu } 200*e0d153c6SMasami Hiramatsu 201*e0d153c6SMasami Hiramatsu /** 202*e0d153c6SMasami Hiramatsu * die_is_signed_type - Check whether a type DIE is signed or not 203*e0d153c6SMasami Hiramatsu * @tp_die: a DIE of a type 204*e0d153c6SMasami Hiramatsu * 205*e0d153c6SMasami Hiramatsu * Get the encoding of @tp_die and return true if the encoding 206*e0d153c6SMasami Hiramatsu * is signed. 207*e0d153c6SMasami Hiramatsu */ 208*e0d153c6SMasami Hiramatsu bool die_is_signed_type(Dwarf_Die *tp_die) 209*e0d153c6SMasami Hiramatsu { 210*e0d153c6SMasami Hiramatsu Dwarf_Word ret; 211*e0d153c6SMasami Hiramatsu 212*e0d153c6SMasami Hiramatsu if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret)) 213*e0d153c6SMasami Hiramatsu return false; 214*e0d153c6SMasami Hiramatsu 215*e0d153c6SMasami Hiramatsu return (ret == DW_ATE_signed_char || ret == DW_ATE_signed || 216*e0d153c6SMasami Hiramatsu ret == DW_ATE_signed_fixed); 217*e0d153c6SMasami Hiramatsu } 218*e0d153c6SMasami Hiramatsu 219*e0d153c6SMasami Hiramatsu /** 220*e0d153c6SMasami Hiramatsu * die_get_data_member_location - Get the data-member offset 221*e0d153c6SMasami Hiramatsu * @mb_die: a DIE of a member of a data structure 222*e0d153c6SMasami Hiramatsu * @offs: The offset of the member in the data structure 223*e0d153c6SMasami Hiramatsu * 224*e0d153c6SMasami Hiramatsu * Get the offset of @mb_die in the data structure including @mb_die, and 225*e0d153c6SMasami Hiramatsu * stores result offset to @offs. If any error occurs this returns errno. 226*e0d153c6SMasami Hiramatsu */ 227*e0d153c6SMasami Hiramatsu int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs) 228*e0d153c6SMasami Hiramatsu { 229*e0d153c6SMasami Hiramatsu Dwarf_Attribute attr; 230*e0d153c6SMasami Hiramatsu Dwarf_Op *expr; 231*e0d153c6SMasami Hiramatsu size_t nexpr; 232*e0d153c6SMasami Hiramatsu int ret; 233*e0d153c6SMasami Hiramatsu 234*e0d153c6SMasami Hiramatsu if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL) 235*e0d153c6SMasami Hiramatsu return -ENOENT; 236*e0d153c6SMasami Hiramatsu 237*e0d153c6SMasami Hiramatsu if (dwarf_formudata(&attr, offs) != 0) { 238*e0d153c6SMasami Hiramatsu /* DW_AT_data_member_location should be DW_OP_plus_uconst */ 239*e0d153c6SMasami Hiramatsu ret = dwarf_getlocation(&attr, &expr, &nexpr); 240*e0d153c6SMasami Hiramatsu if (ret < 0 || nexpr == 0) 241*e0d153c6SMasami Hiramatsu return -ENOENT; 242*e0d153c6SMasami Hiramatsu 243*e0d153c6SMasami Hiramatsu if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) { 244*e0d153c6SMasami Hiramatsu pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n", 245*e0d153c6SMasami Hiramatsu expr[0].atom, nexpr); 246*e0d153c6SMasami Hiramatsu return -ENOTSUP; 247*e0d153c6SMasami Hiramatsu } 248*e0d153c6SMasami Hiramatsu *offs = (Dwarf_Word)expr[0].number; 249*e0d153c6SMasami Hiramatsu } 250*e0d153c6SMasami Hiramatsu return 0; 251*e0d153c6SMasami Hiramatsu } 252*e0d153c6SMasami Hiramatsu 253*e0d153c6SMasami Hiramatsu /** 254*e0d153c6SMasami Hiramatsu * die_find_child - Generic DIE search function in DIE tree 255*e0d153c6SMasami Hiramatsu * @rt_die: a root DIE 256*e0d153c6SMasami Hiramatsu * @callback: a callback function 257*e0d153c6SMasami Hiramatsu * @data: a user data passed to the callback function 258*e0d153c6SMasami Hiramatsu * @die_mem: a buffer for result DIE 259*e0d153c6SMasami Hiramatsu * 260*e0d153c6SMasami Hiramatsu * Trace DIE tree from @rt_die and call @callback for each child DIE. 261*e0d153c6SMasami Hiramatsu * If @callback returns DIE_FIND_CB_END, this stores the DIE into 262*e0d153c6SMasami Hiramatsu * @die_mem and returns it. If @callback returns DIE_FIND_CB_CONTINUE, 263*e0d153c6SMasami Hiramatsu * this continues to trace the tree. Optionally, @callback can return 264*e0d153c6SMasami Hiramatsu * DIE_FIND_CB_CHILD and DIE_FIND_CB_SIBLING, those means trace only 265*e0d153c6SMasami Hiramatsu * the children and trace only the siblings respectively. 266*e0d153c6SMasami Hiramatsu * Returns NULL if @callback can't find any appropriate DIE. 267*e0d153c6SMasami Hiramatsu */ 268*e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_child(Dwarf_Die *rt_die, 269*e0d153c6SMasami Hiramatsu int (*callback)(Dwarf_Die *, void *), 270*e0d153c6SMasami Hiramatsu void *data, Dwarf_Die *die_mem) 271*e0d153c6SMasami Hiramatsu { 272*e0d153c6SMasami Hiramatsu Dwarf_Die child_die; 273*e0d153c6SMasami Hiramatsu int ret; 274*e0d153c6SMasami Hiramatsu 275*e0d153c6SMasami Hiramatsu ret = dwarf_child(rt_die, die_mem); 276*e0d153c6SMasami Hiramatsu if (ret != 0) 277*e0d153c6SMasami Hiramatsu return NULL; 278*e0d153c6SMasami Hiramatsu 279*e0d153c6SMasami Hiramatsu do { 280*e0d153c6SMasami Hiramatsu ret = callback(die_mem, data); 281*e0d153c6SMasami Hiramatsu if (ret == DIE_FIND_CB_END) 282*e0d153c6SMasami Hiramatsu return die_mem; 283*e0d153c6SMasami Hiramatsu 284*e0d153c6SMasami Hiramatsu if ((ret & DIE_FIND_CB_CHILD) && 285*e0d153c6SMasami Hiramatsu die_find_child(die_mem, callback, data, &child_die)) { 286*e0d153c6SMasami Hiramatsu memcpy(die_mem, &child_die, sizeof(Dwarf_Die)); 287*e0d153c6SMasami Hiramatsu return die_mem; 288*e0d153c6SMasami Hiramatsu } 289*e0d153c6SMasami Hiramatsu } while ((ret & DIE_FIND_CB_SIBLING) && 290*e0d153c6SMasami Hiramatsu dwarf_siblingof(die_mem, die_mem) == 0); 291*e0d153c6SMasami Hiramatsu 292*e0d153c6SMasami Hiramatsu return NULL; 293*e0d153c6SMasami Hiramatsu } 294*e0d153c6SMasami Hiramatsu 295*e0d153c6SMasami Hiramatsu struct __addr_die_search_param { 296*e0d153c6SMasami Hiramatsu Dwarf_Addr addr; 297*e0d153c6SMasami Hiramatsu Dwarf_Die *die_mem; 298*e0d153c6SMasami Hiramatsu }; 299*e0d153c6SMasami Hiramatsu 300*e0d153c6SMasami Hiramatsu /* die_find callback for non-inlined function search */ 301*e0d153c6SMasami Hiramatsu static int __die_search_func_cb(Dwarf_Die *fn_die, void *data) 302*e0d153c6SMasami Hiramatsu { 303*e0d153c6SMasami Hiramatsu struct __addr_die_search_param *ad = data; 304*e0d153c6SMasami Hiramatsu 305*e0d153c6SMasami Hiramatsu if (dwarf_tag(fn_die) == DW_TAG_subprogram && 306*e0d153c6SMasami Hiramatsu dwarf_haspc(fn_die, ad->addr)) { 307*e0d153c6SMasami Hiramatsu memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die)); 308*e0d153c6SMasami Hiramatsu return DWARF_CB_ABORT; 309*e0d153c6SMasami Hiramatsu } 310*e0d153c6SMasami Hiramatsu return DWARF_CB_OK; 311*e0d153c6SMasami Hiramatsu } 312*e0d153c6SMasami Hiramatsu 313*e0d153c6SMasami Hiramatsu /** 314*e0d153c6SMasami Hiramatsu * die_find_realfunc - Search a non-inlined function at given address 315*e0d153c6SMasami Hiramatsu * @cu_die: a CU DIE which including @addr 316*e0d153c6SMasami Hiramatsu * @addr: target address 317*e0d153c6SMasami Hiramatsu * @die_mem: a buffer for result DIE 318*e0d153c6SMasami Hiramatsu * 319*e0d153c6SMasami Hiramatsu * Search a non-inlined function DIE which includes @addr. Stores the 320*e0d153c6SMasami Hiramatsu * DIE to @die_mem and returns it if found. Returns NULl if failed. 321*e0d153c6SMasami Hiramatsu */ 322*e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr, 323*e0d153c6SMasami Hiramatsu Dwarf_Die *die_mem) 324*e0d153c6SMasami Hiramatsu { 325*e0d153c6SMasami Hiramatsu struct __addr_die_search_param ad; 326*e0d153c6SMasami Hiramatsu ad.addr = addr; 327*e0d153c6SMasami Hiramatsu ad.die_mem = die_mem; 328*e0d153c6SMasami Hiramatsu /* dwarf_getscopes can't find subprogram. */ 329*e0d153c6SMasami Hiramatsu if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0)) 330*e0d153c6SMasami Hiramatsu return NULL; 331*e0d153c6SMasami Hiramatsu else 332*e0d153c6SMasami Hiramatsu return die_mem; 333*e0d153c6SMasami Hiramatsu } 334*e0d153c6SMasami Hiramatsu 335*e0d153c6SMasami Hiramatsu /* die_find callback for inline function search */ 336*e0d153c6SMasami Hiramatsu static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data) 337*e0d153c6SMasami Hiramatsu { 338*e0d153c6SMasami Hiramatsu Dwarf_Addr *addr = data; 339*e0d153c6SMasami Hiramatsu 340*e0d153c6SMasami Hiramatsu if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine && 341*e0d153c6SMasami Hiramatsu dwarf_haspc(die_mem, *addr)) 342*e0d153c6SMasami Hiramatsu return DIE_FIND_CB_END; 343*e0d153c6SMasami Hiramatsu 344*e0d153c6SMasami Hiramatsu return DIE_FIND_CB_CONTINUE; 345*e0d153c6SMasami Hiramatsu } 346*e0d153c6SMasami Hiramatsu 347*e0d153c6SMasami Hiramatsu /** 348*e0d153c6SMasami Hiramatsu * die_find_inlinefunc - Search an inlined function at given address 349*e0d153c6SMasami Hiramatsu * @cu_die: a CU DIE which including @addr 350*e0d153c6SMasami Hiramatsu * @addr: target address 351*e0d153c6SMasami Hiramatsu * @die_mem: a buffer for result DIE 352*e0d153c6SMasami Hiramatsu * 353*e0d153c6SMasami Hiramatsu * Search an inlined function DIE which includes @addr. Stores the 354*e0d153c6SMasami Hiramatsu * DIE to @die_mem and returns it if found. Returns NULl if failed. 355*e0d153c6SMasami Hiramatsu * If several inlined functions are expanded recursively, this trace 356*e0d153c6SMasami Hiramatsu * it and returns deepest one. 357*e0d153c6SMasami Hiramatsu */ 358*e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr, 359*e0d153c6SMasami Hiramatsu Dwarf_Die *die_mem) 360*e0d153c6SMasami Hiramatsu { 361*e0d153c6SMasami Hiramatsu Dwarf_Die tmp_die; 362*e0d153c6SMasami Hiramatsu 363*e0d153c6SMasami Hiramatsu sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die); 364*e0d153c6SMasami Hiramatsu if (!sp_die) 365*e0d153c6SMasami Hiramatsu return NULL; 366*e0d153c6SMasami Hiramatsu 367*e0d153c6SMasami Hiramatsu /* Inlined function could be recursive. Trace it until fail */ 368*e0d153c6SMasami Hiramatsu while (sp_die) { 369*e0d153c6SMasami Hiramatsu memcpy(die_mem, sp_die, sizeof(Dwarf_Die)); 370*e0d153c6SMasami Hiramatsu sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, 371*e0d153c6SMasami Hiramatsu &tmp_die); 372*e0d153c6SMasami Hiramatsu } 373*e0d153c6SMasami Hiramatsu 374*e0d153c6SMasami Hiramatsu return die_mem; 375*e0d153c6SMasami Hiramatsu } 376*e0d153c6SMasami Hiramatsu 377*e0d153c6SMasami Hiramatsu /* Line walker internal parameters */ 378*e0d153c6SMasami Hiramatsu struct __line_walk_param { 379*e0d153c6SMasami Hiramatsu const char *fname; 380*e0d153c6SMasami Hiramatsu line_walk_callback_t callback; 381*e0d153c6SMasami Hiramatsu void *data; 382*e0d153c6SMasami Hiramatsu int retval; 383*e0d153c6SMasami Hiramatsu }; 384*e0d153c6SMasami Hiramatsu 385*e0d153c6SMasami Hiramatsu static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data) 386*e0d153c6SMasami Hiramatsu { 387*e0d153c6SMasami Hiramatsu struct __line_walk_param *lw = data; 388*e0d153c6SMasami Hiramatsu Dwarf_Addr addr; 389*e0d153c6SMasami Hiramatsu int lineno; 390*e0d153c6SMasami Hiramatsu 391*e0d153c6SMasami Hiramatsu if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) { 392*e0d153c6SMasami Hiramatsu lineno = die_get_call_lineno(in_die); 393*e0d153c6SMasami Hiramatsu if (lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) { 394*e0d153c6SMasami Hiramatsu lw->retval = lw->callback(lw->fname, lineno, addr, 395*e0d153c6SMasami Hiramatsu lw->data); 396*e0d153c6SMasami Hiramatsu if (lw->retval != 0) 397*e0d153c6SMasami Hiramatsu return DIE_FIND_CB_END; 398*e0d153c6SMasami Hiramatsu } 399*e0d153c6SMasami Hiramatsu } 400*e0d153c6SMasami Hiramatsu return DIE_FIND_CB_SIBLING; 401*e0d153c6SMasami Hiramatsu } 402*e0d153c6SMasami Hiramatsu 403*e0d153c6SMasami Hiramatsu /* Walk on lines of blocks included in given DIE */ 404*e0d153c6SMasami Hiramatsu static int __die_walk_funclines(Dwarf_Die *sp_die, 405*e0d153c6SMasami Hiramatsu line_walk_callback_t callback, void *data) 406*e0d153c6SMasami Hiramatsu { 407*e0d153c6SMasami Hiramatsu struct __line_walk_param lw = { 408*e0d153c6SMasami Hiramatsu .callback = callback, 409*e0d153c6SMasami Hiramatsu .data = data, 410*e0d153c6SMasami Hiramatsu .retval = 0, 411*e0d153c6SMasami Hiramatsu }; 412*e0d153c6SMasami Hiramatsu Dwarf_Die die_mem; 413*e0d153c6SMasami Hiramatsu Dwarf_Addr addr; 414*e0d153c6SMasami Hiramatsu int lineno; 415*e0d153c6SMasami Hiramatsu 416*e0d153c6SMasami Hiramatsu /* Handle function declaration line */ 417*e0d153c6SMasami Hiramatsu lw.fname = dwarf_decl_file(sp_die); 418*e0d153c6SMasami Hiramatsu if (lw.fname && dwarf_decl_line(sp_die, &lineno) == 0 && 419*e0d153c6SMasami Hiramatsu dwarf_entrypc(sp_die, &addr) == 0) { 420*e0d153c6SMasami Hiramatsu lw.retval = callback(lw.fname, lineno, addr, data); 421*e0d153c6SMasami Hiramatsu if (lw.retval != 0) 422*e0d153c6SMasami Hiramatsu goto done; 423*e0d153c6SMasami Hiramatsu } 424*e0d153c6SMasami Hiramatsu die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem); 425*e0d153c6SMasami Hiramatsu done: 426*e0d153c6SMasami Hiramatsu return lw.retval; 427*e0d153c6SMasami Hiramatsu } 428*e0d153c6SMasami Hiramatsu 429*e0d153c6SMasami Hiramatsu static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data) 430*e0d153c6SMasami Hiramatsu { 431*e0d153c6SMasami Hiramatsu struct __line_walk_param *lw = data; 432*e0d153c6SMasami Hiramatsu 433*e0d153c6SMasami Hiramatsu lw->retval = __die_walk_funclines(sp_die, lw->callback, lw->data); 434*e0d153c6SMasami Hiramatsu if (lw->retval != 0) 435*e0d153c6SMasami Hiramatsu return DWARF_CB_ABORT; 436*e0d153c6SMasami Hiramatsu 437*e0d153c6SMasami Hiramatsu return DWARF_CB_OK; 438*e0d153c6SMasami Hiramatsu } 439*e0d153c6SMasami Hiramatsu 440*e0d153c6SMasami Hiramatsu /** 441*e0d153c6SMasami Hiramatsu * die_walk_lines - Walk on lines inside given DIE 442*e0d153c6SMasami Hiramatsu * @rt_die: a root DIE (CU or subprogram) 443*e0d153c6SMasami Hiramatsu * @callback: callback routine 444*e0d153c6SMasami Hiramatsu * @data: user data 445*e0d153c6SMasami Hiramatsu * 446*e0d153c6SMasami Hiramatsu * Walk on all lines inside given @rt_die and call @callback on each line. 447*e0d153c6SMasami Hiramatsu * If the @rt_die is a function, walk only on the lines inside the function, 448*e0d153c6SMasami Hiramatsu * otherwise @rt_die must be a CU DIE. 449*e0d153c6SMasami Hiramatsu * Note that this walks not only dwarf line list, but also function entries 450*e0d153c6SMasami Hiramatsu * and inline call-site. 451*e0d153c6SMasami Hiramatsu */ 452*e0d153c6SMasami Hiramatsu int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data) 453*e0d153c6SMasami Hiramatsu { 454*e0d153c6SMasami Hiramatsu Dwarf_Lines *lines; 455*e0d153c6SMasami Hiramatsu Dwarf_Line *line; 456*e0d153c6SMasami Hiramatsu Dwarf_Addr addr; 457*e0d153c6SMasami Hiramatsu const char *fname; 458*e0d153c6SMasami Hiramatsu int lineno, ret = 0; 459*e0d153c6SMasami Hiramatsu Dwarf_Die die_mem, *cu_die; 460*e0d153c6SMasami Hiramatsu size_t nlines, i; 461*e0d153c6SMasami Hiramatsu 462*e0d153c6SMasami Hiramatsu /* Get the CU die */ 463*e0d153c6SMasami Hiramatsu if (dwarf_tag(rt_die) == DW_TAG_subprogram) 464*e0d153c6SMasami Hiramatsu cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL); 465*e0d153c6SMasami Hiramatsu else 466*e0d153c6SMasami Hiramatsu cu_die = rt_die; 467*e0d153c6SMasami Hiramatsu if (!cu_die) { 468*e0d153c6SMasami Hiramatsu pr_debug2("Failed to get CU from subprogram\n"); 469*e0d153c6SMasami Hiramatsu return -EINVAL; 470*e0d153c6SMasami Hiramatsu } 471*e0d153c6SMasami Hiramatsu 472*e0d153c6SMasami Hiramatsu /* Get lines list in the CU */ 473*e0d153c6SMasami Hiramatsu if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) { 474*e0d153c6SMasami Hiramatsu pr_debug2("Failed to get source lines on this CU.\n"); 475*e0d153c6SMasami Hiramatsu return -ENOENT; 476*e0d153c6SMasami Hiramatsu } 477*e0d153c6SMasami Hiramatsu pr_debug2("Get %zd lines from this CU\n", nlines); 478*e0d153c6SMasami Hiramatsu 479*e0d153c6SMasami Hiramatsu /* Walk on the lines on lines list */ 480*e0d153c6SMasami Hiramatsu for (i = 0; i < nlines; i++) { 481*e0d153c6SMasami Hiramatsu line = dwarf_onesrcline(lines, i); 482*e0d153c6SMasami Hiramatsu if (line == NULL || 483*e0d153c6SMasami Hiramatsu dwarf_lineno(line, &lineno) != 0 || 484*e0d153c6SMasami Hiramatsu dwarf_lineaddr(line, &addr) != 0) { 485*e0d153c6SMasami Hiramatsu pr_debug2("Failed to get line info. " 486*e0d153c6SMasami Hiramatsu "Possible error in debuginfo.\n"); 487*e0d153c6SMasami Hiramatsu continue; 488*e0d153c6SMasami Hiramatsu } 489*e0d153c6SMasami Hiramatsu /* Filter lines based on address */ 490*e0d153c6SMasami Hiramatsu if (rt_die != cu_die) 491*e0d153c6SMasami Hiramatsu /* 492*e0d153c6SMasami Hiramatsu * Address filtering 493*e0d153c6SMasami Hiramatsu * The line is included in given function, and 494*e0d153c6SMasami Hiramatsu * no inline block includes it. 495*e0d153c6SMasami Hiramatsu */ 496*e0d153c6SMasami Hiramatsu if (!dwarf_haspc(rt_die, addr) || 497*e0d153c6SMasami Hiramatsu die_find_inlinefunc(rt_die, addr, &die_mem)) 498*e0d153c6SMasami Hiramatsu continue; 499*e0d153c6SMasami Hiramatsu /* Get source line */ 500*e0d153c6SMasami Hiramatsu fname = dwarf_linesrc(line, NULL, NULL); 501*e0d153c6SMasami Hiramatsu 502*e0d153c6SMasami Hiramatsu ret = callback(fname, lineno, addr, data); 503*e0d153c6SMasami Hiramatsu if (ret != 0) 504*e0d153c6SMasami Hiramatsu return ret; 505*e0d153c6SMasami Hiramatsu } 506*e0d153c6SMasami Hiramatsu 507*e0d153c6SMasami Hiramatsu /* 508*e0d153c6SMasami Hiramatsu * Dwarf lines doesn't include function declarations and inlined 509*e0d153c6SMasami Hiramatsu * subroutines. We have to check functions list or given function. 510*e0d153c6SMasami Hiramatsu */ 511*e0d153c6SMasami Hiramatsu if (rt_die != cu_die) 512*e0d153c6SMasami Hiramatsu ret = __die_walk_funclines(rt_die, callback, data); 513*e0d153c6SMasami Hiramatsu else { 514*e0d153c6SMasami Hiramatsu struct __line_walk_param param = { 515*e0d153c6SMasami Hiramatsu .callback = callback, 516*e0d153c6SMasami Hiramatsu .data = data, 517*e0d153c6SMasami Hiramatsu .retval = 0, 518*e0d153c6SMasami Hiramatsu }; 519*e0d153c6SMasami Hiramatsu dwarf_getfuncs(cu_die, __die_walk_culines_cb, ¶m, 0); 520*e0d153c6SMasami Hiramatsu ret = param.retval; 521*e0d153c6SMasami Hiramatsu } 522*e0d153c6SMasami Hiramatsu 523*e0d153c6SMasami Hiramatsu return ret; 524*e0d153c6SMasami Hiramatsu } 525*e0d153c6SMasami Hiramatsu 526*e0d153c6SMasami Hiramatsu struct __find_variable_param { 527*e0d153c6SMasami Hiramatsu const char *name; 528*e0d153c6SMasami Hiramatsu Dwarf_Addr addr; 529*e0d153c6SMasami Hiramatsu }; 530*e0d153c6SMasami Hiramatsu 531*e0d153c6SMasami Hiramatsu static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data) 532*e0d153c6SMasami Hiramatsu { 533*e0d153c6SMasami Hiramatsu struct __find_variable_param *fvp = data; 534*e0d153c6SMasami Hiramatsu int tag; 535*e0d153c6SMasami Hiramatsu 536*e0d153c6SMasami Hiramatsu tag = dwarf_tag(die_mem); 537*e0d153c6SMasami Hiramatsu if ((tag == DW_TAG_formal_parameter || 538*e0d153c6SMasami Hiramatsu tag == DW_TAG_variable) && 539*e0d153c6SMasami Hiramatsu die_compare_name(die_mem, fvp->name)) 540*e0d153c6SMasami Hiramatsu return DIE_FIND_CB_END; 541*e0d153c6SMasami Hiramatsu 542*e0d153c6SMasami Hiramatsu if (dwarf_haspc(die_mem, fvp->addr)) 543*e0d153c6SMasami Hiramatsu return DIE_FIND_CB_CONTINUE; 544*e0d153c6SMasami Hiramatsu else 545*e0d153c6SMasami Hiramatsu return DIE_FIND_CB_SIBLING; 546*e0d153c6SMasami Hiramatsu } 547*e0d153c6SMasami Hiramatsu 548*e0d153c6SMasami Hiramatsu /** 549*e0d153c6SMasami Hiramatsu * die_find_variable_at - Find a given name variable at given address 550*e0d153c6SMasami Hiramatsu * @sp_die: a function DIE 551*e0d153c6SMasami Hiramatsu * @name: variable name 552*e0d153c6SMasami Hiramatsu * @addr: address 553*e0d153c6SMasami Hiramatsu * @die_mem: a buffer for result DIE 554*e0d153c6SMasami Hiramatsu * 555*e0d153c6SMasami Hiramatsu * Find a variable DIE called @name at @addr in @sp_die. 556*e0d153c6SMasami Hiramatsu */ 557*e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name, 558*e0d153c6SMasami Hiramatsu Dwarf_Addr addr, Dwarf_Die *die_mem) 559*e0d153c6SMasami Hiramatsu { 560*e0d153c6SMasami Hiramatsu struct __find_variable_param fvp = { .name = name, .addr = addr}; 561*e0d153c6SMasami Hiramatsu 562*e0d153c6SMasami Hiramatsu return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp, 563*e0d153c6SMasami Hiramatsu die_mem); 564*e0d153c6SMasami Hiramatsu } 565*e0d153c6SMasami Hiramatsu 566*e0d153c6SMasami Hiramatsu static int __die_find_member_cb(Dwarf_Die *die_mem, void *data) 567*e0d153c6SMasami Hiramatsu { 568*e0d153c6SMasami Hiramatsu const char *name = data; 569*e0d153c6SMasami Hiramatsu 570*e0d153c6SMasami Hiramatsu if ((dwarf_tag(die_mem) == DW_TAG_member) && 571*e0d153c6SMasami Hiramatsu die_compare_name(die_mem, name)) 572*e0d153c6SMasami Hiramatsu return DIE_FIND_CB_END; 573*e0d153c6SMasami Hiramatsu 574*e0d153c6SMasami Hiramatsu return DIE_FIND_CB_SIBLING; 575*e0d153c6SMasami Hiramatsu } 576*e0d153c6SMasami Hiramatsu 577*e0d153c6SMasami Hiramatsu /** 578*e0d153c6SMasami Hiramatsu * die_find_member - Find a given name member in a data structure 579*e0d153c6SMasami Hiramatsu * @st_die: a data structure type DIE 580*e0d153c6SMasami Hiramatsu * @name: member name 581*e0d153c6SMasami Hiramatsu * @die_mem: a buffer for result DIE 582*e0d153c6SMasami Hiramatsu * 583*e0d153c6SMasami Hiramatsu * Find a member DIE called @name in @st_die. 584*e0d153c6SMasami Hiramatsu */ 585*e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name, 586*e0d153c6SMasami Hiramatsu Dwarf_Die *die_mem) 587*e0d153c6SMasami Hiramatsu { 588*e0d153c6SMasami Hiramatsu return die_find_child(st_die, __die_find_member_cb, (void *)name, 589*e0d153c6SMasami Hiramatsu die_mem); 590*e0d153c6SMasami Hiramatsu } 591*e0d153c6SMasami Hiramatsu 592*e0d153c6SMasami Hiramatsu /** 593*e0d153c6SMasami Hiramatsu * die_get_typename - Get the name of given variable DIE 594*e0d153c6SMasami Hiramatsu * @vr_die: a variable DIE 595*e0d153c6SMasami Hiramatsu * @buf: a buffer for result type name 596*e0d153c6SMasami Hiramatsu * @len: a max-length of @buf 597*e0d153c6SMasami Hiramatsu * 598*e0d153c6SMasami Hiramatsu * Get the name of @vr_die and stores it to @buf. Return the actual length 599*e0d153c6SMasami Hiramatsu * of type name if succeeded. Return -E2BIG if @len is not enough long, and 600*e0d153c6SMasami Hiramatsu * Return -ENOENT if failed to find type name. 601*e0d153c6SMasami Hiramatsu * Note that the result will stores typedef name if possible, and stores 602*e0d153c6SMasami Hiramatsu * "*(function_type)" if the type is a function pointer. 603*e0d153c6SMasami Hiramatsu */ 604*e0d153c6SMasami Hiramatsu int die_get_typename(Dwarf_Die *vr_die, char *buf, int len) 605*e0d153c6SMasami Hiramatsu { 606*e0d153c6SMasami Hiramatsu Dwarf_Die type; 607*e0d153c6SMasami Hiramatsu int tag, ret, ret2; 608*e0d153c6SMasami Hiramatsu const char *tmp = ""; 609*e0d153c6SMasami Hiramatsu 610*e0d153c6SMasami Hiramatsu if (__die_get_real_type(vr_die, &type) == NULL) 611*e0d153c6SMasami Hiramatsu return -ENOENT; 612*e0d153c6SMasami Hiramatsu 613*e0d153c6SMasami Hiramatsu tag = dwarf_tag(&type); 614*e0d153c6SMasami Hiramatsu if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type) 615*e0d153c6SMasami Hiramatsu tmp = "*"; 616*e0d153c6SMasami Hiramatsu else if (tag == DW_TAG_subroutine_type) { 617*e0d153c6SMasami Hiramatsu /* Function pointer */ 618*e0d153c6SMasami Hiramatsu ret = snprintf(buf, len, "(function_type)"); 619*e0d153c6SMasami Hiramatsu return (ret >= len) ? -E2BIG : ret; 620*e0d153c6SMasami Hiramatsu } else { 621*e0d153c6SMasami Hiramatsu if (!dwarf_diename(&type)) 622*e0d153c6SMasami Hiramatsu return -ENOENT; 623*e0d153c6SMasami Hiramatsu if (tag == DW_TAG_union_type) 624*e0d153c6SMasami Hiramatsu tmp = "union "; 625*e0d153c6SMasami Hiramatsu else if (tag == DW_TAG_structure_type) 626*e0d153c6SMasami Hiramatsu tmp = "struct "; 627*e0d153c6SMasami Hiramatsu /* Write a base name */ 628*e0d153c6SMasami Hiramatsu ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type)); 629*e0d153c6SMasami Hiramatsu return (ret >= len) ? -E2BIG : ret; 630*e0d153c6SMasami Hiramatsu } 631*e0d153c6SMasami Hiramatsu ret = die_get_typename(&type, buf, len); 632*e0d153c6SMasami Hiramatsu if (ret > 0) { 633*e0d153c6SMasami Hiramatsu ret2 = snprintf(buf + ret, len - ret, "%s", tmp); 634*e0d153c6SMasami Hiramatsu ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret; 635*e0d153c6SMasami Hiramatsu } 636*e0d153c6SMasami Hiramatsu return ret; 637*e0d153c6SMasami Hiramatsu } 638*e0d153c6SMasami Hiramatsu 639*e0d153c6SMasami Hiramatsu /** 640*e0d153c6SMasami Hiramatsu * die_get_varname - Get the name and type of given variable DIE 641*e0d153c6SMasami Hiramatsu * @vr_die: a variable DIE 642*e0d153c6SMasami Hiramatsu * @buf: a buffer for type and variable name 643*e0d153c6SMasami Hiramatsu * @len: the max-length of @buf 644*e0d153c6SMasami Hiramatsu * 645*e0d153c6SMasami Hiramatsu * Get the name and type of @vr_die and stores it in @buf as "type\tname". 646*e0d153c6SMasami Hiramatsu */ 647*e0d153c6SMasami Hiramatsu int die_get_varname(Dwarf_Die *vr_die, char *buf, int len) 648*e0d153c6SMasami Hiramatsu { 649*e0d153c6SMasami Hiramatsu int ret, ret2; 650*e0d153c6SMasami Hiramatsu 651*e0d153c6SMasami Hiramatsu ret = die_get_typename(vr_die, buf, len); 652*e0d153c6SMasami Hiramatsu if (ret < 0) { 653*e0d153c6SMasami Hiramatsu pr_debug("Failed to get type, make it unknown.\n"); 654*e0d153c6SMasami Hiramatsu ret = snprintf(buf, len, "(unknown_type)"); 655*e0d153c6SMasami Hiramatsu } 656*e0d153c6SMasami Hiramatsu if (ret > 0) { 657*e0d153c6SMasami Hiramatsu ret2 = snprintf(buf + ret, len - ret, "\t%s", 658*e0d153c6SMasami Hiramatsu dwarf_diename(vr_die)); 659*e0d153c6SMasami Hiramatsu ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret; 660*e0d153c6SMasami Hiramatsu } 661*e0d153c6SMasami Hiramatsu return ret; 662*e0d153c6SMasami Hiramatsu } 663*e0d153c6SMasami Hiramatsu 664