1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright (C) 2003, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "gdb_obstack.h"
26 #include "cp-support.h"
29 #include "exceptions.h"
31 /* This is used by struct block to store namespace-related info for
32 C++ files, namely using declarations and the current namespace in
35 struct block_namespace_info
38 struct using_direct *using;
41 static void block_initialize_namespace (struct block *block,
42 struct obstack *obstack);
44 /* Return Nonzero if block a is lexically nested within block b,
45 or if a and b have the same pc range.
46 Return zero otherwise. */
49 contained_in (const struct block *a, const struct block *b)
58 /* If A is a function block, then A cannot be contained in B,
59 except if A was inlined. */
60 if (BLOCK_FUNCTION (a) != NULL && !block_inlined_p (a))
62 a = BLOCK_SUPERBLOCK (a);
70 /* Return the symbol for the function which contains a specified
71 lexical block, described by a struct block BL. The return value
72 will not be an inlined function; the containing function will be
76 block_linkage_function (const struct block *bl)
78 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
79 && BLOCK_SUPERBLOCK (bl) != NULL)
80 bl = BLOCK_SUPERBLOCK (bl);
82 return BLOCK_FUNCTION (bl);
85 /* Return one if BL represents an inlined function. */
88 block_inlined_p (const struct block *bl)
90 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
93 /* Return the blockvector immediately containing the innermost lexical
94 block containing the specified pc value and section, or 0 if there
95 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
96 don't pass this information back to the caller. */
99 blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
100 struct block **pblock, struct symtab *symtab)
104 struct blockvector *bl;
106 if (symtab == 0) /* if no symtab specified by caller */
108 /* First search all symtabs for one whose file contains our pc */
109 symtab = find_pc_sect_symtab (pc, section);
114 bl = BLOCKVECTOR (symtab);
116 /* Then search that symtab for the smallest block that wins. */
118 /* If we have an addrmap mapping code addresses to blocks, then use
120 if (BLOCKVECTOR_MAP (bl))
122 b = addrmap_find (BLOCKVECTOR_MAP (bl), pc);
134 /* Otherwise, use binary search to find the last block that starts
137 top = BLOCKVECTOR_NBLOCKS (bl);
139 while (top - bot > 1)
141 half = (top - bot + 1) >> 1;
142 b = BLOCKVECTOR_BLOCK (bl, bot + half);
143 if (BLOCK_START (b) <= pc)
149 /* Now search backward for a block that ends after PC. */
153 b = BLOCKVECTOR_BLOCK (bl, bot);
154 if (BLOCK_END (b) > pc)
165 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
166 must be the next instruction after call (or after tail call jump). Throw
167 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
170 call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc)
172 struct symtab *symtab;
175 /* -1 as tail call PC can be already after the compilation unit range. */
176 symtab = find_pc_symtab (pc - 1);
178 if (symtab != NULL && symtab->call_site_htab != NULL)
179 slot = htab_find_slot (symtab->call_site_htab, &pc, NO_INSERT);
183 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (pc);
185 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
187 throw_error (NO_ENTRY_VALUE_ERROR,
188 _("DW_OP_GNU_entry_value resolving cannot find "
189 "DW_TAG_GNU_call_site %s in %s"),
190 paddress (gdbarch, pc),
191 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
197 /* Return the blockvector immediately containing the innermost lexical block
198 containing the specified pc value, or 0 if there is none.
199 Backward compatibility, no section. */
202 blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
204 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
208 /* Return the innermost lexical block containing the specified pc value
209 in the specified section, or 0 if there is none. */
212 block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
214 struct blockvector *bl;
217 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
223 /* Return the innermost lexical block containing the specified pc value,
224 or 0 if there is none. Backward compatibility, no section. */
227 block_for_pc (CORE_ADDR pc)
229 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
232 /* Now come some functions designed to deal with C++ namespace issues.
233 The accessors are safe to use even in the non-C++ case. */
235 /* This returns the namespace that BLOCK is enclosed in, or "" if it
236 isn't enclosed in a namespace at all. This travels the chain of
237 superblocks looking for a scope, if necessary. */
240 block_scope (const struct block *block)
242 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
244 if (BLOCK_NAMESPACE (block) != NULL
245 && BLOCK_NAMESPACE (block)->scope != NULL)
246 return BLOCK_NAMESPACE (block)->scope;
252 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
253 OBSTACK. (It won't make a copy of SCOPE, however, so that already
254 has to be allocated correctly.) */
257 block_set_scope (struct block *block, const char *scope,
258 struct obstack *obstack)
260 block_initialize_namespace (block, obstack);
262 BLOCK_NAMESPACE (block)->scope = scope;
265 /* This returns the using directives list associated with BLOCK, if
268 struct using_direct *
269 block_using (const struct block *block)
271 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
274 return BLOCK_NAMESPACE (block)->using;
277 /* Set BLOCK's using member to USING; if needed, allocate memory via
278 OBSTACK. (It won't make a copy of USING, however, so that already
279 has to be allocated correctly.) */
282 block_set_using (struct block *block,
283 struct using_direct *using,
284 struct obstack *obstack)
286 block_initialize_namespace (block, obstack);
288 BLOCK_NAMESPACE (block)->using = using;
291 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
292 ititialize its members to zero. */
295 block_initialize_namespace (struct block *block, struct obstack *obstack)
297 if (BLOCK_NAMESPACE (block) == NULL)
299 BLOCK_NAMESPACE (block)
300 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
301 BLOCK_NAMESPACE (block)->scope = NULL;
302 BLOCK_NAMESPACE (block)->using = NULL;
306 /* Return the static block associated to BLOCK. Return NULL if block
307 is NULL or if block is a global block. */
310 block_static_block (const struct block *block)
312 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
315 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
316 block = BLOCK_SUPERBLOCK (block);
321 /* Return the static block associated to BLOCK. Return NULL if block
325 block_global_block (const struct block *block)
330 while (BLOCK_SUPERBLOCK (block) != NULL)
331 block = BLOCK_SUPERBLOCK (block);
336 /* Allocate a block on OBSTACK, and initialize its elements to
337 zero/NULL. This is useful for creating "dummy" blocks that don't
338 correspond to actual source files.
340 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
341 valid value. If you really don't want the block to have a
342 dictionary, then you should subsequently set its BLOCK_DICT to
343 dict_create_linear (obstack, NULL). */
346 allocate_block (struct obstack *obstack)
348 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
350 BLOCK_START (bl) = 0;
352 BLOCK_FUNCTION (bl) = NULL;
353 BLOCK_SUPERBLOCK (bl) = NULL;
354 BLOCK_DICT (bl) = NULL;
355 BLOCK_NAMESPACE (bl) = NULL;