gdb/doc/
[deliverable/binutils-gdb.git] / gdb / blockframe.c
CommitLineData
7cc19214
AC
1/* Get info from stack frames; convert between frames, blocks,
2 functions and pc values.
3
6aba47ca 4 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4c38e0a4 5 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009,
7b6bb8da 6 2010, 2011 Free Software Foundation, Inc.
c906108c 7
c5aa993b 8 This file is part of GDB.
c906108c 9
c5aa993b
JM
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
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
c5aa993b 13 (at your option) any later version.
c906108c 14
c5aa993b
JM
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.
c906108c 19
c5aa993b 20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
22
23#include "defs.h"
24#include "symtab.h"
25#include "bfd.h"
c906108c
SS
26#include "objfiles.h"
27#include "frame.h"
28#include "gdbcore.h"
7157eed4
UW
29#include "value.h"
30#include "target.h"
31#include "inferior.h"
c906108c 32#include "annotate.h"
4e052eda 33#include "regcache.h"
4f460812 34#include "gdb_assert.h"
9c1412c1 35#include "dummy-frame.h"
51603483
DJ
36#include "command.h"
37#include "gdbcmd.h"
fe898f56 38#include "block.h"
edb3359d 39#include "inline-frame.h"
ccefe4c4 40#include "psymtab.h"
c906108c 41
4a64f543
MS
42/* Return the innermost lexical block in execution in a specified
43 stack frame. The frame address is assumed valid.
ae767bfb
JB
44
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.
48
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
56 slot instruction. */
c906108c
SS
57
58struct block *
ae767bfb 59get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
c906108c 60{
e3eebbd7 61 CORE_ADDR pc;
edb3359d
DJ
62 struct block *bl;
63 int inline_count;
ae767bfb 64
e3eebbd7
PA
65 if (!get_frame_address_in_block_if_available (frame, &pc))
66 return NULL;
67
ae767bfb
JB
68 if (addr_in_block)
69 *addr_in_block = pc;
70
edb3359d
DJ
71 bl = block_for_pc (pc);
72 if (bl == NULL)
73 return NULL;
74
75 inline_count = frame_inlined_callees (frame);
76
77 while (inline_count > 0)
78 {
79 if (block_inlined_p (bl))
80 inline_count--;
81
82 bl = BLOCK_SUPERBLOCK (bl);
83 gdb_assert (bl != NULL);
84 }
85
86 return bl;
c906108c
SS
87}
88
c906108c 89CORE_ADDR
fba45db2 90get_pc_function_start (CORE_ADDR pc)
c906108c 91{
2cdd89cb
MK
92 struct block *bl;
93 struct minimal_symbol *msymbol;
c906108c 94
2cdd89cb
MK
95 bl = block_for_pc (pc);
96 if (bl)
c906108c 97 {
7f0df278 98 struct symbol *symbol = block_linkage_function (bl);
2cdd89cb
MK
99
100 if (symbol)
101 {
102 bl = SYMBOL_BLOCK_VALUE (symbol);
103 return BLOCK_START (bl);
104 }
c906108c 105 }
2cdd89cb
MK
106
107 msymbol = lookup_minimal_symbol_by_pc (pc);
108 if (msymbol)
c906108c 109 {
2cdd89cb
MK
110 CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol);
111
112 if (find_pc_section (fstart))
113 return fstart;
c906108c 114 }
2cdd89cb
MK
115
116 return 0;
c906108c
SS
117}
118
119/* Return the symbol for the function executing in frame FRAME. */
120
121struct symbol *
fba45db2 122get_frame_function (struct frame_info *frame)
c906108c 123{
52f0bd74 124 struct block *bl = get_frame_block (frame, 0);
edb3359d
DJ
125
126 if (bl == NULL)
127 return NULL;
128
129 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
130 bl = BLOCK_SUPERBLOCK (bl);
131
132 return BLOCK_FUNCTION (bl);
c906108c
SS
133}
134\f
135
c906108c
SS
136/* Return the function containing pc value PC in section SECTION.
137 Returns 0 if function is not known. */
138
139struct symbol *
714835d5 140find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
c906108c 141{
52f0bd74 142 struct block *b = block_for_pc_sect (pc, section);
cc59ec59 143
c906108c
SS
144 if (b == 0)
145 return 0;
7f0df278 146 return block_linkage_function (b);
c906108c
SS
147}
148
149/* Return the function containing pc value PC.
4a64f543
MS
150 Returns 0 if function is not known.
151 Backward compatibility, no section */
c906108c
SS
152
153struct symbol *
fba45db2 154find_pc_function (CORE_ADDR pc)
c906108c
SS
155{
156 return find_pc_sect_function (pc, find_pc_mapped_section (pc));
157}
158
159/* These variables are used to cache the most recent result
4a64f543 160 of find_pc_partial_function. */
c906108c 161
c5aa993b
JM
162static CORE_ADDR cache_pc_function_low = 0;
163static CORE_ADDR cache_pc_function_high = 0;
164static char *cache_pc_function_name = 0;
714835d5 165static struct obj_section *cache_pc_function_section = NULL;
11c81455 166static int cache_pc_function_is_gnu_ifunc = 0;
c906108c 167
4a64f543 168/* Clear cache, e.g. when symbol table is discarded. */
c906108c
SS
169
170void
fba45db2 171clear_pc_function_cache (void)
c906108c
SS
172{
173 cache_pc_function_low = 0;
174 cache_pc_function_high = 0;
c5aa993b 175 cache_pc_function_name = (char *) 0;
c906108c 176 cache_pc_function_section = NULL;
11c81455 177 cache_pc_function_is_gnu_ifunc = 0;
c906108c
SS
178}
179
180/* Finds the "function" (text symbol) that is smaller than PC but
181 greatest of all of the potential text symbols in SECTION. Sets
182 *NAME and/or *ADDRESS conditionally if that pointer is non-null.
183 If ENDADDR is non-null, then set *ENDADDR to be the end of the
184 function (exclusive), but passing ENDADDR as non-null means that
11c81455
JK
185 the function might cause symbols to be read. If IS_GNU_IFUNC_P is provided
186 *IS_GNU_IFUNC_P is set to 1 on return if the function is STT_GNU_IFUNC.
187 This function either succeeds or fails (not halfway succeeds). If it
188 succeeds, it sets *NAME, *ADDRESS, and *ENDADDR to real information and
189 returns 1. If it fails, it sets *NAME, *ADDRESS, *ENDADDR and
190 *IS_GNU_IFUNC_P to zero and returns 0. */
c906108c 191
73912b9b
AC
192/* Backward compatibility, no section argument. */
193
c906108c 194int
11c81455
JK
195find_pc_partial_function_gnu_ifunc (CORE_ADDR pc, char **name,
196 CORE_ADDR *address, CORE_ADDR *endaddr,
197 int *is_gnu_ifunc_p)
c906108c 198{
714835d5 199 struct obj_section *section;
c5aa993b 200 struct symbol *f;
c906108c 201 struct minimal_symbol *msymbol;
ccefe4c4
TT
202 struct symtab *symtab = NULL;
203 struct objfile *objfile;
c906108c
SS
204 int i;
205 CORE_ADDR mapped_pc;
206
73912b9b
AC
207 /* To ensure that the symbol returned belongs to the correct setion
208 (and that the last [random] symbol from the previous section
209 isn't returned) try to find the section containing PC. First try
210 the overlay code (which by default returns NULL); and second try
211 the normal section code (which almost always succeeds). */
212 section = find_pc_overlay (pc);
213 if (section == NULL)
714835d5 214 section = find_pc_section (pc);
73912b9b 215
c906108c
SS
216 mapped_pc = overlay_mapped_address (pc, section);
217
247055de
MK
218 if (mapped_pc >= cache_pc_function_low
219 && mapped_pc < cache_pc_function_high
220 && section == cache_pc_function_section)
c906108c
SS
221 goto return_cached_value;
222
c906108c 223 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
ccefe4c4
TT
224 ALL_OBJFILES (objfile)
225 {
226 if (objfile->sf)
227 symtab = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol,
228 mapped_pc, section, 0);
229 if (symtab)
230 break;
231 }
232
233 if (symtab)
c906108c 234 {
ccefe4c4
TT
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
239 && (msymbol == NULL
240 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
241 >= SYMBOL_VALUE_ADDRESS (msymbol))))
c906108c 242 {
ccefe4c4
TT
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;
11c81455 247 cache_pc_function_is_gnu_ifunc = TYPE_GNU_IFUNC (SYMBOL_TYPE (f));
ccefe4c4 248 goto return_cached_value;
c906108c
SS
249 }
250 }
251
4a64f543
MS
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. */
c906108c 256
714835d5 257 if (!section)
c906108c
SS
258 msymbol = NULL;
259
260 /* Must be in the minimal symbol table. */
261 if (msymbol == NULL)
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;
11c81455
JK
270 if (is_gnu_ifunc_p != NULL)
271 *is_gnu_ifunc_p = 0;
c906108c
SS
272 return 0;
273 }
274
c5aa993b 275 cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
3567439c 276 cache_pc_function_name = SYMBOL_LINKAGE_NAME (msymbol);
c906108c 277 cache_pc_function_section = section;
11c81455 278 cache_pc_function_is_gnu_ifunc = MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc;
c906108c 279
29e8a844
DJ
280 /* If the minimal symbol has a size, use it for the cache.
281 Otherwise use the lesser of the next minimal symbol in the same
282 section, or the end of the section, as the end of the
283 function. */
c5aa993b 284
29e8a844
DJ
285 if (MSYMBOL_SIZE (msymbol) != 0)
286 cache_pc_function_high = cache_pc_function_low + MSYMBOL_SIZE (msymbol);
287 else
c906108c 288 {
29e8a844
DJ
289 /* Step over other symbols at this same address, and symbols in
290 other sections, to find the next symbol in this section with
291 a different address. */
c906108c 292
3567439c 293 for (i = 1; SYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++)
29e8a844 294 {
3e43a32a
MS
295 if (SYMBOL_VALUE_ADDRESS (msymbol + i)
296 != SYMBOL_VALUE_ADDRESS (msymbol)
297 && SYMBOL_OBJ_SECTION (msymbol + i)
298 == SYMBOL_OBJ_SECTION (msymbol))
29e8a844
DJ
299 break;
300 }
301
3567439c 302 if (SYMBOL_LINKAGE_NAME (msymbol + i) != NULL
3e43a32a
MS
303 && SYMBOL_VALUE_ADDRESS (msymbol + i)
304 < obj_section_endaddr (section))
29e8a844
DJ
305 cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
306 else
307 /* We got the start address from the last msymbol in the objfile.
308 So the end address is the end of the section. */
714835d5 309 cache_pc_function_high = obj_section_endaddr (section);
29e8a844 310 }
c906108c 311
247055de 312 return_cached_value:
c906108c
SS
313
314 if (address)
315 {
316 if (pc_in_unmapped_range (pc, section))
c5aa993b 317 *address = overlay_unmapped_address (cache_pc_function_low, section);
c906108c 318 else
c5aa993b 319 *address = cache_pc_function_low;
c906108c 320 }
c5aa993b 321
c906108c
SS
322 if (name)
323 *name = cache_pc_function_name;
324
325 if (endaddr)
326 {
327 if (pc_in_unmapped_range (pc, section))
c5aa993b 328 {
c906108c
SS
329 /* Because the high address is actually beyond the end of
330 the function (and therefore possibly beyond the end of
247055de 331 the overlay), we must actually convert (high - 1) and
4a64f543 332 then add one to that. */
c906108c 333
c5aa993b 334 *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
c906108c 335 section);
c5aa993b 336 }
c906108c 337 else
c5aa993b 338 *endaddr = cache_pc_function_high;
c906108c
SS
339 }
340
11c81455
JK
341 if (is_gnu_ifunc_p)
342 *is_gnu_ifunc_p = cache_pc_function_is_gnu_ifunc;
343
c906108c
SS
344 return 1;
345}
346
11c81455
JK
347/* See find_pc_partial_function_gnu_ifunc, only the IS_GNU_IFUNC_P parameter
348 is omitted here for backward API compatibility. */
349
350int
351find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address,
352 CORE_ADDR *endaddr)
353{
354 return find_pc_partial_function_gnu_ifunc (pc, name, address, endaddr, NULL);
355}
356
4a64f543
MS
357/* Return the innermost stack frame executing inside of BLOCK, or NULL
358 if there is no such frame. If BLOCK is NULL, just return NULL. */
c906108c
SS
359
360struct frame_info *
fba45db2 361block_innermost_frame (struct block *block)
c906108c
SS
362{
363 struct frame_info *frame;
52f0bd74
AC
364 CORE_ADDR start;
365 CORE_ADDR end;
c906108c
SS
366
367 if (block == NULL)
368 return NULL;
369
370 start = BLOCK_START (block);
371 end = BLOCK_END (block);
372
631b0ed0
JB
373 frame = get_current_frame ();
374 while (frame != NULL)
c906108c 375 {
edb3359d
DJ
376 struct block *frame_block = get_frame_block (frame, NULL);
377 if (frame_block != NULL && contained_in (frame_block, block))
c906108c 378 return frame;
631b0ed0
JB
379
380 frame = get_prev_frame (frame);
c906108c 381 }
631b0ed0
JB
382
383 return NULL;
c906108c 384}
This page took 0.689702 seconds and 4 git commands to generate.