* psymtab.c (partial_map_expand_apply): Add assertion.
[deliverable/binutils-gdb.git] / gdb / block.c
CommitLineData
fe898f56
DC
1/* Block-related functions for the GNU debugger, GDB.
2
0b302171 3 Copyright (C) 2003, 2007-2012 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"
8e3b41a9
JK
27#include "gdbtypes.h"
28#include "exceptions.h"
9219021c
DC
29
30/* This is used by struct block to store namespace-related info for
31 C++ files, namely using declarations and the current namespace in
32 scope. */
33
34struct block_namespace_info
35{
36 const char *scope;
37 struct using_direct *using;
38};
39
40static void block_initialize_namespace (struct block *block,
41 struct obstack *obstack);
fe898f56
DC
42
43/* Return Nonzero if block a is lexically nested within block b,
44 or if a and b have the same pc range.
4a64f543 45 Return zero otherwise. */
fe898f56
DC
46
47int
0cf566ec 48contained_in (const struct block *a, const struct block *b)
fe898f56
DC
49{
50 if (!a || !b)
51 return 0;
edb3359d
DJ
52
53 do
54 {
55 if (a == b)
56 return 1;
49e794ac
JB
57 /* If A is a function block, then A cannot be contained in B,
58 except if A was inlined. */
59 if (BLOCK_FUNCTION (a) != NULL && !block_inlined_p (a))
60 return 0;
edb3359d
DJ
61 a = BLOCK_SUPERBLOCK (a);
62 }
63 while (a != NULL);
64
65 return 0;
fe898f56
DC
66}
67
68
69/* Return the symbol for the function which contains a specified
7f0df278
DJ
70 lexical block, described by a struct block BL. The return value
71 will not be an inlined function; the containing function will be
72 returned instead. */
fe898f56
DC
73
74struct symbol *
7f0df278 75block_linkage_function (const struct block *bl)
fe898f56 76{
edb3359d
DJ
77 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
78 && BLOCK_SUPERBLOCK (bl) != NULL)
fe898f56
DC
79 bl = BLOCK_SUPERBLOCK (bl);
80
81 return BLOCK_FUNCTION (bl);
82}
83
f8eba3c6
TT
84/* Return the symbol for the function which contains a specified
85 block, described by a struct block BL. The return value will be
86 the closest enclosing function, which might be an inline
87 function. */
88
89struct symbol *
90block_containing_function (const struct block *bl)
91{
92 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
93 bl = BLOCK_SUPERBLOCK (bl);
94
95 return BLOCK_FUNCTION (bl);
96}
97
edb3359d
DJ
98/* Return one if BL represents an inlined function. */
99
100int
101block_inlined_p (const struct block *bl)
102{
103 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
104}
105
801e3a5b
JB
106/* Return the blockvector immediately containing the innermost lexical
107 block containing the specified pc value and section, or 0 if there
108 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
109 don't pass this information back to the caller. */
fe898f56
DC
110
111struct blockvector *
714835d5 112blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
801e3a5b 113 struct block **pblock, struct symtab *symtab)
fe898f56 114{
b59661bd
AC
115 struct block *b;
116 int bot, top, half;
fe898f56
DC
117 struct blockvector *bl;
118
119 if (symtab == 0) /* if no symtab specified by caller */
120 {
121 /* First search all symtabs for one whose file contains our pc */
b59661bd
AC
122 symtab = find_pc_sect_symtab (pc, section);
123 if (symtab == 0)
fe898f56
DC
124 return 0;
125 }
126
127 bl = BLOCKVECTOR (symtab);
fe898f56
DC
128
129 /* Then search that symtab for the smallest block that wins. */
fe898f56 130
801e3a5b
JB
131 /* If we have an addrmap mapping code addresses to blocks, then use
132 that. */
133 if (BLOCKVECTOR_MAP (bl))
134 {
135 b = addrmap_find (BLOCKVECTOR_MAP (bl), pc);
136 if (b)
137 {
138 if (pblock)
139 *pblock = b;
140 return bl;
141 }
142 else
143 return 0;
144 }
145
146
147 /* Otherwise, use binary search to find the last block that starts
148 before PC. */
fe898f56
DC
149 bot = 0;
150 top = BLOCKVECTOR_NBLOCKS (bl);
151
152 while (top - bot > 1)
153 {
154 half = (top - bot + 1) >> 1;
155 b = BLOCKVECTOR_BLOCK (bl, bot + half);
156 if (BLOCK_START (b) <= pc)
157 bot += half;
158 else
159 top = bot + half;
160 }
161
162 /* Now search backward for a block that ends after PC. */
163
164 while (bot >= 0)
165 {
166 b = BLOCKVECTOR_BLOCK (bl, bot);
167 if (BLOCK_END (b) > pc)
168 {
801e3a5b
JB
169 if (pblock)
170 *pblock = b;
fe898f56
DC
171 return bl;
172 }
173 bot--;
174 }
175 return 0;
176}
177
8e3b41a9
JK
178/* Return call_site for specified PC in GDBARCH. PC must match exactly, it
179 must be the next instruction after call (or after tail call jump). Throw
180 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
181
182struct call_site *
183call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc)
184{
185 struct symtab *symtab;
186 void **slot = NULL;
187
188 /* -1 as tail call PC can be already after the compilation unit range. */
189 symtab = find_pc_symtab (pc - 1);
190
191 if (symtab != NULL && symtab->call_site_htab != NULL)
192 slot = htab_find_slot (symtab->call_site_htab, &pc, NO_INSERT);
193
194 if (slot == NULL)
195 {
196 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (pc);
197
198 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
199 the call target. */
200 throw_error (NO_ENTRY_VALUE_ERROR,
201 _("DW_OP_GNU_entry_value resolving cannot find "
202 "DW_TAG_GNU_call_site %s in %s"),
203 paddress (gdbarch, pc),
204 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
205 }
206
207 return *slot;
208}
209
fe898f56
DC
210/* Return the blockvector immediately containing the innermost lexical block
211 containing the specified pc value, or 0 if there is none.
212 Backward compatibility, no section. */
213
214struct blockvector *
801e3a5b 215blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
fe898f56
DC
216{
217 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
801e3a5b 218 pblock, NULL);
fe898f56
DC
219}
220
221/* Return the innermost lexical block containing the specified pc value
222 in the specified section, or 0 if there is none. */
223
224struct block *
714835d5 225block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
fe898f56 226{
b59661bd 227 struct blockvector *bl;
801e3a5b 228 struct block *b;
fe898f56 229
801e3a5b 230 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
fe898f56 231 if (bl)
801e3a5b 232 return b;
fe898f56
DC
233 return 0;
234}
235
236/* Return the innermost lexical block containing the specified pc value,
237 or 0 if there is none. Backward compatibility, no section. */
238
239struct block *
b59661bd 240block_for_pc (CORE_ADDR pc)
fe898f56
DC
241{
242 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
243}
9219021c 244
1fcb5155
DC
245/* Now come some functions designed to deal with C++ namespace issues.
246 The accessors are safe to use even in the non-C++ case. */
247
248/* This returns the namespace that BLOCK is enclosed in, or "" if it
249 isn't enclosed in a namespace at all. This travels the chain of
250 superblocks looking for a scope, if necessary. */
251
252const char *
253block_scope (const struct block *block)
254{
255 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
256 {
257 if (BLOCK_NAMESPACE (block) != NULL
258 && BLOCK_NAMESPACE (block)->scope != NULL)
259 return BLOCK_NAMESPACE (block)->scope;
260 }
261
262 return "";
263}
9219021c
DC
264
265/* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
266 OBSTACK. (It won't make a copy of SCOPE, however, so that already
267 has to be allocated correctly.) */
268
269void
270block_set_scope (struct block *block, const char *scope,
271 struct obstack *obstack)
272{
273 block_initialize_namespace (block, obstack);
274
275 BLOCK_NAMESPACE (block)->scope = scope;
276}
277
27aa8d6a 278/* This returns the using directives list associated with BLOCK, if
1fcb5155
DC
279 any. */
280
1fcb5155
DC
281struct using_direct *
282block_using (const struct block *block)
283{
27aa8d6a 284 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
1fcb5155
DC
285 return NULL;
286 else
27aa8d6a 287 return BLOCK_NAMESPACE (block)->using;
1fcb5155
DC
288}
289
9219021c
DC
290/* Set BLOCK's using member to USING; if needed, allocate memory via
291 OBSTACK. (It won't make a copy of USING, however, so that already
292 has to be allocated correctly.) */
293
294void
295block_set_using (struct block *block,
296 struct using_direct *using,
297 struct obstack *obstack)
298{
299 block_initialize_namespace (block, obstack);
300
301 BLOCK_NAMESPACE (block)->using = using;
302}
303
304/* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
305 ititialize its members to zero. */
306
307static void
308block_initialize_namespace (struct block *block, struct obstack *obstack)
309{
310 if (BLOCK_NAMESPACE (block) == NULL)
311 {
312 BLOCK_NAMESPACE (block)
313 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
314 BLOCK_NAMESPACE (block)->scope = NULL;
315 BLOCK_NAMESPACE (block)->using = NULL;
316 }
317}
89a9d1b1
DC
318
319/* Return the static block associated to BLOCK. Return NULL if block
320 is NULL or if block is a global block. */
321
322const struct block *
323block_static_block (const struct block *block)
324{
325 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
326 return NULL;
327
328 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
329 block = BLOCK_SUPERBLOCK (block);
330
331 return block;
332}
1fcb5155
DC
333
334/* Return the static block associated to BLOCK. Return NULL if block
335 is NULL. */
336
337const struct block *
338block_global_block (const struct block *block)
339{
340 if (block == NULL)
341 return NULL;
342
343 while (BLOCK_SUPERBLOCK (block) != NULL)
344 block = BLOCK_SUPERBLOCK (block);
345
346 return block;
347}
5c4e30ca
DC
348
349/* Allocate a block on OBSTACK, and initialize its elements to
350 zero/NULL. This is useful for creating "dummy" blocks that don't
351 correspond to actual source files.
352
353 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
354 valid value. If you really don't want the block to have a
355 dictionary, then you should subsequently set its BLOCK_DICT to
356 dict_create_linear (obstack, NULL). */
357
358struct block *
359allocate_block (struct obstack *obstack)
360{
361 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
362
363 BLOCK_START (bl) = 0;
364 BLOCK_END (bl) = 0;
365 BLOCK_FUNCTION (bl) = NULL;
366 BLOCK_SUPERBLOCK (bl) = NULL;
367 BLOCK_DICT (bl) = NULL;
368 BLOCK_NAMESPACE (bl) = NULL;
5c4e30ca
DC
369
370 return bl;
371}
8157b174
TT
372
373\f
374
375/* See block.h. */
376
377struct symbol *
378block_iterator_first (const struct block *block,
379 struct block_iterator *iterator)
380{
381 return dict_iterator_first (block->dict, &iterator->dict_iter);
382}
383
384/* See block.h. */
385
386struct symbol *
387block_iterator_next (struct block_iterator *iterator)
388{
389 return dict_iterator_next (&iterator->dict_iter);
390}
391
392/* See block.h. */
393
394struct symbol *
395block_iter_name_first (const struct block *block,
396 const char *name,
397 struct block_iterator *iterator)
398{
399 return dict_iter_name_first (block->dict, name, &iterator->dict_iter);
400}
401
402/* See block.h. */
403
404struct symbol *
405block_iter_name_next (const char *name, struct block_iterator *iterator)
406{
407 return dict_iter_name_next (name, &iterator->dict_iter);
408}
409
410/* See block.h. */
411
412struct symbol *
413block_iter_match_first (const struct block *block,
414 const char *name,
415 symbol_compare_ftype *compare,
416 struct block_iterator *iterator)
417{
418 return dict_iter_match_first (block->dict, name, compare,
419 &iterator->dict_iter);
420}
421
422/* See block.h. */
423
424struct symbol *
425block_iter_match_next (const char *name,
426 symbol_compare_ftype *compare,
427 struct block_iterator *iterator)
428{
429 return dict_iter_match_next (name, compare, &iterator->dict_iter);
430}
This page took 0.54592 seconds and 4 git commands to generate.