1 /* Get info from stack frames; convert between frames, blocks,
2 functions and pc values.
4 Copyright (C) 1986-2014 Free Software Foundation, Inc.
6 This file is part of GDB.
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 #include "gdb_assert.h"
33 #include "dummy-frame.h"
37 #include "inline-frame.h"
39 /* Return the innermost lexical block in execution in a specified
40 stack frame. The frame address is assumed valid.
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.
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
56 get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
62 if (!get_frame_address_in_block_if_available (frame, &pc))
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 bound_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.minsym);
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 const char *cache_pc_function_name = 0;
162 static struct obj_section *cache_pc_function_section = NULL;
163 static int cache_pc_function_is_gnu_ifunc = 0;
165 /* Clear cache, e.g. when symbol table is discarded. */
168 clear_pc_function_cache (void)
170 cache_pc_function_low = 0;
171 cache_pc_function_high = 0;
172 cache_pc_function_name = (char *) 0;
173 cache_pc_function_section = NULL;
174 cache_pc_function_is_gnu_ifunc = 0;
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. 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. */
189 /* Backward compatibility, no section argument. */
192 find_pc_partial_function_gnu_ifunc (CORE_ADDR pc, const char **name,
193 CORE_ADDR *address, CORE_ADDR *endaddr,
196 struct obj_section *section;
198 struct minimal_symbol *msymbol;
199 struct symtab *symtab = NULL;
200 struct objfile *objfile;
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);
211 section = find_pc_section (pc);
213 mapped_pc = overlay_mapped_address (pc, section);
215 if (mapped_pc >= cache_pc_function_low
216 && mapped_pc < cache_pc_function_high
217 && section == cache_pc_function_section)
218 goto return_cached_value;
220 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section).minsym;
221 ALL_OBJFILES (objfile)
224 symtab = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol,
225 mapped_pc, section, 0);
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);
237 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
238 >= SYMBOL_VALUE_ADDRESS (msymbol))))
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;
244 cache_pc_function_is_gnu_ifunc = TYPE_GNU_IFUNC (SYMBOL_TYPE (f));
245 goto return_cached_value;
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. */
257 /* Must be in the minimal symbol table. */
260 /* No available symbol. */
267 if (is_gnu_ifunc_p != NULL)
272 cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
273 cache_pc_function_name = SYMBOL_LINKAGE_NAME (msymbol);
274 cache_pc_function_section = section;
275 cache_pc_function_is_gnu_ifunc = MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc;
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
282 if (MSYMBOL_SIZE (msymbol) != 0)
283 cache_pc_function_high = cache_pc_function_low + MSYMBOL_SIZE (msymbol);
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. */
290 for (i = 1; SYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++)
292 if (SYMBOL_VALUE_ADDRESS (msymbol + i)
293 != SYMBOL_VALUE_ADDRESS (msymbol)
294 && SYMBOL_SECTION (msymbol + i)
295 == SYMBOL_SECTION (msymbol))
299 if (SYMBOL_LINKAGE_NAME (msymbol + i) != NULL
300 && SYMBOL_VALUE_ADDRESS (msymbol + i)
301 < obj_section_endaddr (section))
302 cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
304 /* We got the start address from the last msymbol in the objfile.
305 So the end address is the end of the section. */
306 cache_pc_function_high = obj_section_endaddr (section);
313 if (pc_in_unmapped_range (pc, section))
314 *address = overlay_unmapped_address (cache_pc_function_low, section);
316 *address = cache_pc_function_low;
320 *name = cache_pc_function_name;
324 if (pc_in_unmapped_range (pc, section))
326 /* Because the high address is actually beyond the end of
327 the function (and therefore possibly beyond the end of
328 the overlay), we must actually convert (high - 1) and
329 then add one to that. */
331 *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
335 *endaddr = cache_pc_function_high;
339 *is_gnu_ifunc_p = cache_pc_function_is_gnu_ifunc;
344 /* See find_pc_partial_function_gnu_ifunc, only the IS_GNU_IFUNC_P parameter
345 is omitted here for backward API compatibility. */
348 find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
351 return find_pc_partial_function_gnu_ifunc (pc, name, address, endaddr, NULL);
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. */
359 block_innermost_frame (const struct block *block)
361 struct frame_info *frame;
366 frame = get_selected_frame_if_set ();
368 frame = get_current_frame ();
369 while (frame != NULL)
371 struct block *frame_block = get_frame_block (frame, NULL);
372 if (frame_block != NULL && contained_in (frame_block, block))
375 frame = get_prev_frame (frame);