1 /* Get info from stack frames; convert between frames, blocks,
2 functions and pc values.
4 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
5 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009,
6 2010, 2011 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 3 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, see <http://www.gnu.org/licenses/>. */
34 #include "gdb_assert.h"
35 #include "dummy-frame.h"
39 #include "inline-frame.h"
42 /* Return the innermost lexical block in execution in a specified
43 stack frame. The frame address is assumed valid.
45 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
46 address we used to choose the block. We use this to find a source
47 line, to decide which macro definitions are in scope.
49 The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
50 PC, and may not really be a valid PC at all. For example, in the
51 caller of a function declared to never return, the code at the
52 return address will never be reached, so the call instruction may
53 be the very last instruction in the block. So the address we use
54 to choose the block is actually one byte before the return address
55 --- hopefully pointing us at the call instruction, or its delay
59 get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
61 const CORE_ADDR pc = get_frame_address_in_block (frame);
68 bl = block_for_pc (pc);
72 inline_count = frame_inlined_callees (frame);
74 while (inline_count > 0)
76 if (block_inlined_p (bl))
79 bl = BLOCK_SUPERBLOCK (bl);
80 gdb_assert (bl != NULL);
87 get_pc_function_start (CORE_ADDR pc)
90 struct minimal_symbol *msymbol;
92 bl = block_for_pc (pc);
95 struct symbol *symbol = block_linkage_function (bl);
99 bl = SYMBOL_BLOCK_VALUE (symbol);
100 return BLOCK_START (bl);
104 msymbol = lookup_minimal_symbol_by_pc (pc);
107 CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol);
109 if (find_pc_section (fstart))
116 /* Return the symbol for the function executing in frame FRAME. */
119 get_frame_function (struct frame_info *frame)
121 struct block *bl = get_frame_block (frame, 0);
126 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
127 bl = BLOCK_SUPERBLOCK (bl);
129 return BLOCK_FUNCTION (bl);
133 /* Return the function containing pc value PC in section SECTION.
134 Returns 0 if function is not known. */
137 find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
139 struct block *b = block_for_pc_sect (pc, section);
143 return block_linkage_function (b);
146 /* Return the function containing pc value PC.
147 Returns 0 if function is not known.
148 Backward compatibility, no section */
151 find_pc_function (CORE_ADDR pc)
153 return find_pc_sect_function (pc, find_pc_mapped_section (pc));
156 /* These variables are used to cache the most recent result
157 of find_pc_partial_function. */
159 static CORE_ADDR cache_pc_function_low = 0;
160 static CORE_ADDR cache_pc_function_high = 0;
161 static char *cache_pc_function_name = 0;
162 static struct obj_section *cache_pc_function_section = NULL;
164 /* Clear cache, e.g. when symbol table is discarded. */
167 clear_pc_function_cache (void)
169 cache_pc_function_low = 0;
170 cache_pc_function_high = 0;
171 cache_pc_function_name = (char *) 0;
172 cache_pc_function_section = NULL;
175 /* Finds the "function" (text symbol) that is smaller than PC but
176 greatest of all of the potential text symbols in SECTION. Sets
177 *NAME and/or *ADDRESS conditionally if that pointer is non-null.
178 If ENDADDR is non-null, then set *ENDADDR to be the end of the
179 function (exclusive), but passing ENDADDR as non-null means that
180 the function might cause symbols to be read. This function either
181 succeeds or fails (not halfway succeeds). If it succeeds, it sets
182 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
183 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
186 /* Backward compatibility, no section argument. */
189 find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address,
192 struct obj_section *section;
194 struct minimal_symbol *msymbol;
195 struct symtab *symtab = NULL;
196 struct objfile *objfile;
200 /* To ensure that the symbol returned belongs to the correct setion
201 (and that the last [random] symbol from the previous section
202 isn't returned) try to find the section containing PC. First try
203 the overlay code (which by default returns NULL); and second try
204 the normal section code (which almost always succeeds). */
205 section = find_pc_overlay (pc);
207 section = find_pc_section (pc);
209 mapped_pc = overlay_mapped_address (pc, section);
211 if (mapped_pc >= cache_pc_function_low
212 && mapped_pc < cache_pc_function_high
213 && section == cache_pc_function_section)
214 goto return_cached_value;
216 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
217 ALL_OBJFILES (objfile)
220 symtab = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol,
221 mapped_pc, section, 0);
228 /* Checking whether the msymbol has a larger value is for the
229 "pathological" case mentioned in print_frame_info. */
230 f = find_pc_sect_function (mapped_pc, section);
233 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
234 >= SYMBOL_VALUE_ADDRESS (msymbol))))
236 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
237 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
238 cache_pc_function_name = SYMBOL_LINKAGE_NAME (f);
239 cache_pc_function_section = section;
240 goto return_cached_value;
244 /* Not in the normal symbol tables, see if the pc is in a known
245 section. If it's not, then give up. This ensures that anything
246 beyond the end of the text seg doesn't appear to be part of the
247 last function in the text segment. */
252 /* Must be in the minimal symbol table. */
255 /* No available symbol. */
265 cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
266 cache_pc_function_name = SYMBOL_LINKAGE_NAME (msymbol);
267 cache_pc_function_section = section;
269 /* If the minimal symbol has a size, use it for the cache.
270 Otherwise use the lesser of the next minimal symbol in the same
271 section, or the end of the section, as the end of the
274 if (MSYMBOL_SIZE (msymbol) != 0)
275 cache_pc_function_high = cache_pc_function_low + MSYMBOL_SIZE (msymbol);
278 /* Step over other symbols at this same address, and symbols in
279 other sections, to find the next symbol in this section with
280 a different address. */
282 for (i = 1; SYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++)
284 if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol)
285 && SYMBOL_OBJ_SECTION (msymbol + i) == SYMBOL_OBJ_SECTION (msymbol))
289 if (SYMBOL_LINKAGE_NAME (msymbol + i) != NULL
290 && SYMBOL_VALUE_ADDRESS (msymbol + i) < obj_section_endaddr (section))
291 cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
293 /* We got the start address from the last msymbol in the objfile.
294 So the end address is the end of the section. */
295 cache_pc_function_high = obj_section_endaddr (section);
302 if (pc_in_unmapped_range (pc, section))
303 *address = overlay_unmapped_address (cache_pc_function_low, section);
305 *address = cache_pc_function_low;
309 *name = cache_pc_function_name;
313 if (pc_in_unmapped_range (pc, section))
315 /* Because the high address is actually beyond the end of
316 the function (and therefore possibly beyond the end of
317 the overlay), we must actually convert (high - 1) and
318 then add one to that. */
320 *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
324 *endaddr = cache_pc_function_high;
330 /* Return the innermost stack frame executing inside of BLOCK, or NULL
331 if there is no such frame. If BLOCK is NULL, just return NULL. */
334 block_innermost_frame (struct block *block)
336 struct frame_info *frame;
343 start = BLOCK_START (block);
344 end = BLOCK_END (block);
346 frame = get_current_frame ();
347 while (frame != NULL)
349 struct block *frame_block = get_frame_block (frame, NULL);
350 if (frame_block != NULL && contained_in (frame_block, block))
353 frame = get_prev_frame (frame);