dwarf-aux.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. strbuf_addf(buf, "(function_type)");
  808. return 0;
  809. } else {
  810. if (!dwarf_diename(&type))
  811. return -ENOENT;
  812. if (tag == DW_TAG_union_type)
  813. tmp = "union ";
  814. else if (tag == DW_TAG_structure_type)
  815. tmp = "struct ";
  816. else if (tag == DW_TAG_enumeration_type)
  817. tmp = "enum ";
  818. /* Write a base name */
  819. strbuf_addf(buf, "%s%s", tmp, dwarf_diename(&type));
  820. return 0;
  821. }
  822. ret = die_get_typename(&type, buf);
  823. if (ret == 0)
  824. strbuf_addf(buf, "%s", tmp);
  825. return ret;
  826. }
  827. /**
  828. * die_get_varname - Get the name and type of given variable DIE
  829. * @vr_die: a variable DIE
  830. * @buf: a strbuf for type and variable name
  831. *
  832. * Get the name and type of @vr_die and stores it in @buf as "type\tname".
  833. */
  834. int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf)
  835. {
  836. int ret;
  837. ret = die_get_typename(vr_die, buf);
  838. if (ret < 0) {
  839. pr_debug("Failed to get type, make it unknown.\n");
  840. strbuf_addf(buf, "(unknown_type)");
  841. }
  842. strbuf_addf(buf, "\t%s", dwarf_diename(vr_die));
  843. return 0;
  844. }
  845. /**
  846. * die_get_var_innermost_scope - Get innermost scope range of given variable DIE
  847. * @sp_die: a subprogram DIE
  848. * @vr_die: a variable DIE
  849. * @buf: a strbuf for variable byte offset range
  850. *
  851. * Get the innermost scope range of @vr_die and stores it in @buf as
  852. * "@<function_name+[NN-NN,NN-NN]>".
  853. */
  854. static int die_get_var_innermost_scope(Dwarf_Die *sp_die, Dwarf_Die *vr_die,
  855. struct strbuf *buf)
  856. {
  857. Dwarf_Die *scopes;
  858. int count;
  859. size_t offset = 0;
  860. Dwarf_Addr base;
  861. Dwarf_Addr start, end;
  862. Dwarf_Addr entry;
  863. int ret;
  864. bool first = true;
  865. const char *name;
  866. ret = dwarf_entrypc(sp_die, &entry);
  867. if (ret)
  868. return ret;
  869. name = dwarf_diename(sp_die);
  870. if (!name)
  871. return -ENOENT;
  872. count = dwarf_getscopes_die(vr_die, &scopes);
  873. /* (*SCOPES)[1] is the DIE for the scope containing that scope */
  874. if (count <= 1) {
  875. ret = -EINVAL;
  876. goto out;
  877. }
  878. while ((offset = dwarf_ranges(&scopes[1], offset, &base,
  879. &start, &end)) > 0) {
  880. start -= entry;
  881. end -= entry;
  882. if (first) {
  883. strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
  884. name, start, end);
  885. first = false;
  886. } else {
  887. strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
  888. start, end);
  889. }
  890. }
  891. if (!first)
  892. strbuf_addf(buf, "]>");
  893. out:
  894. free(scopes);
  895. return ret;
  896. }
  897. /**
  898. * die_get_var_range - Get byte offset range of given variable DIE
  899. * @sp_die: a subprogram DIE
  900. * @vr_die: a variable DIE
  901. * @buf: a strbuf for type and variable name and byte offset range
  902. *
  903. * Get the byte offset range of @vr_die and stores it in @buf as
  904. * "@<function_name+[NN-NN,NN-NN]>".
  905. */
  906. int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf)
  907. {
  908. int ret = 0;
  909. Dwarf_Addr base;
  910. Dwarf_Addr start, end;
  911. Dwarf_Addr entry;
  912. Dwarf_Op *op;
  913. size_t nops;
  914. size_t offset = 0;
  915. Dwarf_Attribute attr;
  916. bool first = true;
  917. const char *name;
  918. ret = dwarf_entrypc(sp_die, &entry);
  919. if (ret)
  920. return ret;
  921. name = dwarf_diename(sp_die);
  922. if (!name)
  923. return -ENOENT;
  924. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
  925. return -EINVAL;
  926. while ((offset = dwarf_getlocations(
  927. &attr, offset, &base,
  928. &start, &end, &op, &nops)) > 0) {
  929. if (start == 0) {
  930. /* Single Location Descriptions */
  931. ret = die_get_var_innermost_scope(sp_die, vr_die, buf);
  932. return ret;
  933. }
  934. /* Location Lists */
  935. start -= entry;
  936. end -= entry;
  937. if (first) {
  938. strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
  939. name, start, end);
  940. first = false;
  941. } else {
  942. strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
  943. start, end);
  944. }
  945. }
  946. if (!first)
  947. strbuf_addf(buf, "]>");
  948. return ret;
  949. }