Commit | Line | Data |
---|---|---|
7cc19214 AC |
1 | /* Get info from stack frames; convert between frames, blocks, |
2 | functions and pc values. | |
3 | ||
e2882c85 | 4 | Copyright (C) 1986-2018 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" |
9c1412c1 | 32 | #include "dummy-frame.h" |
51603483 DJ |
33 | #include "command.h" |
34 | #include "gdbcmd.h" | |
fe898f56 | 35 | #include "block.h" |
edb3359d | 36 | #include "inline-frame.h" |
c906108c | 37 | |
4a64f543 MS |
38 | /* Return the innermost lexical block in execution in a specified |
39 | stack frame. The frame address is assumed valid. | |
ae767bfb JB |
40 | |
41 | If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code | |
42 | address we used to choose the block. We use this to find a source | |
43 | line, to decide which macro definitions are in scope. | |
44 | ||
45 | The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's | |
46 | PC, and may not really be a valid PC at all. For example, in the | |
47 | caller of a function declared to never return, the code at the | |
48 | return address will never be reached, so the call instruction may | |
49 | be the very last instruction in the block. So the address we use | |
50 | to choose the block is actually one byte before the return address | |
51 | --- hopefully pointing us at the call instruction, or its delay | |
52 | slot instruction. */ | |
c906108c | 53 | |
3977b71f | 54 | const struct block * |
ae767bfb | 55 | get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block) |
c906108c | 56 | { |
e3eebbd7 | 57 | CORE_ADDR pc; |
3977b71f | 58 | const struct block *bl; |
edb3359d | 59 | int inline_count; |
ae767bfb | 60 | |
e3eebbd7 PA |
61 | if (!get_frame_address_in_block_if_available (frame, &pc)) |
62 | return NULL; | |
63 | ||
ae767bfb JB |
64 | if (addr_in_block) |
65 | *addr_in_block = pc; | |
66 | ||
edb3359d DJ |
67 | bl = block_for_pc (pc); |
68 | if (bl == NULL) | |
69 | return NULL; | |
70 | ||
71 | inline_count = frame_inlined_callees (frame); | |
72 | ||
73 | while (inline_count > 0) | |
74 | { | |
75 | if (block_inlined_p (bl)) | |
76 | inline_count--; | |
77 | ||
78 | bl = BLOCK_SUPERBLOCK (bl); | |
79 | gdb_assert (bl != NULL); | |
80 | } | |
81 | ||
82 | return bl; | |
c906108c SS |
83 | } |
84 | ||
c906108c | 85 | CORE_ADDR |
fba45db2 | 86 | get_pc_function_start (CORE_ADDR pc) |
c906108c | 87 | { |
3977b71f | 88 | const struct block *bl; |
7cbd4a93 | 89 | struct bound_minimal_symbol msymbol; |
c906108c | 90 | |
2cdd89cb MK |
91 | bl = block_for_pc (pc); |
92 | if (bl) | |
c906108c | 93 | { |
7f0df278 | 94 | struct symbol *symbol = block_linkage_function (bl); |
2cdd89cb MK |
95 | |
96 | if (symbol) | |
97 | { | |
98 | bl = SYMBOL_BLOCK_VALUE (symbol); | |
99 | return BLOCK_START (bl); | |
100 | } | |
c906108c | 101 | } |
2cdd89cb MK |
102 | |
103 | msymbol = lookup_minimal_symbol_by_pc (pc); | |
7cbd4a93 | 104 | if (msymbol.minsym) |
c906108c | 105 | { |
77e371c0 | 106 | CORE_ADDR fstart = BMSYMBOL_VALUE_ADDRESS (msymbol); |
2cdd89cb MK |
107 | |
108 | if (find_pc_section (fstart)) | |
109 | return fstart; | |
c906108c | 110 | } |
2cdd89cb MK |
111 | |
112 | return 0; | |
c906108c SS |
113 | } |
114 | ||
115 | /* Return the symbol for the function executing in frame FRAME. */ | |
116 | ||
117 | struct symbol * | |
fba45db2 | 118 | get_frame_function (struct frame_info *frame) |
c906108c | 119 | { |
3977b71f | 120 | const struct block *bl = get_frame_block (frame, 0); |
edb3359d DJ |
121 | |
122 | if (bl == NULL) | |
123 | return NULL; | |
124 | ||
125 | while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL) | |
126 | bl = BLOCK_SUPERBLOCK (bl); | |
127 | ||
128 | return BLOCK_FUNCTION (bl); | |
c906108c SS |
129 | } |
130 | \f | |
131 | ||
c906108c SS |
132 | /* Return the function containing pc value PC in section SECTION. |
133 | Returns 0 if function is not known. */ | |
134 | ||
135 | struct symbol * | |
714835d5 | 136 | find_pc_sect_function (CORE_ADDR pc, struct obj_section *section) |
c906108c | 137 | { |
3977b71f | 138 | const struct block *b = block_for_pc_sect (pc, section); |
cc59ec59 | 139 | |
c906108c SS |
140 | if (b == 0) |
141 | return 0; | |
7f0df278 | 142 | return block_linkage_function (b); |
c906108c SS |
143 | } |
144 | ||
145 | /* Return the function containing pc value PC. | |
4a64f543 MS |
146 | Returns 0 if function is not known. |
147 | Backward compatibility, no section */ | |
c906108c SS |
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 | |
4a64f543 | 156 | of find_pc_partial_function. */ |
c906108c | 157 | |
c5aa993b JM |
158 | static CORE_ADDR cache_pc_function_low = 0; |
159 | static CORE_ADDR cache_pc_function_high = 0; | |
2c02bd72 | 160 | static const char *cache_pc_function_name = 0; |
714835d5 | 161 | static struct obj_section *cache_pc_function_section = NULL; |
c906108c | 162 | |
4a64f543 | 163 | /* Clear cache, e.g. when symbol table is discarded. */ |
c906108c SS |
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 | |
a0aca7b0 PA |
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. */ | |
c906108c | 184 | |
73912b9b AC |
185 | /* Backward compatibility, no section argument. */ |
186 | ||
c906108c | 187 | int |
a0aca7b0 PA |
188 | find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address, |
189 | CORE_ADDR *endaddr) | |
c906108c | 190 | { |
714835d5 | 191 | struct obj_section *section; |
c5aa993b | 192 | struct symbol *f; |
50e65b17 | 193 | struct bound_minimal_symbol msymbol; |
43f3e411 | 194 | struct compunit_symtab *compunit_symtab = NULL; |
ccefe4c4 | 195 | struct objfile *objfile; |
c906108c SS |
196 | CORE_ADDR mapped_pc; |
197 | ||
73912b9b AC |
198 | /* To ensure that the symbol returned belongs to the correct setion |
199 | (and that the last [random] symbol from the previous section | |
200 | isn't returned) try to find the section containing PC. First try | |
201 | the overlay code (which by default returns NULL); and second try | |
202 | the normal section code (which almost always succeeds). */ | |
203 | section = find_pc_overlay (pc); | |
204 | if (section == NULL) | |
714835d5 | 205 | section = find_pc_section (pc); |
73912b9b | 206 | |
c906108c SS |
207 | mapped_pc = overlay_mapped_address (pc, section); |
208 | ||
247055de MK |
209 | if (mapped_pc >= cache_pc_function_low |
210 | && mapped_pc < cache_pc_function_high | |
211 | && section == cache_pc_function_section) | |
c906108c SS |
212 | goto return_cached_value; |
213 | ||
50e65b17 | 214 | msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section); |
ccefe4c4 TT |
215 | ALL_OBJFILES (objfile) |
216 | { | |
217 | if (objfile->sf) | |
43f3e411 DE |
218 | { |
219 | compunit_symtab | |
220 | = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol, | |
221 | mapped_pc, section, | |
222 | 0); | |
223 | } | |
224 | if (compunit_symtab != NULL) | |
ccefe4c4 TT |
225 | break; |
226 | } | |
227 | ||
43f3e411 | 228 | if (compunit_symtab != NULL) |
c906108c | 229 | { |
ccefe4c4 TT |
230 | /* Checking whether the msymbol has a larger value is for the |
231 | "pathological" case mentioned in print_frame_info. */ | |
232 | f = find_pc_sect_function (mapped_pc, section); | |
233 | if (f != NULL | |
50e65b17 | 234 | && (msymbol.minsym == NULL |
ccefe4c4 | 235 | || (BLOCK_START (SYMBOL_BLOCK_VALUE (f)) |
77e371c0 | 236 | >= BMSYMBOL_VALUE_ADDRESS (msymbol)))) |
c906108c | 237 | { |
ccefe4c4 TT |
238 | cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f)); |
239 | cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f)); | |
240 | cache_pc_function_name = SYMBOL_LINKAGE_NAME (f); | |
241 | cache_pc_function_section = section; | |
242 | goto return_cached_value; | |
c906108c SS |
243 | } |
244 | } | |
245 | ||
4a64f543 MS |
246 | /* Not in the normal symbol tables, see if the pc is in a known |
247 | section. If it's not, then give up. This ensures that anything | |
248 | beyond the end of the text seg doesn't appear to be part of the | |
249 | last function in the text segment. */ | |
c906108c | 250 | |
714835d5 | 251 | if (!section) |
50e65b17 | 252 | msymbol.minsym = NULL; |
c906108c SS |
253 | |
254 | /* Must be in the minimal symbol table. */ | |
50e65b17 | 255 | if (msymbol.minsym == NULL) |
c906108c SS |
256 | { |
257 | /* No available symbol. */ | |
258 | if (name != NULL) | |
259 | *name = 0; | |
260 | if (address != NULL) | |
261 | *address = 0; | |
262 | if (endaddr != NULL) | |
263 | *endaddr = 0; | |
264 | return 0; | |
265 | } | |
266 | ||
77e371c0 | 267 | cache_pc_function_low = BMSYMBOL_VALUE_ADDRESS (msymbol); |
efd66ac6 | 268 | cache_pc_function_name = MSYMBOL_LINKAGE_NAME (msymbol.minsym); |
c906108c | 269 | cache_pc_function_section = section; |
50e65b17 | 270 | cache_pc_function_high = minimal_symbol_upper_bound (msymbol); |
c906108c | 271 | |
247055de | 272 | return_cached_value: |
c906108c SS |
273 | |
274 | if (address) | |
275 | { | |
276 | if (pc_in_unmapped_range (pc, section)) | |
c5aa993b | 277 | *address = overlay_unmapped_address (cache_pc_function_low, section); |
c906108c | 278 | else |
c5aa993b | 279 | *address = cache_pc_function_low; |
c906108c | 280 | } |
c5aa993b | 281 | |
c906108c SS |
282 | if (name) |
283 | *name = cache_pc_function_name; | |
284 | ||
285 | if (endaddr) | |
286 | { | |
287 | if (pc_in_unmapped_range (pc, section)) | |
c5aa993b | 288 | { |
c906108c SS |
289 | /* Because the high address is actually beyond the end of |
290 | the function (and therefore possibly beyond the end of | |
247055de | 291 | the overlay), we must actually convert (high - 1) and |
4a64f543 | 292 | then add one to that. */ |
c906108c | 293 | |
c5aa993b | 294 | *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1, |
c906108c | 295 | section); |
c5aa993b | 296 | } |
c906108c | 297 | else |
c5aa993b | 298 | *endaddr = cache_pc_function_high; |
c906108c SS |
299 | } |
300 | ||
301 | return 1; | |
302 | } | |
303 | ||
8388016d PA |
304 | /* See symtab.h. */ |
305 | ||
306 | struct type * | |
307 | find_function_type (CORE_ADDR pc) | |
308 | { | |
309 | struct symbol *sym = find_pc_function (pc); | |
310 | ||
311 | if (sym != NULL && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) == pc) | |
312 | return SYMBOL_TYPE (sym); | |
313 | ||
314 | return NULL; | |
315 | } | |
316 | ||
317 | /* See symtab.h. */ | |
318 | ||
319 | struct type * | |
320 | find_gnu_ifunc_target_type (CORE_ADDR resolver_funaddr) | |
321 | { | |
322 | struct type *resolver_type = find_function_type (resolver_funaddr); | |
323 | if (resolver_type != NULL) | |
324 | { | |
325 | /* Get the return type of the resolver. */ | |
326 | struct type *resolver_ret_type | |
327 | = check_typedef (TYPE_TARGET_TYPE (resolver_type)); | |
328 | ||
329 | /* If we found a pointer to function, then the resolved type | |
330 | is the type of the pointed-to function. */ | |
331 | if (TYPE_CODE (resolver_ret_type) == TYPE_CODE_PTR) | |
332 | { | |
333 | struct type *resolved_type | |
334 | = TYPE_TARGET_TYPE (resolver_ret_type); | |
335 | if (TYPE_CODE (check_typedef (resolved_type)) == TYPE_CODE_FUNC) | |
336 | return resolved_type; | |
337 | } | |
338 | } | |
339 | ||
340 | return NULL; | |
341 | } | |
342 | ||
72384ba3 PH |
343 | /* Return the innermost stack frame that is executing inside of BLOCK and is |
344 | at least as old as the selected frame. Return NULL if there is no | |
345 | such frame. If BLOCK is NULL, just return NULL. */ | |
c906108c SS |
346 | |
347 | struct frame_info * | |
9df2fbc4 | 348 | block_innermost_frame (const struct block *block) |
c906108c SS |
349 | { |
350 | struct frame_info *frame; | |
c906108c SS |
351 | |
352 | if (block == NULL) | |
353 | return NULL; | |
354 | ||
72384ba3 PH |
355 | frame = get_selected_frame_if_set (); |
356 | if (frame == NULL) | |
357 | frame = get_current_frame (); | |
631b0ed0 | 358 | while (frame != NULL) |
c906108c | 359 | { |
3977b71f | 360 | const struct block *frame_block = get_frame_block (frame, NULL); |
edb3359d | 361 | if (frame_block != NULL && contained_in (frame_block, block)) |
c906108c | 362 | return frame; |
631b0ed0 JB |
363 | |
364 | frame = get_prev_frame (frame); | |
c906108c | 365 | } |
631b0ed0 JB |
366 | |
367 | return NULL; | |
c906108c | 368 | } |