1 /* Get info from stack frames; convert between frames, blocks,
2 functions and pc values.
4 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
5 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
6 Free Software Foundation, Inc.
8 This file is part of GDB.
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
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
31 #include "value.h" /* for read_register */
32 #include "target.h" /* for target_has_stack */
33 #include "inferior.h" /* for read_pc */
36 #include "gdb_assert.h"
37 #include "dummy-frame.h"
42 /* Prototypes for exported functions. */
44 void _initialize_blockframe (void);
46 /* Test whether PC is in the range of addresses that corresponds to
47 the "main" function. */
50 inside_main_func (CORE_ADDR pc
)
52 struct minimal_symbol
*msymbol
;
54 if (symfile_objfile
== 0)
57 msymbol
= lookup_minimal_symbol (main_name (), NULL
, symfile_objfile
);
59 /* If the address range hasn't been set up at symbol reading time,
63 && symfile_objfile
->ei
.main_func_lowpc
== INVALID_ENTRY_LOWPC
64 && symfile_objfile
->ei
.main_func_highpc
== INVALID_ENTRY_HIGHPC
)
66 /* brobecker/2003-10-10: We used to rely on lookup_symbol() to
67 search the symbol associated to the "main" function.
68 Unfortunately, lookup_symbol() uses the current-language
69 la_lookup_symbol_nonlocal function to do the global symbol
70 search. Depending on the language, this can introduce
71 certain side-effects, because certain languages, for instance
72 Ada, may find more than one match. Therefore we prefer to
73 search the "main" function symbol using its address rather
75 struct symbol
*mainsym
=
76 find_pc_function (SYMBOL_VALUE_ADDRESS (msymbol
));
78 if (mainsym
&& SYMBOL_CLASS (mainsym
) == LOC_BLOCK
)
80 symfile_objfile
->ei
.main_func_lowpc
=
81 BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym
));
82 symfile_objfile
->ei
.main_func_highpc
=
83 BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym
));
87 /* Not in the normal symbol tables, see if "main" is in the partial
88 symbol table. If it's not, then give up. */
89 if (msymbol
!= NULL
&& MSYMBOL_TYPE (msymbol
) == mst_text
)
91 CORE_ADDR maddr
= SYMBOL_VALUE_ADDRESS (msymbol
);
92 asection
*msect
= SYMBOL_BFD_SECTION (msymbol
);
93 struct obj_section
*osect
= find_pc_sect_section (maddr
, msect
);
99 /* Step over other symbols at this same address, and symbols
100 in other sections, to find the next symbol in this
101 section with a different address. */
102 for (i
= 1; SYMBOL_LINKAGE_NAME (msymbol
+ i
) != NULL
; i
++)
104 if (SYMBOL_VALUE_ADDRESS (msymbol
+ i
) != maddr
105 && SYMBOL_BFD_SECTION (msymbol
+ i
) == msect
)
109 symfile_objfile
->ei
.main_func_lowpc
= maddr
;
111 /* Use the lesser of the next minimal symbol in the same
112 section, or the end of the section, as the end of the
114 if (SYMBOL_LINKAGE_NAME (msymbol
+ i
) != NULL
115 && SYMBOL_VALUE_ADDRESS (msymbol
+ i
) < osect
->endaddr
)
116 symfile_objfile
->ei
.main_func_highpc
=
117 SYMBOL_VALUE_ADDRESS (msymbol
+ i
);
119 /* We got the start address from the last msymbol in the
120 objfile. So the end address is the end of the
122 symfile_objfile
->ei
.main_func_highpc
= osect
->endaddr
;
126 return (symfile_objfile
->ei
.main_func_lowpc
<= pc
127 && symfile_objfile
->ei
.main_func_highpc
> pc
);
130 /* Test whether THIS_FRAME is inside the process entry point function. */
133 inside_entry_func (struct frame_info
*this_frame
)
135 return (get_frame_func (this_frame
) == entry_point_address ());
138 /* Return the innermost lexical block in execution
139 in a specified stack frame. The frame address is assumed valid.
141 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
142 address we used to choose the block. We use this to find a source
143 line, to decide which macro definitions are in scope.
145 The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
146 PC, and may not really be a valid PC at all. For example, in the
147 caller of a function declared to never return, the code at the
148 return address will never be reached, so the call instruction may
149 be the very last instruction in the block. So the address we use
150 to choose the block is actually one byte before the return address
151 --- hopefully pointing us at the call instruction, or its delay
155 get_frame_block (struct frame_info
*frame
, CORE_ADDR
*addr_in_block
)
157 const CORE_ADDR pc
= get_frame_address_in_block (frame
);
162 return block_for_pc (pc
);
166 get_pc_function_start (CORE_ADDR pc
)
169 struct minimal_symbol
*msymbol
;
171 bl
= block_for_pc (pc
);
174 struct symbol
*symbol
= block_function (bl
);
178 bl
= SYMBOL_BLOCK_VALUE (symbol
);
179 return BLOCK_START (bl
);
183 msymbol
= lookup_minimal_symbol_by_pc (pc
);
186 CORE_ADDR fstart
= SYMBOL_VALUE_ADDRESS (msymbol
);
188 if (find_pc_section (fstart
))
195 /* Return the symbol for the function executing in frame FRAME. */
198 get_frame_function (struct frame_info
*frame
)
200 struct block
*bl
= get_frame_block (frame
, 0);
203 return block_function (bl
);
207 /* Return the function containing pc value PC in section SECTION.
208 Returns 0 if function is not known. */
211 find_pc_sect_function (CORE_ADDR pc
, struct bfd_section
*section
)
213 struct block
*b
= block_for_pc_sect (pc
, section
);
216 return block_function (b
);
219 /* Return the function containing pc value PC.
220 Returns 0 if function is not known. Backward compatibility, no section */
223 find_pc_function (CORE_ADDR pc
)
225 return find_pc_sect_function (pc
, find_pc_mapped_section (pc
));
228 /* These variables are used to cache the most recent result
229 * of find_pc_partial_function. */
231 static CORE_ADDR cache_pc_function_low
= 0;
232 static CORE_ADDR cache_pc_function_high
= 0;
233 static char *cache_pc_function_name
= 0;
234 static struct bfd_section
*cache_pc_function_section
= NULL
;
236 /* Clear cache, e.g. when symbol table is discarded. */
239 clear_pc_function_cache (void)
241 cache_pc_function_low
= 0;
242 cache_pc_function_high
= 0;
243 cache_pc_function_name
= (char *) 0;
244 cache_pc_function_section
= NULL
;
247 /* Finds the "function" (text symbol) that is smaller than PC but
248 greatest of all of the potential text symbols in SECTION. Sets
249 *NAME and/or *ADDRESS conditionally if that pointer is non-null.
250 If ENDADDR is non-null, then set *ENDADDR to be the end of the
251 function (exclusive), but passing ENDADDR as non-null means that
252 the function might cause symbols to be read. This function either
253 succeeds or fails (not halfway succeeds). If it succeeds, it sets
254 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
255 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
258 /* Backward compatibility, no section argument. */
261 find_pc_partial_function (CORE_ADDR pc
, char **name
, CORE_ADDR
*address
,
264 struct bfd_section
*section
;
265 struct partial_symtab
*pst
;
267 struct minimal_symbol
*msymbol
;
268 struct partial_symbol
*psb
;
269 struct obj_section
*osect
;
273 /* To ensure that the symbol returned belongs to the correct setion
274 (and that the last [random] symbol from the previous section
275 isn't returned) try to find the section containing PC. First try
276 the overlay code (which by default returns NULL); and second try
277 the normal section code (which almost always succeeds). */
278 section
= find_pc_overlay (pc
);
281 struct obj_section
*obj_section
= find_pc_section (pc
);
282 if (obj_section
== NULL
)
285 section
= obj_section
->the_bfd_section
;
288 mapped_pc
= overlay_mapped_address (pc
, section
);
290 if (mapped_pc
>= cache_pc_function_low
291 && mapped_pc
< cache_pc_function_high
292 && section
== cache_pc_function_section
)
293 goto return_cached_value
;
295 msymbol
= lookup_minimal_symbol_by_pc_section (mapped_pc
, section
);
296 pst
= find_pc_sect_psymtab (mapped_pc
, section
);
299 /* Need to read the symbols to get a good value for the end address. */
300 if (endaddr
!= NULL
&& !pst
->readin
)
302 /* Need to get the terminal in case symbol-reading produces
304 target_terminal_ours_for_output ();
305 PSYMTAB_TO_SYMTAB (pst
);
310 /* Checking whether the msymbol has a larger value is for the
311 "pathological" case mentioned in print_frame_info. */
312 f
= find_pc_sect_function (mapped_pc
, section
);
315 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f
))
316 >= SYMBOL_VALUE_ADDRESS (msymbol
))))
318 cache_pc_function_low
= BLOCK_START (SYMBOL_BLOCK_VALUE (f
));
319 cache_pc_function_high
= BLOCK_END (SYMBOL_BLOCK_VALUE (f
));
320 cache_pc_function_name
= DEPRECATED_SYMBOL_NAME (f
);
321 cache_pc_function_section
= section
;
322 goto return_cached_value
;
327 /* Now that static symbols go in the minimal symbol table, perhaps
328 we could just ignore the partial symbols. But at least for now
329 we use the partial or minimal symbol, whichever is larger. */
330 psb
= find_pc_sect_psymbol (pst
, mapped_pc
, section
);
333 && (msymbol
== NULL
||
334 (SYMBOL_VALUE_ADDRESS (psb
)
335 >= SYMBOL_VALUE_ADDRESS (msymbol
))))
337 /* This case isn't being cached currently. */
339 *address
= SYMBOL_VALUE_ADDRESS (psb
);
341 *name
= DEPRECATED_SYMBOL_NAME (psb
);
342 /* endaddr non-NULL can't happen here. */
348 /* Not in the normal symbol tables, see if the pc is in a known section.
349 If it's not, then give up. This ensures that anything beyond the end
350 of the text seg doesn't appear to be part of the last function in the
353 osect
= find_pc_sect_section (mapped_pc
, section
);
358 /* Must be in the minimal symbol table. */
361 /* No available symbol. */
371 cache_pc_function_low
= SYMBOL_VALUE_ADDRESS (msymbol
);
372 cache_pc_function_name
= DEPRECATED_SYMBOL_NAME (msymbol
);
373 cache_pc_function_section
= section
;
375 /* Use the lesser of the next minimal symbol in the same section, or
376 the end of the section, as the end of the function. */
378 /* Step over other symbols at this same address, and symbols in
379 other sections, to find the next symbol in this section with
380 a different address. */
382 for (i
= 1; DEPRECATED_SYMBOL_NAME (msymbol
+ i
) != NULL
; i
++)
384 if (SYMBOL_VALUE_ADDRESS (msymbol
+ i
) != SYMBOL_VALUE_ADDRESS (msymbol
)
385 && SYMBOL_BFD_SECTION (msymbol
+ i
) == SYMBOL_BFD_SECTION (msymbol
))
389 if (DEPRECATED_SYMBOL_NAME (msymbol
+ i
) != NULL
390 && SYMBOL_VALUE_ADDRESS (msymbol
+ i
) < osect
->endaddr
)
391 cache_pc_function_high
= SYMBOL_VALUE_ADDRESS (msymbol
+ i
);
393 /* We got the start address from the last msymbol in the objfile.
394 So the end address is the end of the section. */
395 cache_pc_function_high
= osect
->endaddr
;
401 if (pc_in_unmapped_range (pc
, section
))
402 *address
= overlay_unmapped_address (cache_pc_function_low
, section
);
404 *address
= cache_pc_function_low
;
408 *name
= cache_pc_function_name
;
412 if (pc_in_unmapped_range (pc
, section
))
414 /* Because the high address is actually beyond the end of
415 the function (and therefore possibly beyond the end of
416 the overlay), we must actually convert (high - 1) and
417 then add one to that. */
419 *endaddr
= 1 + overlay_unmapped_address (cache_pc_function_high
- 1,
423 *endaddr
= cache_pc_function_high
;
429 /* Return the innermost stack frame executing inside of BLOCK,
430 or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */
433 block_innermost_frame (struct block
*block
)
435 struct frame_info
*frame
;
438 CORE_ADDR calling_pc
;
443 start
= BLOCK_START (block
);
444 end
= BLOCK_END (block
);
449 frame
= get_prev_frame (frame
);
452 calling_pc
= get_frame_address_in_block (frame
);
453 if (calling_pc
>= start
&& calling_pc
< end
)