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