* symtab.h (struct symtab) <includes, user>: New fields.
[deliverable/binutils-gdb.git] / gdb / block.c
CommitLineData
fe898f56
DC
1/* Block-related functions for the GNU debugger, GDB.
2
0b302171 3 Copyright (C) 2003, 2007-2012 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"
22#include "symtab.h"
23#include "symfile.h"
9219021c
DC
24#include "gdb_obstack.h"
25#include "cp-support.h"
801e3a5b 26#include "addrmap.h"
8e3b41a9
JK
27#include "gdbtypes.h"
28#include "exceptions.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
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);
fe898f56
DC
42
43/* Return Nonzero if block a is lexically nested within block b,
44 or if a and b have the same pc range.
4a64f543 45 Return zero otherwise. */
fe898f56
DC
46
47int
0cf566ec 48contained_in (const struct block *a, const struct block *b)
fe898f56
DC
49{
50 if (!a || !b)
51 return 0;
edb3359d
DJ
52
53 do
54 {
55 if (a == b)
56 return 1;
49e794ac
JB
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;
edb3359d
DJ
61 a = BLOCK_SUPERBLOCK (a);
62 }
63 while (a != NULL);
64
65 return 0;
fe898f56
DC
66}
67
68
69/* Return the symbol for the function which contains a specified
7f0df278
DJ
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. */
fe898f56
DC
73
74struct symbol *
7f0df278 75block_linkage_function (const struct block *bl)
fe898f56 76{
edb3359d
DJ
77 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
78 && BLOCK_SUPERBLOCK (bl) != NULL)
fe898f56
DC
79 bl = BLOCK_SUPERBLOCK (bl);
80
81 return BLOCK_FUNCTION (bl);
82}
83
f8eba3c6
TT
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
edb3359d
DJ
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
801e3a5b
JB
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. */
fe898f56
DC
110
111struct blockvector *
714835d5 112blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
801e3a5b 113 struct block **pblock, struct symtab *symtab)
fe898f56 114{
b59661bd
AC
115 struct block *b;
116 int bot, top, half;
fe898f56
DC
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 */
b59661bd
AC
122 symtab = find_pc_sect_symtab (pc, section);
123 if (symtab == 0)
fe898f56
DC
124 return 0;
125 }
126
127 bl = BLOCKVECTOR (symtab);
fe898f56
DC
128
129 /* Then search that symtab for the smallest block that wins. */
fe898f56 130
801e3a5b
JB
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. */
fe898f56
DC
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 {
801e3a5b
JB
169 if (pblock)
170 *pblock = b;
fe898f56
DC
171 return bl;
172 }
173 bot--;
174 }
175 return 0;
176}
177
8e3b41a9
JK
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
182struct call_site *
183call_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
fe898f56
DC
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
214struct blockvector *
801e3a5b 215blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
fe898f56
DC
216{
217 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
801e3a5b 218 pblock, NULL);
fe898f56
DC
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
224struct block *
714835d5 225block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
fe898f56 226{
b59661bd 227 struct blockvector *bl;
801e3a5b 228 struct block *b;
fe898f56 229
801e3a5b 230 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
fe898f56 231 if (bl)
801e3a5b 232 return b;
fe898f56
DC
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
239struct block *
b59661bd 240block_for_pc (CORE_ADDR pc)
fe898f56
DC
241{
242 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
243}
9219021c 244
1fcb5155
DC
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
252const char *
253block_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}
9219021c
DC
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
269void
270block_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
27aa8d6a 278/* This returns the using directives list associated with BLOCK, if
1fcb5155
DC
279 any. */
280
1fcb5155
DC
281struct using_direct *
282block_using (const struct block *block)
283{
27aa8d6a 284 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
1fcb5155
DC
285 return NULL;
286 else
27aa8d6a 287 return BLOCK_NAMESPACE (block)->using;
1fcb5155
DC
288}
289
9219021c
DC
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
294void
295block_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
307static void
308block_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}
89a9d1b1
DC
318
319/* Return the static block associated to BLOCK. Return NULL if block
320 is NULL or if block is a global block. */
321
322const struct block *
323block_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}
1fcb5155
DC
333
334/* Return the static block associated to BLOCK. Return NULL if block
335 is NULL. */
336
337const struct block *
338block_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}
5c4e30ca
DC
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
358struct block *
359allocate_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;
5c4e30ca
DC
369
370 return bl;
371}
8157b174 372
84a146c9
TT
373/* Allocate a global block. */
374
375struct block *
376allocate_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
385void
386set_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
b5b04b5b
TT
396/* Return the symtab of the global block. */
397
398static struct symtab *
399get_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
8157b174
TT
409\f
410
b5b04b5b
TT
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
415static void
416initialize_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
469static struct symtab *
470find_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
481static struct symbol *
482block_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
8157b174
TT
516/* See block.h. */
517
518struct symbol *
519block_iterator_first (const struct block *block,
520 struct block_iterator *iterator)
521{
b5b04b5b
TT
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);
8157b174
TT
528}
529
530/* See block.h. */
531
532struct symbol *
533block_iterator_next (struct block_iterator *iterator)
534{
b5b04b5b
TT
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
545static struct symbol *
546block_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 }
8157b174
TT
580}
581
582/* See block.h. */
583
584struct symbol *
585block_iter_name_first (const struct block *block,
586 const char *name,
587 struct block_iterator *iterator)
588{
b5b04b5b
TT
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);
8157b174
TT
595}
596
597/* See block.h. */
598
599struct symbol *
600block_iter_name_next (const char *name, struct block_iterator *iterator)
601{
b5b04b5b
TT
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
612static struct symbol *
613block_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 }
8157b174
TT
649}
650
651/* See block.h. */
652
653struct symbol *
654block_iter_match_first (const struct block *block,
655 const char *name,
656 symbol_compare_ftype *compare,
657 struct block_iterator *iterator)
658{
b5b04b5b
TT
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);
8157b174
TT
666}
667
668/* See block.h. */
669
670struct symbol *
671block_iter_match_next (const char *name,
672 symbol_compare_ftype *compare,
673 struct block_iterator *iterator)
674{
b5b04b5b
TT
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);
8157b174 679}
This page took 0.60781 seconds and 4 git commands to generate.