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