2009-06-23 Sami Wagiaalla <swagiaal@redhat.com>
[deliverable/binutils-gdb.git] / gdb / block.c
1 /* Block-related functions for the GNU debugger, GDB.
2
3 Copyright (C) 2003, 2007, 2008, 2009 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "block.h"
22 #include "symtab.h"
23 #include "symfile.h"
24 #include "gdb_obstack.h"
25 #include "cp-support.h"
26 #include "addrmap.h"
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
32 struct block_namespace_info
33 {
34 const char *scope;
35 struct using_direct *using;
36 };
37
38 static void block_initialize_namespace (struct block *block,
39 struct obstack *obstack);
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
45 int
46 contained_in (const struct block *a, const struct block *b)
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
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. */
59
60 struct symbol *
61 block_linkage_function (const struct block *bl)
62 {
63 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
64 bl = BLOCK_SUPERBLOCK (bl);
65
66 return BLOCK_FUNCTION (bl);
67 }
68
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. */
73
74 struct blockvector *
75 blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
76 struct block **pblock, struct symtab *symtab)
77 {
78 struct block *b;
79 int bot, top, half;
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 */
85 symtab = find_pc_sect_symtab (pc, section);
86 if (symtab == 0)
87 return 0;
88 }
89
90 bl = BLOCKVECTOR (symtab);
91
92 /* Then search that symtab for the smallest block that wins. */
93
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. */
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 {
132 if (pblock)
133 *pblock = b;
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
145 struct blockvector *
146 blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
147 {
148 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
149 pblock, NULL);
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
155 struct block *
156 block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
157 {
158 struct blockvector *bl;
159 struct block *b;
160
161 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
162 if (bl)
163 return b;
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
170 struct block *
171 block_for_pc (CORE_ADDR pc)
172 {
173 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
174 }
175
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
183 const char *
184 block_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 }
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
200 void
201 block_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
209 /* This returns the using directives list associated with BLOCK, if
210 any. */
211
212 struct using_direct *
213 block_using (const struct block *block)
214 {
215 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
216 return NULL;
217 else
218 return BLOCK_NAMESPACE (block)->using;
219 }
220
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
225 void
226 block_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
238 static void
239 block_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 }
249
250 /* Return the static block associated to BLOCK. Return NULL if block
251 is NULL or if block is a global block. */
252
253 const struct block *
254 block_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 }
264
265 /* Return the static block associated to BLOCK. Return NULL if block
266 is NULL. */
267
268 const struct block *
269 block_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 }
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
289 struct block *
290 allocate_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;
300
301 return bl;
302 }
This page took 0.04946 seconds and 5 git commands to generate.