add xfail for "print u_var" test in gdb.ada/packed_array.exp
[deliverable/binutils-gdb.git] / gdb / block.c
1 /* Block-related functions for the GNU debugger, GDB.
2
3 Copyright (C) 2003, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
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
10 the Free Software Foundation; either version 3 of the License, or
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "block.h"
23 #include "symtab.h"
24 #include "symfile.h"
25 #include "gdb_obstack.h"
26 #include "cp-support.h"
27 #include "addrmap.h"
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
33 struct block_namespace_info
34 {
35 const char *scope;
36 struct using_direct *using;
37 };
38
39 static void block_initialize_namespace (struct block *block,
40 struct obstack *obstack);
41
42 /* Return Nonzero if block a is lexically nested within block b,
43 or if a and b have the same pc range.
44 Return zero otherwise. */
45
46 int
47 contained_in (const struct block *a, const struct block *b)
48 {
49 if (!a || !b)
50 return 0;
51
52 do
53 {
54 if (a == b)
55 return 1;
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;
60 a = BLOCK_SUPERBLOCK (a);
61 }
62 while (a != NULL);
63
64 return 0;
65 }
66
67
68 /* Return the symbol for the function which contains a specified
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. */
72
73 struct symbol *
74 block_linkage_function (const struct block *bl)
75 {
76 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
77 && BLOCK_SUPERBLOCK (bl) != NULL)
78 bl = BLOCK_SUPERBLOCK (bl);
79
80 return BLOCK_FUNCTION (bl);
81 }
82
83 /* Return one if BL represents an inlined function. */
84
85 int
86 block_inlined_p (const struct block *bl)
87 {
88 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
89 }
90
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. */
95
96 struct blockvector *
97 blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
98 struct block **pblock, struct symtab *symtab)
99 {
100 struct block *b;
101 int bot, top, half;
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 */
107 symtab = find_pc_sect_symtab (pc, section);
108 if (symtab == 0)
109 return 0;
110 }
111
112 bl = BLOCKVECTOR (symtab);
113
114 /* Then search that symtab for the smallest block that wins. */
115
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. */
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 {
154 if (pblock)
155 *pblock = b;
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
167 struct blockvector *
168 blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
169 {
170 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
171 pblock, NULL);
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
177 struct block *
178 block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
179 {
180 struct blockvector *bl;
181 struct block *b;
182
183 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
184 if (bl)
185 return b;
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
192 struct block *
193 block_for_pc (CORE_ADDR pc)
194 {
195 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
196 }
197
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
205 const char *
206 block_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 }
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
222 void
223 block_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
231 /* This returns the using directives list associated with BLOCK, if
232 any. */
233
234 struct using_direct *
235 block_using (const struct block *block)
236 {
237 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
238 return NULL;
239 else
240 return BLOCK_NAMESPACE (block)->using;
241 }
242
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
247 void
248 block_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
260 static void
261 block_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 }
271
272 /* Return the static block associated to BLOCK. Return NULL if block
273 is NULL or if block is a global block. */
274
275 const struct block *
276 block_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 }
286
287 /* Return the static block associated to BLOCK. Return NULL if block
288 is NULL. */
289
290 const struct block *
291 block_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 }
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
311 struct block *
312 allocate_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;
322
323 return bl;
324 }
This page took 0.035332 seconds and 4 git commands to generate.