1 /* Block-related functions for the GNU debugger, 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/>. */
24 #include "gdbsupport/gdb_obstack.h"
25 #include "cp-support.h"
30 /* This is used by struct block to store namespace-related info for
31 C++ files, namely using declarations and the current namespace in
34 struct block_namespace_info
: public allocate_on_obstack
36 const char *scope
= nullptr;
37 struct using_direct
*using_decl
= nullptr;
43 block::objfile () const
45 const struct global_block
*global_block
;
47 if (function () != nullptr)
48 return function ()->objfile ();
50 global_block
= (struct global_block
*) block_global_block (this);
51 return global_block
->compunit_symtab
->objfile ();
57 block::gdbarch () const
59 if (function () != nullptr)
60 return function ()->arch ();
62 return objfile ()->arch ();
68 contained_in (const struct block
*a
, const struct block
*b
,
78 /* If A is a function block, then A cannot be contained in B,
79 except if A was inlined. */
80 if (!allow_nested
&& a
->function () != NULL
&& !a
->inlined_p ())
90 /* Return the symbol for the function which contains a specified
91 lexical block, described by a struct block BL. The return value
92 will not be an inlined function; the containing function will be
96 block_linkage_function (const struct block
*bl
)
98 while ((bl
->function () == NULL
|| bl
->inlined_p ())
99 && bl
->superblock () != NULL
)
100 bl
= bl
->superblock ();
102 return bl
->function ();
105 /* Return the symbol for the function which contains a specified
106 block, described by a struct block BL. The return value will be
107 the closest enclosing function, which might be an inline
111 block_containing_function (const struct block
*bl
)
113 while (bl
->function () == NULL
&& bl
->superblock () != NULL
)
114 bl
= bl
->superblock ();
116 return bl
->function ();
122 block::inlined_p () const
124 return function () != nullptr && function ()->is_inlined ();
127 /* A helper function that checks whether PC is in the blockvector BL.
128 It returns the containing block if there is one, or else NULL. */
130 static const struct block
*
131 find_block_in_blockvector (const struct blockvector
*bl
, CORE_ADDR pc
)
133 const struct block
*b
;
136 /* If we have an addrmap mapping code addresses to blocks, then use
139 return (const struct block
*) bl
->map ()->find (pc
);
141 /* Otherwise, use binary search to find the last block that starts
143 Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1.
144 They both have the same START,END values.
145 Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the
146 fact that this choice was made was subtle, now we make it explicit. */
147 gdb_assert (bl
->blocks ().size () >= 2);
149 top
= bl
->blocks ().size ();
151 while (top
- bot
> 1)
153 half
= (top
- bot
+ 1) >> 1;
154 b
= bl
->block (bot
+ half
);
155 if (b
->start () <= pc
)
161 /* Now search backward for a block that ends after PC. */
163 while (bot
>= STATIC_BLOCK
)
166 if (!(b
->start () <= pc
))
176 /* Return the blockvector immediately containing the innermost lexical
177 block containing the specified pc value and section, or 0 if there
178 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
179 don't pass this information back to the caller. */
181 const struct blockvector
*
182 blockvector_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
,
183 const struct block
**pblock
,
184 struct compunit_symtab
*cust
)
186 const struct blockvector
*bl
;
187 const struct block
*b
;
191 /* First search all symtabs for one whose file contains our pc */
192 cust
= find_pc_sect_compunit_symtab (pc
, section
);
197 bl
= cust
->blockvector ();
199 /* Then search that symtab for the smallest block that wins. */
200 b
= find_block_in_blockvector (bl
, pc
);
209 /* Return true if the blockvector BV contains PC, false otherwise. */
212 blockvector_contains_pc (const struct blockvector
*bv
, CORE_ADDR pc
)
214 return find_block_in_blockvector (bv
, pc
) != NULL
;
217 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
218 must be the next instruction after call (or after tail call jump). Throw
219 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
222 call_site_for_pc (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
224 struct compunit_symtab
*cust
;
225 call_site
*cs
= nullptr;
227 /* -1 as tail call PC can be already after the compilation unit range. */
228 cust
= find_pc_compunit_symtab (pc
- 1);
231 cs
= cust
->find_call_site (pc
);
235 struct bound_minimal_symbol msym
= lookup_minimal_symbol_by_pc (pc
);
237 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
239 throw_error (NO_ENTRY_VALUE_ERROR
,
240 _("DW_OP_entry_value resolving cannot find "
241 "DW_TAG_call_site %s in %s"),
242 paddress (gdbarch
, pc
),
243 (msym
.minsym
== NULL
? "???"
244 : msym
.minsym
->print_name ()));
250 /* Return the blockvector immediately containing the innermost lexical block
251 containing the specified pc value, or 0 if there is none.
252 Backward compatibility, no section. */
254 const struct blockvector
*
255 blockvector_for_pc (CORE_ADDR pc
, const struct block
**pblock
)
257 return blockvector_for_pc_sect (pc
, find_pc_mapped_section (pc
),
261 /* Return the innermost lexical block containing the specified pc value
262 in the specified section, or 0 if there is none. */
265 block_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
)
267 const struct blockvector
*bl
;
268 const struct block
*b
;
270 bl
= blockvector_for_pc_sect (pc
, section
, &b
, NULL
);
276 /* Return the innermost lexical block containing the specified pc value,
277 or 0 if there is none. Backward compatibility, no section. */
280 block_for_pc (CORE_ADDR pc
)
282 return block_for_pc_sect (pc
, find_pc_mapped_section (pc
));
285 /* Now come some functions designed to deal with C++ namespace issues.
286 The accessors are safe to use even in the non-C++ case. */
291 block::scope () const
293 for (const block
*block
= this;
295 block
= block
->superblock ())
297 if (block
->m_namespace_info
!= nullptr
298 && block
->m_namespace_info
->scope
!= nullptr)
299 return block
->m_namespace_info
->scope
;
308 block::initialize_namespace (struct obstack
*obstack
)
310 if (m_namespace_info
== nullptr)
311 m_namespace_info
= new (obstack
) struct block_namespace_info
;
317 block::set_scope (const char *scope
, struct obstack
*obstack
)
319 if (scope
== nullptr || scope
[0] == '\0')
325 initialize_namespace (obstack
);
326 m_namespace_info
->scope
= scope
;
331 struct using_direct
*
332 block::get_using () const
334 if (m_namespace_info
== nullptr)
337 return m_namespace_info
->using_decl
;
343 block::set_using (struct using_direct
*using_decl
, struct obstack
*obstack
)
345 if (using_decl
== nullptr)
351 initialize_namespace (obstack
);
352 m_namespace_info
->using_decl
= using_decl
;
355 /* Return the static block associated to BLOCK. Return NULL if block
356 is a global block. */
359 block_static_block (const struct block
*block
)
361 if (block
->superblock () == NULL
)
364 while (block
->superblock ()->superblock () != NULL
)
365 block
= block
->superblock ();
370 /* Return the static block associated to BLOCK. */
373 block_global_block (const struct block
*block
)
375 while (block
->superblock () != NULL
)
376 block
= block
->superblock ();
381 /* Allocate a block on OBSTACK, and initialize its elements to
382 zero/NULL. This is useful for creating "dummy" blocks that don't
383 correspond to actual source files.
385 Warning: it sets the block's BLOCK_MULTIDICT to NULL, which isn't a
386 valid value. If you really don't want the block to have a
387 dictionary, then you should subsequently set its BLOCK_MULTIDICT to
388 dict_create_linear (obstack, NULL). */
391 allocate_block (struct obstack
*obstack
)
393 struct block
*bl
= OBSTACK_ZALLOC (obstack
, struct block
);
398 /* Allocate a global block. */
401 allocate_global_block (struct obstack
*obstack
)
403 struct global_block
*bl
= OBSTACK_ZALLOC (obstack
, struct global_block
);
408 /* Set the compunit of the global block. */
411 set_block_compunit_symtab (struct block
*block
, struct compunit_symtab
*cu
)
413 struct global_block
*gb
;
415 gdb_assert (block
->superblock () == NULL
);
416 gb
= (struct global_block
*) block
;
417 gdb_assert (gb
->compunit_symtab
== NULL
);
418 gb
->compunit_symtab
= cu
;
423 struct dynamic_prop
*
424 block_static_link (const struct block
*block
)
426 struct objfile
*objfile
= block
->objfile ();
428 /* Only objfile-owned blocks that materialize top function scopes can have
430 if (objfile
== NULL
|| block
->function () == NULL
)
433 return (struct dynamic_prop
*) objfile_lookup_static_link (objfile
, block
);
436 /* Return the compunit of the global block. */
438 static struct compunit_symtab
*
439 get_block_compunit_symtab (const struct block
*block
)
441 struct global_block
*gb
;
443 gdb_assert (block
->superblock () == NULL
);
444 gb
= (struct global_block
*) block
;
445 gdb_assert (gb
->compunit_symtab
!= NULL
);
446 return gb
->compunit_symtab
;
451 /* Initialize a block iterator, either to iterate over a single block,
452 or, for static and global blocks, all the included symtabs as
456 initialize_block_iterator (const struct block
*block
,
457 struct block_iterator
*iter
)
459 enum block_enum which
;
460 struct compunit_symtab
*cu
;
464 if (block
->superblock () == NULL
)
466 which
= GLOBAL_BLOCK
;
467 cu
= get_block_compunit_symtab (block
);
469 else if (block
->superblock ()->superblock () == NULL
)
471 which
= STATIC_BLOCK
;
472 cu
= get_block_compunit_symtab (block
->superblock ());
476 iter
->d
.block
= block
;
477 /* A signal value meaning that we're iterating over a single
479 iter
->which
= FIRST_LOCAL_BLOCK
;
483 /* If this is an included symtab, find the canonical includer and
485 while (cu
->user
!= NULL
)
488 /* Putting this check here simplifies the logic of the iterator
489 functions. If there are no included symtabs, we only need to
490 search a single block, so we might as well just do that
492 if (cu
->includes
== NULL
)
494 iter
->d
.block
= block
;
495 /* A signal value meaning that we're iterating over a single
497 iter
->which
= FIRST_LOCAL_BLOCK
;
501 iter
->d
.compunit_symtab
= cu
;
506 /* A helper function that finds the current compunit over whose static
507 or global block we should iterate. */
509 static struct compunit_symtab
*
510 find_iterator_compunit_symtab (struct block_iterator
*iterator
)
512 if (iterator
->idx
== -1)
513 return iterator
->d
.compunit_symtab
;
514 return iterator
->d
.compunit_symtab
->includes
[iterator
->idx
];
517 /* Perform a single step for a plain block iterator, iterating across
518 symbol tables as needed. Returns the next symbol, or NULL when
519 iteration is complete. */
521 static struct symbol
*
522 block_iterator_step (struct block_iterator
*iterator
, int first
)
526 gdb_assert (iterator
->which
!= FIRST_LOCAL_BLOCK
);
532 struct compunit_symtab
*cust
533 = find_iterator_compunit_symtab (iterator
);
534 const struct block
*block
;
536 /* Iteration is complete. */
540 block
= cust
->blockvector ()->block (iterator
->which
);
541 sym
= mdict_iterator_first (block
->multidict (),
542 &iterator
->mdict_iter
);
545 sym
= mdict_iterator_next (&iterator
->mdict_iter
);
550 /* We have finished iterating the appropriate block of one
551 symtab. Now advance to the next symtab and begin iteration
561 block_iterator_first (const struct block
*block
,
562 struct block_iterator
*iterator
)
564 initialize_block_iterator (block
, iterator
);
566 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
567 return mdict_iterator_first (block
->multidict (), &iterator
->mdict_iter
);
569 return block_iterator_step (iterator
, 1);
575 block_iterator_next (struct block_iterator
*iterator
)
577 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
578 return mdict_iterator_next (&iterator
->mdict_iter
);
580 return block_iterator_step (iterator
, 0);
583 /* Perform a single step for a "match" block iterator, iterating
584 across symbol tables as needed. Returns the next symbol, or NULL
585 when iteration is complete. */
587 static struct symbol
*
588 block_iter_match_step (struct block_iterator
*iterator
,
589 const lookup_name_info
&name
,
594 gdb_assert (iterator
->which
!= FIRST_LOCAL_BLOCK
);
600 struct compunit_symtab
*cust
601 = find_iterator_compunit_symtab (iterator
);
602 const struct block
*block
;
604 /* Iteration is complete. */
608 block
= cust
->blockvector ()->block (iterator
->which
);
609 sym
= mdict_iter_match_first (block
->multidict (), name
,
610 &iterator
->mdict_iter
);
613 sym
= mdict_iter_match_next (name
, &iterator
->mdict_iter
);
618 /* We have finished iterating the appropriate block of one
619 symtab. Now advance to the next symtab and begin iteration
629 block_iter_match_first (const struct block
*block
,
630 const lookup_name_info
&name
,
631 struct block_iterator
*iterator
)
633 initialize_block_iterator (block
, iterator
);
635 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
636 return mdict_iter_match_first (block
->multidict (), name
,
637 &iterator
->mdict_iter
);
639 return block_iter_match_step (iterator
, name
, 1);
645 block_iter_match_next (const lookup_name_info
&name
,
646 struct block_iterator
*iterator
)
648 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
649 return mdict_iter_match_next (name
, &iterator
->mdict_iter
);
651 return block_iter_match_step (iterator
, name
, 0);
657 best_symbol (struct symbol
*a
, const domain_enum domain
)
659 return (a
->domain () == domain
660 && a
->aclass () != LOC_UNRESOLVED
);
666 better_symbol (struct symbol
*a
, struct symbol
*b
, const domain_enum domain
)
673 if (a
->domain () == domain
&& b
->domain () != domain
)
676 if (b
->domain () == domain
&& a
->domain () != domain
)
679 if (a
->aclass () != LOC_UNRESOLVED
&& b
->aclass () == LOC_UNRESOLVED
)
682 if (b
->aclass () != LOC_UNRESOLVED
&& a
->aclass () == LOC_UNRESOLVED
)
690 Note that if NAME is the demangled form of a C++ symbol, we will fail
691 to find a match during the binary search of the non-encoded names, but
692 for now we don't worry about the slight inefficiency of looking for
693 a match we'll never find, since it will go pretty quick. Once the
694 binary search terminates, we drop through and do a straight linear
695 search on the symbols. Each symbol which is marked as being a ObjC/C++
696 symbol (language_cplus or language_objc set) has both the encoded and
697 non-encoded names tested for a match. */
700 block_lookup_symbol (const struct block
*block
, const char *name
,
701 symbol_name_match_type match_type
,
702 const domain_enum domain
)
704 struct block_iterator iter
;
707 lookup_name_info
lookup_name (name
, match_type
);
709 if (!block
->function ())
711 struct symbol
*other
= NULL
;
713 ALL_BLOCK_SYMBOLS_WITH_NAME (block
, lookup_name
, iter
, sym
)
715 /* See comment related to PR gcc/debug/91507 in
716 block_lookup_symbol_primary. */
717 if (best_symbol (sym
, domain
))
719 /* This is a bit of a hack, but symbol_matches_domain might ignore
720 STRUCT vs VAR domain symbols. So if a matching symbol is found,
721 make sure there is no "better" matching symbol, i.e., one with
722 exactly the same domain. PR 16253. */
723 if (symbol_matches_domain (sym
->language (),
724 sym
->domain (), domain
))
725 other
= better_symbol (other
, sym
, domain
);
731 /* Note that parameter symbols do not always show up last in the
732 list; this loop makes sure to take anything else other than
733 parameter symbols first; it only uses parameter symbols as a
734 last resort. Note that this only takes up extra computation
736 It's hard to define types in the parameter list (at least in
737 C/C++) so we don't do the same PR 16253 hack here that is done
738 for the !BLOCK_FUNCTION case. */
740 struct symbol
*sym_found
= NULL
;
742 ALL_BLOCK_SYMBOLS_WITH_NAME (block
, lookup_name
, iter
, sym
)
744 if (symbol_matches_domain (sym
->language (),
745 sym
->domain (), domain
))
748 if (!sym
->is_argument ())
754 return (sym_found
); /* Will be NULL if not found. */
761 block_lookup_symbol_primary (const struct block
*block
, const char *name
,
762 const domain_enum domain
)
764 struct symbol
*sym
, *other
;
765 struct mdict_iterator mdict_iter
;
767 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
769 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
770 gdb_assert (block
->superblock () == NULL
771 || block
->superblock ()->superblock () == NULL
);
774 for (sym
= mdict_iter_match_first (block
->multidict (), lookup_name
,
777 sym
= mdict_iter_match_next (lookup_name
, &mdict_iter
))
779 /* With the fix for PR gcc/debug/91507, we get for:
787 DWARF which will result in two entries in the symbol table, a decl
788 with type char *[] and a def with type char *[2].
790 If we return the decl here, we don't get the value of zzz:
792 $ gdb a.spec.out -batch -ex "p zzz"
795 because we're returning the symbol without location information, and
796 because the fallback that uses the address from the minimal symbols
797 doesn't work either because the type of the decl does not specify a
800 To fix this, we prefer def over decl in best_symbol and
803 In absence of the gcc fix, both def and decl have type char *[], so
804 the only option to make this work is improve the fallback to use the
805 size of the minimal symbol. Filed as PR exp/24989. */
806 if (best_symbol (sym
, domain
))
809 /* This is a bit of a hack, but symbol_matches_domain might ignore
810 STRUCT vs VAR domain symbols. So if a matching symbol is found,
811 make sure there is no "better" matching symbol, i.e., one with
812 exactly the same domain. PR 16253. */
813 if (symbol_matches_domain (sym
->language (), sym
->domain (), domain
))
814 other
= better_symbol (other
, sym
, domain
);
823 block_find_symbol (const struct block
*block
, const char *name
,
824 const domain_enum domain
,
825 block_symbol_matcher_ftype
*matcher
, void *data
)
827 struct block_iterator iter
;
830 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
832 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
833 gdb_assert (block
->superblock () == NULL
834 || block
->superblock ()->superblock () == NULL
);
836 ALL_BLOCK_SYMBOLS_WITH_NAME (block
, lookup_name
, iter
, sym
)
838 /* MATCHER is deliberately called second here so that it never sees
839 a non-domain-matching symbol. */
840 if (symbol_matches_domain (sym
->language (), sym
->domain (), domain
)
841 && matcher (sym
, data
))
850 block_find_non_opaque_type (struct symbol
*sym
, void *data
)
852 return !TYPE_IS_OPAQUE (sym
->type ());
858 block_find_non_opaque_type_preferred (struct symbol
*sym
, void *data
)
860 struct symbol
**best
= (struct symbol
**) data
;
862 if (!TYPE_IS_OPAQUE (sym
->type ()))
871 make_blockranges (struct objfile
*objfile
,
872 const std::vector
<blockrange
> &rangevec
)
874 struct blockranges
*blr
;
875 size_t n
= rangevec
.size();
877 blr
= (struct blockranges
*)
878 obstack_alloc (&objfile
->objfile_obstack
,
879 sizeof (struct blockranges
)
880 + (n
- 1) * sizeof (struct blockrange
));
883 for (int i
= 0; i
< n
; i
++)
884 blr
->range
[i
] = rangevec
[i
];