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