GDB (xrefs)
/tmp/gdb-8.1/gdb/blockframe.c
Go to the documentation of this file.
1 /* Get info from stack frames; convert between frames, blocks,
2  functions and pc values.
3 
4  Copyright (C) 1986-2018 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 
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 
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  {
99  return BLOCK_START (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 *
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 
131 
132 /* Return the function containing pc value PC in section SECTION.
133  Returns 0 if function is not known. */
134 
135 struct symbol *
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 *
151 {
153 }
154 
155 /* These variables are used to cache the most recent result
156  of find_pc_partial_function. */
157 
160 static const char *cache_pc_function_name = 0;
163 
164 /* Clear cache, e.g. when symbol table is discarded. */
165 
166 void
168 {
171  cache_pc_function_name = (char *) 0;
174 }
175 
176 /* Finds the "function" (text symbol) that is smaller than PC but
177  greatest of all of the potential text symbols in SECTION. Sets
178  *NAME and/or *ADDRESS conditionally if that pointer is non-null.
179  If ENDADDR is non-null, then set *ENDADDR to be the end of the
180  function (exclusive), but passing ENDADDR as non-null means that
181  the function might cause symbols to be read. If IS_GNU_IFUNC_P is provided
182  *IS_GNU_IFUNC_P is set to 1 on return if the function is STT_GNU_IFUNC.
183  This function either succeeds or fails (not halfway succeeds). If it
184  succeeds, it sets *NAME, *ADDRESS, and *ENDADDR to real information and
185  returns 1. If it fails, it sets *NAME, *ADDRESS, *ENDADDR and
186  *IS_GNU_IFUNC_P to zero and returns 0. */
187 
188 /* Backward compatibility, no section argument. */
189 
190 int
192  CORE_ADDR *address, CORE_ADDR *endaddr,
193  int *is_gnu_ifunc_p)
194 {
195  struct obj_section *section;
196  struct symbol *f;
197  struct bound_minimal_symbol msymbol;
198  struct compunit_symtab *compunit_symtab = NULL;
199  struct objfile *objfile;
200  CORE_ADDR mapped_pc;
201 
202  /* To ensure that the symbol returned belongs to the correct setion
203  (and that the last [random] symbol from the previous section
204  isn't returned) try to find the section containing PC. First try
205  the overlay code (which by default returns NULL); and second try
206  the normal section code (which almost always succeeds). */
207  section = find_pc_overlay (pc);
208  if (section == NULL)
209  section = find_pc_section (pc);
210 
211  mapped_pc = overlay_mapped_address (pc, section);
212 
213  if (mapped_pc >= cache_pc_function_low
214  && mapped_pc < cache_pc_function_high
215  && section == cache_pc_function_section)
216  goto return_cached_value;
217 
218  msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
220  {
221  if (objfile->sf)
222  {
225  mapped_pc, section,
226  0);
227  }
228  if (compunit_symtab != NULL)
229  break;
230  }
231 
232  if (compunit_symtab != NULL)
233  {
234  /* Checking whether the msymbol has a larger value is for the
235  "pathological" case mentioned in print_frame_info. */
236  f = find_pc_sect_function (mapped_pc, section);
237  if (f != NULL
238  && (msymbol.minsym == NULL
240  >= BMSYMBOL_VALUE_ADDRESS (msymbol))))
241  {
245  cache_pc_function_section = section;
247  goto return_cached_value;
248  }
249  }
250 
251  /* Not in the normal symbol tables, see if the pc is in a known
252  section. If it's not, then give up. This ensures that anything
253  beyond the end of the text seg doesn't appear to be part of the
254  last function in the text segment. */
255 
256  if (!section)
257  msymbol.minsym = NULL;
258 
259  /* Must be in the minimal symbol table. */
260  if (msymbol.minsym == NULL)
261  {
262  /* No available symbol. */
263  if (name != NULL)
264  *name = 0;
265  if (address != NULL)
266  *address = 0;
267  if (endaddr != NULL)
268  *endaddr = 0;
269  if (is_gnu_ifunc_p != NULL)
270  *is_gnu_ifunc_p = 0;
271  return 0;
272  }
273 
276  cache_pc_function_section = section;
278  == mst_text_gnu_ifunc);
280 
281  return_cached_value:
282 
283  if (address)
284  {
285  if (pc_in_unmapped_range (pc, section))
286  *address = overlay_unmapped_address (cache_pc_function_low, section);
287  else
288  *address = cache_pc_function_low;
289  }
290 
291  if (name)
293 
294  if (endaddr)
295  {
296  if (pc_in_unmapped_range (pc, section))
297  {
298  /* Because the high address is actually beyond the end of
299  the function (and therefore possibly beyond the end of
300  the overlay), we must actually convert (high - 1) and
301  then add one to that. */
302 
304  section);
305  }
306  else
307  *endaddr = cache_pc_function_high;
308  }
309 
310  if (is_gnu_ifunc_p)
311  *is_gnu_ifunc_p = cache_pc_function_is_gnu_ifunc;
312 
313  return 1;
314 }
315 
316 /* See find_pc_partial_function_gnu_ifunc, only the IS_GNU_IFUNC_P parameter
317  is omitted here for backward API compatibility. */
318 
319 int
320 find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
321  CORE_ADDR *endaddr)
322 {
323  return find_pc_partial_function_gnu_ifunc (pc, name, address, endaddr, NULL);
324 }
325 
326 /* Return the innermost stack frame that is executing inside of BLOCK and is
327  at least as old as the selected frame. Return NULL if there is no
328  such frame. If BLOCK is NULL, just return NULL. */
329 
330 struct frame_info *
332 {
333  struct frame_info *frame;
334 
335  if (block == NULL)
336  return NULL;
337 
338  frame = get_selected_frame_if_set ();
339  if (frame == NULL)
340  frame = get_current_frame ();
341  while (frame != NULL)
342  {
343  const struct block *frame_block = get_frame_block (frame, NULL);
344  if (frame_block != NULL && contained_in (frame_block, block))
345  return frame;
346 
347  frame = get_prev_frame (frame);
348  }
349 
350  return NULL;
351 }
int get_frame_address_in_block_if_available(struct frame_info *this_frame, CORE_ADDR *pc)
Definition: frame.c:2467
struct bound_minimal_symbol lookup_minimal_symbol_by_pc_section(CORE_ADDR pc, struct obj_section *section)
Definition: minsyms.c:906
static struct obj_section * cache_pc_function_section
Definition: blockframe.c:161
#define MSYMBOL_LINKAGE_NAME(symbol)
Definition: symtab.h:707
struct frame_info * get_current_frame(void)
Definition: frame.c:1563
bfd_vma CORE_ADDR
Definition: common-types.h:41
static int cache_pc_function_is_gnu_ifunc
Definition: blockframe.c:162
struct frame_info * get_prev_frame(struct frame_info *this_frame)
Definition: frame.c:2254
#define BMSYMBOL_VALUE_ADDRESS(symbol)
Definition: symtab.h:691
struct symbol * find_pc_sect_function(CORE_ADDR pc, struct obj_section *section)
Definition: blockframe.c:136
CORE_ADDR minimal_symbol_upper_bound(struct bound_minimal_symbol minsym)
Definition: minsyms.c:1532
struct compunit_symtab *(* find_pc_sect_compunit_symtab)(struct objfile *objfile, struct bound_minimal_symbol msymbol, CORE_ADDR pc, struct obj_section *section, int warn_if_readin)
Definition: symfile.h:270
struct symbol * block_linkage_function(const struct block *bl)
Definition: block.c:100
#define BLOCK_START(bl)
Definition: block.h:105
const struct block * block_for_pc(CORE_ADDR pc)
Definition: block.c:282
struct obj_section * find_pc_overlay(CORE_ADDR pc)
Definition: symfile.c:3173
#define BLOCK_FUNCTION(bl)
Definition: block.h:107
static const char * cache_pc_function_name
Definition: blockframe.c:160
const char *const name
Definition: aarch64-tdep.c:76
#define TYPE_GNU_IFUNC(t)
Definition: gdbtypes.h:283
struct frame_info * block_innermost_frame(const struct block *block)
Definition: blockframe.c:331
int contained_in(const struct block *a, const struct block *b)
Definition: block.c:73
static CORE_ADDR cache_pc_function_low
Definition: blockframe.c:158
objfile(bfd *, const char *, objfile_flags)
Definition: objfiles.c:373
struct symbol * find_pc_function(CORE_ADDR pc)
Definition: blockframe.c:150
const struct block * get_frame_block(struct frame_info *frame, CORE_ADDR *addr_in_block)
Definition: blockframe.c:55
const struct sym_fns * sf
Definition: objfiles.h:381
struct symbol * get_frame_function(struct frame_info *frame)
Definition: blockframe.c:118
struct frame_info * get_selected_frame_if_set(void)
Definition: frame.c:1657
int find_pc_partial_function(CORE_ADDR pc, const char **name, CORE_ADDR *address, CORE_ADDR *endaddr)
Definition: blockframe.c:320
#define BLOCK_SUPERBLOCK(bl)
Definition: block.h:108
#define SYMBOL_LINKAGE_NAME(symbol)
Definition: symtab.h:523
#define BLOCK_END(bl)
Definition: block.h:106
int find_pc_partial_function_gnu_ifunc(CORE_ADDR pc, const char **name, CORE_ADDR *address, CORE_ADDR *endaddr, int *is_gnu_ifunc_p)
Definition: blockframe.c:191
struct obj_section * find_pc_section(CORE_ADDR pc)
Definition: objfiles.c:1395
static CORE_ADDR cache_pc_function_high
Definition: blockframe.c:159
const struct block * block_for_pc_sect(CORE_ADDR pc, struct obj_section *section)
Definition: block.c:267
#define gdb_assert(expr)
Definition: gdb_assert.h:32
Definition: block.h:60
void clear_pc_function_cache(void)
Definition: blockframe.c:167
int frame_inlined_callees(struct frame_info *this_frame)
Definition: inline-frame.c:396
int block_inlined_p(const struct block *bl)
Definition: block.c:126
#define MSYMBOL_TYPE(msymbol)
Definition: symtab.h:680
struct obj_section * find_pc_mapped_section(CORE_ADDR pc)
Definition: symfile.c:3202
CORE_ADDR overlay_unmapped_address(CORE_ADDR pc, struct obj_section *section)
Definition: symfile.c:3107
struct bound_minimal_symbol lookup_minimal_symbol_by_pc(CORE_ADDR pc)
Definition: minsyms.c:928
#define SYMBOL_BLOCK_VALUE(symbol)
Definition: symtab.h:467
struct minimal_symbol * minsym
Definition: minsyms.h:34
#define SYMBOL_TYPE(symbol)
Definition: symtab.h:1161
CORE_ADDR overlay_mapped_address(CORE_ADDR pc, struct obj_section *section)
Definition: symfile.c:3125
CORE_ADDR get_pc_function_start(CORE_ADDR pc)
Definition: blockframe.c:86
#define ALL_OBJFILES(obj)
Definition: objfiles.h:582
const struct quick_symbol_functions * qf
Definition: symfile.h:369
CORE_ADDR pc_in_unmapped_range(CORE_ADDR pc, struct obj_section *section)
Definition: symfile.c:3053