block.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* Code dealing with blocks for GDB.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef BLOCK_H
  15. #define BLOCK_H
  16. #include "dictionary.h"
  17. /* Opaque declarations. */
  18. struct symbol;
  19. struct compunit_symtab;
  20. struct block_namespace_info;
  21. struct using_direct;
  22. struct obstack;
  23. struct addrmap;
  24. /* Blocks can occupy non-contiguous address ranges. When this occurs,
  25. startaddr and endaddr within struct block (still) specify the lowest
  26. and highest addresses of all ranges, but each individual range is
  27. specified by the addresses in struct blockrange. */
  28. struct blockrange
  29. {
  30. blockrange (CORE_ADDR startaddr_, CORE_ADDR endaddr_)
  31. : startaddr (startaddr_),
  32. endaddr (endaddr_)
  33. {
  34. }
  35. /* Lowest address in this range. */
  36. CORE_ADDR startaddr;
  37. /* One past the highest address in the range. */
  38. CORE_ADDR endaddr;
  39. };
  40. /* Two or more non-contiguous ranges in the same order as that provided
  41. via the debug info. */
  42. struct blockranges
  43. {
  44. int nranges;
  45. struct blockrange range[1];
  46. };
  47. /* All of the name-scope contours of the program
  48. are represented by `struct block' objects.
  49. All of these objects are pointed to by the blockvector.
  50. Each block represents one name scope.
  51. Each lexical context has its own block.
  52. The blockvector begins with some special blocks.
  53. The GLOBAL_BLOCK contains all the symbols defined in this compilation
  54. whose scope is the entire program linked together.
  55. The STATIC_BLOCK contains all the symbols whose scope is the
  56. entire compilation excluding other separate compilations.
  57. Blocks starting with the FIRST_LOCAL_BLOCK are not special.
  58. Each block records a range of core addresses for the code that
  59. is in the scope of the block. The STATIC_BLOCK and GLOBAL_BLOCK
  60. give, for the range of code, the entire range of code produced
  61. by the compilation that the symbol segment belongs to.
  62. The blocks appear in the blockvector
  63. in order of increasing starting-address,
  64. and, within that, in order of decreasing ending-address.
  65. This implies that within the body of one function
  66. the blocks appear in the order of a depth-first tree walk. */
  67. struct block
  68. {
  69. /* Addresses in the executable code that are in this block. */
  70. CORE_ADDR startaddr;
  71. CORE_ADDR endaddr;
  72. /* The symbol that names this block, if the block is the body of a
  73. function (real or inlined); otherwise, zero. */
  74. struct symbol *function;
  75. /* The `struct block' for the containing block, or 0 if none.
  76. The superblock of a top-level local block (i.e. a function in the
  77. case of C) is the STATIC_BLOCK. The superblock of the
  78. STATIC_BLOCK is the GLOBAL_BLOCK. */
  79. const struct block *superblock;
  80. /* This is used to store the symbols in the block. */
  81. struct multidictionary *multidict;
  82. /* Contains information about namespace-related info relevant to this block:
  83. using directives and the current namespace scope. */
  84. struct block_namespace_info *namespace_info;
  85. /* Address ranges for blocks with non-contiguous ranges. If this
  86. is NULL, then there is only one range which is specified by
  87. startaddr and endaddr above. */
  88. struct blockranges *ranges;
  89. };
  90. /* The global block is singled out so that we can provide a back-link
  91. to the compunit symtab. */
  92. struct global_block
  93. {
  94. /* The block. */
  95. struct block block;
  96. /* This holds a pointer to the compunit symtab holding this block. */
  97. struct compunit_symtab *compunit_symtab;
  98. };
  99. #define BLOCK_START(bl) (bl)->startaddr
  100. #define BLOCK_END(bl) (bl)->endaddr
  101. #define BLOCK_FUNCTION(bl) (bl)->function
  102. #define BLOCK_SUPERBLOCK(bl) (bl)->superblock
  103. #define BLOCK_MULTIDICT(bl) (bl)->multidict
  104. #define BLOCK_NAMESPACE(bl) (bl)->namespace_info
  105. /* Accessor for ranges field within block BL. */
  106. #define BLOCK_RANGES(bl) (bl)->ranges
  107. /* Number of ranges within a block. */
  108. #define BLOCK_NRANGES(bl) (bl)->ranges->nranges
  109. /* Access range array for block BL. */
  110. #define BLOCK_RANGE(bl) (bl)->ranges->range
  111. /* Are all addresses within a block contiguous? */
  112. #define BLOCK_CONTIGUOUS_P(bl) (BLOCK_RANGES (bl) == nullptr \
  113. || BLOCK_NRANGES (bl) <= 1)
  114. /* Obtain the start address of the Nth range for block BL. */
  115. #define BLOCK_RANGE_START(bl,n) (BLOCK_RANGE (bl)[n].startaddr)
  116. /* Obtain the end address of the Nth range for block BL. */
  117. #define BLOCK_RANGE_END(bl,n) (BLOCK_RANGE (bl)[n].endaddr)
  118. /* Define the "entry pc" for a block BL to be the lowest (start) address
  119. for the block when all addresses within the block are contiguous. If
  120. non-contiguous, then use the start address for the first range in the
  121. block.
  122. At the moment, this almost matches what DWARF specifies as the entry
  123. pc. (The missing bit is support for DW_AT_entry_pc which should be
  124. preferred over range data and the low_pc.)
  125. Once support for DW_AT_entry_pc is added, I expect that an entry_pc
  126. field will be added to one of these data structures. Once that's done,
  127. the entry_pc field can be set from the dwarf reader (and other readers
  128. too). BLOCK_ENTRY_PC can then be redefined to be less DWARF-centric. */
  129. #define BLOCK_ENTRY_PC(bl) (BLOCK_CONTIGUOUS_P (bl) \
  130. ? BLOCK_START (bl) \
  131. : BLOCK_RANGE_START (bl,0))
  132. struct blockvector
  133. {
  134. /* Number of blocks in the list. */
  135. int nblocks;
  136. /* An address map mapping addresses to blocks in this blockvector.
  137. This pointer is zero if the blocks' start and end addresses are
  138. enough. */
  139. struct addrmap *map;
  140. /* The blocks themselves. */
  141. struct block *block[1];
  142. };
  143. #define BLOCKVECTOR_NBLOCKS(blocklist) (blocklist)->nblocks
  144. #define BLOCKVECTOR_BLOCK(blocklist,n) (blocklist)->block[n]
  145. #define BLOCKVECTOR_MAP(blocklist) ((blocklist)->map)
  146. /* Return the objfile of BLOCK, which must be non-NULL. */
  147. extern struct objfile *block_objfile (const struct block *block);
  148. /* Return the architecture of BLOCK, which must be non-NULL. */
  149. extern struct gdbarch *block_gdbarch (const struct block *block);
  150. extern struct symbol *block_linkage_function (const struct block *);
  151. extern struct symbol *block_containing_function (const struct block *);
  152. extern int block_inlined_p (const struct block *block);
  153. /* Return true if block A is lexically nested within block B, or if a
  154. and b have the same pc range. Return false otherwise. If
  155. ALLOW_NESTED is true, then block A is considered to be in block B
  156. if A is in a nested function in B's function. If ALLOW_NESTED is
  157. false (the default), then blocks in nested functions are not
  158. considered to be contained. */
  159. extern bool contained_in (const struct block *a, const struct block *b,
  160. bool allow_nested = false);
  161. extern const struct blockvector *blockvector_for_pc (CORE_ADDR,
  162. const struct block **);
  163. extern const struct blockvector *
  164. blockvector_for_pc_sect (CORE_ADDR, struct obj_section *,
  165. const struct block **, struct compunit_symtab *);
  166. extern int blockvector_contains_pc (const struct blockvector *bv, CORE_ADDR pc);
  167. extern struct call_site *call_site_for_pc (struct gdbarch *gdbarch,
  168. CORE_ADDR pc);
  169. extern const struct block *block_for_pc (CORE_ADDR);
  170. extern const struct block *block_for_pc_sect (CORE_ADDR, struct obj_section *);
  171. extern const char *block_scope (const struct block *block);
  172. extern void block_set_scope (struct block *block, const char *scope,
  173. struct obstack *obstack);
  174. extern struct using_direct *block_using (const struct block *block);
  175. extern void block_set_using (struct block *block,
  176. struct using_direct *using_decl,
  177. struct obstack *obstack);
  178. extern const struct block *block_static_block (const struct block *block);
  179. extern const struct block *block_global_block (const struct block *block);
  180. extern struct block *allocate_block (struct obstack *obstack);
  181. extern struct block *allocate_global_block (struct obstack *obstack);
  182. extern void set_block_compunit_symtab (struct block *,
  183. struct compunit_symtab *);
  184. /* Return a property to evaluate the static link associated to BLOCK.
  185. In the context of nested functions (available in Pascal, Ada and GNU C, for
  186. instance), a static link (as in DWARF's DW_AT_static_link attribute) for a
  187. function is a way to get the frame corresponding to the enclosing function.
  188. Note that only objfile-owned and function-level blocks can have a static
  189. link. Return NULL if there is no such property. */
  190. extern struct dynamic_prop *block_static_link (const struct block *block);
  191. /* A block iterator. This structure should be treated as though it
  192. were opaque; it is only defined here because we want to support
  193. stack allocation of iterators. */
  194. struct block_iterator
  195. {
  196. /* If we're iterating over a single block, this holds the block.
  197. Otherwise, it holds the canonical compunit. */
  198. union
  199. {
  200. struct compunit_symtab *compunit_symtab;
  201. const struct block *block;
  202. } d;
  203. /* If we're iterating over a single block, this is always -1.
  204. Otherwise, it holds the index of the current "included" symtab in
  205. the canonical symtab (that is, d.symtab->includes[idx]), with -1
  206. meaning the canonical symtab itself. */
  207. int idx;
  208. /* Which block, either static or global, to iterate over. If this
  209. is FIRST_LOCAL_BLOCK, then we are iterating over a single block.
  210. This is used to select which field of 'd' is in use. */
  211. enum block_enum which;
  212. /* The underlying multidictionary iterator. */
  213. struct mdict_iterator mdict_iter;
  214. };
  215. /* Initialize ITERATOR to point at the first symbol in BLOCK, and
  216. return that first symbol, or NULL if BLOCK is empty. */
  217. extern struct symbol *block_iterator_first (const struct block *block,
  218. struct block_iterator *iterator);
  219. /* Advance ITERATOR, and return the next symbol, or NULL if there are
  220. no more symbols. Don't call this if you've previously received
  221. NULL from block_iterator_first or block_iterator_next on this
  222. iteration. */
  223. extern struct symbol *block_iterator_next (struct block_iterator *iterator);
  224. /* Initialize ITERATOR to point at the first symbol in BLOCK whose
  225. search_name () matches NAME, and return that first symbol, or
  226. NULL if there are no such symbols. */
  227. extern struct symbol *block_iter_match_first (const struct block *block,
  228. const lookup_name_info &name,
  229. struct block_iterator *iterator);
  230. /* Advance ITERATOR to point at the next symbol in BLOCK whose
  231. search_name () matches NAME, or NULL if there are no more such
  232. symbols. Don't call this if you've previously received NULL from
  233. block_iterator_match_first or block_iterator_match_next on this
  234. iteration. And don't call it unless ITERATOR was created by a
  235. previous call to block_iter_match_first with the same NAME. */
  236. extern struct symbol *block_iter_match_next
  237. (const lookup_name_info &name, struct block_iterator *iterator);
  238. /* Return true if symbol A is the best match possible for DOMAIN. */
  239. extern bool best_symbol (struct symbol *a, const domain_enum domain);
  240. /* Return symbol B if it is a better match than symbol A for DOMAIN.
  241. Otherwise return A. */
  242. extern struct symbol *better_symbol (struct symbol *a, struct symbol *b,
  243. const domain_enum domain);
  244. /* Search BLOCK for symbol NAME in DOMAIN. */
  245. extern struct symbol *block_lookup_symbol (const struct block *block,
  246. const char *name,
  247. symbol_name_match_type match_type,
  248. const domain_enum domain);
  249. /* Search BLOCK for symbol NAME in DOMAIN but only in primary symbol table of
  250. BLOCK. BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. Function is useful if
  251. one iterates all global/static blocks of an objfile. */
  252. extern struct symbol *block_lookup_symbol_primary (const struct block *block,
  253. const char *name,
  254. const domain_enum domain);
  255. /* The type of the MATCHER argument to block_find_symbol. */
  256. typedef int (block_symbol_matcher_ftype) (struct symbol *, void *);
  257. /* Find symbol NAME in BLOCK and in DOMAIN that satisfies MATCHER.
  258. DATA is passed unchanged to MATCHER.
  259. BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. */
  260. extern struct symbol *block_find_symbol (const struct block *block,
  261. const char *name,
  262. const domain_enum domain,
  263. block_symbol_matcher_ftype *matcher,
  264. void *data);
  265. /* A matcher function for block_find_symbol to find only symbols with
  266. non-opaque types. */
  267. extern int block_find_non_opaque_type (struct symbol *sym, void *data);
  268. /* A matcher function for block_find_symbol to prefer symbols with
  269. non-opaque types. The way to use this function is as follows:
  270. struct symbol *with_opaque = NULL;
  271. struct symbol *sym
  272. = block_find_symbol (block, name, domain,
  273. block_find_non_opaque_type_preferred, &with_opaque);
  274. At this point if SYM is non-NULL then a non-opaque type has been found.
  275. Otherwise, if WITH_OPAQUE is non-NULL then an opaque type has been found.
  276. Otherwise, the symbol was not found. */
  277. extern int block_find_non_opaque_type_preferred (struct symbol *sym,
  278. void *data);
  279. /* Macro to loop through all symbols in BLOCK, in no particular
  280. order. ITER helps keep track of the iteration, and must be a
  281. struct block_iterator. SYM points to the current symbol. */
  282. #define ALL_BLOCK_SYMBOLS(block, iter, sym) \
  283. for ((sym) = block_iterator_first ((block), &(iter)); \
  284. (sym); \
  285. (sym) = block_iterator_next (&(iter)))
  286. /* Macro to loop through all symbols in BLOCK with a name that matches
  287. NAME, in no particular order. ITER helps keep track of the
  288. iteration, and must be a struct block_iterator. SYM points to the
  289. current symbol. */
  290. #define ALL_BLOCK_SYMBOLS_WITH_NAME(block, name, iter, sym) \
  291. for ((sym) = block_iter_match_first ((block), (name), &(iter)); \
  292. (sym) != NULL; \
  293. (sym) = block_iter_match_next ((name), &(iter)))
  294. /* Given a vector of pairs, allocate and build an obstack allocated
  295. blockranges struct for a block. */
  296. struct blockranges *make_blockranges (struct objfile *objfile,
  297. const std::vector<blockrange> &rangevec);
  298. #endif /* BLOCK_H */