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