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