gdb/
[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,
0fb0cc75 5 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009
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"
edb3359d 39#include "inline-frame.h"
c906108c 40
51603483 41/* Prototypes for exported functions. */
c5aa993b 42
51603483 43void _initialize_blockframe (void);
c906108c 44
c906108c 45/* Return the innermost lexical block in execution
ae767bfb
JB
46 in a specified stack frame. The frame address is assumed valid.
47
48 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
49 address we used to choose the block. We use this to find a source
50 line, to decide which macro definitions are in scope.
51
52 The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
53 PC, and may not really be a valid PC at all. For example, in the
54 caller of a function declared to never return, the code at the
55 return address will never be reached, so the call instruction may
56 be the very last instruction in the block. So the address we use
57 to choose the block is actually one byte before the return address
58 --- hopefully pointing us at the call instruction, or its delay
59 slot instruction. */
c906108c
SS
60
61struct block *
ae767bfb 62get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
c906108c 63{
c4a09524 64 const CORE_ADDR pc = get_frame_address_in_block (frame);
edb3359d
DJ
65 struct frame_info *next_frame;
66 struct block *bl;
67 int inline_count;
ae767bfb
JB
68
69 if (addr_in_block)
70 *addr_in_block = pc;
71
edb3359d
DJ
72 bl = block_for_pc (pc);
73 if (bl == NULL)
74 return NULL;
75
76 inline_count = frame_inlined_callees (frame);
77
78 while (inline_count > 0)
79 {
80 if (block_inlined_p (bl))
81 inline_count--;
82
83 bl = BLOCK_SUPERBLOCK (bl);
84 gdb_assert (bl != NULL);
85 }
86
87 return bl;
c906108c
SS
88}
89
c906108c 90CORE_ADDR
fba45db2 91get_pc_function_start (CORE_ADDR pc)
c906108c 92{
2cdd89cb
MK
93 struct block *bl;
94 struct minimal_symbol *msymbol;
c906108c 95
2cdd89cb
MK
96 bl = block_for_pc (pc);
97 if (bl)
c906108c 98 {
7f0df278 99 struct symbol *symbol = block_linkage_function (bl);
2cdd89cb
MK
100
101 if (symbol)
102 {
103 bl = SYMBOL_BLOCK_VALUE (symbol);
104 return BLOCK_START (bl);
105 }
c906108c 106 }
2cdd89cb
MK
107
108 msymbol = lookup_minimal_symbol_by_pc (pc);
109 if (msymbol)
c906108c 110 {
2cdd89cb
MK
111 CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol);
112
113 if (find_pc_section (fstart))
114 return fstart;
c906108c 115 }
2cdd89cb
MK
116
117 return 0;
c906108c
SS
118}
119
120/* Return the symbol for the function executing in frame FRAME. */
121
122struct symbol *
fba45db2 123get_frame_function (struct frame_info *frame)
c906108c 124{
52f0bd74 125 struct block *bl = get_frame_block (frame, 0);
edb3359d
DJ
126
127 if (bl == NULL)
128 return NULL;
129
130 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
131 bl = BLOCK_SUPERBLOCK (bl);
132
133 return BLOCK_FUNCTION (bl);
c906108c
SS
134}
135\f
136
c906108c
SS
137/* Return the function containing pc value PC in section SECTION.
138 Returns 0 if function is not known. */
139
140struct symbol *
714835d5 141find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
c906108c 142{
52f0bd74 143 struct block *b = block_for_pc_sect (pc, section);
c906108c
SS
144 if (b == 0)
145 return 0;
7f0df278 146 return block_linkage_function (b);
c906108c
SS
147}
148
149/* Return the function containing pc value PC.
150 Returns 0 if function is not known. Backward compatibility, no section */
151
152struct symbol *
fba45db2 153find_pc_function (CORE_ADDR pc)
c906108c
SS
154{
155 return find_pc_sect_function (pc, find_pc_mapped_section (pc));
156}
157
158/* These variables are used to cache the most recent result
159 * of find_pc_partial_function. */
160
c5aa993b
JM
161static CORE_ADDR cache_pc_function_low = 0;
162static CORE_ADDR cache_pc_function_high = 0;
163static char *cache_pc_function_name = 0;
714835d5 164static struct obj_section *cache_pc_function_section = NULL;
c906108c
SS
165
166/* Clear cache, e.g. when symbol table is discarded. */
167
168void
fba45db2 169clear_pc_function_cache (void)
c906108c
SS
170{
171 cache_pc_function_low = 0;
172 cache_pc_function_high = 0;
c5aa993b 173 cache_pc_function_name = (char *) 0;
c906108c
SS
174 cache_pc_function_section = NULL;
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
182 the function might cause symbols to be read. This function either
183 succeeds or fails (not halfway succeeds). If it succeeds, it sets
184 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
185 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
186 returns 0. */
187
73912b9b
AC
188/* Backward compatibility, no section argument. */
189
c906108c 190int
73912b9b
AC
191find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address,
192 CORE_ADDR *endaddr)
c906108c 193{
714835d5 194 struct obj_section *section;
c906108c 195 struct partial_symtab *pst;
c5aa993b 196 struct symbol *f;
c906108c
SS
197 struct minimal_symbol *msymbol;
198 struct partial_symbol *psb;
c906108c
SS
199 int i;
200 CORE_ADDR mapped_pc;
201
73912b9b
AC
202 /* To ensure that the symbol returned belongs to the correct setion
203 (and that the last [random] symbol from the previous section
204 isn't returned) try to find the section containing PC. First try
205 the overlay code (which by default returns NULL); and second try
206 the normal section code (which almost always succeeds). */
207 section = find_pc_overlay (pc);
208 if (section == NULL)
714835d5 209 section = find_pc_section (pc);
73912b9b 210
c906108c
SS
211 mapped_pc = overlay_mapped_address (pc, section);
212
247055de
MK
213 if (mapped_pc >= cache_pc_function_low
214 && mapped_pc < cache_pc_function_high
215 && section == cache_pc_function_section)
c906108c
SS
216 goto return_cached_value;
217
c906108c
SS
218 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
219 pst = find_pc_sect_psymtab (mapped_pc, section);
220 if (pst)
221 {
222 /* Need to read the symbols to get a good value for the end address. */
223 if (endaddr != NULL && !pst->readin)
224 {
225 /* Need to get the terminal in case symbol-reading produces
226 output. */
227 target_terminal_ours_for_output ();
228 PSYMTAB_TO_SYMTAB (pst);
229 }
230
231 if (pst->readin)
232 {
233 /* Checking whether the msymbol has a larger value is for the
234 "pathological" case mentioned in print_frame_info. */
235 f = find_pc_sect_function (mapped_pc, section);
236 if (f != NULL
237 && (msymbol == NULL
238 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
239 >= SYMBOL_VALUE_ADDRESS (msymbol))))
240 {
c5aa993b
JM
241 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
242 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
3567439c 243 cache_pc_function_name = SYMBOL_LINKAGE_NAME (f);
c906108c
SS
244 cache_pc_function_section = section;
245 goto return_cached_value;
246 }
247 }
248 else
249 {
250 /* Now that static symbols go in the minimal symbol table, perhaps
251 we could just ignore the partial symbols. But at least for now
252 we use the partial or minimal symbol, whichever is larger. */
253 psb = find_pc_sect_psymbol (pst, mapped_pc, section);
254
255 if (psb
256 && (msymbol == NULL ||
257 (SYMBOL_VALUE_ADDRESS (psb)
258 >= SYMBOL_VALUE_ADDRESS (msymbol))))
259 {
260 /* This case isn't being cached currently. */
261 if (address)
262 *address = SYMBOL_VALUE_ADDRESS (psb);
263 if (name)
3567439c 264 *name = SYMBOL_LINKAGE_NAME (psb);
c906108c
SS
265 /* endaddr non-NULL can't happen here. */
266 return 1;
267 }
268 }
269 }
270
271 /* Not in the normal symbol tables, see if the pc is in a known section.
272 If it's not, then give up. This ensures that anything beyond the end
273 of the text seg doesn't appear to be part of the last function in the
274 text segment. */
275
714835d5 276 if (!section)
c906108c
SS
277 msymbol = NULL;
278
279 /* Must be in the minimal symbol table. */
280 if (msymbol == NULL)
281 {
282 /* No available symbol. */
283 if (name != NULL)
284 *name = 0;
285 if (address != NULL)
286 *address = 0;
287 if (endaddr != NULL)
288 *endaddr = 0;
289 return 0;
290 }
291
c5aa993b 292 cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
3567439c 293 cache_pc_function_name = SYMBOL_LINKAGE_NAME (msymbol);
c906108c
SS
294 cache_pc_function_section = section;
295
29e8a844
DJ
296 /* If the minimal symbol has a size, use it for the cache.
297 Otherwise use the lesser of the next minimal symbol in the same
298 section, or the end of the section, as the end of the
299 function. */
c5aa993b 300
29e8a844
DJ
301 if (MSYMBOL_SIZE (msymbol) != 0)
302 cache_pc_function_high = cache_pc_function_low + MSYMBOL_SIZE (msymbol);
303 else
c906108c 304 {
29e8a844
DJ
305 /* Step over other symbols at this same address, and symbols in
306 other sections, to find the next symbol in this section with
307 a different address. */
c906108c 308
3567439c 309 for (i = 1; SYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++)
29e8a844
DJ
310 {
311 if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol)
714835d5 312 && SYMBOL_OBJ_SECTION (msymbol + i) == SYMBOL_OBJ_SECTION (msymbol))
29e8a844
DJ
313 break;
314 }
315
3567439c 316 if (SYMBOL_LINKAGE_NAME (msymbol + i) != NULL
714835d5 317 && SYMBOL_VALUE_ADDRESS (msymbol + i) < obj_section_endaddr (section))
29e8a844
DJ
318 cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
319 else
320 /* We got the start address from the last msymbol in the objfile.
321 So the end address is the end of the section. */
714835d5 322 cache_pc_function_high = obj_section_endaddr (section);
29e8a844 323 }
c906108c 324
247055de 325 return_cached_value:
c906108c
SS
326
327 if (address)
328 {
329 if (pc_in_unmapped_range (pc, section))
c5aa993b 330 *address = overlay_unmapped_address (cache_pc_function_low, section);
c906108c 331 else
c5aa993b 332 *address = cache_pc_function_low;
c906108c 333 }
c5aa993b 334
c906108c
SS
335 if (name)
336 *name = cache_pc_function_name;
337
338 if (endaddr)
339 {
340 if (pc_in_unmapped_range (pc, section))
c5aa993b 341 {
c906108c
SS
342 /* Because the high address is actually beyond the end of
343 the function (and therefore possibly beyond the end of
247055de
MK
344 the overlay), we must actually convert (high - 1) and
345 then add one to that. */
c906108c 346
c5aa993b 347 *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
c906108c 348 section);
c5aa993b 349 }
c906108c 350 else
c5aa993b 351 *endaddr = cache_pc_function_high;
c906108c
SS
352 }
353
354 return 1;
355}
356
c906108c
SS
357/* Return the innermost stack frame executing inside of BLOCK,
358 or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */
359
360struct frame_info *
fba45db2 361block_innermost_frame (struct block *block)
c906108c
SS
362{
363 struct frame_info *frame;
52f0bd74
AC
364 CORE_ADDR start;
365 CORE_ADDR end;
42f99ac2 366 CORE_ADDR calling_pc;
c906108c
SS
367
368 if (block == NULL)
369 return NULL;
370
371 start = BLOCK_START (block);
372 end = BLOCK_END (block);
373
631b0ed0
JB
374 frame = get_current_frame ();
375 while (frame != NULL)
c906108c 376 {
edb3359d
DJ
377 struct block *frame_block = get_frame_block (frame, NULL);
378 if (frame_block != NULL && contained_in (frame_block, block))
c906108c 379 return frame;
631b0ed0
JB
380
381 frame = get_prev_frame (frame);
c906108c 382 }
631b0ed0
JB
383
384 return NULL;
c906108c 385}
This page took 0.585081 seconds and 4 git commands to generate.