1 /* Code dealing with blocks for GDB.
3 Copyright (C) 2003-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "dictionary.h"
25 /* Opaque declarations. */
28 struct compunit_symtab
;
29 struct block_namespace_info
;
34 /* Blocks can occupy non-contiguous address ranges. When this occurs,
35 startaddr and endaddr within struct block (still) specify the lowest
36 and highest addresses of all ranges, but each individual range is
37 specified by the addresses in struct blockrange. */
41 blockrange (CORE_ADDR startaddr_
, CORE_ADDR endaddr_
)
42 : startaddr (startaddr_
),
47 /* Lowest address in this range. */
51 /* One past the highest address in the range. */
56 /* Two or more non-contiguous ranges in the same order as that provided
57 via the debug info. */
62 struct blockrange range
[1];
65 /* All of the name-scope contours of the program
66 are represented by `struct block' objects.
67 All of these objects are pointed to by the blockvector.
69 Each block represents one name scope.
70 Each lexical context has its own block.
72 The blockvector begins with some special blocks.
73 The GLOBAL_BLOCK contains all the symbols defined in this compilation
74 whose scope is the entire program linked together.
75 The STATIC_BLOCK contains all the symbols whose scope is the
76 entire compilation excluding other separate compilations.
77 Blocks starting with the FIRST_LOCAL_BLOCK are not special.
79 Each block records a range of core addresses for the code that
80 is in the scope of the block. The STATIC_BLOCK and GLOBAL_BLOCK
81 give, for the range of code, the entire range of code produced
82 by the compilation that the symbol segment belongs to.
84 The blocks appear in the blockvector
85 in order of increasing starting-address,
86 and, within that, in order of decreasing ending-address.
88 This implies that within the body of one function
89 the blocks appear in the order of a depth-first tree walk. */
93 /* Return this block's start address. */
94 CORE_ADDR
start () const
97 /* Set this block's start address. */
98 void set_start (CORE_ADDR start
)
101 /* Return this block's end address. */
102 CORE_ADDR
end () const
105 /* Set this block's end address. */
106 void set_end (CORE_ADDR end
)
109 /* Return this block's function symbol. */
110 symbol
*function () const
111 { return m_function
; }
113 /* Set this block's function symbol. */
114 void set_function (symbol
*function
)
115 { m_function
= function
; }
117 /* Addresses in the executable code that are in this block. */
122 /* The symbol that names this block, if the block is the body of a
123 function (real or inlined); otherwise, zero. */
125 struct symbol
*m_function
;
127 /* The `struct block' for the containing block, or 0 if none.
129 The superblock of a top-level local block (i.e. a function in the
130 case of C) is the STATIC_BLOCK. The superblock of the
131 STATIC_BLOCK is the GLOBAL_BLOCK. */
133 const struct block
*superblock
;
135 /* This is used to store the symbols in the block. */
137 struct multidictionary
*multidict
;
139 /* Contains information about namespace-related info relevant to this block:
140 using directives and the current namespace scope. */
142 struct block_namespace_info
*namespace_info
;
144 /* Address ranges for blocks with non-contiguous ranges. If this
145 is NULL, then there is only one range which is specified by
146 startaddr and endaddr above. */
148 struct blockranges
*ranges
;
151 /* The global block is singled out so that we can provide a back-link
152 to the compunit symtab. */
160 /* This holds a pointer to the compunit symtab holding this block. */
162 struct compunit_symtab
*compunit_symtab
;
165 #define BLOCK_SUPERBLOCK(bl) (bl)->superblock
166 #define BLOCK_MULTIDICT(bl) (bl)->multidict
167 #define BLOCK_NAMESPACE(bl) (bl)->namespace_info
169 /* Accessor for ranges field within block BL. */
171 #define BLOCK_RANGES(bl) (bl)->ranges
173 /* Number of ranges within a block. */
175 #define BLOCK_NRANGES(bl) (bl)->ranges->nranges
177 /* Access range array for block BL. */
179 #define BLOCK_RANGE(bl) (bl)->ranges->range
181 /* Are all addresses within a block contiguous? */
183 #define BLOCK_CONTIGUOUS_P(bl) (BLOCK_RANGES (bl) == nullptr \
184 || BLOCK_NRANGES (bl) <= 1)
186 /* Obtain the start address of the Nth range for block BL. */
188 #define BLOCK_RANGE_START(bl,n) (BLOCK_RANGE (bl)[n].startaddr)
190 /* Obtain the end address of the Nth range for block BL. */
192 #define BLOCK_RANGE_END(bl,n) (BLOCK_RANGE (bl)[n].endaddr)
194 /* Define the "entry pc" for a block BL to be the lowest (start) address
195 for the block when all addresses within the block are contiguous. If
196 non-contiguous, then use the start address for the first range in the
199 At the moment, this almost matches what DWARF specifies as the entry
200 pc. (The missing bit is support for DW_AT_entry_pc which should be
201 preferred over range data and the low_pc.)
203 Once support for DW_AT_entry_pc is added, I expect that an entry_pc
204 field will be added to one of these data structures. Once that's done,
205 the entry_pc field can be set from the dwarf reader (and other readers
206 too). BLOCK_ENTRY_PC can then be redefined to be less DWARF-centric. */
208 #define BLOCK_ENTRY_PC(bl) (BLOCK_CONTIGUOUS_P (bl) \
210 : BLOCK_RANGE_START (bl,0))
214 /* Number of blocks in the list. */
216 /* An address map mapping addresses to blocks in this blockvector.
217 This pointer is zero if the blocks' start and end addresses are
220 /* The blocks themselves. */
221 struct block
*block
[1];
224 #define BLOCKVECTOR_NBLOCKS(blocklist) (blocklist)->nblocks
225 #define BLOCKVECTOR_BLOCK(blocklist,n) (blocklist)->block[n]
226 #define BLOCKVECTOR_MAP(blocklist) ((blocklist)->map)
228 /* Return the objfile of BLOCK, which must be non-NULL. */
230 extern struct objfile
*block_objfile (const struct block
*block
);
232 /* Return the architecture of BLOCK, which must be non-NULL. */
234 extern struct gdbarch
*block_gdbarch (const struct block
*block
);
236 extern struct symbol
*block_linkage_function (const struct block
*);
238 extern struct symbol
*block_containing_function (const struct block
*);
240 extern int block_inlined_p (const struct block
*block
);
242 /* Return true if block A is lexically nested within block B, or if a
243 and b have the same pc range. Return false otherwise. If
244 ALLOW_NESTED is true, then block A is considered to be in block B
245 if A is in a nested function in B's function. If ALLOW_NESTED is
246 false (the default), then blocks in nested functions are not
247 considered to be contained. */
249 extern bool contained_in (const struct block
*a
, const struct block
*b
,
250 bool allow_nested
= false);
252 extern const struct blockvector
*blockvector_for_pc (CORE_ADDR
,
253 const struct block
**);
255 extern const struct blockvector
*
256 blockvector_for_pc_sect (CORE_ADDR
, struct obj_section
*,
257 const struct block
**, struct compunit_symtab
*);
259 extern int blockvector_contains_pc (const struct blockvector
*bv
, CORE_ADDR pc
);
261 extern struct call_site
*call_site_for_pc (struct gdbarch
*gdbarch
,
264 extern const struct block
*block_for_pc (CORE_ADDR
);
266 extern const struct block
*block_for_pc_sect (CORE_ADDR
, struct obj_section
*);
268 extern const char *block_scope (const struct block
*block
);
270 extern void block_set_scope (struct block
*block
, const char *scope
,
271 struct obstack
*obstack
);
273 extern struct using_direct
*block_using (const struct block
*block
);
275 extern void block_set_using (struct block
*block
,
276 struct using_direct
*using_decl
,
277 struct obstack
*obstack
);
279 extern const struct block
*block_static_block (const struct block
*block
);
281 extern const struct block
*block_global_block (const struct block
*block
);
283 extern struct block
*allocate_block (struct obstack
*obstack
);
285 extern struct block
*allocate_global_block (struct obstack
*obstack
);
287 extern void set_block_compunit_symtab (struct block
*,
288 struct compunit_symtab
*);
290 /* Return a property to evaluate the static link associated to BLOCK.
292 In the context of nested functions (available in Pascal, Ada and GNU C, for
293 instance), a static link (as in DWARF's DW_AT_static_link attribute) for a
294 function is a way to get the frame corresponding to the enclosing function.
296 Note that only objfile-owned and function-level blocks can have a static
297 link. Return NULL if there is no such property. */
299 extern struct dynamic_prop
*block_static_link (const struct block
*block
);
301 /* A block iterator. This structure should be treated as though it
302 were opaque; it is only defined here because we want to support
303 stack allocation of iterators. */
305 struct block_iterator
307 /* If we're iterating over a single block, this holds the block.
308 Otherwise, it holds the canonical compunit. */
312 struct compunit_symtab
*compunit_symtab
;
313 const struct block
*block
;
316 /* If we're iterating over a single block, this is always -1.
317 Otherwise, it holds the index of the current "included" symtab in
318 the canonical symtab (that is, d.symtab->includes[idx]), with -1
319 meaning the canonical symtab itself. */
323 /* Which block, either static or global, to iterate over. If this
324 is FIRST_LOCAL_BLOCK, then we are iterating over a single block.
325 This is used to select which field of 'd' is in use. */
327 enum block_enum which
;
329 /* The underlying multidictionary iterator. */
331 struct mdict_iterator mdict_iter
;
334 /* Initialize ITERATOR to point at the first symbol in BLOCK, and
335 return that first symbol, or NULL if BLOCK is empty. */
337 extern struct symbol
*block_iterator_first (const struct block
*block
,
338 struct block_iterator
*iterator
);
340 /* Advance ITERATOR, and return the next symbol, or NULL if there are
341 no more symbols. Don't call this if you've previously received
342 NULL from block_iterator_first or block_iterator_next on this
345 extern struct symbol
*block_iterator_next (struct block_iterator
*iterator
);
347 /* Initialize ITERATOR to point at the first symbol in BLOCK whose
348 search_name () matches NAME, and return that first symbol, or
349 NULL if there are no such symbols. */
351 extern struct symbol
*block_iter_match_first (const struct block
*block
,
352 const lookup_name_info
&name
,
353 struct block_iterator
*iterator
);
355 /* Advance ITERATOR to point at the next symbol in BLOCK whose
356 search_name () matches NAME, or NULL if there are no more such
357 symbols. Don't call this if you've previously received NULL from
358 block_iterator_match_first or block_iterator_match_next on this
359 iteration. And don't call it unless ITERATOR was created by a
360 previous call to block_iter_match_first with the same NAME. */
362 extern struct symbol
*block_iter_match_next
363 (const lookup_name_info
&name
, struct block_iterator
*iterator
);
365 /* Return true if symbol A is the best match possible for DOMAIN. */
367 extern bool best_symbol (struct symbol
*a
, const domain_enum domain
);
369 /* Return symbol B if it is a better match than symbol A for DOMAIN.
370 Otherwise return A. */
372 extern struct symbol
*better_symbol (struct symbol
*a
, struct symbol
*b
,
373 const domain_enum domain
);
375 /* Search BLOCK for symbol NAME in DOMAIN. */
377 extern struct symbol
*block_lookup_symbol (const struct block
*block
,
379 symbol_name_match_type match_type
,
380 const domain_enum domain
);
382 /* Search BLOCK for symbol NAME in DOMAIN but only in primary symbol table of
383 BLOCK. BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. Function is useful if
384 one iterates all global/static blocks of an objfile. */
386 extern struct symbol
*block_lookup_symbol_primary (const struct block
*block
,
388 const domain_enum domain
);
390 /* The type of the MATCHER argument to block_find_symbol. */
392 typedef int (block_symbol_matcher_ftype
) (struct symbol
*, void *);
394 /* Find symbol NAME in BLOCK and in DOMAIN that satisfies MATCHER.
395 DATA is passed unchanged to MATCHER.
396 BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. */
398 extern struct symbol
*block_find_symbol (const struct block
*block
,
400 const domain_enum domain
,
401 block_symbol_matcher_ftype
*matcher
,
404 /* A matcher function for block_find_symbol to find only symbols with
407 extern int block_find_non_opaque_type (struct symbol
*sym
, void *data
);
409 /* A matcher function for block_find_symbol to prefer symbols with
410 non-opaque types. The way to use this function is as follows:
412 struct symbol *with_opaque = NULL;
414 = block_find_symbol (block, name, domain,
415 block_find_non_opaque_type_preferred, &with_opaque);
417 At this point if SYM is non-NULL then a non-opaque type has been found.
418 Otherwise, if WITH_OPAQUE is non-NULL then an opaque type has been found.
419 Otherwise, the symbol was not found. */
421 extern int block_find_non_opaque_type_preferred (struct symbol
*sym
,
424 /* Macro to loop through all symbols in BLOCK, in no particular
425 order. ITER helps keep track of the iteration, and must be a
426 struct block_iterator. SYM points to the current symbol. */
428 #define ALL_BLOCK_SYMBOLS(block, iter, sym) \
429 for ((sym) = block_iterator_first ((block), &(iter)); \
431 (sym) = block_iterator_next (&(iter)))
433 /* Macro to loop through all symbols in BLOCK with a name that matches
434 NAME, in no particular order. ITER helps keep track of the
435 iteration, and must be a struct block_iterator. SYM points to the
438 #define ALL_BLOCK_SYMBOLS_WITH_NAME(block, name, iter, sym) \
439 for ((sym) = block_iter_match_first ((block), (name), &(iter)); \
441 (sym) = block_iter_match_next ((name), &(iter)))
443 /* Given a vector of pairs, allocate and build an obstack allocated
444 blockranges struct for a block. */
445 struct blockranges
*make_blockranges (struct objfile
*objfile
,
446 const std::vector
<blockrange
> &rangevec
);