dwarf-aux.c 35 KB

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