1 /* Get info from stack frames; convert between frames, blocks,
2 functions and pc values.
4 Copyright (C) 1986-2024 Free Software Foundation, Inc.
6 This file is part of GDB.
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include "dummy-frame.h"
33 #include "cli/cli-cmds.h"
35 #include "inline-frame.h"
37 /* Return the innermost lexical block in execution in a specified
38 stack frame. The frame address is assumed valid.
40 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
41 address we used to choose the block. We use this to find a source
42 line, to decide which macro definitions are in scope.
44 The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
45 PC, and may not really be a valid PC at all. For example, in the
46 caller of a function declared to never return, the code at the
47 return address will never be reached, so the call instruction may
48 be the very last instruction in the block. So the address we use
49 to choose the block is actually one byte before the return address
50 --- hopefully pointing us at the call instruction, or its delay
54 get_frame_block (const frame_info_ptr
&frame
, CORE_ADDR
*addr_in_block
)
57 const struct block
*bl
;
60 if (!get_frame_address_in_block_if_available (frame
, &pc
))
66 bl
= block_for_pc (pc
);
70 inline_count
= frame_inlined_callees (frame
);
72 while (inline_count
> 0)
77 bl
= bl
->superblock ();
78 gdb_assert (bl
!= NULL
);
85 get_pc_function_start (CORE_ADDR pc
)
87 const struct block
*bl
;
89 bl
= block_for_pc (pc
);
92 struct symbol
*symbol
= bl
->linkage_function ();
96 bl
= symbol
->value_block ();
97 return bl
->entry_pc ();
101 bound_minimal_symbol msymbol
= lookup_minimal_symbol_by_pc (pc
);
104 CORE_ADDR fstart
= msymbol
.value_address ();
106 if (find_pc_section (fstart
))
113 /* Return the symbol for the function executing in frame FRAME. */
116 get_frame_function (const frame_info_ptr
&frame
)
118 const struct block
*bl
= get_frame_block (frame
, 0);
123 while (bl
->function () == NULL
&& bl
->superblock () != NULL
)
124 bl
= bl
->superblock ();
126 return bl
->function ();
130 /* Return the function containing pc value PC in section SECTION.
131 Returns 0 if function is not known. */
134 find_pc_sect_function (CORE_ADDR pc
, struct obj_section
*section
)
136 const struct block
*b
= block_for_pc_sect (pc
, section
);
140 return b
->linkage_function ();
143 /* Return the function containing pc value PC.
144 Returns 0 if function is not known.
145 Backward compatibility, no section */
148 find_pc_function (CORE_ADDR pc
)
150 return find_pc_sect_function (pc
, find_pc_mapped_section (pc
));
156 find_pc_sect_containing_function (CORE_ADDR pc
, struct obj_section
*section
)
158 const block
*bl
= block_for_pc_sect (pc
, section
);
163 return bl
->containing_function ();
166 /* These variables are used to cache the most recent result of
167 find_pc_partial_function.
169 The addresses cache_pc_function_low and cache_pc_function_high
170 record the range in which PC was found during the most recent
171 successful lookup. When the function occupies a single contiguous
172 address range, these values correspond to the low and high
173 addresses of the function. (The high address is actually one byte
174 beyond the last byte of the function.) For a function with more
175 than one (non-contiguous) range, the range in which PC was found is
176 used to set the cache bounds.
178 When determining whether or not these cached values apply to a
179 particular PC value, PC must be within the range specified by
180 cache_pc_function_low and cache_pc_function_high. In addition to
181 PC being in that range, cache_pc_section must also match PC's
182 section. See find_pc_partial_function() for details on both the
183 comparison as well as how PC's section is determined.
185 The other values aren't used for determining whether the cache
186 applies, but are used for setting the outputs from
187 find_pc_partial_function. cache_pc_function_low and
188 cache_pc_function_high are used to set outputs as well. */
190 static CORE_ADDR cache_pc_function_low
= 0;
191 static CORE_ADDR cache_pc_function_high
= 0;
192 static const general_symbol_info
*cache_pc_function_sym
= nullptr;
193 static struct obj_section
*cache_pc_function_section
= NULL
;
194 static const struct block
*cache_pc_function_block
= nullptr;
196 /* Clear cache, e.g. when symbol table is discarded. */
199 clear_pc_function_cache (void)
201 cache_pc_function_low
= 0;
202 cache_pc_function_high
= 0;
203 cache_pc_function_sym
= nullptr;
204 cache_pc_function_section
= NULL
;
205 cache_pc_function_block
= nullptr;
211 find_pc_partial_function_sym (CORE_ADDR pc
,
212 const struct general_symbol_info
**sym
,
213 CORE_ADDR
*address
, CORE_ADDR
*endaddr
,
214 const struct block
**block
)
216 struct obj_section
*section
;
218 struct compunit_symtab
*compunit_symtab
= NULL
;
220 bound_minimal_symbol msymbol
;
222 /* To ensure that the symbol returned belongs to the correct section
223 (and that the last [random] symbol from the previous section
224 isn't returned) try to find the section containing PC. First try
225 the overlay code (which by default returns NULL); and second try
226 the normal section code (which almost always succeeds). */
227 section
= find_pc_overlay (pc
);
229 section
= find_pc_section (pc
);
231 mapped_pc
= overlay_mapped_address (pc
, section
);
233 if (mapped_pc
>= cache_pc_function_low
234 && mapped_pc
< cache_pc_function_high
235 && section
== cache_pc_function_section
)
236 goto return_cached_value
;
238 msymbol
= lookup_minimal_symbol_by_pc_section (mapped_pc
, section
);
239 compunit_symtab
= find_pc_sect_compunit_symtab (mapped_pc
, section
);
241 if (compunit_symtab
!= NULL
)
243 /* Checking whether the msymbol has a larger value is for the
244 "pathological" case mentioned in stack.c:find_frame_funname.
246 We use BLOCK_ENTRY_PC instead of BLOCK_START_PC for this
247 comparison because the minimal symbol should refer to the
248 function's entry pc which is not necessarily the lowest
249 address of the function. This will happen when the function
250 has more than one range and the entry pc is not within the
251 lowest range of addresses. */
252 f
= find_pc_sect_function (mapped_pc
, section
);
254 && (msymbol
.minsym
== NULL
255 || (f
->value_block ()->entry_pc ()
256 >= msymbol
.value_address ())))
258 const struct block
*b
= f
->value_block ();
260 cache_pc_function_sym
= f
;
261 cache_pc_function_section
= section
;
262 cache_pc_function_block
= b
;
264 /* For blocks occupying contiguous addresses (i.e. no gaps),
265 the low and high cache addresses are simply the start
266 and end of the block.
268 For blocks with non-contiguous ranges, we have to search
269 for the range containing mapped_pc and then use the start
270 and end of that range.
272 This causes the returned *ADDRESS and *ENDADDR values to
273 be limited to the range in which mapped_pc is found. See
274 comment preceding declaration of find_pc_partial_function
275 in symtab.h for more information. */
277 if (b
->is_contiguous ())
279 cache_pc_function_low
= b
->start ();
280 cache_pc_function_high
= b
->end ();
285 for (const blockrange
&range
: b
->ranges ())
287 if (range
.start () <= mapped_pc
&& mapped_pc
< range
.end ())
289 cache_pc_function_low
= range
.start ();
290 cache_pc_function_high
= range
.end ();
295 /* Above loop should exit via the break. */
300 goto return_cached_value
;
304 /* Not in the normal symbol tables, see if the pc is in a known
305 section. If it's not, then give up. This ensures that anything
306 beyond the end of the text seg doesn't appear to be part of the
307 last function in the text segment. */
310 msymbol
.minsym
= NULL
;
312 /* Must be in the minimal symbol table. */
313 if (msymbol
.minsym
== NULL
)
315 /* No available symbol. */
322 if (block
!= nullptr)
327 cache_pc_function_low
= msymbol
.value_address ();
328 cache_pc_function_sym
= msymbol
.minsym
;
329 cache_pc_function_section
= section
;
330 cache_pc_function_high
= minimal_symbol_upper_bound (msymbol
);
331 cache_pc_function_block
= nullptr;
337 if (pc_in_unmapped_range (pc
, section
))
338 *address
= overlay_unmapped_address (cache_pc_function_low
, section
);
340 *address
= cache_pc_function_low
;
344 *sym
= cache_pc_function_sym
;
348 if (pc_in_unmapped_range (pc
, section
))
350 /* Because the high address is actually beyond the end of
351 the function (and therefore possibly beyond the end of
352 the overlay), we must actually convert (high - 1) and
353 then add one to that. */
355 *endaddr
= 1 + overlay_unmapped_address (cache_pc_function_high
- 1,
359 *endaddr
= cache_pc_function_high
;
362 if (block
!= nullptr)
363 *block
= cache_pc_function_block
;
371 find_pc_partial_function (CORE_ADDR pc
, const char **name
, CORE_ADDR
*address
,
372 CORE_ADDR
*endaddr
, const struct block
**block
)
374 const general_symbol_info
*gsi
;
375 bool r
= find_pc_partial_function_sym (pc
, &gsi
, address
, endaddr
, block
);
377 *name
= r
? gsi
->linkage_name () : nullptr;
385 find_function_entry_range_from_pc (CORE_ADDR pc
, const char **name
,
386 CORE_ADDR
*address
, CORE_ADDR
*endaddr
)
388 const struct block
*block
;
389 bool status
= find_pc_partial_function (pc
, name
, address
, endaddr
, &block
);
391 if (status
&& block
!= nullptr && !block
->is_contiguous ())
393 CORE_ADDR entry_pc
= block
->entry_pc ();
395 for (const blockrange
&range
: block
->ranges ())
397 if (range
.start () <= entry_pc
&& entry_pc
< range
.end ())
399 if (address
!= nullptr)
400 *address
= range
.start ();
402 if (endaddr
!= nullptr)
403 *endaddr
= range
.end ();
409 /* It's an internal error if we exit the above loop without finding
411 internal_error (_("Entry block not found in find_function_entry_range_from_pc"));
420 find_function_type (CORE_ADDR pc
)
422 struct symbol
*sym
= find_pc_function (pc
);
424 if (sym
!= NULL
&& sym
->value_block ()->entry_pc () == pc
)
433 find_gnu_ifunc_target_type (CORE_ADDR resolver_funaddr
)
435 struct type
*resolver_type
= find_function_type (resolver_funaddr
);
436 if (resolver_type
!= NULL
)
438 /* Get the return type of the resolver. */
439 struct type
*resolver_ret_type
440 = check_typedef (resolver_type
->target_type ());
442 /* If we found a pointer to function, then the resolved type
443 is the type of the pointed-to function. */
444 if (resolver_ret_type
->code () == TYPE_CODE_PTR
)
446 struct type
*resolved_type
447 = resolver_ret_type
->target_type ();
448 if (check_typedef (resolved_type
)->code () == TYPE_CODE_FUNC
)
449 return resolved_type
;
456 /* Return the innermost stack frame that is executing inside of BLOCK and is
457 at least as old as the selected frame. Return NULL if there is no
458 such frame. If BLOCK is NULL, just return NULL. */
461 block_innermost_frame (const struct block
*block
)
466 frame_info_ptr frame
= get_selected_frame ();
467 while (frame
!= NULL
)
469 const struct block
*frame_block
= get_frame_block (frame
, NULL
);
470 if (frame_block
!= NULL
&& block
->contains (frame_block
))
473 frame
= get_prev_frame (frame
);