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