Commit | Line | Data |
---|---|---|
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 JB |
5 | 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009, |
6 | 2010 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 | |
c906108c | 42 | /* Return the innermost lexical block in execution |
ae767bfb JB |
43 | in a specified stack frame. The frame address is assumed valid. |
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 | |
58 | struct block * | |
ae767bfb | 59 | get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block) |
c906108c | 60 | { |
c4a09524 | 61 | const CORE_ADDR pc = get_frame_address_in_block (frame); |
edb3359d DJ |
62 | struct block *bl; |
63 | int inline_count; | |
ae767bfb JB |
64 | |
65 | if (addr_in_block) | |
66 | *addr_in_block = pc; | |
67 | ||
edb3359d DJ |
68 | bl = block_for_pc (pc); |
69 | if (bl == NULL) | |
70 | return NULL; | |
71 | ||
72 | inline_count = frame_inlined_callees (frame); | |
73 | ||
74 | while (inline_count > 0) | |
75 | { | |
76 | if (block_inlined_p (bl)) | |
77 | inline_count--; | |
78 | ||
79 | bl = BLOCK_SUPERBLOCK (bl); | |
80 | gdb_assert (bl != NULL); | |
81 | } | |
82 | ||
83 | return bl; | |
c906108c SS |
84 | } |
85 | ||
c906108c | 86 | CORE_ADDR |
fba45db2 | 87 | get_pc_function_start (CORE_ADDR pc) |
c906108c | 88 | { |
2cdd89cb MK |
89 | struct block *bl; |
90 | struct minimal_symbol *msymbol; | |
c906108c | 91 | |
2cdd89cb MK |
92 | bl = block_for_pc (pc); |
93 | if (bl) | |
c906108c | 94 | { |
7f0df278 | 95 | struct symbol *symbol = block_linkage_function (bl); |
2cdd89cb MK |
96 | |
97 | if (symbol) | |
98 | { | |
99 | bl = SYMBOL_BLOCK_VALUE (symbol); | |
100 | return BLOCK_START (bl); | |
101 | } | |
c906108c | 102 | } |
2cdd89cb MK |
103 | |
104 | msymbol = lookup_minimal_symbol_by_pc (pc); | |
105 | if (msymbol) | |
c906108c | 106 | { |
2cdd89cb MK |
107 | CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol); |
108 | ||
109 | if (find_pc_section (fstart)) | |
110 | return fstart; | |
c906108c | 111 | } |
2cdd89cb MK |
112 | |
113 | return 0; | |
c906108c SS |
114 | } |
115 | ||
116 | /* Return the symbol for the function executing in frame FRAME. */ | |
117 | ||
118 | struct symbol * | |
fba45db2 | 119 | get_frame_function (struct frame_info *frame) |
c906108c | 120 | { |
52f0bd74 | 121 | struct block *bl = get_frame_block (frame, 0); |
edb3359d DJ |
122 | |
123 | if (bl == NULL) | |
124 | return NULL; | |
125 | ||
126 | while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL) | |
127 | bl = BLOCK_SUPERBLOCK (bl); | |
128 | ||
129 | return BLOCK_FUNCTION (bl); | |
c906108c SS |
130 | } |
131 | \f | |
132 | ||
c906108c SS |
133 | /* Return the function containing pc value PC in section SECTION. |
134 | Returns 0 if function is not known. */ | |
135 | ||
136 | struct symbol * | |
714835d5 | 137 | find_pc_sect_function (CORE_ADDR pc, struct obj_section *section) |
c906108c | 138 | { |
52f0bd74 | 139 | struct block *b = block_for_pc_sect (pc, section); |
cc59ec59 | 140 | |
c906108c SS |
141 | if (b == 0) |
142 | return 0; | |
7f0df278 | 143 | return block_linkage_function (b); |
c906108c SS |
144 | } |
145 | ||
146 | /* Return the function containing pc value PC. | |
147 | Returns 0 if function is not known. Backward compatibility, no section */ | |
148 | ||
149 | struct symbol * | |
fba45db2 | 150 | find_pc_function (CORE_ADDR pc) |
c906108c SS |
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 | |
156 | * of find_pc_partial_function. */ | |
157 | ||
c5aa993b JM |
158 | static CORE_ADDR cache_pc_function_low = 0; |
159 | static CORE_ADDR cache_pc_function_high = 0; | |
160 | static char *cache_pc_function_name = 0; | |
714835d5 | 161 | static struct obj_section *cache_pc_function_section = NULL; |
c906108c SS |
162 | |
163 | /* Clear cache, e.g. when symbol table is discarded. */ | |
164 | ||
165 | void | |
fba45db2 | 166 | clear_pc_function_cache (void) |
c906108c SS |
167 | { |
168 | cache_pc_function_low = 0; | |
169 | cache_pc_function_high = 0; | |
c5aa993b | 170 | cache_pc_function_name = (char *) 0; |
c906108c SS |
171 | cache_pc_function_section = NULL; |
172 | } | |
173 | ||
174 | /* Finds the "function" (text symbol) that is smaller than PC but | |
175 | greatest of all of the potential text symbols in SECTION. Sets | |
176 | *NAME and/or *ADDRESS conditionally if that pointer is non-null. | |
177 | If ENDADDR is non-null, then set *ENDADDR to be the end of the | |
178 | function (exclusive), but passing ENDADDR as non-null means that | |
179 | the function might cause symbols to be read. This function either | |
180 | succeeds or fails (not halfway succeeds). If it succeeds, it sets | |
181 | *NAME, *ADDRESS, and *ENDADDR to real information and returns 1. | |
182 | If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and | |
183 | returns 0. */ | |
184 | ||
73912b9b AC |
185 | /* Backward compatibility, no section argument. */ |
186 | ||
c906108c | 187 | int |
73912b9b AC |
188 | find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address, |
189 | CORE_ADDR *endaddr) | |
c906108c | 190 | { |
714835d5 | 191 | struct obj_section *section; |
c5aa993b | 192 | struct symbol *f; |
c906108c | 193 | struct minimal_symbol *msymbol; |
ccefe4c4 TT |
194 | struct symtab *symtab = NULL; |
195 | struct objfile *objfile; | |
c906108c SS |
196 | int i; |
197 | CORE_ADDR mapped_pc; | |
198 | ||
73912b9b AC |
199 | /* To ensure that the symbol returned belongs to the correct setion |
200 | (and that the last [random] symbol from the previous section | |
201 | isn't returned) try to find the section containing PC. First try | |
202 | the overlay code (which by default returns NULL); and second try | |
203 | the normal section code (which almost always succeeds). */ | |
204 | section = find_pc_overlay (pc); | |
205 | if (section == NULL) | |
714835d5 | 206 | section = find_pc_section (pc); |
73912b9b | 207 | |
c906108c SS |
208 | mapped_pc = overlay_mapped_address (pc, section); |
209 | ||
247055de MK |
210 | if (mapped_pc >= cache_pc_function_low |
211 | && mapped_pc < cache_pc_function_high | |
212 | && section == cache_pc_function_section) | |
c906108c SS |
213 | goto return_cached_value; |
214 | ||
c906108c | 215 | msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section); |
ccefe4c4 TT |
216 | ALL_OBJFILES (objfile) |
217 | { | |
218 | if (objfile->sf) | |
219 | symtab = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol, | |
220 | mapped_pc, section, 0); | |
221 | if (symtab) | |
222 | break; | |
223 | } | |
224 | ||
225 | if (symtab) | |
c906108c | 226 | { |
ccefe4c4 TT |
227 | /* Checking whether the msymbol has a larger value is for the |
228 | "pathological" case mentioned in print_frame_info. */ | |
229 | f = find_pc_sect_function (mapped_pc, section); | |
230 | if (f != NULL | |
231 | && (msymbol == NULL | |
232 | || (BLOCK_START (SYMBOL_BLOCK_VALUE (f)) | |
233 | >= SYMBOL_VALUE_ADDRESS (msymbol)))) | |
c906108c | 234 | { |
ccefe4c4 TT |
235 | cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f)); |
236 | cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f)); | |
237 | cache_pc_function_name = SYMBOL_LINKAGE_NAME (f); | |
238 | cache_pc_function_section = section; | |
239 | goto return_cached_value; | |
c906108c SS |
240 | } |
241 | } | |
242 | ||
243 | /* Not in the normal symbol tables, see if the pc is in a known section. | |
244 | If it's not, then give up. This ensures that anything beyond the end | |
245 | of the text seg doesn't appear to be part of the last function in the | |
246 | text segment. */ | |
247 | ||
714835d5 | 248 | if (!section) |
c906108c SS |
249 | msymbol = NULL; |
250 | ||
251 | /* Must be in the minimal symbol table. */ | |
252 | if (msymbol == NULL) | |
253 | { | |
254 | /* No available symbol. */ | |
255 | if (name != NULL) | |
256 | *name = 0; | |
257 | if (address != NULL) | |
258 | *address = 0; | |
259 | if (endaddr != NULL) | |
260 | *endaddr = 0; | |
261 | return 0; | |
262 | } | |
263 | ||
c5aa993b | 264 | cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol); |
3567439c | 265 | cache_pc_function_name = SYMBOL_LINKAGE_NAME (msymbol); |
c906108c SS |
266 | cache_pc_function_section = section; |
267 | ||
29e8a844 DJ |
268 | /* If the minimal symbol has a size, use it for the cache. |
269 | Otherwise use the lesser of the next minimal symbol in the same | |
270 | section, or the end of the section, as the end of the | |
271 | function. */ | |
c5aa993b | 272 | |
29e8a844 DJ |
273 | if (MSYMBOL_SIZE (msymbol) != 0) |
274 | cache_pc_function_high = cache_pc_function_low + MSYMBOL_SIZE (msymbol); | |
275 | else | |
c906108c | 276 | { |
29e8a844 DJ |
277 | /* Step over other symbols at this same address, and symbols in |
278 | other sections, to find the next symbol in this section with | |
279 | a different address. */ | |
c906108c | 280 | |
3567439c | 281 | for (i = 1; SYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++) |
29e8a844 DJ |
282 | { |
283 | if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol) | |
714835d5 | 284 | && SYMBOL_OBJ_SECTION (msymbol + i) == SYMBOL_OBJ_SECTION (msymbol)) |
29e8a844 DJ |
285 | break; |
286 | } | |
287 | ||
3567439c | 288 | if (SYMBOL_LINKAGE_NAME (msymbol + i) != NULL |
714835d5 | 289 | && SYMBOL_VALUE_ADDRESS (msymbol + i) < obj_section_endaddr (section)) |
29e8a844 DJ |
290 | cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i); |
291 | else | |
292 | /* We got the start address from the last msymbol in the objfile. | |
293 | So the end address is the end of the section. */ | |
714835d5 | 294 | cache_pc_function_high = obj_section_endaddr (section); |
29e8a844 | 295 | } |
c906108c | 296 | |
247055de | 297 | return_cached_value: |
c906108c SS |
298 | |
299 | if (address) | |
300 | { | |
301 | if (pc_in_unmapped_range (pc, section)) | |
c5aa993b | 302 | *address = overlay_unmapped_address (cache_pc_function_low, section); |
c906108c | 303 | else |
c5aa993b | 304 | *address = cache_pc_function_low; |
c906108c | 305 | } |
c5aa993b | 306 | |
c906108c SS |
307 | if (name) |
308 | *name = cache_pc_function_name; | |
309 | ||
310 | if (endaddr) | |
311 | { | |
312 | if (pc_in_unmapped_range (pc, section)) | |
c5aa993b | 313 | { |
c906108c SS |
314 | /* Because the high address is actually beyond the end of |
315 | the function (and therefore possibly beyond the end of | |
247055de MK |
316 | the overlay), we must actually convert (high - 1) and |
317 | then add one to that. */ | |
c906108c | 318 | |
c5aa993b | 319 | *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1, |
c906108c | 320 | section); |
c5aa993b | 321 | } |
c906108c | 322 | else |
c5aa993b | 323 | *endaddr = cache_pc_function_high; |
c906108c SS |
324 | } |
325 | ||
326 | return 1; | |
327 | } | |
328 | ||
c906108c SS |
329 | /* Return the innermost stack frame executing inside of BLOCK, |
330 | or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */ | |
331 | ||
332 | struct frame_info * | |
fba45db2 | 333 | block_innermost_frame (struct block *block) |
c906108c SS |
334 | { |
335 | struct frame_info *frame; | |
52f0bd74 AC |
336 | CORE_ADDR start; |
337 | CORE_ADDR end; | |
c906108c SS |
338 | |
339 | if (block == NULL) | |
340 | return NULL; | |
341 | ||
342 | start = BLOCK_START (block); | |
343 | end = BLOCK_END (block); | |
344 | ||
631b0ed0 JB |
345 | frame = get_current_frame (); |
346 | while (frame != NULL) | |
c906108c | 347 | { |
edb3359d DJ |
348 | struct block *frame_block = get_frame_block (frame, NULL); |
349 | if (frame_block != NULL && contained_in (frame_block, block)) | |
c906108c | 350 | return frame; |
631b0ed0 JB |
351 | |
352 | frame = get_prev_frame (frame); | |
c906108c | 353 | } |
631b0ed0 JB |
354 | |
355 | return NULL; | |
c906108c | 356 | } |