xref: /openbsd-src/gnu/usr.bin/binutils/gdb/block.c (revision b725ae7711052a2233e31a66fefb8a752c388d7a)
1*b725ae77Skettenis /* Block-related functions for the GNU debugger, GDB.
2*b725ae77Skettenis 
3*b725ae77Skettenis    Copyright 2003 Free Software Foundation, Inc.
4*b725ae77Skettenis 
5*b725ae77Skettenis    This file is part of GDB.
6*b725ae77Skettenis 
7*b725ae77Skettenis    This program is free software; you can redistribute it and/or modify
8*b725ae77Skettenis    it under the terms of the GNU General Public License as published by
9*b725ae77Skettenis    the Free Software Foundation; either version 2 of the License, or
10*b725ae77Skettenis    (at your option) any later version.
11*b725ae77Skettenis 
12*b725ae77Skettenis    This program is distributed in the hope that it will be useful,
13*b725ae77Skettenis    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*b725ae77Skettenis    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*b725ae77Skettenis    GNU General Public License for more details.
16*b725ae77Skettenis 
17*b725ae77Skettenis    You should have received a copy of the GNU General Public License
18*b725ae77Skettenis    along with this program; if not, write to the Free Software
19*b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
20*b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
21*b725ae77Skettenis 
22*b725ae77Skettenis #include "defs.h"
23*b725ae77Skettenis #include "block.h"
24*b725ae77Skettenis #include "symtab.h"
25*b725ae77Skettenis #include "symfile.h"
26*b725ae77Skettenis #include "gdb_obstack.h"
27*b725ae77Skettenis #include "cp-support.h"
28*b725ae77Skettenis 
29*b725ae77Skettenis /* This is used by struct block to store namespace-related info for
30*b725ae77Skettenis    C++ files, namely using declarations and the current namespace in
31*b725ae77Skettenis    scope.  */
32*b725ae77Skettenis 
33*b725ae77Skettenis struct block_namespace_info
34*b725ae77Skettenis {
35*b725ae77Skettenis   const char *scope;
36*b725ae77Skettenis   struct using_direct *using;
37*b725ae77Skettenis };
38*b725ae77Skettenis 
39*b725ae77Skettenis static void block_initialize_namespace (struct block *block,
40*b725ae77Skettenis 					struct obstack *obstack);
41*b725ae77Skettenis 
42*b725ae77Skettenis /* Return Nonzero if block a is lexically nested within block b,
43*b725ae77Skettenis    or if a and b have the same pc range.
44*b725ae77Skettenis    Return zero otherwise. */
45*b725ae77Skettenis 
46*b725ae77Skettenis int
contained_in(const struct block * a,const struct block * b)47*b725ae77Skettenis contained_in (const struct block *a, const struct block *b)
48*b725ae77Skettenis {
49*b725ae77Skettenis   if (!a || !b)
50*b725ae77Skettenis     return 0;
51*b725ae77Skettenis   return BLOCK_START (a) >= BLOCK_START (b)
52*b725ae77Skettenis     && BLOCK_END (a) <= BLOCK_END (b);
53*b725ae77Skettenis }
54*b725ae77Skettenis 
55*b725ae77Skettenis 
56*b725ae77Skettenis /* Return the symbol for the function which contains a specified
57*b725ae77Skettenis    lexical block, described by a struct block BL.  */
58*b725ae77Skettenis 
59*b725ae77Skettenis struct symbol *
block_function(const struct block * bl)60*b725ae77Skettenis block_function (const struct block *bl)
61*b725ae77Skettenis {
62*b725ae77Skettenis   while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
63*b725ae77Skettenis     bl = BLOCK_SUPERBLOCK (bl);
64*b725ae77Skettenis 
65*b725ae77Skettenis   return BLOCK_FUNCTION (bl);
66*b725ae77Skettenis }
67*b725ae77Skettenis 
68*b725ae77Skettenis /* Return the blockvector immediately containing the innermost lexical block
69*b725ae77Skettenis    containing the specified pc value and section, or 0 if there is none.
70*b725ae77Skettenis    PINDEX is a pointer to the index value of the block.  If PINDEX
71*b725ae77Skettenis    is NULL, we don't pass this information back to the caller.  */
72*b725ae77Skettenis 
73*b725ae77Skettenis struct blockvector *
blockvector_for_pc_sect(CORE_ADDR pc,struct bfd_section * section,int * pindex,struct symtab * symtab)74*b725ae77Skettenis blockvector_for_pc_sect (CORE_ADDR pc, struct bfd_section *section,
75*b725ae77Skettenis 			 int *pindex, struct symtab *symtab)
76*b725ae77Skettenis {
77*b725ae77Skettenis   struct block *b;
78*b725ae77Skettenis   int bot, top, half;
79*b725ae77Skettenis   struct blockvector *bl;
80*b725ae77Skettenis 
81*b725ae77Skettenis   if (symtab == 0)		/* if no symtab specified by caller */
82*b725ae77Skettenis     {
83*b725ae77Skettenis       /* First search all symtabs for one whose file contains our pc */
84*b725ae77Skettenis       symtab = find_pc_sect_symtab (pc, section);
85*b725ae77Skettenis       if (symtab == 0)
86*b725ae77Skettenis 	return 0;
87*b725ae77Skettenis     }
88*b725ae77Skettenis 
89*b725ae77Skettenis   bl = BLOCKVECTOR (symtab);
90*b725ae77Skettenis   b = BLOCKVECTOR_BLOCK (bl, 0);
91*b725ae77Skettenis 
92*b725ae77Skettenis   /* Then search that symtab for the smallest block that wins.  */
93*b725ae77Skettenis   /* Use binary search to find the last block that starts before PC.  */
94*b725ae77Skettenis 
95*b725ae77Skettenis   bot = 0;
96*b725ae77Skettenis   top = BLOCKVECTOR_NBLOCKS (bl);
97*b725ae77Skettenis 
98*b725ae77Skettenis   while (top - bot > 1)
99*b725ae77Skettenis     {
100*b725ae77Skettenis       half = (top - bot + 1) >> 1;
101*b725ae77Skettenis       b = BLOCKVECTOR_BLOCK (bl, bot + half);
102*b725ae77Skettenis       if (BLOCK_START (b) <= pc)
103*b725ae77Skettenis 	bot += half;
104*b725ae77Skettenis       else
105*b725ae77Skettenis 	top = bot + half;
106*b725ae77Skettenis     }
107*b725ae77Skettenis 
108*b725ae77Skettenis   /* Now search backward for a block that ends after PC.  */
109*b725ae77Skettenis 
110*b725ae77Skettenis   while (bot >= 0)
111*b725ae77Skettenis     {
112*b725ae77Skettenis       b = BLOCKVECTOR_BLOCK (bl, bot);
113*b725ae77Skettenis       if (BLOCK_END (b) > pc)
114*b725ae77Skettenis 	{
115*b725ae77Skettenis 	  if (pindex)
116*b725ae77Skettenis 	    *pindex = bot;
117*b725ae77Skettenis 	  return bl;
118*b725ae77Skettenis 	}
119*b725ae77Skettenis       bot--;
120*b725ae77Skettenis     }
121*b725ae77Skettenis   return 0;
122*b725ae77Skettenis }
123*b725ae77Skettenis 
124*b725ae77Skettenis /* Return the blockvector immediately containing the innermost lexical block
125*b725ae77Skettenis    containing the specified pc value, or 0 if there is none.
126*b725ae77Skettenis    Backward compatibility, no section.  */
127*b725ae77Skettenis 
128*b725ae77Skettenis struct blockvector *
blockvector_for_pc(CORE_ADDR pc,int * pindex)129*b725ae77Skettenis blockvector_for_pc (CORE_ADDR pc, int *pindex)
130*b725ae77Skettenis {
131*b725ae77Skettenis   return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
132*b725ae77Skettenis 				  pindex, NULL);
133*b725ae77Skettenis }
134*b725ae77Skettenis 
135*b725ae77Skettenis /* Return the innermost lexical block containing the specified pc value
136*b725ae77Skettenis    in the specified section, or 0 if there is none.  */
137*b725ae77Skettenis 
138*b725ae77Skettenis struct block *
block_for_pc_sect(CORE_ADDR pc,struct bfd_section * section)139*b725ae77Skettenis block_for_pc_sect (CORE_ADDR pc, struct bfd_section *section)
140*b725ae77Skettenis {
141*b725ae77Skettenis   struct blockvector *bl;
142*b725ae77Skettenis   int index;
143*b725ae77Skettenis 
144*b725ae77Skettenis   bl = blockvector_for_pc_sect (pc, section, &index, NULL);
145*b725ae77Skettenis   if (bl)
146*b725ae77Skettenis     return BLOCKVECTOR_BLOCK (bl, index);
147*b725ae77Skettenis   return 0;
148*b725ae77Skettenis }
149*b725ae77Skettenis 
150*b725ae77Skettenis /* Return the innermost lexical block containing the specified pc value,
151*b725ae77Skettenis    or 0 if there is none.  Backward compatibility, no section.  */
152*b725ae77Skettenis 
153*b725ae77Skettenis struct block *
block_for_pc(CORE_ADDR pc)154*b725ae77Skettenis block_for_pc (CORE_ADDR pc)
155*b725ae77Skettenis {
156*b725ae77Skettenis   return block_for_pc_sect (pc, find_pc_mapped_section (pc));
157*b725ae77Skettenis }
158*b725ae77Skettenis 
159*b725ae77Skettenis /* Now come some functions designed to deal with C++ namespace issues.
160*b725ae77Skettenis    The accessors are safe to use even in the non-C++ case.  */
161*b725ae77Skettenis 
162*b725ae77Skettenis /* This returns the namespace that BLOCK is enclosed in, or "" if it
163*b725ae77Skettenis    isn't enclosed in a namespace at all.  This travels the chain of
164*b725ae77Skettenis    superblocks looking for a scope, if necessary.  */
165*b725ae77Skettenis 
166*b725ae77Skettenis const char *
block_scope(const struct block * block)167*b725ae77Skettenis block_scope (const struct block *block)
168*b725ae77Skettenis {
169*b725ae77Skettenis   for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
170*b725ae77Skettenis     {
171*b725ae77Skettenis       if (BLOCK_NAMESPACE (block) != NULL
172*b725ae77Skettenis 	  && BLOCK_NAMESPACE (block)->scope != NULL)
173*b725ae77Skettenis 	return BLOCK_NAMESPACE (block)->scope;
174*b725ae77Skettenis     }
175*b725ae77Skettenis 
176*b725ae77Skettenis   return "";
177*b725ae77Skettenis }
178*b725ae77Skettenis 
179*b725ae77Skettenis /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
180*b725ae77Skettenis    OBSTACK.  (It won't make a copy of SCOPE, however, so that already
181*b725ae77Skettenis    has to be allocated correctly.)  */
182*b725ae77Skettenis 
183*b725ae77Skettenis void
block_set_scope(struct block * block,const char * scope,struct obstack * obstack)184*b725ae77Skettenis block_set_scope (struct block *block, const char *scope,
185*b725ae77Skettenis 		 struct obstack *obstack)
186*b725ae77Skettenis {
187*b725ae77Skettenis   block_initialize_namespace (block, obstack);
188*b725ae77Skettenis 
189*b725ae77Skettenis   BLOCK_NAMESPACE (block)->scope = scope;
190*b725ae77Skettenis }
191*b725ae77Skettenis 
192*b725ae77Skettenis /* This returns the first using directives associated to BLOCK, if
193*b725ae77Skettenis    any.  */
194*b725ae77Skettenis 
195*b725ae77Skettenis /* FIXME: carlton/2003-04-23: This uses the fact that we currently
196*b725ae77Skettenis    only have using directives in static blocks, because we only
197*b725ae77Skettenis    generate using directives from anonymous namespaces.  Eventually,
198*b725ae77Skettenis    when we support using directives everywhere, we'll want to replace
199*b725ae77Skettenis    this by some iterator functions.  */
200*b725ae77Skettenis 
201*b725ae77Skettenis struct using_direct *
block_using(const struct block * block)202*b725ae77Skettenis block_using (const struct block *block)
203*b725ae77Skettenis {
204*b725ae77Skettenis   const struct block *static_block = block_static_block (block);
205*b725ae77Skettenis 
206*b725ae77Skettenis   if (static_block == NULL
207*b725ae77Skettenis       || BLOCK_NAMESPACE (static_block) == NULL)
208*b725ae77Skettenis     return NULL;
209*b725ae77Skettenis   else
210*b725ae77Skettenis     return BLOCK_NAMESPACE (static_block)->using;
211*b725ae77Skettenis }
212*b725ae77Skettenis 
213*b725ae77Skettenis /* Set BLOCK's using member to USING; if needed, allocate memory via
214*b725ae77Skettenis    OBSTACK.  (It won't make a copy of USING, however, so that already
215*b725ae77Skettenis    has to be allocated correctly.)  */
216*b725ae77Skettenis 
217*b725ae77Skettenis void
block_set_using(struct block * block,struct using_direct * using,struct obstack * obstack)218*b725ae77Skettenis block_set_using (struct block *block,
219*b725ae77Skettenis 		 struct using_direct *using,
220*b725ae77Skettenis 		 struct obstack *obstack)
221*b725ae77Skettenis {
222*b725ae77Skettenis   block_initialize_namespace (block, obstack);
223*b725ae77Skettenis 
224*b725ae77Skettenis   BLOCK_NAMESPACE (block)->using = using;
225*b725ae77Skettenis }
226*b725ae77Skettenis 
227*b725ae77Skettenis /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
228*b725ae77Skettenis    ititialize its members to zero.  */
229*b725ae77Skettenis 
230*b725ae77Skettenis static void
block_initialize_namespace(struct block * block,struct obstack * obstack)231*b725ae77Skettenis block_initialize_namespace (struct block *block, struct obstack *obstack)
232*b725ae77Skettenis {
233*b725ae77Skettenis   if (BLOCK_NAMESPACE (block) == NULL)
234*b725ae77Skettenis     {
235*b725ae77Skettenis       BLOCK_NAMESPACE (block)
236*b725ae77Skettenis 	= obstack_alloc (obstack, sizeof (struct block_namespace_info));
237*b725ae77Skettenis       BLOCK_NAMESPACE (block)->scope = NULL;
238*b725ae77Skettenis       BLOCK_NAMESPACE (block)->using = NULL;
239*b725ae77Skettenis     }
240*b725ae77Skettenis }
241*b725ae77Skettenis 
242*b725ae77Skettenis /* Return the static block associated to BLOCK.  Return NULL if block
243*b725ae77Skettenis    is NULL or if block is a global block.  */
244*b725ae77Skettenis 
245*b725ae77Skettenis const struct block *
block_static_block(const struct block * block)246*b725ae77Skettenis block_static_block (const struct block *block)
247*b725ae77Skettenis {
248*b725ae77Skettenis   if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
249*b725ae77Skettenis     return NULL;
250*b725ae77Skettenis 
251*b725ae77Skettenis   while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
252*b725ae77Skettenis     block = BLOCK_SUPERBLOCK (block);
253*b725ae77Skettenis 
254*b725ae77Skettenis   return block;
255*b725ae77Skettenis }
256*b725ae77Skettenis 
257*b725ae77Skettenis /* Return the static block associated to BLOCK.  Return NULL if block
258*b725ae77Skettenis    is NULL.  */
259*b725ae77Skettenis 
260*b725ae77Skettenis const struct block *
block_global_block(const struct block * block)261*b725ae77Skettenis block_global_block (const struct block *block)
262*b725ae77Skettenis {
263*b725ae77Skettenis   if (block == NULL)
264*b725ae77Skettenis     return NULL;
265*b725ae77Skettenis 
266*b725ae77Skettenis   while (BLOCK_SUPERBLOCK (block) != NULL)
267*b725ae77Skettenis     block = BLOCK_SUPERBLOCK (block);
268*b725ae77Skettenis 
269*b725ae77Skettenis   return block;
270*b725ae77Skettenis }
271*b725ae77Skettenis 
272*b725ae77Skettenis /* Allocate a block on OBSTACK, and initialize its elements to
273*b725ae77Skettenis    zero/NULL.  This is useful for creating "dummy" blocks that don't
274*b725ae77Skettenis    correspond to actual source files.
275*b725ae77Skettenis 
276*b725ae77Skettenis    Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
277*b725ae77Skettenis    valid value.  If you really don't want the block to have a
278*b725ae77Skettenis    dictionary, then you should subsequently set its BLOCK_DICT to
279*b725ae77Skettenis    dict_create_linear (obstack, NULL).  */
280*b725ae77Skettenis 
281*b725ae77Skettenis struct block *
allocate_block(struct obstack * obstack)282*b725ae77Skettenis allocate_block (struct obstack *obstack)
283*b725ae77Skettenis {
284*b725ae77Skettenis   struct block *bl = obstack_alloc (obstack, sizeof (struct block));
285*b725ae77Skettenis 
286*b725ae77Skettenis   BLOCK_START (bl) = 0;
287*b725ae77Skettenis   BLOCK_END (bl) = 0;
288*b725ae77Skettenis   BLOCK_FUNCTION (bl) = NULL;
289*b725ae77Skettenis   BLOCK_SUPERBLOCK (bl) = NULL;
290*b725ae77Skettenis   BLOCK_DICT (bl) = NULL;
291*b725ae77Skettenis   BLOCK_NAMESPACE (bl) = NULL;
292*b725ae77Skettenis   BLOCK_GCC_COMPILED (bl) = 0;
293*b725ae77Skettenis 
294*b725ae77Skettenis   return bl;
295*b725ae77Skettenis }
296