1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright (C) 2003-2019 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 "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;
40 static void block_initialize_namespace (struct block
*block
,
41 struct obstack
*obstack
);
46 block_objfile (const struct block
*block
)
48 const struct global_block
*global_block
;
50 if (BLOCK_FUNCTION (block
) != NULL
)
51 return symbol_objfile (BLOCK_FUNCTION (block
));
53 global_block
= (struct global_block
*) block_global_block (block
);
54 return COMPUNIT_OBJFILE (global_block
->compunit_symtab
);
60 block_gdbarch (const struct block
*block
)
62 if (BLOCK_FUNCTION (block
) != NULL
)
63 return symbol_arch (BLOCK_FUNCTION (block
));
65 return get_objfile_arch (block_objfile (block
));
68 /* Return Nonzero if block a is lexically nested within block b,
69 or if a and b have the same pc range.
70 Return zero otherwise. */
73 contained_in (const struct block
*a
, const struct block
*b
)
82 a
= BLOCK_SUPERBLOCK (a
);
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 ((BLOCK_FUNCTION (bl
) == NULL
|| block_inlined_p (bl
))
99 && BLOCK_SUPERBLOCK (bl
) != NULL
)
100 bl
= BLOCK_SUPERBLOCK (bl
);
102 return BLOCK_FUNCTION (bl
);
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 (BLOCK_FUNCTION (bl
) == NULL
&& BLOCK_SUPERBLOCK (bl
) != NULL
)
114 bl
= BLOCK_SUPERBLOCK (bl
);
116 return BLOCK_FUNCTION (bl
);
119 /* Return one if BL represents an inlined function. */
122 block_inlined_p (const struct block
*bl
)
124 return BLOCK_FUNCTION (bl
) != NULL
&& SYMBOL_INLINED (BLOCK_FUNCTION (bl
));
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
138 if (BLOCKVECTOR_MAP (bl
))
139 return (const struct block
*) addrmap_find (BLOCKVECTOR_MAP (bl
), 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 (BLOCKVECTOR_NBLOCKS (bl
) >= 2);
149 top
= BLOCKVECTOR_NBLOCKS (bl
);
151 while (top
- bot
> 1)
153 half
= (top
- bot
+ 1) >> 1;
154 b
= BLOCKVECTOR_BLOCK (bl
, bot
+ half
);
155 if (BLOCK_START (b
) <= pc
)
161 /* Now search backward for a block that ends after PC. */
163 while (bot
>= STATIC_BLOCK
)
165 b
= BLOCKVECTOR_BLOCK (bl
, bot
);
166 if (BLOCK_END (b
) > pc
)
174 /* Return the blockvector immediately containing the innermost lexical
175 block containing the specified pc value and section, or 0 if there
176 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
177 don't pass this information back to the caller. */
179 const struct blockvector
*
180 blockvector_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
,
181 const struct block
**pblock
,
182 struct compunit_symtab
*cust
)
184 const struct blockvector
*bl
;
185 const struct block
*b
;
189 /* First search all symtabs for one whose file contains our pc */
190 cust
= find_pc_sect_compunit_symtab (pc
, section
);
195 bl
= COMPUNIT_BLOCKVECTOR (cust
);
197 /* Then search that symtab for the smallest block that wins. */
198 b
= find_block_in_blockvector (bl
, pc
);
207 /* Return true if the blockvector BV contains PC, false otherwise. */
210 blockvector_contains_pc (const struct blockvector
*bv
, CORE_ADDR pc
)
212 return find_block_in_blockvector (bv
, pc
) != NULL
;
215 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
216 must be the next instruction after call (or after tail call jump). Throw
217 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
220 call_site_for_pc (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
222 struct compunit_symtab
*cust
;
225 /* -1 as tail call PC can be already after the compilation unit range. */
226 cust
= find_pc_compunit_symtab (pc
- 1);
228 if (cust
!= NULL
&& COMPUNIT_CALL_SITE_HTAB (cust
) != NULL
)
229 slot
= htab_find_slot (COMPUNIT_CALL_SITE_HTAB (cust
), &pc
, NO_INSERT
);
233 struct bound_minimal_symbol msym
= lookup_minimal_symbol_by_pc (pc
);
235 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
237 throw_error (NO_ENTRY_VALUE_ERROR
,
238 _("DW_OP_entry_value resolving cannot find "
239 "DW_TAG_call_site %s in %s"),
240 paddress (gdbarch
, pc
),
241 (msym
.minsym
== NULL
? "???"
242 : MSYMBOL_PRINT_NAME (msym
.minsym
)));
245 return (struct call_site
*) *slot
;
248 /* Return the blockvector immediately containing the innermost lexical block
249 containing the specified pc value, or 0 if there is none.
250 Backward compatibility, no section. */
252 const struct blockvector
*
253 blockvector_for_pc (CORE_ADDR pc
, const struct block
**pblock
)
255 return blockvector_for_pc_sect (pc
, find_pc_mapped_section (pc
),
259 /* Return the innermost lexical block containing the specified pc value
260 in the specified section, or 0 if there is none. */
263 block_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
)
265 const struct blockvector
*bl
;
266 const struct block
*b
;
268 bl
= blockvector_for_pc_sect (pc
, section
, &b
, NULL
);
274 /* Return the innermost lexical block containing the specified pc value,
275 or 0 if there is none. Backward compatibility, no section. */
278 block_for_pc (CORE_ADDR pc
)
280 return block_for_pc_sect (pc
, find_pc_mapped_section (pc
));
283 /* Now come some functions designed to deal with C++ namespace issues.
284 The accessors are safe to use even in the non-C++ case. */
286 /* This returns the namespace that BLOCK is enclosed in, or "" if it
287 isn't enclosed in a namespace at all. This travels the chain of
288 superblocks looking for a scope, if necessary. */
291 block_scope (const struct block
*block
)
293 for (; block
!= NULL
; block
= BLOCK_SUPERBLOCK (block
))
295 if (BLOCK_NAMESPACE (block
) != NULL
296 && BLOCK_NAMESPACE (block
)->scope
!= NULL
)
297 return BLOCK_NAMESPACE (block
)->scope
;
303 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
304 OBSTACK. (It won't make a copy of SCOPE, however, so that already
305 has to be allocated correctly.) */
308 block_set_scope (struct block
*block
, const char *scope
,
309 struct obstack
*obstack
)
311 block_initialize_namespace (block
, obstack
);
313 BLOCK_NAMESPACE (block
)->scope
= scope
;
316 /* This returns the using directives list associated with BLOCK, if
319 struct using_direct
*
320 block_using (const struct block
*block
)
322 if (block
== NULL
|| BLOCK_NAMESPACE (block
) == NULL
)
325 return BLOCK_NAMESPACE (block
)->using_decl
;
328 /* Set BLOCK's using member to USING; if needed, allocate memory via
329 OBSTACK. (It won't make a copy of USING, however, so that already
330 has to be allocated correctly.) */
333 block_set_using (struct block
*block
,
334 struct using_direct
*using_decl
,
335 struct obstack
*obstack
)
337 block_initialize_namespace (block
, obstack
);
339 BLOCK_NAMESPACE (block
)->using_decl
= using_decl
;
342 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
343 ititialize its members to zero. */
346 block_initialize_namespace (struct block
*block
, struct obstack
*obstack
)
348 if (BLOCK_NAMESPACE (block
) == NULL
)
349 BLOCK_NAMESPACE (block
) = new (obstack
) struct block_namespace_info ();
352 /* Return the static block associated to BLOCK. Return NULL if block
353 is NULL or if block is a global block. */
356 block_static_block (const struct block
*block
)
358 if (block
== NULL
|| BLOCK_SUPERBLOCK (block
) == NULL
)
361 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block
)) != NULL
)
362 block
= BLOCK_SUPERBLOCK (block
);
367 /* Return the static block associated to BLOCK. Return NULL if block
371 block_global_block (const struct block
*block
)
376 while (BLOCK_SUPERBLOCK (block
) != NULL
)
377 block
= BLOCK_SUPERBLOCK (block
);
382 /* Allocate a block on OBSTACK, and initialize its elements to
383 zero/NULL. This is useful for creating "dummy" blocks that don't
384 correspond to actual source files.
386 Warning: it sets the block's BLOCK_MULTIDICT to NULL, which isn't a
387 valid value. If you really don't want the block to have a
388 dictionary, then you should subsequently set its BLOCK_MULTIDICT to
389 dict_create_linear (obstack, NULL). */
392 allocate_block (struct obstack
*obstack
)
394 struct block
*bl
= OBSTACK_ZALLOC (obstack
, struct block
);
399 /* Allocate a global block. */
402 allocate_global_block (struct obstack
*obstack
)
404 struct global_block
*bl
= OBSTACK_ZALLOC (obstack
, struct global_block
);
409 /* Set the compunit of the global block. */
412 set_block_compunit_symtab (struct block
*block
, struct compunit_symtab
*cu
)
414 struct global_block
*gb
;
416 gdb_assert (BLOCK_SUPERBLOCK (block
) == NULL
);
417 gb
= (struct global_block
*) block
;
418 gdb_assert (gb
->compunit_symtab
== NULL
);
419 gb
->compunit_symtab
= cu
;
424 struct dynamic_prop
*
425 block_static_link (const struct block
*block
)
427 struct objfile
*objfile
= block_objfile (block
);
429 /* Only objfile-owned blocks that materialize top function scopes can have
431 if (objfile
== NULL
|| BLOCK_FUNCTION (block
) == NULL
)
434 return (struct dynamic_prop
*) objfile_lookup_static_link (objfile
, block
);
437 /* Return the compunit of the global block. */
439 static struct compunit_symtab
*
440 get_block_compunit_symtab (const struct block
*block
)
442 struct global_block
*gb
;
444 gdb_assert (BLOCK_SUPERBLOCK (block
) == NULL
);
445 gb
= (struct global_block
*) block
;
446 gdb_assert (gb
->compunit_symtab
!= NULL
);
447 return gb
->compunit_symtab
;
452 /* Initialize a block iterator, either to iterate over a single block,
453 or, for static and global blocks, all the included symtabs as
457 initialize_block_iterator (const struct block
*block
,
458 struct block_iterator
*iter
)
460 enum block_enum which
;
461 struct compunit_symtab
*cu
;
465 if (BLOCK_SUPERBLOCK (block
) == NULL
)
467 which
= GLOBAL_BLOCK
;
468 cu
= get_block_compunit_symtab (block
);
470 else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block
)) == NULL
)
472 which
= STATIC_BLOCK
;
473 cu
= get_block_compunit_symtab (BLOCK_SUPERBLOCK (block
));
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
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust
),
543 sym
= mdict_iterator_first (BLOCK_MULTIDICT (block
),
544 &iterator
->mdict_iter
);
547 sym
= mdict_iterator_next (&iterator
->mdict_iter
);
552 /* We have finished iterating the appropriate block of one
553 symtab. Now advance to the next symtab and begin iteration
563 block_iterator_first (const struct block
*block
,
564 struct block_iterator
*iterator
)
566 initialize_block_iterator (block
, iterator
);
568 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
569 return mdict_iterator_first (block
->multidict
, &iterator
->mdict_iter
);
571 return block_iterator_step (iterator
, 1);
577 block_iterator_next (struct block_iterator
*iterator
)
579 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
580 return mdict_iterator_next (&iterator
->mdict_iter
);
582 return block_iterator_step (iterator
, 0);
585 /* Perform a single step for a "match" block iterator, iterating
586 across symbol tables as needed. Returns the next symbol, or NULL
587 when iteration is complete. */
589 static struct symbol
*
590 block_iter_match_step (struct block_iterator
*iterator
,
591 const lookup_name_info
&name
,
596 gdb_assert (iterator
->which
!= FIRST_LOCAL_BLOCK
);
602 struct compunit_symtab
*cust
603 = find_iterator_compunit_symtab (iterator
);
604 const struct block
*block
;
606 /* Iteration is complete. */
610 block
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust
),
612 sym
= mdict_iter_match_first (BLOCK_MULTIDICT (block
), name
,
613 &iterator
->mdict_iter
);
616 sym
= mdict_iter_match_next (name
, &iterator
->mdict_iter
);
621 /* We have finished iterating the appropriate block of one
622 symtab. Now advance to the next symtab and begin iteration
632 block_iter_match_first (const struct block
*block
,
633 const lookup_name_info
&name
,
634 struct block_iterator
*iterator
)
636 initialize_block_iterator (block
, iterator
);
638 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
639 return mdict_iter_match_first (block
->multidict
, name
,
640 &iterator
->mdict_iter
);
642 return block_iter_match_step (iterator
, name
, 1);
648 block_iter_match_next (const lookup_name_info
&name
,
649 struct block_iterator
*iterator
)
651 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
652 return mdict_iter_match_next (name
, &iterator
->mdict_iter
);
654 return block_iter_match_step (iterator
, name
, 0);
659 Note that if NAME is the demangled form of a C++ symbol, we will fail
660 to find a match during the binary search of the non-encoded names, but
661 for now we don't worry about the slight inefficiency of looking for
662 a match we'll never find, since it will go pretty quick. Once the
663 binary search terminates, we drop through and do a straight linear
664 search on the symbols. Each symbol which is marked as being a ObjC/C++
665 symbol (language_cplus or language_objc set) has both the encoded and
666 non-encoded names tested for a match. */
669 block_lookup_symbol (const struct block
*block
, const char *name
,
670 symbol_name_match_type match_type
,
671 const domain_enum domain
)
673 struct block_iterator iter
;
676 lookup_name_info
lookup_name (name
, match_type
);
678 if (!BLOCK_FUNCTION (block
))
680 struct symbol
*other
= NULL
;
682 ALL_BLOCK_SYMBOLS_WITH_NAME (block
, lookup_name
, iter
, sym
)
684 if (SYMBOL_DOMAIN (sym
) == domain
)
686 /* This is a bit of a hack, but symbol_matches_domain might ignore
687 STRUCT vs VAR domain symbols. So if a matching symbol is found,
688 make sure there is no "better" matching symbol, i.e., one with
689 exactly the same domain. PR 16253. */
690 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
691 SYMBOL_DOMAIN (sym
), domain
))
698 /* Note that parameter symbols do not always show up last in the
699 list; this loop makes sure to take anything else other than
700 parameter symbols first; it only uses parameter symbols as a
701 last resort. Note that this only takes up extra computation
703 It's hard to define types in the parameter list (at least in
704 C/C++) so we don't do the same PR 16253 hack here that is done
705 for the !BLOCK_FUNCTION case. */
707 struct symbol
*sym_found
= NULL
;
709 ALL_BLOCK_SYMBOLS_WITH_NAME (block
, lookup_name
, iter
, sym
)
711 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
712 SYMBOL_DOMAIN (sym
), domain
))
715 if (!SYMBOL_IS_ARGUMENT (sym
))
721 return (sym_found
); /* Will be NULL if not found. */
728 block_lookup_symbol_primary (const struct block
*block
, const char *name
,
729 const domain_enum domain
)
731 struct symbol
*sym
, *other
;
732 struct mdict_iterator mdict_iter
;
734 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
736 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
737 gdb_assert (BLOCK_SUPERBLOCK (block
) == NULL
738 || BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block
)) == NULL
);
742 = mdict_iter_match_first (block
->multidict
, lookup_name
, &mdict_iter
);
744 sym
= mdict_iter_match_next (lookup_name
, &mdict_iter
))
746 if (SYMBOL_DOMAIN (sym
) == domain
)
749 /* This is a bit of a hack, but symbol_matches_domain might ignore
750 STRUCT vs VAR domain symbols. So if a matching symbol is found,
751 make sure there is no "better" matching symbol, i.e., one with
752 exactly the same domain. PR 16253. */
753 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
754 SYMBOL_DOMAIN (sym
), domain
))
764 block_find_symbol (const struct block
*block
, const char *name
,
765 const domain_enum domain
,
766 block_symbol_matcher_ftype
*matcher
, void *data
)
768 struct block_iterator iter
;
771 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
773 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
774 gdb_assert (BLOCK_SUPERBLOCK (block
) == NULL
775 || BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block
)) == NULL
);
777 ALL_BLOCK_SYMBOLS_WITH_NAME (block
, lookup_name
, iter
, sym
)
779 /* MATCHER is deliberately called second here so that it never sees
780 a non-domain-matching symbol. */
781 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
782 SYMBOL_DOMAIN (sym
), domain
)
783 && matcher (sym
, data
))
792 block_find_non_opaque_type (struct symbol
*sym
, void *data
)
794 return !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym
));
800 block_find_non_opaque_type_preferred (struct symbol
*sym
, void *data
)
802 struct symbol
**best
= (struct symbol
**) data
;
804 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym
)))
813 make_blockranges (struct objfile
*objfile
,
814 const std::vector
<blockrange
> &rangevec
)
816 struct blockranges
*blr
;
817 size_t n
= rangevec
.size();
819 blr
= (struct blockranges
*)
820 obstack_alloc (&objfile
->objfile_obstack
,
821 sizeof (struct blockranges
)
822 + (n
- 1) * sizeof (struct blockrange
));
825 for (int i
= 0; i
< n
; i
++)
826 blr
->range
[i
] = rangevec
[i
];