dwarf-aux.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data);
  90. /**
  91. * cu_walk_functions_at - Walk on function DIEs at given address
  92. * @cu_die: A CU DIE
  93. * @addr: An address
  94. * @callback: A callback which called with found DIEs
  95. * @data: A user data
  96. *
  97. * Walk on function DIEs at given @addr in @cu_die. Passed DIEs
  98. * should be subprogram or inlined-subroutines.
  99. */
  100. int cu_walk_functions_at(Dwarf_Die *cu_die, Dwarf_Addr addr,
  101. int (*callback)(Dwarf_Die *, void *), void *data)
  102. {
  103. Dwarf_Die die_mem;
  104. Dwarf_Die *sc_die;
  105. int ret = -ENOENT;
  106. /* Inlined function could be recursive. Trace it until fail */
  107. for (sc_die = die_find_realfunc(cu_die, addr, &die_mem);
  108. sc_die != NULL;
  109. sc_die = die_find_child(sc_die, __die_find_inline_cb, &addr,
  110. &die_mem)) {
  111. ret = callback(sc_die, data);
  112. if (ret)
  113. break;
  114. }
  115. return ret;
  116. }
  117. /**
  118. * die_compare_name - Compare diename and tname
  119. * @dw_die: a DIE
  120. * @tname: a string of target name
  121. *
  122. * Compare the name of @dw_die and @tname. Return false if @dw_die has no name.
  123. */
  124. bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
  125. {
  126. const char *name;
  127. name = dwarf_diename(dw_die);
  128. return name ? (strcmp(tname, name) == 0) : false;
  129. }
  130. /**
  131. * die_match_name - Match diename and glob
  132. * @dw_die: a DIE
  133. * @glob: a string of target glob pattern
  134. *
  135. * Glob matching the name of @dw_die and @glob. Return false if matching fail.
  136. */
  137. bool die_match_name(Dwarf_Die *dw_die, const char *glob)
  138. {
  139. const char *name;
  140. name = dwarf_diename(dw_die);
  141. return name ? strglobmatch(name, glob) : false;
  142. }
  143. /**
  144. * die_get_call_lineno - Get callsite line number of inline-function instance
  145. * @in_die: a DIE of an inlined function instance
  146. *
  147. * Get call-site line number of @in_die. This means from where the inline
  148. * function is called.
  149. */
  150. int die_get_call_lineno(Dwarf_Die *in_die)
  151. {
  152. Dwarf_Attribute attr;
  153. Dwarf_Word ret;
  154. if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
  155. return -ENOENT;
  156. dwarf_formudata(&attr, &ret);
  157. return (int)ret;
  158. }
  159. /**
  160. * die_get_type - Get type DIE
  161. * @vr_die: a DIE of a variable
  162. * @die_mem: where to store a type DIE
  163. *
  164. * Get a DIE of the type of given variable (@vr_die), and store
  165. * it to die_mem. Return NULL if fails to get a type DIE.
  166. */
  167. Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  168. {
  169. Dwarf_Attribute attr;
  170. if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
  171. dwarf_formref_die(&attr, die_mem))
  172. return die_mem;
  173. else
  174. return NULL;
  175. }
  176. /* Get a type die, but skip qualifiers */
  177. static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  178. {
  179. int tag;
  180. do {
  181. vr_die = die_get_type(vr_die, die_mem);
  182. if (!vr_die)
  183. break;
  184. tag = dwarf_tag(vr_die);
  185. } while (tag == DW_TAG_const_type ||
  186. tag == DW_TAG_restrict_type ||
  187. tag == DW_TAG_volatile_type ||
  188. tag == DW_TAG_shared_type);
  189. return vr_die;
  190. }
  191. /**
  192. * die_get_real_type - Get a type die, but skip qualifiers and typedef
  193. * @vr_die: a DIE of a variable
  194. * @die_mem: where to store a type DIE
  195. *
  196. * Get a DIE of the type of given variable (@vr_die), and store
  197. * it to die_mem. Return NULL if fails to get a type DIE.
  198. * If the type is qualifiers (e.g. const) or typedef, this skips it
  199. * and tries to find real type (structure or basic types, e.g. int).
  200. */
  201. Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  202. {
  203. do {
  204. vr_die = __die_get_real_type(vr_die, die_mem);
  205. } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
  206. return vr_die;
  207. }
  208. /* Get attribute and translate it as a udata */
  209. static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
  210. Dwarf_Word *result)
  211. {
  212. Dwarf_Attribute attr;
  213. if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
  214. dwarf_formudata(&attr, result) != 0)
  215. return -ENOENT;
  216. return 0;
  217. }
  218. /* Get attribute and translate it as a sdata */
  219. static int die_get_attr_sdata(Dwarf_Die *tp_die, unsigned int attr_name,
  220. Dwarf_Sword *result)
  221. {
  222. Dwarf_Attribute attr;
  223. if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
  224. dwarf_formsdata(&attr, result) != 0)
  225. return -ENOENT;
  226. return 0;
  227. }
  228. /**
  229. * die_is_signed_type - Check whether a type DIE is signed or not
  230. * @tp_die: a DIE of a type
  231. *
  232. * Get the encoding of @tp_die and return true if the encoding
  233. * is signed.
  234. */
  235. bool die_is_signed_type(Dwarf_Die *tp_die)
  236. {
  237. Dwarf_Word ret;
  238. if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
  239. return false;
  240. return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
  241. ret == DW_ATE_signed_fixed);
  242. }
  243. /**
  244. * die_is_func_def - Ensure that this DIE is a subprogram and definition
  245. * @dw_die: a DIE
  246. *
  247. * Ensure that this DIE is a subprogram and NOT a declaration. This
  248. * returns true if @dw_die is a function definition.
  249. **/
  250. bool die_is_func_def(Dwarf_Die *dw_die)
  251. {
  252. Dwarf_Attribute attr;
  253. return (dwarf_tag(dw_die) == DW_TAG_subprogram &&
  254. dwarf_attr(dw_die, DW_AT_declaration, &attr) == NULL);
  255. }
  256. /**
  257. * die_is_func_instance - Ensure that this DIE is an instance of a subprogram
  258. * @dw_die: a DIE
  259. *
  260. * Ensure that this DIE is an instance (which has an entry address).
  261. * This returns true if @dw_die is a function instance. If not, you need to
  262. * call die_walk_instances() to find actual instances.
  263. **/
  264. bool die_is_func_instance(Dwarf_Die *dw_die)
  265. {
  266. Dwarf_Addr tmp;
  267. /* Actually gcc optimizes non-inline as like as inlined */
  268. return !dwarf_func_inline(dw_die) && dwarf_entrypc(dw_die, &tmp) == 0;
  269. }
  270. /**
  271. * die_get_data_member_location - Get the data-member offset
  272. * @mb_die: a DIE of a member of a data structure
  273. * @offs: The offset of the member in the data structure
  274. *
  275. * Get the offset of @mb_die in the data structure including @mb_die, and
  276. * stores result offset to @offs. If any error occurs this returns errno.
  277. */
  278. int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
  279. {
  280. Dwarf_Attribute attr;
  281. Dwarf_Op *expr;
  282. size_t nexpr;
  283. int ret;
  284. if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
  285. return -ENOENT;
  286. if (dwarf_formudata(&attr, offs) != 0) {
  287. /* DW_AT_data_member_location should be DW_OP_plus_uconst */
  288. ret = dwarf_getlocation(&attr, &expr, &nexpr);
  289. if (ret < 0 || nexpr == 0)
  290. return -ENOENT;
  291. if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
  292. pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
  293. expr[0].atom, nexpr);
  294. return -ENOTSUP;
  295. }
  296. *offs = (Dwarf_Word)expr[0].number;
  297. }
  298. return 0;
  299. }
  300. /* Get the call file index number in CU DIE */
  301. static int die_get_call_fileno(Dwarf_Die *in_die)
  302. {
  303. Dwarf_Sword idx;
  304. if (die_get_attr_sdata(in_die, DW_AT_call_file, &idx) == 0)
  305. return (int)idx;
  306. else
  307. return -ENOENT;
  308. }
  309. /* Get the declared file index number in CU DIE */
  310. static int die_get_decl_fileno(Dwarf_Die *pdie)
  311. {
  312. Dwarf_Sword idx;
  313. if (die_get_attr_sdata(pdie, DW_AT_decl_file, &idx) == 0)
  314. return (int)idx;
  315. else
  316. return -ENOENT;
  317. }
  318. /**
  319. * die_get_call_file - Get callsite file name of inlined function instance
  320. * @in_die: a DIE of an inlined function instance
  321. *
  322. * Get call-site file name of @in_die. This means from which file the inline
  323. * function is called.
  324. */
  325. const char *die_get_call_file(Dwarf_Die *in_die)
  326. {
  327. Dwarf_Die cu_die;
  328. Dwarf_Files *files;
  329. int idx;
  330. idx = die_get_call_fileno(in_die);
  331. if (idx < 0 || !dwarf_diecu(in_die, &cu_die, NULL, NULL) ||
  332. dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
  333. return NULL;
  334. return dwarf_filesrc(files, idx, NULL, NULL);
  335. }
  336. /**
  337. * die_find_child - Generic DIE search function in DIE tree
  338. * @rt_die: a root DIE
  339. * @callback: a callback function
  340. * @data: a user data passed to the callback function
  341. * @die_mem: a buffer for result DIE
  342. *
  343. * Trace DIE tree from @rt_die and call @callback for each child DIE.
  344. * If @callback returns DIE_FIND_CB_END, this stores the DIE into
  345. * @die_mem and returns it. If @callback returns DIE_FIND_CB_CONTINUE,
  346. * this continues to trace the tree. Optionally, @callback can return
  347. * DIE_FIND_CB_CHILD and DIE_FIND_CB_SIBLING, those means trace only
  348. * the children and trace only the siblings respectively.
  349. * Returns NULL if @callback can't find any appropriate DIE.
  350. */
  351. Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
  352. int (*callback)(Dwarf_Die *, void *),
  353. void *data, Dwarf_Die *die_mem)
  354. {
  355. Dwarf_Die child_die;
  356. int ret;
  357. ret = dwarf_child(rt_die, die_mem);
  358. if (ret != 0)
  359. return NULL;
  360. do {
  361. ret = callback(die_mem, data);
  362. if (ret == DIE_FIND_CB_END)
  363. return die_mem;
  364. if ((ret & DIE_FIND_CB_CHILD) &&
  365. die_find_child(die_mem, callback, data, &child_die)) {
  366. memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
  367. return die_mem;
  368. }
  369. } while ((ret & DIE_FIND_CB_SIBLING) &&
  370. dwarf_siblingof(die_mem, die_mem) == 0);
  371. return NULL;
  372. }
  373. struct __addr_die_search_param {
  374. Dwarf_Addr addr;
  375. Dwarf_Die *die_mem;
  376. };
  377. static int __die_search_func_tail_cb(Dwarf_Die *fn_die, void *data)
  378. {
  379. struct __addr_die_search_param *ad = data;
  380. Dwarf_Addr addr = 0;
  381. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  382. !dwarf_highpc(fn_die, &addr) &&
  383. addr == ad->addr) {
  384. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  385. return DWARF_CB_ABORT;
  386. }
  387. return DWARF_CB_OK;
  388. }
  389. /**
  390. * die_find_tailfunc - Search for a non-inlined function with tail call at
  391. * given address
  392. * @cu_die: a CU DIE which including @addr
  393. * @addr: target address
  394. * @die_mem: a buffer for result DIE
  395. *
  396. * Search for a non-inlined function DIE with tail call at @addr. Stores the
  397. * DIE to @die_mem and returns it if found. Returns NULL if failed.
  398. */
  399. Dwarf_Die *die_find_tailfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
  400. Dwarf_Die *die_mem)
  401. {
  402. struct __addr_die_search_param ad;
  403. ad.addr = addr;
  404. ad.die_mem = die_mem;
  405. /* dwarf_getscopes can't find subprogram. */
  406. if (!dwarf_getfuncs(cu_die, __die_search_func_tail_cb, &ad, 0))
  407. return NULL;
  408. else
  409. return die_mem;
  410. }
  411. /* die_find callback for non-inlined function search */
  412. static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
  413. {
  414. struct __addr_die_search_param *ad = data;
  415. /*
  416. * Since a declaration entry doesn't has given pc, this always returns
  417. * function definition entry.
  418. */
  419. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  420. dwarf_haspc(fn_die, ad->addr)) {
  421. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  422. return DWARF_CB_ABORT;
  423. }
  424. return DWARF_CB_OK;
  425. }
  426. /**
  427. * die_find_realfunc - Search a non-inlined function at given address
  428. * @cu_die: a CU DIE which including @addr
  429. * @addr: target address
  430. * @die_mem: a buffer for result DIE
  431. *
  432. * Search a non-inlined function DIE which includes @addr. Stores the
  433. * DIE to @die_mem and returns it if found. Returns NULL if failed.
  434. */
  435. Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
  436. Dwarf_Die *die_mem)
  437. {
  438. struct __addr_die_search_param ad;
  439. ad.addr = addr;
  440. ad.die_mem = die_mem;
  441. /* dwarf_getscopes can't find subprogram. */
  442. if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
  443. return NULL;
  444. else
  445. return die_mem;
  446. }
  447. /* die_find callback for inline function search */
  448. static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
  449. {
  450. Dwarf_Addr *addr = data;
  451. if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
  452. dwarf_haspc(die_mem, *addr))
  453. return DIE_FIND_CB_END;
  454. return DIE_FIND_CB_CONTINUE;
  455. }
  456. /**
  457. * die_find_top_inlinefunc - Search the top inlined function at given address
  458. * @sp_die: a subprogram DIE which including @addr
  459. * @addr: target address
  460. * @die_mem: a buffer for result DIE
  461. *
  462. * Search an inlined function DIE which includes @addr. Stores the
  463. * DIE to @die_mem and returns it if found. Returns NULL if failed.
  464. * Even if several inlined functions are expanded recursively, this
  465. * doesn't trace it down, and returns the topmost one.
  466. */
  467. Dwarf_Die *die_find_top_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  468. Dwarf_Die *die_mem)
  469. {
  470. return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
  471. }
  472. /**
  473. * die_find_inlinefunc - Search an inlined function at given address
  474. * @sp_die: a subprogram DIE which including @addr
  475. * @addr: target address
  476. * @die_mem: a buffer for result DIE
  477. *
  478. * Search an inlined function DIE which includes @addr. Stores the
  479. * DIE to @die_mem and returns it if found. Returns NULL if failed.
  480. * If several inlined functions are expanded recursively, this trace
  481. * it down and returns deepest one.
  482. */
  483. Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  484. Dwarf_Die *die_mem)
  485. {
  486. Dwarf_Die tmp_die;
  487. sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
  488. if (!sp_die)
  489. return NULL;
  490. /* Inlined function could be recursive. Trace it until fail */
  491. while (sp_die) {
  492. memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
  493. sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
  494. &tmp_die);
  495. }
  496. return die_mem;
  497. }
  498. struct __instance_walk_param {
  499. void *addr;
  500. int (*callback)(Dwarf_Die *, void *);
  501. void *data;
  502. int retval;
  503. };
  504. static int __die_walk_instances_cb(Dwarf_Die *inst, void *data)
  505. {
  506. struct __instance_walk_param *iwp = data;
  507. Dwarf_Attribute attr_mem;
  508. Dwarf_Die origin_mem;
  509. Dwarf_Attribute *attr;
  510. Dwarf_Die *origin;
  511. int tmp;
  512. attr = dwarf_attr(inst, DW_AT_abstract_origin, &attr_mem);
  513. if (attr == NULL)
  514. return DIE_FIND_CB_CONTINUE;
  515. origin = dwarf_formref_die(attr, &origin_mem);
  516. if (origin == NULL || origin->addr != iwp->addr)
  517. return DIE_FIND_CB_CONTINUE;
  518. /* Ignore redundant instances */
  519. if (dwarf_tag(inst) == DW_TAG_inlined_subroutine) {
  520. dwarf_decl_line(origin, &tmp);
  521. if (die_get_call_lineno(inst) == tmp) {
  522. tmp = die_get_decl_fileno(origin);
  523. if (die_get_call_fileno(inst) == tmp)
  524. return DIE_FIND_CB_CONTINUE;
  525. }
  526. }
  527. iwp->retval = iwp->callback(inst, iwp->data);
  528. return (iwp->retval) ? DIE_FIND_CB_END : DIE_FIND_CB_CONTINUE;
  529. }
  530. /**
  531. * die_walk_instances - Walk on instances of given DIE
  532. * @or_die: an abstract original DIE
  533. * @callback: a callback function which is called with instance DIE
  534. * @data: user data
  535. *
  536. * Walk on the instances of give @in_die. @in_die must be an inlined function
  537. * declartion. This returns the return value of @callback if it returns
  538. * non-zero value, or -ENOENT if there is no instance.
  539. */
  540. int die_walk_instances(Dwarf_Die *or_die, int (*callback)(Dwarf_Die *, void *),
  541. void *data)
  542. {
  543. Dwarf_Die cu_die;
  544. Dwarf_Die die_mem;
  545. struct __instance_walk_param iwp = {
  546. .addr = or_die->addr,
  547. .callback = callback,
  548. .data = data,
  549. .retval = -ENOENT,
  550. };
  551. if (dwarf_diecu(or_die, &cu_die, NULL, NULL) == NULL)
  552. return -ENOENT;
  553. die_find_child(&cu_die, __die_walk_instances_cb, &iwp, &die_mem);
  554. return iwp.retval;
  555. }
  556. /* Line walker internal parameters */
  557. struct __line_walk_param {
  558. bool recursive;
  559. line_walk_callback_t callback;
  560. void *data;
  561. int retval;
  562. };
  563. static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
  564. {
  565. struct __line_walk_param *lw = data;
  566. Dwarf_Addr addr = 0;
  567. const char *fname;
  568. int lineno;
  569. if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
  570. fname = die_get_call_file(in_die);
  571. lineno = die_get_call_lineno(in_die);
  572. if (fname && lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) {
  573. lw->retval = lw->callback(fname, lineno, addr, lw->data);
  574. if (lw->retval != 0)
  575. return DIE_FIND_CB_END;
  576. }
  577. }
  578. if (!lw->recursive)
  579. /* Don't need to search recursively */
  580. return DIE_FIND_CB_SIBLING;
  581. if (addr) {
  582. fname = dwarf_decl_file(in_die);
  583. if (fname && dwarf_decl_line(in_die, &lineno) == 0) {
  584. lw->retval = lw->callback(fname, lineno, addr, lw->data);
  585. if (lw->retval != 0)
  586. return DIE_FIND_CB_END;
  587. }
  588. }
  589. /* Continue to search nested inlined function call-sites */
  590. return DIE_FIND_CB_CONTINUE;
  591. }
  592. /* Walk on lines of blocks included in given DIE */
  593. static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive,
  594. line_walk_callback_t callback, void *data)
  595. {
  596. struct __line_walk_param lw = {
  597. .recursive = recursive,
  598. .callback = callback,
  599. .data = data,
  600. .retval = 0,
  601. };
  602. Dwarf_Die die_mem;
  603. Dwarf_Addr addr;
  604. const char *fname;
  605. int lineno;
  606. /* Handle function declaration line */
  607. fname = dwarf_decl_file(sp_die);
  608. if (fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
  609. dwarf_entrypc(sp_die, &addr) == 0) {
  610. lw.retval = callback(fname, lineno, addr, data);
  611. if (lw.retval != 0)
  612. goto done;
  613. }
  614. die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
  615. done:
  616. return lw.retval;
  617. }
  618. static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
  619. {
  620. struct __line_walk_param *lw = data;
  621. lw->retval = __die_walk_funclines(sp_die, true, lw->callback, lw->data);
  622. if (lw->retval != 0)
  623. return DWARF_CB_ABORT;
  624. return DWARF_CB_OK;
  625. }
  626. /**
  627. * die_walk_lines - Walk on lines inside given DIE
  628. * @rt_die: a root DIE (CU, subprogram or inlined_subroutine)
  629. * @callback: callback routine
  630. * @data: user data
  631. *
  632. * Walk on all lines inside given @rt_die and call @callback on each line.
  633. * If the @rt_die is a function, walk only on the lines inside the function,
  634. * otherwise @rt_die must be a CU DIE.
  635. * Note that this walks not only dwarf line list, but also function entries
  636. * and inline call-site.
  637. */
  638. int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data)
  639. {
  640. Dwarf_Lines *lines;
  641. Dwarf_Line *line;
  642. Dwarf_Addr addr;
  643. const char *fname, *decf = NULL;
  644. int lineno, ret = 0;
  645. int decl = 0, inl;
  646. Dwarf_Die die_mem, *cu_die;
  647. size_t nlines, i;
  648. /* Get the CU die */
  649. if (dwarf_tag(rt_die) != DW_TAG_compile_unit) {
  650. cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL);
  651. dwarf_decl_line(rt_die, &decl);
  652. decf = dwarf_decl_file(rt_die);
  653. } else
  654. cu_die = rt_die;
  655. if (!cu_die) {
  656. pr_debug2("Failed to get CU from given DIE.\n");
  657. return -EINVAL;
  658. }
  659. /* Get lines list in the CU */
  660. if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
  661. pr_debug2("Failed to get source lines on this CU.\n");
  662. return -ENOENT;
  663. }
  664. pr_debug2("Get %zd lines from this CU\n", nlines);
  665. /* Walk on the lines on lines list */
  666. for (i = 0; i < nlines; i++) {
  667. line = dwarf_onesrcline(lines, i);
  668. if (line == NULL ||
  669. dwarf_lineno(line, &lineno) != 0 ||
  670. dwarf_lineaddr(line, &addr) != 0) {
  671. pr_debug2("Failed to get line info. "
  672. "Possible error in debuginfo.\n");
  673. continue;
  674. }
  675. /* Filter lines based on address */
  676. if (rt_die != cu_die) {
  677. /*
  678. * Address filtering
  679. * The line is included in given function, and
  680. * no inline block includes it.
  681. */
  682. if (!dwarf_haspc(rt_die, addr))
  683. continue;
  684. if (die_find_inlinefunc(rt_die, addr, &die_mem)) {
  685. dwarf_decl_line(&die_mem, &inl);
  686. if (inl != decl ||
  687. decf != dwarf_decl_file(&die_mem))
  688. continue;
  689. }
  690. }
  691. /* Get source line */
  692. fname = dwarf_linesrc(line, NULL, NULL);
  693. ret = callback(fname, lineno, addr, data);
  694. if (ret != 0)
  695. return ret;
  696. }
  697. /*
  698. * Dwarf lines doesn't include function declarations and inlined
  699. * subroutines. We have to check functions list or given function.
  700. */
  701. if (rt_die != cu_die)
  702. /*
  703. * Don't need walk functions recursively, because nested
  704. * inlined functions don't have lines of the specified DIE.
  705. */
  706. ret = __die_walk_funclines(rt_die, false, callback, data);
  707. else {
  708. struct __line_walk_param param = {
  709. .callback = callback,
  710. .data = data,
  711. .retval = 0,
  712. };
  713. dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
  714. ret = param.retval;
  715. }
  716. return ret;
  717. }
  718. struct __find_variable_param {
  719. const char *name;
  720. Dwarf_Addr addr;
  721. };
  722. static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
  723. {
  724. struct __find_variable_param *fvp = data;
  725. Dwarf_Attribute attr;
  726. int tag;
  727. tag = dwarf_tag(die_mem);
  728. if ((tag == DW_TAG_formal_parameter ||
  729. tag == DW_TAG_variable) &&
  730. die_compare_name(die_mem, fvp->name) &&
  731. /* Does the DIE have location information or external instance? */
  732. (dwarf_attr(die_mem, DW_AT_external, &attr) ||
  733. dwarf_attr(die_mem, DW_AT_location, &attr)))
  734. return DIE_FIND_CB_END;
  735. if (dwarf_haspc(die_mem, fvp->addr))
  736. return DIE_FIND_CB_CONTINUE;
  737. else
  738. return DIE_FIND_CB_SIBLING;
  739. }
  740. /**
  741. * die_find_variable_at - Find a given name variable at given address
  742. * @sp_die: a function DIE
  743. * @name: variable name
  744. * @addr: address
  745. * @die_mem: a buffer for result DIE
  746. *
  747. * Find a variable DIE called @name at @addr in @sp_die.
  748. */
  749. Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
  750. Dwarf_Addr addr, Dwarf_Die *die_mem)
  751. {
  752. struct __find_variable_param fvp = { .name = name, .addr = addr};
  753. return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
  754. die_mem);
  755. }
  756. static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
  757. {
  758. const char *name = data;
  759. if (dwarf_tag(die_mem) == DW_TAG_member) {
  760. if (die_compare_name(die_mem, name))
  761. return DIE_FIND_CB_END;
  762. else if (!dwarf_diename(die_mem)) { /* Unnamed structure */
  763. Dwarf_Die type_die, tmp_die;
  764. if (die_get_type(die_mem, &type_die) &&
  765. die_find_member(&type_die, name, &tmp_die))
  766. return DIE_FIND_CB_END;
  767. }
  768. }
  769. return DIE_FIND_CB_SIBLING;
  770. }
  771. /**
  772. * die_find_member - Find a given name member in a data structure
  773. * @st_die: a data structure type DIE
  774. * @name: member name
  775. * @die_mem: a buffer for result DIE
  776. *
  777. * Find a member DIE called @name in @st_die.
  778. */
  779. Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
  780. Dwarf_Die *die_mem)
  781. {
  782. return die_find_child(st_die, __die_find_member_cb, (void *)name,
  783. die_mem);
  784. }
  785. /**
  786. * die_get_typename - Get the name of given variable DIE
  787. * @vr_die: a variable DIE
  788. * @buf: a strbuf for result type name
  789. *
  790. * Get the name of @vr_die and stores it to @buf. Return 0 if succeeded.
  791. * and Return -ENOENT if failed to find type name.
  792. * Note that the result will stores typedef name if possible, and stores
  793. * "*(function_type)" if the type is a function pointer.
  794. */
  795. int die_get_typename(Dwarf_Die *vr_die, struct strbuf *buf)
  796. {
  797. Dwarf_Die type;
  798. int tag, ret;
  799. const char *tmp = "";
  800. if (__die_get_real_type(vr_die, &type) == NULL)
  801. return -ENOENT;
  802. tag = dwarf_tag(&type);
  803. if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
  804. tmp = "*";
  805. else if (tag == DW_TAG_subroutine_type) {
  806. /* Function pointer */
  807. return strbuf_add(buf, "(function_type)", 15);
  808. } else {
  809. if (!dwarf_diename(&type))
  810. return -ENOENT;
  811. if (tag == DW_TAG_union_type)
  812. tmp = "union ";
  813. else if (tag == DW_TAG_structure_type)
  814. tmp = "struct ";
  815. else if (tag == DW_TAG_enumeration_type)
  816. tmp = "enum ";
  817. /* Write a base name */
  818. return strbuf_addf(buf, "%s%s", tmp, dwarf_diename(&type));
  819. }
  820. ret = die_get_typename(&type, buf);
  821. return ret ? ret : strbuf_addstr(buf, tmp);
  822. }
  823. /**
  824. * die_get_varname - Get the name and type of given variable DIE
  825. * @vr_die: a variable DIE
  826. * @buf: a strbuf for type and variable name
  827. *
  828. * Get the name and type of @vr_die and stores it in @buf as "type\tname".
  829. */
  830. int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf)
  831. {
  832. int ret;
  833. ret = die_get_typename(vr_die, buf);
  834. if (ret < 0) {
  835. pr_debug("Failed to get type, make it unknown.\n");
  836. ret = strbuf_add(buf, " (unknown_type)", 14);
  837. }
  838. return ret < 0 ? ret : strbuf_addf(buf, "\t%s", dwarf_diename(vr_die));
  839. }
  840. #ifdef HAVE_DWARF_GETLOCATIONS
  841. /**
  842. * die_get_var_innermost_scope - Get innermost scope range of given variable DIE
  843. * @sp_die: a subprogram DIE
  844. * @vr_die: a variable DIE
  845. * @buf: a strbuf for variable byte offset range
  846. *
  847. * Get the innermost scope range of @vr_die and stores it in @buf as
  848. * "@<function_name+[NN-NN,NN-NN]>".
  849. */
  850. static int die_get_var_innermost_scope(Dwarf_Die *sp_die, Dwarf_Die *vr_die,
  851. struct strbuf *buf)
  852. {
  853. Dwarf_Die *scopes;
  854. int count;
  855. size_t offset = 0;
  856. Dwarf_Addr base;
  857. Dwarf_Addr start, end;
  858. Dwarf_Addr entry;
  859. int ret;
  860. bool first = true;
  861. const char *name;
  862. ret = dwarf_entrypc(sp_die, &entry);
  863. if (ret)
  864. return ret;
  865. name = dwarf_diename(sp_die);
  866. if (!name)
  867. return -ENOENT;
  868. count = dwarf_getscopes_die(vr_die, &scopes);
  869. /* (*SCOPES)[1] is the DIE for the scope containing that scope */
  870. if (count <= 1) {
  871. ret = -EINVAL;
  872. goto out;
  873. }
  874. while ((offset = dwarf_ranges(&scopes[1], offset, &base,
  875. &start, &end)) > 0) {
  876. start -= entry;
  877. end -= entry;
  878. if (first) {
  879. ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
  880. name, start, end);
  881. first = false;
  882. } else {
  883. ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
  884. start, end);
  885. }
  886. if (ret < 0)
  887. goto out;
  888. }
  889. if (!first)
  890. ret = strbuf_add(buf, "]>", 2);
  891. out:
  892. free(scopes);
  893. return ret;
  894. }
  895. /**
  896. * die_get_var_range - Get byte offset range of given variable DIE
  897. * @sp_die: a subprogram DIE
  898. * @vr_die: a variable DIE
  899. * @buf: a strbuf for type and variable name and byte offset range
  900. *
  901. * Get the byte offset range of @vr_die and stores it in @buf as
  902. * "@<function_name+[NN-NN,NN-NN]>".
  903. */
  904. int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf)
  905. {
  906. int ret = 0;
  907. Dwarf_Addr base;
  908. Dwarf_Addr start, end;
  909. Dwarf_Addr entry;
  910. Dwarf_Op *op;
  911. size_t nops;
  912. size_t offset = 0;
  913. Dwarf_Attribute attr;
  914. bool first = true;
  915. const char *name;
  916. ret = dwarf_entrypc(sp_die, &entry);
  917. if (ret)
  918. return ret;
  919. name = dwarf_diename(sp_die);
  920. if (!name)
  921. return -ENOENT;
  922. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
  923. return -EINVAL;
  924. while ((offset = dwarf_getlocations(&attr, offset, &base,
  925. &start, &end, &op, &nops)) > 0) {
  926. if (start == 0) {
  927. /* Single Location Descriptions */
  928. ret = die_get_var_innermost_scope(sp_die, vr_die, buf);
  929. goto out;
  930. }
  931. /* Location Lists */
  932. start -= entry;
  933. end -= entry;
  934. if (first) {
  935. ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
  936. name, start, end);
  937. first = false;
  938. } else {
  939. ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
  940. start, end);
  941. }
  942. if (ret < 0)
  943. goto out;
  944. }
  945. if (!first)
  946. ret = strbuf_add(buf, "]>", 2);
  947. out:
  948. return ret;
  949. }
  950. #else
  951. int die_get_var_range(Dwarf_Die *sp_die __maybe_unused,
  952. Dwarf_Die *vr_die __maybe_unused,
  953. struct strbuf *buf __maybe_unused)
  954. {
  955. return -ENOTSUP;
  956. }
  957. #endif