dwarf-aux.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * dwarf-aux.c : libdw auxiliary interfaces
  4. */
  5. #include <errno.h>
  6. #include <inttypes.h>
  7. #include <stdbool.h>
  8. #include <stdlib.h>
  9. #include "debug.h"
  10. #include "dwarf-aux.h"
  11. #include "strbuf.h"
  12. #include "string2.h"
  13. /**
  14. * cu_find_realpath - Find the realpath of the target file
  15. * @cu_die: A DIE(dwarf information entry) of CU(compilation Unit)
  16. * @fname: The tail filename of the target file
  17. *
  18. * Find the real(long) path of @fname in @cu_die.
  19. */
  20. const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
  21. {
  22. Dwarf_Files *files;
  23. size_t nfiles, i;
  24. const char *src = NULL;
  25. int ret;
  26. if (!fname)
  27. return NULL;
  28. ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
  29. if (ret != 0)
  30. return NULL;
  31. for (i = 0; i < nfiles; i++) {
  32. src = dwarf_filesrc(files, i, NULL, NULL);
  33. if (strtailcmp(src, fname) == 0)
  34. break;
  35. }
  36. if (i == nfiles)
  37. return NULL;
  38. return src;
  39. }
  40. /**
  41. * cu_get_comp_dir - Get the path of compilation directory
  42. * @cu_die: a CU DIE
  43. *
  44. * Get the path of compilation directory of given @cu_die.
  45. * Since this depends on DW_AT_comp_dir, older gcc will not
  46. * embedded it. In that case, this returns NULL.
  47. */
  48. const char *cu_get_comp_dir(Dwarf_Die *cu_die)
  49. {
  50. Dwarf_Attribute attr;
  51. if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
  52. return NULL;
  53. return dwarf_formstring(&attr);
  54. }
  55. /* Unlike dwarf_getsrc_die(), cu_getsrc_die() only returns statement line */
  56. static Dwarf_Line *cu_getsrc_die(Dwarf_Die *cu_die, Dwarf_Addr addr)
  57. {
  58. Dwarf_Addr laddr;
  59. Dwarf_Lines *lines;
  60. Dwarf_Line *line;
  61. size_t nlines, l, u, n;
  62. bool flag;
  63. if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0 ||
  64. nlines == 0)
  65. return NULL;
  66. /* Lines are sorted by address, use binary search */
  67. l = 0; u = nlines - 1;
  68. while (l < u) {
  69. n = u - (u - l) / 2;
  70. line = dwarf_onesrcline(lines, n);
  71. if (!line || dwarf_lineaddr(line, &laddr) != 0)
  72. return NULL;
  73. if (addr < laddr)
  74. u = n - 1;
  75. else
  76. l = n;
  77. }
  78. /* Going backward to find the lowest line */
  79. do {
  80. line = dwarf_onesrcline(lines, --l);
  81. if (!line || dwarf_lineaddr(line, &laddr) != 0)
  82. return NULL;
  83. } while (laddr == addr);
  84. l++;
  85. /* Going foward to find the statement line */
  86. do {
  87. line = dwarf_onesrcline(lines, l++);
  88. if (!line || dwarf_lineaddr(line, &laddr) != 0 ||
  89. dwarf_linebeginstatement(line, &flag) != 0)
  90. return NULL;
  91. if (laddr > addr)
  92. return NULL;
  93. } while (!flag);
  94. return line;
  95. }
  96. /**
  97. * cu_find_lineinfo - Get a line number and file name for given address
  98. * @cu_die: a CU DIE
  99. * @addr: An address
  100. * @fname: a pointer which returns the file name string
  101. * @lineno: a pointer which returns the line number
  102. *
  103. * Find a line number and file name for @addr in @cu_die.
  104. */
  105. int cu_find_lineinfo(Dwarf_Die *cu_die, unsigned long addr,
  106. const char **fname, int *lineno)
  107. {
  108. Dwarf_Line *line;
  109. Dwarf_Die die_mem;
  110. Dwarf_Addr faddr;
  111. if (die_find_realfunc(cu_die, (Dwarf_Addr)addr, &die_mem)
  112. && die_entrypc(&die_mem, &faddr) == 0 &&
  113. faddr == addr) {
  114. *fname = dwarf_decl_file(&die_mem);
  115. dwarf_decl_line(&die_mem, lineno);
  116. goto out;
  117. }
  118. line = cu_getsrc_die(cu_die, (Dwarf_Addr)addr);
  119. if (line && dwarf_lineno(line, lineno) == 0) {
  120. *fname = dwarf_linesrc(line, NULL, NULL);
  121. if (!*fname)
  122. /* line number is useless without filename */
  123. *lineno = 0;
  124. }
  125. out:
  126. return *lineno ?: -ENOENT;
  127. }
  128. static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data);
  129. /**
  130. * cu_walk_functions_at - Walk on function DIEs at given address
  131. * @cu_die: A CU DIE
  132. * @addr: An address
  133. * @callback: A callback which called with found DIEs
  134. * @data: A user data
  135. *
  136. * Walk on function DIEs at given @addr in @cu_die. Passed DIEs
  137. * should be subprogram or inlined-subroutines.
  138. */
  139. int cu_walk_functions_at(Dwarf_Die *cu_die, Dwarf_Addr addr,
  140. int (*callback)(Dwarf_Die *, void *), void *data)
  141. {
  142. Dwarf_Die die_mem;
  143. Dwarf_Die *sc_die;
  144. int ret = -ENOENT;
  145. /* Inlined function could be recursive. Trace it until fail */
  146. for (sc_die = die_find_realfunc(cu_die, addr, &die_mem);
  147. sc_die != NULL;
  148. sc_die = die_find_child(sc_die, __die_find_inline_cb, &addr,
  149. &die_mem)) {
  150. ret = callback(sc_die, data);
  151. if (ret)
  152. break;
  153. }
  154. return ret;
  155. }
  156. /**
  157. * die_get_linkage_name - Get the linkage name of the object
  158. * @dw_die: A DIE of the object
  159. *
  160. * Get the linkage name attiribute of given @dw_die.
  161. * For C++ binary, the linkage name will be the mangled symbol.
  162. */
  163. const char *die_get_linkage_name(Dwarf_Die *dw_die)
  164. {
  165. Dwarf_Attribute attr;
  166. if (dwarf_attr_integrate(dw_die, DW_AT_linkage_name, &attr) == NULL)
  167. return NULL;
  168. return dwarf_formstring(&attr);
  169. }
  170. /**
  171. * die_compare_name - Compare diename and tname
  172. * @dw_die: a DIE
  173. * @tname: a string of target name
  174. *
  175. * Compare the name of @dw_die and @tname. Return false if @dw_die has no name.
  176. */
  177. bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
  178. {
  179. const char *name;
  180. name = dwarf_diename(dw_die);
  181. return name ? (strcmp(tname, name) == 0) : false;
  182. }
  183. /**
  184. * die_match_name - Match diename/linkage name and glob
  185. * @dw_die: a DIE
  186. * @glob: a string of target glob pattern
  187. *
  188. * Glob matching the name of @dw_die and @glob. Return false if matching fail.
  189. * This also match linkage name.
  190. */
  191. bool die_match_name(Dwarf_Die *dw_die, const char *glob)
  192. {
  193. const char *name;
  194. name = dwarf_diename(dw_die);
  195. if (name && strglobmatch(name, glob))
  196. return true;
  197. /* fall back to check linkage name */
  198. name = die_get_linkage_name(dw_die);
  199. if (name && strglobmatch(name, glob))
  200. return true;
  201. return false;
  202. }
  203. /**
  204. * die_get_call_lineno - Get callsite line number of inline-function instance
  205. * @in_die: a DIE of an inlined function instance
  206. *
  207. * Get call-site line number of @in_die. This means from where the inline
  208. * function is called.
  209. */
  210. int die_get_call_lineno(Dwarf_Die *in_die)
  211. {
  212. Dwarf_Attribute attr;
  213. Dwarf_Word ret;
  214. if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
  215. return -ENOENT;
  216. dwarf_formudata(&attr, &ret);
  217. return (int)ret;
  218. }
  219. /**
  220. * die_get_type - Get type DIE
  221. * @vr_die: a DIE of a variable
  222. * @die_mem: where to store a type DIE
  223. *
  224. * Get a DIE of the type of given variable (@vr_die), and store
  225. * it to die_mem. Return NULL if fails to get a type DIE.
  226. */
  227. Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  228. {
  229. Dwarf_Attribute attr;
  230. if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
  231. dwarf_formref_die(&attr, die_mem))
  232. return die_mem;
  233. else
  234. return NULL;
  235. }
  236. /* Get a type die, but skip qualifiers */
  237. static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  238. {
  239. int tag;
  240. do {
  241. vr_die = die_get_type(vr_die, die_mem);
  242. if (!vr_die)
  243. break;
  244. tag = dwarf_tag(vr_die);
  245. } while (tag == DW_TAG_const_type ||
  246. tag == DW_TAG_restrict_type ||
  247. tag == DW_TAG_volatile_type ||
  248. tag == DW_TAG_shared_type);
  249. return vr_die;
  250. }
  251. /**
  252. * die_get_real_type - Get a type die, but skip qualifiers and typedef
  253. * @vr_die: a DIE of a variable
  254. * @die_mem: where to store a type DIE
  255. *
  256. * Get a DIE of the type of given variable (@vr_die), and store
  257. * it to die_mem. Return NULL if fails to get a type DIE.
  258. * If the type is qualifiers (e.g. const) or typedef, this skips it
  259. * and tries to find real type (structure or basic types, e.g. int).
  260. */
  261. Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  262. {
  263. do {
  264. vr_die = __die_get_real_type(vr_die, die_mem);
  265. } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
  266. return vr_die;
  267. }
  268. /* Get attribute and translate it as a udata */
  269. static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
  270. Dwarf_Word *result)
  271. {
  272. Dwarf_Attribute attr;
  273. if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
  274. dwarf_formudata(&attr, result) != 0)
  275. return -ENOENT;
  276. return 0;
  277. }
  278. /* Get attribute and translate it as a sdata */
  279. static int die_get_attr_sdata(Dwarf_Die *tp_die, unsigned int attr_name,
  280. Dwarf_Sword *result)
  281. {
  282. Dwarf_Attribute attr;
  283. if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
  284. dwarf_formsdata(&attr, result) != 0)
  285. return -ENOENT;
  286. return 0;
  287. }
  288. /**
  289. * die_is_signed_type - Check whether a type DIE is signed or not
  290. * @tp_die: a DIE of a type
  291. *
  292. * Get the encoding of @tp_die and return true if the encoding
  293. * is signed.
  294. */
  295. bool die_is_signed_type(Dwarf_Die *tp_die)
  296. {
  297. Dwarf_Word ret;
  298. if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
  299. return false;
  300. return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
  301. ret == DW_ATE_signed_fixed);
  302. }
  303. /**
  304. * die_is_func_def - Ensure that this DIE is a subprogram and definition
  305. * @dw_die: a DIE
  306. *
  307. * Ensure that this DIE is a subprogram and NOT a declaration. This
  308. * returns true if @dw_die is a function definition.
  309. **/
  310. bool die_is_func_def(Dwarf_Die *dw_die)
  311. {
  312. Dwarf_Attribute attr;
  313. Dwarf_Addr addr = 0;
  314. if (dwarf_tag(dw_die) != DW_TAG_subprogram)
  315. return false;
  316. if (dwarf_attr(dw_die, DW_AT_declaration, &attr))
  317. return false;
  318. /*
  319. * DW_AT_declaration can be lost from function declaration
  320. * by gcc's bug #97060.
  321. * So we need to check this subprogram DIE has DW_AT_inline
  322. * or an entry address.
  323. */
  324. if (!dwarf_attr(dw_die, DW_AT_inline, &attr) &&
  325. die_entrypc(dw_die, &addr) < 0)
  326. return false;
  327. return true;
  328. }
  329. /**
  330. * die_entrypc - Returns entry PC (the lowest address) of a DIE
  331. * @dw_die: a DIE
  332. * @addr: where to store entry PC
  333. *
  334. * Since dwarf_entrypc() does not return entry PC if the DIE has only address
  335. * range, we have to use this to retrieve the lowest address from the address
  336. * range attribute.
  337. */
  338. int die_entrypc(Dwarf_Die *dw_die, Dwarf_Addr *addr)
  339. {
  340. Dwarf_Addr base, end;
  341. Dwarf_Attribute attr;
  342. if (!addr)
  343. return -EINVAL;
  344. if (dwarf_entrypc(dw_die, addr) == 0)
  345. return 0;
  346. /*
  347. * Since the dwarf_ranges() will return 0 if there is no
  348. * DW_AT_ranges attribute, we should check it first.
  349. */
  350. if (!dwarf_attr(dw_die, DW_AT_ranges, &attr))
  351. return -ENOENT;
  352. return dwarf_ranges(dw_die, 0, &base, addr, &end) < 0 ? -ENOENT : 0;
  353. }
  354. /**
  355. * die_is_func_instance - Ensure that this DIE is an instance of a subprogram
  356. * @dw_die: a DIE
  357. *
  358. * Ensure that this DIE is an instance (which has an entry address).
  359. * This returns true if @dw_die is a function instance. If not, the @dw_die
  360. * must be a prototype. You can use die_walk_instances() to find actual
  361. * instances.
  362. **/
  363. bool die_is_func_instance(Dwarf_Die *dw_die)
  364. {
  365. Dwarf_Addr tmp;
  366. Dwarf_Attribute attr_mem;
  367. int tag = dwarf_tag(dw_die);
  368. if (tag != DW_TAG_subprogram &&
  369. tag != DW_TAG_inlined_subroutine)
  370. return false;
  371. return dwarf_entrypc(dw_die, &tmp) == 0 ||
  372. dwarf_attr(dw_die, DW_AT_ranges, &attr_mem) != NULL;
  373. }
  374. /**
  375. * die_get_data_member_location - Get the data-member offset
  376. * @mb_die: a DIE of a member of a data structure
  377. * @offs: The offset of the member in the data structure
  378. *
  379. * Get the offset of @mb_die in the data structure including @mb_die, and
  380. * stores result offset to @offs. If any error occurs this returns errno.
  381. */
  382. int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
  383. {
  384. Dwarf_Attribute attr;
  385. Dwarf_Op *expr;
  386. size_t nexpr;
  387. int ret;
  388. if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
  389. return -ENOENT;
  390. if (dwarf_formudata(&attr, offs) != 0) {
  391. /* DW_AT_data_member_location should be DW_OP_plus_uconst */
  392. ret = dwarf_getlocation(&attr, &expr, &nexpr);
  393. if (ret < 0 || nexpr == 0)
  394. return -ENOENT;
  395. if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
  396. pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
  397. expr[0].atom, nexpr);
  398. return -ENOTSUP;
  399. }
  400. *offs = (Dwarf_Word)expr[0].number;
  401. }
  402. return 0;
  403. }
  404. /* Get the call file index number in CU DIE */
  405. static int die_get_call_fileno(Dwarf_Die *in_die)
  406. {
  407. Dwarf_Sword idx;
  408. if (die_get_attr_sdata(in_die, DW_AT_call_file, &idx) == 0)
  409. return (int)idx;
  410. else
  411. return -ENOENT;
  412. }
  413. /* Get the declared file index number in CU DIE */
  414. static int die_get_decl_fileno(Dwarf_Die *pdie)
  415. {
  416. Dwarf_Sword idx;
  417. if (die_get_attr_sdata(pdie, DW_AT_decl_file, &idx) == 0)
  418. return (int)idx;
  419. else
  420. return -ENOENT;
  421. }
  422. /**
  423. * die_get_call_file - Get callsite file name of inlined function instance
  424. * @in_die: a DIE of an inlined function instance
  425. *
  426. * Get call-site file name of @in_die. This means from which file the inline
  427. * function is called.
  428. */
  429. const char *die_get_call_file(Dwarf_Die *in_die)
  430. {
  431. Dwarf_Die cu_die;
  432. Dwarf_Files *files;
  433. int idx;
  434. idx = die_get_call_fileno(in_die);
  435. if (idx < 0 || !dwarf_diecu(in_die, &cu_die, NULL, NULL) ||
  436. dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
  437. return NULL;
  438. return dwarf_filesrc(files, idx, NULL, NULL);
  439. }
  440. /**
  441. * die_find_child - Generic DIE search function in DIE tree
  442. * @rt_die: a root DIE
  443. * @callback: a callback function
  444. * @data: a user data passed to the callback function
  445. * @die_mem: a buffer for result DIE
  446. *
  447. * Trace DIE tree from @rt_die and call @callback for each child DIE.
  448. * If @callback returns DIE_FIND_CB_END, this stores the DIE into
  449. * @die_mem and returns it. If @callback returns DIE_FIND_CB_CONTINUE,
  450. * this continues to trace the tree. Optionally, @callback can return
  451. * DIE_FIND_CB_CHILD and DIE_FIND_CB_SIBLING, those means trace only
  452. * the children and trace only the siblings respectively.
  453. * Returns NULL if @callback can't find any appropriate DIE.
  454. */
  455. Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
  456. int (*callback)(Dwarf_Die *, void *),
  457. void *data, Dwarf_Die *die_mem)
  458. {
  459. Dwarf_Die child_die;
  460. int ret;
  461. ret = dwarf_child(rt_die, die_mem);
  462. if (ret != 0)
  463. return NULL;
  464. do {
  465. ret = callback(die_mem, data);
  466. if (ret == DIE_FIND_CB_END)
  467. return die_mem;
  468. if ((ret & DIE_FIND_CB_CHILD) &&
  469. die_find_child(die_mem, callback, data, &child_die)) {
  470. memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
  471. return die_mem;
  472. }
  473. } while ((ret & DIE_FIND_CB_SIBLING) &&
  474. dwarf_siblingof(die_mem, die_mem) == 0);
  475. return NULL;
  476. }
  477. struct __addr_die_search_param {
  478. Dwarf_Addr addr;
  479. Dwarf_Die *die_mem;
  480. };
  481. static int __die_search_func_tail_cb(Dwarf_Die *fn_die, void *data)
  482. {
  483. struct __addr_die_search_param *ad = data;
  484. Dwarf_Addr addr = 0;
  485. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  486. !dwarf_highpc(fn_die, &addr) &&
  487. addr == ad->addr) {
  488. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  489. return DWARF_CB_ABORT;
  490. }
  491. return DWARF_CB_OK;
  492. }
  493. /**
  494. * die_find_tailfunc - Search for a non-inlined function with tail call at
  495. * given address
  496. * @cu_die: a CU DIE which including @addr
  497. * @addr: target address
  498. * @die_mem: a buffer for result DIE
  499. *
  500. * Search for a non-inlined function DIE with tail call at @addr. Stores the
  501. * DIE to @die_mem and returns it if found. Returns NULL if failed.
  502. */
  503. Dwarf_Die *die_find_tailfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
  504. Dwarf_Die *die_mem)
  505. {
  506. struct __addr_die_search_param ad;
  507. ad.addr = addr;
  508. ad.die_mem = die_mem;
  509. /* dwarf_getscopes can't find subprogram. */
  510. if (!dwarf_getfuncs(cu_die, __die_search_func_tail_cb, &ad, 0))
  511. return NULL;
  512. else
  513. return die_mem;
  514. }
  515. /* die_find callback for non-inlined function search */
  516. static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
  517. {
  518. struct __addr_die_search_param *ad = data;
  519. /*
  520. * Since a declaration entry doesn't has given pc, this always returns
  521. * function definition entry.
  522. */
  523. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  524. dwarf_haspc(fn_die, ad->addr)) {
  525. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  526. return DWARF_CB_ABORT;
  527. }
  528. return DWARF_CB_OK;
  529. }
  530. /**
  531. * die_find_realfunc - Search a non-inlined function at given address
  532. * @cu_die: a CU DIE which including @addr
  533. * @addr: target address
  534. * @die_mem: a buffer for result DIE
  535. *
  536. * Search a non-inlined function DIE which includes @addr. Stores the
  537. * DIE to @die_mem and returns it if found. Returns NULL if failed.
  538. */
  539. Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
  540. Dwarf_Die *die_mem)
  541. {
  542. struct __addr_die_search_param ad;
  543. ad.addr = addr;
  544. ad.die_mem = die_mem;
  545. /* dwarf_getscopes can't find subprogram. */
  546. if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
  547. return NULL;
  548. else
  549. return die_mem;
  550. }
  551. /* die_find callback for inline function search */
  552. static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
  553. {
  554. Dwarf_Addr *addr = data;
  555. if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
  556. dwarf_haspc(die_mem, *addr))
  557. return DIE_FIND_CB_END;
  558. return DIE_FIND_CB_CONTINUE;
  559. }
  560. /**
  561. * die_find_top_inlinefunc - Search the top inlined function at given address
  562. * @sp_die: a subprogram DIE which including @addr
  563. * @addr: target address
  564. * @die_mem: a buffer for result DIE
  565. *
  566. * Search an inlined function DIE which includes @addr. Stores the
  567. * DIE to @die_mem and returns it if found. Returns NULL if failed.
  568. * Even if several inlined functions are expanded recursively, this
  569. * doesn't trace it down, and returns the topmost one.
  570. */
  571. Dwarf_Die *die_find_top_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  572. Dwarf_Die *die_mem)
  573. {
  574. return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
  575. }
  576. /**
  577. * die_find_inlinefunc - Search an inlined function at given address
  578. * @sp_die: a subprogram DIE which including @addr
  579. * @addr: target address
  580. * @die_mem: a buffer for result DIE
  581. *
  582. * Search an inlined function DIE which includes @addr. Stores the
  583. * DIE to @die_mem and returns it if found. Returns NULL if failed.
  584. * If several inlined functions are expanded recursively, this trace
  585. * it down and returns deepest one.
  586. */
  587. Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  588. Dwarf_Die *die_mem)
  589. {
  590. Dwarf_Die tmp_die;
  591. sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
  592. if (!sp_die)
  593. return NULL;
  594. /* Inlined function could be recursive. Trace it until fail */
  595. while (sp_die) {
  596. memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
  597. sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
  598. &tmp_die);
  599. }
  600. return die_mem;
  601. }
  602. struct __instance_walk_param {
  603. void *addr;
  604. int (*callback)(Dwarf_Die *, void *);
  605. void *data;
  606. int retval;
  607. };
  608. static int __die_walk_instances_cb(Dwarf_Die *inst, void *data)
  609. {
  610. struct __instance_walk_param *iwp = data;
  611. Dwarf_Attribute attr_mem;
  612. Dwarf_Die origin_mem;
  613. Dwarf_Attribute *attr;
  614. Dwarf_Die *origin;
  615. int tmp;
  616. if (!die_is_func_instance(inst))
  617. return DIE_FIND_CB_CONTINUE;
  618. attr = dwarf_attr(inst, DW_AT_abstract_origin, &attr_mem);
  619. if (attr == NULL)
  620. return DIE_FIND_CB_CONTINUE;
  621. origin = dwarf_formref_die(attr, &origin_mem);
  622. if (origin == NULL || origin->addr != iwp->addr)
  623. return DIE_FIND_CB_CONTINUE;
  624. /* Ignore redundant instances */
  625. if (dwarf_tag(inst) == DW_TAG_inlined_subroutine) {
  626. dwarf_decl_line(origin, &tmp);
  627. if (die_get_call_lineno(inst) == tmp) {
  628. tmp = die_get_decl_fileno(origin);
  629. if (die_get_call_fileno(inst) == tmp)
  630. return DIE_FIND_CB_CONTINUE;
  631. }
  632. }
  633. iwp->retval = iwp->callback(inst, iwp->data);
  634. return (iwp->retval) ? DIE_FIND_CB_END : DIE_FIND_CB_CONTINUE;
  635. }
  636. /**
  637. * die_walk_instances - Walk on instances of given DIE
  638. * @or_die: an abstract original DIE
  639. * @callback: a callback function which is called with instance DIE
  640. * @data: user data
  641. *
  642. * Walk on the instances of give @in_die. @in_die must be an inlined function
  643. * declartion. This returns the return value of @callback if it returns
  644. * non-zero value, or -ENOENT if there is no instance.
  645. */
  646. int die_walk_instances(Dwarf_Die *or_die, int (*callback)(Dwarf_Die *, void *),
  647. void *data)
  648. {
  649. Dwarf_Die cu_die;
  650. Dwarf_Die die_mem;
  651. struct __instance_walk_param iwp = {
  652. .addr = or_die->addr,
  653. .callback = callback,
  654. .data = data,
  655. .retval = -ENOENT,
  656. };
  657. if (dwarf_diecu(or_die, &cu_die, NULL, NULL) == NULL)
  658. return -ENOENT;
  659. die_find_child(&cu_die, __die_walk_instances_cb, &iwp, &die_mem);
  660. return iwp.retval;
  661. }
  662. /* Line walker internal parameters */
  663. struct __line_walk_param {
  664. bool recursive;
  665. line_walk_callback_t callback;
  666. void *data;
  667. int retval;
  668. };
  669. static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
  670. {
  671. struct __line_walk_param *lw = data;
  672. Dwarf_Addr addr = 0;
  673. const char *fname;
  674. int lineno;
  675. if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
  676. fname = die_get_call_file(in_die);
  677. lineno = die_get_call_lineno(in_die);
  678. if (fname && lineno > 0 && die_entrypc(in_die, &addr) == 0) {
  679. lw->retval = lw->callback(fname, lineno, addr, lw->data);
  680. if (lw->retval != 0)
  681. return DIE_FIND_CB_END;
  682. }
  683. if (!lw->recursive)
  684. return DIE_FIND_CB_SIBLING;
  685. }
  686. if (addr) {
  687. fname = dwarf_decl_file(in_die);
  688. if (fname && dwarf_decl_line(in_die, &lineno) == 0) {
  689. lw->retval = lw->callback(fname, lineno, addr, lw->data);
  690. if (lw->retval != 0)
  691. return DIE_FIND_CB_END;
  692. }
  693. }
  694. /* Continue to search nested inlined function call-sites */
  695. return DIE_FIND_CB_CONTINUE;
  696. }
  697. /* Walk on lines of blocks included in given DIE */
  698. static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive,
  699. line_walk_callback_t callback, void *data)
  700. {
  701. struct __line_walk_param lw = {
  702. .recursive = recursive,
  703. .callback = callback,
  704. .data = data,
  705. .retval = 0,
  706. };
  707. Dwarf_Die die_mem;
  708. Dwarf_Addr addr;
  709. const char *fname;
  710. int lineno;
  711. /* Handle function declaration line */
  712. fname = dwarf_decl_file(sp_die);
  713. if (fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
  714. die_entrypc(sp_die, &addr) == 0) {
  715. lw.retval = callback(fname, lineno, addr, data);
  716. if (lw.retval != 0)
  717. goto done;
  718. }
  719. die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
  720. done:
  721. return lw.retval;
  722. }
  723. static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
  724. {
  725. struct __line_walk_param *lw = data;
  726. /*
  727. * Since inlined function can include another inlined function in
  728. * the same file, we need to walk in it recursively.
  729. */
  730. lw->retval = __die_walk_funclines(sp_die, true, lw->callback, lw->data);
  731. if (lw->retval != 0)
  732. return DWARF_CB_ABORT;
  733. return DWARF_CB_OK;
  734. }
  735. /**
  736. * die_walk_lines - Walk on lines inside given DIE
  737. * @rt_die: a root DIE (CU, subprogram or inlined_subroutine)
  738. * @callback: callback routine
  739. * @data: user data
  740. *
  741. * Walk on all lines inside given @rt_die and call @callback on each line.
  742. * If the @rt_die is a function, walk only on the lines inside the function,
  743. * otherwise @rt_die must be a CU DIE.
  744. * Note that this walks not only dwarf line list, but also function entries
  745. * and inline call-site.
  746. */
  747. int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data)
  748. {
  749. Dwarf_Lines *lines;
  750. Dwarf_Line *line;
  751. Dwarf_Addr addr;
  752. const char *fname, *decf = NULL, *inf = NULL;
  753. int lineno, ret = 0;
  754. int decl = 0, inl;
  755. Dwarf_Die die_mem, *cu_die;
  756. size_t nlines, i;
  757. bool flag;
  758. /* Get the CU die */
  759. if (dwarf_tag(rt_die) != DW_TAG_compile_unit) {
  760. cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL);
  761. dwarf_decl_line(rt_die, &decl);
  762. decf = dwarf_decl_file(rt_die);
  763. } else
  764. cu_die = rt_die;
  765. if (!cu_die) {
  766. pr_debug2("Failed to get CU from given DIE.\n");
  767. return -EINVAL;
  768. }
  769. /* Get lines list in the CU */
  770. if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
  771. pr_debug2("Failed to get source lines on this CU.\n");
  772. return -ENOENT;
  773. }
  774. pr_debug2("Get %zd lines from this CU\n", nlines);
  775. /* Walk on the lines on lines list */
  776. for (i = 0; i < nlines; i++) {
  777. line = dwarf_onesrcline(lines, i);
  778. if (line == NULL ||
  779. dwarf_lineno(line, &lineno) != 0 ||
  780. dwarf_lineaddr(line, &addr) != 0) {
  781. pr_debug2("Failed to get line info. "
  782. "Possible error in debuginfo.\n");
  783. continue;
  784. }
  785. /* Skip end-of-sequence */
  786. if (dwarf_lineendsequence(line, &flag) != 0 || flag)
  787. continue;
  788. /* Skip Non statement line-info */
  789. if (dwarf_linebeginstatement(line, &flag) != 0 || !flag)
  790. continue;
  791. /* Filter lines based on address */
  792. if (rt_die != cu_die) {
  793. /*
  794. * Address filtering
  795. * The line is included in given function, and
  796. * no inline block includes it.
  797. */
  798. if (!dwarf_haspc(rt_die, addr))
  799. continue;
  800. if (die_find_inlinefunc(rt_die, addr, &die_mem)) {
  801. /* Call-site check */
  802. inf = die_get_call_file(&die_mem);
  803. if ((inf && !strcmp(inf, decf)) &&
  804. die_get_call_lineno(&die_mem) == lineno)
  805. goto found;
  806. dwarf_decl_line(&die_mem, &inl);
  807. if (inl != decl ||
  808. decf != dwarf_decl_file(&die_mem))
  809. continue;
  810. }
  811. }
  812. found:
  813. /* Get source line */
  814. fname = dwarf_linesrc(line, NULL, NULL);
  815. ret = callback(fname, lineno, addr, data);
  816. if (ret != 0)
  817. return ret;
  818. }
  819. /*
  820. * Dwarf lines doesn't include function declarations and inlined
  821. * subroutines. We have to check functions list or given function.
  822. */
  823. if (rt_die != cu_die)
  824. /*
  825. * Don't need walk inlined functions recursively, because
  826. * inner inlined functions don't have the lines of the
  827. * specified function.
  828. */
  829. ret = __die_walk_funclines(rt_die, false, callback, data);
  830. else {
  831. struct __line_walk_param param = {
  832. .callback = callback,
  833. .data = data,
  834. .retval = 0,
  835. };
  836. dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
  837. ret = param.retval;
  838. }
  839. return ret;
  840. }
  841. struct __find_variable_param {
  842. const char *name;
  843. Dwarf_Addr addr;
  844. };
  845. static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
  846. {
  847. struct __find_variable_param *fvp = data;
  848. Dwarf_Attribute attr;
  849. int tag;
  850. tag = dwarf_tag(die_mem);
  851. if ((tag == DW_TAG_formal_parameter ||
  852. tag == DW_TAG_variable) &&
  853. die_compare_name(die_mem, fvp->name) &&
  854. /*
  855. * Does the DIE have location information or const value
  856. * or external instance?
  857. */
  858. (dwarf_attr(die_mem, DW_AT_external, &attr) ||
  859. dwarf_attr(die_mem, DW_AT_location, &attr) ||
  860. dwarf_attr(die_mem, DW_AT_const_value, &attr)))
  861. return DIE_FIND_CB_END;
  862. if (dwarf_haspc(die_mem, fvp->addr))
  863. return DIE_FIND_CB_CONTINUE;
  864. else
  865. return DIE_FIND_CB_SIBLING;
  866. }
  867. /**
  868. * die_find_variable_at - Find a given name variable at given address
  869. * @sp_die: a function DIE
  870. * @name: variable name
  871. * @addr: address
  872. * @die_mem: a buffer for result DIE
  873. *
  874. * Find a variable DIE called @name at @addr in @sp_die.
  875. */
  876. Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
  877. Dwarf_Addr addr, Dwarf_Die *die_mem)
  878. {
  879. struct __find_variable_param fvp = { .name = name, .addr = addr};
  880. return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
  881. die_mem);
  882. }
  883. static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
  884. {
  885. const char *name = data;
  886. if (dwarf_tag(die_mem) == DW_TAG_member) {
  887. if (die_compare_name(die_mem, name))
  888. return DIE_FIND_CB_END;
  889. else if (!dwarf_diename(die_mem)) { /* Unnamed structure */
  890. Dwarf_Die type_die, tmp_die;
  891. if (die_get_type(die_mem, &type_die) &&
  892. die_find_member(&type_die, name, &tmp_die))
  893. return DIE_FIND_CB_END;
  894. }
  895. }
  896. return DIE_FIND_CB_SIBLING;
  897. }
  898. /**
  899. * die_find_member - Find a given name member in a data structure
  900. * @st_die: a data structure type DIE
  901. * @name: member name
  902. * @die_mem: a buffer for result DIE
  903. *
  904. * Find a member DIE called @name in @st_die.
  905. */
  906. Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
  907. Dwarf_Die *die_mem)
  908. {
  909. return die_find_child(st_die, __die_find_member_cb, (void *)name,
  910. die_mem);
  911. }
  912. /**
  913. * die_get_typename - Get the name of given variable DIE
  914. * @vr_die: a variable DIE
  915. * @buf: a strbuf for result type name
  916. *
  917. * Get the name of @vr_die and stores it to @buf. Return 0 if succeeded.
  918. * and Return -ENOENT if failed to find type name.
  919. * Note that the result will stores typedef name if possible, and stores
  920. * "*(function_type)" if the type is a function pointer.
  921. */
  922. int die_get_typename(Dwarf_Die *vr_die, struct strbuf *buf)
  923. {
  924. Dwarf_Die type;
  925. int tag, ret;
  926. const char *tmp = "";
  927. if (__die_get_real_type(vr_die, &type) == NULL)
  928. return -ENOENT;
  929. tag = dwarf_tag(&type);
  930. if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
  931. tmp = "*";
  932. else if (tag == DW_TAG_subroutine_type) {
  933. /* Function pointer */
  934. return strbuf_add(buf, "(function_type)", 15);
  935. } else {
  936. if (!dwarf_diename(&type))
  937. return -ENOENT;
  938. if (tag == DW_TAG_union_type)
  939. tmp = "union ";
  940. else if (tag == DW_TAG_structure_type)
  941. tmp = "struct ";
  942. else if (tag == DW_TAG_enumeration_type)
  943. tmp = "enum ";
  944. /* Write a base name */
  945. return strbuf_addf(buf, "%s%s", tmp, dwarf_diename(&type));
  946. }
  947. ret = die_get_typename(&type, buf);
  948. return ret ? ret : strbuf_addstr(buf, tmp);
  949. }
  950. /**
  951. * die_get_varname - Get the name and type of given variable DIE
  952. * @vr_die: a variable DIE
  953. * @buf: a strbuf for type and variable name
  954. *
  955. * Get the name and type of @vr_die and stores it in @buf as "type\tname".
  956. */
  957. int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf)
  958. {
  959. int ret;
  960. ret = die_get_typename(vr_die, buf);
  961. if (ret < 0) {
  962. pr_debug("Failed to get type, make it unknown.\n");
  963. ret = strbuf_add(buf, " (unknown_type)", 14);
  964. }
  965. return ret < 0 ? ret : strbuf_addf(buf, "\t%s", dwarf_diename(vr_die));
  966. }
  967. #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT
  968. /**
  969. * die_get_var_innermost_scope - Get innermost scope range of given variable DIE
  970. * @sp_die: a subprogram DIE
  971. * @vr_die: a variable DIE
  972. * @buf: a strbuf for variable byte offset range
  973. *
  974. * Get the innermost scope range of @vr_die and stores it in @buf as
  975. * "@<function_name+[NN-NN,NN-NN]>".
  976. */
  977. static int die_get_var_innermost_scope(Dwarf_Die *sp_die, Dwarf_Die *vr_die,
  978. struct strbuf *buf)
  979. {
  980. Dwarf_Die *scopes;
  981. int count;
  982. size_t offset = 0;
  983. Dwarf_Addr base;
  984. Dwarf_Addr start, end;
  985. Dwarf_Addr entry;
  986. int ret;
  987. bool first = true;
  988. const char *name;
  989. ret = die_entrypc(sp_die, &entry);
  990. if (ret)
  991. return ret;
  992. name = dwarf_diename(sp_die);
  993. if (!name)
  994. return -ENOENT;
  995. count = dwarf_getscopes_die(vr_die, &scopes);
  996. /* (*SCOPES)[1] is the DIE for the scope containing that scope */
  997. if (count <= 1) {
  998. ret = -EINVAL;
  999. goto out;
  1000. }
  1001. while ((offset = dwarf_ranges(&scopes[1], offset, &base,
  1002. &start, &end)) > 0) {
  1003. start -= entry;
  1004. end -= entry;
  1005. if (first) {
  1006. ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
  1007. name, start, end);
  1008. first = false;
  1009. } else {
  1010. ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
  1011. start, end);
  1012. }
  1013. if (ret < 0)
  1014. goto out;
  1015. }
  1016. if (!first)
  1017. ret = strbuf_add(buf, "]>", 2);
  1018. out:
  1019. free(scopes);
  1020. return ret;
  1021. }
  1022. /**
  1023. * die_get_var_range - Get byte offset range of given variable DIE
  1024. * @sp_die: a subprogram DIE
  1025. * @vr_die: a variable DIE
  1026. * @buf: a strbuf for type and variable name and byte offset range
  1027. *
  1028. * Get the byte offset range of @vr_die and stores it in @buf as
  1029. * "@<function_name+[NN-NN,NN-NN]>".
  1030. */
  1031. int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf)
  1032. {
  1033. int ret = 0;
  1034. Dwarf_Addr base;
  1035. Dwarf_Addr start, end;
  1036. Dwarf_Addr entry;
  1037. Dwarf_Op *op;
  1038. size_t nops;
  1039. size_t offset = 0;
  1040. Dwarf_Attribute attr;
  1041. bool first = true;
  1042. const char *name;
  1043. ret = die_entrypc(sp_die, &entry);
  1044. if (ret)
  1045. return ret;
  1046. name = dwarf_diename(sp_die);
  1047. if (!name)
  1048. return -ENOENT;
  1049. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
  1050. return -EINVAL;
  1051. while ((offset = dwarf_getlocations(&attr, offset, &base,
  1052. &start, &end, &op, &nops)) > 0) {
  1053. if (start == 0) {
  1054. /* Single Location Descriptions */
  1055. ret = die_get_var_innermost_scope(sp_die, vr_die, buf);
  1056. goto out;
  1057. }
  1058. /* Location Lists */
  1059. start -= entry;
  1060. end -= entry;
  1061. if (first) {
  1062. ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
  1063. name, start, end);
  1064. first = false;
  1065. } else {
  1066. ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
  1067. start, end);
  1068. }
  1069. if (ret < 0)
  1070. goto out;
  1071. }
  1072. if (!first)
  1073. ret = strbuf_add(buf, "]>", 2);
  1074. out:
  1075. return ret;
  1076. }
  1077. #else
  1078. int die_get_var_range(Dwarf_Die *sp_die __maybe_unused,
  1079. Dwarf_Die *vr_die __maybe_unused,
  1080. struct strbuf *buf __maybe_unused)
  1081. {
  1082. return -ENOTSUP;
  1083. }
  1084. #endif
  1085. /*
  1086. * die_has_loclist - Check if DW_AT_location of @vr_die is a location list
  1087. * @vr_die: a variable DIE
  1088. */
  1089. static bool die_has_loclist(Dwarf_Die *vr_die)
  1090. {
  1091. Dwarf_Attribute loc;
  1092. int tag = dwarf_tag(vr_die);
  1093. if (tag != DW_TAG_formal_parameter &&
  1094. tag != DW_TAG_variable)
  1095. return false;
  1096. return (dwarf_attr_integrate(vr_die, DW_AT_location, &loc) &&
  1097. dwarf_whatform(&loc) == DW_FORM_sec_offset);
  1098. }
  1099. /*
  1100. * die_is_optimized_target - Check if target program is compiled with
  1101. * optimization
  1102. * @cu_die: a CU DIE
  1103. *
  1104. * For any object in given CU whose DW_AT_location is a location list,
  1105. * target program is compiled with optimization. This is applicable to
  1106. * clang as well.
  1107. */
  1108. bool die_is_optimized_target(Dwarf_Die *cu_die)
  1109. {
  1110. Dwarf_Die tmp_die;
  1111. if (die_has_loclist(cu_die))
  1112. return true;
  1113. if (!dwarf_child(cu_die, &tmp_die) &&
  1114. die_is_optimized_target(&tmp_die))
  1115. return true;
  1116. if (!dwarf_siblingof(cu_die, &tmp_die) &&
  1117. die_is_optimized_target(&tmp_die))
  1118. return true;
  1119. return false;
  1120. }
  1121. /*
  1122. * die_search_idx - Search index of given line address
  1123. * @lines: Line records of single CU
  1124. * @nr_lines: Number of @lines
  1125. * @addr: address we are looking for
  1126. * @idx: index to be set by this function (return value)
  1127. *
  1128. * Search for @addr by looping over every lines of CU. If address
  1129. * matches, set index of that line in @idx. Note that single source
  1130. * line can have multiple line records. i.e. single source line can
  1131. * have multiple index.
  1132. */
  1133. static bool die_search_idx(Dwarf_Lines *lines, unsigned long nr_lines,
  1134. Dwarf_Addr addr, unsigned long *idx)
  1135. {
  1136. unsigned long i;
  1137. Dwarf_Addr tmp;
  1138. for (i = 0; i < nr_lines; i++) {
  1139. if (dwarf_lineaddr(dwarf_onesrcline(lines, i), &tmp))
  1140. return false;
  1141. if (tmp == addr) {
  1142. *idx = i;
  1143. return true;
  1144. }
  1145. }
  1146. return false;
  1147. }
  1148. /*
  1149. * die_get_postprologue_addr - Search next address after function prologue
  1150. * @entrypc_idx: entrypc index
  1151. * @lines: Line records of single CU
  1152. * @nr_lines: Number of @lines
  1153. * @hignpc: high PC address of function
  1154. * @postprologue_addr: Next address after function prologue (return value)
  1155. *
  1156. * Look for prologue-end marker. If there is no explicit marker, return
  1157. * address of next line record or next source line.
  1158. */
  1159. static bool die_get_postprologue_addr(unsigned long entrypc_idx,
  1160. Dwarf_Lines *lines,
  1161. unsigned long nr_lines,
  1162. Dwarf_Addr highpc,
  1163. Dwarf_Addr *postprologue_addr)
  1164. {
  1165. unsigned long i;
  1166. int entrypc_lno, lno;
  1167. Dwarf_Line *line;
  1168. Dwarf_Addr addr;
  1169. bool p_end;
  1170. /* entrypc_lno is actual source line number */
  1171. line = dwarf_onesrcline(lines, entrypc_idx);
  1172. if (dwarf_lineno(line, &entrypc_lno))
  1173. return false;
  1174. for (i = entrypc_idx; i < nr_lines; i++) {
  1175. line = dwarf_onesrcline(lines, i);
  1176. if (dwarf_lineaddr(line, &addr) ||
  1177. dwarf_lineno(line, &lno) ||
  1178. dwarf_lineprologueend(line, &p_end))
  1179. return false;
  1180. /* highpc is exclusive. [entrypc,highpc) */
  1181. if (addr >= highpc)
  1182. break;
  1183. /* clang supports prologue-end marker */
  1184. if (p_end)
  1185. break;
  1186. /* Actual next line in source */
  1187. if (lno != entrypc_lno)
  1188. break;
  1189. /*
  1190. * Single source line can have multiple line records.
  1191. * For Example,
  1192. * void foo() { printf("hello\n"); }
  1193. * contains two line records. One points to declaration and
  1194. * other points to printf() line. Variable 'lno' won't get
  1195. * incremented in this case but 'i' will.
  1196. */
  1197. if (i != entrypc_idx)
  1198. break;
  1199. }
  1200. dwarf_lineaddr(line, postprologue_addr);
  1201. if (*postprologue_addr >= highpc)
  1202. dwarf_lineaddr(dwarf_onesrcline(lines, i - 1),
  1203. postprologue_addr);
  1204. return true;
  1205. }
  1206. /*
  1207. * die_skip_prologue - Use next address after prologue as probe location
  1208. * @sp_die: a subprogram DIE
  1209. * @cu_die: a CU DIE
  1210. * @entrypc: entrypc of the function
  1211. *
  1212. * Function prologue prepares stack and registers before executing function
  1213. * logic. When target program is compiled without optimization, function
  1214. * parameter information is only valid after prologue. When we probe entrypc
  1215. * of the function, and try to record function parameter, it contains
  1216. * garbage value.
  1217. */
  1218. void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die,
  1219. Dwarf_Addr *entrypc)
  1220. {
  1221. size_t nr_lines = 0;
  1222. unsigned long entrypc_idx = 0;
  1223. Dwarf_Lines *lines = NULL;
  1224. Dwarf_Addr postprologue_addr;
  1225. Dwarf_Addr highpc;
  1226. if (dwarf_highpc(sp_die, &highpc))
  1227. return;
  1228. if (dwarf_getsrclines(cu_die, &lines, &nr_lines))
  1229. return;
  1230. if (!die_search_idx(lines, nr_lines, *entrypc, &entrypc_idx))
  1231. return;
  1232. if (!die_get_postprologue_addr(entrypc_idx, lines, nr_lines,
  1233. highpc, &postprologue_addr))
  1234. return;
  1235. *entrypc = postprologue_addr;
  1236. }