GDB (xrefs)
Loading...
Searching...
No Matches
blockframe.c
Go to the documentation of this file.
1/* Get info from stack frames; convert between frames, blocks,
2 functions and pc values.
3
4 Copyright (C) 1986-2023 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21#include "defs.h"
22#include "symtab.h"
23#include "bfd.h"
24#include "objfiles.h"
25#include "frame.h"
26#include "gdbcore.h"
27#include "value.h"
28#include "target.h"
29#include "inferior.h"
30#include "annotate.h"
31#include "regcache.h"
32#include "dummy-frame.h"
33#include "command.h"
34#include "gdbcmd.h"
35#include "block.h"
36#include "inline-frame.h"
37
38/* Return the innermost lexical block in execution in a specified
39 stack frame. The frame address is assumed valid.
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. */
53
54const struct block *
55get_frame_block (frame_info_ptr frame, CORE_ADDR *addr_in_block)
56{
57 CORE_ADDR pc;
58 const struct block *bl;
59 int inline_count;
60
62 return NULL;
63
64 if (addr_in_block)
65 *addr_in_block = pc;
66
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 (bl->inlined_p ())
76 inline_count--;
77
78 bl = bl->superblock ();
79 gdb_assert (bl != NULL);
80 }
81
82 return bl;
83}
84
85CORE_ADDR
87{
88 const struct block *bl;
89 struct bound_minimal_symbol msymbol;
90
91 bl = block_for_pc (pc);
92 if (bl)
93 {
94 struct symbol *symbol = bl->linkage_function ();
95
96 if (symbol)
97 {
98 bl = symbol->value_block ();
99 return bl->entry_pc ();
100 }
101 }
102
103 msymbol = lookup_minimal_symbol_by_pc (pc);
104 if (msymbol.minsym)
105 {
106 CORE_ADDR fstart = msymbol.value_address ();
107
108 if (find_pc_section (fstart))
109 return fstart;
110 }
111
112 return 0;
113}
114
115/* Return the symbol for the function executing in frame FRAME. */
116
117struct symbol *
119{
120 const struct block *bl = get_frame_block (frame, 0);
121
122 if (bl == NULL)
123 return NULL;
124
125 while (bl->function () == NULL && bl->superblock () != NULL)
126 bl = bl->superblock ();
127
128 return bl->function ();
129}
130
131
132/* Return the function containing pc value PC in section SECTION.
133 Returns 0 if function is not known. */
134
135struct symbol *
136find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
137{
138 const struct block *b = block_for_pc_sect (pc, section);
139
140 if (b == 0)
141 return 0;
142 return b->linkage_function ();
143}
144
145/* Return the function containing pc value PC.
146 Returns 0 if function is not known.
147 Backward compatibility, no section */
148
149struct symbol *
150find_pc_function (CORE_ADDR pc)
151{
153}
154
155/* See symtab.h. */
156
157struct symbol *
158find_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 bl->containing_function ();
166}
167
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. */
191
192static CORE_ADDR cache_pc_function_low = 0;
193static CORE_ADDR cache_pc_function_high = 0;
196static const struct block *cache_pc_function_block = nullptr;
197
198/* Clear cache, e.g. when symbol table is discarded. */
199
200void
209
210/* See symtab.h. */
211
212bool
214 const struct general_symbol_info **sym,
215 CORE_ADDR *address, CORE_ADDR *endaddr,
216 const struct block **block)
217{
218 struct obj_section *section;
219 struct symbol *f;
220 struct bound_minimal_symbol msymbol;
221 struct compunit_symtab *compunit_symtab = NULL;
222 CORE_ADDR mapped_pc;
223
224 /* To ensure that the symbol returned belongs to the correct section
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)
231 section = find_pc_section (pc);
232
233 mapped_pc = overlay_mapped_address (pc, section);
234
235 if (mapped_pc >= cache_pc_function_low
236 && mapped_pc < cache_pc_function_high
237 && section == cache_pc_function_section)
238 goto return_cached_value;
239
240 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
241 compunit_symtab = find_pc_sect_compunit_symtab (mapped_pc, section);
242
243 if (compunit_symtab != NULL)
244 {
245 /* Checking whether the msymbol has a larger value is for the
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. */
254 f = find_pc_sect_function (mapped_pc, section);
255 if (f != NULL
256 && (msymbol.minsym == NULL
257 || (f->value_block ()->entry_pc ()
258 >= msymbol.value_address ())))
259 {
260 const struct block *b = f->value_block ();
261
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 (b->is_contiguous ())
280 {
283 }
284 else
285 {
286 bool found = false;
287 for (const blockrange &range : b->ranges ())
288 {
289 if (range.start () <= mapped_pc && mapped_pc < range.end ())
290 {
291 cache_pc_function_low = range.start ();
293 found = true;
294 break;
295 }
296 }
297 /* Above loop should exit via the break. */
298 gdb_assert (found);
299 }
300
301
302 goto return_cached_value;
303 }
304 }
305
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. */
310
311 if (!section)
312 msymbol.minsym = NULL;
313
314 /* Must be in the minimal symbol table. */
315 if (msymbol.minsym == NULL)
316 {
317 /* No available symbol. */
318 if (sym != nullptr)
319 *sym = 0;
320 if (address != NULL)
321 *address = 0;
322 if (endaddr != NULL)
323 *endaddr = 0;
324 if (block != nullptr)
325 *block = nullptr;
326 return false;
327 }
328
333 cache_pc_function_block = nullptr;
334
335 return_cached_value:
336
337 if (address)
338 {
339 if (pc_in_unmapped_range (pc, section))
341 else
342 *address = cache_pc_function_low;
343 }
344
345 if (sym != nullptr)
347
348 if (endaddr)
349 {
350 if (pc_in_unmapped_range (pc, section))
351 {
352 /* Because the high address is actually beyond the end of
353 the function (and therefore possibly beyond the end of
354 the overlay), we must actually convert (high - 1) and
355 then add one to that. */
356
358 section);
359 }
360 else
361 *endaddr = cache_pc_function_high;
362 }
363
364 if (block != nullptr)
366
367 return true;
368}
369
370/* See symtab.h. */
371
372bool
373find_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
384/* See symtab.h. */
385
386bool
387find_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->is_contiguous ())
394 {
395 CORE_ADDR entry_pc = block->entry_pc ();
396
397 for (const blockrange &range : block->ranges ())
398 {
399 if (range.start () <= entry_pc && entry_pc < range.end ())
400 {
401 if (address != nullptr)
402 *address = range.start ();
403
404 if (endaddr != nullptr)
405 *endaddr = range.end ();
406
407 return status;
408 }
409 }
410
411 /* It's an internal error if we exit the above loop without finding
412 the range. */
413 internal_error (_("Entry block not found in find_function_entry_range_from_pc"));
414 }
415
416 return status;
417}
418
419/* See symtab.h. */
420
421struct type *
422find_function_type (CORE_ADDR pc)
423{
424 struct symbol *sym = find_pc_function (pc);
425
426 if (sym != NULL && sym->value_block ()->entry_pc () == pc)
427 return sym->type ();
428
429 return NULL;
430}
431
432/* See symtab.h. */
433
434struct type *
435find_gnu_ifunc_target_type (CORE_ADDR resolver_funaddr)
436{
437 struct type *resolver_type = find_function_type (resolver_funaddr);
438 if (resolver_type != NULL)
439 {
440 /* Get the return type of the resolver. */
441 struct type *resolver_ret_type
442 = check_typedef (resolver_type->target_type ());
443
444 /* If we found a pointer to function, then the resolved type
445 is the type of the pointed-to function. */
446 if (resolver_ret_type->code () == TYPE_CODE_PTR)
447 {
448 struct type *resolved_type
449 = resolver_ret_type->target_type ();
450 if (check_typedef (resolved_type)->code () == TYPE_CODE_FUNC)
451 return resolved_type;
452 }
453 }
454
455 return NULL;
456}
457
458/* Return the innermost stack frame that is executing inside of BLOCK and is
459 at least as old as the selected frame. Return NULL if there is no
460 such frame. If BLOCK is NULL, just return NULL. */
461
464{
465 if (block == NULL)
466 return NULL;
467
469 while (frame != NULL)
470 {
471 const struct block *frame_block = get_frame_block (frame, NULL);
472 if (frame_block != NULL && block->contains (frame_block))
473 return frame;
474
475 frame = get_prev_frame (frame);
476 }
477
478 return NULL;
479}
const char *const name
int code
Definition ada-lex.l:670
void f()
Definition 1.cc:36
const struct block * block_for_pc(CORE_ADDR pc)
Definition block.c:276
const struct block * block_for_pc_sect(CORE_ADDR pc, struct obj_section *section)
Definition block.c:261
static CORE_ADDR cache_pc_function_low
Definition blockframe.c:192
bool find_pc_partial_function_sym(CORE_ADDR pc, const struct general_symbol_info **sym, CORE_ADDR *address, CORE_ADDR *endaddr, const struct block **block)
Definition blockframe.c:213
struct symbol * find_pc_function(CORE_ADDR pc)
Definition blockframe.c:150
bool find_function_entry_range_from_pc(CORE_ADDR pc, const char **name, CORE_ADDR *address, CORE_ADDR *endaddr)
Definition blockframe.c:387
const struct block * get_frame_block(frame_info_ptr frame, CORE_ADDR *addr_in_block)
Definition blockframe.c:55
bool find_pc_partial_function(CORE_ADDR pc, const char **name, CORE_ADDR *address, CORE_ADDR *endaddr, const struct block **block)
Definition blockframe.c:373
struct symbol * get_frame_function(frame_info_ptr frame)
Definition blockframe.c:118
frame_info_ptr block_innermost_frame(const struct block *block)
Definition blockframe.c:463
CORE_ADDR get_pc_function_start(CORE_ADDR pc)
Definition blockframe.c:86
struct symbol * find_pc_sect_containing_function(CORE_ADDR pc, struct obj_section *section)
Definition blockframe.c:158
struct type * find_function_type(CORE_ADDR pc)
Definition blockframe.c:422
static const general_symbol_info * cache_pc_function_sym
Definition blockframe.c:194
static const struct block * cache_pc_function_block
Definition blockframe.c:196
static struct obj_section * cache_pc_function_section
Definition blockframe.c:195
struct type * find_gnu_ifunc_target_type(CORE_ADDR resolver_funaddr)
Definition blockframe.c:435
struct symbol * find_pc_sect_function(CORE_ADDR pc, struct obj_section *section)
Definition blockframe.c:136
static CORE_ADDR cache_pc_function_high
Definition blockframe.c:193
void clear_pc_function_cache(void)
Definition blockframe.c:201
bool get_frame_address_in_block_if_available(frame_info_ptr this_frame, CORE_ADDR *pc)
Definition frame.c:2802
frame_info_ptr get_selected_frame(const char *message)
Definition frame.c:1888
frame_info_ptr get_prev_frame(frame_info_ptr this_frame)
Definition frame.c:2614
struct type * check_typedef(struct type *type)
Definition gdbtypes.c:2966
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
Definition gnu-nat.c:1790
int frame_inlined_callees(frame_info_ptr this_frame)
bound_minimal_symbol lookup_minimal_symbol_by_pc_section(CORE_ADDR pc_in, struct obj_section *section, lookup_msym_prefer prefer, bound_minimal_symbol *previous)
Definition minsyms.c:746
CORE_ADDR minimal_symbol_upper_bound(struct bound_minimal_symbol minsym)
Definition minsyms.c:1589
struct bound_minimal_symbol lookup_minimal_symbol_by_pc(CORE_ADDR pc)
Definition minsyms.c:996
struct obj_section * find_pc_section(CORE_ADDR pc)
Definition objfiles.c:1128
Definition block.h:109
const block * superblock() const
Definition block.h:135
bool is_contiguous() const
Definition block.h:177
bool contains(const struct block *a, bool allow_nested=false) const
Definition block.c:68
gdb::array_view< blockrange > ranges()
Definition block.h:155
CORE_ADDR start() const
Definition block.h:111
CORE_ADDR entry_pc() const
Definition block.h:195
struct symbol * containing_function() const
Definition block.c:105
bool inlined_p() const
Definition block.c:118
CORE_ADDR end() const
Definition block.h:119
symbol * function() const
Definition block.h:127
struct symbol * linkage_function() const
Definition block.c:91
CORE_ADDR value_address() const
Definition minsyms.h:41
struct minimal_symbol * minsym
Definition minsyms.h:49
const char * linkage_name() const
Definition symtab.h:460
Definition value.h:90
const block * value_block() const
Definition symtab.h:1549
struct type * type() const
Definition symtab.h:1331
struct type * target_type() const
Definition gdbtypes.h:1037
type_code code() const
Definition gdbtypes.h:956
struct obj_section * find_pc_overlay(CORE_ADDR pc)
Definition symfile.c:3174
CORE_ADDR overlay_unmapped_address(CORE_ADDR pc, struct obj_section *section)
Definition symfile.c:3108
bool pc_in_unmapped_range(CORE_ADDR pc, struct obj_section *section)
Definition symfile.c:3055
CORE_ADDR overlay_mapped_address(CORE_ADDR pc, struct obj_section *section)
Definition symfile.c:3126
struct obj_section * find_pc_mapped_section(CORE_ADDR pc)
Definition symfile.c:3203
struct compunit_symtab * find_pc_sect_compunit_symtab(CORE_ADDR pc, struct obj_section *section)
Definition symtab.c:2827