GDB (API)
|
00001 /* Block-related functions for the GNU debugger, GDB. 00002 00003 Copyright (C) 2003-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "block.h" 00022 #include "symtab.h" 00023 #include "symfile.h" 00024 #include "gdb_obstack.h" 00025 #include "cp-support.h" 00026 #include "addrmap.h" 00027 #include "gdbtypes.h" 00028 #include "exceptions.h" 00029 00030 /* This is used by struct block to store namespace-related info for 00031 C++ files, namely using declarations and the current namespace in 00032 scope. */ 00033 00034 struct block_namespace_info 00035 { 00036 const char *scope; 00037 struct using_direct *using; 00038 }; 00039 00040 static void block_initialize_namespace (struct block *block, 00041 struct obstack *obstack); 00042 00043 /* Return Nonzero if block a is lexically nested within block b, 00044 or if a and b have the same pc range. 00045 Return zero otherwise. */ 00046 00047 int 00048 contained_in (const struct block *a, const struct block *b) 00049 { 00050 if (!a || !b) 00051 return 0; 00052 00053 do 00054 { 00055 if (a == b) 00056 return 1; 00057 /* If A is a function block, then A cannot be contained in B, 00058 except if A was inlined. */ 00059 if (BLOCK_FUNCTION (a) != NULL && !block_inlined_p (a)) 00060 return 0; 00061 a = BLOCK_SUPERBLOCK (a); 00062 } 00063 while (a != NULL); 00064 00065 return 0; 00066 } 00067 00068 00069 /* Return the symbol for the function which contains a specified 00070 lexical block, described by a struct block BL. The return value 00071 will not be an inlined function; the containing function will be 00072 returned instead. */ 00073 00074 struct symbol * 00075 block_linkage_function (const struct block *bl) 00076 { 00077 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl)) 00078 && BLOCK_SUPERBLOCK (bl) != NULL) 00079 bl = BLOCK_SUPERBLOCK (bl); 00080 00081 return BLOCK_FUNCTION (bl); 00082 } 00083 00084 /* Return the symbol for the function which contains a specified 00085 block, described by a struct block BL. The return value will be 00086 the closest enclosing function, which might be an inline 00087 function. */ 00088 00089 struct symbol * 00090 block_containing_function (const struct block *bl) 00091 { 00092 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL) 00093 bl = BLOCK_SUPERBLOCK (bl); 00094 00095 return BLOCK_FUNCTION (bl); 00096 } 00097 00098 /* Return one if BL represents an inlined function. */ 00099 00100 int 00101 block_inlined_p (const struct block *bl) 00102 { 00103 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl)); 00104 } 00105 00106 /* A helper function that checks whether PC is in the blockvector BL. 00107 It returns the containing block if there is one, or else NULL. */ 00108 00109 static struct block * 00110 find_block_in_blockvector (struct blockvector *bl, CORE_ADDR pc) 00111 { 00112 struct block *b; 00113 int bot, top, half; 00114 00115 /* If we have an addrmap mapping code addresses to blocks, then use 00116 that. */ 00117 if (BLOCKVECTOR_MAP (bl)) 00118 return addrmap_find (BLOCKVECTOR_MAP (bl), pc); 00119 00120 /* Otherwise, use binary search to find the last block that starts 00121 before PC. 00122 Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1. 00123 They both have the same START,END values. 00124 Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the 00125 fact that this choice was made was subtle, now we make it explicit. */ 00126 gdb_assert (BLOCKVECTOR_NBLOCKS (bl) >= 2); 00127 bot = STATIC_BLOCK; 00128 top = BLOCKVECTOR_NBLOCKS (bl); 00129 00130 while (top - bot > 1) 00131 { 00132 half = (top - bot + 1) >> 1; 00133 b = BLOCKVECTOR_BLOCK (bl, bot + half); 00134 if (BLOCK_START (b) <= pc) 00135 bot += half; 00136 else 00137 top = bot + half; 00138 } 00139 00140 /* Now search backward for a block that ends after PC. */ 00141 00142 while (bot >= STATIC_BLOCK) 00143 { 00144 b = BLOCKVECTOR_BLOCK (bl, bot); 00145 if (BLOCK_END (b) > pc) 00146 return b; 00147 bot--; 00148 } 00149 00150 return NULL; 00151 } 00152 00153 /* Return the blockvector immediately containing the innermost lexical 00154 block containing the specified pc value and section, or 0 if there 00155 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we 00156 don't pass this information back to the caller. */ 00157 00158 struct blockvector * 00159 blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section, 00160 struct block **pblock, struct symtab *symtab) 00161 { 00162 struct blockvector *bl; 00163 struct block *b; 00164 00165 if (symtab == 0) /* if no symtab specified by caller */ 00166 { 00167 /* First search all symtabs for one whose file contains our pc */ 00168 symtab = find_pc_sect_symtab (pc, section); 00169 if (symtab == 0) 00170 return 0; 00171 } 00172 00173 bl = BLOCKVECTOR (symtab); 00174 00175 /* Then search that symtab for the smallest block that wins. */ 00176 b = find_block_in_blockvector (bl, pc); 00177 if (b == NULL) 00178 return NULL; 00179 00180 if (pblock) 00181 *pblock = b; 00182 return bl; 00183 } 00184 00185 /* Return true if the blockvector BV contains PC, false otherwise. */ 00186 00187 int 00188 blockvector_contains_pc (struct blockvector *bv, CORE_ADDR pc) 00189 { 00190 return find_block_in_blockvector (bv, pc) != NULL; 00191 } 00192 00193 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it 00194 must be the next instruction after call (or after tail call jump). Throw 00195 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */ 00196 00197 struct call_site * 00198 call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc) 00199 { 00200 struct symtab *symtab; 00201 void **slot = NULL; 00202 00203 /* -1 as tail call PC can be already after the compilation unit range. */ 00204 symtab = find_pc_symtab (pc - 1); 00205 00206 if (symtab != NULL && symtab->call_site_htab != NULL) 00207 slot = htab_find_slot (symtab->call_site_htab, &pc, NO_INSERT); 00208 00209 if (slot == NULL) 00210 { 00211 struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc); 00212 00213 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine 00214 the call target. */ 00215 throw_error (NO_ENTRY_VALUE_ERROR, 00216 _("DW_OP_GNU_entry_value resolving cannot find " 00217 "DW_TAG_GNU_call_site %s in %s"), 00218 paddress (gdbarch, pc), 00219 (msym.minsym == NULL ? "???" 00220 : SYMBOL_PRINT_NAME (msym.minsym))); 00221 } 00222 00223 return *slot; 00224 } 00225 00226 /* Return the blockvector immediately containing the innermost lexical block 00227 containing the specified pc value, or 0 if there is none. 00228 Backward compatibility, no section. */ 00229 00230 struct blockvector * 00231 blockvector_for_pc (CORE_ADDR pc, struct block **pblock) 00232 { 00233 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc), 00234 pblock, NULL); 00235 } 00236 00237 /* Return the innermost lexical block containing the specified pc value 00238 in the specified section, or 0 if there is none. */ 00239 00240 struct block * 00241 block_for_pc_sect (CORE_ADDR pc, struct obj_section *section) 00242 { 00243 struct blockvector *bl; 00244 struct block *b; 00245 00246 bl = blockvector_for_pc_sect (pc, section, &b, NULL); 00247 if (bl) 00248 return b; 00249 return 0; 00250 } 00251 00252 /* Return the innermost lexical block containing the specified pc value, 00253 or 0 if there is none. Backward compatibility, no section. */ 00254 00255 struct block * 00256 block_for_pc (CORE_ADDR pc) 00257 { 00258 return block_for_pc_sect (pc, find_pc_mapped_section (pc)); 00259 } 00260 00261 /* Now come some functions designed to deal with C++ namespace issues. 00262 The accessors are safe to use even in the non-C++ case. */ 00263 00264 /* This returns the namespace that BLOCK is enclosed in, or "" if it 00265 isn't enclosed in a namespace at all. This travels the chain of 00266 superblocks looking for a scope, if necessary. */ 00267 00268 const char * 00269 block_scope (const struct block *block) 00270 { 00271 for (; block != NULL; block = BLOCK_SUPERBLOCK (block)) 00272 { 00273 if (BLOCK_NAMESPACE (block) != NULL 00274 && BLOCK_NAMESPACE (block)->scope != NULL) 00275 return BLOCK_NAMESPACE (block)->scope; 00276 } 00277 00278 return ""; 00279 } 00280 00281 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via 00282 OBSTACK. (It won't make a copy of SCOPE, however, so that already 00283 has to be allocated correctly.) */ 00284 00285 void 00286 block_set_scope (struct block *block, const char *scope, 00287 struct obstack *obstack) 00288 { 00289 block_initialize_namespace (block, obstack); 00290 00291 BLOCK_NAMESPACE (block)->scope = scope; 00292 } 00293 00294 /* This returns the using directives list associated with BLOCK, if 00295 any. */ 00296 00297 struct using_direct * 00298 block_using (const struct block *block) 00299 { 00300 if (block == NULL || BLOCK_NAMESPACE (block) == NULL) 00301 return NULL; 00302 else 00303 return BLOCK_NAMESPACE (block)->using; 00304 } 00305 00306 /* Set BLOCK's using member to USING; if needed, allocate memory via 00307 OBSTACK. (It won't make a copy of USING, however, so that already 00308 has to be allocated correctly.) */ 00309 00310 void 00311 block_set_using (struct block *block, 00312 struct using_direct *using, 00313 struct obstack *obstack) 00314 { 00315 block_initialize_namespace (block, obstack); 00316 00317 BLOCK_NAMESPACE (block)->using = using; 00318 } 00319 00320 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and 00321 ititialize its members to zero. */ 00322 00323 static void 00324 block_initialize_namespace (struct block *block, struct obstack *obstack) 00325 { 00326 if (BLOCK_NAMESPACE (block) == NULL) 00327 { 00328 BLOCK_NAMESPACE (block) 00329 = obstack_alloc (obstack, sizeof (struct block_namespace_info)); 00330 BLOCK_NAMESPACE (block)->scope = NULL; 00331 BLOCK_NAMESPACE (block)->using = NULL; 00332 } 00333 } 00334 00335 /* Return the static block associated to BLOCK. Return NULL if block 00336 is NULL or if block is a global block. */ 00337 00338 const struct block * 00339 block_static_block (const struct block *block) 00340 { 00341 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL) 00342 return NULL; 00343 00344 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL) 00345 block = BLOCK_SUPERBLOCK (block); 00346 00347 return block; 00348 } 00349 00350 /* Return the static block associated to BLOCK. Return NULL if block 00351 is NULL. */ 00352 00353 const struct block * 00354 block_global_block (const struct block *block) 00355 { 00356 if (block == NULL) 00357 return NULL; 00358 00359 while (BLOCK_SUPERBLOCK (block) != NULL) 00360 block = BLOCK_SUPERBLOCK (block); 00361 00362 return block; 00363 } 00364 00365 /* Allocate a block on OBSTACK, and initialize its elements to 00366 zero/NULL. This is useful for creating "dummy" blocks that don't 00367 correspond to actual source files. 00368 00369 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a 00370 valid value. If you really don't want the block to have a 00371 dictionary, then you should subsequently set its BLOCK_DICT to 00372 dict_create_linear (obstack, NULL). */ 00373 00374 struct block * 00375 allocate_block (struct obstack *obstack) 00376 { 00377 struct block *bl = obstack_alloc (obstack, sizeof (struct block)); 00378 00379 BLOCK_START (bl) = 0; 00380 BLOCK_END (bl) = 0; 00381 BLOCK_FUNCTION (bl) = NULL; 00382 BLOCK_SUPERBLOCK (bl) = NULL; 00383 BLOCK_DICT (bl) = NULL; 00384 BLOCK_NAMESPACE (bl) = NULL; 00385 00386 return bl; 00387 } 00388 00389 /* Allocate a global block. */ 00390 00391 struct block * 00392 allocate_global_block (struct obstack *obstack) 00393 { 00394 struct global_block *bl = OBSTACK_ZALLOC (obstack, struct global_block); 00395 00396 return &bl->block; 00397 } 00398 00399 /* Set the symtab of the global block. */ 00400 00401 void 00402 set_block_symtab (struct block *block, struct symtab *symtab) 00403 { 00404 struct global_block *gb; 00405 00406 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL); 00407 gb = (struct global_block *) block; 00408 gdb_assert (gb->symtab == NULL); 00409 gb->symtab = symtab; 00410 } 00411 00412 /* Return the symtab of the global block. */ 00413 00414 static struct symtab * 00415 get_block_symtab (const struct block *block) 00416 { 00417 struct global_block *gb; 00418 00419 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL); 00420 gb = (struct global_block *) block; 00421 gdb_assert (gb->symtab != NULL); 00422 return gb->symtab; 00423 } 00424 00425 00426 00427 /* Initialize a block iterator, either to iterate over a single block, 00428 or, for static and global blocks, all the included symtabs as 00429 well. */ 00430 00431 static void 00432 initialize_block_iterator (const struct block *block, 00433 struct block_iterator *iter) 00434 { 00435 enum block_enum which; 00436 struct symtab *symtab; 00437 00438 iter->idx = -1; 00439 00440 if (BLOCK_SUPERBLOCK (block) == NULL) 00441 { 00442 which = GLOBAL_BLOCK; 00443 symtab = get_block_symtab (block); 00444 } 00445 else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL) 00446 { 00447 which = STATIC_BLOCK; 00448 symtab = get_block_symtab (BLOCK_SUPERBLOCK (block)); 00449 } 00450 else 00451 { 00452 iter->d.block = block; 00453 /* A signal value meaning that we're iterating over a single 00454 block. */ 00455 iter->which = FIRST_LOCAL_BLOCK; 00456 return; 00457 } 00458 00459 /* If this is an included symtab, find the canonical includer and 00460 use it instead. */ 00461 while (symtab->user != NULL) 00462 symtab = symtab->user; 00463 00464 /* Putting this check here simplifies the logic of the iterator 00465 functions. If there are no included symtabs, we only need to 00466 search a single block, so we might as well just do that 00467 directly. */ 00468 if (symtab->includes == NULL) 00469 { 00470 iter->d.block = block; 00471 /* A signal value meaning that we're iterating over a single 00472 block. */ 00473 iter->which = FIRST_LOCAL_BLOCK; 00474 } 00475 else 00476 { 00477 iter->d.symtab = symtab; 00478 iter->which = which; 00479 } 00480 } 00481 00482 /* A helper function that finds the current symtab over whose static 00483 or global block we should iterate. */ 00484 00485 static struct symtab * 00486 find_iterator_symtab (struct block_iterator *iterator) 00487 { 00488 if (iterator->idx == -1) 00489 return iterator->d.symtab; 00490 return iterator->d.symtab->includes[iterator->idx]; 00491 } 00492 00493 /* Perform a single step for a plain block iterator, iterating across 00494 symbol tables as needed. Returns the next symbol, or NULL when 00495 iteration is complete. */ 00496 00497 static struct symbol * 00498 block_iterator_step (struct block_iterator *iterator, int first) 00499 { 00500 struct symbol *sym; 00501 00502 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK); 00503 00504 while (1) 00505 { 00506 if (first) 00507 { 00508 struct symtab *symtab = find_iterator_symtab (iterator); 00509 const struct block *block; 00510 00511 /* Iteration is complete. */ 00512 if (symtab == NULL) 00513 return NULL; 00514 00515 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which); 00516 sym = dict_iterator_first (BLOCK_DICT (block), &iterator->dict_iter); 00517 } 00518 else 00519 sym = dict_iterator_next (&iterator->dict_iter); 00520 00521 if (sym != NULL) 00522 return sym; 00523 00524 /* We have finished iterating the appropriate block of one 00525 symtab. Now advance to the next symtab and begin iteration 00526 there. */ 00527 ++iterator->idx; 00528 first = 1; 00529 } 00530 } 00531 00532 /* See block.h. */ 00533 00534 struct symbol * 00535 block_iterator_first (const struct block *block, 00536 struct block_iterator *iterator) 00537 { 00538 initialize_block_iterator (block, iterator); 00539 00540 if (iterator->which == FIRST_LOCAL_BLOCK) 00541 return dict_iterator_first (block->dict, &iterator->dict_iter); 00542 00543 return block_iterator_step (iterator, 1); 00544 } 00545 00546 /* See block.h. */ 00547 00548 struct symbol * 00549 block_iterator_next (struct block_iterator *iterator) 00550 { 00551 if (iterator->which == FIRST_LOCAL_BLOCK) 00552 return dict_iterator_next (&iterator->dict_iter); 00553 00554 return block_iterator_step (iterator, 0); 00555 } 00556 00557 /* Perform a single step for a "name" block iterator, iterating across 00558 symbol tables as needed. Returns the next symbol, or NULL when 00559 iteration is complete. */ 00560 00561 static struct symbol * 00562 block_iter_name_step (struct block_iterator *iterator, const char *name, 00563 int first) 00564 { 00565 struct symbol *sym; 00566 00567 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK); 00568 00569 while (1) 00570 { 00571 if (first) 00572 { 00573 struct symtab *symtab = find_iterator_symtab (iterator); 00574 const struct block *block; 00575 00576 /* Iteration is complete. */ 00577 if (symtab == NULL) 00578 return NULL; 00579 00580 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which); 00581 sym = dict_iter_name_first (BLOCK_DICT (block), name, 00582 &iterator->dict_iter); 00583 } 00584 else 00585 sym = dict_iter_name_next (name, &iterator->dict_iter); 00586 00587 if (sym != NULL) 00588 return sym; 00589 00590 /* We have finished iterating the appropriate block of one 00591 symtab. Now advance to the next symtab and begin iteration 00592 there. */ 00593 ++iterator->idx; 00594 first = 1; 00595 } 00596 } 00597 00598 /* See block.h. */ 00599 00600 struct symbol * 00601 block_iter_name_first (const struct block *block, 00602 const char *name, 00603 struct block_iterator *iterator) 00604 { 00605 initialize_block_iterator (block, iterator); 00606 00607 if (iterator->which == FIRST_LOCAL_BLOCK) 00608 return dict_iter_name_first (block->dict, name, &iterator->dict_iter); 00609 00610 return block_iter_name_step (iterator, name, 1); 00611 } 00612 00613 /* See block.h. */ 00614 00615 struct symbol * 00616 block_iter_name_next (const char *name, struct block_iterator *iterator) 00617 { 00618 if (iterator->which == FIRST_LOCAL_BLOCK) 00619 return dict_iter_name_next (name, &iterator->dict_iter); 00620 00621 return block_iter_name_step (iterator, name, 0); 00622 } 00623 00624 /* Perform a single step for a "match" block iterator, iterating 00625 across symbol tables as needed. Returns the next symbol, or NULL 00626 when iteration is complete. */ 00627 00628 static struct symbol * 00629 block_iter_match_step (struct block_iterator *iterator, 00630 const char *name, 00631 symbol_compare_ftype *compare, 00632 int first) 00633 { 00634 struct symbol *sym; 00635 00636 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK); 00637 00638 while (1) 00639 { 00640 if (first) 00641 { 00642 struct symtab *symtab = find_iterator_symtab (iterator); 00643 const struct block *block; 00644 00645 /* Iteration is complete. */ 00646 if (symtab == NULL) 00647 return NULL; 00648 00649 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which); 00650 sym = dict_iter_match_first (BLOCK_DICT (block), name, 00651 compare, &iterator->dict_iter); 00652 } 00653 else 00654 sym = dict_iter_match_next (name, compare, &iterator->dict_iter); 00655 00656 if (sym != NULL) 00657 return sym; 00658 00659 /* We have finished iterating the appropriate block of one 00660 symtab. Now advance to the next symtab and begin iteration 00661 there. */ 00662 ++iterator->idx; 00663 first = 1; 00664 } 00665 } 00666 00667 /* See block.h. */ 00668 00669 struct symbol * 00670 block_iter_match_first (const struct block *block, 00671 const char *name, 00672 symbol_compare_ftype *compare, 00673 struct block_iterator *iterator) 00674 { 00675 initialize_block_iterator (block, iterator); 00676 00677 if (iterator->which == FIRST_LOCAL_BLOCK) 00678 return dict_iter_match_first (block->dict, name, compare, 00679 &iterator->dict_iter); 00680 00681 return block_iter_match_step (iterator, name, compare, 1); 00682 } 00683 00684 /* See block.h. */ 00685 00686 struct symbol * 00687 block_iter_match_next (const char *name, 00688 symbol_compare_ftype *compare, 00689 struct block_iterator *iterator) 00690 { 00691 if (iterator->which == FIRST_LOCAL_BLOCK) 00692 return dict_iter_match_next (name, compare, &iterator->dict_iter); 00693 00694 return block_iter_match_step (iterator, name, compare, 0); 00695 }