gdb: add target_ops::supports_displaced_step
[deliverable/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-2020 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "bfd.h"
24 #include "objfiles.h"
25 #include "frame.h"
26 #include "gdbcore.h"
27 #include "value.h"
28 #include "target.h"
29 #include "inferior.h"
30 #include "annotate.h"
31 #include "regcache.h"
32 #include "dummy-frame.h"
33 #include "command.h"
34 #include "gdbcmd.h"
35 #include "block.h"
36 #include "inline-frame.h"
37
38 /* Return the innermost lexical block in execution in a specified
39 stack frame. The frame address is assumed valid.
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. */
53
54 const struct block *
55 get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
56 {
57 CORE_ADDR pc;
58 const struct block *bl;
59 int inline_count;
60
61 if (!get_frame_address_in_block_if_available (frame, &pc))
62 return NULL;
63
64 if (addr_in_block)
65 *addr_in_block = pc;
66
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;
83 }
84
85 CORE_ADDR
86 get_pc_function_start (CORE_ADDR pc)
87 {
88 const struct block *bl;
89 struct bound_minimal_symbol msymbol;
90
91 bl = block_for_pc (pc);
92 if (bl)
93 {
94 struct symbol *symbol = block_linkage_function (bl);
95
96 if (symbol)
97 {
98 bl = SYMBOL_BLOCK_VALUE (symbol);
99 return BLOCK_ENTRY_PC (bl);
100 }
101 }
102
103 msymbol = lookup_minimal_symbol_by_pc (pc);
104 if (msymbol.minsym)
105 {
106 CORE_ADDR fstart = BMSYMBOL_VALUE_ADDRESS (msymbol);
107
108 if (find_pc_section (fstart))
109 return fstart;
110 }
111
112 return 0;
113 }
114
115 /* Return the symbol for the function executing in frame FRAME. */
116
117 struct symbol *
118 get_frame_function (struct frame_info *frame)
119 {
120 const struct block *bl = get_frame_block (frame, 0);
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);
129 }
130 \f
131
132 /* Return the function containing pc value PC in section SECTION.
133 Returns 0 if function is not known. */
134
135 struct symbol *
136 find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
137 {
138 const struct block *b = block_for_pc_sect (pc, section);
139
140 if (b == 0)
141 return 0;
142 return block_linkage_function (b);
143 }
144
145 /* Return the function containing pc value PC.
146 Returns 0 if function is not known.
147 Backward compatibility, no section */
148
149 struct symbol *
150 find_pc_function (CORE_ADDR pc)
151 {
152 return find_pc_sect_function (pc, find_pc_mapped_section (pc));
153 }
154
155 /* See symtab.h. */
156
157 struct symbol *
158 find_pc_sect_containing_function (CORE_ADDR pc, struct obj_section *section)
159 {
160 const block *bl = block_for_pc_sect (pc, section);
161
162 if (bl == nullptr)
163 return nullptr;
164
165 return block_containing_function (bl);
166 }
167
168 /* These variables are used to cache the most recent result of
169 find_pc_partial_function.
170
171 The addresses cache_pc_function_low and cache_pc_function_high
172 record the range in which PC was found during the most recent
173 successful lookup. When the function occupies a single contiguous
174 address range, these values correspond to the low and high
175 addresses of the function. (The high address is actually one byte
176 beyond the last byte of the function.) For a function with more
177 than one (non-contiguous) range, the range in which PC was found is
178 used to set the cache bounds.
179
180 When determining whether or not these cached values apply to a
181 particular PC value, PC must be within the range specified by
182 cache_pc_function_low and cache_pc_function_high. In addition to
183 PC being in that range, cache_pc_section must also match PC's
184 section. See find_pc_partial_function() for details on both the
185 comparison as well as how PC's section is determined.
186
187 The other values aren't used for determining whether the cache
188 applies, but are used for setting the outputs from
189 find_pc_partial_function. cache_pc_function_low and
190 cache_pc_function_high are used to set outputs as well. */
191
192 static CORE_ADDR cache_pc_function_low = 0;
193 static CORE_ADDR cache_pc_function_high = 0;
194 static const char *cache_pc_function_name = 0;
195 static struct obj_section *cache_pc_function_section = NULL;
196 static const struct block *cache_pc_function_block = nullptr;
197
198 /* Clear cache, e.g. when symbol table is discarded. */
199
200 void
201 clear_pc_function_cache (void)
202 {
203 cache_pc_function_low = 0;
204 cache_pc_function_high = 0;
205 cache_pc_function_name = (char *) 0;
206 cache_pc_function_section = NULL;
207 cache_pc_function_block = nullptr;
208 }
209
210 /* See symtab.h. */
211
212 bool
213 find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
214 CORE_ADDR *endaddr, const struct block **block)
215 {
216 struct obj_section *section;
217 struct symbol *f;
218 struct bound_minimal_symbol msymbol;
219 struct compunit_symtab *compunit_symtab = NULL;
220 CORE_ADDR mapped_pc;
221
222 /* To ensure that the symbol returned belongs to the correct section
223 (and that the last [random] symbol from the previous section
224 isn't returned) try to find the section containing PC. First try
225 the overlay code (which by default returns NULL); and second try
226 the normal section code (which almost always succeeds). */
227 section = find_pc_overlay (pc);
228 if (section == NULL)
229 section = find_pc_section (pc);
230
231 mapped_pc = overlay_mapped_address (pc, section);
232
233 if (mapped_pc >= cache_pc_function_low
234 && mapped_pc < cache_pc_function_high
235 && section == cache_pc_function_section)
236 goto return_cached_value;
237
238 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
239 compunit_symtab = find_pc_sect_compunit_symtab (mapped_pc, section);
240
241 if (compunit_symtab != NULL)
242 {
243 /* Checking whether the msymbol has a larger value is for the
244 "pathological" case mentioned in stack.c:find_frame_funname.
245
246 We use BLOCK_ENTRY_PC instead of BLOCK_START_PC for this
247 comparison because the minimal symbol should refer to the
248 function's entry pc which is not necessarily the lowest
249 address of the function. This will happen when the function
250 has more than one range and the entry pc is not within the
251 lowest range of addresses. */
252 f = find_pc_sect_function (mapped_pc, section);
253 if (f != NULL
254 && (msymbol.minsym == NULL
255 || (BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (f))
256 >= BMSYMBOL_VALUE_ADDRESS (msymbol))))
257 {
258 const struct block *b = SYMBOL_BLOCK_VALUE (f);
259
260 cache_pc_function_name = f->linkage_name ();
261 cache_pc_function_section = section;
262 cache_pc_function_block = b;
263
264 /* For blocks occupying contiguous addresses (i.e. no gaps),
265 the low and high cache addresses are simply the start
266 and end of the block.
267
268 For blocks with non-contiguous ranges, we have to search
269 for the range containing mapped_pc and then use the start
270 and end of that range.
271
272 This causes the returned *ADDRESS and *ENDADDR values to
273 be limited to the range in which mapped_pc is found. See
274 comment preceding declaration of find_pc_partial_function
275 in symtab.h for more information. */
276
277 if (BLOCK_CONTIGUOUS_P (b))
278 {
279 cache_pc_function_low = BLOCK_START (b);
280 cache_pc_function_high = BLOCK_END (b);
281 }
282 else
283 {
284 int i;
285 for (i = 0; i < BLOCK_NRANGES (b); i++)
286 {
287 if (BLOCK_RANGE_START (b, i) <= mapped_pc
288 && mapped_pc < BLOCK_RANGE_END (b, i))
289 {
290 cache_pc_function_low = BLOCK_RANGE_START (b, i);
291 cache_pc_function_high = BLOCK_RANGE_END (b, i);
292 break;
293 }
294 }
295 /* Above loop should exit via the break. */
296 gdb_assert (i < BLOCK_NRANGES (b));
297 }
298
299
300 goto return_cached_value;
301 }
302 }
303
304 /* Not in the normal symbol tables, see if the pc is in a known
305 section. If it's not, then give up. This ensures that anything
306 beyond the end of the text seg doesn't appear to be part of the
307 last function in the text segment. */
308
309 if (!section)
310 msymbol.minsym = NULL;
311
312 /* Must be in the minimal symbol table. */
313 if (msymbol.minsym == NULL)
314 {
315 /* No available symbol. */
316 if (name != NULL)
317 *name = 0;
318 if (address != NULL)
319 *address = 0;
320 if (endaddr != NULL)
321 *endaddr = 0;
322 if (block != nullptr)
323 *block = nullptr;
324 return false;
325 }
326
327 cache_pc_function_low = BMSYMBOL_VALUE_ADDRESS (msymbol);
328 cache_pc_function_name = msymbol.minsym->linkage_name ();
329 cache_pc_function_section = section;
330 cache_pc_function_high = minimal_symbol_upper_bound (msymbol);
331 cache_pc_function_block = nullptr;
332
333 return_cached_value:
334
335 if (address)
336 {
337 if (pc_in_unmapped_range (pc, section))
338 *address = overlay_unmapped_address (cache_pc_function_low, section);
339 else
340 *address = cache_pc_function_low;
341 }
342
343 if (name)
344 *name = cache_pc_function_name;
345
346 if (endaddr)
347 {
348 if (pc_in_unmapped_range (pc, section))
349 {
350 /* Because the high address is actually beyond the end of
351 the function (and therefore possibly beyond the end of
352 the overlay), we must actually convert (high - 1) and
353 then add one to that. */
354
355 *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
356 section);
357 }
358 else
359 *endaddr = cache_pc_function_high;
360 }
361
362 if (block != nullptr)
363 *block = cache_pc_function_block;
364
365 return true;
366 }
367
368 /* See symtab.h. */
369
370 bool
371 find_function_entry_range_from_pc (CORE_ADDR pc, const char **name,
372 CORE_ADDR *address, CORE_ADDR *endaddr)
373 {
374 const struct block *block;
375 bool status = find_pc_partial_function (pc, name, address, endaddr, &block);
376
377 if (status && block != nullptr && !BLOCK_CONTIGUOUS_P (block))
378 {
379 CORE_ADDR entry_pc = BLOCK_ENTRY_PC (block);
380
381 for (int i = 0; i < BLOCK_NRANGES (block); i++)
382 {
383 if (BLOCK_RANGE_START (block, i) <= entry_pc
384 && entry_pc < BLOCK_RANGE_END (block, i))
385 {
386 if (address != nullptr)
387 *address = BLOCK_RANGE_START (block, i);
388
389 if (endaddr != nullptr)
390 *endaddr = BLOCK_RANGE_END (block, i);
391
392 return status;
393 }
394 }
395
396 /* It's an internal error if we exit the above loop without finding
397 the range. */
398 internal_error (__FILE__, __LINE__,
399 _("Entry block not found in find_function_entry_range_from_pc"));
400 }
401
402 return status;
403 }
404
405 /* See symtab.h. */
406
407 struct type *
408 find_function_type (CORE_ADDR pc)
409 {
410 struct symbol *sym = find_pc_function (pc);
411
412 if (sym != NULL && BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym)) == pc)
413 return SYMBOL_TYPE (sym);
414
415 return NULL;
416 }
417
418 /* See symtab.h. */
419
420 struct type *
421 find_gnu_ifunc_target_type (CORE_ADDR resolver_funaddr)
422 {
423 struct type *resolver_type = find_function_type (resolver_funaddr);
424 if (resolver_type != NULL)
425 {
426 /* Get the return type of the resolver. */
427 struct type *resolver_ret_type
428 = check_typedef (TYPE_TARGET_TYPE (resolver_type));
429
430 /* If we found a pointer to function, then the resolved type
431 is the type of the pointed-to function. */
432 if (resolver_ret_type->code () == TYPE_CODE_PTR)
433 {
434 struct type *resolved_type
435 = TYPE_TARGET_TYPE (resolver_ret_type);
436 if (check_typedef (resolved_type)->code () == TYPE_CODE_FUNC)
437 return resolved_type;
438 }
439 }
440
441 return NULL;
442 }
443
444 /* Return the innermost stack frame that is executing inside of BLOCK and is
445 at least as old as the selected frame. Return NULL if there is no
446 such frame. If BLOCK is NULL, just return NULL. */
447
448 struct frame_info *
449 block_innermost_frame (const struct block *block)
450 {
451 struct frame_info *frame;
452
453 if (block == NULL)
454 return NULL;
455
456 frame = get_selected_frame_if_set ();
457 if (frame == NULL)
458 frame = get_current_frame ();
459 while (frame != NULL)
460 {
461 const struct block *frame_block = get_frame_block (frame, NULL);
462 if (frame_block != NULL && contained_in (frame_block, block))
463 return frame;
464
465 frame = get_prev_frame (frame);
466 }
467
468 return NULL;
469 }
This page took 0.04005 seconds and 4 git commands to generate.