Upgrade GDB from 7.4.1 to 7.6.1 on the vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / block.c
CommitLineData
5796c8dc
SS
1/* Block-related functions for the GNU debugger, GDB.
2
ef5ccd6c 3 Copyright (C) 2003-2013 Free Software Foundation, Inc.
5796c8dc
SS
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"
a45ae5f8
JM
27#include "gdbtypes.h"
28#include "exceptions.h"
5796c8dc
SS
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
34struct block_namespace_info
35{
36 const char *scope;
37 struct using_direct *using;
38};
39
40static 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.
c50c785c 45 Return zero otherwise. */
5796c8dc
SS
46
47int
48contained_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
74struct symbol *
75block_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
a45ae5f8
JM
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
89struct symbol *
90block_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
5796c8dc
SS
98/* Return one if BL represents an inlined function. */
99
100int
101block_inlined_p (const struct block *bl)
102{
103 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
104}
105
ef5ccd6c
JM
106/* A helper function that checks whether PC is in the blockvector BL.
107 It returns the containing block if there is one, or else NULL. */
5796c8dc 108
ef5ccd6c
JM
109static struct block *
110find_block_in_blockvector (struct blockvector *bl, CORE_ADDR pc)
5796c8dc
SS
111{
112 struct block *b;
113 int bot, top, half;
5796c8dc
SS
114
115 /* If we have an addrmap mapping code addresses to blocks, then use
116 that. */
117 if (BLOCKVECTOR_MAP (bl))
ef5ccd6c 118 return addrmap_find (BLOCKVECTOR_MAP (bl), pc);
5796c8dc
SS
119
120 /* Otherwise, use binary search to find the last block that starts
ef5ccd6c
JM
121 before PC.
122 Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1.
123 They both have the same START,END values.
124 Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the
125 fact that this choice was made was subtle, now we make it explicit. */
126 gdb_assert (BLOCKVECTOR_NBLOCKS (bl) >= 2);
127 bot = STATIC_BLOCK;
5796c8dc
SS
128 top = BLOCKVECTOR_NBLOCKS (bl);
129
130 while (top - bot > 1)
131 {
132 half = (top - bot + 1) >> 1;
133 b = BLOCKVECTOR_BLOCK (bl, bot + half);
134 if (BLOCK_START (b) <= pc)
135 bot += half;
136 else
137 top = bot + half;
138 }
139
140 /* Now search backward for a block that ends after PC. */
141
ef5ccd6c 142 while (bot >= STATIC_BLOCK)
5796c8dc
SS
143 {
144 b = BLOCKVECTOR_BLOCK (bl, bot);
145 if (BLOCK_END (b) > pc)
ef5ccd6c 146 return b;
5796c8dc
SS
147 bot--;
148 }
ef5ccd6c
JM
149
150 return NULL;
151}
152
153/* Return the blockvector immediately containing the innermost lexical
154 block containing the specified pc value and section, or 0 if there
155 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
156 don't pass this information back to the caller. */
157
158struct blockvector *
159blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
160 struct block **pblock, struct symtab *symtab)
161{
162 struct blockvector *bl;
163 struct block *b;
164
165 if (symtab == 0) /* if no symtab specified by caller */
166 {
167 /* First search all symtabs for one whose file contains our pc */
168 symtab = find_pc_sect_symtab (pc, section);
169 if (symtab == 0)
170 return 0;
171 }
172
173 bl = BLOCKVECTOR (symtab);
174
175 /* Then search that symtab for the smallest block that wins. */
176 b = find_block_in_blockvector (bl, pc);
177 if (b == NULL)
178 return NULL;
179
180 if (pblock)
181 *pblock = b;
182 return bl;
183}
184
185/* Return true if the blockvector BV contains PC, false otherwise. */
186
187int
188blockvector_contains_pc (struct blockvector *bv, CORE_ADDR pc)
189{
190 return find_block_in_blockvector (bv, pc) != NULL;
5796c8dc
SS
191}
192
a45ae5f8
JM
193/* Return call_site for specified PC in GDBARCH. PC must match exactly, it
194 must be the next instruction after call (or after tail call jump). Throw
195 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
196
197struct call_site *
198call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc)
199{
200 struct symtab *symtab;
201 void **slot = NULL;
202
203 /* -1 as tail call PC can be already after the compilation unit range. */
204 symtab = find_pc_symtab (pc - 1);
205
206 if (symtab != NULL && symtab->call_site_htab != NULL)
207 slot = htab_find_slot (symtab->call_site_htab, &pc, NO_INSERT);
208
209 if (slot == NULL)
210 {
211 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (pc);
212
213 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
214 the call target. */
215 throw_error (NO_ENTRY_VALUE_ERROR,
216 _("DW_OP_GNU_entry_value resolving cannot find "
217 "DW_TAG_GNU_call_site %s in %s"),
218 paddress (gdbarch, pc),
219 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
220 }
221
222 return *slot;
223}
224
5796c8dc
SS
225/* Return the blockvector immediately containing the innermost lexical block
226 containing the specified pc value, or 0 if there is none.
227 Backward compatibility, no section. */
228
229struct blockvector *
230blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
231{
232 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
233 pblock, NULL);
234}
235
236/* Return the innermost lexical block containing the specified pc value
237 in the specified section, or 0 if there is none. */
238
239struct block *
240block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
241{
242 struct blockvector *bl;
243 struct block *b;
244
245 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
246 if (bl)
247 return b;
248 return 0;
249}
250
251/* Return the innermost lexical block containing the specified pc value,
252 or 0 if there is none. Backward compatibility, no section. */
253
254struct block *
255block_for_pc (CORE_ADDR pc)
256{
257 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
258}
259
260/* Now come some functions designed to deal with C++ namespace issues.
261 The accessors are safe to use even in the non-C++ case. */
262
263/* This returns the namespace that BLOCK is enclosed in, or "" if it
264 isn't enclosed in a namespace at all. This travels the chain of
265 superblocks looking for a scope, if necessary. */
266
267const char *
268block_scope (const struct block *block)
269{
270 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
271 {
272 if (BLOCK_NAMESPACE (block) != NULL
273 && BLOCK_NAMESPACE (block)->scope != NULL)
274 return BLOCK_NAMESPACE (block)->scope;
275 }
276
277 return "";
278}
279
280/* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
281 OBSTACK. (It won't make a copy of SCOPE, however, so that already
282 has to be allocated correctly.) */
283
284void
285block_set_scope (struct block *block, const char *scope,
286 struct obstack *obstack)
287{
288 block_initialize_namespace (block, obstack);
289
290 BLOCK_NAMESPACE (block)->scope = scope;
291}
292
293/* This returns the using directives list associated with BLOCK, if
294 any. */
295
296struct using_direct *
297block_using (const struct block *block)
298{
299 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
300 return NULL;
301 else
302 return BLOCK_NAMESPACE (block)->using;
303}
304
305/* Set BLOCK's using member to USING; if needed, allocate memory via
306 OBSTACK. (It won't make a copy of USING, however, so that already
307 has to be allocated correctly.) */
308
309void
310block_set_using (struct block *block,
311 struct using_direct *using,
312 struct obstack *obstack)
313{
314 block_initialize_namespace (block, obstack);
315
316 BLOCK_NAMESPACE (block)->using = using;
317}
318
319/* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
320 ititialize its members to zero. */
321
322static void
323block_initialize_namespace (struct block *block, struct obstack *obstack)
324{
325 if (BLOCK_NAMESPACE (block) == NULL)
326 {
327 BLOCK_NAMESPACE (block)
328 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
329 BLOCK_NAMESPACE (block)->scope = NULL;
330 BLOCK_NAMESPACE (block)->using = NULL;
331 }
332}
333
334/* Return the static block associated to BLOCK. Return NULL if block
335 is NULL or if block is a global block. */
336
337const struct block *
338block_static_block (const struct block *block)
339{
340 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
341 return NULL;
342
343 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
344 block = BLOCK_SUPERBLOCK (block);
345
346 return block;
347}
348
349/* Return the static block associated to BLOCK. Return NULL if block
350 is NULL. */
351
352const struct block *
353block_global_block (const struct block *block)
354{
355 if (block == NULL)
356 return NULL;
357
358 while (BLOCK_SUPERBLOCK (block) != NULL)
359 block = BLOCK_SUPERBLOCK (block);
360
361 return block;
362}
363
364/* Allocate a block on OBSTACK, and initialize its elements to
365 zero/NULL. This is useful for creating "dummy" blocks that don't
366 correspond to actual source files.
367
368 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
369 valid value. If you really don't want the block to have a
370 dictionary, then you should subsequently set its BLOCK_DICT to
371 dict_create_linear (obstack, NULL). */
372
373struct block *
374allocate_block (struct obstack *obstack)
375{
376 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
377
378 BLOCK_START (bl) = 0;
379 BLOCK_END (bl) = 0;
380 BLOCK_FUNCTION (bl) = NULL;
381 BLOCK_SUPERBLOCK (bl) = NULL;
382 BLOCK_DICT (bl) = NULL;
383 BLOCK_NAMESPACE (bl) = NULL;
384
385 return bl;
386}
ef5ccd6c
JM
387
388/* Allocate a global block. */
389
390struct block *
391allocate_global_block (struct obstack *obstack)
392{
393 struct global_block *bl = OBSTACK_ZALLOC (obstack, struct global_block);
394
395 return &bl->block;
396}
397
398/* Set the symtab of the global block. */
399
400void
401set_block_symtab (struct block *block, struct symtab *symtab)
402{
403 struct global_block *gb;
404
405 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
406 gb = (struct global_block *) block;
407 gdb_assert (gb->symtab == NULL);
408 gb->symtab = symtab;
409}
410
411/* Return the symtab of the global block. */
412
413static struct symtab *
414get_block_symtab (const struct block *block)
415{
416 struct global_block *gb;
417
418 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
419 gb = (struct global_block *) block;
420 gdb_assert (gb->symtab != NULL);
421 return gb->symtab;
422}
423
424\f
425
426/* Initialize a block iterator, either to iterate over a single block,
427 or, for static and global blocks, all the included symtabs as
428 well. */
429
430static void
431initialize_block_iterator (const struct block *block,
432 struct block_iterator *iter)
433{
434 enum block_enum which;
435 struct symtab *symtab;
436
437 iter->idx = -1;
438
439 if (BLOCK_SUPERBLOCK (block) == NULL)
440 {
441 which = GLOBAL_BLOCK;
442 symtab = get_block_symtab (block);
443 }
444 else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL)
445 {
446 which = STATIC_BLOCK;
447 symtab = get_block_symtab (BLOCK_SUPERBLOCK (block));
448 }
449 else
450 {
451 iter->d.block = block;
452 /* A signal value meaning that we're iterating over a single
453 block. */
454 iter->which = FIRST_LOCAL_BLOCK;
455 return;
456 }
457
458 /* If this is an included symtab, find the canonical includer and
459 use it instead. */
460 while (symtab->user != NULL)
461 symtab = symtab->user;
462
463 /* Putting this check here simplifies the logic of the iterator
464 functions. If there are no included symtabs, we only need to
465 search a single block, so we might as well just do that
466 directly. */
467 if (symtab->includes == NULL)
468 {
469 iter->d.block = block;
470 /* A signal value meaning that we're iterating over a single
471 block. */
472 iter->which = FIRST_LOCAL_BLOCK;
473 }
474 else
475 {
476 iter->d.symtab = symtab;
477 iter->which = which;
478 }
479}
480
481/* A helper function that finds the current symtab over whose static
482 or global block we should iterate. */
483
484static struct symtab *
485find_iterator_symtab (struct block_iterator *iterator)
486{
487 if (iterator->idx == -1)
488 return iterator->d.symtab;
489 return iterator->d.symtab->includes[iterator->idx];
490}
491
492/* Perform a single step for a plain block iterator, iterating across
493 symbol tables as needed. Returns the next symbol, or NULL when
494 iteration is complete. */
495
496static struct symbol *
497block_iterator_step (struct block_iterator *iterator, int first)
498{
499 struct symbol *sym;
500
501 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
502
503 while (1)
504 {
505 if (first)
506 {
507 struct symtab *symtab = find_iterator_symtab (iterator);
508 const struct block *block;
509
510 /* Iteration is complete. */
511 if (symtab == NULL)
512 return NULL;
513
514 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
515 sym = dict_iterator_first (BLOCK_DICT (block), &iterator->dict_iter);
516 }
517 else
518 sym = dict_iterator_next (&iterator->dict_iter);
519
520 if (sym != NULL)
521 return sym;
522
523 /* We have finished iterating the appropriate block of one
524 symtab. Now advance to the next symtab and begin iteration
525 there. */
526 ++iterator->idx;
527 first = 1;
528 }
529}
530
531/* See block.h. */
532
533struct symbol *
534block_iterator_first (const struct block *block,
535 struct block_iterator *iterator)
536{
537 initialize_block_iterator (block, iterator);
538
539 if (iterator->which == FIRST_LOCAL_BLOCK)
540 return dict_iterator_first (block->dict, &iterator->dict_iter);
541
542 return block_iterator_step (iterator, 1);
543}
544
545/* See block.h. */
546
547struct symbol *
548block_iterator_next (struct block_iterator *iterator)
549{
550 if (iterator->which == FIRST_LOCAL_BLOCK)
551 return dict_iterator_next (&iterator->dict_iter);
552
553 return block_iterator_step (iterator, 0);
554}
555
556/* Perform a single step for a "name" block iterator, iterating across
557 symbol tables as needed. Returns the next symbol, or NULL when
558 iteration is complete. */
559
560static struct symbol *
561block_iter_name_step (struct block_iterator *iterator, const char *name,
562 int first)
563{
564 struct symbol *sym;
565
566 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
567
568 while (1)
569 {
570 if (first)
571 {
572 struct symtab *symtab = find_iterator_symtab (iterator);
573 const struct block *block;
574
575 /* Iteration is complete. */
576 if (symtab == NULL)
577 return NULL;
578
579 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
580 sym = dict_iter_name_first (BLOCK_DICT (block), name,
581 &iterator->dict_iter);
582 }
583 else
584 sym = dict_iter_name_next (name, &iterator->dict_iter);
585
586 if (sym != NULL)
587 return sym;
588
589 /* We have finished iterating the appropriate block of one
590 symtab. Now advance to the next symtab and begin iteration
591 there. */
592 ++iterator->idx;
593 first = 1;
594 }
595}
596
597/* See block.h. */
598
599struct symbol *
600block_iter_name_first (const struct block *block,
601 const char *name,
602 struct block_iterator *iterator)
603{
604 initialize_block_iterator (block, iterator);
605
606 if (iterator->which == FIRST_LOCAL_BLOCK)
607 return dict_iter_name_first (block->dict, name, &iterator->dict_iter);
608
609 return block_iter_name_step (iterator, name, 1);
610}
611
612/* See block.h. */
613
614struct symbol *
615block_iter_name_next (const char *name, struct block_iterator *iterator)
616{
617 if (iterator->which == FIRST_LOCAL_BLOCK)
618 return dict_iter_name_next (name, &iterator->dict_iter);
619
620 return block_iter_name_step (iterator, name, 0);
621}
622
623/* Perform a single step for a "match" block iterator, iterating
624 across symbol tables as needed. Returns the next symbol, or NULL
625 when iteration is complete. */
626
627static struct symbol *
628block_iter_match_step (struct block_iterator *iterator,
629 const char *name,
630 symbol_compare_ftype *compare,
631 int first)
632{
633 struct symbol *sym;
634
635 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
636
637 while (1)
638 {
639 if (first)
640 {
641 struct symtab *symtab = find_iterator_symtab (iterator);
642 const struct block *block;
643
644 /* Iteration is complete. */
645 if (symtab == NULL)
646 return NULL;
647
648 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
649 sym = dict_iter_match_first (BLOCK_DICT (block), name,
650 compare, &iterator->dict_iter);
651 }
652 else
653 sym = dict_iter_match_next (name, compare, &iterator->dict_iter);
654
655 if (sym != NULL)
656 return sym;
657
658 /* We have finished iterating the appropriate block of one
659 symtab. Now advance to the next symtab and begin iteration
660 there. */
661 ++iterator->idx;
662 first = 1;
663 }
664}
665
666/* See block.h. */
667
668struct symbol *
669block_iter_match_first (const struct block *block,
670 const char *name,
671 symbol_compare_ftype *compare,
672 struct block_iterator *iterator)
673{
674 initialize_block_iterator (block, iterator);
675
676 if (iterator->which == FIRST_LOCAL_BLOCK)
677 return dict_iter_match_first (block->dict, name, compare,
678 &iterator->dict_iter);
679
680 return block_iter_match_step (iterator, name, compare, 1);
681}
682
683/* See block.h. */
684
685struct symbol *
686block_iter_match_next (const char *name,
687 symbol_compare_ftype *compare,
688 struct block_iterator *iterator)
689{
690 if (iterator->which == FIRST_LOCAL_BLOCK)
691 return dict_iter_match_next (name, compare, &iterator->dict_iter);
692
693 return block_iter_match_step (iterator, name, compare, 0);
694}