1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright (C) 2003, 2007-2012 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"
28 #include "exceptions.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
37 struct using_direct *using;
40 static void block_initialize_namespace (struct block *block,
41 struct obstack *obstack);
43 /* Return Nonzero if block a is lexically nested within block b,
44 or if a and b have the same pc range.
45 Return zero otherwise. */
48 contained_in (const struct block *a, const struct block *b)
57 /* If A is a function block, then A cannot be contained in B,
58 except if A was inlined. */
59 if (BLOCK_FUNCTION (a) != NULL && !block_inlined_p (a))
61 a = BLOCK_SUPERBLOCK (a);
69 /* Return the symbol for the function which contains a specified
70 lexical block, described by a struct block BL. The return value
71 will not be an inlined function; the containing function will be
75 block_linkage_function (const struct block *bl)
77 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
78 && BLOCK_SUPERBLOCK (bl) != NULL)
79 bl = BLOCK_SUPERBLOCK (bl);
81 return BLOCK_FUNCTION (bl);
84 /* Return the symbol for the function which contains a specified
85 block, described by a struct block BL. The return value will be
86 the closest enclosing function, which might be an inline
90 block_containing_function (const struct block *bl)
92 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
93 bl = BLOCK_SUPERBLOCK (bl);
95 return BLOCK_FUNCTION (bl);
98 /* Return one if BL represents an inlined function. */
101 block_inlined_p (const struct block *bl)
103 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
106 /* Return the blockvector immediately containing the innermost lexical
107 block containing the specified pc value and section, or 0 if there
108 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
109 don't pass this information back to the caller. */
112 blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
113 struct block **pblock, struct symtab *symtab)
117 struct blockvector *bl;
119 if (symtab == 0) /* if no symtab specified by caller */
121 /* First search all symtabs for one whose file contains our pc */
122 symtab = find_pc_sect_symtab (pc, section);
127 bl = BLOCKVECTOR (symtab);
129 /* Then search that symtab for the smallest block that wins. */
131 /* If we have an addrmap mapping code addresses to blocks, then use
133 if (BLOCKVECTOR_MAP (bl))
135 b = addrmap_find (BLOCKVECTOR_MAP (bl), pc);
147 /* Otherwise, use binary search to find the last block that starts
150 top = BLOCKVECTOR_NBLOCKS (bl);
152 while (top - bot > 1)
154 half = (top - bot + 1) >> 1;
155 b = BLOCKVECTOR_BLOCK (bl, bot + half);
156 if (BLOCK_START (b) <= pc)
162 /* Now search backward for a block that ends after PC. */
166 b = BLOCKVECTOR_BLOCK (bl, bot);
167 if (BLOCK_END (b) > pc)
178 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
179 must be the next instruction after call (or after tail call jump). Throw
180 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
183 call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc)
185 struct symtab *symtab;
188 /* -1 as tail call PC can be already after the compilation unit range. */
189 symtab = find_pc_symtab (pc - 1);
191 if (symtab != NULL && symtab->call_site_htab != NULL)
192 slot = htab_find_slot (symtab->call_site_htab, &pc, NO_INSERT);
196 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (pc);
198 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
200 throw_error (NO_ENTRY_VALUE_ERROR,
201 _("DW_OP_GNU_entry_value resolving cannot find "
202 "DW_TAG_GNU_call_site %s in %s"),
203 paddress (gdbarch, pc),
204 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
210 /* Return the blockvector immediately containing the innermost lexical block
211 containing the specified pc value, or 0 if there is none.
212 Backward compatibility, no section. */
215 blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
217 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
221 /* Return the innermost lexical block containing the specified pc value
222 in the specified section, or 0 if there is none. */
225 block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
227 struct blockvector *bl;
230 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
236 /* Return the innermost lexical block containing the specified pc value,
237 or 0 if there is none. Backward compatibility, no section. */
240 block_for_pc (CORE_ADDR pc)
242 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
245 /* Now come some functions designed to deal with C++ namespace issues.
246 The accessors are safe to use even in the non-C++ case. */
248 /* This returns the namespace that BLOCK is enclosed in, or "" if it
249 isn't enclosed in a namespace at all. This travels the chain of
250 superblocks looking for a scope, if necessary. */
253 block_scope (const struct block *block)
255 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
257 if (BLOCK_NAMESPACE (block) != NULL
258 && BLOCK_NAMESPACE (block)->scope != NULL)
259 return BLOCK_NAMESPACE (block)->scope;
265 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
266 OBSTACK. (It won't make a copy of SCOPE, however, so that already
267 has to be allocated correctly.) */
270 block_set_scope (struct block *block, const char *scope,
271 struct obstack *obstack)
273 block_initialize_namespace (block, obstack);
275 BLOCK_NAMESPACE (block)->scope = scope;
278 /* This returns the using directives list associated with BLOCK, if
281 struct using_direct *
282 block_using (const struct block *block)
284 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
287 return BLOCK_NAMESPACE (block)->using;
290 /* Set BLOCK's using member to USING; if needed, allocate memory via
291 OBSTACK. (It won't make a copy of USING, however, so that already
292 has to be allocated correctly.) */
295 block_set_using (struct block *block,
296 struct using_direct *using,
297 struct obstack *obstack)
299 block_initialize_namespace (block, obstack);
301 BLOCK_NAMESPACE (block)->using = using;
304 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
305 ititialize its members to zero. */
308 block_initialize_namespace (struct block *block, struct obstack *obstack)
310 if (BLOCK_NAMESPACE (block) == NULL)
312 BLOCK_NAMESPACE (block)
313 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
314 BLOCK_NAMESPACE (block)->scope = NULL;
315 BLOCK_NAMESPACE (block)->using = NULL;
319 /* Return the static block associated to BLOCK. Return NULL if block
320 is NULL or if block is a global block. */
323 block_static_block (const struct block *block)
325 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
328 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
329 block = BLOCK_SUPERBLOCK (block);
334 /* Return the static block associated to BLOCK. Return NULL if block
338 block_global_block (const struct block *block)
343 while (BLOCK_SUPERBLOCK (block) != NULL)
344 block = BLOCK_SUPERBLOCK (block);
349 /* Allocate a block on OBSTACK, and initialize its elements to
350 zero/NULL. This is useful for creating "dummy" blocks that don't
351 correspond to actual source files.
353 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
354 valid value. If you really don't want the block to have a
355 dictionary, then you should subsequently set its BLOCK_DICT to
356 dict_create_linear (obstack, NULL). */
359 allocate_block (struct obstack *obstack)
361 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
363 BLOCK_START (bl) = 0;
365 BLOCK_FUNCTION (bl) = NULL;
366 BLOCK_SUPERBLOCK (bl) = NULL;
367 BLOCK_DICT (bl) = NULL;
368 BLOCK_NAMESPACE (bl) = NULL;
378 block_iterator_first (const struct block *block,
379 struct block_iterator *iterator)
381 return dict_iterator_first (block->dict, &iterator->dict_iter);
387 block_iterator_next (struct block_iterator *iterator)
389 return dict_iterator_next (&iterator->dict_iter);
395 block_iter_name_first (const struct block *block,
397 struct block_iterator *iterator)
399 return dict_iter_name_first (block->dict, name, &iterator->dict_iter);
405 block_iter_name_next (const char *name, struct block_iterator *iterator)
407 return dict_iter_name_next (name, &iterator->dict_iter);
413 block_iter_match_first (const struct block *block,
415 symbol_compare_ftype *compare,
416 struct block_iterator *iterator)
418 return dict_iter_match_first (block->dict, name, compare,
419 &iterator->dict_iter);
425 block_iter_match_next (const char *name,
426 symbol_compare_ftype *compare,
427 struct block_iterator *iterator)
429 return dict_iter_match_next (name, compare, &iterator->dict_iter);