* gas/arm/abs12.d: Update expected disassembly.
[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
6aba47ca 4 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1 5 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008
26b0da32 6 Free Software Foundation, Inc.
c906108c 7
c5aa993b 8 This file is part of GDB.
c906108c 9
c5aa993b
JM
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
c5aa993b 13 (at your option) any later version.
c906108c 14
c5aa993b
JM
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
c906108c 19
c5aa993b 20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
22
23#include "defs.h"
24#include "symtab.h"
25#include "bfd.h"
c906108c
SS
26#include "objfiles.h"
27#include "frame.h"
28#include "gdbcore.h"
7157eed4
UW
29#include "value.h"
30#include "target.h"
31#include "inferior.h"
c906108c 32#include "annotate.h"
4e052eda 33#include "regcache.h"
4f460812 34#include "gdb_assert.h"
9c1412c1 35#include "dummy-frame.h"
51603483
DJ
36#include "command.h"
37#include "gdbcmd.h"
fe898f56 38#include "block.h"
c906108c 39
51603483 40/* Prototypes for exported functions. */
c5aa993b 41
51603483 42void _initialize_blockframe (void);
c906108c 43
c906108c 44/* Return the innermost lexical block in execution
ae767bfb
JB
45 in a specified stack frame. The frame address is assumed valid.
46
47 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
48 address we used to choose the block. We use this to find a source
49 line, to decide which macro definitions are in scope.
50
51 The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
52 PC, and may not really be a valid PC at all. For example, in the
53 caller of a function declared to never return, the code at the
54 return address will never be reached, so the call instruction may
55 be the very last instruction in the block. So the address we use
56 to choose the block is actually one byte before the return address
57 --- hopefully pointing us at the call instruction, or its delay
58 slot instruction. */
c906108c
SS
59
60struct block *
ae767bfb 61get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
c906108c 62{
c4a09524 63 const CORE_ADDR pc = get_frame_address_in_block (frame);
ae767bfb
JB
64
65 if (addr_in_block)
66 *addr_in_block = pc;
67
c906108c
SS
68 return block_for_pc (pc);
69}
70
c906108c 71CORE_ADDR
fba45db2 72get_pc_function_start (CORE_ADDR pc)
c906108c 73{
2cdd89cb
MK
74 struct block *bl;
75 struct minimal_symbol *msymbol;
c906108c 76
2cdd89cb
MK
77 bl = block_for_pc (pc);
78 if (bl)
c906108c 79 {
7f0df278 80 struct symbol *symbol = block_linkage_function (bl);
2cdd89cb
MK
81
82 if (symbol)
83 {
84 bl = SYMBOL_BLOCK_VALUE (symbol);
85 return BLOCK_START (bl);
86 }
c906108c 87 }
2cdd89cb
MK
88
89 msymbol = lookup_minimal_symbol_by_pc (pc);
90 if (msymbol)
c906108c 91 {
2cdd89cb
MK
92 CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol);
93
94 if (find_pc_section (fstart))
95 return fstart;
c906108c 96 }
2cdd89cb
MK
97
98 return 0;
c906108c
SS
99}
100
101/* Return the symbol for the function executing in frame FRAME. */
102
103struct symbol *
fba45db2 104get_frame_function (struct frame_info *frame)
c906108c 105{
52f0bd74 106 struct block *bl = get_frame_block (frame, 0);
c906108c
SS
107 if (bl == 0)
108 return 0;
7f0df278 109 return block_linkage_function (bl);
c906108c
SS
110}
111\f
112
c906108c
SS
113/* Return the function containing pc value PC in section SECTION.
114 Returns 0 if function is not known. */
115
116struct symbol *
198beae2 117find_pc_sect_function (CORE_ADDR pc, struct bfd_section *section)
c906108c 118{
52f0bd74 119 struct block *b = block_for_pc_sect (pc, section);
c906108c
SS
120 if (b == 0)
121 return 0;
7f0df278 122 return block_linkage_function (b);
c906108c
SS
123}
124
125/* Return the function containing pc value PC.
126 Returns 0 if function is not known. Backward compatibility, no section */
127
128struct symbol *
fba45db2 129find_pc_function (CORE_ADDR pc)
c906108c
SS
130{
131 return find_pc_sect_function (pc, find_pc_mapped_section (pc));
132}
133
134/* These variables are used to cache the most recent result
135 * of find_pc_partial_function. */
136
c5aa993b
JM
137static CORE_ADDR cache_pc_function_low = 0;
138static CORE_ADDR cache_pc_function_high = 0;
139static char *cache_pc_function_name = 0;
198beae2 140static struct bfd_section *cache_pc_function_section = NULL;
c906108c
SS
141
142/* Clear cache, e.g. when symbol table is discarded. */
143
144void
fba45db2 145clear_pc_function_cache (void)
c906108c
SS
146{
147 cache_pc_function_low = 0;
148 cache_pc_function_high = 0;
c5aa993b 149 cache_pc_function_name = (char *) 0;
c906108c
SS
150 cache_pc_function_section = NULL;
151}
152
153/* Finds the "function" (text symbol) that is smaller than PC but
154 greatest of all of the potential text symbols in SECTION. Sets
155 *NAME and/or *ADDRESS conditionally if that pointer is non-null.
156 If ENDADDR is non-null, then set *ENDADDR to be the end of the
157 function (exclusive), but passing ENDADDR as non-null means that
158 the function might cause symbols to be read. This function either
159 succeeds or fails (not halfway succeeds). If it succeeds, it sets
160 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
161 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
162 returns 0. */
163
73912b9b
AC
164/* Backward compatibility, no section argument. */
165
c906108c 166int
73912b9b
AC
167find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address,
168 CORE_ADDR *endaddr)
c906108c 169{
73912b9b 170 struct bfd_section *section;
c906108c 171 struct partial_symtab *pst;
c5aa993b 172 struct symbol *f;
c906108c
SS
173 struct minimal_symbol *msymbol;
174 struct partial_symbol *psb;
c5aa993b 175 struct obj_section *osect;
c906108c
SS
176 int i;
177 CORE_ADDR mapped_pc;
178
73912b9b
AC
179 /* To ensure that the symbol returned belongs to the correct setion
180 (and that the last [random] symbol from the previous section
181 isn't returned) try to find the section containing PC. First try
182 the overlay code (which by default returns NULL); and second try
183 the normal section code (which almost always succeeds). */
184 section = find_pc_overlay (pc);
185 if (section == NULL)
186 {
187 struct obj_section *obj_section = find_pc_section (pc);
188 if (obj_section == NULL)
189 section = NULL;
190 else
191 section = obj_section->the_bfd_section;
192 }
193
c906108c
SS
194 mapped_pc = overlay_mapped_address (pc, section);
195
247055de
MK
196 if (mapped_pc >= cache_pc_function_low
197 && mapped_pc < cache_pc_function_high
198 && section == cache_pc_function_section)
c906108c
SS
199 goto return_cached_value;
200
c906108c
SS
201 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
202 pst = find_pc_sect_psymtab (mapped_pc, section);
203 if (pst)
204 {
205 /* Need to read the symbols to get a good value for the end address. */
206 if (endaddr != NULL && !pst->readin)
207 {
208 /* Need to get the terminal in case symbol-reading produces
209 output. */
210 target_terminal_ours_for_output ();
211 PSYMTAB_TO_SYMTAB (pst);
212 }
213
214 if (pst->readin)
215 {
216 /* Checking whether the msymbol has a larger value is for the
217 "pathological" case mentioned in print_frame_info. */
218 f = find_pc_sect_function (mapped_pc, section);
219 if (f != NULL
220 && (msymbol == NULL
221 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
222 >= SYMBOL_VALUE_ADDRESS (msymbol))))
223 {
c5aa993b
JM
224 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
225 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
3567439c 226 cache_pc_function_name = SYMBOL_LINKAGE_NAME (f);
c906108c
SS
227 cache_pc_function_section = section;
228 goto return_cached_value;
229 }
230 }
231 else
232 {
233 /* Now that static symbols go in the minimal symbol table, perhaps
234 we could just ignore the partial symbols. But at least for now
235 we use the partial or minimal symbol, whichever is larger. */
236 psb = find_pc_sect_psymbol (pst, mapped_pc, section);
237
238 if (psb
239 && (msymbol == NULL ||
240 (SYMBOL_VALUE_ADDRESS (psb)
241 >= SYMBOL_VALUE_ADDRESS (msymbol))))
242 {
243 /* This case isn't being cached currently. */
244 if (address)
245 *address = SYMBOL_VALUE_ADDRESS (psb);
246 if (name)
3567439c 247 *name = SYMBOL_LINKAGE_NAME (psb);
c906108c
SS
248 /* endaddr non-NULL can't happen here. */
249 return 1;
250 }
251 }
252 }
253
254 /* Not in the normal symbol tables, see if the pc is in a known section.
255 If it's not, then give up. This ensures that anything beyond the end
256 of the text seg doesn't appear to be part of the last function in the
257 text segment. */
258
259 osect = find_pc_sect_section (mapped_pc, section);
260
261 if (!osect)
262 msymbol = NULL;
263
264 /* Must be in the minimal symbol table. */
265 if (msymbol == NULL)
266 {
267 /* No available symbol. */
268 if (name != NULL)
269 *name = 0;
270 if (address != NULL)
271 *address = 0;
272 if (endaddr != NULL)
273 *endaddr = 0;
274 return 0;
275 }
276
c5aa993b 277 cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
3567439c 278 cache_pc_function_name = SYMBOL_LINKAGE_NAME (msymbol);
c906108c
SS
279 cache_pc_function_section = section;
280
29e8a844
DJ
281 /* If the minimal symbol has a size, use it for the cache.
282 Otherwise use the lesser of the next minimal symbol in the same
283 section, or the end of the section, as the end of the
284 function. */
c5aa993b 285
29e8a844
DJ
286 if (MSYMBOL_SIZE (msymbol) != 0)
287 cache_pc_function_high = cache_pc_function_low + MSYMBOL_SIZE (msymbol);
288 else
c906108c 289 {
29e8a844
DJ
290 /* Step over other symbols at this same address, and symbols in
291 other sections, to find the next symbol in this section with
292 a different address. */
c906108c 293
3567439c 294 for (i = 1; SYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++)
29e8a844
DJ
295 {
296 if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol)
297 && SYMBOL_BFD_SECTION (msymbol + i) == SYMBOL_BFD_SECTION (msymbol))
298 break;
299 }
300
3567439c 301 if (SYMBOL_LINKAGE_NAME (msymbol + i) != NULL
f1f6aadf 302 && SYMBOL_VALUE_ADDRESS (msymbol + i) < obj_section_endaddr (osect))
29e8a844
DJ
303 cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
304 else
305 /* We got the start address from the last msymbol in the objfile.
306 So the end address is the end of the section. */
f1f6aadf 307 cache_pc_function_high = obj_section_endaddr (osect);
29e8a844 308 }
c906108c 309
247055de 310 return_cached_value:
c906108c
SS
311
312 if (address)
313 {
314 if (pc_in_unmapped_range (pc, section))
c5aa993b 315 *address = overlay_unmapped_address (cache_pc_function_low, section);
c906108c 316 else
c5aa993b 317 *address = cache_pc_function_low;
c906108c 318 }
c5aa993b 319
c906108c
SS
320 if (name)
321 *name = cache_pc_function_name;
322
323 if (endaddr)
324 {
325 if (pc_in_unmapped_range (pc, section))
c5aa993b 326 {
c906108c
SS
327 /* Because the high address is actually beyond the end of
328 the function (and therefore possibly beyond the end of
247055de
MK
329 the overlay), we must actually convert (high - 1) and
330 then add one to that. */
c906108c 331
c5aa993b 332 *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
c906108c 333 section);
c5aa993b 334 }
c906108c 335 else
c5aa993b 336 *endaddr = cache_pc_function_high;
c906108c
SS
337 }
338
339 return 1;
340}
341
c906108c
SS
342/* Return the innermost stack frame executing inside of BLOCK,
343 or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */
344
345struct frame_info *
fba45db2 346block_innermost_frame (struct block *block)
c906108c
SS
347{
348 struct frame_info *frame;
52f0bd74
AC
349 CORE_ADDR start;
350 CORE_ADDR end;
42f99ac2 351 CORE_ADDR calling_pc;
c906108c
SS
352
353 if (block == NULL)
354 return NULL;
355
356 start = BLOCK_START (block);
357 end = BLOCK_END (block);
358
631b0ed0
JB
359 frame = get_current_frame ();
360 while (frame != NULL)
c906108c 361 {
c4a09524 362 calling_pc = get_frame_address_in_block (frame);
42f99ac2 363 if (calling_pc >= start && calling_pc < end)
c906108c 364 return frame;
631b0ed0
JB
365
366 frame = get_prev_frame (frame);
c906108c 367 }
631b0ed0
JB
368
369 return NULL;
c906108c 370}
This page took 0.789003 seconds and 4 git commands to generate.