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