1 /* Get info from stack frames; convert between frames, blocks,
2 functions and pc values.
4 Copyright (C) 1986-2018 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 "dummy-frame.h"
36 #include "inline-frame.h"
38 /* Return the innermost lexical block in execution in a specified
39 stack frame. The frame address is assumed valid.
41 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
42 address we used to choose the block. We use this to find a source
43 line, to decide which macro definitions are in scope.
45 The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
46 PC, and may not really be a valid PC at all. For example, in the
47 caller of a function declared to never return, the code at the
48 return address will never be reached, so the call instruction may
49 be the very last instruction in the block. So the address we use
50 to choose the block is actually one byte before the return address
51 --- hopefully pointing us at the call instruction, or its delay
55 get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
58 const struct block *bl;
61 if (!get_frame_address_in_block_if_available (frame, &pc))
67 bl = block_for_pc (pc);
71 inline_count = frame_inlined_callees (frame);
73 while (inline_count > 0)
75 if (block_inlined_p (bl))
78 bl = BLOCK_SUPERBLOCK (bl);
79 gdb_assert (bl != NULL);
86 get_pc_function_start (CORE_ADDR pc)
88 const struct block *bl;
89 struct bound_minimal_symbol msymbol;
91 bl = block_for_pc (pc);
94 struct symbol *symbol = block_linkage_function (bl);
98 bl = SYMBOL_BLOCK_VALUE (symbol);
99 return BLOCK_START (bl);
103 msymbol = lookup_minimal_symbol_by_pc (pc);
106 CORE_ADDR fstart = BMSYMBOL_VALUE_ADDRESS (msymbol);
108 if (find_pc_section (fstart))
115 /* Return the symbol for the function executing in frame FRAME. */
118 get_frame_function (struct frame_info *frame)
120 const struct block *bl = get_frame_block (frame, 0);
125 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
126 bl = BLOCK_SUPERBLOCK (bl);
128 return BLOCK_FUNCTION (bl);
132 /* Return the function containing pc value PC in section SECTION.
133 Returns 0 if function is not known. */
136 find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
138 const struct block *b = block_for_pc_sect (pc, section);
142 return block_linkage_function (b);
145 /* Return the function containing pc value PC.
146 Returns 0 if function is not known.
147 Backward compatibility, no section */
150 find_pc_function (CORE_ADDR pc)
152 return find_pc_sect_function (pc, find_pc_mapped_section (pc));
155 /* These variables are used to cache the most recent result
156 of find_pc_partial_function. */
158 static CORE_ADDR cache_pc_function_low = 0;
159 static CORE_ADDR cache_pc_function_high = 0;
160 static const char *cache_pc_function_name = 0;
161 static struct obj_section *cache_pc_function_section = NULL;
162 static int cache_pc_function_is_gnu_ifunc = 0;
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;
173 cache_pc_function_is_gnu_ifunc = 0;
176 /* Finds the "function" (text symbol) that is smaller than PC but
177 greatest of all of the potential text symbols in SECTION. Sets
178 *NAME and/or *ADDRESS conditionally if that pointer is non-null.
179 If ENDADDR is non-null, then set *ENDADDR to be the end of the
180 function (exclusive), but passing ENDADDR as non-null means that
181 the function might cause symbols to be read. If IS_GNU_IFUNC_P is provided
182 *IS_GNU_IFUNC_P is set to 1 on return if the function is STT_GNU_IFUNC.
183 This function either succeeds or fails (not halfway succeeds). If it
184 succeeds, it sets *NAME, *ADDRESS, and *ENDADDR to real information and
185 returns 1. If it fails, it sets *NAME, *ADDRESS, *ENDADDR and
186 *IS_GNU_IFUNC_P to zero and returns 0. */
188 /* Backward compatibility, no section argument. */
191 find_pc_partial_function_gnu_ifunc (CORE_ADDR pc, const char **name,
192 CORE_ADDR *address, CORE_ADDR *endaddr,
195 struct obj_section *section;
197 struct bound_minimal_symbol msymbol;
198 struct compunit_symtab *compunit_symtab = NULL;
199 struct objfile *objfile;
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);
209 section = find_pc_section (pc);
211 mapped_pc = overlay_mapped_address (pc, section);
213 if (mapped_pc >= cache_pc_function_low
214 && mapped_pc < cache_pc_function_high
215 && section == cache_pc_function_section)
216 goto return_cached_value;
218 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
219 ALL_OBJFILES (objfile)
224 = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
228 if (compunit_symtab != NULL)
232 if (compunit_symtab != NULL)
234 /* Checking whether the msymbol has a larger value is for the
235 "pathological" case mentioned in print_frame_info. */
236 f = find_pc_sect_function (mapped_pc, section);
238 && (msymbol.minsym == NULL
239 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
240 >= BMSYMBOL_VALUE_ADDRESS (msymbol))))
242 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
243 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
244 cache_pc_function_name = SYMBOL_LINKAGE_NAME (f);
245 cache_pc_function_section = section;
246 cache_pc_function_is_gnu_ifunc = TYPE_GNU_IFUNC (SYMBOL_TYPE (f));
247 goto return_cached_value;
251 /* Not in the normal symbol tables, see if the pc is in a known
252 section. If it's not, then give up. This ensures that anything
253 beyond the end of the text seg doesn't appear to be part of the
254 last function in the text segment. */
257 msymbol.minsym = NULL;
259 /* Must be in the minimal symbol table. */
260 if (msymbol.minsym == NULL)
262 /* No available symbol. */
269 if (is_gnu_ifunc_p != NULL)
274 cache_pc_function_low = BMSYMBOL_VALUE_ADDRESS (msymbol);
275 cache_pc_function_name = MSYMBOL_LINKAGE_NAME (msymbol.minsym);
276 cache_pc_function_section = section;
277 cache_pc_function_is_gnu_ifunc = (MSYMBOL_TYPE (msymbol.minsym)
278 == mst_text_gnu_ifunc);
279 cache_pc_function_high = minimal_symbol_upper_bound (msymbol);
285 if (pc_in_unmapped_range (pc, section))
286 *address = overlay_unmapped_address (cache_pc_function_low, section);
288 *address = cache_pc_function_low;
292 *name = cache_pc_function_name;
296 if (pc_in_unmapped_range (pc, section))
298 /* Because the high address is actually beyond the end of
299 the function (and therefore possibly beyond the end of
300 the overlay), we must actually convert (high - 1) and
301 then add one to that. */
303 *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
307 *endaddr = cache_pc_function_high;
311 *is_gnu_ifunc_p = cache_pc_function_is_gnu_ifunc;
316 /* See find_pc_partial_function_gnu_ifunc, only the IS_GNU_IFUNC_P parameter
317 is omitted here for backward API compatibility. */
320 find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
323 return find_pc_partial_function_gnu_ifunc (pc, name, address, endaddr, NULL);
329 find_function_type (CORE_ADDR pc)
331 struct symbol *sym = find_pc_function (pc);
333 if (sym != NULL && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) == pc)
334 return SYMBOL_TYPE (sym);
342 find_gnu_ifunc_target_type (CORE_ADDR resolver_funaddr)
344 struct type *resolver_type = find_function_type (resolver_funaddr);
345 if (resolver_type != NULL)
347 /* Get the return type of the resolver. */
348 struct type *resolver_ret_type
349 = check_typedef (TYPE_TARGET_TYPE (resolver_type));
351 /* If we found a pointer to function, then the resolved type
352 is the type of the pointed-to function. */
353 if (TYPE_CODE (resolver_ret_type) == TYPE_CODE_PTR)
355 struct type *resolved_type
356 = TYPE_TARGET_TYPE (resolver_ret_type);
357 if (TYPE_CODE (check_typedef (resolved_type)) == TYPE_CODE_FUNC)
358 return resolved_type;
365 /* Return the innermost stack frame that is executing inside of BLOCK and is
366 at least as old as the selected frame. Return NULL if there is no
367 such frame. If BLOCK is NULL, just return NULL. */
370 block_innermost_frame (const struct block *block)
372 struct frame_info *frame;
377 frame = get_selected_frame_if_set ();
379 frame = get_current_frame ();
380 while (frame != NULL)
382 const struct block *frame_block = get_frame_block (frame, NULL);
383 if (frame_block != NULL && contained_in (frame_block, block))
386 frame = get_prev_frame (frame);