]>
Commit | Line | Data |
---|---|---|
fe898f56 DC |
1 | /* Block-related functions for the GNU debugger, GDB. |
2 | ||
213516ef | 3 | Copyright (C) 2003-2023 Free Software Foundation, Inc. |
fe898f56 DC |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
fe898f56 DC |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
fe898f56 DC |
19 | |
20 | #include "defs.h" | |
21 | #include "block.h" | |
4de283e4 TT |
22 | #include "symtab.h" |
23 | #include "symfile.h" | |
bf31fd38 | 24 | #include "gdbsupport/gdb_obstack.h" |
4de283e4 TT |
25 | #include "cp-support.h" |
26 | #include "addrmap.h" | |
8e3b41a9 | 27 | #include "gdbtypes.h" |
1994afbf | 28 | #include "objfiles.h" |
9219021c DC |
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 | ||
fd90ace4 | 34 | struct block_namespace_info : public allocate_on_obstack |
9219021c | 35 | { |
fd90ace4 YQ |
36 | const char *scope = nullptr; |
37 | struct using_direct *using_decl = nullptr; | |
9219021c DC |
38 | }; |
39 | ||
40 | static void block_initialize_namespace (struct block *block, | |
41 | struct obstack *obstack); | |
fe898f56 | 42 | |
1994afbf DE |
43 | /* See block.h. */ |
44 | ||
45 | struct objfile * | |
46 | block_objfile (const struct block *block) | |
47 | { | |
48 | const struct global_block *global_block; | |
49 | ||
6c00f721 SM |
50 | if (block->function () != nullptr) |
51 | return block->function ()->objfile (); | |
1994afbf DE |
52 | |
53 | global_block = (struct global_block *) block_global_block (block); | |
9821f3fa | 54 | return global_block->compunit_symtab->objfile (); |
1994afbf DE |
55 | } |
56 | ||
57 | /* See block. */ | |
58 | ||
59 | struct gdbarch * | |
60 | block_gdbarch (const struct block *block) | |
61 | { | |
6c00f721 SM |
62 | if (block->function () != nullptr) |
63 | return block->function ()->arch (); | |
1994afbf | 64 | |
08feed99 | 65 | return block_objfile (block)->arch (); |
1994afbf DE |
66 | } |
67 | ||
f21c2bd7 | 68 | /* See block.h. */ |
fe898f56 | 69 | |
f21c2bd7 TT |
70 | bool |
71 | contained_in (const struct block *a, const struct block *b, | |
72 | bool allow_nested) | |
fe898f56 DC |
73 | { |
74 | if (!a || !b) | |
f21c2bd7 | 75 | return false; |
edb3359d DJ |
76 | |
77 | do | |
78 | { | |
79 | if (a == b) | |
f21c2bd7 TT |
80 | return true; |
81 | /* If A is a function block, then A cannot be contained in B, | |
dda83cd7 | 82 | except if A was inlined. */ |
6c00f721 | 83 | if (!allow_nested && a->function () != NULL && !block_inlined_p (a)) |
dda83cd7 | 84 | return false; |
f135fe72 | 85 | a = a->superblock (); |
edb3359d DJ |
86 | } |
87 | while (a != NULL); | |
88 | ||
d4207696 | 89 | return false; |
fe898f56 DC |
90 | } |
91 | ||
92 | ||
93 | /* Return the symbol for the function which contains a specified | |
7f0df278 DJ |
94 | lexical block, described by a struct block BL. The return value |
95 | will not be an inlined function; the containing function will be | |
96 | returned instead. */ | |
fe898f56 DC |
97 | |
98 | struct symbol * | |
7f0df278 | 99 | block_linkage_function (const struct block *bl) |
fe898f56 | 100 | { |
6c00f721 | 101 | while ((bl->function () == NULL || block_inlined_p (bl)) |
f135fe72 SM |
102 | && bl->superblock () != NULL) |
103 | bl = bl->superblock (); | |
fe898f56 | 104 | |
6c00f721 | 105 | return bl->function (); |
fe898f56 DC |
106 | } |
107 | ||
f8eba3c6 TT |
108 | /* Return the symbol for the function which contains a specified |
109 | block, described by a struct block BL. The return value will be | |
110 | the closest enclosing function, which might be an inline | |
111 | function. */ | |
112 | ||
113 | struct symbol * | |
114 | block_containing_function (const struct block *bl) | |
115 | { | |
f135fe72 SM |
116 | while (bl->function () == NULL && bl->superblock () != NULL) |
117 | bl = bl->superblock (); | |
f8eba3c6 | 118 | |
6c00f721 | 119 | return bl->function (); |
f8eba3c6 TT |
120 | } |
121 | ||
edb3359d DJ |
122 | /* Return one if BL represents an inlined function. */ |
123 | ||
124 | int | |
125 | block_inlined_p (const struct block *bl) | |
126 | { | |
6c00f721 | 127 | return bl->function () != NULL && bl->function ()->is_inlined (); |
edb3359d DJ |
128 | } |
129 | ||
9703b513 TT |
130 | /* A helper function that checks whether PC is in the blockvector BL. |
131 | It returns the containing block if there is one, or else NULL. */ | |
fe898f56 | 132 | |
582942f4 | 133 | static const struct block * |
346d1dfe | 134 | find_block_in_blockvector (const struct blockvector *bl, CORE_ADDR pc) |
fe898f56 | 135 | { |
582942f4 | 136 | const struct block *b; |
b59661bd | 137 | int bot, top, half; |
fe898f56 | 138 | |
801e3a5b JB |
139 | /* If we have an addrmap mapping code addresses to blocks, then use |
140 | that. */ | |
769520b7 TT |
141 | if (bl->map ()) |
142 | return (const struct block *) bl->map ()->find (pc); | |
801e3a5b JB |
143 | |
144 | /* Otherwise, use binary search to find the last block that starts | |
6ac9ef80 DE |
145 | before PC. |
146 | Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1. | |
147 | They both have the same START,END values. | |
148 | Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the | |
149 | fact that this choice was made was subtle, now we make it explicit. */ | |
63d609de | 150 | gdb_assert (bl->blocks ().size () >= 2); |
6ac9ef80 | 151 | bot = STATIC_BLOCK; |
63d609de | 152 | top = bl->blocks ().size (); |
fe898f56 DC |
153 | |
154 | while (top - bot > 1) | |
155 | { | |
156 | half = (top - bot + 1) >> 1; | |
63d609de | 157 | b = bl->block (bot + half); |
4b8791e1 | 158 | if (b->start () <= pc) |
fe898f56 DC |
159 | bot += half; |
160 | else | |
161 | top = bot + half; | |
162 | } | |
163 | ||
164 | /* Now search backward for a block that ends after PC. */ | |
165 | ||
6ac9ef80 | 166 | while (bot >= STATIC_BLOCK) |
fe898f56 | 167 | { |
63d609de | 168 | b = bl->block (bot); |
4b8791e1 | 169 | if (!(b->start () <= pc)) |
5fb4027f | 170 | return NULL; |
4b8791e1 | 171 | if (b->end () > pc) |
9703b513 | 172 | return b; |
fe898f56 DC |
173 | bot--; |
174 | } | |
9703b513 TT |
175 | |
176 | return NULL; | |
177 | } | |
178 | ||
179 | /* Return the blockvector immediately containing the innermost lexical | |
180 | block containing the specified pc value and section, or 0 if there | |
181 | is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we | |
182 | don't pass this information back to the caller. */ | |
183 | ||
346d1dfe | 184 | const struct blockvector * |
9703b513 | 185 | blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section, |
43f3e411 DE |
186 | const struct block **pblock, |
187 | struct compunit_symtab *cust) | |
9703b513 | 188 | { |
346d1dfe | 189 | const struct blockvector *bl; |
582942f4 | 190 | const struct block *b; |
9703b513 | 191 | |
43f3e411 | 192 | if (cust == NULL) |
9703b513 TT |
193 | { |
194 | /* First search all symtabs for one whose file contains our pc */ | |
43f3e411 DE |
195 | cust = find_pc_sect_compunit_symtab (pc, section); |
196 | if (cust == NULL) | |
9703b513 TT |
197 | return 0; |
198 | } | |
199 | ||
af39c5c8 | 200 | bl = cust->blockvector (); |
9703b513 TT |
201 | |
202 | /* Then search that symtab for the smallest block that wins. */ | |
203 | b = find_block_in_blockvector (bl, pc); | |
204 | if (b == NULL) | |
205 | return NULL; | |
206 | ||
207 | if (pblock) | |
208 | *pblock = b; | |
209 | return bl; | |
210 | } | |
211 | ||
212 | /* Return true if the blockvector BV contains PC, false otherwise. */ | |
213 | ||
214 | int | |
346d1dfe | 215 | blockvector_contains_pc (const struct blockvector *bv, CORE_ADDR pc) |
9703b513 TT |
216 | { |
217 | return find_block_in_blockvector (bv, pc) != NULL; | |
fe898f56 DC |
218 | } |
219 | ||
8e3b41a9 JK |
220 | /* Return call_site for specified PC in GDBARCH. PC must match exactly, it |
221 | must be the next instruction after call (or after tail call jump). Throw | |
222 | NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */ | |
223 | ||
224 | struct call_site * | |
225 | call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc) | |
226 | { | |
43f3e411 | 227 | struct compunit_symtab *cust; |
b625c770 | 228 | call_site *cs = nullptr; |
8e3b41a9 JK |
229 | |
230 | /* -1 as tail call PC can be already after the compilation unit range. */ | |
43f3e411 | 231 | cust = find_pc_compunit_symtab (pc - 1); |
8e3b41a9 | 232 | |
b625c770 SM |
233 | if (cust != nullptr) |
234 | cs = cust->find_call_site (pc); | |
8e3b41a9 | 235 | |
b625c770 | 236 | if (cs == nullptr) |
8e3b41a9 | 237 | { |
7cbd4a93 | 238 | struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc); |
8e3b41a9 JK |
239 | |
240 | /* DW_TAG_gnu_call_site will be missing just if GCC could not determine | |
241 | the call target. */ | |
242 | throw_error (NO_ENTRY_VALUE_ERROR, | |
216f72a1 JK |
243 | _("DW_OP_entry_value resolving cannot find " |
244 | "DW_TAG_call_site %s in %s"), | |
8e3b41a9 | 245 | paddress (gdbarch, pc), |
7cbd4a93 | 246 | (msym.minsym == NULL ? "???" |
c9d95fa3 | 247 | : msym.minsym->print_name ())); |
8e3b41a9 JK |
248 | } |
249 | ||
b625c770 | 250 | return cs; |
8e3b41a9 JK |
251 | } |
252 | ||
fe898f56 DC |
253 | /* Return the blockvector immediately containing the innermost lexical block |
254 | containing the specified pc value, or 0 if there is none. | |
255 | Backward compatibility, no section. */ | |
256 | ||
346d1dfe | 257 | const struct blockvector * |
3977b71f | 258 | blockvector_for_pc (CORE_ADDR pc, const struct block **pblock) |
fe898f56 DC |
259 | { |
260 | return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc), | |
801e3a5b | 261 | pblock, NULL); |
fe898f56 DC |
262 | } |
263 | ||
264 | /* Return the innermost lexical block containing the specified pc value | |
265 | in the specified section, or 0 if there is none. */ | |
266 | ||
3977b71f | 267 | const struct block * |
714835d5 | 268 | block_for_pc_sect (CORE_ADDR pc, struct obj_section *section) |
fe898f56 | 269 | { |
346d1dfe | 270 | const struct blockvector *bl; |
3977b71f | 271 | const struct block *b; |
fe898f56 | 272 | |
801e3a5b | 273 | bl = blockvector_for_pc_sect (pc, section, &b, NULL); |
fe898f56 | 274 | if (bl) |
801e3a5b | 275 | return b; |
fe898f56 DC |
276 | return 0; |
277 | } | |
278 | ||
279 | /* Return the innermost lexical block containing the specified pc value, | |
280 | or 0 if there is none. Backward compatibility, no section. */ | |
281 | ||
3977b71f | 282 | const struct block * |
b59661bd | 283 | block_for_pc (CORE_ADDR pc) |
fe898f56 DC |
284 | { |
285 | return block_for_pc_sect (pc, find_pc_mapped_section (pc)); | |
286 | } | |
9219021c | 287 | |
1fcb5155 DC |
288 | /* Now come some functions designed to deal with C++ namespace issues. |
289 | The accessors are safe to use even in the non-C++ case. */ | |
290 | ||
291 | /* This returns the namespace that BLOCK is enclosed in, or "" if it | |
292 | isn't enclosed in a namespace at all. This travels the chain of | |
293 | superblocks looking for a scope, if necessary. */ | |
294 | ||
295 | const char * | |
296 | block_scope (const struct block *block) | |
297 | { | |
f135fe72 | 298 | for (; block != NULL; block = block->superblock ()) |
1fcb5155 | 299 | { |
3fe38936 SM |
300 | if (block->namespace_info () != NULL |
301 | && block->namespace_info ()->scope != NULL) | |
302 | return block->namespace_info ()->scope; | |
1fcb5155 DC |
303 | } |
304 | ||
305 | return ""; | |
306 | } | |
9219021c DC |
307 | |
308 | /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via | |
309 | OBSTACK. (It won't make a copy of SCOPE, however, so that already | |
310 | has to be allocated correctly.) */ | |
311 | ||
312 | void | |
313 | block_set_scope (struct block *block, const char *scope, | |
314 | struct obstack *obstack) | |
315 | { | |
316 | block_initialize_namespace (block, obstack); | |
317 | ||
3fe38936 | 318 | block->namespace_info ()->scope = scope; |
9219021c DC |
319 | } |
320 | ||
27aa8d6a | 321 | /* This returns the using directives list associated with BLOCK, if |
1fcb5155 DC |
322 | any. */ |
323 | ||
1fcb5155 DC |
324 | struct using_direct * |
325 | block_using (const struct block *block) | |
326 | { | |
3fe38936 | 327 | if (block == NULL || block->namespace_info () == NULL) |
1fcb5155 DC |
328 | return NULL; |
329 | else | |
3fe38936 | 330 | return block->namespace_info ()->using_decl; |
1fcb5155 DC |
331 | } |
332 | ||
9219021c DC |
333 | /* Set BLOCK's using member to USING; if needed, allocate memory via |
334 | OBSTACK. (It won't make a copy of USING, however, so that already | |
335 | has to be allocated correctly.) */ | |
336 | ||
337 | void | |
338 | block_set_using (struct block *block, | |
fe978cb0 | 339 | struct using_direct *using_decl, |
9219021c DC |
340 | struct obstack *obstack) |
341 | { | |
342 | block_initialize_namespace (block, obstack); | |
343 | ||
3fe38936 | 344 | block->namespace_info ()->using_decl = using_decl; |
9219021c DC |
345 | } |
346 | ||
3fe38936 | 347 | /* If block->namespace_info () is NULL, allocate it via OBSTACK and |
30baf67b | 348 | initialize its members to zero. */ |
9219021c DC |
349 | |
350 | static void | |
351 | block_initialize_namespace (struct block *block, struct obstack *obstack) | |
352 | { | |
3fe38936 SM |
353 | if (block->namespace_info () == NULL) |
354 | block->set_namespace_info (new (obstack) struct block_namespace_info ()); | |
9219021c | 355 | } |
89a9d1b1 DC |
356 | |
357 | /* Return the static block associated to BLOCK. Return NULL if block | |
358 | is NULL or if block is a global block. */ | |
359 | ||
360 | const struct block * | |
361 | block_static_block (const struct block *block) | |
362 | { | |
f135fe72 | 363 | if (block == NULL || block->superblock () == NULL) |
89a9d1b1 DC |
364 | return NULL; |
365 | ||
f135fe72 SM |
366 | while (block->superblock ()->superblock () != NULL) |
367 | block = block->superblock (); | |
89a9d1b1 DC |
368 | |
369 | return block; | |
370 | } | |
1fcb5155 DC |
371 | |
372 | /* Return the static block associated to BLOCK. Return NULL if block | |
373 | is NULL. */ | |
374 | ||
375 | const struct block * | |
376 | block_global_block (const struct block *block) | |
377 | { | |
378 | if (block == NULL) | |
379 | return NULL; | |
380 | ||
f135fe72 SM |
381 | while (block->superblock () != NULL) |
382 | block = block->superblock (); | |
1fcb5155 DC |
383 | |
384 | return block; | |
385 | } | |
5c4e30ca DC |
386 | |
387 | /* Allocate a block on OBSTACK, and initialize its elements to | |
388 | zero/NULL. This is useful for creating "dummy" blocks that don't | |
389 | correspond to actual source files. | |
390 | ||
b026f593 | 391 | Warning: it sets the block's BLOCK_MULTIDICT to NULL, which isn't a |
5c4e30ca | 392 | valid value. If you really don't want the block to have a |
b026f593 | 393 | dictionary, then you should subsequently set its BLOCK_MULTIDICT to |
5c4e30ca DC |
394 | dict_create_linear (obstack, NULL). */ |
395 | ||
396 | struct block * | |
397 | allocate_block (struct obstack *obstack) | |
398 | { | |
4c35218e | 399 | struct block *bl = OBSTACK_ZALLOC (obstack, struct block); |
5c4e30ca DC |
400 | |
401 | return bl; | |
402 | } | |
8157b174 | 403 | |
84a146c9 TT |
404 | /* Allocate a global block. */ |
405 | ||
406 | struct block * | |
407 | allocate_global_block (struct obstack *obstack) | |
408 | { | |
409 | struct global_block *bl = OBSTACK_ZALLOC (obstack, struct global_block); | |
410 | ||
411 | return &bl->block; | |
412 | } | |
413 | ||
43f3e411 | 414 | /* Set the compunit of the global block. */ |
84a146c9 TT |
415 | |
416 | void | |
43f3e411 | 417 | set_block_compunit_symtab (struct block *block, struct compunit_symtab *cu) |
84a146c9 TT |
418 | { |
419 | struct global_block *gb; | |
420 | ||
f135fe72 | 421 | gdb_assert (block->superblock () == NULL); |
84a146c9 | 422 | gb = (struct global_block *) block; |
43f3e411 DE |
423 | gdb_assert (gb->compunit_symtab == NULL); |
424 | gb->compunit_symtab = cu; | |
84a146c9 TT |
425 | } |
426 | ||
63e43d3a PMR |
427 | /* See block.h. */ |
428 | ||
429 | struct dynamic_prop * | |
430 | block_static_link (const struct block *block) | |
431 | { | |
432 | struct objfile *objfile = block_objfile (block); | |
433 | ||
434 | /* Only objfile-owned blocks that materialize top function scopes can have | |
435 | static links. */ | |
6c00f721 | 436 | if (objfile == NULL || block->function () == NULL) |
63e43d3a PMR |
437 | return NULL; |
438 | ||
439 | return (struct dynamic_prop *) objfile_lookup_static_link (objfile, block); | |
440 | } | |
441 | ||
43f3e411 | 442 | /* Return the compunit of the global block. */ |
b5b04b5b | 443 | |
43f3e411 DE |
444 | static struct compunit_symtab * |
445 | get_block_compunit_symtab (const struct block *block) | |
b5b04b5b TT |
446 | { |
447 | struct global_block *gb; | |
448 | ||
f135fe72 | 449 | gdb_assert (block->superblock () == NULL); |
b5b04b5b | 450 | gb = (struct global_block *) block; |
43f3e411 DE |
451 | gdb_assert (gb->compunit_symtab != NULL); |
452 | return gb->compunit_symtab; | |
b5b04b5b TT |
453 | } |
454 | ||
8157b174 TT |
455 | \f |
456 | ||
b5b04b5b TT |
457 | /* Initialize a block iterator, either to iterate over a single block, |
458 | or, for static and global blocks, all the included symtabs as | |
459 | well. */ | |
460 | ||
461 | static void | |
462 | initialize_block_iterator (const struct block *block, | |
463 | struct block_iterator *iter) | |
464 | { | |
465 | enum block_enum which; | |
43f3e411 | 466 | struct compunit_symtab *cu; |
b5b04b5b TT |
467 | |
468 | iter->idx = -1; | |
469 | ||
f135fe72 | 470 | if (block->superblock () == NULL) |
b5b04b5b TT |
471 | { |
472 | which = GLOBAL_BLOCK; | |
43f3e411 | 473 | cu = get_block_compunit_symtab (block); |
b5b04b5b | 474 | } |
f135fe72 | 475 | else if (block->superblock ()->superblock () == NULL) |
b5b04b5b TT |
476 | { |
477 | which = STATIC_BLOCK; | |
f135fe72 | 478 | cu = get_block_compunit_symtab (block->superblock ()); |
b5b04b5b TT |
479 | } |
480 | else | |
481 | { | |
482 | iter->d.block = block; | |
483 | /* A signal value meaning that we're iterating over a single | |
484 | block. */ | |
485 | iter->which = FIRST_LOCAL_BLOCK; | |
486 | return; | |
487 | } | |
488 | ||
489 | /* If this is an included symtab, find the canonical includer and | |
490 | use it instead. */ | |
43f3e411 DE |
491 | while (cu->user != NULL) |
492 | cu = cu->user; | |
b5b04b5b TT |
493 | |
494 | /* Putting this check here simplifies the logic of the iterator | |
495 | functions. If there are no included symtabs, we only need to | |
496 | search a single block, so we might as well just do that | |
497 | directly. */ | |
43f3e411 | 498 | if (cu->includes == NULL) |
b5b04b5b TT |
499 | { |
500 | iter->d.block = block; | |
501 | /* A signal value meaning that we're iterating over a single | |
502 | block. */ | |
503 | iter->which = FIRST_LOCAL_BLOCK; | |
504 | } | |
505 | else | |
506 | { | |
43f3e411 | 507 | iter->d.compunit_symtab = cu; |
b5b04b5b TT |
508 | iter->which = which; |
509 | } | |
510 | } | |
511 | ||
43f3e411 | 512 | /* A helper function that finds the current compunit over whose static |
b5b04b5b TT |
513 | or global block we should iterate. */ |
514 | ||
43f3e411 DE |
515 | static struct compunit_symtab * |
516 | find_iterator_compunit_symtab (struct block_iterator *iterator) | |
b5b04b5b TT |
517 | { |
518 | if (iterator->idx == -1) | |
43f3e411 DE |
519 | return iterator->d.compunit_symtab; |
520 | return iterator->d.compunit_symtab->includes[iterator->idx]; | |
b5b04b5b TT |
521 | } |
522 | ||
523 | /* Perform a single step for a plain block iterator, iterating across | |
524 | symbol tables as needed. Returns the next symbol, or NULL when | |
525 | iteration is complete. */ | |
526 | ||
527 | static struct symbol * | |
528 | block_iterator_step (struct block_iterator *iterator, int first) | |
529 | { | |
530 | struct symbol *sym; | |
531 | ||
532 | gdb_assert (iterator->which != FIRST_LOCAL_BLOCK); | |
533 | ||
534 | while (1) | |
535 | { | |
536 | if (first) | |
537 | { | |
43f3e411 DE |
538 | struct compunit_symtab *cust |
539 | = find_iterator_compunit_symtab (iterator); | |
b5b04b5b TT |
540 | const struct block *block; |
541 | ||
542 | /* Iteration is complete. */ | |
43f3e411 | 543 | if (cust == NULL) |
b5b04b5b TT |
544 | return NULL; |
545 | ||
63d609de | 546 | block = cust->blockvector ()->block (iterator->which); |
24d74bb5 | 547 | sym = mdict_iterator_first (block->multidict (), |
b026f593 | 548 | &iterator->mdict_iter); |
b5b04b5b TT |
549 | } |
550 | else | |
b026f593 | 551 | sym = mdict_iterator_next (&iterator->mdict_iter); |
b5b04b5b TT |
552 | |
553 | if (sym != NULL) | |
554 | return sym; | |
555 | ||
556 | /* We have finished iterating the appropriate block of one | |
557 | symtab. Now advance to the next symtab and begin iteration | |
558 | there. */ | |
559 | ++iterator->idx; | |
560 | first = 1; | |
561 | } | |
562 | } | |
563 | ||
8157b174 TT |
564 | /* See block.h. */ |
565 | ||
566 | struct symbol * | |
567 | block_iterator_first (const struct block *block, | |
568 | struct block_iterator *iterator) | |
569 | { | |
b5b04b5b TT |
570 | initialize_block_iterator (block, iterator); |
571 | ||
572 | if (iterator->which == FIRST_LOCAL_BLOCK) | |
24d74bb5 | 573 | return mdict_iterator_first (block->multidict (), &iterator->mdict_iter); |
b5b04b5b TT |
574 | |
575 | return block_iterator_step (iterator, 1); | |
8157b174 TT |
576 | } |
577 | ||
578 | /* See block.h. */ | |
579 | ||
580 | struct symbol * | |
581 | block_iterator_next (struct block_iterator *iterator) | |
582 | { | |
b5b04b5b | 583 | if (iterator->which == FIRST_LOCAL_BLOCK) |
b026f593 | 584 | return mdict_iterator_next (&iterator->mdict_iter); |
b5b04b5b TT |
585 | |
586 | return block_iterator_step (iterator, 0); | |
587 | } | |
588 | ||
b5b04b5b TT |
589 | /* Perform a single step for a "match" block iterator, iterating |
590 | across symbol tables as needed. Returns the next symbol, or NULL | |
591 | when iteration is complete. */ | |
592 | ||
593 | static struct symbol * | |
594 | block_iter_match_step (struct block_iterator *iterator, | |
b5ec771e | 595 | const lookup_name_info &name, |
b5b04b5b TT |
596 | int first) |
597 | { | |
598 | struct symbol *sym; | |
599 | ||
600 | gdb_assert (iterator->which != FIRST_LOCAL_BLOCK); | |
601 | ||
602 | while (1) | |
603 | { | |
604 | if (first) | |
605 | { | |
43f3e411 DE |
606 | struct compunit_symtab *cust |
607 | = find_iterator_compunit_symtab (iterator); | |
b5b04b5b TT |
608 | const struct block *block; |
609 | ||
610 | /* Iteration is complete. */ | |
43f3e411 | 611 | if (cust == NULL) |
b5b04b5b TT |
612 | return NULL; |
613 | ||
63d609de | 614 | block = cust->blockvector ()->block (iterator->which); |
24d74bb5 | 615 | sym = mdict_iter_match_first (block->multidict (), name, |
b026f593 | 616 | &iterator->mdict_iter); |
b5b04b5b TT |
617 | } |
618 | else | |
b026f593 | 619 | sym = mdict_iter_match_next (name, &iterator->mdict_iter); |
b5b04b5b TT |
620 | |
621 | if (sym != NULL) | |
622 | return sym; | |
623 | ||
624 | /* We have finished iterating the appropriate block of one | |
625 | symtab. Now advance to the next symtab and begin iteration | |
626 | there. */ | |
627 | ++iterator->idx; | |
628 | first = 1; | |
629 | } | |
8157b174 TT |
630 | } |
631 | ||
632 | /* See block.h. */ | |
633 | ||
634 | struct symbol * | |
635 | block_iter_match_first (const struct block *block, | |
b5ec771e | 636 | const lookup_name_info &name, |
8157b174 TT |
637 | struct block_iterator *iterator) |
638 | { | |
b5b04b5b TT |
639 | initialize_block_iterator (block, iterator); |
640 | ||
641 | if (iterator->which == FIRST_LOCAL_BLOCK) | |
24d74bb5 | 642 | return mdict_iter_match_first (block->multidict (), name, |
b026f593 | 643 | &iterator->mdict_iter); |
b5b04b5b | 644 | |
b5ec771e | 645 | return block_iter_match_step (iterator, name, 1); |
8157b174 TT |
646 | } |
647 | ||
648 | /* See block.h. */ | |
649 | ||
650 | struct symbol * | |
b5ec771e | 651 | block_iter_match_next (const lookup_name_info &name, |
8157b174 TT |
652 | struct block_iterator *iterator) |
653 | { | |
b5b04b5b | 654 | if (iterator->which == FIRST_LOCAL_BLOCK) |
b026f593 | 655 | return mdict_iter_match_next (name, &iterator->mdict_iter); |
b5b04b5b | 656 | |
b5ec771e | 657 | return block_iter_match_step (iterator, name, 0); |
8157b174 | 658 | } |
16b2eaa1 | 659 | |
de82891c | 660 | /* See block.h. */ |
93e55f0a | 661 | |
de82891c | 662 | bool |
93e55f0a TV |
663 | best_symbol (struct symbol *a, const domain_enum domain) |
664 | { | |
6c9c307c | 665 | return (a->domain () == domain |
66d7f48f | 666 | && a->aclass () != LOC_UNRESOLVED); |
93e55f0a TV |
667 | } |
668 | ||
de82891c | 669 | /* See block.h. */ |
93e55f0a | 670 | |
de82891c | 671 | struct symbol * |
93e55f0a TV |
672 | better_symbol (struct symbol *a, struct symbol *b, const domain_enum domain) |
673 | { | |
674 | if (a == NULL) | |
675 | return b; | |
676 | if (b == NULL) | |
677 | return a; | |
678 | ||
6c9c307c | 679 | if (a->domain () == domain && b->domain () != domain) |
93e55f0a | 680 | return a; |
6c9c307c SM |
681 | |
682 | if (b->domain () == domain && a->domain () != domain) | |
93e55f0a TV |
683 | return b; |
684 | ||
66d7f48f | 685 | if (a->aclass () != LOC_UNRESOLVED && b->aclass () == LOC_UNRESOLVED) |
93e55f0a | 686 | return a; |
66d7f48f SM |
687 | |
688 | if (b->aclass () != LOC_UNRESOLVED && a->aclass () == LOC_UNRESOLVED) | |
93e55f0a TV |
689 | return b; |
690 | ||
691 | return a; | |
692 | } | |
693 | ||
16b2eaa1 DE |
694 | /* See block.h. |
695 | ||
696 | Note that if NAME is the demangled form of a C++ symbol, we will fail | |
697 | to find a match during the binary search of the non-encoded names, but | |
698 | for now we don't worry about the slight inefficiency of looking for | |
699 | a match we'll never find, since it will go pretty quick. Once the | |
700 | binary search terminates, we drop through and do a straight linear | |
701 | search on the symbols. Each symbol which is marked as being a ObjC/C++ | |
702 | symbol (language_cplus or language_objc set) has both the encoded and | |
703 | non-encoded names tested for a match. */ | |
704 | ||
705 | struct symbol * | |
706 | block_lookup_symbol (const struct block *block, const char *name, | |
de63c46b | 707 | symbol_name_match_type match_type, |
16b2eaa1 DE |
708 | const domain_enum domain) |
709 | { | |
710 | struct block_iterator iter; | |
711 | struct symbol *sym; | |
712 | ||
de63c46b | 713 | lookup_name_info lookup_name (name, match_type); |
b5ec771e | 714 | |
6c00f721 | 715 | if (!block->function ()) |
16b2eaa1 | 716 | { |
ee93cd5e KS |
717 | struct symbol *other = NULL; |
718 | ||
b5ec771e | 719 | ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym) |
16b2eaa1 | 720 | { |
93e55f0a TV |
721 | /* See comment related to PR gcc/debug/91507 in |
722 | block_lookup_symbol_primary. */ | |
723 | if (best_symbol (sym, domain)) | |
ee93cd5e KS |
724 | return sym; |
725 | /* This is a bit of a hack, but symbol_matches_domain might ignore | |
726 | STRUCT vs VAR domain symbols. So if a matching symbol is found, | |
727 | make sure there is no "better" matching symbol, i.e., one with | |
728 | exactly the same domain. PR 16253. */ | |
c1b5c1eb | 729 | if (symbol_matches_domain (sym->language (), |
6c9c307c | 730 | sym->domain (), domain)) |
93e55f0a | 731 | other = better_symbol (other, sym, domain); |
16b2eaa1 | 732 | } |
ee93cd5e | 733 | return other; |
16b2eaa1 DE |
734 | } |
735 | else | |
736 | { | |
737 | /* Note that parameter symbols do not always show up last in the | |
738 | list; this loop makes sure to take anything else other than | |
739 | parameter symbols first; it only uses parameter symbols as a | |
740 | last resort. Note that this only takes up extra computation | |
ee93cd5e KS |
741 | time on a match. |
742 | It's hard to define types in the parameter list (at least in | |
743 | C/C++) so we don't do the same PR 16253 hack here that is done | |
744 | for the !BLOCK_FUNCTION case. */ | |
16b2eaa1 DE |
745 | |
746 | struct symbol *sym_found = NULL; | |
747 | ||
b5ec771e | 748 | ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym) |
16b2eaa1 | 749 | { |
c1b5c1eb | 750 | if (symbol_matches_domain (sym->language (), |
6c9c307c | 751 | sym->domain (), domain)) |
16b2eaa1 DE |
752 | { |
753 | sym_found = sym; | |
d9743061 | 754 | if (!sym->is_argument ()) |
16b2eaa1 DE |
755 | { |
756 | break; | |
757 | } | |
758 | } | |
759 | } | |
760 | return (sym_found); /* Will be NULL if not found. */ | |
761 | } | |
762 | } | |
ba715d7f JK |
763 | |
764 | /* See block.h. */ | |
765 | ||
766 | struct symbol * | |
767 | block_lookup_symbol_primary (const struct block *block, const char *name, | |
768 | const domain_enum domain) | |
769 | { | |
ee93cd5e | 770 | struct symbol *sym, *other; |
b026f593 | 771 | struct mdict_iterator mdict_iter; |
ba715d7f | 772 | |
b5ec771e PA |
773 | lookup_name_info lookup_name (name, symbol_name_match_type::FULL); |
774 | ||
ba715d7f | 775 | /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */ |
f135fe72 SM |
776 | gdb_assert (block->superblock () == NULL |
777 | || block->superblock ()->superblock () == NULL); | |
ba715d7f | 778 | |
ee93cd5e | 779 | other = NULL; |
24d74bb5 SM |
780 | for (sym = mdict_iter_match_first (block->multidict (), lookup_name, |
781 | &mdict_iter); | |
ba715d7f | 782 | sym != NULL; |
b026f593 | 783 | sym = mdict_iter_match_next (lookup_name, &mdict_iter)) |
ba715d7f | 784 | { |
93e55f0a TV |
785 | /* With the fix for PR gcc/debug/91507, we get for: |
786 | ... | |
787 | extern char *zzz[]; | |
788 | char *zzz[ ] = { | |
789 | "abc", | |
790 | "cde" | |
791 | }; | |
792 | ... | |
793 | DWARF which will result in two entries in the symbol table, a decl | |
794 | with type char *[] and a def with type char *[2]. | |
795 | ||
796 | If we return the decl here, we don't get the value of zzz: | |
797 | ... | |
798 | $ gdb a.spec.out -batch -ex "p zzz" | |
799 | $1 = 0x601030 <zzz> | |
800 | ... | |
801 | because we're returning the symbol without location information, and | |
802 | because the fallback that uses the address from the minimal symbols | |
803 | doesn't work either because the type of the decl does not specify a | |
804 | size. | |
805 | ||
806 | To fix this, we prefer def over decl in best_symbol and | |
807 | better_symbol. | |
808 | ||
809 | In absence of the gcc fix, both def and decl have type char *[], so | |
810 | the only option to make this work is improve the fallback to use the | |
811 | size of the minimal symbol. Filed as PR exp/24989. */ | |
812 | if (best_symbol (sym, domain)) | |
ee93cd5e KS |
813 | return sym; |
814 | ||
815 | /* This is a bit of a hack, but symbol_matches_domain might ignore | |
816 | STRUCT vs VAR domain symbols. So if a matching symbol is found, | |
817 | make sure there is no "better" matching symbol, i.e., one with | |
818 | exactly the same domain. PR 16253. */ | |
6c9c307c | 819 | if (symbol_matches_domain (sym->language (), sym->domain (), domain)) |
93e55f0a | 820 | other = better_symbol (other, sym, domain); |
ba715d7f JK |
821 | } |
822 | ||
ee93cd5e | 823 | return other; |
ba715d7f | 824 | } |
b2e2f908 DE |
825 | |
826 | /* See block.h. */ | |
827 | ||
828 | struct symbol * | |
829 | block_find_symbol (const struct block *block, const char *name, | |
830 | const domain_enum domain, | |
831 | block_symbol_matcher_ftype *matcher, void *data) | |
832 | { | |
833 | struct block_iterator iter; | |
834 | struct symbol *sym; | |
835 | ||
b5ec771e PA |
836 | lookup_name_info lookup_name (name, symbol_name_match_type::FULL); |
837 | ||
b2e2f908 | 838 | /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */ |
f135fe72 SM |
839 | gdb_assert (block->superblock () == NULL |
840 | || block->superblock ()->superblock () == NULL); | |
b2e2f908 | 841 | |
b5ec771e | 842 | ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym) |
b2e2f908 DE |
843 | { |
844 | /* MATCHER is deliberately called second here so that it never sees | |
845 | a non-domain-matching symbol. */ | |
6c9c307c | 846 | if (symbol_matches_domain (sym->language (), sym->domain (), domain) |
b2e2f908 DE |
847 | && matcher (sym, data)) |
848 | return sym; | |
849 | } | |
850 | return NULL; | |
851 | } | |
852 | ||
853 | /* See block.h. */ | |
854 | ||
855 | int | |
856 | block_find_non_opaque_type (struct symbol *sym, void *data) | |
857 | { | |
5f9c5a63 | 858 | return !TYPE_IS_OPAQUE (sym->type ()); |
b2e2f908 DE |
859 | } |
860 | ||
861 | /* See block.h. */ | |
862 | ||
863 | int | |
864 | block_find_non_opaque_type_preferred (struct symbol *sym, void *data) | |
865 | { | |
9a3c8263 | 866 | struct symbol **best = (struct symbol **) data; |
b2e2f908 | 867 | |
5f9c5a63 | 868 | if (!TYPE_IS_OPAQUE (sym->type ())) |
b2e2f908 DE |
869 | return 1; |
870 | *best = sym; | |
871 | return 0; | |
872 | } | |
26457a9c KB |
873 | |
874 | /* See block.h. */ | |
875 | ||
876 | struct blockranges * | |
877 | make_blockranges (struct objfile *objfile, | |
dda83cd7 | 878 | const std::vector<blockrange> &rangevec) |
26457a9c KB |
879 | { |
880 | struct blockranges *blr; | |
881 | size_t n = rangevec.size(); | |
882 | ||
883 | blr = (struct blockranges *) | |
884 | obstack_alloc (&objfile->objfile_obstack, | |
dda83cd7 | 885 | sizeof (struct blockranges) |
26457a9c KB |
886 | + (n - 1) * sizeof (struct blockrange)); |
887 | ||
888 | blr->nranges = n; | |
889 | for (int i = 0; i < n; i++) | |
890 | blr->range[i] = rangevec[i]; | |
891 | return blr; | |
892 | } | |
893 |