*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / block.c
CommitLineData
fe898f56
DC
1/* Block-related functions for the GNU debugger, GDB.
2
7b6bb8da
JB
3 Copyright (C) 2003, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
fe898f56
DC
5
6 This file is part of GDB.
7
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
fe898f56
DC
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fe898f56
DC
20
21#include "defs.h"
22#include "block.h"
23#include "symtab.h"
24#include "symfile.h"
9219021c
DC
25#include "gdb_obstack.h"
26#include "cp-support.h"
801e3a5b 27#include "addrmap.h"
8e3b41a9
JK
28#include "gdbtypes.h"
29#include "exceptions.h"
9219021c
DC
30
31/* This is used by struct block to store namespace-related info for
32 C++ files, namely using declarations and the current namespace in
33 scope. */
34
35struct block_namespace_info
36{
37 const char *scope;
38 struct using_direct *using;
39};
40
41static void block_initialize_namespace (struct block *block,
42 struct obstack *obstack);
fe898f56
DC
43
44/* Return Nonzero if block a is lexically nested within block b,
45 or if a and b have the same pc range.
4a64f543 46 Return zero otherwise. */
fe898f56
DC
47
48int
0cf566ec 49contained_in (const struct block *a, const struct block *b)
fe898f56
DC
50{
51 if (!a || !b)
52 return 0;
edb3359d
DJ
53
54 do
55 {
56 if (a == b)
57 return 1;
49e794ac
JB
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))
61 return 0;
edb3359d
DJ
62 a = BLOCK_SUPERBLOCK (a);
63 }
64 while (a != NULL);
65
66 return 0;
fe898f56
DC
67}
68
69
70/* Return the symbol for the function which contains a specified
7f0df278
DJ
71 lexical block, described by a struct block BL. The return value
72 will not be an inlined function; the containing function will be
73 returned instead. */
fe898f56
DC
74
75struct symbol *
7f0df278 76block_linkage_function (const struct block *bl)
fe898f56 77{
edb3359d
DJ
78 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
79 && BLOCK_SUPERBLOCK (bl) != NULL)
fe898f56
DC
80 bl = BLOCK_SUPERBLOCK (bl);
81
82 return BLOCK_FUNCTION (bl);
83}
84
f8eba3c6
TT
85/* Return the symbol for the function which contains a specified
86 block, described by a struct block BL. The return value will be
87 the closest enclosing function, which might be an inline
88 function. */
89
90struct symbol *
91block_containing_function (const struct block *bl)
92{
93 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
94 bl = BLOCK_SUPERBLOCK (bl);
95
96 return BLOCK_FUNCTION (bl);
97}
98
edb3359d
DJ
99/* Return one if BL represents an inlined function. */
100
101int
102block_inlined_p (const struct block *bl)
103{
104 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
105}
106
801e3a5b
JB
107/* Return the blockvector immediately containing the innermost lexical
108 block containing the specified pc value and section, or 0 if there
109 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
110 don't pass this information back to the caller. */
fe898f56
DC
111
112struct blockvector *
714835d5 113blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
801e3a5b 114 struct block **pblock, struct symtab *symtab)
fe898f56 115{
b59661bd
AC
116 struct block *b;
117 int bot, top, half;
fe898f56
DC
118 struct blockvector *bl;
119
120 if (symtab == 0) /* if no symtab specified by caller */
121 {
122 /* First search all symtabs for one whose file contains our pc */
b59661bd
AC
123 symtab = find_pc_sect_symtab (pc, section);
124 if (symtab == 0)
fe898f56
DC
125 return 0;
126 }
127
128 bl = BLOCKVECTOR (symtab);
fe898f56
DC
129
130 /* Then search that symtab for the smallest block that wins. */
fe898f56 131
801e3a5b
JB
132 /* If we have an addrmap mapping code addresses to blocks, then use
133 that. */
134 if (BLOCKVECTOR_MAP (bl))
135 {
136 b = addrmap_find (BLOCKVECTOR_MAP (bl), pc);
137 if (b)
138 {
139 if (pblock)
140 *pblock = b;
141 return bl;
142 }
143 else
144 return 0;
145 }
146
147
148 /* Otherwise, use binary search to find the last block that starts
149 before PC. */
fe898f56
DC
150 bot = 0;
151 top = BLOCKVECTOR_NBLOCKS (bl);
152
153 while (top - bot > 1)
154 {
155 half = (top - bot + 1) >> 1;
156 b = BLOCKVECTOR_BLOCK (bl, bot + half);
157 if (BLOCK_START (b) <= pc)
158 bot += half;
159 else
160 top = bot + half;
161 }
162
163 /* Now search backward for a block that ends after PC. */
164
165 while (bot >= 0)
166 {
167 b = BLOCKVECTOR_BLOCK (bl, bot);
168 if (BLOCK_END (b) > pc)
169 {
801e3a5b
JB
170 if (pblock)
171 *pblock = b;
fe898f56
DC
172 return bl;
173 }
174 bot--;
175 }
176 return 0;
177}
178
8e3b41a9
JK
179/* Return call_site for specified PC in GDBARCH. PC must match exactly, it
180 must be the next instruction after call (or after tail call jump). Throw
181 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
182
183struct call_site *
184call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc)
185{
186 struct symtab *symtab;
187 void **slot = NULL;
188
189 /* -1 as tail call PC can be already after the compilation unit range. */
190 symtab = find_pc_symtab (pc - 1);
191
192 if (symtab != NULL && symtab->call_site_htab != NULL)
193 slot = htab_find_slot (symtab->call_site_htab, &pc, NO_INSERT);
194
195 if (slot == NULL)
196 {
197 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (pc);
198
199 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
200 the call target. */
201 throw_error (NO_ENTRY_VALUE_ERROR,
202 _("DW_OP_GNU_entry_value resolving cannot find "
203 "DW_TAG_GNU_call_site %s in %s"),
204 paddress (gdbarch, pc),
205 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
206 }
207
208 return *slot;
209}
210
fe898f56
DC
211/* Return the blockvector immediately containing the innermost lexical block
212 containing the specified pc value, or 0 if there is none.
213 Backward compatibility, no section. */
214
215struct blockvector *
801e3a5b 216blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
fe898f56
DC
217{
218 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
801e3a5b 219 pblock, NULL);
fe898f56
DC
220}
221
222/* Return the innermost lexical block containing the specified pc value
223 in the specified section, or 0 if there is none. */
224
225struct block *
714835d5 226block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
fe898f56 227{
b59661bd 228 struct blockvector *bl;
801e3a5b 229 struct block *b;
fe898f56 230
801e3a5b 231 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
fe898f56 232 if (bl)
801e3a5b 233 return b;
fe898f56
DC
234 return 0;
235}
236
237/* Return the innermost lexical block containing the specified pc value,
238 or 0 if there is none. Backward compatibility, no section. */
239
240struct block *
b59661bd 241block_for_pc (CORE_ADDR pc)
fe898f56
DC
242{
243 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
244}
9219021c 245
1fcb5155
DC
246/* Now come some functions designed to deal with C++ namespace issues.
247 The accessors are safe to use even in the non-C++ case. */
248
249/* This returns the namespace that BLOCK is enclosed in, or "" if it
250 isn't enclosed in a namespace at all. This travels the chain of
251 superblocks looking for a scope, if necessary. */
252
253const char *
254block_scope (const struct block *block)
255{
256 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
257 {
258 if (BLOCK_NAMESPACE (block) != NULL
259 && BLOCK_NAMESPACE (block)->scope != NULL)
260 return BLOCK_NAMESPACE (block)->scope;
261 }
262
263 return "";
264}
9219021c
DC
265
266/* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
267 OBSTACK. (It won't make a copy of SCOPE, however, so that already
268 has to be allocated correctly.) */
269
270void
271block_set_scope (struct block *block, const char *scope,
272 struct obstack *obstack)
273{
274 block_initialize_namespace (block, obstack);
275
276 BLOCK_NAMESPACE (block)->scope = scope;
277}
278
27aa8d6a 279/* This returns the using directives list associated with BLOCK, if
1fcb5155
DC
280 any. */
281
1fcb5155
DC
282struct using_direct *
283block_using (const struct block *block)
284{
27aa8d6a 285 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
1fcb5155
DC
286 return NULL;
287 else
27aa8d6a 288 return BLOCK_NAMESPACE (block)->using;
1fcb5155
DC
289}
290
9219021c
DC
291/* Set BLOCK's using member to USING; if needed, allocate memory via
292 OBSTACK. (It won't make a copy of USING, however, so that already
293 has to be allocated correctly.) */
294
295void
296block_set_using (struct block *block,
297 struct using_direct *using,
298 struct obstack *obstack)
299{
300 block_initialize_namespace (block, obstack);
301
302 BLOCK_NAMESPACE (block)->using = using;
303}
304
305/* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
306 ititialize its members to zero. */
307
308static void
309block_initialize_namespace (struct block *block, struct obstack *obstack)
310{
311 if (BLOCK_NAMESPACE (block) == NULL)
312 {
313 BLOCK_NAMESPACE (block)
314 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
315 BLOCK_NAMESPACE (block)->scope = NULL;
316 BLOCK_NAMESPACE (block)->using = NULL;
317 }
318}
89a9d1b1
DC
319
320/* Return the static block associated to BLOCK. Return NULL if block
321 is NULL or if block is a global block. */
322
323const struct block *
324block_static_block (const struct block *block)
325{
326 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
327 return NULL;
328
329 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
330 block = BLOCK_SUPERBLOCK (block);
331
332 return block;
333}
1fcb5155
DC
334
335/* Return the static block associated to BLOCK. Return NULL if block
336 is NULL. */
337
338const struct block *
339block_global_block (const struct block *block)
340{
341 if (block == NULL)
342 return NULL;
343
344 while (BLOCK_SUPERBLOCK (block) != NULL)
345 block = BLOCK_SUPERBLOCK (block);
346
347 return block;
348}
5c4e30ca
DC
349
350/* Allocate a block on OBSTACK, and initialize its elements to
351 zero/NULL. This is useful for creating "dummy" blocks that don't
352 correspond to actual source files.
353
354 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
355 valid value. If you really don't want the block to have a
356 dictionary, then you should subsequently set its BLOCK_DICT to
357 dict_create_linear (obstack, NULL). */
358
359struct block *
360allocate_block (struct obstack *obstack)
361{
362 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
363
364 BLOCK_START (bl) = 0;
365 BLOCK_END (bl) = 0;
366 BLOCK_FUNCTION (bl) = NULL;
367 BLOCK_SUPERBLOCK (bl) = NULL;
368 BLOCK_DICT (bl) = NULL;
369 BLOCK_NAMESPACE (bl) = NULL;
5c4e30ca
DC
370
371 return bl;
372}
This page took 0.764727 seconds and 4 git commands to generate.