* psymtab.c (find_pc_sect_symtab_from_partial): Return the symtab
[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 /* 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. */
108
109 static struct block *
110 find_block_in_blockvector (struct blockvector *bl, CORE_ADDR pc)
111 {
112 struct block *b;
113 int bot, top, half;
114
115 /* If we have an addrmap mapping code addresses to blocks, then use
116 that. */
117 if (BLOCKVECTOR_MAP (bl))
118 return addrmap_find (BLOCKVECTOR_MAP (bl), pc);
119
120 /* Otherwise, use binary search to find the last block that starts
121 before PC. */
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)
141 return b;
142 bot--;
143 }
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
153 struct blockvector *
154 blockvector_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
182 int
183 blockvector_contains_pc (struct blockvector *bv, CORE_ADDR pc)
184 {
185 return find_block_in_blockvector (bv, pc) != NULL;
186 }
187
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
192 struct call_site *
193 call_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
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
224 struct blockvector *
225 blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
226 {
227 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
228 pblock, NULL);
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
234 struct block *
235 block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
236 {
237 struct blockvector *bl;
238 struct block *b;
239
240 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
241 if (bl)
242 return b;
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
249 struct block *
250 block_for_pc (CORE_ADDR pc)
251 {
252 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
253 }
254
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
262 const char *
263 block_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 }
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
279 void
280 block_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
288 /* This returns the using directives list associated with BLOCK, if
289 any. */
290
291 struct using_direct *
292 block_using (const struct block *block)
293 {
294 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
295 return NULL;
296 else
297 return BLOCK_NAMESPACE (block)->using;
298 }
299
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
304 void
305 block_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
317 static void
318 block_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 }
328
329 /* Return the static block associated to BLOCK. Return NULL if block
330 is NULL or if block is a global block. */
331
332 const struct block *
333 block_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 }
343
344 /* Return the static block associated to BLOCK. Return NULL if block
345 is NULL. */
346
347 const struct block *
348 block_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 }
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
368 struct block *
369 allocate_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;
379
380 return bl;
381 }
382
383 /* Allocate a global block. */
384
385 struct block *
386 allocate_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
395 void
396 set_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
406 /* Return the symtab of the global block. */
407
408 static struct symtab *
409 get_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
419 \f
420
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
425 static void
426 initialize_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
479 static struct symtab *
480 find_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
491 static struct symbol *
492 block_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
526 /* See block.h. */
527
528 struct symbol *
529 block_iterator_first (const struct block *block,
530 struct block_iterator *iterator)
531 {
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);
538 }
539
540 /* See block.h. */
541
542 struct symbol *
543 block_iterator_next (struct block_iterator *iterator)
544 {
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
555 static struct symbol *
556 block_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 }
590 }
591
592 /* See block.h. */
593
594 struct symbol *
595 block_iter_name_first (const struct block *block,
596 const char *name,
597 struct block_iterator *iterator)
598 {
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);
605 }
606
607 /* See block.h. */
608
609 struct symbol *
610 block_iter_name_next (const char *name, struct block_iterator *iterator)
611 {
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
622 static struct symbol *
623 block_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 }
659 }
660
661 /* See block.h. */
662
663 struct symbol *
664 block_iter_match_first (const struct block *block,
665 const char *name,
666 symbol_compare_ftype *compare,
667 struct block_iterator *iterator)
668 {
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);
676 }
677
678 /* See block.h. */
679
680 struct symbol *
681 block_iter_match_next (const char *name,
682 symbol_compare_ftype *compare,
683 struct block_iterator *iterator)
684 {
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);
689 }
This page took 0.060996 seconds and 4 git commands to generate.