xref: /dflybsd-src/contrib/gdb-7/gdb/block.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Block-related functions for the GNU debugger, GDB.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 2003-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "block.h"
225796c8dcSSimon Schubert #include "symtab.h"
235796c8dcSSimon Schubert #include "symfile.h"
245796c8dcSSimon Schubert #include "gdb_obstack.h"
255796c8dcSSimon Schubert #include "cp-support.h"
265796c8dcSSimon Schubert #include "addrmap.h"
27a45ae5f8SJohn Marino #include "gdbtypes.h"
28a45ae5f8SJohn Marino #include "exceptions.h"
295796c8dcSSimon Schubert 
305796c8dcSSimon Schubert /* This is used by struct block to store namespace-related info for
315796c8dcSSimon Schubert    C++ files, namely using declarations and the current namespace in
325796c8dcSSimon Schubert    scope.  */
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert struct block_namespace_info
355796c8dcSSimon Schubert {
365796c8dcSSimon Schubert   const char *scope;
375796c8dcSSimon Schubert   struct using_direct *using;
385796c8dcSSimon Schubert };
395796c8dcSSimon Schubert 
405796c8dcSSimon Schubert static void block_initialize_namespace (struct block *block,
415796c8dcSSimon Schubert 					struct obstack *obstack);
425796c8dcSSimon Schubert 
435796c8dcSSimon Schubert /* Return Nonzero if block a is lexically nested within block b,
445796c8dcSSimon Schubert    or if a and b have the same pc range.
455796c8dcSSimon Schubert    Return zero otherwise.  */
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert int
contained_in(const struct block * a,const struct block * b)485796c8dcSSimon Schubert contained_in (const struct block *a, const struct block *b)
495796c8dcSSimon Schubert {
505796c8dcSSimon Schubert   if (!a || !b)
515796c8dcSSimon Schubert     return 0;
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert   do
545796c8dcSSimon Schubert     {
555796c8dcSSimon Schubert       if (a == b)
565796c8dcSSimon Schubert 	return 1;
575796c8dcSSimon Schubert       /* If A is a function block, then A cannot be contained in B,
585796c8dcSSimon Schubert          except if A was inlined.  */
595796c8dcSSimon Schubert       if (BLOCK_FUNCTION (a) != NULL && !block_inlined_p (a))
605796c8dcSSimon Schubert         return 0;
615796c8dcSSimon Schubert       a = BLOCK_SUPERBLOCK (a);
625796c8dcSSimon Schubert     }
635796c8dcSSimon Schubert   while (a != NULL);
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert   return 0;
665796c8dcSSimon Schubert }
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert 
695796c8dcSSimon Schubert /* Return the symbol for the function which contains a specified
705796c8dcSSimon Schubert    lexical block, described by a struct block BL.  The return value
715796c8dcSSimon Schubert    will not be an inlined function; the containing function will be
725796c8dcSSimon Schubert    returned instead.  */
735796c8dcSSimon Schubert 
745796c8dcSSimon Schubert struct symbol *
block_linkage_function(const struct block * bl)755796c8dcSSimon Schubert block_linkage_function (const struct block *bl)
765796c8dcSSimon Schubert {
775796c8dcSSimon Schubert   while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
785796c8dcSSimon Schubert 	 && BLOCK_SUPERBLOCK (bl) != NULL)
795796c8dcSSimon Schubert     bl = BLOCK_SUPERBLOCK (bl);
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert   return BLOCK_FUNCTION (bl);
825796c8dcSSimon Schubert }
835796c8dcSSimon Schubert 
84a45ae5f8SJohn Marino /* Return the symbol for the function which contains a specified
85a45ae5f8SJohn Marino    block, described by a struct block BL.  The return value will be
86a45ae5f8SJohn Marino    the closest enclosing function, which might be an inline
87a45ae5f8SJohn Marino    function.  */
88a45ae5f8SJohn Marino 
89a45ae5f8SJohn Marino struct symbol *
block_containing_function(const struct block * bl)90a45ae5f8SJohn Marino block_containing_function (const struct block *bl)
91a45ae5f8SJohn Marino {
92a45ae5f8SJohn Marino   while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
93a45ae5f8SJohn Marino     bl = BLOCK_SUPERBLOCK (bl);
94a45ae5f8SJohn Marino 
95a45ae5f8SJohn Marino   return BLOCK_FUNCTION (bl);
96a45ae5f8SJohn Marino }
97a45ae5f8SJohn Marino 
985796c8dcSSimon Schubert /* Return one if BL represents an inlined function.  */
995796c8dcSSimon Schubert 
1005796c8dcSSimon Schubert int
block_inlined_p(const struct block * bl)1015796c8dcSSimon Schubert block_inlined_p (const struct block *bl)
1025796c8dcSSimon Schubert {
1035796c8dcSSimon Schubert   return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
1045796c8dcSSimon Schubert }
1055796c8dcSSimon Schubert 
106*ef5ccd6cSJohn Marino /* A helper function that checks whether PC is in the blockvector BL.
107*ef5ccd6cSJohn Marino    It returns the containing block if there is one, or else NULL.  */
1085796c8dcSSimon Schubert 
109*ef5ccd6cSJohn Marino static struct block *
find_block_in_blockvector(struct blockvector * bl,CORE_ADDR pc)110*ef5ccd6cSJohn Marino find_block_in_blockvector (struct blockvector *bl, CORE_ADDR pc)
1115796c8dcSSimon Schubert {
1125796c8dcSSimon Schubert   struct block *b;
1135796c8dcSSimon Schubert   int bot, top, half;
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert   /* If we have an addrmap mapping code addresses to blocks, then use
1165796c8dcSSimon Schubert      that.  */
1175796c8dcSSimon Schubert   if (BLOCKVECTOR_MAP (bl))
118*ef5ccd6cSJohn Marino     return addrmap_find (BLOCKVECTOR_MAP (bl), pc);
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert   /* Otherwise, use binary search to find the last block that starts
121*ef5ccd6cSJohn Marino      before PC.
122*ef5ccd6cSJohn Marino      Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1.
123*ef5ccd6cSJohn Marino      They both have the same START,END values.
124*ef5ccd6cSJohn Marino      Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the
125*ef5ccd6cSJohn Marino      fact that this choice was made was subtle, now we make it explicit.  */
126*ef5ccd6cSJohn Marino   gdb_assert (BLOCKVECTOR_NBLOCKS (bl) >= 2);
127*ef5ccd6cSJohn Marino   bot = STATIC_BLOCK;
1285796c8dcSSimon Schubert   top = BLOCKVECTOR_NBLOCKS (bl);
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert   while (top - bot > 1)
1315796c8dcSSimon Schubert     {
1325796c8dcSSimon Schubert       half = (top - bot + 1) >> 1;
1335796c8dcSSimon Schubert       b = BLOCKVECTOR_BLOCK (bl, bot + half);
1345796c8dcSSimon Schubert       if (BLOCK_START (b) <= pc)
1355796c8dcSSimon Schubert 	bot += half;
1365796c8dcSSimon Schubert       else
1375796c8dcSSimon Schubert 	top = bot + half;
1385796c8dcSSimon Schubert     }
1395796c8dcSSimon Schubert 
1405796c8dcSSimon Schubert   /* Now search backward for a block that ends after PC.  */
1415796c8dcSSimon Schubert 
142*ef5ccd6cSJohn Marino   while (bot >= STATIC_BLOCK)
1435796c8dcSSimon Schubert     {
1445796c8dcSSimon Schubert       b = BLOCKVECTOR_BLOCK (bl, bot);
1455796c8dcSSimon Schubert       if (BLOCK_END (b) > pc)
146*ef5ccd6cSJohn Marino 	return b;
147*ef5ccd6cSJohn Marino       bot--;
148*ef5ccd6cSJohn Marino     }
149*ef5ccd6cSJohn Marino 
150*ef5ccd6cSJohn Marino   return NULL;
151*ef5ccd6cSJohn Marino }
152*ef5ccd6cSJohn Marino 
153*ef5ccd6cSJohn Marino /* Return the blockvector immediately containing the innermost lexical
154*ef5ccd6cSJohn Marino    block containing the specified pc value and section, or 0 if there
155*ef5ccd6cSJohn Marino    is none.  PBLOCK is a pointer to the block.  If PBLOCK is NULL, we
156*ef5ccd6cSJohn Marino    don't pass this information back to the caller.  */
157*ef5ccd6cSJohn Marino 
158*ef5ccd6cSJohn Marino struct blockvector *
blockvector_for_pc_sect(CORE_ADDR pc,struct obj_section * section,struct block ** pblock,struct symtab * symtab)159*ef5ccd6cSJohn Marino blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
160*ef5ccd6cSJohn Marino 			 struct block **pblock, struct symtab *symtab)
1615796c8dcSSimon Schubert {
162*ef5ccd6cSJohn Marino   struct blockvector *bl;
163*ef5ccd6cSJohn Marino   struct block *b;
164*ef5ccd6cSJohn Marino 
165*ef5ccd6cSJohn Marino   if (symtab == 0)		/* if no symtab specified by caller */
166*ef5ccd6cSJohn Marino     {
167*ef5ccd6cSJohn Marino       /* First search all symtabs for one whose file contains our pc */
168*ef5ccd6cSJohn Marino       symtab = find_pc_sect_symtab (pc, section);
169*ef5ccd6cSJohn Marino       if (symtab == 0)
170*ef5ccd6cSJohn Marino 	return 0;
171*ef5ccd6cSJohn Marino     }
172*ef5ccd6cSJohn Marino 
173*ef5ccd6cSJohn Marino   bl = BLOCKVECTOR (symtab);
174*ef5ccd6cSJohn Marino 
175*ef5ccd6cSJohn Marino   /* Then search that symtab for the smallest block that wins.  */
176*ef5ccd6cSJohn Marino   b = find_block_in_blockvector (bl, pc);
177*ef5ccd6cSJohn Marino   if (b == NULL)
178*ef5ccd6cSJohn Marino     return NULL;
179*ef5ccd6cSJohn Marino 
1805796c8dcSSimon Schubert   if (pblock)
1815796c8dcSSimon Schubert     *pblock = b;
1825796c8dcSSimon Schubert   return bl;
1835796c8dcSSimon Schubert }
184*ef5ccd6cSJohn Marino 
185*ef5ccd6cSJohn Marino /* Return true if the blockvector BV contains PC, false otherwise.  */
186*ef5ccd6cSJohn Marino 
187*ef5ccd6cSJohn Marino int
blockvector_contains_pc(struct blockvector * bv,CORE_ADDR pc)188*ef5ccd6cSJohn Marino blockvector_contains_pc (struct blockvector *bv, CORE_ADDR pc)
189*ef5ccd6cSJohn Marino {
190*ef5ccd6cSJohn Marino   return find_block_in_blockvector (bv, pc) != NULL;
1915796c8dcSSimon Schubert }
1925796c8dcSSimon Schubert 
193a45ae5f8SJohn Marino /* Return call_site for specified PC in GDBARCH.  PC must match exactly, it
194a45ae5f8SJohn Marino    must be the next instruction after call (or after tail call jump).  Throw
195a45ae5f8SJohn Marino    NO_ENTRY_VALUE_ERROR otherwise.  This function never returns NULL.  */
196a45ae5f8SJohn Marino 
197a45ae5f8SJohn Marino struct call_site *
call_site_for_pc(struct gdbarch * gdbarch,CORE_ADDR pc)198a45ae5f8SJohn Marino call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc)
199a45ae5f8SJohn Marino {
200a45ae5f8SJohn Marino   struct symtab *symtab;
201a45ae5f8SJohn Marino   void **slot = NULL;
202a45ae5f8SJohn Marino 
203a45ae5f8SJohn Marino   /* -1 as tail call PC can be already after the compilation unit range.  */
204a45ae5f8SJohn Marino   symtab = find_pc_symtab (pc - 1);
205a45ae5f8SJohn Marino 
206a45ae5f8SJohn Marino   if (symtab != NULL && symtab->call_site_htab != NULL)
207a45ae5f8SJohn Marino     slot = htab_find_slot (symtab->call_site_htab, &pc, NO_INSERT);
208a45ae5f8SJohn Marino 
209a45ae5f8SJohn Marino   if (slot == NULL)
210a45ae5f8SJohn Marino     {
211a45ae5f8SJohn Marino       struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (pc);
212a45ae5f8SJohn Marino 
213a45ae5f8SJohn Marino       /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
214a45ae5f8SJohn Marino 	 the call target.  */
215a45ae5f8SJohn Marino       throw_error (NO_ENTRY_VALUE_ERROR,
216a45ae5f8SJohn Marino 		   _("DW_OP_GNU_entry_value resolving cannot find "
217a45ae5f8SJohn Marino 		     "DW_TAG_GNU_call_site %s in %s"),
218a45ae5f8SJohn Marino 		   paddress (gdbarch, pc),
219a45ae5f8SJohn Marino 		   msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
220a45ae5f8SJohn Marino     }
221a45ae5f8SJohn Marino 
222a45ae5f8SJohn Marino   return *slot;
223a45ae5f8SJohn Marino }
224a45ae5f8SJohn Marino 
2255796c8dcSSimon Schubert /* Return the blockvector immediately containing the innermost lexical block
2265796c8dcSSimon Schubert    containing the specified pc value, or 0 if there is none.
2275796c8dcSSimon Schubert    Backward compatibility, no section.  */
2285796c8dcSSimon Schubert 
2295796c8dcSSimon Schubert struct blockvector *
blockvector_for_pc(CORE_ADDR pc,struct block ** pblock)2305796c8dcSSimon Schubert blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
2315796c8dcSSimon Schubert {
2325796c8dcSSimon Schubert   return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
2335796c8dcSSimon Schubert 				  pblock, NULL);
2345796c8dcSSimon Schubert }
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert /* Return the innermost lexical block containing the specified pc value
2375796c8dcSSimon Schubert    in the specified section, or 0 if there is none.  */
2385796c8dcSSimon Schubert 
2395796c8dcSSimon Schubert struct block *
block_for_pc_sect(CORE_ADDR pc,struct obj_section * section)2405796c8dcSSimon Schubert block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
2415796c8dcSSimon Schubert {
2425796c8dcSSimon Schubert   struct blockvector *bl;
2435796c8dcSSimon Schubert   struct block *b;
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert   bl = blockvector_for_pc_sect (pc, section, &b, NULL);
2465796c8dcSSimon Schubert   if (bl)
2475796c8dcSSimon Schubert     return b;
2485796c8dcSSimon Schubert   return 0;
2495796c8dcSSimon Schubert }
2505796c8dcSSimon Schubert 
2515796c8dcSSimon Schubert /* Return the innermost lexical block containing the specified pc value,
2525796c8dcSSimon Schubert    or 0 if there is none.  Backward compatibility, no section.  */
2535796c8dcSSimon Schubert 
2545796c8dcSSimon Schubert struct block *
block_for_pc(CORE_ADDR pc)2555796c8dcSSimon Schubert block_for_pc (CORE_ADDR pc)
2565796c8dcSSimon Schubert {
2575796c8dcSSimon Schubert   return block_for_pc_sect (pc, find_pc_mapped_section (pc));
2585796c8dcSSimon Schubert }
2595796c8dcSSimon Schubert 
2605796c8dcSSimon Schubert /* Now come some functions designed to deal with C++ namespace issues.
2615796c8dcSSimon Schubert    The accessors are safe to use even in the non-C++ case.  */
2625796c8dcSSimon Schubert 
2635796c8dcSSimon Schubert /* This returns the namespace that BLOCK is enclosed in, or "" if it
2645796c8dcSSimon Schubert    isn't enclosed in a namespace at all.  This travels the chain of
2655796c8dcSSimon Schubert    superblocks looking for a scope, if necessary.  */
2665796c8dcSSimon Schubert 
2675796c8dcSSimon Schubert const char *
block_scope(const struct block * block)2685796c8dcSSimon Schubert block_scope (const struct block *block)
2695796c8dcSSimon Schubert {
2705796c8dcSSimon Schubert   for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
2715796c8dcSSimon Schubert     {
2725796c8dcSSimon Schubert       if (BLOCK_NAMESPACE (block) != NULL
2735796c8dcSSimon Schubert 	  && BLOCK_NAMESPACE (block)->scope != NULL)
2745796c8dcSSimon Schubert 	return BLOCK_NAMESPACE (block)->scope;
2755796c8dcSSimon Schubert     }
2765796c8dcSSimon Schubert 
2775796c8dcSSimon Schubert   return "";
2785796c8dcSSimon Schubert }
2795796c8dcSSimon Schubert 
2805796c8dcSSimon Schubert /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
2815796c8dcSSimon Schubert    OBSTACK.  (It won't make a copy of SCOPE, however, so that already
2825796c8dcSSimon Schubert    has to be allocated correctly.)  */
2835796c8dcSSimon Schubert 
2845796c8dcSSimon Schubert void
block_set_scope(struct block * block,const char * scope,struct obstack * obstack)2855796c8dcSSimon Schubert block_set_scope (struct block *block, const char *scope,
2865796c8dcSSimon Schubert 		 struct obstack *obstack)
2875796c8dcSSimon Schubert {
2885796c8dcSSimon Schubert   block_initialize_namespace (block, obstack);
2895796c8dcSSimon Schubert 
2905796c8dcSSimon Schubert   BLOCK_NAMESPACE (block)->scope = scope;
2915796c8dcSSimon Schubert }
2925796c8dcSSimon Schubert 
2935796c8dcSSimon Schubert /* This returns the using directives list associated with BLOCK, if
2945796c8dcSSimon Schubert    any.  */
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert struct using_direct *
block_using(const struct block * block)2975796c8dcSSimon Schubert block_using (const struct block *block)
2985796c8dcSSimon Schubert {
2995796c8dcSSimon Schubert   if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
3005796c8dcSSimon Schubert     return NULL;
3015796c8dcSSimon Schubert   else
3025796c8dcSSimon Schubert     return BLOCK_NAMESPACE (block)->using;
3035796c8dcSSimon Schubert }
3045796c8dcSSimon Schubert 
3055796c8dcSSimon Schubert /* Set BLOCK's using member to USING; if needed, allocate memory via
3065796c8dcSSimon Schubert    OBSTACK.  (It won't make a copy of USING, however, so that already
3075796c8dcSSimon Schubert    has to be allocated correctly.)  */
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert void
block_set_using(struct block * block,struct using_direct * using,struct obstack * obstack)3105796c8dcSSimon Schubert block_set_using (struct block *block,
3115796c8dcSSimon Schubert 		 struct using_direct *using,
3125796c8dcSSimon Schubert 		 struct obstack *obstack)
3135796c8dcSSimon Schubert {
3145796c8dcSSimon Schubert   block_initialize_namespace (block, obstack);
3155796c8dcSSimon Schubert 
3165796c8dcSSimon Schubert   BLOCK_NAMESPACE (block)->using = using;
3175796c8dcSSimon Schubert }
3185796c8dcSSimon Schubert 
3195796c8dcSSimon Schubert /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
3205796c8dcSSimon Schubert    ititialize its members to zero.  */
3215796c8dcSSimon Schubert 
3225796c8dcSSimon Schubert static void
block_initialize_namespace(struct block * block,struct obstack * obstack)3235796c8dcSSimon Schubert block_initialize_namespace (struct block *block, struct obstack *obstack)
3245796c8dcSSimon Schubert {
3255796c8dcSSimon Schubert   if (BLOCK_NAMESPACE (block) == NULL)
3265796c8dcSSimon Schubert     {
3275796c8dcSSimon Schubert       BLOCK_NAMESPACE (block)
3285796c8dcSSimon Schubert 	= obstack_alloc (obstack, sizeof (struct block_namespace_info));
3295796c8dcSSimon Schubert       BLOCK_NAMESPACE (block)->scope = NULL;
3305796c8dcSSimon Schubert       BLOCK_NAMESPACE (block)->using = NULL;
3315796c8dcSSimon Schubert     }
3325796c8dcSSimon Schubert }
3335796c8dcSSimon Schubert 
3345796c8dcSSimon Schubert /* Return the static block associated to BLOCK.  Return NULL if block
3355796c8dcSSimon Schubert    is NULL or if block is a global block.  */
3365796c8dcSSimon Schubert 
3375796c8dcSSimon Schubert const struct block *
block_static_block(const struct block * block)3385796c8dcSSimon Schubert block_static_block (const struct block *block)
3395796c8dcSSimon Schubert {
3405796c8dcSSimon Schubert   if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
3415796c8dcSSimon Schubert     return NULL;
3425796c8dcSSimon Schubert 
3435796c8dcSSimon Schubert   while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
3445796c8dcSSimon Schubert     block = BLOCK_SUPERBLOCK (block);
3455796c8dcSSimon Schubert 
3465796c8dcSSimon Schubert   return block;
3475796c8dcSSimon Schubert }
3485796c8dcSSimon Schubert 
3495796c8dcSSimon Schubert /* Return the static block associated to BLOCK.  Return NULL if block
3505796c8dcSSimon Schubert    is NULL.  */
3515796c8dcSSimon Schubert 
3525796c8dcSSimon Schubert const struct block *
block_global_block(const struct block * block)3535796c8dcSSimon Schubert block_global_block (const struct block *block)
3545796c8dcSSimon Schubert {
3555796c8dcSSimon Schubert   if (block == NULL)
3565796c8dcSSimon Schubert     return NULL;
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert   while (BLOCK_SUPERBLOCK (block) != NULL)
3595796c8dcSSimon Schubert     block = BLOCK_SUPERBLOCK (block);
3605796c8dcSSimon Schubert 
3615796c8dcSSimon Schubert   return block;
3625796c8dcSSimon Schubert }
3635796c8dcSSimon Schubert 
3645796c8dcSSimon Schubert /* Allocate a block on OBSTACK, and initialize its elements to
3655796c8dcSSimon Schubert    zero/NULL.  This is useful for creating "dummy" blocks that don't
3665796c8dcSSimon Schubert    correspond to actual source files.
3675796c8dcSSimon Schubert 
3685796c8dcSSimon Schubert    Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
3695796c8dcSSimon Schubert    valid value.  If you really don't want the block to have a
3705796c8dcSSimon Schubert    dictionary, then you should subsequently set its BLOCK_DICT to
3715796c8dcSSimon Schubert    dict_create_linear (obstack, NULL).  */
3725796c8dcSSimon Schubert 
3735796c8dcSSimon Schubert struct block *
allocate_block(struct obstack * obstack)3745796c8dcSSimon Schubert allocate_block (struct obstack *obstack)
3755796c8dcSSimon Schubert {
3765796c8dcSSimon Schubert   struct block *bl = obstack_alloc (obstack, sizeof (struct block));
3775796c8dcSSimon Schubert 
3785796c8dcSSimon Schubert   BLOCK_START (bl) = 0;
3795796c8dcSSimon Schubert   BLOCK_END (bl) = 0;
3805796c8dcSSimon Schubert   BLOCK_FUNCTION (bl) = NULL;
3815796c8dcSSimon Schubert   BLOCK_SUPERBLOCK (bl) = NULL;
3825796c8dcSSimon Schubert   BLOCK_DICT (bl) = NULL;
3835796c8dcSSimon Schubert   BLOCK_NAMESPACE (bl) = NULL;
3845796c8dcSSimon Schubert 
3855796c8dcSSimon Schubert   return bl;
3865796c8dcSSimon Schubert }
387*ef5ccd6cSJohn Marino 
388*ef5ccd6cSJohn Marino /* Allocate a global block.  */
389*ef5ccd6cSJohn Marino 
390*ef5ccd6cSJohn Marino struct block *
allocate_global_block(struct obstack * obstack)391*ef5ccd6cSJohn Marino allocate_global_block (struct obstack *obstack)
392*ef5ccd6cSJohn Marino {
393*ef5ccd6cSJohn Marino   struct global_block *bl = OBSTACK_ZALLOC (obstack, struct global_block);
394*ef5ccd6cSJohn Marino 
395*ef5ccd6cSJohn Marino   return &bl->block;
396*ef5ccd6cSJohn Marino }
397*ef5ccd6cSJohn Marino 
398*ef5ccd6cSJohn Marino /* Set the symtab of the global block.  */
399*ef5ccd6cSJohn Marino 
400*ef5ccd6cSJohn Marino void
set_block_symtab(struct block * block,struct symtab * symtab)401*ef5ccd6cSJohn Marino set_block_symtab (struct block *block, struct symtab *symtab)
402*ef5ccd6cSJohn Marino {
403*ef5ccd6cSJohn Marino   struct global_block *gb;
404*ef5ccd6cSJohn Marino 
405*ef5ccd6cSJohn Marino   gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
406*ef5ccd6cSJohn Marino   gb = (struct global_block *) block;
407*ef5ccd6cSJohn Marino   gdb_assert (gb->symtab == NULL);
408*ef5ccd6cSJohn Marino   gb->symtab = symtab;
409*ef5ccd6cSJohn Marino }
410*ef5ccd6cSJohn Marino 
411*ef5ccd6cSJohn Marino /* Return the symtab of the global block.  */
412*ef5ccd6cSJohn Marino 
413*ef5ccd6cSJohn Marino static struct symtab *
get_block_symtab(const struct block * block)414*ef5ccd6cSJohn Marino get_block_symtab (const struct block *block)
415*ef5ccd6cSJohn Marino {
416*ef5ccd6cSJohn Marino   struct global_block *gb;
417*ef5ccd6cSJohn Marino 
418*ef5ccd6cSJohn Marino   gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
419*ef5ccd6cSJohn Marino   gb = (struct global_block *) block;
420*ef5ccd6cSJohn Marino   gdb_assert (gb->symtab != NULL);
421*ef5ccd6cSJohn Marino   return gb->symtab;
422*ef5ccd6cSJohn Marino }
423*ef5ccd6cSJohn Marino 
424*ef5ccd6cSJohn Marino 
425*ef5ccd6cSJohn Marino 
426*ef5ccd6cSJohn Marino /* Initialize a block iterator, either to iterate over a single block,
427*ef5ccd6cSJohn Marino    or, for static and global blocks, all the included symtabs as
428*ef5ccd6cSJohn Marino    well.  */
429*ef5ccd6cSJohn Marino 
430*ef5ccd6cSJohn Marino static void
initialize_block_iterator(const struct block * block,struct block_iterator * iter)431*ef5ccd6cSJohn Marino initialize_block_iterator (const struct block *block,
432*ef5ccd6cSJohn Marino 			   struct block_iterator *iter)
433*ef5ccd6cSJohn Marino {
434*ef5ccd6cSJohn Marino   enum block_enum which;
435*ef5ccd6cSJohn Marino   struct symtab *symtab;
436*ef5ccd6cSJohn Marino 
437*ef5ccd6cSJohn Marino   iter->idx = -1;
438*ef5ccd6cSJohn Marino 
439*ef5ccd6cSJohn Marino   if (BLOCK_SUPERBLOCK (block) == NULL)
440*ef5ccd6cSJohn Marino     {
441*ef5ccd6cSJohn Marino       which = GLOBAL_BLOCK;
442*ef5ccd6cSJohn Marino       symtab = get_block_symtab (block);
443*ef5ccd6cSJohn Marino     }
444*ef5ccd6cSJohn Marino   else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL)
445*ef5ccd6cSJohn Marino     {
446*ef5ccd6cSJohn Marino       which = STATIC_BLOCK;
447*ef5ccd6cSJohn Marino       symtab = get_block_symtab (BLOCK_SUPERBLOCK (block));
448*ef5ccd6cSJohn Marino     }
449*ef5ccd6cSJohn Marino   else
450*ef5ccd6cSJohn Marino     {
451*ef5ccd6cSJohn Marino       iter->d.block = block;
452*ef5ccd6cSJohn Marino       /* A signal value meaning that we're iterating over a single
453*ef5ccd6cSJohn Marino 	 block.  */
454*ef5ccd6cSJohn Marino       iter->which = FIRST_LOCAL_BLOCK;
455*ef5ccd6cSJohn Marino       return;
456*ef5ccd6cSJohn Marino     }
457*ef5ccd6cSJohn Marino 
458*ef5ccd6cSJohn Marino   /* If this is an included symtab, find the canonical includer and
459*ef5ccd6cSJohn Marino      use it instead.  */
460*ef5ccd6cSJohn Marino   while (symtab->user != NULL)
461*ef5ccd6cSJohn Marino     symtab = symtab->user;
462*ef5ccd6cSJohn Marino 
463*ef5ccd6cSJohn Marino   /* Putting this check here simplifies the logic of the iterator
464*ef5ccd6cSJohn Marino      functions.  If there are no included symtabs, we only need to
465*ef5ccd6cSJohn Marino      search a single block, so we might as well just do that
466*ef5ccd6cSJohn Marino      directly.  */
467*ef5ccd6cSJohn Marino   if (symtab->includes == NULL)
468*ef5ccd6cSJohn Marino     {
469*ef5ccd6cSJohn Marino       iter->d.block = block;
470*ef5ccd6cSJohn Marino       /* A signal value meaning that we're iterating over a single
471*ef5ccd6cSJohn Marino 	 block.  */
472*ef5ccd6cSJohn Marino       iter->which = FIRST_LOCAL_BLOCK;
473*ef5ccd6cSJohn Marino     }
474*ef5ccd6cSJohn Marino   else
475*ef5ccd6cSJohn Marino     {
476*ef5ccd6cSJohn Marino       iter->d.symtab = symtab;
477*ef5ccd6cSJohn Marino       iter->which = which;
478*ef5ccd6cSJohn Marino     }
479*ef5ccd6cSJohn Marino }
480*ef5ccd6cSJohn Marino 
481*ef5ccd6cSJohn Marino /* A helper function that finds the current symtab over whose static
482*ef5ccd6cSJohn Marino    or global block we should iterate.  */
483*ef5ccd6cSJohn Marino 
484*ef5ccd6cSJohn Marino static struct symtab *
find_iterator_symtab(struct block_iterator * iterator)485*ef5ccd6cSJohn Marino find_iterator_symtab (struct block_iterator *iterator)
486*ef5ccd6cSJohn Marino {
487*ef5ccd6cSJohn Marino   if (iterator->idx == -1)
488*ef5ccd6cSJohn Marino     return iterator->d.symtab;
489*ef5ccd6cSJohn Marino   return iterator->d.symtab->includes[iterator->idx];
490*ef5ccd6cSJohn Marino }
491*ef5ccd6cSJohn Marino 
492*ef5ccd6cSJohn Marino /* Perform a single step for a plain block iterator, iterating across
493*ef5ccd6cSJohn Marino    symbol tables as needed.  Returns the next symbol, or NULL when
494*ef5ccd6cSJohn Marino    iteration is complete.  */
495*ef5ccd6cSJohn Marino 
496*ef5ccd6cSJohn Marino static struct symbol *
block_iterator_step(struct block_iterator * iterator,int first)497*ef5ccd6cSJohn Marino block_iterator_step (struct block_iterator *iterator, int first)
498*ef5ccd6cSJohn Marino {
499*ef5ccd6cSJohn Marino   struct symbol *sym;
500*ef5ccd6cSJohn Marino 
501*ef5ccd6cSJohn Marino   gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
502*ef5ccd6cSJohn Marino 
503*ef5ccd6cSJohn Marino   while (1)
504*ef5ccd6cSJohn Marino     {
505*ef5ccd6cSJohn Marino       if (first)
506*ef5ccd6cSJohn Marino 	{
507*ef5ccd6cSJohn Marino 	  struct symtab *symtab = find_iterator_symtab (iterator);
508*ef5ccd6cSJohn Marino 	  const struct block *block;
509*ef5ccd6cSJohn Marino 
510*ef5ccd6cSJohn Marino 	  /* Iteration is complete.  */
511*ef5ccd6cSJohn Marino 	  if (symtab == NULL)
512*ef5ccd6cSJohn Marino 	    return  NULL;
513*ef5ccd6cSJohn Marino 
514*ef5ccd6cSJohn Marino 	  block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
515*ef5ccd6cSJohn Marino 	  sym = dict_iterator_first (BLOCK_DICT (block), &iterator->dict_iter);
516*ef5ccd6cSJohn Marino 	}
517*ef5ccd6cSJohn Marino       else
518*ef5ccd6cSJohn Marino 	sym = dict_iterator_next (&iterator->dict_iter);
519*ef5ccd6cSJohn Marino 
520*ef5ccd6cSJohn Marino       if (sym != NULL)
521*ef5ccd6cSJohn Marino 	return sym;
522*ef5ccd6cSJohn Marino 
523*ef5ccd6cSJohn Marino       /* We have finished iterating the appropriate block of one
524*ef5ccd6cSJohn Marino 	 symtab.  Now advance to the next symtab and begin iteration
525*ef5ccd6cSJohn Marino 	 there.  */
526*ef5ccd6cSJohn Marino       ++iterator->idx;
527*ef5ccd6cSJohn Marino       first = 1;
528*ef5ccd6cSJohn Marino     }
529*ef5ccd6cSJohn Marino }
530*ef5ccd6cSJohn Marino 
531*ef5ccd6cSJohn Marino /* See block.h.  */
532*ef5ccd6cSJohn Marino 
533*ef5ccd6cSJohn Marino struct symbol *
block_iterator_first(const struct block * block,struct block_iterator * iterator)534*ef5ccd6cSJohn Marino block_iterator_first (const struct block *block,
535*ef5ccd6cSJohn Marino 		      struct block_iterator *iterator)
536*ef5ccd6cSJohn Marino {
537*ef5ccd6cSJohn Marino   initialize_block_iterator (block, iterator);
538*ef5ccd6cSJohn Marino 
539*ef5ccd6cSJohn Marino   if (iterator->which == FIRST_LOCAL_BLOCK)
540*ef5ccd6cSJohn Marino     return dict_iterator_first (block->dict, &iterator->dict_iter);
541*ef5ccd6cSJohn Marino 
542*ef5ccd6cSJohn Marino   return block_iterator_step (iterator, 1);
543*ef5ccd6cSJohn Marino }
544*ef5ccd6cSJohn Marino 
545*ef5ccd6cSJohn Marino /* See block.h.  */
546*ef5ccd6cSJohn Marino 
547*ef5ccd6cSJohn Marino struct symbol *
block_iterator_next(struct block_iterator * iterator)548*ef5ccd6cSJohn Marino block_iterator_next (struct block_iterator *iterator)
549*ef5ccd6cSJohn Marino {
550*ef5ccd6cSJohn Marino   if (iterator->which == FIRST_LOCAL_BLOCK)
551*ef5ccd6cSJohn Marino     return dict_iterator_next (&iterator->dict_iter);
552*ef5ccd6cSJohn Marino 
553*ef5ccd6cSJohn Marino   return block_iterator_step (iterator, 0);
554*ef5ccd6cSJohn Marino }
555*ef5ccd6cSJohn Marino 
556*ef5ccd6cSJohn Marino /* Perform a single step for a "name" block iterator, iterating across
557*ef5ccd6cSJohn Marino    symbol tables as needed.  Returns the next symbol, or NULL when
558*ef5ccd6cSJohn Marino    iteration is complete.  */
559*ef5ccd6cSJohn Marino 
560*ef5ccd6cSJohn Marino static struct symbol *
block_iter_name_step(struct block_iterator * iterator,const char * name,int first)561*ef5ccd6cSJohn Marino block_iter_name_step (struct block_iterator *iterator, const char *name,
562*ef5ccd6cSJohn Marino 		      int first)
563*ef5ccd6cSJohn Marino {
564*ef5ccd6cSJohn Marino   struct symbol *sym;
565*ef5ccd6cSJohn Marino 
566*ef5ccd6cSJohn Marino   gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
567*ef5ccd6cSJohn Marino 
568*ef5ccd6cSJohn Marino   while (1)
569*ef5ccd6cSJohn Marino     {
570*ef5ccd6cSJohn Marino       if (first)
571*ef5ccd6cSJohn Marino 	{
572*ef5ccd6cSJohn Marino 	  struct symtab *symtab = find_iterator_symtab (iterator);
573*ef5ccd6cSJohn Marino 	  const struct block *block;
574*ef5ccd6cSJohn Marino 
575*ef5ccd6cSJohn Marino 	  /* Iteration is complete.  */
576*ef5ccd6cSJohn Marino 	  if (symtab == NULL)
577*ef5ccd6cSJohn Marino 	    return  NULL;
578*ef5ccd6cSJohn Marino 
579*ef5ccd6cSJohn Marino 	  block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
580*ef5ccd6cSJohn Marino 	  sym = dict_iter_name_first (BLOCK_DICT (block), name,
581*ef5ccd6cSJohn Marino 				      &iterator->dict_iter);
582*ef5ccd6cSJohn Marino 	}
583*ef5ccd6cSJohn Marino       else
584*ef5ccd6cSJohn Marino 	sym = dict_iter_name_next (name, &iterator->dict_iter);
585*ef5ccd6cSJohn Marino 
586*ef5ccd6cSJohn Marino       if (sym != NULL)
587*ef5ccd6cSJohn Marino 	return sym;
588*ef5ccd6cSJohn Marino 
589*ef5ccd6cSJohn Marino       /* We have finished iterating the appropriate block of one
590*ef5ccd6cSJohn Marino 	 symtab.  Now advance to the next symtab and begin iteration
591*ef5ccd6cSJohn Marino 	 there.  */
592*ef5ccd6cSJohn Marino       ++iterator->idx;
593*ef5ccd6cSJohn Marino       first = 1;
594*ef5ccd6cSJohn Marino     }
595*ef5ccd6cSJohn Marino }
596*ef5ccd6cSJohn Marino 
597*ef5ccd6cSJohn Marino /* See block.h.  */
598*ef5ccd6cSJohn Marino 
599*ef5ccd6cSJohn Marino struct symbol *
block_iter_name_first(const struct block * block,const char * name,struct block_iterator * iterator)600*ef5ccd6cSJohn Marino block_iter_name_first (const struct block *block,
601*ef5ccd6cSJohn Marino 		       const char *name,
602*ef5ccd6cSJohn Marino 		       struct block_iterator *iterator)
603*ef5ccd6cSJohn Marino {
604*ef5ccd6cSJohn Marino   initialize_block_iterator (block, iterator);
605*ef5ccd6cSJohn Marino 
606*ef5ccd6cSJohn Marino   if (iterator->which == FIRST_LOCAL_BLOCK)
607*ef5ccd6cSJohn Marino     return dict_iter_name_first (block->dict, name, &iterator->dict_iter);
608*ef5ccd6cSJohn Marino 
609*ef5ccd6cSJohn Marino   return block_iter_name_step (iterator, name, 1);
610*ef5ccd6cSJohn Marino }
611*ef5ccd6cSJohn Marino 
612*ef5ccd6cSJohn Marino /* See block.h.  */
613*ef5ccd6cSJohn Marino 
614*ef5ccd6cSJohn Marino struct symbol *
block_iter_name_next(const char * name,struct block_iterator * iterator)615*ef5ccd6cSJohn Marino block_iter_name_next (const char *name, struct block_iterator *iterator)
616*ef5ccd6cSJohn Marino {
617*ef5ccd6cSJohn Marino   if (iterator->which == FIRST_LOCAL_BLOCK)
618*ef5ccd6cSJohn Marino     return dict_iter_name_next (name, &iterator->dict_iter);
619*ef5ccd6cSJohn Marino 
620*ef5ccd6cSJohn Marino   return block_iter_name_step (iterator, name, 0);
621*ef5ccd6cSJohn Marino }
622*ef5ccd6cSJohn Marino 
623*ef5ccd6cSJohn Marino /* Perform a single step for a "match" block iterator, iterating
624*ef5ccd6cSJohn Marino    across symbol tables as needed.  Returns the next symbol, or NULL
625*ef5ccd6cSJohn Marino    when iteration is complete.  */
626*ef5ccd6cSJohn Marino 
627*ef5ccd6cSJohn Marino static struct symbol *
block_iter_match_step(struct block_iterator * iterator,const char * name,symbol_compare_ftype * compare,int first)628*ef5ccd6cSJohn Marino block_iter_match_step (struct block_iterator *iterator,
629*ef5ccd6cSJohn Marino 		       const char *name,
630*ef5ccd6cSJohn Marino 		       symbol_compare_ftype *compare,
631*ef5ccd6cSJohn Marino 		       int first)
632*ef5ccd6cSJohn Marino {
633*ef5ccd6cSJohn Marino   struct symbol *sym;
634*ef5ccd6cSJohn Marino 
635*ef5ccd6cSJohn Marino   gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
636*ef5ccd6cSJohn Marino 
637*ef5ccd6cSJohn Marino   while (1)
638*ef5ccd6cSJohn Marino     {
639*ef5ccd6cSJohn Marino       if (first)
640*ef5ccd6cSJohn Marino 	{
641*ef5ccd6cSJohn Marino 	  struct symtab *symtab = find_iterator_symtab (iterator);
642*ef5ccd6cSJohn Marino 	  const struct block *block;
643*ef5ccd6cSJohn Marino 
644*ef5ccd6cSJohn Marino 	  /* Iteration is complete.  */
645*ef5ccd6cSJohn Marino 	  if (symtab == NULL)
646*ef5ccd6cSJohn Marino 	    return  NULL;
647*ef5ccd6cSJohn Marino 
648*ef5ccd6cSJohn Marino 	  block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
649*ef5ccd6cSJohn Marino 	  sym = dict_iter_match_first (BLOCK_DICT (block), name,
650*ef5ccd6cSJohn Marino 				       compare, &iterator->dict_iter);
651*ef5ccd6cSJohn Marino 	}
652*ef5ccd6cSJohn Marino       else
653*ef5ccd6cSJohn Marino 	sym = dict_iter_match_next (name, compare, &iterator->dict_iter);
654*ef5ccd6cSJohn Marino 
655*ef5ccd6cSJohn Marino       if (sym != NULL)
656*ef5ccd6cSJohn Marino 	return sym;
657*ef5ccd6cSJohn Marino 
658*ef5ccd6cSJohn Marino       /* We have finished iterating the appropriate block of one
659*ef5ccd6cSJohn Marino 	 symtab.  Now advance to the next symtab and begin iteration
660*ef5ccd6cSJohn Marino 	 there.  */
661*ef5ccd6cSJohn Marino       ++iterator->idx;
662*ef5ccd6cSJohn Marino       first = 1;
663*ef5ccd6cSJohn Marino     }
664*ef5ccd6cSJohn Marino }
665*ef5ccd6cSJohn Marino 
666*ef5ccd6cSJohn Marino /* See block.h.  */
667*ef5ccd6cSJohn Marino 
668*ef5ccd6cSJohn Marino struct symbol *
block_iter_match_first(const struct block * block,const char * name,symbol_compare_ftype * compare,struct block_iterator * iterator)669*ef5ccd6cSJohn Marino block_iter_match_first (const struct block *block,
670*ef5ccd6cSJohn Marino 			const char *name,
671*ef5ccd6cSJohn Marino 			symbol_compare_ftype *compare,
672*ef5ccd6cSJohn Marino 			struct block_iterator *iterator)
673*ef5ccd6cSJohn Marino {
674*ef5ccd6cSJohn Marino   initialize_block_iterator (block, iterator);
675*ef5ccd6cSJohn Marino 
676*ef5ccd6cSJohn Marino   if (iterator->which == FIRST_LOCAL_BLOCK)
677*ef5ccd6cSJohn Marino     return dict_iter_match_first (block->dict, name, compare,
678*ef5ccd6cSJohn Marino 				  &iterator->dict_iter);
679*ef5ccd6cSJohn Marino 
680*ef5ccd6cSJohn Marino   return block_iter_match_step (iterator, name, compare, 1);
681*ef5ccd6cSJohn Marino }
682*ef5ccd6cSJohn Marino 
683*ef5ccd6cSJohn Marino /* See block.h.  */
684*ef5ccd6cSJohn Marino 
685*ef5ccd6cSJohn Marino struct symbol *
block_iter_match_next(const char * name,symbol_compare_ftype * compare,struct block_iterator * iterator)686*ef5ccd6cSJohn Marino block_iter_match_next (const char *name,
687*ef5ccd6cSJohn Marino 		       symbol_compare_ftype *compare,
688*ef5ccd6cSJohn Marino 		       struct block_iterator *iterator)
689*ef5ccd6cSJohn Marino {
690*ef5ccd6cSJohn Marino   if (iterator->which == FIRST_LOCAL_BLOCK)
691*ef5ccd6cSJohn Marino     return dict_iter_match_next (name, compare, &iterator->dict_iter);
692*ef5ccd6cSJohn Marino 
693*ef5ccd6cSJohn Marino   return block_iter_match_step (iterator, name, compare, 0);
694*ef5ccd6cSJohn Marino }
695