blockframe.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* Get info from stack frames; convert between frames, blocks,
  2. functions and pc values.
  3. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "symtab.h"
  17. #include "bfd.h"
  18. #include "objfiles.h"
  19. #include "frame.h"
  20. #include "gdbcore.h"
  21. #include "value.h"
  22. #include "target.h"
  23. #include "inferior.h"
  24. #include "annotate.h"
  25. #include "regcache.h"
  26. #include "dummy-frame.h"
  27. #include "command.h"
  28. #include "gdbcmd.h"
  29. #include "block.h"
  30. #include "inline-frame.h"
  31. /* Return the innermost lexical block in execution in a specified
  32. stack frame. The frame address is assumed valid.
  33. If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
  34. address we used to choose the block. We use this to find a source
  35. line, to decide which macro definitions are in scope.
  36. The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
  37. PC, and may not really be a valid PC at all. For example, in the
  38. caller of a function declared to never return, the code at the
  39. return address will never be reached, so the call instruction may
  40. be the very last instruction in the block. So the address we use
  41. to choose the block is actually one byte before the return address
  42. --- hopefully pointing us at the call instruction, or its delay
  43. slot instruction. */
  44. const struct block *
  45. get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
  46. {
  47. CORE_ADDR pc;
  48. const struct block *bl;
  49. int inline_count;
  50. if (!get_frame_address_in_block_if_available (frame, &pc))
  51. return NULL;
  52. if (addr_in_block)
  53. *addr_in_block = pc;
  54. bl = block_for_pc (pc);
  55. if (bl == NULL)
  56. return NULL;
  57. inline_count = frame_inlined_callees (frame);
  58. while (inline_count > 0)
  59. {
  60. if (block_inlined_p (bl))
  61. inline_count--;
  62. bl = BLOCK_SUPERBLOCK (bl);
  63. gdb_assert (bl != NULL);
  64. }
  65. return bl;
  66. }
  67. CORE_ADDR
  68. get_pc_function_start (CORE_ADDR pc)
  69. {
  70. const struct block *bl;
  71. struct bound_minimal_symbol msymbol;
  72. bl = block_for_pc (pc);
  73. if (bl)
  74. {
  75. struct symbol *symbol = block_linkage_function (bl);
  76. if (symbol)
  77. {
  78. bl = SYMBOL_BLOCK_VALUE (symbol);
  79. return BLOCK_ENTRY_PC (bl);
  80. }
  81. }
  82. msymbol = lookup_minimal_symbol_by_pc (pc);
  83. if (msymbol.minsym)
  84. {
  85. CORE_ADDR fstart = BMSYMBOL_VALUE_ADDRESS (msymbol);
  86. if (find_pc_section (fstart))
  87. return fstart;
  88. }
  89. return 0;
  90. }
  91. /* Return the symbol for the function executing in frame FRAME. */
  92. struct symbol *
  93. get_frame_function (struct frame_info *frame)
  94. {
  95. const struct block *bl = get_frame_block (frame, 0);
  96. if (bl == NULL)
  97. return NULL;
  98. while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
  99. bl = BLOCK_SUPERBLOCK (bl);
  100. return BLOCK_FUNCTION (bl);
  101. }
  102. /* Return the function containing pc value PC in section SECTION.
  103. Returns 0 if function is not known. */
  104. struct symbol *
  105. find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
  106. {
  107. const struct block *b = block_for_pc_sect (pc, section);
  108. if (b == 0)
  109. return 0;
  110. return block_linkage_function (b);
  111. }
  112. /* Return the function containing pc value PC.
  113. Returns 0 if function is not known.
  114. Backward compatibility, no section */
  115. struct symbol *
  116. find_pc_function (CORE_ADDR pc)
  117. {
  118. return find_pc_sect_function (pc, find_pc_mapped_section (pc));
  119. }
  120. /* See symtab.h. */
  121. struct symbol *
  122. find_pc_sect_containing_function (CORE_ADDR pc, struct obj_section *section)
  123. {
  124. const block *bl = block_for_pc_sect (pc, section);
  125. if (bl == nullptr)
  126. return nullptr;
  127. return block_containing_function (bl);
  128. }
  129. /* These variables are used to cache the most recent result of
  130. find_pc_partial_function.
  131. The addresses cache_pc_function_low and cache_pc_function_high
  132. record the range in which PC was found during the most recent
  133. successful lookup. When the function occupies a single contiguous
  134. address range, these values correspond to the low and high
  135. addresses of the function. (The high address is actually one byte
  136. beyond the last byte of the function.) For a function with more
  137. than one (non-contiguous) range, the range in which PC was found is
  138. used to set the cache bounds.
  139. When determining whether or not these cached values apply to a
  140. particular PC value, PC must be within the range specified by
  141. cache_pc_function_low and cache_pc_function_high. In addition to
  142. PC being in that range, cache_pc_section must also match PC's
  143. section. See find_pc_partial_function() for details on both the
  144. comparison as well as how PC's section is determined.
  145. The other values aren't used for determining whether the cache
  146. applies, but are used for setting the outputs from
  147. find_pc_partial_function. cache_pc_function_low and
  148. cache_pc_function_high are used to set outputs as well. */
  149. static CORE_ADDR cache_pc_function_low = 0;
  150. static CORE_ADDR cache_pc_function_high = 0;
  151. static const general_symbol_info *cache_pc_function_sym = nullptr;
  152. static struct obj_section *cache_pc_function_section = NULL;
  153. static const struct block *cache_pc_function_block = nullptr;
  154. /* Clear cache, e.g. when symbol table is discarded. */
  155. void
  156. clear_pc_function_cache (void)
  157. {
  158. cache_pc_function_low = 0;
  159. cache_pc_function_high = 0;
  160. cache_pc_function_sym = nullptr;
  161. cache_pc_function_section = NULL;
  162. cache_pc_function_block = nullptr;
  163. }
  164. /* See symtab.h. */
  165. bool
  166. find_pc_partial_function_sym (CORE_ADDR pc,
  167. const struct general_symbol_info **sym,
  168. CORE_ADDR *address, CORE_ADDR *endaddr,
  169. const struct block **block)
  170. {
  171. struct obj_section *section;
  172. struct symbol *f;
  173. struct bound_minimal_symbol msymbol;
  174. struct compunit_symtab *compunit_symtab = NULL;
  175. CORE_ADDR mapped_pc;
  176. /* To ensure that the symbol returned belongs to the correct section
  177. (and that the last [random] symbol from the previous section
  178. isn't returned) try to find the section containing PC. First try
  179. the overlay code (which by default returns NULL); and second try
  180. the normal section code (which almost always succeeds). */
  181. section = find_pc_overlay (pc);
  182. if (section == NULL)
  183. section = find_pc_section (pc);
  184. mapped_pc = overlay_mapped_address (pc, section);
  185. if (mapped_pc >= cache_pc_function_low
  186. && mapped_pc < cache_pc_function_high
  187. && section == cache_pc_function_section)
  188. goto return_cached_value;
  189. msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
  190. compunit_symtab = find_pc_sect_compunit_symtab (mapped_pc, section);
  191. if (compunit_symtab != NULL)
  192. {
  193. /* Checking whether the msymbol has a larger value is for the
  194. "pathological" case mentioned in stack.c:find_frame_funname.
  195. We use BLOCK_ENTRY_PC instead of BLOCK_START_PC for this
  196. comparison because the minimal symbol should refer to the
  197. function's entry pc which is not necessarily the lowest
  198. address of the function. This will happen when the function
  199. has more than one range and the entry pc is not within the
  200. lowest range of addresses. */
  201. f = find_pc_sect_function (mapped_pc, section);
  202. if (f != NULL
  203. && (msymbol.minsym == NULL
  204. || (BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (f))
  205. >= BMSYMBOL_VALUE_ADDRESS (msymbol))))
  206. {
  207. const struct block *b = SYMBOL_BLOCK_VALUE (f);
  208. cache_pc_function_sym = f;
  209. cache_pc_function_section = section;
  210. cache_pc_function_block = b;
  211. /* For blocks occupying contiguous addresses (i.e. no gaps),
  212. the low and high cache addresses are simply the start
  213. and end of the block.
  214. For blocks with non-contiguous ranges, we have to search
  215. for the range containing mapped_pc and then use the start
  216. and end of that range.
  217. This causes the returned *ADDRESS and *ENDADDR values to
  218. be limited to the range in which mapped_pc is found. See
  219. comment preceding declaration of find_pc_partial_function
  220. in symtab.h for more information. */
  221. if (BLOCK_CONTIGUOUS_P (b))
  222. {
  223. cache_pc_function_low = BLOCK_START (b);
  224. cache_pc_function_high = BLOCK_END (b);
  225. }
  226. else
  227. {
  228. int i;
  229. for (i = 0; i < BLOCK_NRANGES (b); i++)
  230. {
  231. if (BLOCK_RANGE_START (b, i) <= mapped_pc
  232. && mapped_pc < BLOCK_RANGE_END (b, i))
  233. {
  234. cache_pc_function_low = BLOCK_RANGE_START (b, i);
  235. cache_pc_function_high = BLOCK_RANGE_END (b, i);
  236. break;
  237. }
  238. }
  239. /* Above loop should exit via the break. */
  240. gdb_assert (i < BLOCK_NRANGES (b));
  241. }
  242. goto return_cached_value;
  243. }
  244. }
  245. /* Not in the normal symbol tables, see if the pc is in a known
  246. section. If it's not, then give up. This ensures that anything
  247. beyond the end of the text seg doesn't appear to be part of the
  248. last function in the text segment. */
  249. if (!section)
  250. msymbol.minsym = NULL;
  251. /* Must be in the minimal symbol table. */
  252. if (msymbol.minsym == NULL)
  253. {
  254. /* No available symbol. */
  255. if (sym != nullptr)
  256. *sym = 0;
  257. if (address != NULL)
  258. *address = 0;
  259. if (endaddr != NULL)
  260. *endaddr = 0;
  261. if (block != nullptr)
  262. *block = nullptr;
  263. return false;
  264. }
  265. cache_pc_function_low = BMSYMBOL_VALUE_ADDRESS (msymbol);
  266. cache_pc_function_sym = msymbol.minsym;
  267. cache_pc_function_section = section;
  268. cache_pc_function_high = minimal_symbol_upper_bound (msymbol);
  269. cache_pc_function_block = nullptr;
  270. return_cached_value:
  271. if (address)
  272. {
  273. if (pc_in_unmapped_range (pc, section))
  274. *address = overlay_unmapped_address (cache_pc_function_low, section);
  275. else
  276. *address = cache_pc_function_low;
  277. }
  278. if (sym != nullptr)
  279. *sym = cache_pc_function_sym;
  280. if (endaddr)
  281. {
  282. if (pc_in_unmapped_range (pc, section))
  283. {
  284. /* Because the high address is actually beyond the end of
  285. the function (and therefore possibly beyond the end of
  286. the overlay), we must actually convert (high - 1) and
  287. then add one to that. */
  288. *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
  289. section);
  290. }
  291. else
  292. *endaddr = cache_pc_function_high;
  293. }
  294. if (block != nullptr)
  295. *block = cache_pc_function_block;
  296. return true;
  297. }
  298. /* See symtab.h. */
  299. bool
  300. find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
  301. CORE_ADDR *endaddr, const struct block **block)
  302. {
  303. const general_symbol_info *gsi;
  304. bool r = find_pc_partial_function_sym (pc, &gsi, address, endaddr, block);
  305. if (name != nullptr)
  306. *name = r ? gsi->linkage_name () : nullptr;
  307. return r;
  308. }
  309. /* See symtab.h. */
  310. bool
  311. find_function_entry_range_from_pc (CORE_ADDR pc, const char **name,
  312. CORE_ADDR *address, CORE_ADDR *endaddr)
  313. {
  314. const struct block *block;
  315. bool status = find_pc_partial_function (pc, name, address, endaddr, &block);
  316. if (status && block != nullptr && !BLOCK_CONTIGUOUS_P (block))
  317. {
  318. CORE_ADDR entry_pc = BLOCK_ENTRY_PC (block);
  319. for (int i = 0; i < BLOCK_NRANGES (block); i++)
  320. {
  321. if (BLOCK_RANGE_START (block, i) <= entry_pc
  322. && entry_pc < BLOCK_RANGE_END (block, i))
  323. {
  324. if (address != nullptr)
  325. *address = BLOCK_RANGE_START (block, i);
  326. if (endaddr != nullptr)
  327. *endaddr = BLOCK_RANGE_END (block, i);
  328. return status;
  329. }
  330. }
  331. /* It's an internal error if we exit the above loop without finding
  332. the range. */
  333. internal_error (__FILE__, __LINE__,
  334. _("Entry block not found in find_function_entry_range_from_pc"));
  335. }
  336. return status;
  337. }
  338. /* See symtab.h. */
  339. struct type *
  340. find_function_type (CORE_ADDR pc)
  341. {
  342. struct symbol *sym = find_pc_function (pc);
  343. if (sym != NULL && BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym)) == pc)
  344. return sym->type ();
  345. return NULL;
  346. }
  347. /* See symtab.h. */
  348. struct type *
  349. find_gnu_ifunc_target_type (CORE_ADDR resolver_funaddr)
  350. {
  351. struct type *resolver_type = find_function_type (resolver_funaddr);
  352. if (resolver_type != NULL)
  353. {
  354. /* Get the return type of the resolver. */
  355. struct type *resolver_ret_type
  356. = check_typedef (TYPE_TARGET_TYPE (resolver_type));
  357. /* If we found a pointer to function, then the resolved type
  358. is the type of the pointed-to function. */
  359. if (resolver_ret_type->code () == TYPE_CODE_PTR)
  360. {
  361. struct type *resolved_type
  362. = TYPE_TARGET_TYPE (resolver_ret_type);
  363. if (check_typedef (resolved_type)->code () == TYPE_CODE_FUNC)
  364. return resolved_type;
  365. }
  366. }
  367. return NULL;
  368. }
  369. /* Return the innermost stack frame that is executing inside of BLOCK and is
  370. at least as old as the selected frame. Return NULL if there is no
  371. such frame. If BLOCK is NULL, just return NULL. */
  372. struct frame_info *
  373. block_innermost_frame (const struct block *block)
  374. {
  375. if (block == NULL)
  376. return NULL;
  377. frame_info *frame = get_selected_frame ();
  378. while (frame != NULL)
  379. {
  380. const struct block *frame_block = get_frame_block (frame, NULL);
  381. if (frame_block != NULL && contained_in (frame_block, block))
  382. return frame;
  383. frame = get_prev_frame (frame);
  384. }
  385. return NULL;
  386. }