1 /* Code dealing with blocks for GDB.
3 Copyright (C) 2003-2023 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. */
108 struct block
: public allocate_on_obstack
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 /* Return an iterator range for this block's multidict. */
147 iterator_range
<mdict_iterator_wrapper
> multidict_symbols () const
148 { return iterator_range
<mdict_iterator_wrapper
> (m_multidict
); }
150 /* Set this block's multidict. */
151 void set_multidict (multidictionary
*multidict
)
152 { m_multidict
= multidict
; }
154 /* Return a view on this block's ranges. */
155 gdb::array_view
<blockrange
> ranges ()
157 if (m_ranges
== nullptr)
160 return gdb::make_array_view (m_ranges
->range
, m_ranges
->nranges
);
163 /* Const version of the above. */
164 gdb::array_view
<const blockrange
> ranges () const
166 if (m_ranges
== nullptr)
169 return gdb::make_array_view (m_ranges
->range
, m_ranges
->nranges
);
172 /* Set this block's ranges array. */
173 void set_ranges (blockranges
*ranges
)
174 { m_ranges
= ranges
; }
176 /* Return true if all addresses within this block are contiguous. */
177 bool is_contiguous () const
178 { return this->ranges ().size () <= 1; }
180 /* Return the "entry PC" of this block.
182 The entry PC is the lowest (start) address for the block when all addresses
183 within the block are contiguous. If non-contiguous, then use the start
184 address for the first range in the block.
186 At the moment, this almost matches what DWARF specifies as the entry
187 pc. (The missing bit is support for DW_AT_entry_pc which should be
188 preferred over range data and the low_pc.)
190 Once support for DW_AT_entry_pc is added, I expect that an entry_pc
191 field will be added to one of these data structures. Once that's done,
192 the entry_pc field can be set from the dwarf reader (and other readers
193 too). ENTRY_PC can then be redefined to be less DWARF-centric. */
195 CORE_ADDR
entry_pc () const
197 if (this->is_contiguous ())
198 return this->start ();
200 return this->ranges ()[0].start ();
203 /* Return the objfile of this block. */
205 struct objfile
*objfile () const;
207 /* Return the architecture of this block. */
209 struct gdbarch
*gdbarch () const;
211 /* Return true if BL represents an inlined function. */
213 bool inlined_p () const;
215 /* This returns the namespace that this block is enclosed in, or ""
216 if it isn't enclosed in a namespace at all. This travels the
217 chain of superblocks looking for a scope, if necessary. */
219 const char *scope () const;
221 /* Set this block's scope member to SCOPE; if needed, allocate
222 memory via OBSTACK. (It won't make a copy of SCOPE, however, so
223 that already has to be allocated correctly.) */
225 void set_scope (const char *scope
, struct obstack
*obstack
);
227 /* This returns the using directives list associated with this
230 struct using_direct
*get_using () const;
232 /* Set this block's using member to USING; if needed, allocate
233 memory via OBSTACK. (It won't make a copy of USING, however, so
234 that already has to be allocated correctly.) */
236 void set_using (struct using_direct
*using_decl
, struct obstack
*obstack
);
238 /* Return the symbol for the function which contains a specified
239 lexical block, described by a struct block. The return value
240 will not be an inlined function; the containing function will be
243 struct symbol
*linkage_function () const;
245 /* Return the symbol for the function which contains a specified
246 block, described by a struct block. The return value will be the
247 closest enclosing function, which might be an inline
250 struct symbol
*containing_function () const;
252 /* Return the static block associated with this block. Return NULL
253 if block is a global block. */
255 const struct block
*static_block () const;
257 /* Return true if this block is a static block. */
259 bool is_static_block () const
261 const block
*sup
= superblock ();
264 return sup
->is_global_block ();
267 /* Return the static block associated with block. */
269 const struct block
*global_block () const;
271 /* Return true if this block is a global block. */
273 bool is_global_block () const
274 { return superblock () == nullptr; }
276 /* Set the compunit of this block, which must be a global block. */
278 void set_compunit_symtab (struct compunit_symtab
*);
280 /* Return a property to evaluate the static link associated to this
283 In the context of nested functions (available in Pascal, Ada and
284 GNU C, for instance), a static link (as in DWARF's
285 DW_AT_static_link attribute) for a function is a way to get the
286 frame corresponding to the enclosing function.
288 Note that only objfile-owned and function-level blocks can have a
289 static link. Return NULL if there is no such property. */
291 struct dynamic_prop
*static_link () const;
293 /* Return true if block A is lexically nested within this block, or
294 if A and this block have the same pc range. Return false
295 otherwise. If ALLOW_NESTED is true, then block A is considered
296 to be in this block if A is in a nested function in this block's
297 function. If ALLOW_NESTED is false (the default), then blocks in
298 nested functions are not considered to be contained. */
300 bool contains (const struct block
*a
, bool allow_nested
= false) const;
304 /* If the namespace_info is NULL, allocate it via OBSTACK and
305 initialize its members to zero. */
306 void initialize_namespace (struct obstack
*obstack
);
308 /* Addresses in the executable code that are in this block. */
310 CORE_ADDR m_start
= 0;
313 /* The symbol that names this block, if the block is the body of a
314 function (real or inlined); otherwise, zero. */
316 struct symbol
*m_function
= nullptr;
318 /* The `struct block' for the containing block, or 0 if none.
320 The superblock of a top-level local block (i.e. a function in the
321 case of C) is the STATIC_BLOCK. The superblock of the
322 STATIC_BLOCK is the GLOBAL_BLOCK. */
324 const struct block
*m_superblock
= nullptr;
326 /* This is used to store the symbols in the block. */
328 struct multidictionary
*m_multidict
= nullptr;
330 /* Contains information about namespace-related info relevant to this block:
331 using directives and the current namespace scope. */
333 struct block_namespace_info
*m_namespace_info
= nullptr;
335 /* Address ranges for blocks with non-contiguous ranges. If this
336 is NULL, then there is only one range which is specified by
337 startaddr and endaddr above. */
339 struct blockranges
*m_ranges
= nullptr;
342 /* The global block is singled out so that we can provide a back-link
343 to the compunit symtab. */
345 struct global_block
: public block
347 /* This holds a pointer to the compunit symtab holding this block. */
349 struct compunit_symtab
*compunit_symtab
= nullptr;
354 /* Return a view on the blocks of this blockvector. */
355 gdb::array_view
<struct block
*> blocks ()
357 return gdb::array_view
<struct block
*> (m_blocks
, m_num_blocks
);
360 /* Const version of the above. */
361 gdb::array_view
<const struct block
*const> blocks () const
363 const struct block
**blocks
= (const struct block
**) m_blocks
;
364 return gdb::array_view
<const struct block
*const> (blocks
, m_num_blocks
);
367 /* Return the block at index I. */
368 struct block
*block (size_t i
)
369 { return this->blocks ()[i
]; }
371 /* Const version of the above. */
372 const struct block
*block (size_t i
) const
373 { return this->blocks ()[i
]; }
375 /* Set the block at index I. */
376 void set_block (int i
, struct block
*block
)
377 { m_blocks
[i
] = block
; }
379 /* Set the number of blocks of this blockvector.
381 The storage of blocks is done using a flexible array member, so the number
382 of blocks set here must agree with what was effectively allocated. */
383 void set_num_blocks (int num_blocks
)
384 { m_num_blocks
= num_blocks
; }
386 /* Return the number of blocks in this blockvector. */
387 int num_blocks () const
388 { return m_num_blocks
; }
390 /* Return the global block of this blockvector. */
391 struct block
*global_block ()
392 { return this->block (GLOBAL_BLOCK
); }
394 /* Const version of the above. */
395 const struct block
*global_block () const
396 { return this->block (GLOBAL_BLOCK
); }
398 /* Return the static block of this blockvector. */
399 struct block
*static_block ()
400 { return this->block (STATIC_BLOCK
); }
402 /* Const version of the above. */
403 const struct block
*static_block () const
404 { return this->block (STATIC_BLOCK
); }
406 /* Return the address -> block map of this blockvector. */
410 /* Const version of the above. */
411 const addrmap
*map () const
414 /* Set this blockvector's address -> block map. */
415 void set_map (addrmap
*map
)
419 /* An address map mapping addresses to blocks in this blockvector.
420 This pointer is zero if the blocks' start and end addresses are
422 struct addrmap
*m_map
;
424 /* Number of blocks in the list. */
427 /* The blocks themselves. */
428 struct block
*m_blocks
[1];
431 extern const struct blockvector
*blockvector_for_pc (CORE_ADDR
,
432 const struct block
**);
434 extern const struct blockvector
*
435 blockvector_for_pc_sect (CORE_ADDR
, struct obj_section
*,
436 const struct block
**, struct compunit_symtab
*);
438 extern int blockvector_contains_pc (const struct blockvector
*bv
, CORE_ADDR pc
);
440 extern struct call_site
*call_site_for_pc (struct gdbarch
*gdbarch
,
443 extern const struct block
*block_for_pc (CORE_ADDR
);
445 extern const struct block
*block_for_pc_sect (CORE_ADDR
, struct obj_section
*);
447 /* A block iterator. This structure should be treated as though it
448 were opaque; it is only defined here because we want to support
449 stack allocation of iterators. */
451 struct block_iterator
453 /* If we're iterating over a single block, this holds the block.
454 Otherwise, it holds the canonical compunit. */
458 struct compunit_symtab
*compunit_symtab
;
459 const struct block
*block
;
462 /* If we're trying to match a name, this will be non-NULL. */
463 const lookup_name_info
*name
;
465 /* If we're iterating over a single block, this is always -1.
466 Otherwise, it holds the index of the current "included" symtab in
467 the canonical symtab (that is, d.symtab->includes[idx]), with -1
468 meaning the canonical symtab itself. */
472 /* Which block, either static or global, to iterate over. If this
473 is FIRST_LOCAL_BLOCK, then we are iterating over a single block.
474 This is used to select which field of 'd' is in use. */
476 enum block_enum which
;
478 /* The underlying multidictionary iterator. */
480 struct mdict_iterator mdict_iter
;
483 /* Initialize ITERATOR to point at the first symbol in BLOCK, and
484 return that first symbol, or NULL if BLOCK is empty. If NAME is
485 not NULL, only return symbols matching that name. */
487 extern struct symbol
*block_iterator_first
488 (const struct block
*block
,
489 struct block_iterator
*iterator
,
490 const lookup_name_info
*name
= nullptr);
492 /* Advance ITERATOR, and return the next symbol, or NULL if there are
493 no more symbols. Don't call this if you've previously received
494 NULL from block_iterator_first or block_iterator_next on this
497 extern struct symbol
*block_iterator_next (struct block_iterator
*iterator
);
499 /* An iterator that wraps a block_iterator. The naming here is
500 unfortunate, but block_iterator was named before gdb switched to
502 struct block_iterator_wrapper
504 typedef block_iterator_wrapper self_type
;
505 typedef struct symbol
*value_type
;
507 explicit block_iterator_wrapper (const struct block
*block
,
508 const lookup_name_info
*name
= nullptr)
509 : m_sym (block_iterator_first (block
, &m_iter
, name
))
513 block_iterator_wrapper ()
518 value_type
operator* () const
523 bool operator== (const self_type
&other
) const
525 return m_sym
== other
.m_sym
;
528 bool operator!= (const self_type
&other
) const
530 return m_sym
!= other
.m_sym
;
533 self_type
&operator++ ()
535 m_sym
= block_iterator_next (&m_iter
);
541 struct symbol
*m_sym
;
542 struct block_iterator m_iter
;
545 /* An iterator range for block_iterator_wrapper. */
547 typedef iterator_range
<block_iterator_wrapper
> block_iterator_range
;
549 /* Return true if symbol A is the best match possible for DOMAIN. */
551 extern bool best_symbol (struct symbol
*a
, const domain_enum domain
);
553 /* Return symbol B if it is a better match than symbol A for DOMAIN.
554 Otherwise return A. */
556 extern struct symbol
*better_symbol (struct symbol
*a
, struct symbol
*b
,
557 const domain_enum domain
);
559 /* Search BLOCK for symbol NAME in DOMAIN. */
561 extern struct symbol
*block_lookup_symbol (const struct block
*block
,
563 symbol_name_match_type match_type
,
564 const domain_enum domain
);
566 /* Search BLOCK for symbol NAME in DOMAIN but only in primary symbol table of
567 BLOCK. BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. Function is useful if
568 one iterates all global/static blocks of an objfile. */
570 extern struct symbol
*block_lookup_symbol_primary (const struct block
*block
,
572 const domain_enum domain
);
574 /* Find symbol NAME in BLOCK and in DOMAIN. This will return a
575 matching symbol whose type is not a "opaque", see TYPE_IS_OPAQUE.
576 If STUB is non-NULL, an otherwise matching symbol whose type is a
577 opaque will be stored here. */
579 extern struct symbol
*block_find_symbol (const struct block
*block
,
580 const lookup_name_info
&name
,
581 const domain_enum domain
,
582 struct symbol
**stub
);
584 /* Given a vector of pairs, allocate and build an obstack allocated
585 blockranges struct for a block. */
586 struct blockranges
*make_blockranges (struct objfile
*objfile
,
587 const std::vector
<blockrange
> &rangevec
);