1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright (C) 2003, 2007-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
24 #include "gdb_obstack.h"
25 #include "cp-support.h"
28 #include "exceptions.h"
30 /* This is used by struct block to store namespace-related info for
31 C++ files, namely using declarations and the current namespace in
34 struct block_namespace_info
37 struct using_direct
*using;
40 static void block_initialize_namespace (struct block
*block
,
41 struct obstack
*obstack
);
43 /* Return Nonzero if block a is lexically nested within block b,
44 or if a and b have the same pc range.
45 Return zero otherwise. */
48 contained_in (const struct block
*a
, const struct block
*b
)
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
))
61 a
= BLOCK_SUPERBLOCK (a
);
69 /* Return the symbol for the function which contains a specified
70 lexical block, described by a struct block BL. The return value
71 will not be an inlined function; the containing function will be
75 block_linkage_function (const struct block
*bl
)
77 while ((BLOCK_FUNCTION (bl
) == NULL
|| block_inlined_p (bl
))
78 && BLOCK_SUPERBLOCK (bl
) != NULL
)
79 bl
= BLOCK_SUPERBLOCK (bl
);
81 return BLOCK_FUNCTION (bl
);
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
90 block_containing_function (const struct block
*bl
)
92 while (BLOCK_FUNCTION (bl
) == NULL
&& BLOCK_SUPERBLOCK (bl
) != NULL
)
93 bl
= BLOCK_SUPERBLOCK (bl
);
95 return BLOCK_FUNCTION (bl
);
98 /* Return one if BL represents an inlined function. */
101 block_inlined_p (const struct block
*bl
)
103 return BLOCK_FUNCTION (bl
) != NULL
&& SYMBOL_INLINED (BLOCK_FUNCTION (bl
));
106 /* A helper function that checks whether PC is in the blockvector BL.
107 It returns the containing block if there is one, or else NULL. */
109 static struct block
*
110 find_block_in_blockvector (struct blockvector
*bl
, CORE_ADDR pc
)
115 /* If we have an addrmap mapping code addresses to blocks, then use
117 if (BLOCKVECTOR_MAP (bl
))
118 return addrmap_find (BLOCKVECTOR_MAP (bl
), pc
);
120 /* Otherwise, use binary search to find the last block that starts
123 top
= BLOCKVECTOR_NBLOCKS (bl
);
125 while (top
- bot
> 1)
127 half
= (top
- bot
+ 1) >> 1;
128 b
= BLOCKVECTOR_BLOCK (bl
, bot
+ half
);
129 if (BLOCK_START (b
) <= pc
)
135 /* Now search backward for a block that ends after PC. */
139 b
= BLOCKVECTOR_BLOCK (bl
, bot
);
140 if (BLOCK_END (b
) > pc
)
148 /* Return the blockvector immediately containing the innermost lexical
149 block containing the specified pc value and section, or 0 if there
150 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
151 don't pass this information back to the caller. */
154 blockvector_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
,
155 struct block
**pblock
, struct symtab
*symtab
)
157 struct blockvector
*bl
;
160 if (symtab
== 0) /* if no symtab specified by caller */
162 /* First search all symtabs for one whose file contains our pc */
163 symtab
= find_pc_sect_symtab (pc
, section
);
168 bl
= BLOCKVECTOR (symtab
);
170 /* Then search that symtab for the smallest block that wins. */
171 b
= find_block_in_blockvector (bl
, pc
);
180 /* Return true if the blockvector BV contains PC, false otherwise. */
183 blockvector_contains_pc (struct blockvector
*bv
, CORE_ADDR pc
)
185 return find_block_in_blockvector (bv
, pc
) != NULL
;
188 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
189 must be the next instruction after call (or after tail call jump). Throw
190 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
193 call_site_for_pc (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
195 struct symtab
*symtab
;
198 /* -1 as tail call PC can be already after the compilation unit range. */
199 symtab
= find_pc_symtab (pc
- 1);
201 if (symtab
!= NULL
&& symtab
->call_site_htab
!= NULL
)
202 slot
= htab_find_slot (symtab
->call_site_htab
, &pc
, NO_INSERT
);
206 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (pc
);
208 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
210 throw_error (NO_ENTRY_VALUE_ERROR
,
211 _("DW_OP_GNU_entry_value resolving cannot find "
212 "DW_TAG_GNU_call_site %s in %s"),
213 paddress (gdbarch
, pc
),
214 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
220 /* Return the blockvector immediately containing the innermost lexical block
221 containing the specified pc value, or 0 if there is none.
222 Backward compatibility, no section. */
225 blockvector_for_pc (CORE_ADDR pc
, struct block
**pblock
)
227 return blockvector_for_pc_sect (pc
, find_pc_mapped_section (pc
),
231 /* Return the innermost lexical block containing the specified pc value
232 in the specified section, or 0 if there is none. */
235 block_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
)
237 struct blockvector
*bl
;
240 bl
= blockvector_for_pc_sect (pc
, section
, &b
, NULL
);
246 /* Return the innermost lexical block containing the specified pc value,
247 or 0 if there is none. Backward compatibility, no section. */
250 block_for_pc (CORE_ADDR pc
)
252 return block_for_pc_sect (pc
, find_pc_mapped_section (pc
));
255 /* Now come some functions designed to deal with C++ namespace issues.
256 The accessors are safe to use even in the non-C++ case. */
258 /* This returns the namespace that BLOCK is enclosed in, or "" if it
259 isn't enclosed in a namespace at all. This travels the chain of
260 superblocks looking for a scope, if necessary. */
263 block_scope (const struct block
*block
)
265 for (; block
!= NULL
; block
= BLOCK_SUPERBLOCK (block
))
267 if (BLOCK_NAMESPACE (block
) != NULL
268 && BLOCK_NAMESPACE (block
)->scope
!= NULL
)
269 return BLOCK_NAMESPACE (block
)->scope
;
275 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
276 OBSTACK. (It won't make a copy of SCOPE, however, so that already
277 has to be allocated correctly.) */
280 block_set_scope (struct block
*block
, const char *scope
,
281 struct obstack
*obstack
)
283 block_initialize_namespace (block
, obstack
);
285 BLOCK_NAMESPACE (block
)->scope
= scope
;
288 /* This returns the using directives list associated with BLOCK, if
291 struct using_direct
*
292 block_using (const struct block
*block
)
294 if (block
== NULL
|| BLOCK_NAMESPACE (block
) == NULL
)
297 return BLOCK_NAMESPACE (block
)->using;
300 /* Set BLOCK's using member to USING; if needed, allocate memory via
301 OBSTACK. (It won't make a copy of USING, however, so that already
302 has to be allocated correctly.) */
305 block_set_using (struct block
*block
,
306 struct using_direct
*using,
307 struct obstack
*obstack
)
309 block_initialize_namespace (block
, obstack
);
311 BLOCK_NAMESPACE (block
)->using = using;
314 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
315 ititialize its members to zero. */
318 block_initialize_namespace (struct block
*block
, struct obstack
*obstack
)
320 if (BLOCK_NAMESPACE (block
) == NULL
)
322 BLOCK_NAMESPACE (block
)
323 = obstack_alloc (obstack
, sizeof (struct block_namespace_info
));
324 BLOCK_NAMESPACE (block
)->scope
= NULL
;
325 BLOCK_NAMESPACE (block
)->using = NULL
;
329 /* Return the static block associated to BLOCK. Return NULL if block
330 is NULL or if block is a global block. */
333 block_static_block (const struct block
*block
)
335 if (block
== NULL
|| BLOCK_SUPERBLOCK (block
) == NULL
)
338 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block
)) != NULL
)
339 block
= BLOCK_SUPERBLOCK (block
);
344 /* Return the static block associated to BLOCK. Return NULL if block
348 block_global_block (const struct block
*block
)
353 while (BLOCK_SUPERBLOCK (block
) != NULL
)
354 block
= BLOCK_SUPERBLOCK (block
);
359 /* Allocate a block on OBSTACK, and initialize its elements to
360 zero/NULL. This is useful for creating "dummy" blocks that don't
361 correspond to actual source files.
363 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
364 valid value. If you really don't want the block to have a
365 dictionary, then you should subsequently set its BLOCK_DICT to
366 dict_create_linear (obstack, NULL). */
369 allocate_block (struct obstack
*obstack
)
371 struct block
*bl
= obstack_alloc (obstack
, sizeof (struct block
));
373 BLOCK_START (bl
) = 0;
375 BLOCK_FUNCTION (bl
) = NULL
;
376 BLOCK_SUPERBLOCK (bl
) = NULL
;
377 BLOCK_DICT (bl
) = NULL
;
378 BLOCK_NAMESPACE (bl
) = NULL
;
383 /* Allocate a global block. */
386 allocate_global_block (struct obstack
*obstack
)
388 struct global_block
*bl
= OBSTACK_ZALLOC (obstack
, struct global_block
);
393 /* Set the symtab of the global block. */
396 set_block_symtab (struct block
*block
, struct symtab
*symtab
)
398 struct global_block
*gb
;
400 gdb_assert (BLOCK_SUPERBLOCK (block
) == NULL
);
401 gb
= (struct global_block
*) block
;
402 gdb_assert (gb
->symtab
== NULL
);
406 /* Return the symtab of the global block. */
408 static struct symtab
*
409 get_block_symtab (const struct block
*block
)
411 struct global_block
*gb
;
413 gdb_assert (BLOCK_SUPERBLOCK (block
) == NULL
);
414 gb
= (struct global_block
*) block
;
415 gdb_assert (gb
->symtab
!= NULL
);
421 /* Initialize a block iterator, either to iterate over a single block,
422 or, for static and global blocks, all the included symtabs as
426 initialize_block_iterator (const struct block
*block
,
427 struct block_iterator
*iter
)
429 enum block_enum which
;
430 struct symtab
*symtab
;
434 if (BLOCK_SUPERBLOCK (block
) == NULL
)
436 which
= GLOBAL_BLOCK
;
437 symtab
= get_block_symtab (block
);
439 else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block
)) == NULL
)
441 which
= STATIC_BLOCK
;
442 symtab
= get_block_symtab (BLOCK_SUPERBLOCK (block
));
446 iter
->d
.block
= block
;
447 /* A signal value meaning that we're iterating over a single
449 iter
->which
= FIRST_LOCAL_BLOCK
;
453 /* If this is an included symtab, find the canonical includer and
455 while (symtab
->user
!= NULL
)
456 symtab
= symtab
->user
;
458 /* Putting this check here simplifies the logic of the iterator
459 functions. If there are no included symtabs, we only need to
460 search a single block, so we might as well just do that
462 if (symtab
->includes
== NULL
)
464 iter
->d
.block
= block
;
465 /* A signal value meaning that we're iterating over a single
467 iter
->which
= FIRST_LOCAL_BLOCK
;
471 iter
->d
.symtab
= symtab
;
476 /* A helper function that finds the current symtab over whose static
477 or global block we should iterate. */
479 static struct symtab
*
480 find_iterator_symtab (struct block_iterator
*iterator
)
482 if (iterator
->idx
== -1)
483 return iterator
->d
.symtab
;
484 return iterator
->d
.symtab
->includes
[iterator
->idx
];
487 /* Perform a single step for a plain block iterator, iterating across
488 symbol tables as needed. Returns the next symbol, or NULL when
489 iteration is complete. */
491 static struct symbol
*
492 block_iterator_step (struct block_iterator
*iterator
, int first
)
496 gdb_assert (iterator
->which
!= FIRST_LOCAL_BLOCK
);
502 struct symtab
*symtab
= find_iterator_symtab (iterator
);
503 const struct block
*block
;
505 /* Iteration is complete. */
509 block
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab
), iterator
->which
);
510 sym
= dict_iterator_first (BLOCK_DICT (block
), &iterator
->dict_iter
);
513 sym
= dict_iterator_next (&iterator
->dict_iter
);
518 /* We have finished iterating the appropriate block of one
519 symtab. Now advance to the next symtab and begin iteration
529 block_iterator_first (const struct block
*block
,
530 struct block_iterator
*iterator
)
532 initialize_block_iterator (block
, iterator
);
534 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
535 return dict_iterator_first (block
->dict
, &iterator
->dict_iter
);
537 return block_iterator_step (iterator
, 1);
543 block_iterator_next (struct block_iterator
*iterator
)
545 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
546 return dict_iterator_next (&iterator
->dict_iter
);
548 return block_iterator_step (iterator
, 0);
551 /* Perform a single step for a "name" block iterator, iterating across
552 symbol tables as needed. Returns the next symbol, or NULL when
553 iteration is complete. */
555 static struct symbol
*
556 block_iter_name_step (struct block_iterator
*iterator
, const char *name
,
561 gdb_assert (iterator
->which
!= FIRST_LOCAL_BLOCK
);
567 struct symtab
*symtab
= find_iterator_symtab (iterator
);
568 const struct block
*block
;
570 /* Iteration is complete. */
574 block
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab
), iterator
->which
);
575 sym
= dict_iter_name_first (BLOCK_DICT (block
), name
,
576 &iterator
->dict_iter
);
579 sym
= dict_iter_name_next (name
, &iterator
->dict_iter
);
584 /* We have finished iterating the appropriate block of one
585 symtab. Now advance to the next symtab and begin iteration
595 block_iter_name_first (const struct block
*block
,
597 struct block_iterator
*iterator
)
599 initialize_block_iterator (block
, iterator
);
601 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
602 return dict_iter_name_first (block
->dict
, name
, &iterator
->dict_iter
);
604 return block_iter_name_step (iterator
, name
, 1);
610 block_iter_name_next (const char *name
, struct block_iterator
*iterator
)
612 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
613 return dict_iter_name_next (name
, &iterator
->dict_iter
);
615 return block_iter_name_step (iterator
, name
, 0);
618 /* Perform a single step for a "match" block iterator, iterating
619 across symbol tables as needed. Returns the next symbol, or NULL
620 when iteration is complete. */
622 static struct symbol
*
623 block_iter_match_step (struct block_iterator
*iterator
,
625 symbol_compare_ftype
*compare
,
630 gdb_assert (iterator
->which
!= FIRST_LOCAL_BLOCK
);
636 struct symtab
*symtab
= find_iterator_symtab (iterator
);
637 const struct block
*block
;
639 /* Iteration is complete. */
643 block
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab
), iterator
->which
);
644 sym
= dict_iter_match_first (BLOCK_DICT (block
), name
,
645 compare
, &iterator
->dict_iter
);
648 sym
= dict_iter_match_next (name
, compare
, &iterator
->dict_iter
);
653 /* We have finished iterating the appropriate block of one
654 symtab. Now advance to the next symtab and begin iteration
664 block_iter_match_first (const struct block
*block
,
666 symbol_compare_ftype
*compare
,
667 struct block_iterator
*iterator
)
669 initialize_block_iterator (block
, iterator
);
671 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
672 return dict_iter_match_first (block
->dict
, name
, compare
,
673 &iterator
->dict_iter
);
675 return block_iter_match_step (iterator
, name
, compare
, 1);
681 block_iter_match_next (const char *name
,
682 symbol_compare_ftype
*compare
,
683 struct block_iterator
*iterator
)
685 if (iterator
->which
== FIRST_LOCAL_BLOCK
)
686 return dict_iter_match_next (name
, compare
, &iterator
->dict_iter
);
688 return block_iter_match_step (iterator
, name
, compare
, 0);
This page took 0.043451 seconds and 4 git commands to generate.