frame: add frame_id_build_unavailable_stack_special
[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
ecd75fc8 4 Copyright (C) 1986-2014 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"
22#include "symtab.h"
23#include "bfd.h"
c906108c
SS
24#include "objfiles.h"
25#include "frame.h"
26#include "gdbcore.h"
7157eed4
UW
27#include "value.h"
28#include "target.h"
29#include "inferior.h"
c906108c 30#include "annotate.h"
4e052eda 31#include "regcache.h"
4f460812 32#include "gdb_assert.h"
9c1412c1 33#include "dummy-frame.h"
51603483
DJ
34#include "command.h"
35#include "gdbcmd.h"
fe898f56 36#include "block.h"
edb3359d 37#include "inline-frame.h"
c906108c 38
4a64f543
MS
39/* Return the innermost lexical block in execution in a specified
40 stack frame. The frame address is assumed valid.
ae767bfb
JB
41
42 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
43 address we used to choose the block. We use this to find a source
44 line, to decide which macro definitions are in scope.
45
46 The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
47 PC, and may not really be a valid PC at all. For example, in the
48 caller of a function declared to never return, the code at the
49 return address will never be reached, so the call instruction may
50 be the very last instruction in the block. So the address we use
51 to choose the block is actually one byte before the return address
52 --- hopefully pointing us at the call instruction, or its delay
53 slot instruction. */
c906108c
SS
54
55struct block *
ae767bfb 56get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
c906108c 57{
e3eebbd7 58 CORE_ADDR pc;
edb3359d
DJ
59 struct block *bl;
60 int inline_count;
ae767bfb 61
e3eebbd7
PA
62 if (!get_frame_address_in_block_if_available (frame, &pc))
63 return NULL;
64
ae767bfb
JB
65 if (addr_in_block)
66 *addr_in_block = pc;
67
edb3359d
DJ
68 bl = block_for_pc (pc);
69 if (bl == NULL)
70 return NULL;
71
72 inline_count = frame_inlined_callees (frame);
73
74 while (inline_count > 0)
75 {
76 if (block_inlined_p (bl))
77 inline_count--;
78
79 bl = BLOCK_SUPERBLOCK (bl);
80 gdb_assert (bl != NULL);
81 }
82
83 return bl;
c906108c
SS
84}
85
c906108c 86CORE_ADDR
fba45db2 87get_pc_function_start (CORE_ADDR pc)
c906108c 88{
2cdd89cb 89 struct block *bl;
7cbd4a93 90 struct bound_minimal_symbol msymbol;
c906108c 91
2cdd89cb
MK
92 bl = block_for_pc (pc);
93 if (bl)
c906108c 94 {
7f0df278 95 struct symbol *symbol = block_linkage_function (bl);
2cdd89cb
MK
96
97 if (symbol)
98 {
99 bl = SYMBOL_BLOCK_VALUE (symbol);
100 return BLOCK_START (bl);
101 }
c906108c 102 }
2cdd89cb
MK
103
104 msymbol = lookup_minimal_symbol_by_pc (pc);
7cbd4a93 105 if (msymbol.minsym)
c906108c 106 {
7cbd4a93 107 CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol.minsym);
2cdd89cb
MK
108
109 if (find_pc_section (fstart))
110 return fstart;
c906108c 111 }
2cdd89cb
MK
112
113 return 0;
c906108c
SS
114}
115
116/* Return the symbol for the function executing in frame FRAME. */
117
118struct symbol *
fba45db2 119get_frame_function (struct frame_info *frame)
c906108c 120{
52f0bd74 121 struct block *bl = get_frame_block (frame, 0);
edb3359d
DJ
122
123 if (bl == NULL)
124 return NULL;
125
126 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
127 bl = BLOCK_SUPERBLOCK (bl);
128
129 return BLOCK_FUNCTION (bl);
c906108c
SS
130}
131\f
132
c906108c
SS
133/* Return the function containing pc value PC in section SECTION.
134 Returns 0 if function is not known. */
135
136struct symbol *
714835d5 137find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
c906108c 138{
52f0bd74 139 struct block *b = block_for_pc_sect (pc, section);
cc59ec59 140
c906108c
SS
141 if (b == 0)
142 return 0;
7f0df278 143 return block_linkage_function (b);
c906108c
SS
144}
145
146/* Return the function containing pc value PC.
4a64f543
MS
147 Returns 0 if function is not known.
148 Backward compatibility, no section */
c906108c
SS
149
150struct symbol *
fba45db2 151find_pc_function (CORE_ADDR pc)
c906108c
SS
152{
153 return find_pc_sect_function (pc, find_pc_mapped_section (pc));
154}
155
156/* These variables are used to cache the most recent result
4a64f543 157 of find_pc_partial_function. */
c906108c 158
c5aa993b
JM
159static CORE_ADDR cache_pc_function_low = 0;
160static CORE_ADDR cache_pc_function_high = 0;
2c02bd72 161static const char *cache_pc_function_name = 0;
714835d5 162static struct obj_section *cache_pc_function_section = NULL;
11c81455 163static int cache_pc_function_is_gnu_ifunc = 0;
c906108c 164
4a64f543 165/* Clear cache, e.g. when symbol table is discarded. */
c906108c
SS
166
167void
fba45db2 168clear_pc_function_cache (void)
c906108c
SS
169{
170 cache_pc_function_low = 0;
171 cache_pc_function_high = 0;
c5aa993b 172 cache_pc_function_name = (char *) 0;
c906108c 173 cache_pc_function_section = NULL;
11c81455 174 cache_pc_function_is_gnu_ifunc = 0;
c906108c
SS
175}
176
177/* Finds the "function" (text symbol) that is smaller than PC but
178 greatest of all of the potential text symbols in SECTION. Sets
179 *NAME and/or *ADDRESS conditionally if that pointer is non-null.
180 If ENDADDR is non-null, then set *ENDADDR to be the end of the
181 function (exclusive), but passing ENDADDR as non-null means that
11c81455
JK
182 the function might cause symbols to be read. If IS_GNU_IFUNC_P is provided
183 *IS_GNU_IFUNC_P is set to 1 on return if the function is STT_GNU_IFUNC.
184 This function either succeeds or fails (not halfway succeeds). If it
185 succeeds, it sets *NAME, *ADDRESS, and *ENDADDR to real information and
186 returns 1. If it fails, it sets *NAME, *ADDRESS, *ENDADDR and
187 *IS_GNU_IFUNC_P to zero and returns 0. */
c906108c 188
73912b9b
AC
189/* Backward compatibility, no section argument. */
190
c906108c 191int
2c02bd72 192find_pc_partial_function_gnu_ifunc (CORE_ADDR pc, const char **name,
11c81455
JK
193 CORE_ADDR *address, CORE_ADDR *endaddr,
194 int *is_gnu_ifunc_p)
c906108c 195{
714835d5 196 struct obj_section *section;
c5aa993b 197 struct symbol *f;
c906108c 198 struct minimal_symbol *msymbol;
ccefe4c4
TT
199 struct symtab *symtab = NULL;
200 struct objfile *objfile;
c906108c
SS
201 int i;
202 CORE_ADDR mapped_pc;
203
73912b9b
AC
204 /* To ensure that the symbol returned belongs to the correct setion
205 (and that the last [random] symbol from the previous section
206 isn't returned) try to find the section containing PC. First try
207 the overlay code (which by default returns NULL); and second try
208 the normal section code (which almost always succeeds). */
209 section = find_pc_overlay (pc);
210 if (section == NULL)
714835d5 211 section = find_pc_section (pc);
73912b9b 212
c906108c
SS
213 mapped_pc = overlay_mapped_address (pc, section);
214
247055de
MK
215 if (mapped_pc >= cache_pc_function_low
216 && mapped_pc < cache_pc_function_high
217 && section == cache_pc_function_section)
c906108c
SS
218 goto return_cached_value;
219
7cbd4a93 220 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section).minsym;
ccefe4c4
TT
221 ALL_OBJFILES (objfile)
222 {
223 if (objfile->sf)
224 symtab = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol,
225 mapped_pc, section, 0);
226 if (symtab)
227 break;
228 }
229
230 if (symtab)
c906108c 231 {
ccefe4c4
TT
232 /* Checking whether the msymbol has a larger value is for the
233 "pathological" case mentioned in print_frame_info. */
234 f = find_pc_sect_function (mapped_pc, section);
235 if (f != NULL
236 && (msymbol == NULL
237 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
238 >= SYMBOL_VALUE_ADDRESS (msymbol))))
c906108c 239 {
ccefe4c4
TT
240 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
241 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
242 cache_pc_function_name = SYMBOL_LINKAGE_NAME (f);
243 cache_pc_function_section = section;
11c81455 244 cache_pc_function_is_gnu_ifunc = TYPE_GNU_IFUNC (SYMBOL_TYPE (f));
ccefe4c4 245 goto return_cached_value;
c906108c
SS
246 }
247 }
248
4a64f543
MS
249 /* Not in the normal symbol tables, see if the pc is in a known
250 section. If it's not, then give up. This ensures that anything
251 beyond the end of the text seg doesn't appear to be part of the
252 last function in the text segment. */
c906108c 253
714835d5 254 if (!section)
c906108c
SS
255 msymbol = NULL;
256
257 /* Must be in the minimal symbol table. */
258 if (msymbol == NULL)
259 {
260 /* No available symbol. */
261 if (name != NULL)
262 *name = 0;
263 if (address != NULL)
264 *address = 0;
265 if (endaddr != NULL)
266 *endaddr = 0;
11c81455
JK
267 if (is_gnu_ifunc_p != NULL)
268 *is_gnu_ifunc_p = 0;
c906108c
SS
269 return 0;
270 }
271
c5aa993b 272 cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
3567439c 273 cache_pc_function_name = SYMBOL_LINKAGE_NAME (msymbol);
c906108c 274 cache_pc_function_section = section;
11c81455 275 cache_pc_function_is_gnu_ifunc = MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc;
c906108c 276
29e8a844
DJ
277 /* If the minimal symbol has a size, use it for the cache.
278 Otherwise use the lesser of the next minimal symbol in the same
279 section, or the end of the section, as the end of the
280 function. */
c5aa993b 281
29e8a844
DJ
282 if (MSYMBOL_SIZE (msymbol) != 0)
283 cache_pc_function_high = cache_pc_function_low + MSYMBOL_SIZE (msymbol);
284 else
c906108c 285 {
29e8a844
DJ
286 /* Step over other symbols at this same address, and symbols in
287 other sections, to find the next symbol in this section with
288 a different address. */
c906108c 289
3567439c 290 for (i = 1; SYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++)
29e8a844 291 {
3e43a32a
MS
292 if (SYMBOL_VALUE_ADDRESS (msymbol + i)
293 != SYMBOL_VALUE_ADDRESS (msymbol)
e27d198c
TT
294 && SYMBOL_SECTION (msymbol + i)
295 == SYMBOL_SECTION (msymbol))
29e8a844
DJ
296 break;
297 }
298
3567439c 299 if (SYMBOL_LINKAGE_NAME (msymbol + i) != NULL
3e43a32a
MS
300 && SYMBOL_VALUE_ADDRESS (msymbol + i)
301 < obj_section_endaddr (section))
29e8a844
DJ
302 cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
303 else
304 /* We got the start address from the last msymbol in the objfile.
305 So the end address is the end of the section. */
714835d5 306 cache_pc_function_high = obj_section_endaddr (section);
29e8a844 307 }
c906108c 308
247055de 309 return_cached_value:
c906108c
SS
310
311 if (address)
312 {
313 if (pc_in_unmapped_range (pc, section))
c5aa993b 314 *address = overlay_unmapped_address (cache_pc_function_low, section);
c906108c 315 else
c5aa993b 316 *address = cache_pc_function_low;
c906108c 317 }
c5aa993b 318
c906108c
SS
319 if (name)
320 *name = cache_pc_function_name;
321
322 if (endaddr)
323 {
324 if (pc_in_unmapped_range (pc, section))
c5aa993b 325 {
c906108c
SS
326 /* Because the high address is actually beyond the end of
327 the function (and therefore possibly beyond the end of
247055de 328 the overlay), we must actually convert (high - 1) and
4a64f543 329 then add one to that. */
c906108c 330
c5aa993b 331 *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
c906108c 332 section);
c5aa993b 333 }
c906108c 334 else
c5aa993b 335 *endaddr = cache_pc_function_high;
c906108c
SS
336 }
337
11c81455
JK
338 if (is_gnu_ifunc_p)
339 *is_gnu_ifunc_p = cache_pc_function_is_gnu_ifunc;
340
c906108c
SS
341 return 1;
342}
343
11c81455
JK
344/* See find_pc_partial_function_gnu_ifunc, only the IS_GNU_IFUNC_P parameter
345 is omitted here for backward API compatibility. */
346
347int
2c02bd72 348find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
11c81455
JK
349 CORE_ADDR *endaddr)
350{
351 return find_pc_partial_function_gnu_ifunc (pc, name, address, endaddr, NULL);
352}
353
72384ba3
PH
354/* Return the innermost stack frame that is executing inside of BLOCK and is
355 at least as old as the selected frame. Return NULL if there is no
356 such frame. If BLOCK is NULL, just return NULL. */
c906108c
SS
357
358struct frame_info *
9df2fbc4 359block_innermost_frame (const struct block *block)
c906108c
SS
360{
361 struct frame_info *frame;
c906108c
SS
362
363 if (block == NULL)
364 return NULL;
365
72384ba3
PH
366 frame = get_selected_frame_if_set ();
367 if (frame == NULL)
368 frame = get_current_frame ();
631b0ed0 369 while (frame != NULL)
c906108c 370 {
edb3359d
DJ
371 struct block *frame_block = get_frame_block (frame, NULL);
372 if (frame_block != NULL && contained_in (frame_block, block))
c906108c 373 return frame;
631b0ed0
JB
374
375 frame = get_prev_frame (frame);
c906108c 376 }
631b0ed0
JB
377
378 return NULL;
c906108c 379}
This page took 0.824698 seconds and 4 git commands to generate.