* symtab.h (struct symtab) <includes, user>: New fields.
[deliverable/binutils-gdb.git] / gdb / block.c
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 }
372
373 /* Allocate a global block. */
374
375 struct block *
376 allocate_global_block (struct obstack *obstack)
377 {
378 struct global_block *bl = OBSTACK_ZALLOC (obstack, struct global_block);
379
380 return &bl->block;
381 }
382
383 /* Set the symtab of the global block. */
384
385 void
386 set_block_symtab (struct block *block, struct symtab *symtab)
387 {
388 struct global_block *gb;
389
390 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
391 gb = (struct global_block *) block;
392 gdb_assert (gb->symtab == NULL);
393 gb->symtab = symtab;
394 }
395
396 /* Return the symtab of the global block. */
397
398 static struct symtab *
399 get_block_symtab (const struct block *block)
400 {
401 struct global_block *gb;
402
403 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
404 gb = (struct global_block *) block;
405 gdb_assert (gb->symtab != NULL);
406 return gb->symtab;
407 }
408
409 \f
410
411 /* Initialize a block iterator, either to iterate over a single block,
412 or, for static and global blocks, all the included symtabs as
413 well. */
414
415 static void
416 initialize_block_iterator (const struct block *block,
417 struct block_iterator *iter)
418 {
419 enum block_enum which;
420 struct symtab *symtab;
421
422 iter->idx = -1;
423
424 if (BLOCK_SUPERBLOCK (block) == NULL)
425 {
426 which = GLOBAL_BLOCK;
427 symtab = get_block_symtab (block);
428 }
429 else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL)
430 {
431 which = STATIC_BLOCK;
432 symtab = get_block_symtab (BLOCK_SUPERBLOCK (block));
433 }
434 else
435 {
436 iter->d.block = block;
437 /* A signal value meaning that we're iterating over a single
438 block. */
439 iter->which = FIRST_LOCAL_BLOCK;
440 return;
441 }
442
443 /* If this is an included symtab, find the canonical includer and
444 use it instead. */
445 while (symtab->user != NULL)
446 symtab = symtab->user;
447
448 /* Putting this check here simplifies the logic of the iterator
449 functions. If there are no included symtabs, we only need to
450 search a single block, so we might as well just do that
451 directly. */
452 if (symtab->includes == NULL)
453 {
454 iter->d.block = block;
455 /* A signal value meaning that we're iterating over a single
456 block. */
457 iter->which = FIRST_LOCAL_BLOCK;
458 }
459 else
460 {
461 iter->d.symtab = symtab;
462 iter->which = which;
463 }
464 }
465
466 /* A helper function that finds the current symtab over whose static
467 or global block we should iterate. */
468
469 static struct symtab *
470 find_iterator_symtab (struct block_iterator *iterator)
471 {
472 if (iterator->idx == -1)
473 return iterator->d.symtab;
474 return iterator->d.symtab->includes[iterator->idx];
475 }
476
477 /* Perform a single step for a plain block iterator, iterating across
478 symbol tables as needed. Returns the next symbol, or NULL when
479 iteration is complete. */
480
481 static struct symbol *
482 block_iterator_step (struct block_iterator *iterator, int first)
483 {
484 struct symbol *sym;
485
486 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
487
488 while (1)
489 {
490 if (first)
491 {
492 struct symtab *symtab = find_iterator_symtab (iterator);
493 const struct block *block;
494
495 /* Iteration is complete. */
496 if (symtab == NULL)
497 return NULL;
498
499 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
500 sym = dict_iterator_first (BLOCK_DICT (block), &iterator->dict_iter);
501 }
502 else
503 sym = dict_iterator_next (&iterator->dict_iter);
504
505 if (sym != NULL)
506 return sym;
507
508 /* We have finished iterating the appropriate block of one
509 symtab. Now advance to the next symtab and begin iteration
510 there. */
511 ++iterator->idx;
512 first = 1;
513 }
514 }
515
516 /* See block.h. */
517
518 struct symbol *
519 block_iterator_first (const struct block *block,
520 struct block_iterator *iterator)
521 {
522 initialize_block_iterator (block, iterator);
523
524 if (iterator->which == FIRST_LOCAL_BLOCK)
525 return dict_iterator_first (block->dict, &iterator->dict_iter);
526
527 return block_iterator_step (iterator, 1);
528 }
529
530 /* See block.h. */
531
532 struct symbol *
533 block_iterator_next (struct block_iterator *iterator)
534 {
535 if (iterator->which == FIRST_LOCAL_BLOCK)
536 return dict_iterator_next (&iterator->dict_iter);
537
538 return block_iterator_step (iterator, 0);
539 }
540
541 /* Perform a single step for a "name" block iterator, iterating across
542 symbol tables as needed. Returns the next symbol, or NULL when
543 iteration is complete. */
544
545 static struct symbol *
546 block_iter_name_step (struct block_iterator *iterator, const char *name,
547 int first)
548 {
549 struct symbol *sym;
550
551 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
552
553 while (1)
554 {
555 if (first)
556 {
557 struct symtab *symtab = find_iterator_symtab (iterator);
558 const struct block *block;
559
560 /* Iteration is complete. */
561 if (symtab == NULL)
562 return NULL;
563
564 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
565 sym = dict_iter_name_first (BLOCK_DICT (block), name,
566 &iterator->dict_iter);
567 }
568 else
569 sym = dict_iter_name_next (name, &iterator->dict_iter);
570
571 if (sym != NULL)
572 return sym;
573
574 /* We have finished iterating the appropriate block of one
575 symtab. Now advance to the next symtab and begin iteration
576 there. */
577 ++iterator->idx;
578 first = 1;
579 }
580 }
581
582 /* See block.h. */
583
584 struct symbol *
585 block_iter_name_first (const struct block *block,
586 const char *name,
587 struct block_iterator *iterator)
588 {
589 initialize_block_iterator (block, iterator);
590
591 if (iterator->which == FIRST_LOCAL_BLOCK)
592 return dict_iter_name_first (block->dict, name, &iterator->dict_iter);
593
594 return block_iter_name_step (iterator, name, 1);
595 }
596
597 /* See block.h. */
598
599 struct symbol *
600 block_iter_name_next (const char *name, struct block_iterator *iterator)
601 {
602 if (iterator->which == FIRST_LOCAL_BLOCK)
603 return dict_iter_name_next (name, &iterator->dict_iter);
604
605 return block_iter_name_step (iterator, name, 0);
606 }
607
608 /* Perform a single step for a "match" block iterator, iterating
609 across symbol tables as needed. Returns the next symbol, or NULL
610 when iteration is complete. */
611
612 static struct symbol *
613 block_iter_match_step (struct block_iterator *iterator,
614 const char *name,
615 symbol_compare_ftype *compare,
616 int first)
617 {
618 struct symbol *sym;
619
620 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
621
622 while (1)
623 {
624 if (first)
625 {
626 struct symtab *symtab = find_iterator_symtab (iterator);
627 const struct block *block;
628
629 /* Iteration is complete. */
630 if (symtab == NULL)
631 return NULL;
632
633 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
634 sym = dict_iter_match_first (BLOCK_DICT (block), name,
635 compare, &iterator->dict_iter);
636 }
637 else
638 sym = dict_iter_match_next (name, compare, &iterator->dict_iter);
639
640 if (sym != NULL)
641 return sym;
642
643 /* We have finished iterating the appropriate block of one
644 symtab. Now advance to the next symtab and begin iteration
645 there. */
646 ++iterator->idx;
647 first = 1;
648 }
649 }
650
651 /* See block.h. */
652
653 struct symbol *
654 block_iter_match_first (const struct block *block,
655 const char *name,
656 symbol_compare_ftype *compare,
657 struct block_iterator *iterator)
658 {
659 initialize_block_iterator (block, iterator);
660
661 if (iterator->which == FIRST_LOCAL_BLOCK)
662 return dict_iter_match_first (block->dict, name, compare,
663 &iterator->dict_iter);
664
665 return block_iter_match_step (iterator, name, compare, 1);
666 }
667
668 /* See block.h. */
669
670 struct symbol *
671 block_iter_match_next (const char *name,
672 symbol_compare_ftype *compare,
673 struct block_iterator *iterator)
674 {
675 if (iterator->which == FIRST_LOCAL_BLOCK)
676 return dict_iter_match_next (name, compare, &iterator->dict_iter);
677
678 return block_iter_match_step (iterator, name, compare, 0);
679 }
This page took 0.062883 seconds and 4 git commands to generate.