2009-06-27 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / block.c
CommitLineData
fe898f56
DC
1/* Block-related functions for the GNU debugger, GDB.
2
0fb0cc75 3 Copyright (C) 2003, 2007, 2008, 2009 Free Software Foundation, Inc.
fe898f56
DC
4
5 This file is part of GDB.
6
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
fe898f56
DC
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fe898f56
DC
19
20#include "defs.h"
21#include "block.h"
22#include "symtab.h"
23#include "symfile.h"
9219021c
DC
24#include "gdb_obstack.h"
25#include "cp-support.h"
801e3a5b 26#include "addrmap.h"
9219021c
DC
27
28/* This is used by struct block to store namespace-related info for
29 C++ files, namely using declarations and the current namespace in
30 scope. */
31
32struct block_namespace_info
33{
34 const char *scope;
35 struct using_direct *using;
36};
37
38static void block_initialize_namespace (struct block *block,
39 struct obstack *obstack);
fe898f56
DC
40
41/* Return Nonzero if block a is lexically nested within block b,
42 or if a and b have the same pc range.
43 Return zero otherwise. */
44
45int
0cf566ec 46contained_in (const struct block *a, const struct block *b)
fe898f56
DC
47{
48 if (!a || !b)
49 return 0;
50 return BLOCK_START (a) >= BLOCK_START (b)
51 && BLOCK_END (a) <= BLOCK_END (b);
52}
53
54
55/* Return the symbol for the function which contains a specified
7f0df278
DJ
56 lexical block, described by a struct block BL. The return value
57 will not be an inlined function; the containing function will be
58 returned instead. */
fe898f56
DC
59
60struct symbol *
7f0df278 61block_linkage_function (const struct block *bl)
fe898f56
DC
62{
63 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
64 bl = BLOCK_SUPERBLOCK (bl);
65
66 return BLOCK_FUNCTION (bl);
67}
68
801e3a5b
JB
69/* Return the blockvector immediately containing the innermost lexical
70 block containing the specified pc value and section, or 0 if there
71 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
72 don't pass this information back to the caller. */
fe898f56
DC
73
74struct blockvector *
714835d5 75blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
801e3a5b 76 struct block **pblock, struct symtab *symtab)
fe898f56 77{
b59661bd
AC
78 struct block *b;
79 int bot, top, half;
fe898f56
DC
80 struct blockvector *bl;
81
82 if (symtab == 0) /* if no symtab specified by caller */
83 {
84 /* First search all symtabs for one whose file contains our pc */
b59661bd
AC
85 symtab = find_pc_sect_symtab (pc, section);
86 if (symtab == 0)
fe898f56
DC
87 return 0;
88 }
89
90 bl = BLOCKVECTOR (symtab);
fe898f56
DC
91
92 /* Then search that symtab for the smallest block that wins. */
fe898f56 93
801e3a5b
JB
94 /* If we have an addrmap mapping code addresses to blocks, then use
95 that. */
96 if (BLOCKVECTOR_MAP (bl))
97 {
98 b = addrmap_find (BLOCKVECTOR_MAP (bl), pc);
99 if (b)
100 {
101 if (pblock)
102 *pblock = b;
103 return bl;
104 }
105 else
106 return 0;
107 }
108
109
110 /* Otherwise, use binary search to find the last block that starts
111 before PC. */
fe898f56
DC
112 bot = 0;
113 top = BLOCKVECTOR_NBLOCKS (bl);
114
115 while (top - bot > 1)
116 {
117 half = (top - bot + 1) >> 1;
118 b = BLOCKVECTOR_BLOCK (bl, bot + half);
119 if (BLOCK_START (b) <= pc)
120 bot += half;
121 else
122 top = bot + half;
123 }
124
125 /* Now search backward for a block that ends after PC. */
126
127 while (bot >= 0)
128 {
129 b = BLOCKVECTOR_BLOCK (bl, bot);
130 if (BLOCK_END (b) > pc)
131 {
801e3a5b
JB
132 if (pblock)
133 *pblock = b;
fe898f56
DC
134 return bl;
135 }
136 bot--;
137 }
138 return 0;
139}
140
141/* Return the blockvector immediately containing the innermost lexical block
142 containing the specified pc value, or 0 if there is none.
143 Backward compatibility, no section. */
144
145struct blockvector *
801e3a5b 146blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
fe898f56
DC
147{
148 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
801e3a5b 149 pblock, NULL);
fe898f56
DC
150}
151
152/* Return the innermost lexical block containing the specified pc value
153 in the specified section, or 0 if there is none. */
154
155struct block *
714835d5 156block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
fe898f56 157{
b59661bd 158 struct blockvector *bl;
801e3a5b 159 struct block *b;
fe898f56 160
801e3a5b 161 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
fe898f56 162 if (bl)
801e3a5b 163 return b;
fe898f56
DC
164 return 0;
165}
166
167/* Return the innermost lexical block containing the specified pc value,
168 or 0 if there is none. Backward compatibility, no section. */
169
170struct block *
b59661bd 171block_for_pc (CORE_ADDR pc)
fe898f56
DC
172{
173 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
174}
9219021c 175
1fcb5155
DC
176/* Now come some functions designed to deal with C++ namespace issues.
177 The accessors are safe to use even in the non-C++ case. */
178
179/* This returns the namespace that BLOCK is enclosed in, or "" if it
180 isn't enclosed in a namespace at all. This travels the chain of
181 superblocks looking for a scope, if necessary. */
182
183const char *
184block_scope (const struct block *block)
185{
186 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
187 {
188 if (BLOCK_NAMESPACE (block) != NULL
189 && BLOCK_NAMESPACE (block)->scope != NULL)
190 return BLOCK_NAMESPACE (block)->scope;
191 }
192
193 return "";
194}
9219021c
DC
195
196/* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
197 OBSTACK. (It won't make a copy of SCOPE, however, so that already
198 has to be allocated correctly.) */
199
200void
201block_set_scope (struct block *block, const char *scope,
202 struct obstack *obstack)
203{
204 block_initialize_namespace (block, obstack);
205
206 BLOCK_NAMESPACE (block)->scope = scope;
207}
208
27aa8d6a 209/* This returns the using directives list associated with BLOCK, if
1fcb5155
DC
210 any. */
211
1fcb5155
DC
212struct using_direct *
213block_using (const struct block *block)
214{
27aa8d6a 215 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
1fcb5155
DC
216 return NULL;
217 else
27aa8d6a 218 return BLOCK_NAMESPACE (block)->using;
1fcb5155
DC
219}
220
9219021c
DC
221/* Set BLOCK's using member to USING; if needed, allocate memory via
222 OBSTACK. (It won't make a copy of USING, however, so that already
223 has to be allocated correctly.) */
224
225void
226block_set_using (struct block *block,
227 struct using_direct *using,
228 struct obstack *obstack)
229{
230 block_initialize_namespace (block, obstack);
231
232 BLOCK_NAMESPACE (block)->using = using;
233}
234
235/* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
236 ititialize its members to zero. */
237
238static void
239block_initialize_namespace (struct block *block, struct obstack *obstack)
240{
241 if (BLOCK_NAMESPACE (block) == NULL)
242 {
243 BLOCK_NAMESPACE (block)
244 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
245 BLOCK_NAMESPACE (block)->scope = NULL;
246 BLOCK_NAMESPACE (block)->using = NULL;
247 }
248}
89a9d1b1
DC
249
250/* Return the static block associated to BLOCK. Return NULL if block
251 is NULL or if block is a global block. */
252
253const struct block *
254block_static_block (const struct block *block)
255{
256 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
257 return NULL;
258
259 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
260 block = BLOCK_SUPERBLOCK (block);
261
262 return block;
263}
1fcb5155
DC
264
265/* Return the static block associated to BLOCK. Return NULL if block
266 is NULL. */
267
268const struct block *
269block_global_block (const struct block *block)
270{
271 if (block == NULL)
272 return NULL;
273
274 while (BLOCK_SUPERBLOCK (block) != NULL)
275 block = BLOCK_SUPERBLOCK (block);
276
277 return block;
278}
5c4e30ca
DC
279
280/* Allocate a block on OBSTACK, and initialize its elements to
281 zero/NULL. This is useful for creating "dummy" blocks that don't
282 correspond to actual source files.
283
284 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
285 valid value. If you really don't want the block to have a
286 dictionary, then you should subsequently set its BLOCK_DICT to
287 dict_create_linear (obstack, NULL). */
288
289struct block *
290allocate_block (struct obstack *obstack)
291{
292 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
293
294 BLOCK_START (bl) = 0;
295 BLOCK_END (bl) = 0;
296 BLOCK_FUNCTION (bl) = NULL;
297 BLOCK_SUPERBLOCK (bl) = NULL;
298 BLOCK_DICT (bl) = NULL;
299 BLOCK_NAMESPACE (bl) = NULL;
5c4e30ca
DC
300
301 return bl;
302}
This page took 0.380952 seconds and 4 git commands to generate.