Commit | Line | Data |
---|---|---|
7cc19214 AC |
1 | /* Get info from stack frames; convert between frames, blocks, |
2 | functions and pc values. | |
3 | ||
ecd75fc8 | 4 | Copyright (C) 1986-2014 Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
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 | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 11 | (at your option) any later version. |
c906108c | 12 | |
c5aa993b JM |
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. | |
c906108c | 17 | |
c5aa993b | 18 | You should have received a copy of the GNU General Public License |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
20 | |
21 | #include "defs.h" | |
22 | #include "symtab.h" | |
23 | #include "bfd.h" | |
c906108c SS |
24 | #include "objfiles.h" |
25 | #include "frame.h" | |
26 | #include "gdbcore.h" | |
7157eed4 UW |
27 | #include "value.h" |
28 | #include "target.h" | |
29 | #include "inferior.h" | |
c906108c | 30 | #include "annotate.h" |
4e052eda | 31 | #include "regcache.h" |
4f460812 | 32 | #include "gdb_assert.h" |
9c1412c1 | 33 | #include "dummy-frame.h" |
51603483 DJ |
34 | #include "command.h" |
35 | #include "gdbcmd.h" | |
fe898f56 | 36 | #include "block.h" |
edb3359d | 37 | #include "inline-frame.h" |
c906108c | 38 | |
4a64f543 MS |
39 | /* Return the innermost lexical block in execution in a specified |
40 | stack frame. The frame address is assumed valid. | |
ae767bfb JB |
41 | |
42 | If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code | |
43 | address we used to choose the block. We use this to find a source | |
44 | line, to decide which macro definitions are in scope. | |
45 | ||
46 | The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's | |
47 | PC, and may not really be a valid PC at all. For example, in the | |
48 | caller of a function declared to never return, the code at the | |
49 | return address will never be reached, so the call instruction may | |
50 | be the very last instruction in the block. So the address we use | |
51 | to choose the block is actually one byte before the return address | |
52 | --- hopefully pointing us at the call instruction, or its delay | |
53 | slot instruction. */ | |
c906108c SS |
54 | |
55 | struct block * | |
ae767bfb | 56 | get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block) |
c906108c | 57 | { |
e3eebbd7 | 58 | CORE_ADDR pc; |
edb3359d DJ |
59 | struct block *bl; |
60 | int inline_count; | |
ae767bfb | 61 | |
e3eebbd7 PA |
62 | if (!get_frame_address_in_block_if_available (frame, &pc)) |
63 | return NULL; | |
64 | ||
ae767bfb JB |
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 | 89 | struct block *bl; |
7cbd4a93 | 90 | struct bound_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); | |
7cbd4a93 | 105 | if (msymbol.minsym) |
c906108c | 106 | { |
77e371c0 | 107 | CORE_ADDR fstart = BMSYMBOL_VALUE_ADDRESS (msymbol); |
2cdd89cb MK |
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. | |
4a64f543 MS |
147 | Returns 0 if function is not known. |
148 | Backward compatibility, no section */ | |
c906108c SS |
149 | |
150 | struct symbol * | |
fba45db2 | 151 | find_pc_function (CORE_ADDR pc) |
c906108c SS |
152 | { |
153 | return find_pc_sect_function (pc, find_pc_mapped_section (pc)); | |
154 | } | |
155 | ||
156 | /* These variables are used to cache the most recent result | |
4a64f543 | 157 | of find_pc_partial_function. */ |
c906108c | 158 | |
c5aa993b JM |
159 | static CORE_ADDR cache_pc_function_low = 0; |
160 | static CORE_ADDR cache_pc_function_high = 0; | |
2c02bd72 | 161 | static const char *cache_pc_function_name = 0; |
714835d5 | 162 | static struct obj_section *cache_pc_function_section = NULL; |
11c81455 | 163 | static int cache_pc_function_is_gnu_ifunc = 0; |
c906108c | 164 | |
4a64f543 | 165 | /* Clear cache, e.g. when symbol table is discarded. */ |
c906108c SS |
166 | |
167 | void | |
fba45db2 | 168 | clear_pc_function_cache (void) |
c906108c SS |
169 | { |
170 | cache_pc_function_low = 0; | |
171 | cache_pc_function_high = 0; | |
c5aa993b | 172 | cache_pc_function_name = (char *) 0; |
c906108c | 173 | cache_pc_function_section = NULL; |
11c81455 | 174 | cache_pc_function_is_gnu_ifunc = 0; |
c906108c SS |
175 | } |
176 | ||
177 | /* Finds the "function" (text symbol) that is smaller than PC but | |
178 | greatest of all of the potential text symbols in SECTION. Sets | |
179 | *NAME and/or *ADDRESS conditionally if that pointer is non-null. | |
180 | If ENDADDR is non-null, then set *ENDADDR to be the end of the | |
181 | function (exclusive), but passing ENDADDR as non-null means that | |
11c81455 JK |
182 | the function might cause symbols to be read. If IS_GNU_IFUNC_P is provided |
183 | *IS_GNU_IFUNC_P is set to 1 on return if the function is STT_GNU_IFUNC. | |
184 | This function either succeeds or fails (not halfway succeeds). If it | |
185 | succeeds, it sets *NAME, *ADDRESS, and *ENDADDR to real information and | |
186 | returns 1. If it fails, it sets *NAME, *ADDRESS, *ENDADDR and | |
187 | *IS_GNU_IFUNC_P to zero and returns 0. */ | |
c906108c | 188 | |
73912b9b AC |
189 | /* Backward compatibility, no section argument. */ |
190 | ||
c906108c | 191 | int |
2c02bd72 | 192 | find_pc_partial_function_gnu_ifunc (CORE_ADDR pc, const char **name, |
11c81455 JK |
193 | CORE_ADDR *address, CORE_ADDR *endaddr, |
194 | int *is_gnu_ifunc_p) | |
c906108c | 195 | { |
714835d5 | 196 | struct obj_section *section; |
c5aa993b | 197 | struct symbol *f; |
50e65b17 | 198 | struct bound_minimal_symbol msymbol; |
ccefe4c4 TT |
199 | struct symtab *symtab = NULL; |
200 | struct objfile *objfile; | |
c906108c SS |
201 | int i; |
202 | CORE_ADDR mapped_pc; | |
203 | ||
73912b9b AC |
204 | /* To ensure that the symbol returned belongs to the correct setion |
205 | (and that the last [random] symbol from the previous section | |
206 | isn't returned) try to find the section containing PC. First try | |
207 | the overlay code (which by default returns NULL); and second try | |
208 | the normal section code (which almost always succeeds). */ | |
209 | section = find_pc_overlay (pc); | |
210 | if (section == NULL) | |
714835d5 | 211 | section = find_pc_section (pc); |
73912b9b | 212 | |
c906108c SS |
213 | mapped_pc = overlay_mapped_address (pc, section); |
214 | ||
247055de MK |
215 | if (mapped_pc >= cache_pc_function_low |
216 | && mapped_pc < cache_pc_function_high | |
217 | && section == cache_pc_function_section) | |
c906108c SS |
218 | goto return_cached_value; |
219 | ||
50e65b17 | 220 | msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section); |
ccefe4c4 TT |
221 | ALL_OBJFILES (objfile) |
222 | { | |
223 | if (objfile->sf) | |
77e371c0 | 224 | symtab = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol, |
ccefe4c4 TT |
225 | mapped_pc, section, 0); |
226 | if (symtab) | |
227 | break; | |
228 | } | |
229 | ||
230 | if (symtab) | |
c906108c | 231 | { |
ccefe4c4 TT |
232 | /* Checking whether the msymbol has a larger value is for the |
233 | "pathological" case mentioned in print_frame_info. */ | |
234 | f = find_pc_sect_function (mapped_pc, section); | |
235 | if (f != NULL | |
50e65b17 | 236 | && (msymbol.minsym == NULL |
ccefe4c4 | 237 | || (BLOCK_START (SYMBOL_BLOCK_VALUE (f)) |
77e371c0 | 238 | >= BMSYMBOL_VALUE_ADDRESS (msymbol)))) |
c906108c | 239 | { |
ccefe4c4 TT |
240 | cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f)); |
241 | cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f)); | |
242 | cache_pc_function_name = SYMBOL_LINKAGE_NAME (f); | |
243 | cache_pc_function_section = section; | |
11c81455 | 244 | cache_pc_function_is_gnu_ifunc = TYPE_GNU_IFUNC (SYMBOL_TYPE (f)); |
ccefe4c4 | 245 | goto return_cached_value; |
c906108c SS |
246 | } |
247 | } | |
248 | ||
4a64f543 MS |
249 | /* Not in the normal symbol tables, see if the pc is in a known |
250 | section. If it's not, then give up. This ensures that anything | |
251 | beyond the end of the text seg doesn't appear to be part of the | |
252 | last function in the text segment. */ | |
c906108c | 253 | |
714835d5 | 254 | if (!section) |
50e65b17 | 255 | msymbol.minsym = NULL; |
c906108c SS |
256 | |
257 | /* Must be in the minimal symbol table. */ | |
50e65b17 | 258 | if (msymbol.minsym == NULL) |
c906108c SS |
259 | { |
260 | /* No available symbol. */ | |
261 | if (name != NULL) | |
262 | *name = 0; | |
263 | if (address != NULL) | |
264 | *address = 0; | |
265 | if (endaddr != NULL) | |
266 | *endaddr = 0; | |
11c81455 JK |
267 | if (is_gnu_ifunc_p != NULL) |
268 | *is_gnu_ifunc_p = 0; | |
c906108c SS |
269 | return 0; |
270 | } | |
271 | ||
77e371c0 | 272 | cache_pc_function_low = BMSYMBOL_VALUE_ADDRESS (msymbol); |
efd66ac6 | 273 | cache_pc_function_name = MSYMBOL_LINKAGE_NAME (msymbol.minsym); |
c906108c | 274 | cache_pc_function_section = section; |
50e65b17 TT |
275 | cache_pc_function_is_gnu_ifunc = (MSYMBOL_TYPE (msymbol.minsym) |
276 | == mst_text_gnu_ifunc); | |
277 | cache_pc_function_high = minimal_symbol_upper_bound (msymbol); | |
c906108c | 278 | |
247055de | 279 | return_cached_value: |
c906108c SS |
280 | |
281 | if (address) | |
282 | { | |
283 | if (pc_in_unmapped_range (pc, section)) | |
c5aa993b | 284 | *address = overlay_unmapped_address (cache_pc_function_low, section); |
c906108c | 285 | else |
c5aa993b | 286 | *address = cache_pc_function_low; |
c906108c | 287 | } |
c5aa993b | 288 | |
c906108c SS |
289 | if (name) |
290 | *name = cache_pc_function_name; | |
291 | ||
292 | if (endaddr) | |
293 | { | |
294 | if (pc_in_unmapped_range (pc, section)) | |
c5aa993b | 295 | { |
c906108c SS |
296 | /* Because the high address is actually beyond the end of |
297 | the function (and therefore possibly beyond the end of | |
247055de | 298 | the overlay), we must actually convert (high - 1) and |
4a64f543 | 299 | then add one to that. */ |
c906108c | 300 | |
c5aa993b | 301 | *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1, |
c906108c | 302 | section); |
c5aa993b | 303 | } |
c906108c | 304 | else |
c5aa993b | 305 | *endaddr = cache_pc_function_high; |
c906108c SS |
306 | } |
307 | ||
11c81455 JK |
308 | if (is_gnu_ifunc_p) |
309 | *is_gnu_ifunc_p = cache_pc_function_is_gnu_ifunc; | |
310 | ||
c906108c SS |
311 | return 1; |
312 | } | |
313 | ||
11c81455 JK |
314 | /* See find_pc_partial_function_gnu_ifunc, only the IS_GNU_IFUNC_P parameter |
315 | is omitted here for backward API compatibility. */ | |
316 | ||
317 | int | |
2c02bd72 | 318 | find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address, |
11c81455 JK |
319 | CORE_ADDR *endaddr) |
320 | { | |
321 | return find_pc_partial_function_gnu_ifunc (pc, name, address, endaddr, NULL); | |
322 | } | |
323 | ||
72384ba3 PH |
324 | /* Return the innermost stack frame that is executing inside of BLOCK and is |
325 | at least as old as the selected frame. Return NULL if there is no | |
326 | such frame. If BLOCK is NULL, just return NULL. */ | |
c906108c SS |
327 | |
328 | struct frame_info * | |
9df2fbc4 | 329 | block_innermost_frame (const struct block *block) |
c906108c SS |
330 | { |
331 | struct frame_info *frame; | |
c906108c SS |
332 | |
333 | if (block == NULL) | |
334 | return NULL; | |
335 | ||
72384ba3 PH |
336 | frame = get_selected_frame_if_set (); |
337 | if (frame == NULL) | |
338 | frame = get_current_frame (); | |
631b0ed0 | 339 | while (frame != NULL) |
c906108c | 340 | { |
edb3359d DJ |
341 | struct block *frame_block = get_frame_block (frame, NULL); |
342 | if (frame_block != NULL && contained_in (frame_block, block)) | |
c906108c | 343 | return frame; |
631b0ed0 JB |
344 | |
345 | frame = get_prev_frame (frame); | |
c906108c | 346 | } |
631b0ed0 JB |
347 | |
348 | return NULL; | |
c906108c | 349 | } |