1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright (C) 2003-2014 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"
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
36 struct using_direct *using;
39 static void block_initialize_namespace (struct block *block,
40 struct obstack *obstack);
42 /* Return Nonzero if block a is lexically nested within block b,
43 or if a and b have the same pc range.
44 Return zero otherwise. */
47 contained_in (const struct block *a, const struct block *b)
56 /* If A is a function block, then A cannot be contained in B,
57 except if A was inlined. */
58 if (BLOCK_FUNCTION (a) != NULL && !block_inlined_p (a))
60 a = BLOCK_SUPERBLOCK (a);
68 /* Return the symbol for the function which contains a specified
69 lexical block, described by a struct block BL. The return value
70 will not be an inlined function; the containing function will be
74 block_linkage_function (const struct block *bl)
76 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
77 && BLOCK_SUPERBLOCK (bl) != NULL)
78 bl = BLOCK_SUPERBLOCK (bl);
80 return BLOCK_FUNCTION (bl);
83 /* Return the symbol for the function which contains a specified
84 block, described by a struct block BL. The return value will be
85 the closest enclosing function, which might be an inline
89 block_containing_function (const struct block *bl)
91 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
92 bl = BLOCK_SUPERBLOCK (bl);
94 return BLOCK_FUNCTION (bl);
97 /* Return one if BL represents an inlined function. */
100 block_inlined_p (const struct block *bl)
102 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
105 /* A helper function that checks whether PC is in the blockvector BL.
106 It returns the containing block if there is one, or else NULL. */
108 static struct block *
109 find_block_in_blockvector (const struct blockvector *bl, CORE_ADDR pc)
114 /* If we have an addrmap mapping code addresses to blocks, then use
116 if (BLOCKVECTOR_MAP (bl))
117 return addrmap_find (BLOCKVECTOR_MAP (bl), pc);
119 /* Otherwise, use binary search to find the last block that starts
121 Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1.
122 They both have the same START,END values.
123 Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the
124 fact that this choice was made was subtle, now we make it explicit. */
125 gdb_assert (BLOCKVECTOR_NBLOCKS (bl) >= 2);
127 top = BLOCKVECTOR_NBLOCKS (bl);
129 while (top - bot > 1)
131 half = (top - bot + 1) >> 1;
132 b = BLOCKVECTOR_BLOCK (bl, bot + half);
133 if (BLOCK_START (b) <= pc)
139 /* Now search backward for a block that ends after PC. */
141 while (bot >= STATIC_BLOCK)
143 b = BLOCKVECTOR_BLOCK (bl, bot);
144 if (BLOCK_END (b) > pc)
152 /* Return the blockvector immediately containing the innermost lexical
153 block containing the specified pc value and section, or 0 if there
154 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
155 don't pass this information back to the caller. */
157 const struct blockvector *
158 blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
159 const struct block **pblock, struct symtab *symtab)
161 const struct blockvector *bl;
164 if (symtab == 0) /* if no symtab specified by caller */
166 /* First search all symtabs for one whose file contains our pc */
167 symtab = find_pc_sect_symtab (pc, section);
172 bl = BLOCKVECTOR (symtab);
174 /* Then search that symtab for the smallest block that wins. */
175 b = find_block_in_blockvector (bl, pc);
184 /* Return true if the blockvector BV contains PC, false otherwise. */
187 blockvector_contains_pc (const struct blockvector *bv, CORE_ADDR pc)
189 return find_block_in_blockvector (bv, pc) != NULL;
192 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
193 must be the next instruction after call (or after tail call jump). Throw
194 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
197 call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc)
199 struct symtab *symtab;
202 /* -1 as tail call PC can be already after the compilation unit range. */
203 symtab = find_pc_symtab (pc - 1);
205 if (symtab != NULL && symtab->call_site_htab != NULL)
206 slot = htab_find_slot (symtab->call_site_htab, &pc, NO_INSERT);
210 struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc);
212 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
214 throw_error (NO_ENTRY_VALUE_ERROR,
215 _("DW_OP_GNU_entry_value resolving cannot find "
216 "DW_TAG_GNU_call_site %s in %s"),
217 paddress (gdbarch, pc),
218 (msym.minsym == NULL ? "???"
219 : MSYMBOL_PRINT_NAME (msym.minsym)));
225 /* Return the blockvector immediately containing the innermost lexical block
226 containing the specified pc value, or 0 if there is none.
227 Backward compatibility, no section. */
229 const struct blockvector *
230 blockvector_for_pc (CORE_ADDR pc, const struct block **pblock)
232 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
236 /* Return the innermost lexical block containing the specified pc value
237 in the specified section, or 0 if there is none. */
240 block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
242 const struct blockvector *bl;
243 const struct block *b;
245 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
251 /* Return the innermost lexical block containing the specified pc value,
252 or 0 if there is none. Backward compatibility, no section. */
255 block_for_pc (CORE_ADDR pc)
257 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
260 /* Now come some functions designed to deal with C++ namespace issues.
261 The accessors are safe to use even in the non-C++ case. */
263 /* This returns the namespace that BLOCK is enclosed in, or "" if it
264 isn't enclosed in a namespace at all. This travels the chain of
265 superblocks looking for a scope, if necessary. */
268 block_scope (const struct block *block)
270 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
272 if (BLOCK_NAMESPACE (block) != NULL
273 && BLOCK_NAMESPACE (block)->scope != NULL)
274 return BLOCK_NAMESPACE (block)->scope;
280 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
281 OBSTACK. (It won't make a copy of SCOPE, however, so that already
282 has to be allocated correctly.) */
285 block_set_scope (struct block *block, const char *scope,
286 struct obstack *obstack)
288 block_initialize_namespace (block, obstack);
290 BLOCK_NAMESPACE (block)->scope = scope;
293 /* This returns the using directives list associated with BLOCK, if
296 struct using_direct *
297 block_using (const struct block *block)
299 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
302 return BLOCK_NAMESPACE (block)->using;
305 /* Set BLOCK's using member to USING; if needed, allocate memory via
306 OBSTACK. (It won't make a copy of USING, however, so that already
307 has to be allocated correctly.) */
310 block_set_using (struct block *block,
311 struct using_direct *using,
312 struct obstack *obstack)
314 block_initialize_namespace (block, obstack);
316 BLOCK_NAMESPACE (block)->using = using;
319 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
320 ititialize its members to zero. */
323 block_initialize_namespace (struct block *block, struct obstack *obstack)
325 if (BLOCK_NAMESPACE (block) == NULL)
327 BLOCK_NAMESPACE (block)
328 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
329 BLOCK_NAMESPACE (block)->scope = NULL;
330 BLOCK_NAMESPACE (block)->using = NULL;
334 /* Return the static block associated to BLOCK. Return NULL if block
335 is NULL or if block is a global block. */
338 block_static_block (const struct block *block)
340 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
343 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
344 block = BLOCK_SUPERBLOCK (block);
349 /* Return the static block associated to BLOCK. Return NULL if block
353 block_global_block (const struct block *block)
358 while (BLOCK_SUPERBLOCK (block) != NULL)
359 block = BLOCK_SUPERBLOCK (block);
364 /* Allocate a block on OBSTACK, and initialize its elements to
365 zero/NULL. This is useful for creating "dummy" blocks that don't
366 correspond to actual source files.
368 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
369 valid value. If you really don't want the block to have a
370 dictionary, then you should subsequently set its BLOCK_DICT to
371 dict_create_linear (obstack, NULL). */
374 allocate_block (struct obstack *obstack)
376 struct block *bl = OBSTACK_ZALLOC (obstack, struct block);
381 /* Allocate a global block. */
384 allocate_global_block (struct obstack *obstack)
386 struct global_block *bl = OBSTACK_ZALLOC (obstack, struct global_block);
391 /* Set the symtab of the global block. */
394 set_block_symtab (struct block *block, struct symtab *symtab)
396 struct global_block *gb;
398 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
399 gb = (struct global_block *) block;
400 gdb_assert (gb->symtab == NULL);
404 /* Return the symtab of the global block. */
406 static struct symtab *
407 get_block_symtab (const struct block *block)
409 struct global_block *gb;
411 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
412 gb = (struct global_block *) block;
413 gdb_assert (gb->symtab != NULL);
419 /* Initialize a block iterator, either to iterate over a single block,
420 or, for static and global blocks, all the included symtabs as
424 initialize_block_iterator (const struct block *block,
425 struct block_iterator *iter)
427 enum block_enum which;
428 struct symtab *symtab;
432 if (BLOCK_SUPERBLOCK (block) == NULL)
434 which = GLOBAL_BLOCK;
435 symtab = get_block_symtab (block);
437 else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL)
439 which = STATIC_BLOCK;
440 symtab = get_block_symtab (BLOCK_SUPERBLOCK (block));
444 iter->d.block = block;
445 /* A signal value meaning that we're iterating over a single
447 iter->which = FIRST_LOCAL_BLOCK;
451 /* If this is an included symtab, find the canonical includer and
453 while (symtab->user != NULL)
454 symtab = symtab->user;
456 /* Putting this check here simplifies the logic of the iterator
457 functions. If there are no included symtabs, we only need to
458 search a single block, so we might as well just do that
460 if (symtab->includes == NULL)
462 iter->d.block = block;
463 /* A signal value meaning that we're iterating over a single
465 iter->which = FIRST_LOCAL_BLOCK;
469 iter->d.symtab = symtab;
474 /* A helper function that finds the current symtab over whose static
475 or global block we should iterate. */
477 static struct symtab *
478 find_iterator_symtab (struct block_iterator *iterator)
480 if (iterator->idx == -1)
481 return iterator->d.symtab;
482 return iterator->d.symtab->includes[iterator->idx];
485 /* Perform a single step for a plain block iterator, iterating across
486 symbol tables as needed. Returns the next symbol, or NULL when
487 iteration is complete. */
489 static struct symbol *
490 block_iterator_step (struct block_iterator *iterator, int first)
494 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
500 struct symtab *symtab = find_iterator_symtab (iterator);
501 const struct block *block;
503 /* Iteration is complete. */
507 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
508 sym = dict_iterator_first (BLOCK_DICT (block), &iterator->dict_iter);
511 sym = dict_iterator_next (&iterator->dict_iter);
516 /* We have finished iterating the appropriate block of one
517 symtab. Now advance to the next symtab and begin iteration
527 block_iterator_first (const struct block *block,
528 struct block_iterator *iterator)
530 initialize_block_iterator (block, iterator);
532 if (iterator->which == FIRST_LOCAL_BLOCK)
533 return dict_iterator_first (block->dict, &iterator->dict_iter);
535 return block_iterator_step (iterator, 1);
541 block_iterator_next (struct block_iterator *iterator)
543 if (iterator->which == FIRST_LOCAL_BLOCK)
544 return dict_iterator_next (&iterator->dict_iter);
546 return block_iterator_step (iterator, 0);
549 /* Perform a single step for a "name" block iterator, iterating across
550 symbol tables as needed. Returns the next symbol, or NULL when
551 iteration is complete. */
553 static struct symbol *
554 block_iter_name_step (struct block_iterator *iterator, const char *name,
559 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
565 struct symtab *symtab = find_iterator_symtab (iterator);
566 const struct block *block;
568 /* Iteration is complete. */
572 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
573 sym = dict_iter_name_first (BLOCK_DICT (block), name,
574 &iterator->dict_iter);
577 sym = dict_iter_name_next (name, &iterator->dict_iter);
582 /* We have finished iterating the appropriate block of one
583 symtab. Now advance to the next symtab and begin iteration
593 block_iter_name_first (const struct block *block,
595 struct block_iterator *iterator)
597 initialize_block_iterator (block, iterator);
599 if (iterator->which == FIRST_LOCAL_BLOCK)
600 return dict_iter_name_first (block->dict, name, &iterator->dict_iter);
602 return block_iter_name_step (iterator, name, 1);
608 block_iter_name_next (const char *name, struct block_iterator *iterator)
610 if (iterator->which == FIRST_LOCAL_BLOCK)
611 return dict_iter_name_next (name, &iterator->dict_iter);
613 return block_iter_name_step (iterator, name, 0);
616 /* Perform a single step for a "match" block iterator, iterating
617 across symbol tables as needed. Returns the next symbol, or NULL
618 when iteration is complete. */
620 static struct symbol *
621 block_iter_match_step (struct block_iterator *iterator,
623 symbol_compare_ftype *compare,
628 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
634 struct symtab *symtab = find_iterator_symtab (iterator);
635 const struct block *block;
637 /* Iteration is complete. */
641 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
642 sym = dict_iter_match_first (BLOCK_DICT (block), name,
643 compare, &iterator->dict_iter);
646 sym = dict_iter_match_next (name, compare, &iterator->dict_iter);
651 /* We have finished iterating the appropriate block of one
652 symtab. Now advance to the next symtab and begin iteration
662 block_iter_match_first (const struct block *block,
664 symbol_compare_ftype *compare,
665 struct block_iterator *iterator)
667 initialize_block_iterator (block, iterator);
669 if (iterator->which == FIRST_LOCAL_BLOCK)
670 return dict_iter_match_first (block->dict, name, compare,
671 &iterator->dict_iter);
673 return block_iter_match_step (iterator, name, compare, 1);
679 block_iter_match_next (const char *name,
680 symbol_compare_ftype *compare,
681 struct block_iterator *iterator)
683 if (iterator->which == FIRST_LOCAL_BLOCK)
684 return dict_iter_match_next (name, compare, &iterator->dict_iter);
686 return block_iter_match_step (iterator, name, compare, 0);
691 Note that if NAME is the demangled form of a C++ symbol, we will fail
692 to find a match during the binary search of the non-encoded names, but
693 for now we don't worry about the slight inefficiency of looking for
694 a match we'll never find, since it will go pretty quick. Once the
695 binary search terminates, we drop through and do a straight linear
696 search on the symbols. Each symbol which is marked as being a ObjC/C++
697 symbol (language_cplus or language_objc set) has both the encoded and
698 non-encoded names tested for a match. */
701 block_lookup_symbol (const struct block *block, const char *name,
702 const domain_enum domain)
704 struct block_iterator iter;
707 if (!BLOCK_FUNCTION (block))
709 for (sym = block_iter_name_first (block, name, &iter);
711 sym = block_iter_name_next (name, &iter))
713 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
714 SYMBOL_DOMAIN (sym), domain))
721 /* Note that parameter symbols do not always show up last in the
722 list; this loop makes sure to take anything else other than
723 parameter symbols first; it only uses parameter symbols as a
724 last resort. Note that this only takes up extra computation
727 struct symbol *sym_found = NULL;
729 for (sym = block_iter_name_first (block, name, &iter);
731 sym = block_iter_name_next (name, &iter))
733 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
734 SYMBOL_DOMAIN (sym), domain))
737 if (!SYMBOL_IS_ARGUMENT (sym))
743 return (sym_found); /* Will be NULL if not found. */