gdb/
[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;
edb3359d
DJ
50
51 do
52 {
53 if (a == b)
54 return 1;
55 a = BLOCK_SUPERBLOCK (a);
56 }
57 while (a != NULL);
58
59 return 0;
fe898f56
DC
60}
61
62
63/* Return the symbol for the function which contains a specified
7f0df278
DJ
64 lexical block, described by a struct block BL. The return value
65 will not be an inlined function; the containing function will be
66 returned instead. */
fe898f56
DC
67
68struct symbol *
7f0df278 69block_linkage_function (const struct block *bl)
fe898f56 70{
edb3359d
DJ
71 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
72 && BLOCK_SUPERBLOCK (bl) != NULL)
fe898f56
DC
73 bl = BLOCK_SUPERBLOCK (bl);
74
75 return BLOCK_FUNCTION (bl);
76}
77
edb3359d
DJ
78/* Return one if BL represents an inlined function. */
79
80int
81block_inlined_p (const struct block *bl)
82{
83 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
84}
85
801e3a5b
JB
86/* Return the blockvector immediately containing the innermost lexical
87 block containing the specified pc value and section, or 0 if there
88 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
89 don't pass this information back to the caller. */
fe898f56
DC
90
91struct blockvector *
714835d5 92blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
801e3a5b 93 struct block **pblock, struct symtab *symtab)
fe898f56 94{
b59661bd
AC
95 struct block *b;
96 int bot, top, half;
fe898f56
DC
97 struct blockvector *bl;
98
99 if (symtab == 0) /* if no symtab specified by caller */
100 {
101 /* First search all symtabs for one whose file contains our pc */
b59661bd
AC
102 symtab = find_pc_sect_symtab (pc, section);
103 if (symtab == 0)
fe898f56
DC
104 return 0;
105 }
106
107 bl = BLOCKVECTOR (symtab);
fe898f56
DC
108
109 /* Then search that symtab for the smallest block that wins. */
fe898f56 110
801e3a5b
JB
111 /* If we have an addrmap mapping code addresses to blocks, then use
112 that. */
113 if (BLOCKVECTOR_MAP (bl))
114 {
115 b = addrmap_find (BLOCKVECTOR_MAP (bl), pc);
116 if (b)
117 {
118 if (pblock)
119 *pblock = b;
120 return bl;
121 }
122 else
123 return 0;
124 }
125
126
127 /* Otherwise, use binary search to find the last block that starts
128 before PC. */
fe898f56
DC
129 bot = 0;
130 top = BLOCKVECTOR_NBLOCKS (bl);
131
132 while (top - bot > 1)
133 {
134 half = (top - bot + 1) >> 1;
135 b = BLOCKVECTOR_BLOCK (bl, bot + half);
136 if (BLOCK_START (b) <= pc)
137 bot += half;
138 else
139 top = bot + half;
140 }
141
142 /* Now search backward for a block that ends after PC. */
143
144 while (bot >= 0)
145 {
146 b = BLOCKVECTOR_BLOCK (bl, bot);
147 if (BLOCK_END (b) > pc)
148 {
801e3a5b
JB
149 if (pblock)
150 *pblock = b;
fe898f56
DC
151 return bl;
152 }
153 bot--;
154 }
155 return 0;
156}
157
158/* Return the blockvector immediately containing the innermost lexical block
159 containing the specified pc value, or 0 if there is none.
160 Backward compatibility, no section. */
161
162struct blockvector *
801e3a5b 163blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
fe898f56
DC
164{
165 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
801e3a5b 166 pblock, NULL);
fe898f56
DC
167}
168
169/* Return the innermost lexical block containing the specified pc value
170 in the specified section, or 0 if there is none. */
171
172struct block *
714835d5 173block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
fe898f56 174{
b59661bd 175 struct blockvector *bl;
801e3a5b 176 struct block *b;
fe898f56 177
801e3a5b 178 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
fe898f56 179 if (bl)
801e3a5b 180 return b;
fe898f56
DC
181 return 0;
182}
183
184/* Return the innermost lexical block containing the specified pc value,
185 or 0 if there is none. Backward compatibility, no section. */
186
187struct block *
b59661bd 188block_for_pc (CORE_ADDR pc)
fe898f56
DC
189{
190 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
191}
9219021c 192
1fcb5155
DC
193/* Now come some functions designed to deal with C++ namespace issues.
194 The accessors are safe to use even in the non-C++ case. */
195
196/* This returns the namespace that BLOCK is enclosed in, or "" if it
197 isn't enclosed in a namespace at all. This travels the chain of
198 superblocks looking for a scope, if necessary. */
199
200const char *
201block_scope (const struct block *block)
202{
203 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
204 {
205 if (BLOCK_NAMESPACE (block) != NULL
206 && BLOCK_NAMESPACE (block)->scope != NULL)
207 return BLOCK_NAMESPACE (block)->scope;
208 }
209
210 return "";
211}
9219021c
DC
212
213/* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
214 OBSTACK. (It won't make a copy of SCOPE, however, so that already
215 has to be allocated correctly.) */
216
217void
218block_set_scope (struct block *block, const char *scope,
219 struct obstack *obstack)
220{
221 block_initialize_namespace (block, obstack);
222
223 BLOCK_NAMESPACE (block)->scope = scope;
224}
225
27aa8d6a 226/* This returns the using directives list associated with BLOCK, if
1fcb5155
DC
227 any. */
228
1fcb5155
DC
229struct using_direct *
230block_using (const struct block *block)
231{
27aa8d6a 232 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
1fcb5155
DC
233 return NULL;
234 else
27aa8d6a 235 return BLOCK_NAMESPACE (block)->using;
1fcb5155
DC
236}
237
9219021c
DC
238/* Set BLOCK's using member to USING; if needed, allocate memory via
239 OBSTACK. (It won't make a copy of USING, however, so that already
240 has to be allocated correctly.) */
241
242void
243block_set_using (struct block *block,
244 struct using_direct *using,
245 struct obstack *obstack)
246{
247 block_initialize_namespace (block, obstack);
248
249 BLOCK_NAMESPACE (block)->using = using;
250}
251
252/* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
253 ititialize its members to zero. */
254
255static void
256block_initialize_namespace (struct block *block, struct obstack *obstack)
257{
258 if (BLOCK_NAMESPACE (block) == NULL)
259 {
260 BLOCK_NAMESPACE (block)
261 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
262 BLOCK_NAMESPACE (block)->scope = NULL;
263 BLOCK_NAMESPACE (block)->using = NULL;
264 }
265}
89a9d1b1
DC
266
267/* Return the static block associated to BLOCK. Return NULL if block
268 is NULL or if block is a global block. */
269
270const struct block *
271block_static_block (const struct block *block)
272{
273 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
274 return NULL;
275
276 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
277 block = BLOCK_SUPERBLOCK (block);
278
279 return block;
280}
1fcb5155
DC
281
282/* Return the static block associated to BLOCK. Return NULL if block
283 is NULL. */
284
285const struct block *
286block_global_block (const struct block *block)
287{
288 if (block == NULL)
289 return NULL;
290
291 while (BLOCK_SUPERBLOCK (block) != NULL)
292 block = BLOCK_SUPERBLOCK (block);
293
294 return block;
295}
5c4e30ca
DC
296
297/* Allocate a block on OBSTACK, and initialize its elements to
298 zero/NULL. This is useful for creating "dummy" blocks that don't
299 correspond to actual source files.
300
301 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
302 valid value. If you really don't want the block to have a
303 dictionary, then you should subsequently set its BLOCK_DICT to
304 dict_create_linear (obstack, NULL). */
305
306struct block *
307allocate_block (struct obstack *obstack)
308{
309 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
310
311 BLOCK_START (bl) = 0;
312 BLOCK_END (bl) = 0;
313 BLOCK_FUNCTION (bl) = NULL;
314 BLOCK_SUPERBLOCK (bl) = NULL;
315 BLOCK_DICT (bl) = NULL;
316 BLOCK_NAMESPACE (bl) = NULL;
5c4e30ca
DC
317
318 return bl;
319}
This page took 0.562518 seconds and 4 git commands to generate.