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
*) this->global_block ();
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 ())
92 block::linkage_function () const
94 const block
*bl
= this;
96 while ((bl
->function () == NULL
|| bl
->inlined_p ())
97 && bl
->superblock () != NULL
)
98 bl
= bl
->superblock ();
100 return bl
->function ();
106 block::containing_function () const
108 const block
*bl
= this;
110 while (bl
->function () == NULL
&& bl
->superblock () != NULL
)
111 bl
= bl
->superblock ();
113 return bl
->function ();
119 block::inlined_p () const
121 return function () != nullptr && function ()->is_inlined ();
124 /* A helper function that checks whether PC is in the blockvector BL.
125 It returns the containing block if there is one, or else NULL. */
127 static const struct block
*
128 find_block_in_blockvector (const struct blockvector
*bl
, CORE_ADDR pc
)
130 const struct block
*b
;
133 /* If we have an addrmap mapping code addresses to blocks, then use
136 return (const struct block
*) bl
->map ()->find (pc
);
138 /* Otherwise, use binary search to find the last block that starts
140 Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1.
141 They both have the same START,END values.
142 Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the
143 fact that this choice was made was subtle, now we make it explicit. */
144 gdb_assert (bl
->blocks ().size () >= 2);
146 top
= bl
->blocks ().size ();
148 while (top
- bot
> 1)
150 half
= (top
- bot
+ 1) >> 1;
151 b
= bl
->block (bot
+ half
);
152 if (b
->start () <= pc
)
158 /* Now search backward for a block that ends after PC. */
160 while (bot
>= STATIC_BLOCK
)
163 if (!(b
->start () <= pc
))
173 /* Return the blockvector immediately containing the innermost lexical
174 block containing the specified pc value and section, or 0 if there
175 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
176 don't pass this information back to the caller. */
178 const struct blockvector
*
179 blockvector_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
,
180 const struct block
**pblock
,
181 struct compunit_symtab
*cust
)
183 const struct blockvector
*bl
;
184 const struct block
*b
;
188 /* First search all symtabs for one whose file contains our pc */
189 cust
= find_pc_sect_compunit_symtab (pc
, section
);
194 bl
= cust
->blockvector ();
196 /* Then search that symtab for the smallest block that wins. */
197 b
= find_block_in_blockvector (bl
, pc
);
206 /* Return true if the blockvector BV contains PC, false otherwise. */
209 blockvector_contains_pc (const struct blockvector
*bv
, CORE_ADDR pc
)
211 return find_block_in_blockvector (bv
, pc
) != NULL
;
214 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
215 must be the next instruction after call (or after tail call jump). Throw
216 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
219 call_site_for_pc (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
221 struct compunit_symtab
*cust
;
222 call_site
*cs
= nullptr;
224 /* -1 as tail call PC can be already after the compilation unit range. */
225 cust
= find_pc_compunit_symtab (pc
- 1);
228 cs
= cust
->find_call_site (pc
);
232 struct bound_minimal_symbol msym
= lookup_minimal_symbol_by_pc (pc
);
234 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
236 throw_error (NO_ENTRY_VALUE_ERROR
,
237 _("DW_OP_entry_value resolving cannot find "
238 "DW_TAG_call_site %s in %s"),
239 paddress (gdbarch
, pc
),
240 (msym
.minsym
== NULL
? "???"
241 : msym
.minsym
->print_name ()));
247 /* Return the blockvector immediately containing the innermost lexical block
248 containing the specified pc value, or 0 if there is none.
249 Backward compatibility, no section. */
251 const struct blockvector
*
252 blockvector_for_pc (CORE_ADDR pc
, const struct block
**pblock
)
254 return blockvector_for_pc_sect (pc
, find_pc_mapped_section (pc
),
258 /* Return the innermost lexical block containing the specified pc value
259 in the specified section, or 0 if there is none. */
262 block_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
)
264 const struct blockvector
*bl
;
265 const struct block
*b
;
267 bl
= blockvector_for_pc_sect (pc
, section
, &b
, NULL
);
273 /* Return the innermost lexical block containing the specified pc value,
274 or 0 if there is none. Backward compatibility, no section. */
277 block_for_pc (CORE_ADDR pc
)
279 return block_for_pc_sect (pc
, find_pc_mapped_section (pc
));
282 /* Now come some functions designed to deal with C++ namespace issues.
283 The accessors are safe to use even in the non-C++ case. */
288 block::scope () const
290 for (const block
*block
= this;
292 block
= block
->superblock ())
294 if (block
->m_namespace_info
!= nullptr
295 && block
->m_namespace_info
->scope
!= nullptr)
296 return block
->m_namespace_info
->scope
;
305 block::initialize_namespace (struct obstack
*obstack
)
307 if (m_namespace_info
== nullptr)
308 m_namespace_info
= new (obstack
) struct block_namespace_info
;
314 block::set_scope (const char *scope
, struct obstack
*obstack
)
316 if (scope
== nullptr || scope
[0] == '\0')
322 initialize_namespace (obstack
);
323 m_namespace_info
->scope
= scope
;
328 struct using_direct
*
329 block::get_using () const
331 if (m_namespace_info
== nullptr)
334 return m_namespace_info
->using_decl
;
340 block::set_using (struct using_direct
*using_decl
, struct obstack
*obstack
)
342 if (using_decl
== nullptr)
348 initialize_namespace (obstack
);
349 m_namespace_info
->using_decl
= using_decl
;
355 block::static_block () const
357 if (superblock () == nullptr)
360 const block
*block
= this;
361 while (block
->superblock ()->superblock () != NULL
)
362 block
= block
->superblock ();
370 block::global_block () const
372 const block
*block
= this;
374 while (block
->superblock () != NULL
)
375 block
= block
->superblock ();
380 /* Allocate a block on OBSTACK, and initialize its elements to
381 zero/NULL. This is useful for creating "dummy" blocks that don't
382 correspond to actual source files.
384 Warning: it sets the block's BLOCK_MULTIDICT to NULL, which isn't a
385 valid value. If you really don't want the block to have a
386 dictionary, then you should subsequently set its BLOCK_MULTIDICT to
387 dict_create_linear (obstack, NULL). */
390 allocate_block (struct obstack
*obstack
)
392 struct block
*bl
= OBSTACK_ZALLOC (obstack
, struct block
);
397 /* Allocate a global block. */
400 allocate_global_block (struct obstack
*obstack
)
402 struct global_block
*bl
= OBSTACK_ZALLOC (obstack
, struct global_block
);
410 block::set_compunit_symtab (struct compunit_symtab
*cu
)
412 struct global_block
*gb
;
414 gdb_assert (superblock () == NULL
);
415 gb
= (struct global_block
*) this;
416 gdb_assert (gb
->compunit_symtab
== NULL
);
417 gb
->compunit_symtab
= cu
;
422 struct dynamic_prop
*
423 block::static_link () const
425 struct objfile
*objfile
= this->objfile ();
427 /* Only objfile-owned blocks that materialize top function scopes can have
429 if (objfile
== NULL
|| function () == NULL
)
432 return (struct dynamic_prop
*) objfile_lookup_static_link (objfile
, this);
435 /* Return the compunit of the global block. */
437 static struct compunit_symtab
*
438 get_block_compunit_symtab (const struct block
*block
)
440 struct global_block
*gb
;
442 gdb_assert (block
->superblock () == NULL
);
443 gb
= (struct global_block
*) block
;
444 gdb_assert (gb
->compunit_symtab
!= NULL
);
445 return gb
->compunit_symtab
;
450 /* Initialize a block iterator, either to iterate over a single block,
451 or, for static and global blocks, all the included symtabs as
455 initialize_block_iterator (const struct block
*block
,
456 struct block_iterator
*iter
,
457 const lookup_name_info
*name
= nullptr)
459 enum block_enum which
;
460 struct compunit_symtab
*cu
;
465 if (block
->superblock () == NULL
)
467 which
= GLOBAL_BLOCK
;
468 cu
= get_block_compunit_symtab (block
);
470 else if (block
->superblock ()->superblock () == NULL
)
472 which
= STATIC_BLOCK
;
473 cu
= get_block_compunit_symtab (block
->superblock ());
477 iter
->d
.block
= block
;
478 /* A signal value meaning that we're iterating over a single
480 iter
->which
= FIRST_LOCAL_BLOCK
;
484 /* If this is an included symtab, find the canonical includer and
486 while (cu
->user
!= NULL
)
489 /* Putting this check here simplifies the logic of the iterator
490 functions. If there are no included symtabs, we only need to
491 search a single block, so we might as well just do that
493 if (cu
->includes
== NULL
)
495 iter
->d
.block
= block
;
496 /* A signal value meaning that we're iterating over a single
498 iter
->which
= FIRST_LOCAL_BLOCK
;
502 iter
->d
.compunit_symtab
= cu
;
507 /* A helper function that finds the current compunit over whose static
508 or global block we should iterate. */
510 static struct compunit_symtab
*
511 find_iterator_compunit_symtab (struct block_iterator
*iterator
)
513 if (iterator
->idx
== -1)
514 return iterator
->d
.compunit_symtab
;
515 return iterator
->d
.compunit_symtab
->includes
[iterator
->idx
];
518 /* Perform a single step for a plain block iterator, iterating across
519 symbol tables as needed. Returns the next symbol, or NULL when
520 iteration is complete. */
522 static struct symbol
*
523 block_iterator_step (struct block_iterator
*iterator
, int first
)
527 gdb_assert (iterator
->which
!= FIRST_LOCAL_BLOCK
);
533 struct compunit_symtab
*cust
534 = find_iterator_compunit_symtab (iterator
);
535 const struct block
*block
;
537 /* Iteration is complete. */
541 block
= cust
->blockvector ()->block (iterator
->which
);
542 sym
= mdict_iterator_first (block
->multidict (),
543 &iterator
->mdict_iter
);
546 sym
= mdict_iterator_next (&iterator
->mdict_iter
);
551 /* We have finished iterating the appropriate block of one
552 symtab. Now advance to the next symtab and begin iteration
559 /* Perform a single step for a "match" block iterator, iterating
560 across symbol tables as needed. Returns the next symbol, or NULL
561 when iteration is complete. */
563 static struct symbol
*
564 block_iter_match_step (struct block_iterator
*iterator
,
569 gdb_assert (iterator
->which
!= FIRST_LOCAL_BLOCK
);
575 struct compunit_symtab
*cust
576 = find_iterator_compunit_symtab (iterator
);
577 const struct block
*block
;
579 /* Iteration is complete. */
583 block
= cust
->blockvector ()->block (iterator
->which
);
584 sym
= mdict_iter_match_first (block
->multidict (), *iterator
->name
,
585 &iterator
->mdict_iter
);
588 sym
= mdict_iter_match_next (*iterator
->name
, &iterator
->mdict_iter
);
593 /* We have finished iterating the appropriate block of one
594 symtab. Now advance to the next symtab and begin iteration
604 block_iterator_first (const struct block
*block
,
605 struct block_iterator
*iterator
,
606 const lookup_name_info
*name
)
608 initialize_block_iterator (block
, iterator
, name
);
612 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
613 return mdict_iterator_first (block
->multidict (),
614 &iterator
->mdict_iter
);
616 return block_iterator_step (iterator
, 1);
619 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
620 return mdict_iter_match_first (block
->multidict (), *name
,
621 &iterator
->mdict_iter
);
623 return block_iter_match_step (iterator
, 1);
629 block_iterator_next (struct block_iterator
*iterator
)
631 if (iterator
->name
== nullptr)
633 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
634 return mdict_iterator_next (&iterator
->mdict_iter
);
636 return block_iterator_step (iterator
, 0);
639 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
640 return mdict_iter_match_next (*iterator
->name
, &iterator
->mdict_iter
);
642 return block_iter_match_step (iterator
, 0);
648 best_symbol (struct symbol
*a
, const domain_enum domain
)
650 return (a
->domain () == domain
651 && a
->aclass () != LOC_UNRESOLVED
);
657 better_symbol (struct symbol
*a
, struct symbol
*b
, const domain_enum domain
)
664 if (a
->domain () == domain
&& b
->domain () != domain
)
667 if (b
->domain () == domain
&& a
->domain () != domain
)
670 if (a
->aclass () != LOC_UNRESOLVED
&& b
->aclass () == LOC_UNRESOLVED
)
673 if (b
->aclass () != LOC_UNRESOLVED
&& a
->aclass () == LOC_UNRESOLVED
)
681 Note that if NAME is the demangled form of a C++ symbol, we will fail
682 to find a match during the binary search of the non-encoded names, but
683 for now we don't worry about the slight inefficiency of looking for
684 a match we'll never find, since it will go pretty quick. Once the
685 binary search terminates, we drop through and do a straight linear
686 search on the symbols. Each symbol which is marked as being a ObjC/C++
687 symbol (language_cplus or language_objc set) has both the encoded and
688 non-encoded names tested for a match. */
691 block_lookup_symbol (const struct block
*block
, const char *name
,
692 symbol_name_match_type match_type
,
693 const domain_enum domain
)
695 lookup_name_info
lookup_name (name
, match_type
);
697 if (!block
->function ())
699 struct symbol
*other
= NULL
;
701 for (struct symbol
*sym
: block_iterator_range (block
, &lookup_name
))
703 /* See comment related to PR gcc/debug/91507 in
704 block_lookup_symbol_primary. */
705 if (best_symbol (sym
, domain
))
707 /* This is a bit of a hack, but symbol_matches_domain might ignore
708 STRUCT vs VAR domain symbols. So if a matching symbol is found,
709 make sure there is no "better" matching symbol, i.e., one with
710 exactly the same domain. PR 16253. */
711 if (symbol_matches_domain (sym
->language (),
712 sym
->domain (), domain
))
713 other
= better_symbol (other
, sym
, domain
);
719 /* Note that parameter symbols do not always show up last in the
720 list; this loop makes sure to take anything else other than
721 parameter symbols first; it only uses parameter symbols as a
722 last resort. Note that this only takes up extra computation
724 It's hard to define types in the parameter list (at least in
725 C/C++) so we don't do the same PR 16253 hack here that is done
726 for the !BLOCK_FUNCTION case. */
728 struct symbol
*sym_found
= NULL
;
730 for (struct symbol
*sym
: block_iterator_range (block
, &lookup_name
))
732 if (symbol_matches_domain (sym
->language (),
733 sym
->domain (), domain
))
736 if (!sym
->is_argument ())
742 return (sym_found
); /* Will be NULL if not found. */
749 block_lookup_symbol_primary (const struct block
*block
, const char *name
,
750 const domain_enum domain
)
752 struct symbol
*sym
, *other
;
753 struct mdict_iterator mdict_iter
;
755 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
757 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
758 gdb_assert (block
->superblock () == NULL
759 || block
->superblock ()->superblock () == NULL
);
762 for (sym
= mdict_iter_match_first (block
->multidict (), lookup_name
,
765 sym
= mdict_iter_match_next (lookup_name
, &mdict_iter
))
767 /* With the fix for PR gcc/debug/91507, we get for:
775 DWARF which will result in two entries in the symbol table, a decl
776 with type char *[] and a def with type char *[2].
778 If we return the decl here, we don't get the value of zzz:
780 $ gdb a.spec.out -batch -ex "p zzz"
783 because we're returning the symbol without location information, and
784 because the fallback that uses the address from the minimal symbols
785 doesn't work either because the type of the decl does not specify a
788 To fix this, we prefer def over decl in best_symbol and
791 In absence of the gcc fix, both def and decl have type char *[], so
792 the only option to make this work is improve the fallback to use the
793 size of the minimal symbol. Filed as PR exp/24989. */
794 if (best_symbol (sym
, domain
))
797 /* This is a bit of a hack, but symbol_matches_domain might ignore
798 STRUCT vs VAR domain symbols. So if a matching symbol is found,
799 make sure there is no "better" matching symbol, i.e., one with
800 exactly the same domain. PR 16253. */
801 if (symbol_matches_domain (sym
->language (), sym
->domain (), domain
))
802 other
= better_symbol (other
, sym
, domain
);
811 block_find_symbol (const struct block
*block
, const char *name
,
812 const domain_enum domain
,
813 block_symbol_matcher_ftype
*matcher
, void *data
)
815 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
817 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
818 gdb_assert (block
->superblock () == NULL
819 || block
->superblock ()->superblock () == NULL
);
821 for (struct symbol
*sym
: block_iterator_range (block
, &lookup_name
))
823 /* MATCHER is deliberately called second here so that it never sees
824 a non-domain-matching symbol. */
825 if (symbol_matches_domain (sym
->language (), sym
->domain (), domain
)
826 && matcher (sym
, data
))
835 block_find_non_opaque_type (struct symbol
*sym
, void *data
)
837 return !TYPE_IS_OPAQUE (sym
->type ());
843 block_find_non_opaque_type_preferred (struct symbol
*sym
, void *data
)
845 struct symbol
**best
= (struct symbol
**) data
;
847 if (!TYPE_IS_OPAQUE (sym
->type ()))
856 make_blockranges (struct objfile
*objfile
,
857 const std::vector
<blockrange
> &rangevec
)
859 struct blockranges
*blr
;
860 size_t n
= rangevec
.size();
862 blr
= (struct blockranges
*)
863 obstack_alloc (&objfile
->objfile_obstack
,
864 sizeof (struct blockranges
)
865 + (n
- 1) * sizeof (struct blockrange
));
868 for (int i
= 0; i
< n
; i
++)
869 blr
->range
[i
] = rangevec
[i
];