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