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/>. */
22 /* Local non-gdb includes. */
25 #include "cp-support.h"
26 #include "gdb_obstack.h"
32 /* This is used by struct block to store namespace-related info for
33 C++ files, namely using declarations and the current namespace in
36 struct block_namespace_info
: public allocate_on_obstack
38 const char *scope
= nullptr;
39 struct using_direct
*using_decl
= nullptr;
42 static void block_initialize_namespace (struct block
*block
,
43 struct obstack
*obstack
);
48 block_objfile (const struct block
*block
)
50 const struct global_block
*global_block
;
52 if (BLOCK_FUNCTION (block
) != NULL
)
53 return symbol_objfile (BLOCK_FUNCTION (block
));
55 global_block
= (struct global_block
*) block_global_block (block
);
56 return COMPUNIT_OBJFILE (global_block
->compunit_symtab
);
62 block_gdbarch (const struct block
*block
)
64 if (BLOCK_FUNCTION (block
) != NULL
)
65 return symbol_arch (BLOCK_FUNCTION (block
));
67 return get_objfile_arch (block_objfile (block
));
70 /* Return Nonzero if block a is lexically nested within block b,
71 or if a and b have the same pc range.
72 Return zero otherwise. */
75 contained_in (const struct block
*a
, const struct block
*b
)
84 /* If A is a function block, then A cannot be contained in B,
85 except if A was inlined. */
86 if (BLOCK_FUNCTION (a
) != NULL
&& !block_inlined_p (a
))
88 a
= BLOCK_SUPERBLOCK (a
);
96 /* Return the symbol for the function which contains a specified
97 lexical block, described by a struct block BL. The return value
98 will not be an inlined function; the containing function will be
102 block_linkage_function (const struct block
*bl
)
104 while ((BLOCK_FUNCTION (bl
) == NULL
|| block_inlined_p (bl
))
105 && BLOCK_SUPERBLOCK (bl
) != NULL
)
106 bl
= BLOCK_SUPERBLOCK (bl
);
108 return BLOCK_FUNCTION (bl
);
111 /* Return the symbol for the function which contains a specified
112 block, described by a struct block BL. The return value will be
113 the closest enclosing function, which might be an inline
117 block_containing_function (const struct block
*bl
)
119 while (BLOCK_FUNCTION (bl
) == NULL
&& BLOCK_SUPERBLOCK (bl
) != NULL
)
120 bl
= BLOCK_SUPERBLOCK (bl
);
122 return BLOCK_FUNCTION (bl
);
125 /* Return one if BL represents an inlined function. */
128 block_inlined_p (const struct block
*bl
)
130 return BLOCK_FUNCTION (bl
) != NULL
&& SYMBOL_INLINED (BLOCK_FUNCTION (bl
));
133 /* A helper function that checks whether PC is in the blockvector BL.
134 It returns the containing block if there is one, or else NULL. */
136 static const struct block
*
137 find_block_in_blockvector (const struct blockvector
*bl
, CORE_ADDR pc
)
139 const struct block
*b
;
142 /* If we have an addrmap mapping code addresses to blocks, then use
144 if (BLOCKVECTOR_MAP (bl
))
145 return (const struct block
*) addrmap_find (BLOCKVECTOR_MAP (bl
), pc
);
147 /* Otherwise, use binary search to find the last block that starts
149 Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1.
150 They both have the same START,END values.
151 Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the
152 fact that this choice was made was subtle, now we make it explicit. */
153 gdb_assert (BLOCKVECTOR_NBLOCKS (bl
) >= 2);
155 top
= BLOCKVECTOR_NBLOCKS (bl
);
157 while (top
- bot
> 1)
159 half
= (top
- bot
+ 1) >> 1;
160 b
= BLOCKVECTOR_BLOCK (bl
, bot
+ half
);
161 if (BLOCK_START (b
) <= pc
)
167 /* Now search backward for a block that ends after PC. */
169 while (bot
>= STATIC_BLOCK
)
171 b
= BLOCKVECTOR_BLOCK (bl
, bot
);
172 if (BLOCK_END (b
) > pc
)
180 /* Return the blockvector immediately containing the innermost lexical
181 block containing the specified pc value and section, or 0 if there
182 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
183 don't pass this information back to the caller. */
185 const struct blockvector
*
186 blockvector_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
,
187 const struct block
**pblock
,
188 struct compunit_symtab
*cust
)
190 const struct blockvector
*bl
;
191 const struct block
*b
;
195 /* First search all symtabs for one whose file contains our pc */
196 cust
= find_pc_sect_compunit_symtab (pc
, section
);
201 bl
= COMPUNIT_BLOCKVECTOR (cust
);
203 /* Then search that symtab for the smallest block that wins. */
204 b
= find_block_in_blockvector (bl
, pc
);
213 /* Return true if the blockvector BV contains PC, false otherwise. */
216 blockvector_contains_pc (const struct blockvector
*bv
, CORE_ADDR pc
)
218 return find_block_in_blockvector (bv
, pc
) != NULL
;
221 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
222 must be the next instruction after call (or after tail call jump). Throw
223 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
226 call_site_for_pc (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
228 struct compunit_symtab
*cust
;
231 /* -1 as tail call PC can be already after the compilation unit range. */
232 cust
= find_pc_compunit_symtab (pc
- 1);
234 if (cust
!= NULL
&& COMPUNIT_CALL_SITE_HTAB (cust
) != NULL
)
235 slot
= htab_find_slot (COMPUNIT_CALL_SITE_HTAB (cust
), &pc
, NO_INSERT
);
239 struct bound_minimal_symbol msym
= lookup_minimal_symbol_by_pc (pc
);
241 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
243 throw_error (NO_ENTRY_VALUE_ERROR
,
244 _("DW_OP_entry_value resolving cannot find "
245 "DW_TAG_call_site %s in %s"),
246 paddress (gdbarch
, pc
),
247 (msym
.minsym
== NULL
? "???"
248 : MSYMBOL_PRINT_NAME (msym
.minsym
)));
251 return (struct call_site
*) *slot
;
254 /* Return the blockvector immediately containing the innermost lexical block
255 containing the specified pc value, or 0 if there is none.
256 Backward compatibility, no section. */
258 const struct blockvector
*
259 blockvector_for_pc (CORE_ADDR pc
, const struct block
**pblock
)
261 return blockvector_for_pc_sect (pc
, find_pc_mapped_section (pc
),
265 /* Return the innermost lexical block containing the specified pc value
266 in the specified section, or 0 if there is none. */
269 block_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
)
271 const struct blockvector
*bl
;
272 const struct block
*b
;
274 bl
= blockvector_for_pc_sect (pc
, section
, &b
, NULL
);
280 /* Return the innermost lexical block containing the specified pc value,
281 or 0 if there is none. Backward compatibility, no section. */
284 block_for_pc (CORE_ADDR pc
)
286 return block_for_pc_sect (pc
, find_pc_mapped_section (pc
));
289 /* Now come some functions designed to deal with C++ namespace issues.
290 The accessors are safe to use even in the non-C++ case. */
292 /* This returns the namespace that BLOCK is enclosed in, or "" if it
293 isn't enclosed in a namespace at all. This travels the chain of
294 superblocks looking for a scope, if necessary. */
297 block_scope (const struct block
*block
)
299 for (; block
!= NULL
; block
= BLOCK_SUPERBLOCK (block
))
301 if (BLOCK_NAMESPACE (block
) != NULL
302 && BLOCK_NAMESPACE (block
)->scope
!= NULL
)
303 return BLOCK_NAMESPACE (block
)->scope
;
309 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
310 OBSTACK. (It won't make a copy of SCOPE, however, so that already
311 has to be allocated correctly.) */
314 block_set_scope (struct block
*block
, const char *scope
,
315 struct obstack
*obstack
)
317 block_initialize_namespace (block
, obstack
);
319 BLOCK_NAMESPACE (block
)->scope
= scope
;
322 /* This returns the using directives list associated with BLOCK, if
325 struct using_direct
*
326 block_using (const struct block
*block
)
328 if (block
== NULL
|| BLOCK_NAMESPACE (block
) == NULL
)
331 return BLOCK_NAMESPACE (block
)->using_decl
;
334 /* Set BLOCK's using member to USING; if needed, allocate memory via
335 OBSTACK. (It won't make a copy of USING, however, so that already
336 has to be allocated correctly.) */
339 block_set_using (struct block
*block
,
340 struct using_direct
*using_decl
,
341 struct obstack
*obstack
)
343 block_initialize_namespace (block
, obstack
);
345 BLOCK_NAMESPACE (block
)->using_decl
= using_decl
;
348 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
349 ititialize its members to zero. */
352 block_initialize_namespace (struct block
*block
, struct obstack
*obstack
)
354 if (BLOCK_NAMESPACE (block
) == NULL
)
355 BLOCK_NAMESPACE (block
) = new (obstack
) struct block_namespace_info ();
358 /* Return the static block associated to BLOCK. Return NULL if block
359 is NULL or if block is a global block. */
362 block_static_block (const struct block
*block
)
364 if (block
== NULL
|| BLOCK_SUPERBLOCK (block
) == NULL
)
367 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block
)) != NULL
)
368 block
= BLOCK_SUPERBLOCK (block
);
373 /* Return the static block associated to BLOCK. Return NULL if block
377 block_global_block (const struct block
*block
)
382 while (BLOCK_SUPERBLOCK (block
) != NULL
)
383 block
= BLOCK_SUPERBLOCK (block
);
388 /* Allocate a block on OBSTACK, and initialize its elements to
389 zero/NULL. This is useful for creating "dummy" blocks that don't
390 correspond to actual source files.
392 Warning: it sets the block's BLOCK_MULTIDICT to NULL, which isn't a
393 valid value. If you really don't want the block to have a
394 dictionary, then you should subsequently set its BLOCK_MULTIDICT to
395 dict_create_linear (obstack, NULL). */
398 allocate_block (struct obstack
*obstack
)
400 struct block
*bl
= OBSTACK_ZALLOC (obstack
, struct block
);
405 /* Allocate a global block. */
408 allocate_global_block (struct obstack
*obstack
)
410 struct global_block
*bl
= OBSTACK_ZALLOC (obstack
, struct global_block
);
415 /* Set the compunit of the global block. */
418 set_block_compunit_symtab (struct block
*block
, struct compunit_symtab
*cu
)
420 struct global_block
*gb
;
422 gdb_assert (BLOCK_SUPERBLOCK (block
) == NULL
);
423 gb
= (struct global_block
*) block
;
424 gdb_assert (gb
->compunit_symtab
== NULL
);
425 gb
->compunit_symtab
= cu
;
430 struct dynamic_prop
*
431 block_static_link (const struct block
*block
)
433 struct objfile
*objfile
= block_objfile (block
);
435 /* Only objfile-owned blocks that materialize top function scopes can have
437 if (objfile
== NULL
|| BLOCK_FUNCTION (block
) == NULL
)
440 return (struct dynamic_prop
*) objfile_lookup_static_link (objfile
, block
);
443 /* Return the compunit of the global block. */
445 static struct compunit_symtab
*
446 get_block_compunit_symtab (const struct block
*block
)
448 struct global_block
*gb
;
450 gdb_assert (BLOCK_SUPERBLOCK (block
) == NULL
);
451 gb
= (struct global_block
*) block
;
452 gdb_assert (gb
->compunit_symtab
!= NULL
);
453 return gb
->compunit_symtab
;
458 /* Initialize a block iterator, either to iterate over a single block,
459 or, for static and global blocks, all the included symtabs as
463 initialize_block_iterator (const struct block
*block
,
464 struct block_iterator
*iter
)
466 enum block_enum which
;
467 struct compunit_symtab
*cu
;
471 if (BLOCK_SUPERBLOCK (block
) == NULL
)
473 which
= GLOBAL_BLOCK
;
474 cu
= get_block_compunit_symtab (block
);
476 else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block
)) == NULL
)
478 which
= STATIC_BLOCK
;
479 cu
= get_block_compunit_symtab (BLOCK_SUPERBLOCK (block
));
483 iter
->d
.block
= block
;
484 /* A signal value meaning that we're iterating over a single
486 iter
->which
= FIRST_LOCAL_BLOCK
;
490 /* If this is an included symtab, find the canonical includer and
492 while (cu
->user
!= NULL
)
495 /* Putting this check here simplifies the logic of the iterator
496 functions. If there are no included symtabs, we only need to
497 search a single block, so we might as well just do that
499 if (cu
->includes
== NULL
)
501 iter
->d
.block
= block
;
502 /* A signal value meaning that we're iterating over a single
504 iter
->which
= FIRST_LOCAL_BLOCK
;
508 iter
->d
.compunit_symtab
= cu
;
513 /* A helper function that finds the current compunit over whose static
514 or global block we should iterate. */
516 static struct compunit_symtab
*
517 find_iterator_compunit_symtab (struct block_iterator
*iterator
)
519 if (iterator
->idx
== -1)
520 return iterator
->d
.compunit_symtab
;
521 return iterator
->d
.compunit_symtab
->includes
[iterator
->idx
];
524 /* Perform a single step for a plain block iterator, iterating across
525 symbol tables as needed. Returns the next symbol, or NULL when
526 iteration is complete. */
528 static struct symbol
*
529 block_iterator_step (struct block_iterator
*iterator
, int first
)
533 gdb_assert (iterator
->which
!= FIRST_LOCAL_BLOCK
);
539 struct compunit_symtab
*cust
540 = find_iterator_compunit_symtab (iterator
);
541 const struct block
*block
;
543 /* Iteration is complete. */
547 block
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust
),
549 sym
= mdict_iterator_first (BLOCK_MULTIDICT (block
),
550 &iterator
->mdict_iter
);
553 sym
= mdict_iterator_next (&iterator
->mdict_iter
);
558 /* We have finished iterating the appropriate block of one
559 symtab. Now advance to the next symtab and begin iteration
569 block_iterator_first (const struct block
*block
,
570 struct block_iterator
*iterator
)
572 initialize_block_iterator (block
, iterator
);
574 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
575 return mdict_iterator_first (block
->multidict
, &iterator
->mdict_iter
);
577 return block_iterator_step (iterator
, 1);
583 block_iterator_next (struct block_iterator
*iterator
)
585 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
586 return mdict_iterator_next (&iterator
->mdict_iter
);
588 return block_iterator_step (iterator
, 0);
591 /* Perform a single step for a "match" block iterator, iterating
592 across symbol tables as needed. Returns the next symbol, or NULL
593 when iteration is complete. */
595 static struct symbol
*
596 block_iter_match_step (struct block_iterator
*iterator
,
597 const lookup_name_info
&name
,
602 gdb_assert (iterator
->which
!= FIRST_LOCAL_BLOCK
);
608 struct compunit_symtab
*cust
609 = find_iterator_compunit_symtab (iterator
);
610 const struct block
*block
;
612 /* Iteration is complete. */
616 block
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust
),
618 sym
= mdict_iter_match_first (BLOCK_MULTIDICT (block
), name
,
619 &iterator
->mdict_iter
);
622 sym
= mdict_iter_match_next (name
, &iterator
->mdict_iter
);
627 /* We have finished iterating the appropriate block of one
628 symtab. Now advance to the next symtab and begin iteration
638 block_iter_match_first (const struct block
*block
,
639 const lookup_name_info
&name
,
640 struct block_iterator
*iterator
)
642 initialize_block_iterator (block
, iterator
);
644 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
645 return mdict_iter_match_first (block
->multidict
, name
,
646 &iterator
->mdict_iter
);
648 return block_iter_match_step (iterator
, name
, 1);
654 block_iter_match_next (const lookup_name_info
&name
,
655 struct block_iterator
*iterator
)
657 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
658 return mdict_iter_match_next (name
, &iterator
->mdict_iter
);
660 return block_iter_match_step (iterator
, name
, 0);
665 Note that if NAME is the demangled form of a C++ symbol, we will fail
666 to find a match during the binary search of the non-encoded names, but
667 for now we don't worry about the slight inefficiency of looking for
668 a match we'll never find, since it will go pretty quick. Once the
669 binary search terminates, we drop through and do a straight linear
670 search on the symbols. Each symbol which is marked as being a ObjC/C++
671 symbol (language_cplus or language_objc set) has both the encoded and
672 non-encoded names tested for a match. */
675 block_lookup_symbol (const struct block
*block
, const char *name
,
676 symbol_name_match_type match_type
,
677 const domain_enum domain
)
679 struct block_iterator iter
;
682 lookup_name_info
lookup_name (name
, match_type
);
684 if (!BLOCK_FUNCTION (block
))
686 struct symbol
*other
= NULL
;
688 ALL_BLOCK_SYMBOLS_WITH_NAME (block
, lookup_name
, iter
, sym
)
690 if (SYMBOL_DOMAIN (sym
) == domain
)
692 /* This is a bit of a hack, but symbol_matches_domain might ignore
693 STRUCT vs VAR domain symbols. So if a matching symbol is found,
694 make sure there is no "better" matching symbol, i.e., one with
695 exactly the same domain. PR 16253. */
696 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
697 SYMBOL_DOMAIN (sym
), domain
))
704 /* Note that parameter symbols do not always show up last in the
705 list; this loop makes sure to take anything else other than
706 parameter symbols first; it only uses parameter symbols as a
707 last resort. Note that this only takes up extra computation
709 It's hard to define types in the parameter list (at least in
710 C/C++) so we don't do the same PR 16253 hack here that is done
711 for the !BLOCK_FUNCTION case. */
713 struct symbol
*sym_found
= NULL
;
715 ALL_BLOCK_SYMBOLS_WITH_NAME (block
, lookup_name
, iter
, sym
)
717 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
718 SYMBOL_DOMAIN (sym
), domain
))
721 if (!SYMBOL_IS_ARGUMENT (sym
))
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_enum 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 (block
) == NULL
744 || BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block
)) == NULL
);
748 = mdict_iter_match_first (block
->multidict
, lookup_name
, &mdict_iter
);
750 sym
= mdict_iter_match_next (lookup_name
, &mdict_iter
))
752 if (SYMBOL_DOMAIN (sym
) == domain
)
755 /* This is a bit of a hack, but symbol_matches_domain might ignore
756 STRUCT vs VAR domain symbols. So if a matching symbol is found,
757 make sure there is no "better" matching symbol, i.e., one with
758 exactly the same domain. PR 16253. */
759 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
760 SYMBOL_DOMAIN (sym
), domain
))
770 block_find_symbol (const struct block
*block
, const char *name
,
771 const domain_enum domain
,
772 block_symbol_matcher_ftype
*matcher
, void *data
)
774 struct block_iterator iter
;
777 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
779 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
780 gdb_assert (BLOCK_SUPERBLOCK (block
) == NULL
781 || BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block
)) == NULL
);
783 ALL_BLOCK_SYMBOLS_WITH_NAME (block
, lookup_name
, iter
, sym
)
785 /* MATCHER is deliberately called second here so that it never sees
786 a non-domain-matching symbol. */
787 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
788 SYMBOL_DOMAIN (sym
), domain
)
789 && matcher (sym
, data
))
798 block_find_non_opaque_type (struct symbol
*sym
, void *data
)
800 return !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym
));
806 block_find_non_opaque_type_preferred (struct symbol
*sym
, void *data
)
808 struct symbol
**best
= (struct symbol
**) data
;
810 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym
)))
819 make_blockranges (struct objfile
*objfile
,
820 const std::vector
<blockrange
> &rangevec
)
822 struct blockranges
*blr
;
823 size_t n
= rangevec
.size();
825 blr
= (struct blockranges
*)
826 obstack_alloc (&objfile
->objfile_obstack
,
827 sizeof (struct blockranges
)
828 + (n
- 1) * sizeof (struct blockrange
));
831 for (int i
= 0; i
< n
; i
++)
832 blr
->range
[i
] = rangevec
[i
];