| 1 | /* Block-related functions for the GNU debugger, GDB. |
| 2 | |
| 3 | Copyright (C) 2003, 2007-2012 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 | #include "gdbtypes.h" |
| 28 | #include "exceptions.h" |
| 29 | |
| 30 | /* This is used by struct block to store namespace-related info for |
| 31 | C++ files, namely using declarations and the current namespace in |
| 32 | scope. */ |
| 33 | |
| 34 | struct block_namespace_info |
| 35 | { |
| 36 | const char *scope; |
| 37 | struct using_direct *using; |
| 38 | }; |
| 39 | |
| 40 | static void block_initialize_namespace (struct block *block, |
| 41 | struct obstack *obstack); |
| 42 | |
| 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. */ |
| 46 | |
| 47 | int |
| 48 | contained_in (const struct block *a, const struct block *b) |
| 49 | { |
| 50 | if (!a || !b) |
| 51 | return 0; |
| 52 | |
| 53 | do |
| 54 | { |
| 55 | if (a == b) |
| 56 | return 1; |
| 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)) |
| 60 | return 0; |
| 61 | a = BLOCK_SUPERBLOCK (a); |
| 62 | } |
| 63 | while (a != NULL); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | |
| 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 |
| 72 | returned instead. */ |
| 73 | |
| 74 | struct symbol * |
| 75 | block_linkage_function (const struct block *bl) |
| 76 | { |
| 77 | while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl)) |
| 78 | && BLOCK_SUPERBLOCK (bl) != NULL) |
| 79 | bl = BLOCK_SUPERBLOCK (bl); |
| 80 | |
| 81 | return BLOCK_FUNCTION (bl); |
| 82 | } |
| 83 | |
| 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 |
| 87 | function. */ |
| 88 | |
| 89 | struct symbol * |
| 90 | block_containing_function (const struct block *bl) |
| 91 | { |
| 92 | while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL) |
| 93 | bl = BLOCK_SUPERBLOCK (bl); |
| 94 | |
| 95 | return BLOCK_FUNCTION (bl); |
| 96 | } |
| 97 | |
| 98 | /* Return one if BL represents an inlined function. */ |
| 99 | |
| 100 | int |
| 101 | block_inlined_p (const struct block *bl) |
| 102 | { |
| 103 | return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl)); |
| 104 | } |
| 105 | |
| 106 | /* Return the blockvector immediately containing the innermost lexical |
| 107 | block containing the specified pc value and section, or 0 if there |
| 108 | is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we |
| 109 | don't pass this information back to the caller. */ |
| 110 | |
| 111 | struct blockvector * |
| 112 | blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section, |
| 113 | struct block **pblock, struct symtab *symtab) |
| 114 | { |
| 115 | struct block *b; |
| 116 | int bot, top, half; |
| 117 | struct blockvector *bl; |
| 118 | |
| 119 | if (symtab == 0) /* if no symtab specified by caller */ |
| 120 | { |
| 121 | /* First search all symtabs for one whose file contains our pc */ |
| 122 | symtab = find_pc_sect_symtab (pc, section); |
| 123 | if (symtab == 0) |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | bl = BLOCKVECTOR (symtab); |
| 128 | |
| 129 | /* Then search that symtab for the smallest block that wins. */ |
| 130 | |
| 131 | /* If we have an addrmap mapping code addresses to blocks, then use |
| 132 | that. */ |
| 133 | if (BLOCKVECTOR_MAP (bl)) |
| 134 | { |
| 135 | b = addrmap_find (BLOCKVECTOR_MAP (bl), pc); |
| 136 | if (b) |
| 137 | { |
| 138 | if (pblock) |
| 139 | *pblock = b; |
| 140 | return bl; |
| 141 | } |
| 142 | else |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /* Otherwise, use binary search to find the last block that starts |
| 148 | before PC. */ |
| 149 | bot = 0; |
| 150 | top = BLOCKVECTOR_NBLOCKS (bl); |
| 151 | |
| 152 | while (top - bot > 1) |
| 153 | { |
| 154 | half = (top - bot + 1) >> 1; |
| 155 | b = BLOCKVECTOR_BLOCK (bl, bot + half); |
| 156 | if (BLOCK_START (b) <= pc) |
| 157 | bot += half; |
| 158 | else |
| 159 | top = bot + half; |
| 160 | } |
| 161 | |
| 162 | /* Now search backward for a block that ends after PC. */ |
| 163 | |
| 164 | while (bot >= 0) |
| 165 | { |
| 166 | b = BLOCKVECTOR_BLOCK (bl, bot); |
| 167 | if (BLOCK_END (b) > pc) |
| 168 | { |
| 169 | if (pblock) |
| 170 | *pblock = b; |
| 171 | return bl; |
| 172 | } |
| 173 | bot--; |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* Return call_site for specified PC in GDBARCH. PC must match exactly, it |
| 179 | must be the next instruction after call (or after tail call jump). Throw |
| 180 | NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */ |
| 181 | |
| 182 | struct call_site * |
| 183 | call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc) |
| 184 | { |
| 185 | struct symtab *symtab; |
| 186 | void **slot = NULL; |
| 187 | |
| 188 | /* -1 as tail call PC can be already after the compilation unit range. */ |
| 189 | symtab = find_pc_symtab (pc - 1); |
| 190 | |
| 191 | if (symtab != NULL && symtab->call_site_htab != NULL) |
| 192 | slot = htab_find_slot (symtab->call_site_htab, &pc, NO_INSERT); |
| 193 | |
| 194 | if (slot == NULL) |
| 195 | { |
| 196 | struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (pc); |
| 197 | |
| 198 | /* DW_TAG_gnu_call_site will be missing just if GCC could not determine |
| 199 | the call target. */ |
| 200 | throw_error (NO_ENTRY_VALUE_ERROR, |
| 201 | _("DW_OP_GNU_entry_value resolving cannot find " |
| 202 | "DW_TAG_GNU_call_site %s in %s"), |
| 203 | paddress (gdbarch, pc), |
| 204 | msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym)); |
| 205 | } |
| 206 | |
| 207 | return *slot; |
| 208 | } |
| 209 | |
| 210 | /* Return the blockvector immediately containing the innermost lexical block |
| 211 | containing the specified pc value, or 0 if there is none. |
| 212 | Backward compatibility, no section. */ |
| 213 | |
| 214 | struct blockvector * |
| 215 | blockvector_for_pc (CORE_ADDR pc, struct block **pblock) |
| 216 | { |
| 217 | return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc), |
| 218 | pblock, NULL); |
| 219 | } |
| 220 | |
| 221 | /* Return the innermost lexical block containing the specified pc value |
| 222 | in the specified section, or 0 if there is none. */ |
| 223 | |
| 224 | struct block * |
| 225 | block_for_pc_sect (CORE_ADDR pc, struct obj_section *section) |
| 226 | { |
| 227 | struct blockvector *bl; |
| 228 | struct block *b; |
| 229 | |
| 230 | bl = blockvector_for_pc_sect (pc, section, &b, NULL); |
| 231 | if (bl) |
| 232 | return b; |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | /* Return the innermost lexical block containing the specified pc value, |
| 237 | or 0 if there is none. Backward compatibility, no section. */ |
| 238 | |
| 239 | struct block * |
| 240 | block_for_pc (CORE_ADDR pc) |
| 241 | { |
| 242 | return block_for_pc_sect (pc, find_pc_mapped_section (pc)); |
| 243 | } |
| 244 | |
| 245 | /* Now come some functions designed to deal with C++ namespace issues. |
| 246 | The accessors are safe to use even in the non-C++ case. */ |
| 247 | |
| 248 | /* This returns the namespace that BLOCK is enclosed in, or "" if it |
| 249 | isn't enclosed in a namespace at all. This travels the chain of |
| 250 | superblocks looking for a scope, if necessary. */ |
| 251 | |
| 252 | const char * |
| 253 | block_scope (const struct block *block) |
| 254 | { |
| 255 | for (; block != NULL; block = BLOCK_SUPERBLOCK (block)) |
| 256 | { |
| 257 | if (BLOCK_NAMESPACE (block) != NULL |
| 258 | && BLOCK_NAMESPACE (block)->scope != NULL) |
| 259 | return BLOCK_NAMESPACE (block)->scope; |
| 260 | } |
| 261 | |
| 262 | return ""; |
| 263 | } |
| 264 | |
| 265 | /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via |
| 266 | OBSTACK. (It won't make a copy of SCOPE, however, so that already |
| 267 | has to be allocated correctly.) */ |
| 268 | |
| 269 | void |
| 270 | block_set_scope (struct block *block, const char *scope, |
| 271 | struct obstack *obstack) |
| 272 | { |
| 273 | block_initialize_namespace (block, obstack); |
| 274 | |
| 275 | BLOCK_NAMESPACE (block)->scope = scope; |
| 276 | } |
| 277 | |
| 278 | /* This returns the using directives list associated with BLOCK, if |
| 279 | any. */ |
| 280 | |
| 281 | struct using_direct * |
| 282 | block_using (const struct block *block) |
| 283 | { |
| 284 | if (block == NULL || BLOCK_NAMESPACE (block) == NULL) |
| 285 | return NULL; |
| 286 | else |
| 287 | return BLOCK_NAMESPACE (block)->using; |
| 288 | } |
| 289 | |
| 290 | /* Set BLOCK's using member to USING; if needed, allocate memory via |
| 291 | OBSTACK. (It won't make a copy of USING, however, so that already |
| 292 | has to be allocated correctly.) */ |
| 293 | |
| 294 | void |
| 295 | block_set_using (struct block *block, |
| 296 | struct using_direct *using, |
| 297 | struct obstack *obstack) |
| 298 | { |
| 299 | block_initialize_namespace (block, obstack); |
| 300 | |
| 301 | BLOCK_NAMESPACE (block)->using = using; |
| 302 | } |
| 303 | |
| 304 | /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and |
| 305 | ititialize its members to zero. */ |
| 306 | |
| 307 | static void |
| 308 | block_initialize_namespace (struct block *block, struct obstack *obstack) |
| 309 | { |
| 310 | if (BLOCK_NAMESPACE (block) == NULL) |
| 311 | { |
| 312 | BLOCK_NAMESPACE (block) |
| 313 | = obstack_alloc (obstack, sizeof (struct block_namespace_info)); |
| 314 | BLOCK_NAMESPACE (block)->scope = NULL; |
| 315 | BLOCK_NAMESPACE (block)->using = NULL; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /* Return the static block associated to BLOCK. Return NULL if block |
| 320 | is NULL or if block is a global block. */ |
| 321 | |
| 322 | const struct block * |
| 323 | block_static_block (const struct block *block) |
| 324 | { |
| 325 | if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL) |
| 326 | return NULL; |
| 327 | |
| 328 | while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL) |
| 329 | block = BLOCK_SUPERBLOCK (block); |
| 330 | |
| 331 | return block; |
| 332 | } |
| 333 | |
| 334 | /* Return the static block associated to BLOCK. Return NULL if block |
| 335 | is NULL. */ |
| 336 | |
| 337 | const struct block * |
| 338 | block_global_block (const struct block *block) |
| 339 | { |
| 340 | if (block == NULL) |
| 341 | return NULL; |
| 342 | |
| 343 | while (BLOCK_SUPERBLOCK (block) != NULL) |
| 344 | block = BLOCK_SUPERBLOCK (block); |
| 345 | |
| 346 | return block; |
| 347 | } |
| 348 | |
| 349 | /* Allocate a block on OBSTACK, and initialize its elements to |
| 350 | zero/NULL. This is useful for creating "dummy" blocks that don't |
| 351 | correspond to actual source files. |
| 352 | |
| 353 | Warning: it sets the block's BLOCK_DICT to NULL, which isn't a |
| 354 | valid value. If you really don't want the block to have a |
| 355 | dictionary, then you should subsequently set its BLOCK_DICT to |
| 356 | dict_create_linear (obstack, NULL). */ |
| 357 | |
| 358 | struct block * |
| 359 | allocate_block (struct obstack *obstack) |
| 360 | { |
| 361 | struct block *bl = obstack_alloc (obstack, sizeof (struct block)); |
| 362 | |
| 363 | BLOCK_START (bl) = 0; |
| 364 | BLOCK_END (bl) = 0; |
| 365 | BLOCK_FUNCTION (bl) = NULL; |
| 366 | BLOCK_SUPERBLOCK (bl) = NULL; |
| 367 | BLOCK_DICT (bl) = NULL; |
| 368 | BLOCK_NAMESPACE (bl) = NULL; |
| 369 | |
| 370 | return bl; |
| 371 | } |