gdb - Local mods (compile)
[dragonfly.git] / contrib / gdb-7 / gdb / blockframe.c
CommitLineData
5796c8dc
SS
1/* Get info from stack frames; convert between frames, blocks,
2 functions and pc values.
3
25e4902b 4 Copyright (C) 1986-2015 Free Software Foundation, Inc.
5796c8dc
SS
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21#include "defs.h"
22#include "symtab.h"
23#include "bfd.h"
24#include "objfiles.h"
25#include "frame.h"
26#include "gdbcore.h"
27#include "value.h"
28#include "target.h"
29#include "inferior.h"
30#include "annotate.h"
31#include "regcache.h"
5796c8dc
SS
32#include "dummy-frame.h"
33#include "command.h"
34#include "gdbcmd.h"
35#include "block.h"
36#include "inline-frame.h"
5796c8dc 37
c50c785c
JM
38/* Return the innermost lexical block in execution in a specified
39 stack frame. The frame address is assumed valid.
5796c8dc
SS
40
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.
44
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
52 slot instruction. */
53
25e4902b 54const struct block *
5796c8dc
SS
55get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
56{
c50c785c 57 CORE_ADDR pc;
25e4902b 58 const struct block *bl;
5796c8dc
SS
59 int inline_count;
60
c50c785c
JM
61 if (!get_frame_address_in_block_if_available (frame, &pc))
62 return NULL;
63
5796c8dc
SS
64 if (addr_in_block)
65 *addr_in_block = pc;
66
67 bl = block_for_pc (pc);
68 if (bl == NULL)
69 return NULL;
70
71 inline_count = frame_inlined_callees (frame);
72
73 while (inline_count > 0)
74 {
75 if (block_inlined_p (bl))
76 inline_count--;
77
78 bl = BLOCK_SUPERBLOCK (bl);
79 gdb_assert (bl != NULL);
80 }
81
82 return bl;
83}
84
85CORE_ADDR
86get_pc_function_start (CORE_ADDR pc)
87{
25e4902b
AHJ
88 const struct block *bl;
89 struct bound_minimal_symbol msymbol;
5796c8dc
SS
90
91 bl = block_for_pc (pc);
92 if (bl)
93 {
94 struct symbol *symbol = block_linkage_function (bl);
95
96 if (symbol)
97 {
98 bl = SYMBOL_BLOCK_VALUE (symbol);
99 return BLOCK_START (bl);
100 }
101 }
102
103 msymbol = lookup_minimal_symbol_by_pc (pc);
25e4902b 104 if (msymbol.minsym)
5796c8dc 105 {
25e4902b 106 CORE_ADDR fstart = BMSYMBOL_VALUE_ADDRESS (msymbol);
5796c8dc
SS
107
108 if (find_pc_section (fstart))
109 return fstart;
110 }
111
112 return 0;
113}
114
115/* Return the symbol for the function executing in frame FRAME. */
116
117struct symbol *
118get_frame_function (struct frame_info *frame)
119{
25e4902b 120 const struct block *bl = get_frame_block (frame, 0);
5796c8dc
SS
121
122 if (bl == NULL)
123 return NULL;
124
125 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
126 bl = BLOCK_SUPERBLOCK (bl);
127
128 return BLOCK_FUNCTION (bl);
129}
130\f
131
132/* Return the function containing pc value PC in section SECTION.
133 Returns 0 if function is not known. */
134
135struct symbol *
136find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
137{
25e4902b 138 const struct block *b = block_for_pc_sect (pc, section);
cf7f2e2d 139
5796c8dc
SS
140 if (b == 0)
141 return 0;
142 return block_linkage_function (b);
143}
144
145/* Return the function containing pc value PC.
c50c785c
JM
146 Returns 0 if function is not known.
147 Backward compatibility, no section */
5796c8dc
SS
148
149struct symbol *
150find_pc_function (CORE_ADDR pc)
151{
152 return find_pc_sect_function (pc, find_pc_mapped_section (pc));
153}
154
155/* These variables are used to cache the most recent result
c50c785c 156 of find_pc_partial_function. */
5796c8dc
SS
157
158static CORE_ADDR cache_pc_function_low = 0;
159static CORE_ADDR cache_pc_function_high = 0;
ef5ccd6c 160static const char *cache_pc_function_name = 0;
5796c8dc 161static struct obj_section *cache_pc_function_section = NULL;
c50c785c 162static int cache_pc_function_is_gnu_ifunc = 0;
5796c8dc 163
c50c785c 164/* Clear cache, e.g. when symbol table is discarded. */
5796c8dc
SS
165
166void
167clear_pc_function_cache (void)
168{
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;
c50c785c 173 cache_pc_function_is_gnu_ifunc = 0;
5796c8dc
SS
174}
175
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
c50c785c
JM
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. */
5796c8dc
SS
187
188/* Backward compatibility, no section argument. */
189
190int
ef5ccd6c 191find_pc_partial_function_gnu_ifunc (CORE_ADDR pc, const char **name,
c50c785c
JM
192 CORE_ADDR *address, CORE_ADDR *endaddr,
193 int *is_gnu_ifunc_p)
5796c8dc
SS
194{
195 struct obj_section *section;
5796c8dc 196 struct symbol *f;
25e4902b
AHJ
197 struct bound_minimal_symbol msymbol;
198 struct compunit_symtab *compunit_symtab = NULL;
cf7f2e2d 199 struct objfile *objfile;
5796c8dc
SS
200 int i;
201 CORE_ADDR mapped_pc;
202
203 /* To ensure that the symbol returned belongs to the correct setion
204 (and that the last [random] symbol from the previous section
205 isn't returned) try to find the section containing PC. First try
206 the overlay code (which by default returns NULL); and second try
207 the normal section code (which almost always succeeds). */
208 section = find_pc_overlay (pc);
209 if (section == NULL)
210 section = find_pc_section (pc);
211
212 mapped_pc = overlay_mapped_address (pc, section);
213
214 if (mapped_pc >= cache_pc_function_low
215 && mapped_pc < cache_pc_function_high
216 && section == cache_pc_function_section)
217 goto return_cached_value;
218
219 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
cf7f2e2d
JM
220 ALL_OBJFILES (objfile)
221 {
222 if (objfile->sf)
25e4902b
AHJ
223 {
224 compunit_symtab
225 = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
226 mapped_pc, section,
227 0);
228 }
229 if (compunit_symtab != NULL)
cf7f2e2d
JM
230 break;
231 }
232
25e4902b 233 if (compunit_symtab != NULL)
5796c8dc 234 {
cf7f2e2d
JM
235 /* Checking whether the msymbol has a larger value is for the
236 "pathological" case mentioned in print_frame_info. */
237 f = find_pc_sect_function (mapped_pc, section);
238 if (f != NULL
25e4902b 239 && (msymbol.minsym == NULL
cf7f2e2d 240 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
25e4902b 241 >= BMSYMBOL_VALUE_ADDRESS (msymbol))))
5796c8dc 242 {
cf7f2e2d
JM
243 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
244 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
245 cache_pc_function_name = SYMBOL_LINKAGE_NAME (f);
246 cache_pc_function_section = section;
c50c785c 247 cache_pc_function_is_gnu_ifunc = TYPE_GNU_IFUNC (SYMBOL_TYPE (f));
cf7f2e2d 248 goto return_cached_value;
5796c8dc
SS
249 }
250 }
251
c50c785c
JM
252 /* Not in the normal symbol tables, see if the pc is in a known
253 section. If it's not, then give up. This ensures that anything
254 beyond the end of the text seg doesn't appear to be part of the
255 last function in the text segment. */
5796c8dc
SS
256
257 if (!section)
25e4902b 258 msymbol.minsym = NULL;
5796c8dc
SS
259
260 /* Must be in the minimal symbol table. */
25e4902b 261 if (msymbol.minsym == NULL)
5796c8dc
SS
262 {
263 /* No available symbol. */
264 if (name != NULL)
265 *name = 0;
266 if (address != NULL)
267 *address = 0;
268 if (endaddr != NULL)
269 *endaddr = 0;
c50c785c
JM
270 if (is_gnu_ifunc_p != NULL)
271 *is_gnu_ifunc_p = 0;
5796c8dc
SS
272 return 0;
273 }
274
25e4902b
AHJ
275 cache_pc_function_low = BMSYMBOL_VALUE_ADDRESS (msymbol);
276 cache_pc_function_name = MSYMBOL_LINKAGE_NAME (msymbol.minsym);
5796c8dc 277 cache_pc_function_section = section;
25e4902b
AHJ
278 cache_pc_function_is_gnu_ifunc = (MSYMBOL_TYPE (msymbol.minsym)
279 == mst_text_gnu_ifunc);
280 cache_pc_function_high = minimal_symbol_upper_bound (msymbol);
5796c8dc
SS
281
282 return_cached_value:
283
284 if (address)
285 {
286 if (pc_in_unmapped_range (pc, section))
287 *address = overlay_unmapped_address (cache_pc_function_low, section);
288 else
289 *address = cache_pc_function_low;
290 }
291
292 if (name)
293 *name = cache_pc_function_name;
294
295 if (endaddr)
296 {
297 if (pc_in_unmapped_range (pc, section))
298 {
299 /* Because the high address is actually beyond the end of
300 the function (and therefore possibly beyond the end of
301 the overlay), we must actually convert (high - 1) and
c50c785c 302 then add one to that. */
5796c8dc
SS
303
304 *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
305 section);
306 }
307 else
308 *endaddr = cache_pc_function_high;
309 }
310
c50c785c
JM
311 if (is_gnu_ifunc_p)
312 *is_gnu_ifunc_p = cache_pc_function_is_gnu_ifunc;
313
5796c8dc
SS
314 return 1;
315}
316
c50c785c
JM
317/* See find_pc_partial_function_gnu_ifunc, only the IS_GNU_IFUNC_P parameter
318 is omitted here for backward API compatibility. */
319
320int
ef5ccd6c 321find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
c50c785c
JM
322 CORE_ADDR *endaddr)
323{
324 return find_pc_partial_function_gnu_ifunc (pc, name, address, endaddr, NULL);
325}
326
ef5ccd6c
JM
327/* Return the innermost stack frame that is executing inside of BLOCK and is
328 at least as old as the selected frame. Return NULL if there is no
329 such frame. If BLOCK is NULL, just return NULL. */
5796c8dc
SS
330
331struct frame_info *
a45ae5f8 332block_innermost_frame (const struct block *block)
5796c8dc
SS
333{
334 struct frame_info *frame;
5796c8dc
SS
335
336 if (block == NULL)
337 return NULL;
338
ef5ccd6c
JM
339 frame = get_selected_frame_if_set ();
340 if (frame == NULL)
341 frame = get_current_frame ();
5796c8dc
SS
342 while (frame != NULL)
343 {
25e4902b 344 const struct block *frame_block = get_frame_block (frame, NULL);
5796c8dc
SS
345 if (frame_block != NULL && contained_in (frame_block, block))
346 return frame;
347
348 frame = get_prev_frame (frame);
349 }
350
351 return NULL;
352}