1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright (C) 2003, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
25 #include "gdb_obstack.h"
26 #include "cp-support.h"
29 #include "exceptions.h"
31 /* This is used by struct block to store namespace-related info for
32 C++ files, namely using declarations and the current namespace in
35 struct block_namespace_info
38 struct using_direct
*using;
41 static void block_initialize_namespace (struct block
*block
,
42 struct obstack
*obstack
);
44 /* Return Nonzero if block a is lexically nested within block b,
45 or if a and b have the same pc range.
46 Return zero otherwise. */
49 contained_in (const struct block
*a
, const struct block
*b
)
58 /* If A is a function block, then A cannot be contained in B,
59 except if A was inlined. */
60 if (BLOCK_FUNCTION (a
) != NULL
&& !block_inlined_p (a
))
62 a
= BLOCK_SUPERBLOCK (a
);
70 /* Return the symbol for the function which contains a specified
71 lexical block, described by a struct block BL. The return value
72 will not be an inlined function; the containing function will be
76 block_linkage_function (const struct block
*bl
)
78 while ((BLOCK_FUNCTION (bl
) == NULL
|| block_inlined_p (bl
))
79 && BLOCK_SUPERBLOCK (bl
) != NULL
)
80 bl
= BLOCK_SUPERBLOCK (bl
);
82 return BLOCK_FUNCTION (bl
);
85 /* Return the symbol for the function which contains a specified
86 block, described by a struct block BL. The return value will be
87 the closest enclosing function, which might be an inline
91 block_containing_function (const struct block
*bl
)
93 while (BLOCK_FUNCTION (bl
) == NULL
&& BLOCK_SUPERBLOCK (bl
) != NULL
)
94 bl
= BLOCK_SUPERBLOCK (bl
);
96 return BLOCK_FUNCTION (bl
);
99 /* Return one if BL represents an inlined function. */
102 block_inlined_p (const struct block
*bl
)
104 return BLOCK_FUNCTION (bl
) != NULL
&& SYMBOL_INLINED (BLOCK_FUNCTION (bl
));
107 /* Return the blockvector immediately containing the innermost lexical
108 block containing the specified pc value and section, or 0 if there
109 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
110 don't pass this information back to the caller. */
113 blockvector_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
,
114 struct block
**pblock
, struct symtab
*symtab
)
118 struct blockvector
*bl
;
120 if (symtab
== 0) /* if no symtab specified by caller */
122 /* First search all symtabs for one whose file contains our pc */
123 symtab
= find_pc_sect_symtab (pc
, section
);
128 bl
= BLOCKVECTOR (symtab
);
130 /* Then search that symtab for the smallest block that wins. */
132 /* If we have an addrmap mapping code addresses to blocks, then use
134 if (BLOCKVECTOR_MAP (bl
))
136 b
= addrmap_find (BLOCKVECTOR_MAP (bl
), pc
);
148 /* Otherwise, use binary search to find the last block that starts
151 top
= BLOCKVECTOR_NBLOCKS (bl
);
153 while (top
- bot
> 1)
155 half
= (top
- bot
+ 1) >> 1;
156 b
= BLOCKVECTOR_BLOCK (bl
, bot
+ half
);
157 if (BLOCK_START (b
) <= pc
)
163 /* Now search backward for a block that ends after PC. */
167 b
= BLOCKVECTOR_BLOCK (bl
, bot
);
168 if (BLOCK_END (b
) > pc
)
179 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
180 must be the next instruction after call (or after tail call jump). Throw
181 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
184 call_site_for_pc (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
186 struct symtab
*symtab
;
189 /* -1 as tail call PC can be already after the compilation unit range. */
190 symtab
= find_pc_symtab (pc
- 1);
192 if (symtab
!= NULL
&& symtab
->call_site_htab
!= NULL
)
193 slot
= htab_find_slot (symtab
->call_site_htab
, &pc
, NO_INSERT
);
197 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (pc
);
199 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
201 throw_error (NO_ENTRY_VALUE_ERROR
,
202 _("DW_OP_GNU_entry_value resolving cannot find "
203 "DW_TAG_GNU_call_site %s in %s"),
204 paddress (gdbarch
, pc
),
205 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
211 /* Return the blockvector immediately containing the innermost lexical block
212 containing the specified pc value, or 0 if there is none.
213 Backward compatibility, no section. */
216 blockvector_for_pc (CORE_ADDR pc
, struct block
**pblock
)
218 return blockvector_for_pc_sect (pc
, find_pc_mapped_section (pc
),
222 /* Return the innermost lexical block containing the specified pc value
223 in the specified section, or 0 if there is none. */
226 block_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
)
228 struct blockvector
*bl
;
231 bl
= blockvector_for_pc_sect (pc
, section
, &b
, NULL
);
237 /* Return the innermost lexical block containing the specified pc value,
238 or 0 if there is none. Backward compatibility, no section. */
241 block_for_pc (CORE_ADDR pc
)
243 return block_for_pc_sect (pc
, find_pc_mapped_section (pc
));
246 /* Now come some functions designed to deal with C++ namespace issues.
247 The accessors are safe to use even in the non-C++ case. */
249 /* This returns the namespace that BLOCK is enclosed in, or "" if it
250 isn't enclosed in a namespace at all. This travels the chain of
251 superblocks looking for a scope, if necessary. */
254 block_scope (const struct block
*block
)
256 for (; block
!= NULL
; block
= BLOCK_SUPERBLOCK (block
))
258 if (BLOCK_NAMESPACE (block
) != NULL
259 && BLOCK_NAMESPACE (block
)->scope
!= NULL
)
260 return BLOCK_NAMESPACE (block
)->scope
;
266 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
267 OBSTACK. (It won't make a copy of SCOPE, however, so that already
268 has to be allocated correctly.) */
271 block_set_scope (struct block
*block
, const char *scope
,
272 struct obstack
*obstack
)
274 block_initialize_namespace (block
, obstack
);
276 BLOCK_NAMESPACE (block
)->scope
= scope
;
279 /* This returns the using directives list associated with BLOCK, if
282 struct using_direct
*
283 block_using (const struct block
*block
)
285 if (block
== NULL
|| BLOCK_NAMESPACE (block
) == NULL
)
288 return BLOCK_NAMESPACE (block
)->using;
291 /* Set BLOCK's using member to USING; if needed, allocate memory via
292 OBSTACK. (It won't make a copy of USING, however, so that already
293 has to be allocated correctly.) */
296 block_set_using (struct block
*block
,
297 struct using_direct
*using,
298 struct obstack
*obstack
)
300 block_initialize_namespace (block
, obstack
);
302 BLOCK_NAMESPACE (block
)->using = using;
305 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
306 ititialize its members to zero. */
309 block_initialize_namespace (struct block
*block
, struct obstack
*obstack
)
311 if (BLOCK_NAMESPACE (block
) == NULL
)
313 BLOCK_NAMESPACE (block
)
314 = obstack_alloc (obstack
, sizeof (struct block_namespace_info
));
315 BLOCK_NAMESPACE (block
)->scope
= NULL
;
316 BLOCK_NAMESPACE (block
)->using = NULL
;
320 /* Return the static block associated to BLOCK. Return NULL if block
321 is NULL or if block is a global block. */
324 block_static_block (const struct block
*block
)
326 if (block
== NULL
|| BLOCK_SUPERBLOCK (block
) == NULL
)
329 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block
)) != NULL
)
330 block
= BLOCK_SUPERBLOCK (block
);
335 /* Return the static block associated to BLOCK. Return NULL if block
339 block_global_block (const struct block
*block
)
344 while (BLOCK_SUPERBLOCK (block
) != NULL
)
345 block
= BLOCK_SUPERBLOCK (block
);
350 /* Allocate a block on OBSTACK, and initialize its elements to
351 zero/NULL. This is useful for creating "dummy" blocks that don't
352 correspond to actual source files.
354 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
355 valid value. If you really don't want the block to have a
356 dictionary, then you should subsequently set its BLOCK_DICT to
357 dict_create_linear (obstack, NULL). */
360 allocate_block (struct obstack
*obstack
)
362 struct block
*bl
= obstack_alloc (obstack
, sizeof (struct block
));
364 BLOCK_START (bl
) = 0;
366 BLOCK_FUNCTION (bl
) = NULL
;
367 BLOCK_SUPERBLOCK (bl
) = NULL
;
368 BLOCK_DICT (bl
) = NULL
;
369 BLOCK_NAMESPACE (bl
) = NULL
;
This page took 0.03723 seconds and 4 git commands to generate.