1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright (C) 2003-2018 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 /* If A is a function block, then A cannot be contained in B,
83 except if A was inlined. */
84 if (BLOCK_FUNCTION (a) != NULL && !block_inlined_p (a))
86 a = BLOCK_SUPERBLOCK (a);
94 /* Return the symbol for the function which contains a specified
95 lexical block, described by a struct block BL. The return value
96 will not be an inlined function; the containing function will be
100 block_linkage_function (const struct block *bl)
102 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
103 && BLOCK_SUPERBLOCK (bl) != NULL)
104 bl = BLOCK_SUPERBLOCK (bl);
106 return BLOCK_FUNCTION (bl);
109 /* Return the symbol for the function which contains a specified
110 block, described by a struct block BL. The return value will be
111 the closest enclosing function, which might be an inline
115 block_containing_function (const struct block *bl)
117 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
118 bl = BLOCK_SUPERBLOCK (bl);
120 return BLOCK_FUNCTION (bl);
123 /* Return one if BL represents an inlined function. */
126 block_inlined_p (const struct block *bl)
128 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
131 /* A helper function that checks whether PC is in the blockvector BL.
132 It returns the containing block if there is one, or else NULL. */
134 static struct block *
135 find_block_in_blockvector (const struct blockvector *bl, CORE_ADDR pc)
140 /* If we have an addrmap mapping code addresses to blocks, then use
142 if (BLOCKVECTOR_MAP (bl))
143 return (struct block *) addrmap_find (BLOCKVECTOR_MAP (bl), pc);
145 /* Otherwise, use binary search to find the last block that starts
147 Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1.
148 They both have the same START,END values.
149 Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the
150 fact that this choice was made was subtle, now we make it explicit. */
151 gdb_assert (BLOCKVECTOR_NBLOCKS (bl) >= 2);
153 top = BLOCKVECTOR_NBLOCKS (bl);
155 while (top - bot > 1)
157 half = (top - bot + 1) >> 1;
158 b = BLOCKVECTOR_BLOCK (bl, bot + half);
159 if (BLOCK_START (b) <= pc)
165 /* Now search backward for a block that ends after PC. */
167 while (bot >= STATIC_BLOCK)
169 b = BLOCKVECTOR_BLOCK (bl, bot);
170 if (BLOCK_END (b) > pc)
178 /* Return the blockvector immediately containing the innermost lexical
179 block containing the specified pc value and section, or 0 if there
180 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
181 don't pass this information back to the caller. */
183 const struct blockvector *
184 blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
185 const struct block **pblock,
186 struct compunit_symtab *cust)
188 const struct blockvector *bl;
193 /* First search all symtabs for one whose file contains our pc */
194 cust = find_pc_sect_compunit_symtab (pc, section);
199 bl = COMPUNIT_BLOCKVECTOR (cust);
201 /* Then search that symtab for the smallest block that wins. */
202 b = find_block_in_blockvector (bl, pc);
211 /* Return true if the blockvector BV contains PC, false otherwise. */
214 blockvector_contains_pc (const struct blockvector *bv, CORE_ADDR pc)
216 return find_block_in_blockvector (bv, pc) != NULL;
219 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
220 must be the next instruction after call (or after tail call jump). Throw
221 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
224 call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc)
226 struct compunit_symtab *cust;
229 /* -1 as tail call PC can be already after the compilation unit range. */
230 cust = find_pc_compunit_symtab (pc - 1);
232 if (cust != NULL && COMPUNIT_CALL_SITE_HTAB (cust) != NULL)
233 slot = htab_find_slot (COMPUNIT_CALL_SITE_HTAB (cust), &pc, NO_INSERT);
237 struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc);
239 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
241 throw_error (NO_ENTRY_VALUE_ERROR,
242 _("DW_OP_entry_value resolving cannot find "
243 "DW_TAG_call_site %s in %s"),
244 paddress (gdbarch, pc),
245 (msym.minsym == NULL ? "???"
246 : MSYMBOL_PRINT_NAME (msym.minsym)));
249 return (struct call_site *) *slot;
252 /* Return the blockvector immediately containing the innermost lexical block
253 containing the specified pc value, or 0 if there is none.
254 Backward compatibility, no section. */
256 const struct blockvector *
257 blockvector_for_pc (CORE_ADDR pc, const struct block **pblock)
259 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
263 /* Return the innermost lexical block containing the specified pc value
264 in the specified section, or 0 if there is none. */
267 block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
269 const struct blockvector *bl;
270 const struct block *b;
272 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
278 /* Return the innermost lexical block containing the specified pc value,
279 or 0 if there is none. Backward compatibility, no section. */
282 block_for_pc (CORE_ADDR pc)
284 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
287 /* Now come some functions designed to deal with C++ namespace issues.
288 The accessors are safe to use even in the non-C++ case. */
290 /* This returns the namespace that BLOCK is enclosed in, or "" if it
291 isn't enclosed in a namespace at all. This travels the chain of
292 superblocks looking for a scope, if necessary. */
295 block_scope (const struct block *block)
297 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
299 if (BLOCK_NAMESPACE (block) != NULL
300 && BLOCK_NAMESPACE (block)->scope != NULL)
301 return BLOCK_NAMESPACE (block)->scope;
307 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
308 OBSTACK. (It won't make a copy of SCOPE, however, so that already
309 has to be allocated correctly.) */
312 block_set_scope (struct block *block, const char *scope,
313 struct obstack *obstack)
315 block_initialize_namespace (block, obstack);
317 BLOCK_NAMESPACE (block)->scope = scope;
320 /* This returns the using directives list associated with BLOCK, if
323 struct using_direct *
324 block_using (const struct block *block)
326 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
329 return BLOCK_NAMESPACE (block)->using_decl;
332 /* Set BLOCK's using member to USING; if needed, allocate memory via
333 OBSTACK. (It won't make a copy of USING, however, so that already
334 has to be allocated correctly.) */
337 block_set_using (struct block *block,
338 struct using_direct *using_decl,
339 struct obstack *obstack)
341 block_initialize_namespace (block, obstack);
343 BLOCK_NAMESPACE (block)->using_decl = using_decl;
346 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
347 ititialize its members to zero. */
350 block_initialize_namespace (struct block *block, struct obstack *obstack)
352 if (BLOCK_NAMESPACE (block) == NULL)
353 BLOCK_NAMESPACE (block) = new (obstack) struct block_namespace_info ();
356 /* Return the static block associated to BLOCK. Return NULL if block
357 is NULL or if block is a global block. */
360 block_static_block (const struct block *block)
362 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
365 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
366 block = BLOCK_SUPERBLOCK (block);
371 /* Return the static block associated to BLOCK. Return NULL if block
375 block_global_block (const struct block *block)
380 while (BLOCK_SUPERBLOCK (block) != NULL)
381 block = BLOCK_SUPERBLOCK (block);
386 /* Allocate a block on OBSTACK, and initialize its elements to
387 zero/NULL. This is useful for creating "dummy" blocks that don't
388 correspond to actual source files.
390 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
391 valid value. If you really don't want the block to have a
392 dictionary, then you should subsequently set its BLOCK_DICT to
393 dict_create_linear (obstack, NULL). */
396 allocate_block (struct obstack *obstack)
398 struct block *bl = OBSTACK_ZALLOC (obstack, struct block);
403 /* Allocate a global block. */
406 allocate_global_block (struct obstack *obstack)
408 struct global_block *bl = OBSTACK_ZALLOC (obstack, struct global_block);
413 /* Set the compunit of the global block. */
416 set_block_compunit_symtab (struct block *block, struct compunit_symtab *cu)
418 struct global_block *gb;
420 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
421 gb = (struct global_block *) block;
422 gdb_assert (gb->compunit_symtab == NULL);
423 gb->compunit_symtab = cu;
428 struct dynamic_prop *
429 block_static_link (const struct block *block)
431 struct objfile *objfile = block_objfile (block);
433 /* Only objfile-owned blocks that materialize top function scopes can have
435 if (objfile == NULL || BLOCK_FUNCTION (block) == NULL)
438 return (struct dynamic_prop *) objfile_lookup_static_link (objfile, block);
441 /* Return the compunit of the global block. */
443 static struct compunit_symtab *
444 get_block_compunit_symtab (const struct block *block)
446 struct global_block *gb;
448 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
449 gb = (struct global_block *) block;
450 gdb_assert (gb->compunit_symtab != NULL);
451 return gb->compunit_symtab;
456 /* Initialize a block iterator, either to iterate over a single block,
457 or, for static and global blocks, all the included symtabs as
461 initialize_block_iterator (const struct block *block,
462 struct block_iterator *iter)
464 enum block_enum which;
465 struct compunit_symtab *cu;
469 if (BLOCK_SUPERBLOCK (block) == NULL)
471 which = GLOBAL_BLOCK;
472 cu = get_block_compunit_symtab (block);
474 else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL)
476 which = STATIC_BLOCK;
477 cu = get_block_compunit_symtab (BLOCK_SUPERBLOCK (block));
481 iter->d.block = block;
482 /* A signal value meaning that we're iterating over a single
484 iter->which = FIRST_LOCAL_BLOCK;
488 /* If this is an included symtab, find the canonical includer and
490 while (cu->user != NULL)
493 /* Putting this check here simplifies the logic of the iterator
494 functions. If there are no included symtabs, we only need to
495 search a single block, so we might as well just do that
497 if (cu->includes == NULL)
499 iter->d.block = block;
500 /* A signal value meaning that we're iterating over a single
502 iter->which = FIRST_LOCAL_BLOCK;
506 iter->d.compunit_symtab = cu;
511 /* A helper function that finds the current compunit over whose static
512 or global block we should iterate. */
514 static struct compunit_symtab *
515 find_iterator_compunit_symtab (struct block_iterator *iterator)
517 if (iterator->idx == -1)
518 return iterator->d.compunit_symtab;
519 return iterator->d.compunit_symtab->includes[iterator->idx];
522 /* Perform a single step for a plain block iterator, iterating across
523 symbol tables as needed. Returns the next symbol, or NULL when
524 iteration is complete. */
526 static struct symbol *
527 block_iterator_step (struct block_iterator *iterator, int first)
531 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
537 struct compunit_symtab *cust
538 = find_iterator_compunit_symtab (iterator);
539 const struct block *block;
541 /* Iteration is complete. */
545 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust),
547 sym = dict_iterator_first (BLOCK_DICT (block), &iterator->dict_iter);
550 sym = dict_iterator_next (&iterator->dict_iter);
555 /* We have finished iterating the appropriate block of one
556 symtab. Now advance to the next symtab and begin iteration
566 block_iterator_first (const struct block *block,
567 struct block_iterator *iterator)
569 initialize_block_iterator (block, iterator);
571 if (iterator->which == FIRST_LOCAL_BLOCK)
572 return dict_iterator_first (block->dict, &iterator->dict_iter);
574 return block_iterator_step (iterator, 1);
580 block_iterator_next (struct block_iterator *iterator)
582 if (iterator->which == FIRST_LOCAL_BLOCK)
583 return dict_iterator_next (&iterator->dict_iter);
585 return block_iterator_step (iterator, 0);
588 /* Perform a single step for a "match" block iterator, iterating
589 across symbol tables as needed. Returns the next symbol, or NULL
590 when iteration is complete. */
592 static struct symbol *
593 block_iter_match_step (struct block_iterator *iterator,
594 const lookup_name_info &name,
599 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
605 struct compunit_symtab *cust
606 = find_iterator_compunit_symtab (iterator);
607 const struct block *block;
609 /* Iteration is complete. */
613 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust),
615 sym = dict_iter_match_first (BLOCK_DICT (block), name,
616 &iterator->dict_iter);
619 sym = dict_iter_match_next (name, &iterator->dict_iter);
624 /* We have finished iterating the appropriate block of one
625 symtab. Now advance to the next symtab and begin iteration
635 block_iter_match_first (const struct block *block,
636 const lookup_name_info &name,
637 struct block_iterator *iterator)
639 initialize_block_iterator (block, iterator);
641 if (iterator->which == FIRST_LOCAL_BLOCK)
642 return dict_iter_match_first (block->dict, name, &iterator->dict_iter);
644 return block_iter_match_step (iterator, name, 1);
650 block_iter_match_next (const lookup_name_info &name,
651 struct block_iterator *iterator)
653 if (iterator->which == FIRST_LOCAL_BLOCK)
654 return dict_iter_match_next (name, &iterator->dict_iter);
656 return block_iter_match_step (iterator, name, 0);
661 Note that if NAME is the demangled form of a C++ symbol, we will fail
662 to find a match during the binary search of the non-encoded names, but
663 for now we don't worry about the slight inefficiency of looking for
664 a match we'll never find, since it will go pretty quick. Once the
665 binary search terminates, we drop through and do a straight linear
666 search on the symbols. Each symbol which is marked as being a ObjC/C++
667 symbol (language_cplus or language_objc set) has both the encoded and
668 non-encoded names tested for a match. */
671 block_lookup_symbol (const struct block *block, const char *name,
672 symbol_name_match_type match_type,
673 const domain_enum domain)
675 struct block_iterator iter;
678 lookup_name_info lookup_name (name, match_type);
680 if (!BLOCK_FUNCTION (block))
682 struct symbol *other = NULL;
684 ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
686 if (SYMBOL_DOMAIN (sym) == domain)
688 /* This is a bit of a hack, but symbol_matches_domain might ignore
689 STRUCT vs VAR domain symbols. So if a matching symbol is found,
690 make sure there is no "better" matching symbol, i.e., one with
691 exactly the same domain. PR 16253. */
692 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
693 SYMBOL_DOMAIN (sym), domain))
700 /* Note that parameter symbols do not always show up last in the
701 list; this loop makes sure to take anything else other than
702 parameter symbols first; it only uses parameter symbols as a
703 last resort. Note that this only takes up extra computation
705 It's hard to define types in the parameter list (at least in
706 C/C++) so we don't do the same PR 16253 hack here that is done
707 for the !BLOCK_FUNCTION case. */
709 struct symbol *sym_found = NULL;
711 ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
713 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
714 SYMBOL_DOMAIN (sym), domain))
717 if (!SYMBOL_IS_ARGUMENT (sym))
723 return (sym_found); /* Will be NULL if not found. */
730 block_lookup_symbol_primary (const struct block *block, const char *name,
731 const domain_enum domain)
733 struct symbol *sym, *other;
734 struct dict_iterator dict_iter;
736 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
738 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
739 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL
740 || BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL);
743 for (sym = dict_iter_match_first (block->dict, lookup_name, &dict_iter);
745 sym = dict_iter_match_next (lookup_name, &dict_iter))
747 if (SYMBOL_DOMAIN (sym) == domain)
750 /* This is a bit of a hack, but symbol_matches_domain might ignore
751 STRUCT vs VAR domain symbols. So if a matching symbol is found,
752 make sure there is no "better" matching symbol, i.e., one with
753 exactly the same domain. PR 16253. */
754 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
755 SYMBOL_DOMAIN (sym), domain))
765 block_find_symbol (const struct block *block, const char *name,
766 const domain_enum domain,
767 block_symbol_matcher_ftype *matcher, void *data)
769 struct block_iterator iter;
772 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
774 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
775 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL
776 || BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL);
778 ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
780 /* MATCHER is deliberately called second here so that it never sees
781 a non-domain-matching symbol. */
782 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
783 SYMBOL_DOMAIN (sym), domain)
784 && matcher (sym, data))
793 block_find_non_opaque_type (struct symbol *sym, void *data)
795 return !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym));
801 block_find_non_opaque_type_preferred (struct symbol *sym, void *data)
803 struct symbol **best = (struct symbol **) data;
805 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
814 make_blockranges (struct objfile *objfile,
815 const std::vector<blockrange> &rangevec)
817 struct blockranges *blr;
818 size_t n = rangevec.size();
820 blr = (struct blockranges *)
821 obstack_alloc (&objfile->objfile_obstack,
822 sizeof (struct blockranges)
823 + (n - 1) * sizeof (struct blockrange));
826 for (int i = 0; i < n; i++)
827 blr->range[i] = rangevec[i];