1 /* Partial symbol tables.
3 Copyright (C) 2009-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
25 #include "filenames.h"
32 #include "readline/readline.h"
33 #include "gdb_regex.h"
34 #include "dictionary.h"
36 #include "cp-support.h"
43 struct bcache
*bcache
;
46 static struct partial_symbol
*match_partial_symbol (struct objfile
*,
47 struct partial_symtab
*,
49 const char *, domain_enum
,
50 symbol_name_match_type
,
51 symbol_compare_ftype
*);
53 static struct partial_symbol
*lookup_partial_symbol (struct objfile
*,
54 struct partial_symtab
*,
58 static const char *psymtab_to_fullname (struct partial_symtab
*ps
);
60 static struct partial_symbol
*find_pc_sect_psymbol (struct objfile
*,
61 struct partial_symtab
*,
63 struct obj_section
*);
65 static struct compunit_symtab
*psymtab_to_symtab (struct objfile
*objfile
,
66 struct partial_symtab
*pst
);
70 psymtab_storage::psymtab_storage (struct objfile
*objfile
)
71 : psymbol_cache (psymbol_bcache_init ()),
72 m_obstack (&objfile
->objfile_obstack
)
76 psymtab_storage::~psymtab_storage ()
78 psymbol_bcache_free (psymbol_cache
);
83 struct partial_symtab
*
84 psymtab_storage::allocate_psymtab ()
86 struct partial_symtab
*psymtab
;
88 if (free_psymtabs
!= nullptr)
90 psymtab
= free_psymtabs
;
91 free_psymtabs
= psymtab
->next
;
94 psymtab
= XOBNEW (obstack (), struct partial_symtab
);
96 memset (psymtab
, 0, sizeof (struct partial_symtab
));
98 psymtab
->next
= psymtabs
;
109 require_partial_symbols (struct objfile
*objfile
, int verbose
)
111 if ((objfile
->flags
& OBJF_PSYMTABS_READ
) == 0)
113 objfile
->flags
|= OBJF_PSYMTABS_READ
;
115 if (objfile
->sf
->sym_read_psymbols
)
118 printf_filtered (_("Reading symbols from %s...\n"),
119 objfile_name (objfile
));
120 (*objfile
->sf
->sym_read_psymbols
) (objfile
);
122 /* Partial symbols list are not expected to changed after this
124 objfile
->partial_symtabs
->global_psymbols
.shrink_to_fit ();
125 objfile
->partial_symtabs
->static_psymbols
.shrink_to_fit ();
127 if (verbose
&& !objfile_has_symbols (objfile
))
128 printf_filtered (_("(No debugging symbols found in %s)\n"),
129 objfile_name (objfile
));
133 return objfile_psymtabs (objfile
);
136 /* Helper function for psym_map_symtabs_matching_filename that
137 expands the symtabs and calls the iterator. */
140 partial_map_expand_apply (struct objfile
*objfile
,
142 const char *real_path
,
143 struct partial_symtab
*pst
,
144 gdb::function_view
<bool (symtab
*)> callback
)
146 struct compunit_symtab
*last_made
= objfile
->compunit_symtabs
;
148 /* Shared psymtabs should never be seen here. Instead they should
149 be handled properly by the caller. */
150 gdb_assert (pst
->user
== NULL
);
152 /* Don't visit already-expanded psymtabs. */
156 /* This may expand more than one symtab, and we want to iterate over
158 psymtab_to_symtab (objfile
, pst
);
160 return iterate_over_some_symtabs (name
, real_path
, objfile
->compunit_symtabs
,
161 last_made
, callback
);
164 /* Psymtab version of map_symtabs_matching_filename. See its definition in
165 the definition of quick_symbol_functions in symfile.h. */
168 psym_map_symtabs_matching_filename
169 (struct objfile
*objfile
,
171 const char *real_path
,
172 gdb::function_view
<bool (symtab
*)> callback
)
174 const char *name_basename
= lbasename (name
);
176 for (partial_symtab
*pst
: require_partial_symbols (objfile
, 1))
178 /* We can skip shared psymtabs here, because any file name will be
179 attached to the unshared psymtab. */
180 if (pst
->user
!= NULL
)
183 /* Anonymous psymtabs don't have a file name. */
187 if (compare_filenames_for_search (pst
->filename
, name
))
189 if (partial_map_expand_apply (objfile
, name
, real_path
,
195 /* Before we invoke realpath, which can get expensive when many
196 files are involved, do a quick comparison of the basenames. */
197 if (! basenames_may_differ
198 && FILENAME_CMP (name_basename
, lbasename (pst
->filename
)) != 0)
201 if (compare_filenames_for_search (psymtab_to_fullname (pst
), name
))
203 if (partial_map_expand_apply (objfile
, name
, real_path
,
209 /* If the user gave us an absolute path, try to find the file in
210 this symtab and use its absolute path. */
211 if (real_path
!= NULL
)
213 gdb_assert (IS_ABSOLUTE_PATH (real_path
));
214 gdb_assert (IS_ABSOLUTE_PATH (name
));
215 if (filename_cmp (psymtab_to_fullname (pst
), real_path
) == 0)
217 if (partial_map_expand_apply (objfile
, name
, real_path
,
228 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
229 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
231 static struct partial_symtab
*
232 find_pc_sect_psymtab_closer (struct objfile
*objfile
,
233 CORE_ADDR pc
, struct obj_section
*section
,
234 struct partial_symtab
*pst
,
235 struct bound_minimal_symbol msymbol
)
237 struct partial_symtab
*tpst
;
238 struct partial_symtab
*best_pst
= pst
;
239 CORE_ADDR best_addr
= pst
->text_low (objfile
);
241 gdb_assert (!pst
->psymtabs_addrmap_supported
);
243 /* An objfile that has its functions reordered might have
244 many partial symbol tables containing the PC, but
245 we want the partial symbol table that contains the
246 function containing the PC. */
247 if (!(objfile
->flags
& OBJF_REORDERED
)
248 && section
== NULL
) /* Can't validate section this way. */
251 if (msymbol
.minsym
== NULL
)
254 /* The code range of partial symtabs sometimes overlap, so, in
255 the loop below, we need to check all partial symtabs and
256 find the one that fits better for the given PC address. We
257 select the partial symtab that contains a symbol whose
258 address is closest to the PC address. By closest we mean
259 that find_pc_sect_symbol returns the symbol with address
260 that is closest and still less than the given PC. */
261 for (tpst
= pst
; tpst
!= NULL
; tpst
= tpst
->next
)
263 if (pc
>= tpst
->text_low (objfile
) && pc
< tpst
->text_high (objfile
))
265 struct partial_symbol
*p
;
268 /* NOTE: This assumes that every psymbol has a
269 corresponding msymbol, which is not necessarily
270 true; the debug info might be much richer than the
271 object's symbol table. */
272 p
= find_pc_sect_psymbol (objfile
, tpst
, pc
, section
);
274 && (p
->address (objfile
) == BMSYMBOL_VALUE_ADDRESS (msymbol
)))
277 /* Also accept the textlow value of a psymtab as a
278 "symbol", to provide some support for partial
279 symbol tables with line information but no debug
280 symbols (e.g. those produced by an assembler). */
282 this_addr
= p
->address (objfile
);
284 this_addr
= tpst
->text_low (objfile
);
286 /* Check whether it is closer than our current
287 BEST_ADDR. Since this symbol address is
288 necessarily lower or equal to PC, the symbol closer
289 to PC is the symbol which address is the highest.
290 This way we return the psymtab which contains such
291 best match symbol. This can help in cases where the
292 symbol information/debuginfo is not complete, like
293 for instance on IRIX6 with gcc, where no debug info
294 is emitted for statics. (See also the nodebug.exp
296 if (this_addr
> best_addr
)
298 best_addr
= this_addr
;
306 /* Find which partial symtab contains PC and SECTION. Return NULL if
307 none. We return the psymtab that contains a symbol whose address
308 exactly matches PC, or, if we cannot find an exact match, the
309 psymtab that contains a symbol whose address is closest to PC. */
311 static struct partial_symtab
*
312 find_pc_sect_psymtab (struct objfile
*objfile
, CORE_ADDR pc
,
313 struct obj_section
*section
,
314 struct bound_minimal_symbol msymbol
)
316 CORE_ADDR baseaddr
= ANOFFSET (objfile
->section_offsets
,
317 SECT_OFF_TEXT (objfile
));
319 /* Try just the PSYMTABS_ADDRMAP mapping first as it has better granularity
320 than the later used TEXTLOW/TEXTHIGH one. */
322 if (objfile
->partial_symtabs
->psymtabs_addrmap
!= NULL
)
324 struct partial_symtab
*pst
325 = ((struct partial_symtab
*)
326 addrmap_find (objfile
->partial_symtabs
->psymtabs_addrmap
,
330 /* FIXME: addrmaps currently do not handle overlayed sections,
331 so fall back to the non-addrmap case if we're debugging
332 overlays and the addrmap returned the wrong section. */
333 if (overlay_debugging
&& msymbol
.minsym
!= NULL
&& section
!= NULL
)
335 struct partial_symbol
*p
;
337 /* NOTE: This assumes that every psymbol has a
338 corresponding msymbol, which is not necessarily
339 true; the debug info might be much richer than the
340 object's symbol table. */
341 p
= find_pc_sect_psymbol (objfile
, pst
, pc
, section
);
343 || (p
->address (objfile
)
344 != BMSYMBOL_VALUE_ADDRESS (msymbol
)))
348 /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
349 PSYMTABS_ADDRMAP we used has already the best 1-byte
350 granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
351 a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
360 /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
361 which still have no corresponding full SYMTABs read. But it is not
362 present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
365 /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
366 its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
367 debug info type in single OBJFILE. */
369 for (partial_symtab
*pst
: require_partial_symbols (objfile
, 1))
370 if (!pst
->psymtabs_addrmap_supported
371 && pc
>= pst
->text_low (objfile
) && pc
< pst
->text_high (objfile
))
373 struct partial_symtab
*best_pst
;
375 best_pst
= find_pc_sect_psymtab_closer (objfile
, pc
, section
, pst
,
377 if (best_pst
!= NULL
)
384 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
385 the definition of quick_symbol_functions in symfile.h. */
387 static struct compunit_symtab
*
388 psym_find_pc_sect_compunit_symtab (struct objfile
*objfile
,
389 struct bound_minimal_symbol msymbol
,
391 struct obj_section
*section
,
394 struct partial_symtab
*ps
= find_pc_sect_psymtab (objfile
, pc
, section
,
398 if (warn_if_readin
&& ps
->readin
)
399 /* Might want to error() here (in case symtab is corrupt and
400 will cause a core dump), but maybe we can successfully
401 continue, so let's not. */
403 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
404 paddress (get_objfile_arch (objfile
), pc
));
405 psymtab_to_symtab (objfile
, ps
);
406 return ps
->compunit_symtab
;
411 /* Find which partial symbol within a psymtab matches PC and SECTION.
412 Return NULL if none. */
414 static struct partial_symbol
*
415 find_pc_sect_psymbol (struct objfile
*objfile
,
416 struct partial_symtab
*psymtab
, CORE_ADDR pc
,
417 struct obj_section
*section
)
419 struct partial_symbol
*best
= NULL
;
421 const CORE_ADDR textlow
= psymtab
->text_low (objfile
);
423 gdb_assert (psymtab
!= NULL
);
425 /* Cope with programs that start at address 0. */
426 best_pc
= (textlow
!= 0) ? textlow
- 1 : 0;
428 /* Search the global symbols as well as the static symbols, so that
429 find_pc_partial_function doesn't use a minimal symbol and thus
430 cache a bad endaddr. */
431 for (int i
= 0; i
< psymtab
->n_global_syms
; i
++)
434 = objfile
->partial_symtabs
->global_psymbols
[psymtab
->globals_offset
437 if (p
->domain
== VAR_DOMAIN
438 && p
->aclass
== LOC_BLOCK
439 && pc
>= p
->address (objfile
)
440 && (p
->address (objfile
) > best_pc
441 || (psymtab
->text_low (objfile
) == 0
442 && best_pc
== 0 && p
->address (objfile
) == 0)))
444 if (section
!= NULL
) /* Match on a specific section. */
446 if (!matching_obj_sections (p
->obj_section (objfile
),
450 best_pc
= p
->address (objfile
);
455 for (int i
= 0; i
< psymtab
->n_static_syms
; i
++)
458 = objfile
->partial_symtabs
->static_psymbols
[psymtab
->statics_offset
461 if (p
->domain
== VAR_DOMAIN
462 && p
->aclass
== LOC_BLOCK
463 && pc
>= p
->address (objfile
)
464 && (p
->address (objfile
) > best_pc
465 || (psymtab
->text_low (objfile
) == 0
466 && best_pc
== 0 && p
->address (objfile
) == 0)))
468 if (section
!= NULL
) /* Match on a specific section. */
470 if (!matching_obj_sections (p
->obj_section (objfile
),
474 best_pc
= p
->address (objfile
);
482 /* Psymtab version of lookup_symbol. See its definition in
483 the definition of quick_symbol_functions in symfile.h. */
485 static struct compunit_symtab
*
486 psym_lookup_symbol (struct objfile
*objfile
,
487 int block_index
, const char *name
,
488 const domain_enum domain
)
490 const int psymtab_index
= (block_index
== GLOBAL_BLOCK
? 1 : 0);
491 struct compunit_symtab
*stab_best
= NULL
;
493 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
495 for (partial_symtab
*ps
: require_partial_symbols (objfile
, 1))
497 if (!ps
->readin
&& lookup_partial_symbol (objfile
, ps
, name
,
498 psymtab_index
, domain
))
500 struct symbol
*sym
, *with_opaque
= NULL
;
501 struct compunit_symtab
*stab
= psymtab_to_symtab (objfile
, ps
);
502 /* Note: While psymtab_to_symtab can return NULL if the
503 partial symtab is empty, we can assume it won't here
504 because lookup_partial_symbol succeeded. */
505 const struct blockvector
*bv
= COMPUNIT_BLOCKVECTOR (stab
);
506 struct block
*block
= BLOCKVECTOR_BLOCK (bv
, block_index
);
508 sym
= block_find_symbol (block
, name
, domain
,
509 block_find_non_opaque_type_preferred
,
512 /* Some caution must be observed with overloaded functions
513 and methods, since the index will not contain any overload
514 information (but NAME might contain it). */
517 && SYMBOL_MATCHES_SEARCH_NAME (sym
, lookup_name
))
519 if (with_opaque
!= NULL
520 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque
, lookup_name
))
523 /* Keep looking through other psymtabs. */
530 /* Returns true if PSYM matches LOOKUP_NAME. */
533 psymbol_name_matches (partial_symbol
*psym
,
534 const lookup_name_info
&lookup_name
)
536 const language_defn
*lang
= language_def (psym
->language
);
537 symbol_name_matcher_ftype
*name_match
538 = get_symbol_name_matcher (lang
, lookup_name
);
539 return name_match (symbol_search_name (psym
), lookup_name
, NULL
);
542 /* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
543 the global block of PST if GLOBAL, and otherwise the static block.
544 MATCH is the comparison operation that returns true iff MATCH (s,
545 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
546 non-null, the symbols in the block are assumed to be ordered
547 according to it (allowing binary search). It must be compatible
548 with MATCH. Returns the symbol, if found, and otherwise NULL. */
550 static struct partial_symbol
*
551 match_partial_symbol (struct objfile
*objfile
,
552 struct partial_symtab
*pst
, int global
,
553 const char *name
, domain_enum domain
,
554 symbol_name_match_type match_type
,
555 symbol_compare_ftype
*ordered_compare
)
557 struct partial_symbol
**start
, **psym
;
558 struct partial_symbol
**top
, **real_top
, **bottom
, **center
;
559 int length
= (global
? pst
->n_global_syms
: pst
->n_static_syms
);
560 int do_linear_search
= 1;
565 lookup_name_info
lookup_name (name
, match_type
);
568 &objfile
->partial_symtabs
->global_psymbols
[pst
->globals_offset
] :
569 &objfile
->partial_symtabs
->static_psymbols
[pst
->statics_offset
]);
571 if (global
&& ordered_compare
) /* Can use a binary search. */
573 do_linear_search
= 0;
575 /* Binary search. This search is guaranteed to end with center
576 pointing at the earliest partial symbol whose name might be
577 correct. At that point *all* partial symbols with an
578 appropriate name will be checked against the correct
582 top
= start
+ length
- 1;
586 center
= bottom
+ (top
- bottom
) / 2;
587 gdb_assert (center
< top
);
589 enum language lang
= (*center
)->language
;
591 = lookup_name
.language_lookup_name (lang
).c_str ();
593 if (ordered_compare (symbol_search_name (*center
), lang_ln
) >= 0)
598 gdb_assert (top
== bottom
);
600 while (top
<= real_top
601 && psymbol_name_matches (*top
, lookup_name
))
603 if (symbol_matches_domain ((*top
)->language
,
604 (*top
)->domain
, domain
))
610 /* Can't use a binary search or else we found during the binary search that
611 we should also do a linear search. */
613 if (do_linear_search
)
615 for (psym
= start
; psym
< start
+ length
; psym
++)
617 if (symbol_matches_domain ((*psym
)->language
,
618 (*psym
)->domain
, domain
)
619 && psymbol_name_matches (*psym
, lookup_name
))
627 /* Returns the name used to search psymtabs. Unlike symtabs, psymtabs do
628 not contain any method/function instance information (since this would
629 force reading type information while reading psymtabs). Therefore,
630 if NAME contains overload information, it must be stripped before searching
633 static gdb::unique_xmalloc_ptr
<char>
634 psymtab_search_name (const char *name
)
636 switch (current_language
->la_language
)
640 if (strchr (name
, '('))
642 gdb::unique_xmalloc_ptr
<char> ret
= cp_remove_params (name
);
654 return gdb::unique_xmalloc_ptr
<char> (xstrdup (name
));
657 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
658 Check the global symbols if GLOBAL, the static symbols if not. */
660 static struct partial_symbol
*
661 lookup_partial_symbol (struct objfile
*objfile
,
662 struct partial_symtab
*pst
, const char *name
,
663 int global
, domain_enum domain
)
665 struct partial_symbol
**start
, **psym
;
666 struct partial_symbol
**top
, **real_top
, **bottom
, **center
;
667 int length
= (global
? pst
->n_global_syms
: pst
->n_static_syms
);
668 int do_linear_search
= 1;
673 gdb::unique_xmalloc_ptr
<char> search_name
= psymtab_search_name (name
);
675 lookup_name_info
lookup_name (search_name
.get (), symbol_name_match_type::FULL
);
678 &objfile
->partial_symtabs
->global_psymbols
[pst
->globals_offset
] :
679 &objfile
->partial_symtabs
->static_psymbols
[pst
->statics_offset
]);
681 if (global
) /* This means we can use a binary search. */
683 do_linear_search
= 0;
685 /* Binary search. This search is guaranteed to end with center
686 pointing at the earliest partial symbol whose name might be
687 correct. At that point *all* partial symbols with an
688 appropriate name will be checked against the correct
692 top
= start
+ length
- 1;
696 center
= bottom
+ (top
- bottom
) / 2;
698 internal_error (__FILE__
, __LINE__
,
699 _("failed internal consistency check"));
700 if (strcmp_iw_ordered (symbol_search_name (*center
),
701 search_name
.get ()) >= 0)
710 if (!(top
== bottom
))
711 internal_error (__FILE__
, __LINE__
,
712 _("failed internal consistency check"));
714 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
715 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
716 while (top
>= start
&& symbol_matches_search_name (*top
, lookup_name
))
719 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
722 while (top
<= real_top
&& symbol_matches_search_name (*top
, lookup_name
))
724 if (symbol_matches_domain ((*top
)->language
,
725 (*top
)->domain
, domain
))
731 /* Can't use a binary search or else we found during the binary search that
732 we should also do a linear search. */
734 if (do_linear_search
)
736 for (psym
= start
; psym
< start
+ length
; psym
++)
738 if (symbol_matches_domain ((*psym
)->language
,
739 (*psym
)->domain
, domain
)
740 && symbol_matches_search_name (*psym
, lookup_name
))
748 /* Get the symbol table that corresponds to a partial_symtab.
749 This is fast after the first time you do it.
750 The result will be NULL if the primary symtab has no symbols,
751 which can happen. Otherwise the result is the primary symtab
752 that contains PST. */
754 static struct compunit_symtab
*
755 psymtab_to_symtab (struct objfile
*objfile
, struct partial_symtab
*pst
)
757 /* If it is a shared psymtab, find an unshared psymtab that includes
758 it. Any such psymtab will do. */
759 while (pst
->user
!= NULL
)
762 /* If it's been looked up before, return it. */
763 if (pst
->compunit_symtab
)
764 return pst
->compunit_symtab
;
766 /* If it has not yet been read in, read it. */
769 scoped_restore decrementer
= increment_reading_symtab ();
771 (*pst
->read_symtab
) (pst
, objfile
);
774 return pst
->compunit_symtab
;
777 /* Psymtab version of find_last_source_symtab. See its definition in
778 the definition of quick_symbol_functions in symfile.h. */
780 static struct symtab
*
781 psym_find_last_source_symtab (struct objfile
*ofp
)
783 struct partial_symtab
*cs_pst
= NULL
;
785 for (partial_symtab
*ps
: require_partial_symbols (ofp
, 1))
787 const char *name
= ps
->filename
;
788 int len
= strlen (name
);
790 if (!(len
> 2 && (strcmp (&name
[len
- 2], ".h") == 0
791 || strcmp (name
, "<<C++-namespaces>>") == 0)))
799 internal_error (__FILE__
, __LINE__
,
800 _("select_source_symtab: "
801 "readin pst found and no symtabs."));
805 struct compunit_symtab
*cust
= psymtab_to_symtab (ofp
, cs_pst
);
809 return compunit_primary_filetab (cust
);
815 /* Psymtab version of forget_cached_source_info. See its definition in
816 the definition of quick_symbol_functions in symfile.h. */
819 psym_forget_cached_source_info (struct objfile
*objfile
)
821 for (partial_symtab
*pst
: require_partial_symbols (objfile
, 1))
823 if (pst
->fullname
!= NULL
)
825 xfree (pst
->fullname
);
826 pst
->fullname
= NULL
;
832 print_partial_symbols (struct gdbarch
*gdbarch
, struct objfile
*objfile
,
833 struct partial_symbol
**p
, int count
, const char *what
,
834 struct ui_file
*outfile
)
836 fprintf_filtered (outfile
, " %s partial symbols:\n", what
);
840 fprintf_filtered (outfile
, " `%s'", (*p
)->name
);
841 if (symbol_demangled_name (*p
) != NULL
)
843 fprintf_filtered (outfile
, " `%s'", symbol_demangled_name (*p
));
845 fputs_filtered (", ", outfile
);
846 switch ((*p
)->domain
)
849 fputs_filtered ("undefined domain, ", outfile
);
852 /* This is the usual thing -- don't print it. */
855 fputs_filtered ("struct domain, ", outfile
);
858 fputs_filtered ("label domain, ", outfile
);
861 fputs_filtered ("<invalid domain>, ", outfile
);
864 switch ((*p
)->aclass
)
867 fputs_filtered ("undefined", outfile
);
870 fputs_filtered ("constant int", outfile
);
873 fputs_filtered ("static", outfile
);
876 fputs_filtered ("register", outfile
);
879 fputs_filtered ("pass by value", outfile
);
882 fputs_filtered ("pass by reference", outfile
);
884 case LOC_REGPARM_ADDR
:
885 fputs_filtered ("register address parameter", outfile
);
888 fputs_filtered ("stack parameter", outfile
);
891 fputs_filtered ("type", outfile
);
894 fputs_filtered ("label", outfile
);
897 fputs_filtered ("function", outfile
);
899 case LOC_CONST_BYTES
:
900 fputs_filtered ("constant bytes", outfile
);
903 fputs_filtered ("unresolved", outfile
);
905 case LOC_OPTIMIZED_OUT
:
906 fputs_filtered ("optimized out", outfile
);
909 fputs_filtered ("computed at runtime", outfile
);
912 fputs_filtered ("<invalid location>", outfile
);
915 fputs_filtered (", ", outfile
);
916 fputs_filtered (paddress (gdbarch
, (*p
)->unrelocated_address ()), outfile
);
917 fprintf_filtered (outfile
, "\n");
923 dump_psymtab (struct objfile
*objfile
, struct partial_symtab
*psymtab
,
924 struct ui_file
*outfile
)
926 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
929 if (psymtab
->anonymous
)
931 fprintf_filtered (outfile
, "\nAnonymous partial symtab (%s) ",
936 fprintf_filtered (outfile
, "\nPartial symtab for source file %s ",
939 fprintf_filtered (outfile
, "(object ");
940 gdb_print_host_address (psymtab
, outfile
);
941 fprintf_filtered (outfile
, ")\n\n");
942 fprintf_filtered (outfile
, " Read from object file %s (",
943 objfile_name (objfile
));
944 gdb_print_host_address (objfile
, outfile
);
945 fprintf_filtered (outfile
, ")\n");
949 fprintf_filtered (outfile
,
950 " Full symtab was read (at ");
951 gdb_print_host_address (psymtab
->compunit_symtab
, outfile
);
952 fprintf_filtered (outfile
, " by function at ");
953 gdb_print_host_address (psymtab
->read_symtab
, outfile
);
954 fprintf_filtered (outfile
, ")\n");
957 fprintf_filtered (outfile
, " Symbols cover text addresses ");
958 fputs_filtered (paddress (gdbarch
, psymtab
->text_low (objfile
)), outfile
);
959 fprintf_filtered (outfile
, "-");
960 fputs_filtered (paddress (gdbarch
, psymtab
->text_high (objfile
)), outfile
);
961 fprintf_filtered (outfile
, "\n");
962 fprintf_filtered (outfile
, " Address map supported - %s.\n",
963 psymtab
->psymtabs_addrmap_supported
? "yes" : "no");
964 fprintf_filtered (outfile
, " Depends on %d other partial symtabs.\n",
965 psymtab
->number_of_dependencies
);
966 for (i
= 0; i
< psymtab
->number_of_dependencies
; i
++)
968 fprintf_filtered (outfile
, " %d ", i
);
969 gdb_print_host_address (psymtab
->dependencies
[i
], outfile
);
970 fprintf_filtered (outfile
, " %s\n",
971 psymtab
->dependencies
[i
]->filename
);
973 if (psymtab
->user
!= NULL
)
975 fprintf_filtered (outfile
, " Shared partial symtab with user ");
976 gdb_print_host_address (psymtab
->user
, outfile
);
977 fprintf_filtered (outfile
, "\n");
979 if (psymtab
->n_global_syms
> 0)
981 print_partial_symbols
983 &objfile
->partial_symtabs
->global_psymbols
[psymtab
->globals_offset
],
984 psymtab
->n_global_syms
, "Global", outfile
);
986 if (psymtab
->n_static_syms
> 0)
988 print_partial_symbols
990 &objfile
->partial_symtabs
->static_psymbols
[psymtab
->statics_offset
],
991 psymtab
->n_static_syms
, "Static", outfile
);
993 fprintf_filtered (outfile
, "\n");
996 /* Psymtab version of print_stats. See its definition in
997 the definition of quick_symbol_functions in symfile.h. */
1000 psym_print_stats (struct objfile
*objfile
)
1005 for (partial_symtab
*ps
: require_partial_symbols (objfile
, 1))
1007 if (ps
->readin
== 0)
1010 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i
);
1013 /* Psymtab version of dump. See its definition in
1014 the definition of quick_symbol_functions in symfile.h. */
1017 psym_dump (struct objfile
*objfile
)
1019 struct partial_symtab
*psymtab
;
1021 if (objfile
->partial_symtabs
->psymtabs
)
1023 printf_filtered ("Psymtabs:\n");
1024 for (psymtab
= objfile
->partial_symtabs
->psymtabs
;
1026 psymtab
= psymtab
->next
)
1028 printf_filtered ("%s at ",
1030 gdb_print_host_address (psymtab
, gdb_stdout
);
1031 printf_filtered (", ");
1034 printf_filtered ("\n\n");
1038 /* Psymtab version of expand_symtabs_for_function. See its definition in
1039 the definition of quick_symbol_functions in symfile.h. */
1042 psym_expand_symtabs_for_function (struct objfile
*objfile
,
1043 const char *func_name
)
1045 for (partial_symtab
*ps
: require_partial_symbols (objfile
, 1))
1050 if ((lookup_partial_symbol (objfile
, ps
, func_name
, 1, VAR_DOMAIN
)
1052 || (lookup_partial_symbol (objfile
, ps
, func_name
, 0, VAR_DOMAIN
)
1054 psymtab_to_symtab (objfile
, ps
);
1058 /* Psymtab version of expand_all_symtabs. See its definition in
1059 the definition of quick_symbol_functions in symfile.h. */
1062 psym_expand_all_symtabs (struct objfile
*objfile
)
1064 for (partial_symtab
*psymtab
: require_partial_symbols (objfile
, 1))
1065 psymtab_to_symtab (objfile
, psymtab
);
1068 /* Psymtab version of expand_symtabs_with_fullname. See its definition in
1069 the definition of quick_symbol_functions in symfile.h. */
1072 psym_expand_symtabs_with_fullname (struct objfile
*objfile
,
1073 const char *fullname
)
1075 for (partial_symtab
*p
: require_partial_symbols (objfile
, 1))
1077 /* Anonymous psymtabs don't have a name of a source file. */
1081 /* psymtab_to_fullname tries to open the file which is slow.
1082 Don't call it if we know the basenames don't match. */
1083 if ((basenames_may_differ
1084 || filename_cmp (lbasename (fullname
), lbasename (p
->filename
)) == 0)
1085 && filename_cmp (fullname
, psymtab_to_fullname (p
)) == 0)
1086 psymtab_to_symtab (objfile
, p
);
1090 /* Psymtab version of map_symbol_filenames. See its definition in
1091 the definition of quick_symbol_functions in symfile.h. */
1094 psym_map_symbol_filenames (struct objfile
*objfile
,
1095 symbol_filename_ftype
*fun
, void *data
,
1098 for (partial_symtab
*ps
: require_partial_symbols (objfile
, 1))
1100 const char *fullname
;
1105 /* We can skip shared psymtabs here, because any file name will be
1106 attached to the unshared psymtab. */
1107 if (ps
->user
!= NULL
)
1110 /* Anonymous psymtabs don't have a file name. */
1116 fullname
= psymtab_to_fullname (ps
);
1119 (*fun
) (ps
->filename
, fullname
, data
);
1123 /* Finds the fullname that a partial_symtab represents.
1125 If this functions finds the fullname, it will save it in ps->fullname
1126 and it will also return the value.
1128 If this function fails to find the file that this partial_symtab represents,
1129 NULL will be returned and ps->fullname will be set to NULL. */
1132 psymtab_to_fullname (struct partial_symtab
*ps
)
1134 gdb_assert (!ps
->anonymous
);
1136 /* Use cached copy if we have it.
1137 We rely on forget_cached_source_info being called appropriately
1138 to handle cases like the file being moved. */
1139 if (ps
->fullname
== NULL
)
1141 gdb::unique_xmalloc_ptr
<char> fullname
;
1142 scoped_fd fd
= find_and_open_source (ps
->filename
, ps
->dirname
,
1144 ps
->fullname
= fullname
.release ();
1148 /* rewrite_source_path would be applied by find_and_open_source, we
1149 should report the pathname where GDB tried to find the file. */
1151 if (ps
->dirname
== NULL
|| IS_ABSOLUTE_PATH (ps
->filename
))
1152 fullname
.reset (xstrdup (ps
->filename
));
1154 fullname
.reset (concat (ps
->dirname
, SLASH_STRING
,
1155 ps
->filename
, (char *) NULL
));
1157 ps
->fullname
= rewrite_source_path (fullname
.get ()).release ();
1158 if (ps
->fullname
== NULL
)
1159 ps
->fullname
= fullname
.release ();
1163 return ps
->fullname
;
1166 /* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1167 according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1168 BLOCK is assumed to come from OBJFILE. Returns 1 iff CALLBACK
1169 ever returns non-zero, and otherwise returns 0. */
1172 map_block (const char *name
, domain_enum domain
, struct objfile
*objfile
,
1173 struct block
*block
,
1174 int (*callback
) (struct block
*, struct symbol
*, void *),
1175 void *data
, symbol_name_match_type match
)
1177 struct block_iterator iter
;
1180 lookup_name_info
lookup_name (name
, match
);
1182 for (sym
= block_iter_match_first (block
, lookup_name
, &iter
);
1184 sym
= block_iter_match_next (lookup_name
, &iter
))
1186 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
1187 SYMBOL_DOMAIN (sym
), domain
))
1189 if (callback (block
, sym
, data
))
1197 /* Psymtab version of map_matching_symbols. See its definition in
1198 the definition of quick_symbol_functions in symfile.h. */
1201 psym_map_matching_symbols (struct objfile
*objfile
,
1202 const char *name
, domain_enum domain
,
1204 int (*callback
) (struct block
*,
1205 struct symbol
*, void *),
1207 symbol_name_match_type match
,
1208 symbol_compare_ftype
*ordered_compare
)
1210 const int block_kind
= global
? GLOBAL_BLOCK
: STATIC_BLOCK
;
1212 for (partial_symtab
*ps
: require_partial_symbols (objfile
, 1))
1216 || match_partial_symbol (objfile
, ps
, global
, name
, domain
, match
,
1219 struct compunit_symtab
*cust
= psymtab_to_symtab (objfile
, ps
);
1220 struct block
*block
;
1224 block
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust
), block_kind
);
1225 if (map_block (name
, domain
, objfile
, block
,
1226 callback
, data
, match
))
1228 if (callback (block
, NULL
, data
))
1234 /* A helper for psym_expand_symtabs_matching that handles searching
1235 included psymtabs. This returns true if a symbol is found, and
1236 false otherwise. It also updates the 'searched_flag' on the
1237 various psymtabs that it searches. */
1240 recursively_search_psymtabs
1241 (struct partial_symtab
*ps
,
1242 struct objfile
*objfile
,
1243 enum search_domain domain
,
1244 const lookup_name_info
&lookup_name
,
1245 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> sym_matcher
)
1248 enum psymtab_search_status result
= PST_SEARCHED_AND_NOT_FOUND
;
1251 if (ps
->searched_flag
!= PST_NOT_SEARCHED
)
1252 return ps
->searched_flag
== PST_SEARCHED_AND_FOUND
;
1254 /* Recurse into shared psymtabs first, because they may have already
1255 been searched, and this could save some time. */
1256 for (i
= 0; i
< ps
->number_of_dependencies
; ++i
)
1260 /* Skip non-shared dependencies, these are handled elsewhere. */
1261 if (ps
->dependencies
[i
]->user
== NULL
)
1264 r
= recursively_search_psymtabs (ps
->dependencies
[i
],
1265 objfile
, domain
, lookup_name
,
1269 ps
->searched_flag
= PST_SEARCHED_AND_FOUND
;
1274 partial_symbol
**gbound
1275 = (objfile
->partial_symtabs
->global_psymbols
.data ()
1276 + ps
->globals_offset
+ ps
->n_global_syms
);
1277 partial_symbol
**sbound
1278 = (objfile
->partial_symtabs
->static_psymbols
.data ()
1279 + ps
->statics_offset
+ ps
->n_static_syms
);
1280 partial_symbol
**bound
= gbound
;
1282 /* Go through all of the symbols stored in a partial
1283 symtab in one loop. */
1284 partial_symbol
**psym
= (objfile
->partial_symtabs
->global_psymbols
.data ()
1285 + ps
->globals_offset
);
1290 if (bound
== gbound
&& ps
->n_static_syms
!= 0)
1292 psym
= (objfile
->partial_symtabs
->static_psymbols
.data ()
1293 + ps
->statics_offset
);
1304 if ((domain
== ALL_DOMAIN
1305 || (domain
== VARIABLES_DOMAIN
1306 && (*psym
)->aclass
!= LOC_TYPEDEF
1307 && (*psym
)->aclass
!= LOC_BLOCK
)
1308 || (domain
== FUNCTIONS_DOMAIN
1309 && (*psym
)->aclass
== LOC_BLOCK
)
1310 || (domain
== TYPES_DOMAIN
1311 && (*psym
)->aclass
== LOC_TYPEDEF
))
1312 && psymbol_name_matches (*psym
, lookup_name
)
1313 && (sym_matcher
== NULL
|| sym_matcher (symbol_search_name (*psym
))))
1315 /* Found a match, so notify our caller. */
1316 result
= PST_SEARCHED_AND_FOUND
;
1323 ps
->searched_flag
= result
;
1324 return result
== PST_SEARCHED_AND_FOUND
;
1327 /* Psymtab version of expand_symtabs_matching. See its definition in
1328 the definition of quick_symbol_functions in symfile.h. */
1331 psym_expand_symtabs_matching
1332 (struct objfile
*objfile
,
1333 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1334 const lookup_name_info
&lookup_name_in
,
1335 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1336 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1337 enum search_domain domain
)
1339 lookup_name_info lookup_name
= lookup_name_in
.make_ignore_params ();
1341 /* Clear the search flags. */
1342 for (partial_symtab
*ps
: require_partial_symbols (objfile
, 1))
1343 ps
->searched_flag
= PST_NOT_SEARCHED
;
1345 for (partial_symtab
*ps
: objfile_psymtabs (objfile
))
1352 /* We skip shared psymtabs because file-matching doesn't apply
1353 to them; but we search them later in the loop. */
1354 if (ps
->user
!= NULL
)
1364 match
= file_matcher (ps
->filename
, false);
1367 /* Before we invoke realpath, which can get expensive when many
1368 files are involved, do a quick comparison of the basenames. */
1369 if (basenames_may_differ
1370 || file_matcher (lbasename (ps
->filename
), true))
1371 match
= file_matcher (psymtab_to_fullname (ps
), false);
1377 if (recursively_search_psymtabs (ps
, objfile
, domain
,
1378 lookup_name
, symbol_matcher
))
1380 struct compunit_symtab
*symtab
=
1381 psymtab_to_symtab (objfile
, ps
);
1383 if (expansion_notify
!= NULL
)
1384 expansion_notify (symtab
);
1389 /* Psymtab version of has_symbols. See its definition in
1390 the definition of quick_symbol_functions in symfile.h. */
1393 psym_has_symbols (struct objfile
*objfile
)
1395 return objfile
->partial_symtabs
->psymtabs
!= NULL
;
1398 /* Helper function for psym_find_compunit_symtab_by_address that fills
1399 in psymbol_map for a given range of psymbols. */
1402 psym_fill_psymbol_map (struct objfile
*objfile
,
1403 struct partial_symtab
*psymtab
,
1404 std::set
<CORE_ADDR
> *seen_addrs
,
1405 const std::vector
<partial_symbol
*> &symbols
,
1409 for (int i
= 0; i
< length
; ++i
)
1411 struct partial_symbol
*psym
= symbols
[start
+ i
];
1413 if (psym
->aclass
== LOC_STATIC
)
1415 CORE_ADDR addr
= psym
->address (objfile
);
1416 if (seen_addrs
->find (addr
) == seen_addrs
->end ())
1418 seen_addrs
->insert (addr
);
1419 objfile
->psymbol_map
.emplace_back (addr
, psymtab
);
1425 /* See find_compunit_symtab_by_address in quick_symbol_functions, in
1428 static compunit_symtab
*
1429 psym_find_compunit_symtab_by_address (struct objfile
*objfile
,
1432 if (objfile
->psymbol_map
.empty ())
1434 std::set
<CORE_ADDR
> seen_addrs
;
1436 for (partial_symtab
*pst
: require_partial_symbols (objfile
, 1))
1438 psym_fill_psymbol_map (objfile
, pst
,
1440 objfile
->partial_symtabs
->global_psymbols
,
1441 pst
->globals_offset
,
1442 pst
->n_global_syms
);
1443 psym_fill_psymbol_map (objfile
, pst
,
1445 objfile
->partial_symtabs
->static_psymbols
,
1446 pst
->statics_offset
,
1447 pst
->n_static_syms
);
1450 objfile
->psymbol_map
.shrink_to_fit ();
1452 std::sort (objfile
->psymbol_map
.begin (), objfile
->psymbol_map
.end (),
1453 [] (const std::pair
<CORE_ADDR
, partial_symtab
*> &a
,
1454 const std::pair
<CORE_ADDR
, partial_symtab
*> &b
)
1456 return a
.first
< b
.first
;
1460 auto iter
= std::lower_bound
1461 (objfile
->psymbol_map
.begin (), objfile
->psymbol_map
.end (), address
,
1462 [] (const std::pair
<CORE_ADDR
, partial_symtab
*> &a
,
1468 if (iter
== objfile
->psymbol_map
.end () || iter
->first
!= address
)
1471 return psymtab_to_symtab (objfile
, iter
->second
);
1474 const struct quick_symbol_functions psym_functions
=
1477 psym_find_last_source_symtab
,
1478 psym_forget_cached_source_info
,
1479 psym_map_symtabs_matching_filename
,
1483 psym_expand_symtabs_for_function
,
1484 psym_expand_all_symtabs
,
1485 psym_expand_symtabs_with_fullname
,
1486 psym_map_matching_symbols
,
1487 psym_expand_symtabs_matching
,
1488 psym_find_pc_sect_compunit_symtab
,
1489 psym_find_compunit_symtab_by_address
,
1490 psym_map_symbol_filenames
1496 sort_pst_symbols (struct objfile
*objfile
, struct partial_symtab
*pst
)
1498 /* Sort the global list; don't sort the static list. */
1499 auto begin
= objfile
->partial_symtabs
->global_psymbols
.begin ();
1500 std::advance (begin
, pst
->globals_offset
);
1502 /* The psymbols for this partial_symtab are currently at the end of the
1504 auto end
= objfile
->partial_symtabs
->global_psymbols
.end ();
1506 std::sort (begin
, end
, [] (partial_symbol
*s1
, partial_symbol
*s2
)
1508 return strcmp_iw_ordered (symbol_search_name (s1
),
1509 symbol_search_name (s2
)) < 0;
1513 /* Allocate and partially fill a partial symtab. It will be
1514 completely filled at the end of the symbol list.
1516 FILENAME is the name of the symbol-file we are reading from. */
1518 struct partial_symtab
*
1519 start_psymtab_common (struct objfile
*objfile
,
1520 const char *filename
,
1523 struct partial_symtab
*psymtab
;
1525 psymtab
= allocate_psymtab (filename
, objfile
);
1526 psymtab
->set_text_low (textlow
);
1527 psymtab
->set_text_high (psymtab
->raw_text_low ()); /* default */
1528 psymtab
->globals_offset
= objfile
->partial_symtabs
->global_psymbols
.size ();
1529 psymtab
->statics_offset
= objfile
->partial_symtabs
->static_psymbols
.size ();
1533 /* Perform "finishing up" operations of a partial symtab. */
1536 end_psymtab_common (struct objfile
*objfile
, struct partial_symtab
*pst
)
1538 pst
->n_global_syms
= (objfile
->partial_symtabs
->global_psymbols
.size ()
1539 - pst
->globals_offset
);
1540 pst
->n_static_syms
= (objfile
->partial_symtabs
->static_psymbols
.size ()
1541 - pst
->statics_offset
);
1543 sort_pst_symbols (objfile
, pst
);
1546 /* Calculate a hash code for the given partial symbol. The hash is
1547 calculated using the symbol's value, language, domain, class
1548 and name. These are the values which are set by
1549 add_psymbol_to_bcache. */
1551 static unsigned long
1552 psymbol_hash (const void *addr
, int length
)
1554 unsigned long h
= 0;
1555 struct partial_symbol
*psymbol
= (struct partial_symbol
*) addr
;
1556 unsigned int lang
= psymbol
->language
;
1557 unsigned int domain
= psymbol
->domain
;
1558 unsigned int theclass
= psymbol
->aclass
;
1560 h
= hash_continue (&psymbol
->value
, sizeof (psymbol
->value
), h
);
1561 h
= hash_continue (&lang
, sizeof (unsigned int), h
);
1562 h
= hash_continue (&domain
, sizeof (unsigned int), h
);
1563 h
= hash_continue (&theclass
, sizeof (unsigned int), h
);
1564 /* Note that psymbol names are interned via symbol_set_names, so
1565 there's no need to hash the contents of the name here. */
1566 h
= hash_continue (&psymbol
->name
,
1567 sizeof (psymbol
->name
), h
);
1572 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1573 For the comparison this function uses a symbols value,
1574 language, domain, class and name. */
1577 psymbol_compare (const void *addr1
, const void *addr2
, int length
)
1579 struct partial_symbol
*sym1
= (struct partial_symbol
*) addr1
;
1580 struct partial_symbol
*sym2
= (struct partial_symbol
*) addr2
;
1582 return (memcmp (&sym1
->value
, &sym2
->value
,
1583 sizeof (sym1
->value
)) == 0
1584 && sym1
->language
== sym2
->language
1585 && sym1
->domain
== sym2
->domain
1586 && sym1
->aclass
== sym2
->aclass
1587 /* Note that psymbol names are interned via
1588 symbol_set_names, so there's no need to compare the
1589 contents of the name here. */
1590 && sym1
->name
== sym2
->name
);
1593 /* Initialize a partial symbol bcache. */
1595 struct psymbol_bcache
*
1596 psymbol_bcache_init (void)
1598 struct psymbol_bcache
*bcache
= XCNEW (struct psymbol_bcache
);
1600 bcache
->bcache
= bcache_xmalloc (psymbol_hash
, psymbol_compare
);
1604 /* Free a partial symbol bcache. */
1607 psymbol_bcache_free (struct psymbol_bcache
*bcache
)
1612 bcache_xfree (bcache
->bcache
);
1616 /* Return the internal bcache of the psymbol_bcache BCACHE. */
1619 psymbol_bcache_get_bcache (struct psymbol_bcache
*bcache
)
1621 return bcache
->bcache
;
1624 /* Find a copy of the SYM in BCACHE. If BCACHE has never seen this
1625 symbol before, add a copy to BCACHE. In either case, return a pointer
1626 to BCACHE's copy of the symbol. If optional ADDED is not NULL, return
1627 1 in case of new entry or 0 if returning an old entry. */
1629 static struct partial_symbol
*
1630 psymbol_bcache_full (struct partial_symbol
*sym
,
1631 struct psymbol_bcache
*bcache
,
1634 return ((struct partial_symbol
*)
1635 bcache_full (sym
, sizeof (struct partial_symbol
), bcache
->bcache
,
1639 /* Helper function, initialises partial symbol structure and stashes
1640 it into objfile's bcache. Note that our caching mechanism will
1641 use all fields of struct partial_symbol to determine hash value of the
1642 structure. In other words, having two symbols with the same name but
1643 different domain (or address) is possible and correct. */
1645 static struct partial_symbol
*
1646 add_psymbol_to_bcache (const char *name
, int namelength
, int copy_name
,
1648 enum address_class theclass
,
1651 enum language language
, struct objfile
*objfile
,
1654 struct partial_symbol psymbol
;
1656 psymbol
.set_unrelocated_address (coreaddr
);
1657 psymbol
.section
= section
;
1658 psymbol
.domain
= domain
;
1659 psymbol
.aclass
= theclass
;
1661 memset (&psymbol
.language_specific
, 0, sizeof (psymbol
.language_specific
));
1662 psymbol
.ada_mangled
= 0;
1663 symbol_set_language (&psymbol
, language
,
1664 objfile
->partial_symtabs
->obstack ());
1665 symbol_set_names (&psymbol
, name
, namelength
, copy_name
, objfile
->per_bfd
);
1667 /* Stash the partial symbol away in the cache. */
1668 return psymbol_bcache_full (&psymbol
,
1669 objfile
->partial_symtabs
->psymbol_cache
,
1673 /* Helper function, adds partial symbol to the given partial symbol list. */
1676 append_psymbol_to_list (std::vector
<partial_symbol
*> *list
,
1677 struct partial_symbol
*psym
,
1678 struct objfile
*objfile
)
1680 list
->push_back (psym
);
1681 OBJSTAT (objfile
, n_psyms
++);
1684 /* Add a symbol with a long value to a psymtab.
1685 Since one arg is a struct, we pass in a ptr and deref it (sigh).
1686 The only value we need to store for psyms is an address.
1687 For all other psyms pass zero for COREADDR.
1688 Return the partial symbol that has been added. */
1691 add_psymbol_to_list (const char *name
, int namelength
, int copy_name
,
1693 enum address_class theclass
,
1695 psymbol_placement where
,
1697 enum language language
, struct objfile
*objfile
)
1699 struct partial_symbol
*psym
;
1703 /* Stash the partial symbol away in the cache. */
1704 psym
= add_psymbol_to_bcache (name
, namelength
, copy_name
, domain
, theclass
,
1705 section
, coreaddr
, language
, objfile
, &added
);
1707 /* Do not duplicate global partial symbols. */
1708 if (where
== psymbol_placement::GLOBAL
&& !added
)
1711 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1712 std::vector
<partial_symbol
*> *list
1713 = (where
== psymbol_placement::STATIC
1714 ? &objfile
->partial_symtabs
->static_psymbols
1715 : &objfile
->partial_symtabs
->global_psymbols
);
1716 append_psymbol_to_list (list
, psym
, objfile
);
1719 /* See psympriv.h. */
1722 init_psymbol_list (struct objfile
*objfile
, int total_symbols
)
1724 if (objfile
->partial_symtabs
->global_psymbols
.capacity () == 0
1725 && objfile
->partial_symtabs
->static_psymbols
.capacity () == 0)
1727 /* Current best guess is that approximately a twentieth of the
1728 total symbols (in a debugging file) are global or static
1729 oriented symbols, then multiply that by slop factor of
1731 objfile
->partial_symtabs
->global_psymbols
.reserve (total_symbols
/ 10);
1732 objfile
->partial_symtabs
->static_psymbols
.reserve (total_symbols
/ 10);
1736 /* See psympriv.h. */
1738 struct partial_symtab
*
1739 allocate_psymtab (const char *filename
, struct objfile
*objfile
)
1741 struct partial_symtab
*psymtab
1742 = objfile
->partial_symtabs
->allocate_psymtab ();
1745 = (const char *) bcache (filename
, strlen (filename
) + 1,
1746 objfile
->per_bfd
->filename_cache
);
1747 psymtab
->compunit_symtab
= NULL
;
1749 if (symtab_create_debug
)
1751 /* Be a bit clever with debugging messages, and don't print objfile
1752 every time, only when it changes. */
1753 static char *last_objfile_name
= NULL
;
1755 if (last_objfile_name
== NULL
1756 || strcmp (last_objfile_name
, objfile_name (objfile
)) != 0)
1758 xfree (last_objfile_name
);
1759 last_objfile_name
= xstrdup (objfile_name (objfile
));
1760 fprintf_filtered (gdb_stdlog
,
1761 "Creating one or more psymtabs for objfile %s ...\n",
1764 fprintf_filtered (gdb_stdlog
,
1765 "Created psymtab %s for module %s.\n",
1766 host_address_to_string (psymtab
), filename
);
1773 psymtab_storage::discard_psymtab (struct partial_symtab
*pst
)
1775 struct partial_symtab
**prev_pst
;
1778 Empty psymtabs happen as a result of header files which don't
1779 have any symbols in them. There can be a lot of them. But this
1780 check is wrong, in that a psymtab with N_SLINE entries but
1781 nothing else is not empty, but we don't realize that. Fixing
1782 that without slowing things down might be tricky. */
1784 /* First, snip it out of the psymtab chain. */
1786 prev_pst
= &psymtabs
;
1787 while ((*prev_pst
) != pst
)
1788 prev_pst
= &((*prev_pst
)->next
);
1789 (*prev_pst
) = pst
->next
;
1791 /* Next, put it on a free list for recycling. */
1793 pst
->next
= free_psymtabs
;
1794 free_psymtabs
= pst
;
1799 /* We need to pass a couple of items to the addrmap_foreach function,
1802 struct dump_psymtab_addrmap_data
1804 struct objfile
*objfile
;
1805 struct partial_symtab
*psymtab
;
1806 struct ui_file
*outfile
;
1808 /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1809 If so, we want to print the next one as well (since the next addrmap
1810 entry defines the end of the range). */
1811 int previous_matched
;
1814 /* Helper function for dump_psymtab_addrmap to print an addrmap entry. */
1817 dump_psymtab_addrmap_1 (void *datap
, CORE_ADDR start_addr
, void *obj
)
1819 struct dump_psymtab_addrmap_data
*data
1820 = (struct dump_psymtab_addrmap_data
*) datap
;
1821 struct gdbarch
*gdbarch
= get_objfile_arch (data
->objfile
);
1822 struct partial_symtab
*addrmap_psymtab
= (struct partial_symtab
*) obj
;
1823 const char *psymtab_address_or_end
= NULL
;
1827 if (data
->psymtab
== NULL
1828 || data
->psymtab
== addrmap_psymtab
)
1829 psymtab_address_or_end
= host_address_to_string (addrmap_psymtab
);
1830 else if (data
->previous_matched
)
1831 psymtab_address_or_end
= "<ends here>";
1833 if (data
->psymtab
== NULL
1834 || data
->psymtab
== addrmap_psymtab
1835 || data
->previous_matched
)
1837 fprintf_filtered (data
->outfile
, " %s%s %s\n",
1838 data
->psymtab
!= NULL
? " " : "",
1839 paddress (gdbarch
, start_addr
),
1840 psymtab_address_or_end
);
1843 data
->previous_matched
= (data
->psymtab
== NULL
1844 || data
->psymtab
== addrmap_psymtab
);
1849 /* Helper function for maintenance_print_psymbols to print the addrmap
1850 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1853 dump_psymtab_addrmap (struct objfile
*objfile
, struct partial_symtab
*psymtab
,
1854 struct ui_file
*outfile
)
1856 struct dump_psymtab_addrmap_data addrmap_dump_data
;
1858 if ((psymtab
== NULL
1859 || psymtab
->psymtabs_addrmap_supported
)
1860 && objfile
->partial_symtabs
->psymtabs_addrmap
!= NULL
)
1862 addrmap_dump_data
.objfile
= objfile
;
1863 addrmap_dump_data
.psymtab
= psymtab
;
1864 addrmap_dump_data
.outfile
= outfile
;
1865 addrmap_dump_data
.previous_matched
= 0;
1866 fprintf_filtered (outfile
, "%sddress map:\n",
1867 psymtab
== NULL
? "Entire a" : " A");
1868 addrmap_foreach (objfile
->partial_symtabs
->psymtabs_addrmap
,
1869 dump_psymtab_addrmap_1
, &addrmap_dump_data
);
1874 maintenance_print_psymbols (const char *args
, int from_tty
)
1876 struct ui_file
*outfile
= gdb_stdout
;
1877 char *address_arg
= NULL
, *source_arg
= NULL
, *objfile_arg
= NULL
;
1878 int i
, outfile_idx
, found
;
1880 struct obj_section
*section
= NULL
;
1884 gdb_argv
argv (args
);
1886 for (i
= 0; argv
!= NULL
&& argv
[i
] != NULL
; ++i
)
1888 if (strcmp (argv
[i
], "-pc") == 0)
1890 if (argv
[i
+ 1] == NULL
)
1891 error (_("Missing pc value"));
1892 address_arg
= argv
[++i
];
1894 else if (strcmp (argv
[i
], "-source") == 0)
1896 if (argv
[i
+ 1] == NULL
)
1897 error (_("Missing source file"));
1898 source_arg
= argv
[++i
];
1900 else if (strcmp (argv
[i
], "-objfile") == 0)
1902 if (argv
[i
+ 1] == NULL
)
1903 error (_("Missing objfile name"));
1904 objfile_arg
= argv
[++i
];
1906 else if (strcmp (argv
[i
], "--") == 0)
1908 /* End of options. */
1912 else if (argv
[i
][0] == '-')
1914 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1915 error (_("Unknown option: %s"), argv
[i
]);
1922 if (address_arg
!= NULL
&& source_arg
!= NULL
)
1923 error (_("Must specify at most one of -pc and -source"));
1925 stdio_file arg_outfile
;
1927 if (argv
!= NULL
&& argv
[outfile_idx
] != NULL
)
1929 if (argv
[outfile_idx
+ 1] != NULL
)
1930 error (_("Junk at end of command"));
1931 gdb::unique_xmalloc_ptr
<char> outfile_name
1932 (tilde_expand (argv
[outfile_idx
]));
1933 if (!arg_outfile
.open (outfile_name
.get (), FOPEN_WT
))
1934 perror_with_name (outfile_name
.get ());
1935 outfile
= &arg_outfile
;
1938 if (address_arg
!= NULL
)
1940 pc
= parse_and_eval_address (address_arg
);
1941 /* If we fail to find a section, that's ok, try the lookup anyway. */
1942 section
= find_pc_section (pc
);
1946 for (objfile
*objfile
: all_objfiles (current_program_space
))
1948 int printed_objfile_header
= 0;
1949 int print_for_objfile
= 1;
1952 if (objfile_arg
!= NULL
)
1954 = compare_filenames_for_search (objfile_name (objfile
),
1956 if (!print_for_objfile
)
1959 if (address_arg
!= NULL
)
1961 struct bound_minimal_symbol msymbol
= { NULL
, NULL
};
1963 /* We don't assume each pc has a unique objfile (this is for
1965 struct partial_symtab
*ps
= find_pc_sect_psymtab (objfile
, pc
,
1969 if (!printed_objfile_header
)
1971 outfile
->printf ("\nPartial symtabs for objfile %s\n",
1972 objfile_name (objfile
));
1973 printed_objfile_header
= 1;
1975 dump_psymtab (objfile
, ps
, outfile
);
1976 dump_psymtab_addrmap (objfile
, ps
, outfile
);
1982 for (partial_symtab
*ps
: require_partial_symbols (objfile
, 1))
1984 int print_for_source
= 0;
1987 if (source_arg
!= NULL
)
1990 = compare_filenames_for_search (ps
->filename
, source_arg
);
1993 if (source_arg
== NULL
1994 || print_for_source
)
1996 if (!printed_objfile_header
)
1998 outfile
->printf ("\nPartial symtabs for objfile %s\n",
1999 objfile_name (objfile
));
2000 printed_objfile_header
= 1;
2002 dump_psymtab (objfile
, ps
, outfile
);
2003 dump_psymtab_addrmap (objfile
, ps
, outfile
);
2008 /* If we're printing all the objfile's symbols dump the full addrmap. */
2010 if (address_arg
== NULL
2011 && source_arg
== NULL
2012 && objfile
->partial_symtabs
->psymtabs_addrmap
!= NULL
)
2014 outfile
->puts ("\n");
2015 dump_psymtab_addrmap (objfile
, NULL
, outfile
);
2021 if (address_arg
!= NULL
)
2022 error (_("No partial symtab for address: %s"), address_arg
);
2023 if (source_arg
!= NULL
)
2024 error (_("No partial symtab for source file: %s"), source_arg
);
2028 /* List all the partial symbol tables whose names match REGEXP (optional). */
2031 maintenance_info_psymtabs (const char *regexp
, int from_tty
)
2033 struct program_space
*pspace
;
2038 ALL_PSPACES (pspace
)
2039 for (objfile
*objfile
: all_objfiles (pspace
))
2041 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
2043 /* We don't want to print anything for this objfile until we
2044 actually find a symtab whose name matches. */
2045 int printed_objfile_start
= 0;
2047 for (partial_symtab
*psymtab
: require_partial_symbols (objfile
, 1))
2052 || re_exec (psymtab
->filename
))
2054 if (! printed_objfile_start
)
2056 printf_filtered ("{ objfile %s ", objfile_name (objfile
));
2058 printf_filtered ("((struct objfile *) %s)\n",
2059 host_address_to_string (objfile
));
2060 printed_objfile_start
= 1;
2063 printf_filtered (" { psymtab %s ", psymtab
->filename
);
2065 printf_filtered ("((struct partial_symtab *) %s)\n",
2066 host_address_to_string (psymtab
));
2068 printf_filtered (" readin %s\n",
2069 psymtab
->readin
? "yes" : "no");
2070 printf_filtered (" fullname %s\n",
2072 ? psymtab
->fullname
: "(null)");
2073 printf_filtered (" text addresses ");
2074 fputs_filtered (paddress (gdbarch
,
2075 psymtab
->text_low (objfile
)),
2077 printf_filtered (" -- ");
2078 fputs_filtered (paddress (gdbarch
,
2079 psymtab
->text_high (objfile
)),
2081 printf_filtered ("\n");
2082 printf_filtered (" psymtabs_addrmap_supported %s\n",
2083 (psymtab
->psymtabs_addrmap_supported
2085 printf_filtered (" globals ");
2086 if (psymtab
->n_global_syms
)
2088 auto p
= &(objfile
->partial_symtabs
2089 ->global_psymbols
[psymtab
->globals_offset
]);
2092 ("(* (struct partial_symbol **) %s @ %d)\n",
2093 host_address_to_string (p
),
2094 psymtab
->n_global_syms
);
2097 printf_filtered ("(none)\n");
2098 printf_filtered (" statics ");
2099 if (psymtab
->n_static_syms
)
2101 auto p
= &(objfile
->partial_symtabs
2102 ->static_psymbols
[psymtab
->statics_offset
]);
2105 ("(* (struct partial_symbol **) %s @ %d)\n",
2106 host_address_to_string (p
),
2107 psymtab
->n_static_syms
);
2110 printf_filtered ("(none)\n");
2111 printf_filtered (" dependencies ");
2112 if (psymtab
->number_of_dependencies
)
2116 printf_filtered ("{\n");
2117 for (i
= 0; i
< psymtab
->number_of_dependencies
; i
++)
2119 struct partial_symtab
*dep
= psymtab
->dependencies
[i
];
2121 /* Note the string concatenation there --- no
2123 printf_filtered (" psymtab %s "
2124 "((struct partial_symtab *) %s)\n",
2126 host_address_to_string (dep
));
2128 printf_filtered (" }\n");
2131 printf_filtered ("(none)\n");
2132 printf_filtered (" }\n");
2136 if (printed_objfile_start
)
2137 printf_filtered ("}\n");
2141 /* Check consistency of currently expanded psymtabs vs symtabs. */
2144 maintenance_check_psymtabs (const char *ignore
, int from_tty
)
2147 struct compunit_symtab
*cust
= NULL
;
2148 const struct blockvector
*bv
;
2152 for (objfile
*objfile
: all_objfiles (current_program_space
))
2153 for (partial_symtab
*ps
: require_partial_symbols (objfile
, 1))
2155 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
2157 /* We don't call psymtab_to_symtab here because that may cause symtab
2158 expansion. When debugging a problem it helps if checkers leave
2159 things unchanged. */
2160 cust
= ps
->compunit_symtab
;
2162 /* First do some checks that don't require the associated symtab. */
2163 if (ps
->text_high (objfile
) < ps
->text_low (objfile
))
2165 printf_filtered ("Psymtab ");
2166 puts_filtered (ps
->filename
);
2167 printf_filtered (" covers bad range ");
2168 fputs_filtered (paddress (gdbarch
, ps
->text_low (objfile
)),
2170 printf_filtered (" - ");
2171 fputs_filtered (paddress (gdbarch
, ps
->text_high (objfile
)),
2173 printf_filtered ("\n");
2177 /* Now do checks requiring the associated symtab. */
2180 bv
= COMPUNIT_BLOCKVECTOR (cust
);
2181 b
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
2182 partial_symbol
**psym
2183 = &objfile
->partial_symtabs
->static_psymbols
[ps
->statics_offset
];
2184 length
= ps
->n_static_syms
;
2187 sym
= block_lookup_symbol (b
, symbol_search_name (*psym
),
2188 symbol_name_match_type::SEARCH_NAME
,
2192 printf_filtered ("Static symbol `");
2193 puts_filtered ((*psym
)->name
);
2194 printf_filtered ("' only found in ");
2195 puts_filtered (ps
->filename
);
2196 printf_filtered (" psymtab\n");
2200 b
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
2201 psym
= &objfile
->partial_symtabs
->global_psymbols
[ps
->globals_offset
];
2202 length
= ps
->n_global_syms
;
2205 sym
= block_lookup_symbol (b
, symbol_search_name (*psym
),
2206 symbol_name_match_type::SEARCH_NAME
,
2210 printf_filtered ("Global symbol `");
2211 puts_filtered ((*psym
)->name
);
2212 printf_filtered ("' only found in ");
2213 puts_filtered (ps
->filename
);
2214 printf_filtered (" psymtab\n");
2218 if (ps
->raw_text_high () != 0
2219 && (ps
->text_low (objfile
) < BLOCK_START (b
)
2220 || ps
->text_high (objfile
) > BLOCK_END (b
)))
2222 printf_filtered ("Psymtab ");
2223 puts_filtered (ps
->filename
);
2224 printf_filtered (" covers ");
2225 fputs_filtered (paddress (gdbarch
, ps
->text_low (objfile
)),
2227 printf_filtered (" - ");
2228 fputs_filtered (paddress (gdbarch
, ps
->text_high (objfile
)),
2230 printf_filtered (" but symtab covers only ");
2231 fputs_filtered (paddress (gdbarch
, BLOCK_START (b
)), gdb_stdout
);
2232 printf_filtered (" - ");
2233 fputs_filtered (paddress (gdbarch
, BLOCK_END (b
)), gdb_stdout
);
2234 printf_filtered ("\n");
2240 _initialize_psymtab (void)
2242 add_cmd ("psymbols", class_maintenance
, maintenance_print_psymbols
, _("\
2243 Print dump of current partial symbol definitions.\n\
2244 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
2245 mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
2246 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2247 or the terminal if OUTFILE is unspecified.\n\
2248 If ADDRESS is provided, dump only the file for that address.\n\
2249 If SOURCE is provided, dump only that file's symbols.\n\
2250 If OBJFILE is provided, dump only that file's minimal symbols."),
2251 &maintenanceprintlist
);
2253 add_cmd ("psymtabs", class_maintenance
, maintenance_info_psymtabs
, _("\
2254 List the partial symbol tables for all object files.\n\
2255 This does not include information about individual partial symbols,\n\
2256 just the symbol table structures themselves."),
2257 &maintenanceinfolist
);
2259 add_cmd ("check-psymtabs", class_maintenance
, maintenance_check_psymtabs
,
2261 Check consistency of currently expanded psymtabs versus symtabs."),