2003-02-19 David Carlton <carlton@math.stanford.edu>
[external/binutils.git] / gdb / block.c
1 /* Block-related functions for the GNU debugger, GDB.
2
3    Copyright 2003 Free Software Foundation, Inc.
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
9    the Free Software Foundation; either version 2 of the License, or
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
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "block.h"
24 #include "symtab.h"
25 #include "symfile.h"
26
27 /* Return Nonzero if block a is lexically nested within block b,
28    or if a and b have the same pc range.
29    Return zero otherwise. */
30
31 int
32 contained_in (struct block *a, struct block *b)
33 {
34   if (!a || !b)
35     return 0;
36   return BLOCK_START (a) >= BLOCK_START (b)
37     && BLOCK_END (a) <= BLOCK_END (b);
38 }
39
40
41 /* Return the symbol for the function which contains a specified
42    lexical block, described by a struct block BL.  */
43
44 struct symbol *
45 block_function (struct block *bl)
46 {
47   while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
48     bl = BLOCK_SUPERBLOCK (bl);
49
50   return BLOCK_FUNCTION (bl);
51 }
52
53 /* Return the blockvector immediately containing the innermost lexical block
54    containing the specified pc value and section, or 0 if there is none.
55    PINDEX is a pointer to the index value of the block.  If PINDEX
56    is NULL, we don't pass this information back to the caller.  */
57
58 struct blockvector *
59 blockvector_for_pc_sect (register CORE_ADDR pc, struct sec *section,
60                          int *pindex, struct symtab *symtab)
61 {
62   register struct block *b;
63   register int bot, top, half;
64   struct blockvector *bl;
65
66   if (symtab == 0)              /* if no symtab specified by caller */
67     {
68       /* First search all symtabs for one whose file contains our pc */
69       if ((symtab = find_pc_sect_symtab (pc, section)) == 0)
70         return 0;
71     }
72
73   bl = BLOCKVECTOR (symtab);
74   b = BLOCKVECTOR_BLOCK (bl, 0);
75
76   /* Then search that symtab for the smallest block that wins.  */
77   /* Use binary search to find the last block that starts before PC.  */
78
79   bot = 0;
80   top = BLOCKVECTOR_NBLOCKS (bl);
81
82   while (top - bot > 1)
83     {
84       half = (top - bot + 1) >> 1;
85       b = BLOCKVECTOR_BLOCK (bl, bot + half);
86       if (BLOCK_START (b) <= pc)
87         bot += half;
88       else
89         top = bot + half;
90     }
91
92   /* Now search backward for a block that ends after PC.  */
93
94   while (bot >= 0)
95     {
96       b = BLOCKVECTOR_BLOCK (bl, bot);
97       if (BLOCK_END (b) > pc)
98         {
99           if (pindex)
100             *pindex = bot;
101           return bl;
102         }
103       bot--;
104     }
105   return 0;
106 }
107
108 /* Return the blockvector immediately containing the innermost lexical block
109    containing the specified pc value, or 0 if there is none.
110    Backward compatibility, no section.  */
111
112 struct blockvector *
113 blockvector_for_pc (register CORE_ADDR pc, int *pindex)
114 {
115   return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
116                                   pindex, NULL);
117 }
118
119 /* Return the innermost lexical block containing the specified pc value
120    in the specified section, or 0 if there is none.  */
121
122 struct block *
123 block_for_pc_sect (register CORE_ADDR pc, struct sec *section)
124 {
125   register struct blockvector *bl;
126   int index;
127
128   bl = blockvector_for_pc_sect (pc, section, &index, NULL);
129   if (bl)
130     return BLOCKVECTOR_BLOCK (bl, index);
131   return 0;
132 }
133
134 /* Return the innermost lexical block containing the specified pc value,
135    or 0 if there is none.  Backward compatibility, no section.  */
136
137 struct block *
138 block_for_pc (register CORE_ADDR pc)
139 {
140   return block_for_pc_sect (pc, find_pc_mapped_section (pc));
141 }