]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/blockframe.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / blockframe.c
1 /* Get info from stack frames; convert between frames, blocks,
2 functions and pc values.
3
4 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
5 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007
6 Free Software Foundation, Inc.
7
8 This file is part of GDB.
9
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 2 of the License, or
13 (at your option) any later version.
14
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.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
24
25 #include "defs.h"
26 #include "symtab.h"
27 #include "bfd.h"
28 #include "objfiles.h"
29 #include "frame.h"
30 #include "gdbcore.h"
31 #include "value.h" /* for read_register */
32 #include "target.h" /* for target_has_stack */
33 #include "inferior.h" /* for read_pc */
34 #include "annotate.h"
35 #include "regcache.h"
36 #include "gdb_assert.h"
37 #include "dummy-frame.h"
38 #include "command.h"
39 #include "gdbcmd.h"
40 #include "block.h"
41
42 /* Prototypes for exported functions. */
43
44 void _initialize_blockframe (void);
45
46 /* Return the innermost lexical block in execution
47 in a specified stack frame. The frame address is assumed valid.
48
49 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
50 address we used to choose the block. We use this to find a source
51 line, to decide which macro definitions are in scope.
52
53 The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
54 PC, and may not really be a valid PC at all. For example, in the
55 caller of a function declared to never return, the code at the
56 return address will never be reached, so the call instruction may
57 be the very last instruction in the block. So the address we use
58 to choose the block is actually one byte before the return address
59 --- hopefully pointing us at the call instruction, or its delay
60 slot instruction. */
61
62 struct block *
63 get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
64 {
65 const CORE_ADDR pc = get_frame_address_in_block (frame);
66
67 if (addr_in_block)
68 *addr_in_block = pc;
69
70 return block_for_pc (pc);
71 }
72
73 CORE_ADDR
74 get_pc_function_start (CORE_ADDR pc)
75 {
76 struct block *bl;
77 struct minimal_symbol *msymbol;
78
79 bl = block_for_pc (pc);
80 if (bl)
81 {
82 struct symbol *symbol = block_function (bl);
83
84 if (symbol)
85 {
86 bl = SYMBOL_BLOCK_VALUE (symbol);
87 return BLOCK_START (bl);
88 }
89 }
90
91 msymbol = lookup_minimal_symbol_by_pc (pc);
92 if (msymbol)
93 {
94 CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol);
95
96 if (find_pc_section (fstart))
97 return fstart;
98 }
99
100 return 0;
101 }
102
103 /* Return the symbol for the function executing in frame FRAME. */
104
105 struct symbol *
106 get_frame_function (struct frame_info *frame)
107 {
108 struct block *bl = get_frame_block (frame, 0);
109 if (bl == 0)
110 return 0;
111 return block_function (bl);
112 }
113 \f
114
115 /* Return the function containing pc value PC in section SECTION.
116 Returns 0 if function is not known. */
117
118 struct symbol *
119 find_pc_sect_function (CORE_ADDR pc, struct bfd_section *section)
120 {
121 struct block *b = block_for_pc_sect (pc, section);
122 if (b == 0)
123 return 0;
124 return block_function (b);
125 }
126
127 /* Return the function containing pc value PC.
128 Returns 0 if function is not known. Backward compatibility, no section */
129
130 struct symbol *
131 find_pc_function (CORE_ADDR pc)
132 {
133 return find_pc_sect_function (pc, find_pc_mapped_section (pc));
134 }
135
136 /* These variables are used to cache the most recent result
137 * of find_pc_partial_function. */
138
139 static CORE_ADDR cache_pc_function_low = 0;
140 static CORE_ADDR cache_pc_function_high = 0;
141 static char *cache_pc_function_name = 0;
142 static struct bfd_section *cache_pc_function_section = NULL;
143
144 /* Clear cache, e.g. when symbol table is discarded. */
145
146 void
147 clear_pc_function_cache (void)
148 {
149 cache_pc_function_low = 0;
150 cache_pc_function_high = 0;
151 cache_pc_function_name = (char *) 0;
152 cache_pc_function_section = NULL;
153 }
154
155 /* Finds the "function" (text symbol) that is smaller than PC but
156 greatest of all of the potential text symbols in SECTION. Sets
157 *NAME and/or *ADDRESS conditionally if that pointer is non-null.
158 If ENDADDR is non-null, then set *ENDADDR to be the end of the
159 function (exclusive), but passing ENDADDR as non-null means that
160 the function might cause symbols to be read. This function either
161 succeeds or fails (not halfway succeeds). If it succeeds, it sets
162 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
163 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
164 returns 0. */
165
166 /* Backward compatibility, no section argument. */
167
168 int
169 find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address,
170 CORE_ADDR *endaddr)
171 {
172 struct bfd_section *section;
173 struct partial_symtab *pst;
174 struct symbol *f;
175 struct minimal_symbol *msymbol;
176 struct partial_symbol *psb;
177 struct obj_section *osect;
178 int i;
179 CORE_ADDR mapped_pc;
180
181 /* To ensure that the symbol returned belongs to the correct setion
182 (and that the last [random] symbol from the previous section
183 isn't returned) try to find the section containing PC. First try
184 the overlay code (which by default returns NULL); and second try
185 the normal section code (which almost always succeeds). */
186 section = find_pc_overlay (pc);
187 if (section == NULL)
188 {
189 struct obj_section *obj_section = find_pc_section (pc);
190 if (obj_section == NULL)
191 section = NULL;
192 else
193 section = obj_section->the_bfd_section;
194 }
195
196 mapped_pc = overlay_mapped_address (pc, section);
197
198 if (mapped_pc >= cache_pc_function_low
199 && mapped_pc < cache_pc_function_high
200 && section == cache_pc_function_section)
201 goto return_cached_value;
202
203 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
204 pst = find_pc_sect_psymtab (mapped_pc, section);
205 if (pst)
206 {
207 /* Need to read the symbols to get a good value for the end address. */
208 if (endaddr != NULL && !pst->readin)
209 {
210 /* Need to get the terminal in case symbol-reading produces
211 output. */
212 target_terminal_ours_for_output ();
213 PSYMTAB_TO_SYMTAB (pst);
214 }
215
216 if (pst->readin)
217 {
218 /* Checking whether the msymbol has a larger value is for the
219 "pathological" case mentioned in print_frame_info. */
220 f = find_pc_sect_function (mapped_pc, section);
221 if (f != NULL
222 && (msymbol == NULL
223 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
224 >= SYMBOL_VALUE_ADDRESS (msymbol))))
225 {
226 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
227 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
228 cache_pc_function_name = DEPRECATED_SYMBOL_NAME (f);
229 cache_pc_function_section = section;
230 goto return_cached_value;
231 }
232 }
233 else
234 {
235 /* Now that static symbols go in the minimal symbol table, perhaps
236 we could just ignore the partial symbols. But at least for now
237 we use the partial or minimal symbol, whichever is larger. */
238 psb = find_pc_sect_psymbol (pst, mapped_pc, section);
239
240 if (psb
241 && (msymbol == NULL ||
242 (SYMBOL_VALUE_ADDRESS (psb)
243 >= SYMBOL_VALUE_ADDRESS (msymbol))))
244 {
245 /* This case isn't being cached currently. */
246 if (address)
247 *address = SYMBOL_VALUE_ADDRESS (psb);
248 if (name)
249 *name = DEPRECATED_SYMBOL_NAME (psb);
250 /* endaddr non-NULL can't happen here. */
251 return 1;
252 }
253 }
254 }
255
256 /* Not in the normal symbol tables, see if the pc is in a known section.
257 If it's not, then give up. This ensures that anything beyond the end
258 of the text seg doesn't appear to be part of the last function in the
259 text segment. */
260
261 osect = find_pc_sect_section (mapped_pc, section);
262
263 if (!osect)
264 msymbol = NULL;
265
266 /* Must be in the minimal symbol table. */
267 if (msymbol == NULL)
268 {
269 /* No available symbol. */
270 if (name != NULL)
271 *name = 0;
272 if (address != NULL)
273 *address = 0;
274 if (endaddr != NULL)
275 *endaddr = 0;
276 return 0;
277 }
278
279 cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
280 cache_pc_function_name = DEPRECATED_SYMBOL_NAME (msymbol);
281 cache_pc_function_section = section;
282
283 /* If the minimal symbol has a size, use it for the cache.
284 Otherwise use the lesser of the next minimal symbol in the same
285 section, or the end of the section, as the end of the
286 function. */
287
288 if (MSYMBOL_SIZE (msymbol) != 0)
289 cache_pc_function_high = cache_pc_function_low + MSYMBOL_SIZE (msymbol);
290 else
291 {
292 /* Step over other symbols at this same address, and symbols in
293 other sections, to find the next symbol in this section with
294 a different address. */
295
296 for (i = 1; DEPRECATED_SYMBOL_NAME (msymbol + i) != NULL; i++)
297 {
298 if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol)
299 && SYMBOL_BFD_SECTION (msymbol + i) == SYMBOL_BFD_SECTION (msymbol))
300 break;
301 }
302
303 if (DEPRECATED_SYMBOL_NAME (msymbol + i) != NULL
304 && SYMBOL_VALUE_ADDRESS (msymbol + i) < osect->endaddr)
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. */
309 cache_pc_function_high = osect->endaddr;
310 }
311
312 return_cached_value:
313
314 if (address)
315 {
316 if (pc_in_unmapped_range (pc, section))
317 *address = overlay_unmapped_address (cache_pc_function_low, section);
318 else
319 *address = cache_pc_function_low;
320 }
321
322 if (name)
323 *name = cache_pc_function_name;
324
325 if (endaddr)
326 {
327 if (pc_in_unmapped_range (pc, section))
328 {
329 /* Because the high address is actually beyond the end of
330 the function (and therefore possibly beyond the end of
331 the overlay), we must actually convert (high - 1) and
332 then add one to that. */
333
334 *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
335 section);
336 }
337 else
338 *endaddr = cache_pc_function_high;
339 }
340
341 return 1;
342 }
343
344 /* Return the innermost stack frame executing inside of BLOCK,
345 or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */
346
347 struct frame_info *
348 block_innermost_frame (struct block *block)
349 {
350 struct frame_info *frame;
351 CORE_ADDR start;
352 CORE_ADDR end;
353 CORE_ADDR calling_pc;
354
355 if (block == NULL)
356 return NULL;
357
358 start = BLOCK_START (block);
359 end = BLOCK_END (block);
360
361 frame = get_current_frame ();
362 while (frame != NULL)
363 {
364 calling_pc = get_frame_address_in_block (frame);
365 if (calling_pc >= start && calling_pc < end)
366 return frame;
367
368 frame = get_prev_frame (frame);
369 }
370
371 return NULL;
372 }