1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright (C) 2003-2024 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 "gdbsupport/gdb_obstack.h"
24 #include "cp-support.h"
29 /* This is used by struct block to store namespace-related info for
30 C++ files, namely using declarations and the current namespace in
33 struct block_namespace_info
: public allocate_on_obstack
<block_namespace_info
>
35 const char *scope
= nullptr;
36 struct using_direct
*using_decl
= nullptr;
42 block::objfile () const
44 const struct global_block
*global_block
;
46 if (function () != nullptr)
47 return function ()->objfile ();
49 global_block
= (struct global_block
*) this->global_block ();
50 return global_block
->compunit_symtab
->objfile ();
56 block::gdbarch () const
58 if (function () != nullptr)
59 return function ()->arch ();
61 return objfile ()->arch ();
67 block::contains (const struct block
*a
, bool allow_nested
) const
76 /* If A is a function block, then A cannot be contained in B,
77 except if A was inlined. */
78 if (!allow_nested
&& a
->function () != NULL
&& !a
->inlined_p ())
90 block::linkage_function () const
92 const block
*bl
= this;
94 while ((bl
->function () == NULL
|| bl
->inlined_p ())
95 && bl
->superblock () != NULL
)
96 bl
= bl
->superblock ();
98 return bl
->function ();
104 block::containing_function () const
106 const block
*bl
= this;
108 while (bl
->function () == NULL
&& bl
->superblock () != NULL
)
109 bl
= bl
->superblock ();
111 return bl
->function ();
117 block::inlined_p () const
119 return function () != nullptr && function ()->is_inlined ();
122 /* A helper function that checks whether PC is in the blockvector BL.
123 It returns the containing block if there is one, or else NULL. */
125 static const struct block
*
126 find_block_in_blockvector (const struct blockvector
*bl
, CORE_ADDR pc
)
128 const struct block
*b
;
131 /* If we have an addrmap mapping code addresses to blocks, then use
134 return (const struct block
*) bl
->map ()->find (pc
);
136 /* Otherwise, use binary search to find the last block that starts
138 Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1.
139 They both have the same START,END values.
140 Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the
141 fact that this choice was made was subtle, now we make it explicit. */
142 gdb_assert (bl
->blocks ().size () >= 2);
144 top
= bl
->blocks ().size ();
146 while (top
- bot
> 1)
148 half
= (top
- bot
+ 1) >> 1;
149 b
= bl
->block (bot
+ half
);
150 if (b
->start () <= pc
)
156 /* Now search backward for a block that ends after PC. */
158 while (bot
>= STATIC_BLOCK
)
161 if (!(b
->start () <= pc
))
171 /* Return the blockvector immediately containing the innermost lexical
172 block containing the specified pc value and section, or 0 if there
173 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
174 don't pass this information back to the caller. */
176 const struct blockvector
*
177 blockvector_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
,
178 const struct block
**pblock
,
179 struct compunit_symtab
*cust
)
181 const struct blockvector
*bl
;
182 const struct block
*b
;
186 /* First search all symtabs for one whose file contains our pc */
187 cust
= find_pc_sect_compunit_symtab (pc
, section
);
192 bl
= cust
->blockvector ();
194 /* Then search that symtab for the smallest block that wins. */
195 b
= find_block_in_blockvector (bl
, pc
);
204 /* Return true if the blockvector BV contains PC, false otherwise. */
207 blockvector_contains_pc (const struct blockvector
*bv
, CORE_ADDR pc
)
209 return find_block_in_blockvector (bv
, pc
) != NULL
;
212 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
213 must be the next instruction after call (or after tail call jump). Throw
214 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
217 call_site_for_pc (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
219 struct compunit_symtab
*cust
;
220 call_site
*cs
= nullptr;
222 /* -1 as tail call PC can be already after the compilation unit range. */
223 cust
= find_pc_compunit_symtab (pc
- 1);
226 cs
= cust
->find_call_site (pc
);
230 struct bound_minimal_symbol msym
= lookup_minimal_symbol_by_pc (pc
);
232 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
234 throw_error (NO_ENTRY_VALUE_ERROR
,
235 _("DW_OP_entry_value resolving cannot find "
236 "DW_TAG_call_site %s in %s"),
237 paddress (gdbarch
, pc
),
238 (msym
.minsym
== NULL
? "???"
239 : msym
.minsym
->print_name ()));
245 /* Return the blockvector immediately containing the innermost lexical block
246 containing the specified pc value, or 0 if there is none.
247 Backward compatibility, no section. */
249 const struct blockvector
*
250 blockvector_for_pc (CORE_ADDR pc
, const struct block
**pblock
)
252 return blockvector_for_pc_sect (pc
, find_pc_mapped_section (pc
),
256 /* Return the innermost lexical block containing the specified pc value
257 in the specified section, or 0 if there is none. */
260 block_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
)
262 const struct blockvector
*bl
;
263 const struct block
*b
;
265 bl
= blockvector_for_pc_sect (pc
, section
, &b
, NULL
);
271 /* Return the innermost lexical block containing the specified pc value,
272 or 0 if there is none. Backward compatibility, no section. */
275 block_for_pc (CORE_ADDR pc
)
277 return block_for_pc_sect (pc
, find_pc_mapped_section (pc
));
280 /* Now come some functions designed to deal with C++ namespace issues.
281 The accessors are safe to use even in the non-C++ case. */
286 block::scope () const
288 for (const block
*block
= this;
290 block
= block
->superblock ())
292 if (block
->m_namespace_info
!= nullptr
293 && block
->m_namespace_info
->scope
!= nullptr)
294 return block
->m_namespace_info
->scope
;
303 block::initialize_namespace (struct obstack
*obstack
)
305 if (m_namespace_info
== nullptr)
306 m_namespace_info
= new (obstack
) struct block_namespace_info
;
312 block::set_scope (const char *scope
, struct obstack
*obstack
)
314 if (scope
== nullptr || scope
[0] == '\0')
320 initialize_namespace (obstack
);
321 m_namespace_info
->scope
= scope
;
326 struct using_direct
*
327 block::get_using () const
329 if (m_namespace_info
== nullptr)
332 return m_namespace_info
->using_decl
;
338 block::set_using (struct using_direct
*using_decl
, struct obstack
*obstack
)
340 if (using_decl
== nullptr)
346 initialize_namespace (obstack
);
347 m_namespace_info
->using_decl
= using_decl
;
353 block::static_block () const
355 if (superblock () == nullptr)
358 const block
*block
= this;
359 while (block
->superblock ()->superblock () != NULL
)
360 block
= block
->superblock ();
368 block::global_block () const
370 const block
*block
= this;
372 while (block
->superblock () != NULL
)
373 block
= block
->superblock ();
381 block::function_block () const
383 const block
*block
= this;
385 while (block
!= nullptr && block
->function () == nullptr)
386 block
= block
->superblock ();
394 block::set_compunit_symtab (struct compunit_symtab
*cu
)
396 struct global_block
*gb
;
398 gdb_assert (superblock () == NULL
);
399 gb
= (struct global_block
*) this;
400 gdb_assert (gb
->compunit_symtab
== NULL
);
401 gb
->compunit_symtab
= cu
;
406 struct dynamic_prop
*
407 block::static_link () const
409 struct objfile
*objfile
= this->objfile ();
411 /* Only objfile-owned blocks that materialize top function scopes can have
413 if (objfile
== NULL
|| function () == NULL
)
416 return (struct dynamic_prop
*) objfile_lookup_static_link (objfile
, this);
419 /* Return the compunit of the global block. */
421 static struct compunit_symtab
*
422 get_block_compunit_symtab (const struct block
*block
)
424 struct global_block
*gb
;
426 gdb_assert (block
->superblock () == NULL
);
427 gb
= (struct global_block
*) block
;
428 gdb_assert (gb
->compunit_symtab
!= NULL
);
429 return gb
->compunit_symtab
;
434 /* Initialize a block iterator, either to iterate over a single block,
435 or, for static and global blocks, all the included symtabs as
439 initialize_block_iterator (const struct block
*block
,
440 struct block_iterator
*iter
,
441 const lookup_name_info
*name
= nullptr)
443 enum block_enum which
;
444 struct compunit_symtab
*cu
;
449 if (block
->superblock () == NULL
)
451 which
= GLOBAL_BLOCK
;
452 cu
= get_block_compunit_symtab (block
);
454 else if (block
->superblock ()->superblock () == NULL
)
456 which
= STATIC_BLOCK
;
457 cu
= get_block_compunit_symtab (block
->superblock ());
461 iter
->d
.block
= block
;
462 /* A signal value meaning that we're iterating over a single
464 iter
->which
= FIRST_LOCAL_BLOCK
;
468 /* If this is an included symtab, find the canonical includer and
470 while (cu
->user
!= NULL
)
473 /* Putting this check here simplifies the logic of the iterator
474 functions. If there are no included symtabs, we only need to
475 search a single block, so we might as well just do that
477 if (cu
->includes
== NULL
)
479 iter
->d
.block
= block
;
480 /* A signal value meaning that we're iterating over a single
482 iter
->which
= FIRST_LOCAL_BLOCK
;
486 iter
->d
.compunit_symtab
= cu
;
491 /* A helper function that finds the current compunit over whose static
492 or global block we should iterate. */
494 static struct compunit_symtab
*
495 find_iterator_compunit_symtab (struct block_iterator
*iterator
)
497 if (iterator
->idx
== -1)
498 return iterator
->d
.compunit_symtab
;
499 return iterator
->d
.compunit_symtab
->includes
[iterator
->idx
];
502 /* Perform a single step for a plain block iterator, iterating across
503 symbol tables as needed. Returns the next symbol, or NULL when
504 iteration is complete. */
506 static struct symbol
*
507 block_iterator_step (struct block_iterator
*iterator
, int first
)
511 gdb_assert (iterator
->which
!= FIRST_LOCAL_BLOCK
);
517 struct compunit_symtab
*cust
518 = find_iterator_compunit_symtab (iterator
);
519 const struct block
*block
;
521 /* Iteration is complete. */
525 block
= cust
->blockvector ()->block (iterator
->which
);
526 sym
= mdict_iterator_first (block
->multidict (),
527 &iterator
->mdict_iter
);
530 sym
= mdict_iterator_next (&iterator
->mdict_iter
);
535 /* We have finished iterating the appropriate block of one
536 symtab. Now advance to the next symtab and begin iteration
543 /* Perform a single step for a "match" block iterator, iterating
544 across symbol tables as needed. Returns the next symbol, or NULL
545 when iteration is complete. */
547 static struct symbol
*
548 block_iter_match_step (struct block_iterator
*iterator
,
553 gdb_assert (iterator
->which
!= FIRST_LOCAL_BLOCK
);
559 struct compunit_symtab
*cust
560 = find_iterator_compunit_symtab (iterator
);
561 const struct block
*block
;
563 /* Iteration is complete. */
567 block
= cust
->blockvector ()->block (iterator
->which
);
568 sym
= mdict_iter_match_first (block
->multidict (), *iterator
->name
,
569 &iterator
->mdict_iter
);
572 sym
= mdict_iter_match_next (*iterator
->name
, &iterator
->mdict_iter
);
577 /* We have finished iterating the appropriate block of one
578 symtab. Now advance to the next symtab and begin iteration
588 block_iterator_first (const struct block
*block
,
589 struct block_iterator
*iterator
,
590 const lookup_name_info
*name
)
592 initialize_block_iterator (block
, iterator
, name
);
596 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
597 return mdict_iterator_first (block
->multidict (),
598 &iterator
->mdict_iter
);
600 return block_iterator_step (iterator
, 1);
603 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
604 return mdict_iter_match_first (block
->multidict (), *name
,
605 &iterator
->mdict_iter
);
607 return block_iter_match_step (iterator
, 1);
613 block_iterator_next (struct block_iterator
*iterator
)
615 if (iterator
->name
== nullptr)
617 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
618 return mdict_iterator_next (&iterator
->mdict_iter
);
620 return block_iterator_step (iterator
, 0);
623 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
624 return mdict_iter_match_next (*iterator
->name
, &iterator
->mdict_iter
);
626 return block_iter_match_step (iterator
, 0);
632 best_symbol (struct symbol
*a
, const domain_search_flags domain
)
634 if (a
->aclass () == LOC_UNRESOLVED
)
637 if ((domain
& SEARCH_VAR_DOMAIN
) != 0)
638 return a
->domain () == VAR_DOMAIN
;
640 return a
->matches (domain
);
646 better_symbol (struct symbol
*a
, struct symbol
*b
,
647 const domain_search_flags domain
)
654 if (a
->matches (domain
) && !b
->matches (domain
))
657 if (b
->matches (domain
) && !a
->matches (domain
))
660 if (a
->aclass () != LOC_UNRESOLVED
&& b
->aclass () == LOC_UNRESOLVED
)
663 if (b
->aclass () != LOC_UNRESOLVED
&& a
->aclass () == LOC_UNRESOLVED
)
671 Note that if NAME is the demangled form of a C++ symbol, we will fail
672 to find a match during the binary search of the non-encoded names, but
673 for now we don't worry about the slight inefficiency of looking for
674 a match we'll never find, since it will go pretty quick. Once the
675 binary search terminates, we drop through and do a straight linear
676 search on the symbols. Each symbol which is marked as being a ObjC/C++
677 symbol (language_cplus or language_objc set) has both the encoded and
678 non-encoded names tested for a match. */
681 block_lookup_symbol (const struct block
*block
, const lookup_name_info
&name
,
682 const domain_search_flags domain
)
684 if (!block
->function ())
686 struct symbol
*other
= NULL
;
688 for (struct symbol
*sym
: block_iterator_range (block
, &name
))
690 /* See comment related to PR gcc/debug/91507 in
691 block_lookup_symbol_primary. */
692 if (best_symbol (sym
, domain
))
694 /* This is a bit of a hack, but symbol_matches_domain might ignore
695 STRUCT vs VAR domain symbols. So if a matching symbol is found,
696 make sure there is no "better" matching symbol, i.e., one with
697 exactly the same domain. PR 16253. */
698 if (sym
->matches (domain
))
699 other
= better_symbol (other
, sym
, domain
);
705 /* Note that parameter symbols do not always show up last in the
706 list; this loop makes sure to take anything else other than
707 parameter symbols first; it only uses parameter symbols as a
708 last resort. Note that this only takes up extra computation
710 It's hard to define types in the parameter list (at least in
711 C/C++) so we don't do the same PR 16253 hack here that is done
712 for the !BLOCK_FUNCTION case. */
714 struct symbol
*sym_found
= NULL
;
716 for (struct symbol
*sym
: block_iterator_range (block
, &name
))
718 if (sym
->matches (domain
))
721 if (!sym
->is_argument ())
727 return (sym_found
); /* Will be NULL if not found. */
734 block_lookup_symbol_primary (const struct block
*block
, const char *name
,
735 const domain_search_flags domain
)
737 struct symbol
*sym
, *other
;
738 struct mdict_iterator mdict_iter
;
740 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
742 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
743 gdb_assert (block
->superblock () == NULL
744 || block
->superblock ()->superblock () == NULL
);
747 for (sym
= mdict_iter_match_first (block
->multidict (), lookup_name
,
750 sym
= mdict_iter_match_next (lookup_name
, &mdict_iter
))
752 /* With the fix for PR gcc/debug/91507, we get for:
760 DWARF which will result in two entries in the symbol table, a decl
761 with type char *[] and a def with type char *[2].
763 If we return the decl here, we don't get the value of zzz:
765 $ gdb a.spec.out -batch -ex "p zzz"
768 because we're returning the symbol without location information, and
769 because the fallback that uses the address from the minimal symbols
770 doesn't work either because the type of the decl does not specify a
773 To fix this, we prefer def over decl in best_symbol and
776 In absence of the gcc fix, both def and decl have type char *[], so
777 the only option to make this work is improve the fallback to use the
778 size of the minimal symbol. Filed as PR exp/24989. */
779 if (best_symbol (sym
, domain
))
782 /* This is a bit of a hack, but 'matches' might ignore
783 STRUCT vs VAR domain symbols. So if a matching symbol is found,
784 make sure there is no "better" matching symbol, i.e., one with
785 exactly the same domain. PR 16253. */
786 if (sym
->matches (domain
))
787 other
= better_symbol (other
, sym
, domain
);
796 block_find_symbol (const struct block
*block
, const lookup_name_info
&name
,
797 const domain_search_flags domain
, struct symbol
**stub
)
799 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
800 gdb_assert (block
->superblock () == NULL
801 || block
->superblock ()->superblock () == NULL
);
803 for (struct symbol
*sym
: block_iterator_range (block
, &name
))
805 if (!sym
->matches (domain
))
808 if (!TYPE_IS_OPAQUE (sym
->type ()))
820 make_blockranges (struct objfile
*objfile
,
821 const std::vector
<blockrange
> &rangevec
)
823 struct blockranges
*blr
;
824 size_t n
= rangevec
.size();
826 blr
= (struct blockranges
*)
827 obstack_alloc (&objfile
->objfile_obstack
,
828 sizeof (struct blockranges
)
829 + (n
- 1) * sizeof (struct blockrange
));
832 for (int i
= 0; i
< n
; i
++)
833 blr
->range
[i
] = rangevec
[i
];