dwarf-aux.c 17 KB

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