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"
24 #include "gdbsupport/array-view.h"
26 /* Opaque declarations. */
29 struct compunit_symtab
;
30 struct block_namespace_info
;
35 /* Blocks can occupy non-contiguous address ranges. When this occurs,
36 startaddr and endaddr within struct block (still) specify the lowest
37 and highest addresses of all ranges, but each individual range is
38 specified by the addresses in struct blockrange. */
42 blockrange (CORE_ADDR start
, CORE_ADDR end
)
48 /* Return this blockrange's start address. */
49 CORE_ADDR
start () const
52 /* Set this blockrange's start address. */
53 void set_start (CORE_ADDR start
)
56 /* Return this blockrange's end address. */
57 CORE_ADDR
end () const
60 /* Set this blockrange's end address. */
61 void set_end (CORE_ADDR end
)
64 /* Lowest address in this range. */
68 /* One past the highest address in the range. */
73 /* Two or more non-contiguous ranges in the same order as that provided
74 via the debug info. */
79 struct blockrange range
[1];
82 /* All of the name-scope contours of the program
83 are represented by `struct block' objects.
84 All of these objects are pointed to by the blockvector.
86 Each block represents one name scope.
87 Each lexical context has its own block.
89 The blockvector begins with some special blocks.
90 The GLOBAL_BLOCK contains all the symbols defined in this compilation
91 whose scope is the entire program linked together.
92 The STATIC_BLOCK contains all the symbols whose scope is the
93 entire compilation excluding other separate compilations.
94 Blocks starting with the FIRST_LOCAL_BLOCK are not special.
96 Each block records a range of core addresses for the code that
97 is in the scope of the block. The STATIC_BLOCK and GLOBAL_BLOCK
98 give, for the range of code, the entire range of code produced
99 by the compilation that the symbol segment belongs to.
101 The blocks appear in the blockvector
102 in order of increasing starting-address,
103 and, within that, in order of decreasing ending-address.
105 This implies that within the body of one function
106 the blocks appear in the order of a depth-first tree walk. */
110 /* Return this block's start address. */
111 CORE_ADDR
start () const
114 /* Set this block's start address. */
115 void set_start (CORE_ADDR start
)
118 /* Return this block's end address. */
119 CORE_ADDR
end () const
122 /* Set this block's end address. */
123 void set_end (CORE_ADDR end
)
126 /* Return this block's function symbol. */
127 symbol
*function () const
128 { return m_function
; }
130 /* Set this block's function symbol. */
131 void set_function (symbol
*function
)
132 { m_function
= function
; }
134 /* Return this block's superblock. */
135 const block
*superblock () const
136 { return m_superblock
; }
138 /* Set this block's superblock. */
139 void set_superblock (const block
*superblock
)
140 { m_superblock
= superblock
; }
142 /* Return this block's multidict. */
143 multidictionary
*multidict () const
144 { return m_multidict
; }
146 /* Set this block's multidict. */
147 void set_multidict (multidictionary
*multidict
)
148 { m_multidict
= multidict
; }
150 /* Return this block's namespace info. */
151 block_namespace_info
*namespace_info () const
152 { return m_namespace_info
; }
154 /* Set this block's namespace info. */
155 void set_namespace_info (block_namespace_info
*namespace_info
)
156 { m_namespace_info
= namespace_info
; }
158 /* Return a view on this block's ranges. */
159 gdb::array_view
<blockrange
> ranges ()
160 { return gdb::make_array_view (m_ranges
->range
, m_ranges
->nranges
); }
162 /* Const version of the above. */
163 gdb::array_view
<const blockrange
> ranges () const
164 { return gdb::make_array_view (m_ranges
->range
, m_ranges
->nranges
); }
166 /* Set this block's ranges array. */
167 void set_ranges (blockranges
*ranges
)
168 { m_ranges
= ranges
; }
170 /* Return true if all addresses within this block are contiguous. */
171 bool is_contiguous () const
172 { return this->ranges ().size () <= 1; }
174 /* Return the "entry PC" of this block.
176 The entry PC is the lowest (start) address for the block when all addresses
177 within the block are contiguous. If non-contiguous, then use the start
178 address for the first range in the block.
180 At the moment, this almost matches what DWARF specifies as the entry
181 pc. (The missing bit is support for DW_AT_entry_pc which should be
182 preferred over range data and the low_pc.)
184 Once support for DW_AT_entry_pc is added, I expect that an entry_pc
185 field will be added to one of these data structures. Once that's done,
186 the entry_pc field can be set from the dwarf reader (and other readers
187 too). ENTRY_PC can then be redefined to be less DWARF-centric. */
189 CORE_ADDR
entry_pc () const
191 if (this->is_contiguous ())
192 return this->start ();
194 return this->ranges ()[0].start ();
197 /* Addresses in the executable code that are in this block. */
202 /* The symbol that names this block, if the block is the body of a
203 function (real or inlined); otherwise, zero. */
205 struct symbol
*m_function
;
207 /* The `struct block' for the containing block, or 0 if none.
209 The superblock of a top-level local block (i.e. a function in the
210 case of C) is the STATIC_BLOCK. The superblock of the
211 STATIC_BLOCK is the GLOBAL_BLOCK. */
213 const struct block
*m_superblock
;
215 /* This is used to store the symbols in the block. */
217 struct multidictionary
*m_multidict
;
219 /* Contains information about namespace-related info relevant to this block:
220 using directives and the current namespace scope. */
222 struct block_namespace_info
*m_namespace_info
;
224 /* Address ranges for blocks with non-contiguous ranges. If this
225 is NULL, then there is only one range which is specified by
226 startaddr and endaddr above. */
228 struct blockranges
*m_ranges
;
231 /* The global block is singled out so that we can provide a back-link
232 to the compunit symtab. */
240 /* This holds a pointer to the compunit symtab holding this block. */
242 struct compunit_symtab
*compunit_symtab
;
247 /* Return a view on the blocks of this blockvector. */
248 gdb::array_view
<struct block
*> blocks ()
250 return gdb::array_view
<struct block
*> (m_blocks
, m_num_blocks
);
253 /* Const version of the above. */
254 gdb::array_view
<const struct block
*const> blocks () const
256 const struct block
**blocks
= (const struct block
**) m_blocks
;
257 return gdb::array_view
<const struct block
*const> (blocks
, m_num_blocks
);
260 /* Return the block at index I. */
261 struct block
*block (size_t i
)
262 { return this->blocks ()[i
]; }
264 /* Const version of the above. */
265 const struct block
*block (size_t i
) const
266 { return this->blocks ()[i
]; }
268 /* Set the block at index I. */
269 void set_block (int i
, struct block
*block
)
270 { m_blocks
[i
] = block
; }
272 /* Set the number of blocks of this blockvector.
274 The storage of blocks is done using a flexible array member, so the number
275 of blocks set here must agree with what was effectively allocated. */
276 void set_num_blocks (int num_blocks
)
277 { m_num_blocks
= num_blocks
; }
279 /* Return the number of blocks in this blockvector. */
280 int num_blocks () const
281 { return m_num_blocks
; }
283 /* Return the global block of this blockvector. */
284 struct block
*global_block ()
285 { return this->block (GLOBAL_BLOCK
); }
287 /* Const version of the above. */
288 const struct block
*global_block () const
289 { return this->block (GLOBAL_BLOCK
); }
291 /* Return the static block of this blockvector. */
292 struct block
*static_block ()
293 { return this->block (STATIC_BLOCK
); }
295 /* Const version of the above. */
296 const struct block
*static_block () const
297 { return this->block (STATIC_BLOCK
); }
299 /* An address map mapping addresses to blocks in this blockvector.
300 This pointer is zero if the blocks' start and end addresses are
305 /* Number of blocks in the list. */
308 /* The blocks themselves. */
309 struct block
*m_blocks
[1];
312 #define BLOCKVECTOR_MAP(blocklist) ((blocklist)->map)
314 /* Return the objfile of BLOCK, which must be non-NULL. */
316 extern struct objfile
*block_objfile (const struct block
*block
);
318 /* Return the architecture of BLOCK, which must be non-NULL. */
320 extern struct gdbarch
*block_gdbarch (const struct block
*block
);
322 extern struct symbol
*block_linkage_function (const struct block
*);
324 extern struct symbol
*block_containing_function (const struct block
*);
326 extern int block_inlined_p (const struct block
*block
);
328 /* Return true if block A is lexically nested within block B, or if a
329 and b have the same pc range. Return false otherwise. If
330 ALLOW_NESTED is true, then block A is considered to be in block B
331 if A is in a nested function in B's function. If ALLOW_NESTED is
332 false (the default), then blocks in nested functions are not
333 considered to be contained. */
335 extern bool contained_in (const struct block
*a
, const struct block
*b
,
336 bool allow_nested
= false);
338 extern const struct blockvector
*blockvector_for_pc (CORE_ADDR
,
339 const struct block
**);
341 extern const struct blockvector
*
342 blockvector_for_pc_sect (CORE_ADDR
, struct obj_section
*,
343 const struct block
**, struct compunit_symtab
*);
345 extern int blockvector_contains_pc (const struct blockvector
*bv
, CORE_ADDR pc
);
347 extern struct call_site
*call_site_for_pc (struct gdbarch
*gdbarch
,
350 extern const struct block
*block_for_pc (CORE_ADDR
);
352 extern const struct block
*block_for_pc_sect (CORE_ADDR
, struct obj_section
*);
354 extern const char *block_scope (const struct block
*block
);
356 extern void block_set_scope (struct block
*block
, const char *scope
,
357 struct obstack
*obstack
);
359 extern struct using_direct
*block_using (const struct block
*block
);
361 extern void block_set_using (struct block
*block
,
362 struct using_direct
*using_decl
,
363 struct obstack
*obstack
);
365 extern const struct block
*block_static_block (const struct block
*block
);
367 extern const struct block
*block_global_block (const struct block
*block
);
369 extern struct block
*allocate_block (struct obstack
*obstack
);
371 extern struct block
*allocate_global_block (struct obstack
*obstack
);
373 extern void set_block_compunit_symtab (struct block
*,
374 struct compunit_symtab
*);
376 /* Return a property to evaluate the static link associated to BLOCK.
378 In the context of nested functions (available in Pascal, Ada and GNU C, for
379 instance), a static link (as in DWARF's DW_AT_static_link attribute) for a
380 function is a way to get the frame corresponding to the enclosing function.
382 Note that only objfile-owned and function-level blocks can have a static
383 link. Return NULL if there is no such property. */
385 extern struct dynamic_prop
*block_static_link (const struct block
*block
);
387 /* A block iterator. This structure should be treated as though it
388 were opaque; it is only defined here because we want to support
389 stack allocation of iterators. */
391 struct block_iterator
393 /* If we're iterating over a single block, this holds the block.
394 Otherwise, it holds the canonical compunit. */
398 struct compunit_symtab
*compunit_symtab
;
399 const struct block
*block
;
402 /* If we're iterating over a single block, this is always -1.
403 Otherwise, it holds the index of the current "included" symtab in
404 the canonical symtab (that is, d.symtab->includes[idx]), with -1
405 meaning the canonical symtab itself. */
409 /* Which block, either static or global, to iterate over. If this
410 is FIRST_LOCAL_BLOCK, then we are iterating over a single block.
411 This is used to select which field of 'd' is in use. */
413 enum block_enum which
;
415 /* The underlying multidictionary iterator. */
417 struct mdict_iterator mdict_iter
;
420 /* Initialize ITERATOR to point at the first symbol in BLOCK, and
421 return that first symbol, or NULL if BLOCK is empty. */
423 extern struct symbol
*block_iterator_first (const struct block
*block
,
424 struct block_iterator
*iterator
);
426 /* Advance ITERATOR, and return the next symbol, or NULL if there are
427 no more symbols. Don't call this if you've previously received
428 NULL from block_iterator_first or block_iterator_next on this
431 extern struct symbol
*block_iterator_next (struct block_iterator
*iterator
);
433 /* Initialize ITERATOR to point at the first symbol in BLOCK whose
434 search_name () matches NAME, and return that first symbol, or
435 NULL if there are no such symbols. */
437 extern struct symbol
*block_iter_match_first (const struct block
*block
,
438 const lookup_name_info
&name
,
439 struct block_iterator
*iterator
);
441 /* Advance ITERATOR to point at the next symbol in BLOCK whose
442 search_name () matches NAME, or NULL if there are no more such
443 symbols. Don't call this if you've previously received NULL from
444 block_iterator_match_first or block_iterator_match_next on this
445 iteration. And don't call it unless ITERATOR was created by a
446 previous call to block_iter_match_first with the same NAME. */
448 extern struct symbol
*block_iter_match_next
449 (const lookup_name_info
&name
, struct block_iterator
*iterator
);
451 /* Return true if symbol A is the best match possible for DOMAIN. */
453 extern bool best_symbol (struct symbol
*a
, const domain_enum domain
);
455 /* Return symbol B if it is a better match than symbol A for DOMAIN.
456 Otherwise return A. */
458 extern struct symbol
*better_symbol (struct symbol
*a
, struct symbol
*b
,
459 const domain_enum domain
);
461 /* Search BLOCK for symbol NAME in DOMAIN. */
463 extern struct symbol
*block_lookup_symbol (const struct block
*block
,
465 symbol_name_match_type match_type
,
466 const domain_enum domain
);
468 /* Search BLOCK for symbol NAME in DOMAIN but only in primary symbol table of
469 BLOCK. BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. Function is useful if
470 one iterates all global/static blocks of an objfile. */
472 extern struct symbol
*block_lookup_symbol_primary (const struct block
*block
,
474 const domain_enum domain
);
476 /* The type of the MATCHER argument to block_find_symbol. */
478 typedef int (block_symbol_matcher_ftype
) (struct symbol
*, void *);
480 /* Find symbol NAME in BLOCK and in DOMAIN that satisfies MATCHER.
481 DATA is passed unchanged to MATCHER.
482 BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. */
484 extern struct symbol
*block_find_symbol (const struct block
*block
,
486 const domain_enum domain
,
487 block_symbol_matcher_ftype
*matcher
,
490 /* A matcher function for block_find_symbol to find only symbols with
493 extern int block_find_non_opaque_type (struct symbol
*sym
, void *data
);
495 /* A matcher function for block_find_symbol to prefer symbols with
496 non-opaque types. The way to use this function is as follows:
498 struct symbol *with_opaque = NULL;
500 = block_find_symbol (block, name, domain,
501 block_find_non_opaque_type_preferred, &with_opaque);
503 At this point if SYM is non-NULL then a non-opaque type has been found.
504 Otherwise, if WITH_OPAQUE is non-NULL then an opaque type has been found.
505 Otherwise, the symbol was not found. */
507 extern int block_find_non_opaque_type_preferred (struct symbol
*sym
,
510 /* Macro to loop through all symbols in BLOCK, in no particular
511 order. ITER helps keep track of the iteration, and must be a
512 struct block_iterator. SYM points to the current symbol. */
514 #define ALL_BLOCK_SYMBOLS(block, iter, sym) \
515 for ((sym) = block_iterator_first ((block), &(iter)); \
517 (sym) = block_iterator_next (&(iter)))
519 /* Macro to loop through all symbols in BLOCK with a name that matches
520 NAME, in no particular order. ITER helps keep track of the
521 iteration, and must be a struct block_iterator. SYM points to the
524 #define ALL_BLOCK_SYMBOLS_WITH_NAME(block, name, iter, sym) \
525 for ((sym) = block_iter_match_first ((block), (name), &(iter)); \
527 (sym) = block_iter_match_next ((name), &(iter)))
529 /* Given a vector of pairs, allocate and build an obstack allocated
530 blockranges struct for a block. */
531 struct blockranges
*make_blockranges (struct objfile
*objfile
,
532 const std::vector
<blockrange
> &rangevec
);