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);
86 require_partial_symbols (struct objfile *objfile, int verbose)
88 if ((objfile->flags & OBJF_PSYMTABS_READ) == 0)
90 objfile->flags |= OBJF_PSYMTABS_READ;
92 if (objfile->sf->sym_read_psymbols)
95 printf_filtered (_("Reading symbols from %s...\n"),
96 objfile_name (objfile));
97 (*objfile->sf->sym_read_psymbols) (objfile);
99 /* Partial symbols list are not expected to changed after this
101 objfile->partial_symtabs->global_psymbols.shrink_to_fit ();
102 objfile->partial_symtabs->static_psymbols.shrink_to_fit ();
104 if (verbose && !objfile_has_symbols (objfile))
105 printf_filtered (_("(No debugging symbols found in %s)\n"),
106 objfile_name (objfile));
110 return objfile_psymtabs (objfile);
113 /* Helper function for psym_map_symtabs_matching_filename that
114 expands the symtabs and calls the iterator. */
117 partial_map_expand_apply (struct objfile *objfile,
119 const char *real_path,
120 struct partial_symtab *pst,
121 gdb::function_view<bool (symtab *)> callback)
123 struct compunit_symtab *last_made = objfile->compunit_symtabs;
125 /* Shared psymtabs should never be seen here. Instead they should
126 be handled properly by the caller. */
127 gdb_assert (pst->user == NULL);
129 /* Don't visit already-expanded psymtabs. */
133 /* This may expand more than one symtab, and we want to iterate over
135 psymtab_to_symtab (objfile, pst);
137 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
138 last_made, callback);
141 /* Psymtab version of map_symtabs_matching_filename. See its definition in
142 the definition of quick_symbol_functions in symfile.h. */
145 psym_map_symtabs_matching_filename
146 (struct objfile *objfile,
148 const char *real_path,
149 gdb::function_view<bool (symtab *)> callback)
151 const char *name_basename = lbasename (name);
153 for (partial_symtab *pst : require_partial_symbols (objfile, 1))
155 /* We can skip shared psymtabs here, because any file name will be
156 attached to the unshared psymtab. */
157 if (pst->user != NULL)
160 /* Anonymous psymtabs don't have a file name. */
164 if (compare_filenames_for_search (pst->filename, name))
166 if (partial_map_expand_apply (objfile, name, real_path,
172 /* Before we invoke realpath, which can get expensive when many
173 files are involved, do a quick comparison of the basenames. */
174 if (! basenames_may_differ
175 && FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
178 if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
180 if (partial_map_expand_apply (objfile, name, real_path,
186 /* If the user gave us an absolute path, try to find the file in
187 this symtab and use its absolute path. */
188 if (real_path != NULL)
190 gdb_assert (IS_ABSOLUTE_PATH (real_path));
191 gdb_assert (IS_ABSOLUTE_PATH (name));
192 if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
194 if (partial_map_expand_apply (objfile, name, real_path,
205 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
206 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
208 static struct partial_symtab *
209 find_pc_sect_psymtab_closer (struct objfile *objfile,
210 CORE_ADDR pc, struct obj_section *section,
211 struct partial_symtab *pst,
212 struct bound_minimal_symbol msymbol)
214 struct partial_symtab *tpst;
215 struct partial_symtab *best_pst = pst;
216 CORE_ADDR best_addr = pst->text_low (objfile);
218 gdb_assert (!pst->psymtabs_addrmap_supported);
220 /* An objfile that has its functions reordered might have
221 many partial symbol tables containing the PC, but
222 we want the partial symbol table that contains the
223 function containing the PC. */
224 if (!(objfile->flags & OBJF_REORDERED)
225 && section == NULL) /* Can't validate section this way. */
228 if (msymbol.minsym == NULL)
231 /* The code range of partial symtabs sometimes overlap, so, in
232 the loop below, we need to check all partial symtabs and
233 find the one that fits better for the given PC address. We
234 select the partial symtab that contains a symbol whose
235 address is closest to the PC address. By closest we mean
236 that find_pc_sect_symbol returns the symbol with address
237 that is closest and still less than the given PC. */
238 for (tpst = pst; tpst != NULL; tpst = tpst->next)
240 if (pc >= tpst->text_low (objfile) && pc < tpst->text_high (objfile))
242 struct partial_symbol *p;
245 /* NOTE: This assumes that every psymbol has a
246 corresponding msymbol, which is not necessarily
247 true; the debug info might be much richer than the
248 object's symbol table. */
249 p = find_pc_sect_psymbol (objfile, tpst, pc, section);
251 && (p->address (objfile) == BMSYMBOL_VALUE_ADDRESS (msymbol)))
254 /* Also accept the textlow value of a psymtab as a
255 "symbol", to provide some support for partial
256 symbol tables with line information but no debug
257 symbols (e.g. those produced by an assembler). */
259 this_addr = p->address (objfile);
261 this_addr = tpst->text_low (objfile);
263 /* Check whether it is closer than our current
264 BEST_ADDR. Since this symbol address is
265 necessarily lower or equal to PC, the symbol closer
266 to PC is the symbol which address is the highest.
267 This way we return the psymtab which contains such
268 best match symbol. This can help in cases where the
269 symbol information/debuginfo is not complete, like
270 for instance on IRIX6 with gcc, where no debug info
271 is emitted for statics. (See also the nodebug.exp
273 if (this_addr > best_addr)
275 best_addr = this_addr;
283 /* Find which partial symtab contains PC and SECTION. Return NULL if
284 none. We return the psymtab that contains a symbol whose address
285 exactly matches PC, or, if we cannot find an exact match, the
286 psymtab that contains a symbol whose address is closest to PC. */
288 static struct partial_symtab *
289 find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
290 struct obj_section *section,
291 struct bound_minimal_symbol msymbol)
293 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
294 SECT_OFF_TEXT (objfile));
296 /* Try just the PSYMTABS_ADDRMAP mapping first as it has better granularity
297 than the later used TEXTLOW/TEXTHIGH one. */
299 if (objfile->partial_symtabs->psymtabs_addrmap != NULL)
301 struct partial_symtab *pst
302 = ((struct partial_symtab *)
303 addrmap_find (objfile->partial_symtabs->psymtabs_addrmap,
307 /* FIXME: addrmaps currently do not handle overlayed sections,
308 so fall back to the non-addrmap case if we're debugging
309 overlays and the addrmap returned the wrong section. */
310 if (overlay_debugging && msymbol.minsym != NULL && section != NULL)
312 struct partial_symbol *p;
314 /* NOTE: This assumes that every psymbol has a
315 corresponding msymbol, which is not necessarily
316 true; the debug info might be much richer than the
317 object's symbol table. */
318 p = find_pc_sect_psymbol (objfile, pst, pc, section);
320 || (p->address (objfile)
321 != BMSYMBOL_VALUE_ADDRESS (msymbol)))
325 /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
326 PSYMTABS_ADDRMAP we used has already the best 1-byte
327 granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
328 a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
337 /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
338 which still have no corresponding full SYMTABs read. But it is not
339 present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
342 /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
343 its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
344 debug info type in single OBJFILE. */
346 for (partial_symtab *pst : require_partial_symbols (objfile, 1))
347 if (!pst->psymtabs_addrmap_supported
348 && pc >= pst->text_low (objfile) && pc < pst->text_high (objfile))
350 struct partial_symtab *best_pst;
352 best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
354 if (best_pst != NULL)
361 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
362 the definition of quick_symbol_functions in symfile.h. */
364 static struct compunit_symtab *
365 psym_find_pc_sect_compunit_symtab (struct objfile *objfile,
366 struct bound_minimal_symbol msymbol,
368 struct obj_section *section,
371 struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc, section,
375 if (warn_if_readin && ps->readin)
376 /* Might want to error() here (in case symtab is corrupt and
377 will cause a core dump), but maybe we can successfully
378 continue, so let's not. */
380 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
381 paddress (get_objfile_arch (objfile), pc));
382 psymtab_to_symtab (objfile, ps);
383 return ps->compunit_symtab;
388 /* Find which partial symbol within a psymtab matches PC and SECTION.
389 Return NULL if none. */
391 static struct partial_symbol *
392 find_pc_sect_psymbol (struct objfile *objfile,
393 struct partial_symtab *psymtab, CORE_ADDR pc,
394 struct obj_section *section)
396 struct partial_symbol *best = NULL;
398 const CORE_ADDR textlow = psymtab->text_low (objfile);
400 gdb_assert (psymtab != NULL);
402 /* Cope with programs that start at address 0. */
403 best_pc = (textlow != 0) ? textlow - 1 : 0;
405 /* Search the global symbols as well as the static symbols, so that
406 find_pc_partial_function doesn't use a minimal symbol and thus
407 cache a bad endaddr. */
408 for (int i = 0; i < psymtab->n_global_syms; i++)
411 = objfile->partial_symtabs->global_psymbols[psymtab->globals_offset
414 if (p->domain == VAR_DOMAIN
415 && p->aclass == LOC_BLOCK
416 && pc >= p->address (objfile)
417 && (p->address (objfile) > best_pc
418 || (psymtab->text_low (objfile) == 0
419 && best_pc == 0 && p->address (objfile) == 0)))
421 if (section != NULL) /* Match on a specific section. */
423 if (!matching_obj_sections (p->obj_section (objfile),
427 best_pc = p->address (objfile);
432 for (int i = 0; i < psymtab->n_static_syms; i++)
435 = objfile->partial_symtabs->static_psymbols[psymtab->statics_offset
438 if (p->domain == VAR_DOMAIN
439 && p->aclass == LOC_BLOCK
440 && pc >= p->address (objfile)
441 && (p->address (objfile) > best_pc
442 || (psymtab->text_low (objfile) == 0
443 && best_pc == 0 && p->address (objfile) == 0)))
445 if (section != NULL) /* Match on a specific section. */
447 if (!matching_obj_sections (p->obj_section (objfile),
451 best_pc = p->address (objfile);
459 /* Psymtab version of lookup_symbol. See its definition in
460 the definition of quick_symbol_functions in symfile.h. */
462 static struct compunit_symtab *
463 psym_lookup_symbol (struct objfile *objfile,
464 int block_index, const char *name,
465 const domain_enum domain)
467 const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
468 struct compunit_symtab *stab_best = NULL;
470 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
472 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
474 if (!ps->readin && lookup_partial_symbol (objfile, ps, name,
475 psymtab_index, domain))
477 struct symbol *sym, *with_opaque = NULL;
478 struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
479 /* Note: While psymtab_to_symtab can return NULL if the
480 partial symtab is empty, we can assume it won't here
481 because lookup_partial_symbol succeeded. */
482 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
483 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
485 sym = block_find_symbol (block, name, domain,
486 block_find_non_opaque_type_preferred,
489 /* Some caution must be observed with overloaded functions
490 and methods, since the index will not contain any overload
491 information (but NAME might contain it). */
494 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
496 if (with_opaque != NULL
497 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
500 /* Keep looking through other psymtabs. */
507 /* Returns true if PSYM matches LOOKUP_NAME. */
510 psymbol_name_matches (partial_symbol *psym,
511 const lookup_name_info &lookup_name)
513 const language_defn *lang = language_def (psym->language);
514 symbol_name_matcher_ftype *name_match
515 = get_symbol_name_matcher (lang, lookup_name);
516 return name_match (symbol_search_name (psym), lookup_name, NULL);
519 /* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
520 the global block of PST if GLOBAL, and otherwise the static block.
521 MATCH is the comparison operation that returns true iff MATCH (s,
522 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
523 non-null, the symbols in the block are assumed to be ordered
524 according to it (allowing binary search). It must be compatible
525 with MATCH. Returns the symbol, if found, and otherwise NULL. */
527 static struct partial_symbol *
528 match_partial_symbol (struct objfile *objfile,
529 struct partial_symtab *pst, int global,
530 const char *name, domain_enum domain,
531 symbol_name_match_type match_type,
532 symbol_compare_ftype *ordered_compare)
534 struct partial_symbol **start, **psym;
535 struct partial_symbol **top, **real_top, **bottom, **center;
536 int length = (global ? pst->n_global_syms : pst->n_static_syms);
537 int do_linear_search = 1;
542 lookup_name_info lookup_name (name, match_type);
545 &objfile->partial_symtabs->global_psymbols[pst->globals_offset] :
546 &objfile->partial_symtabs->static_psymbols[pst->statics_offset]);
548 if (global && ordered_compare) /* Can use a binary search. */
550 do_linear_search = 0;
552 /* Binary search. This search is guaranteed to end with center
553 pointing at the earliest partial symbol whose name might be
554 correct. At that point *all* partial symbols with an
555 appropriate name will be checked against the correct
559 top = start + length - 1;
563 center = bottom + (top - bottom) / 2;
564 gdb_assert (center < top);
566 enum language lang = (*center)->language;
568 = lookup_name.language_lookup_name (lang).c_str ();
570 if (ordered_compare (symbol_search_name (*center), lang_ln) >= 0)
575 gdb_assert (top == bottom);
577 while (top <= real_top
578 && psymbol_name_matches (*top, lookup_name))
580 if (symbol_matches_domain ((*top)->language,
581 (*top)->domain, domain))
587 /* Can't use a binary search or else we found during the binary search that
588 we should also do a linear search. */
590 if (do_linear_search)
592 for (psym = start; psym < start + length; psym++)
594 if (symbol_matches_domain ((*psym)->language,
595 (*psym)->domain, domain)
596 && psymbol_name_matches (*psym, lookup_name))
604 /* Returns the name used to search psymtabs. Unlike symtabs, psymtabs do
605 not contain any method/function instance information (since this would
606 force reading type information while reading psymtabs). Therefore,
607 if NAME contains overload information, it must be stripped before searching
610 static gdb::unique_xmalloc_ptr<char>
611 psymtab_search_name (const char *name)
613 switch (current_language->la_language)
617 if (strchr (name, '('))
619 gdb::unique_xmalloc_ptr<char> ret = cp_remove_params (name);
631 return gdb::unique_xmalloc_ptr<char> (xstrdup (name));
634 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
635 Check the global symbols if GLOBAL, the static symbols if not. */
637 static struct partial_symbol *
638 lookup_partial_symbol (struct objfile *objfile,
639 struct partial_symtab *pst, const char *name,
640 int global, domain_enum domain)
642 struct partial_symbol **start, **psym;
643 struct partial_symbol **top, **real_top, **bottom, **center;
644 int length = (global ? pst->n_global_syms : pst->n_static_syms);
645 int do_linear_search = 1;
650 gdb::unique_xmalloc_ptr<char> search_name = psymtab_search_name (name);
652 lookup_name_info lookup_name (search_name.get (), symbol_name_match_type::FULL);
655 &objfile->partial_symtabs->global_psymbols[pst->globals_offset] :
656 &objfile->partial_symtabs->static_psymbols[pst->statics_offset]);
658 if (global) /* This means we can use a binary search. */
660 do_linear_search = 0;
662 /* Binary search. This search is guaranteed to end with center
663 pointing at the earliest partial symbol whose name might be
664 correct. At that point *all* partial symbols with an
665 appropriate name will be checked against the correct
669 top = start + length - 1;
673 center = bottom + (top - bottom) / 2;
675 internal_error (__FILE__, __LINE__,
676 _("failed internal consistency check"));
677 if (strcmp_iw_ordered (symbol_search_name (*center),
678 search_name.get ()) >= 0)
687 if (!(top == bottom))
688 internal_error (__FILE__, __LINE__,
689 _("failed internal consistency check"));
691 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
692 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
693 while (top >= start && symbol_matches_search_name (*top, lookup_name))
696 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
699 while (top <= real_top && symbol_matches_search_name (*top, lookup_name))
701 if (symbol_matches_domain ((*top)->language,
702 (*top)->domain, domain))
708 /* Can't use a binary search or else we found during the binary search that
709 we should also do a linear search. */
711 if (do_linear_search)
713 for (psym = start; psym < start + length; psym++)
715 if (symbol_matches_domain ((*psym)->language,
716 (*psym)->domain, domain)
717 && symbol_matches_search_name (*psym, lookup_name))
725 /* Get the symbol table that corresponds to a partial_symtab.
726 This is fast after the first time you do it.
727 The result will be NULL if the primary symtab has no symbols,
728 which can happen. Otherwise the result is the primary symtab
729 that contains PST. */
731 static struct compunit_symtab *
732 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
734 /* If it is a shared psymtab, find an unshared psymtab that includes
735 it. Any such psymtab will do. */
736 while (pst->user != NULL)
739 /* If it's been looked up before, return it. */
740 if (pst->compunit_symtab)
741 return pst->compunit_symtab;
743 /* If it has not yet been read in, read it. */
746 scoped_restore decrementer = increment_reading_symtab ();
748 (*pst->read_symtab) (pst, objfile);
751 return pst->compunit_symtab;
754 /* Psymtab version of find_last_source_symtab. See its definition in
755 the definition of quick_symbol_functions in symfile.h. */
757 static struct symtab *
758 psym_find_last_source_symtab (struct objfile *ofp)
760 struct partial_symtab *cs_pst = NULL;
762 for (partial_symtab *ps : require_partial_symbols (ofp, 1))
764 const char *name = ps->filename;
765 int len = strlen (name);
767 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
768 || strcmp (name, "<<C++-namespaces>>") == 0)))
776 internal_error (__FILE__, __LINE__,
777 _("select_source_symtab: "
778 "readin pst found and no symtabs."));
782 struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
786 return compunit_primary_filetab (cust);
792 /* Psymtab version of forget_cached_source_info. See its definition in
793 the definition of quick_symbol_functions in symfile.h. */
796 psym_forget_cached_source_info (struct objfile *objfile)
798 for (partial_symtab *pst : require_partial_symbols (objfile, 1))
800 if (pst->fullname != NULL)
802 xfree (pst->fullname);
803 pst->fullname = NULL;
809 print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
810 struct partial_symbol **p, int count, const char *what,
811 struct ui_file *outfile)
813 fprintf_filtered (outfile, " %s partial symbols:\n", what);
817 fprintf_filtered (outfile, " `%s'", (*p)->name);
818 if (symbol_demangled_name (*p) != NULL)
820 fprintf_filtered (outfile, " `%s'", symbol_demangled_name (*p));
822 fputs_filtered (", ", outfile);
823 switch ((*p)->domain)
826 fputs_filtered ("undefined domain, ", outfile);
829 /* This is the usual thing -- don't print it. */
832 fputs_filtered ("struct domain, ", outfile);
835 fputs_filtered ("label domain, ", outfile);
838 fputs_filtered ("<invalid domain>, ", outfile);
841 switch ((*p)->aclass)
844 fputs_filtered ("undefined", outfile);
847 fputs_filtered ("constant int", outfile);
850 fputs_filtered ("static", outfile);
853 fputs_filtered ("register", outfile);
856 fputs_filtered ("pass by value", outfile);
859 fputs_filtered ("pass by reference", outfile);
861 case LOC_REGPARM_ADDR:
862 fputs_filtered ("register address parameter", outfile);
865 fputs_filtered ("stack parameter", outfile);
868 fputs_filtered ("type", outfile);
871 fputs_filtered ("label", outfile);
874 fputs_filtered ("function", outfile);
876 case LOC_CONST_BYTES:
877 fputs_filtered ("constant bytes", outfile);
880 fputs_filtered ("unresolved", outfile);
882 case LOC_OPTIMIZED_OUT:
883 fputs_filtered ("optimized out", outfile);
886 fputs_filtered ("computed at runtime", outfile);
889 fputs_filtered ("<invalid location>", outfile);
892 fputs_filtered (", ", outfile);
893 fputs_filtered (paddress (gdbarch, (*p)->unrelocated_address ()), outfile);
894 fprintf_filtered (outfile, "\n");
900 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
901 struct ui_file *outfile)
903 struct gdbarch *gdbarch = get_objfile_arch (objfile);
906 if (psymtab->anonymous)
908 fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
913 fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
916 fprintf_filtered (outfile, "(object ");
917 gdb_print_host_address (psymtab, outfile);
918 fprintf_filtered (outfile, ")\n\n");
919 fprintf_filtered (outfile, " Read from object file %s (",
920 objfile_name (objfile));
921 gdb_print_host_address (objfile, outfile);
922 fprintf_filtered (outfile, ")\n");
926 fprintf_filtered (outfile,
927 " Full symtab was read (at ");
928 gdb_print_host_address (psymtab->compunit_symtab, outfile);
929 fprintf_filtered (outfile, " by function at ");
930 gdb_print_host_address (psymtab->read_symtab, outfile);
931 fprintf_filtered (outfile, ")\n");
934 fprintf_filtered (outfile, " Symbols cover text addresses ");
935 fputs_filtered (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
936 fprintf_filtered (outfile, "-");
937 fputs_filtered (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
938 fprintf_filtered (outfile, "\n");
939 fprintf_filtered (outfile, " Address map supported - %s.\n",
940 psymtab->psymtabs_addrmap_supported ? "yes" : "no");
941 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
942 psymtab->number_of_dependencies);
943 for (i = 0; i < psymtab->number_of_dependencies; i++)
945 fprintf_filtered (outfile, " %d ", i);
946 gdb_print_host_address (psymtab->dependencies[i], outfile);
947 fprintf_filtered (outfile, " %s\n",
948 psymtab->dependencies[i]->filename);
950 if (psymtab->user != NULL)
952 fprintf_filtered (outfile, " Shared partial symtab with user ");
953 gdb_print_host_address (psymtab->user, outfile);
954 fprintf_filtered (outfile, "\n");
956 if (psymtab->n_global_syms > 0)
958 print_partial_symbols
960 &objfile->partial_symtabs->global_psymbols[psymtab->globals_offset],
961 psymtab->n_global_syms, "Global", outfile);
963 if (psymtab->n_static_syms > 0)
965 print_partial_symbols
967 &objfile->partial_symtabs->static_psymbols[psymtab->statics_offset],
968 psymtab->n_static_syms, "Static", outfile);
970 fprintf_filtered (outfile, "\n");
973 /* Psymtab version of print_stats. See its definition in
974 the definition of quick_symbol_functions in symfile.h. */
977 psym_print_stats (struct objfile *objfile)
982 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
987 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i);
990 /* Psymtab version of dump. See its definition in
991 the definition of quick_symbol_functions in symfile.h. */
994 psym_dump (struct objfile *objfile)
996 struct partial_symtab *psymtab;
998 if (objfile->partial_symtabs->psymtabs)
1000 printf_filtered ("Psymtabs:\n");
1001 for (psymtab = objfile->partial_symtabs->psymtabs;
1003 psymtab = psymtab->next)
1005 printf_filtered ("%s at ",
1007 gdb_print_host_address (psymtab, gdb_stdout);
1008 printf_filtered (", ");
1011 printf_filtered ("\n\n");
1015 /* Psymtab version of expand_symtabs_for_function. See its definition in
1016 the definition of quick_symbol_functions in symfile.h. */
1019 psym_expand_symtabs_for_function (struct objfile *objfile,
1020 const char *func_name)
1022 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1027 if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
1029 || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
1031 psymtab_to_symtab (objfile, ps);
1035 /* Psymtab version of expand_all_symtabs. See its definition in
1036 the definition of quick_symbol_functions in symfile.h. */
1039 psym_expand_all_symtabs (struct objfile *objfile)
1041 for (partial_symtab *psymtab : require_partial_symbols (objfile, 1))
1042 psymtab_to_symtab (objfile, psymtab);
1045 /* Psymtab version of expand_symtabs_with_fullname. See its definition in
1046 the definition of quick_symbol_functions in symfile.h. */
1049 psym_expand_symtabs_with_fullname (struct objfile *objfile,
1050 const char *fullname)
1052 for (partial_symtab *p : require_partial_symbols (objfile, 1))
1054 /* Anonymous psymtabs don't have a name of a source file. */
1058 /* psymtab_to_fullname tries to open the file which is slow.
1059 Don't call it if we know the basenames don't match. */
1060 if ((basenames_may_differ
1061 || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1062 && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
1063 psymtab_to_symtab (objfile, p);
1067 /* Psymtab version of map_symbol_filenames. See its definition in
1068 the definition of quick_symbol_functions in symfile.h. */
1071 psym_map_symbol_filenames (struct objfile *objfile,
1072 symbol_filename_ftype *fun, void *data,
1075 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1077 const char *fullname;
1082 /* We can skip shared psymtabs here, because any file name will be
1083 attached to the unshared psymtab. */
1084 if (ps->user != NULL)
1087 /* Anonymous psymtabs don't have a file name. */
1093 fullname = psymtab_to_fullname (ps);
1096 (*fun) (ps->filename, fullname, data);
1100 /* Finds the fullname that a partial_symtab represents.
1102 If this functions finds the fullname, it will save it in ps->fullname
1103 and it will also return the value.
1105 If this function fails to find the file that this partial_symtab represents,
1106 NULL will be returned and ps->fullname will be set to NULL. */
1109 psymtab_to_fullname (struct partial_symtab *ps)
1111 gdb_assert (!ps->anonymous);
1113 /* Use cached copy if we have it.
1114 We rely on forget_cached_source_info being called appropriately
1115 to handle cases like the file being moved. */
1116 if (ps->fullname == NULL)
1118 gdb::unique_xmalloc_ptr<char> fullname;
1119 scoped_fd fd = find_and_open_source (ps->filename, ps->dirname,
1121 ps->fullname = fullname.release ();
1125 /* rewrite_source_path would be applied by find_and_open_source, we
1126 should report the pathname where GDB tried to find the file. */
1128 if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
1129 fullname.reset (xstrdup (ps->filename));
1131 fullname.reset (concat (ps->dirname, SLASH_STRING,
1132 ps->filename, (char *) NULL));
1134 ps->fullname = rewrite_source_path (fullname.get ()).release ();
1135 if (ps->fullname == NULL)
1136 ps->fullname = fullname.release ();
1140 return ps->fullname;
1143 /* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1144 according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1145 BLOCK is assumed to come from OBJFILE. Returns 1 iff CALLBACK
1146 ever returns non-zero, and otherwise returns 0. */
1149 map_block (const char *name, domain_enum domain, struct objfile *objfile,
1150 struct block *block,
1151 int (*callback) (struct block *, struct symbol *, void *),
1152 void *data, symbol_name_match_type match)
1154 struct block_iterator iter;
1157 lookup_name_info lookup_name (name, match);
1159 for (sym = block_iter_match_first (block, lookup_name, &iter);
1161 sym = block_iter_match_next (lookup_name, &iter))
1163 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
1164 SYMBOL_DOMAIN (sym), domain))
1166 if (callback (block, sym, data))
1174 /* Psymtab version of map_matching_symbols. See its definition in
1175 the definition of quick_symbol_functions in symfile.h. */
1178 psym_map_matching_symbols (struct objfile *objfile,
1179 const char *name, domain_enum domain,
1181 int (*callback) (struct block *,
1182 struct symbol *, void *),
1184 symbol_name_match_type match,
1185 symbol_compare_ftype *ordered_compare)
1187 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
1189 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1193 || match_partial_symbol (objfile, ps, global, name, domain, match,
1196 struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
1197 struct block *block;
1201 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
1202 if (map_block (name, domain, objfile, block,
1203 callback, data, match))
1205 if (callback (block, NULL, data))
1211 /* A helper for psym_expand_symtabs_matching that handles searching
1212 included psymtabs. This returns true if a symbol is found, and
1213 false otherwise. It also updates the 'searched_flag' on the
1214 various psymtabs that it searches. */
1217 recursively_search_psymtabs
1218 (struct partial_symtab *ps,
1219 struct objfile *objfile,
1220 enum search_domain domain,
1221 const lookup_name_info &lookup_name,
1222 gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
1225 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
1228 if (ps->searched_flag != PST_NOT_SEARCHED)
1229 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1231 /* Recurse into shared psymtabs first, because they may have already
1232 been searched, and this could save some time. */
1233 for (i = 0; i < ps->number_of_dependencies; ++i)
1237 /* Skip non-shared dependencies, these are handled elsewhere. */
1238 if (ps->dependencies[i]->user == NULL)
1241 r = recursively_search_psymtabs (ps->dependencies[i],
1242 objfile, domain, lookup_name,
1246 ps->searched_flag = PST_SEARCHED_AND_FOUND;
1251 partial_symbol **gbound
1252 = (objfile->partial_symtabs->global_psymbols.data ()
1253 + ps->globals_offset + ps->n_global_syms);
1254 partial_symbol **sbound
1255 = (objfile->partial_symtabs->static_psymbols.data ()
1256 + ps->statics_offset + ps->n_static_syms);
1257 partial_symbol **bound = gbound;
1259 /* Go through all of the symbols stored in a partial
1260 symtab in one loop. */
1261 partial_symbol **psym = (objfile->partial_symtabs->global_psymbols.data ()
1262 + ps->globals_offset);
1267 if (bound == gbound && ps->n_static_syms != 0)
1269 psym = (objfile->partial_symtabs->static_psymbols.data ()
1270 + ps->statics_offset);
1281 if ((domain == ALL_DOMAIN
1282 || (domain == VARIABLES_DOMAIN
1283 && (*psym)->aclass != LOC_TYPEDEF
1284 && (*psym)->aclass != LOC_BLOCK)
1285 || (domain == FUNCTIONS_DOMAIN
1286 && (*psym)->aclass == LOC_BLOCK)
1287 || (domain == TYPES_DOMAIN
1288 && (*psym)->aclass == LOC_TYPEDEF))
1289 && psymbol_name_matches (*psym, lookup_name)
1290 && (sym_matcher == NULL || sym_matcher (symbol_search_name (*psym))))
1292 /* Found a match, so notify our caller. */
1293 result = PST_SEARCHED_AND_FOUND;
1300 ps->searched_flag = result;
1301 return result == PST_SEARCHED_AND_FOUND;
1304 /* Psymtab version of expand_symtabs_matching. See its definition in
1305 the definition of quick_symbol_functions in symfile.h. */
1308 psym_expand_symtabs_matching
1309 (struct objfile *objfile,
1310 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1311 const lookup_name_info &lookup_name_in,
1312 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1313 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1314 enum search_domain domain)
1316 lookup_name_info lookup_name = lookup_name_in.make_ignore_params ();
1318 /* Clear the search flags. */
1319 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1320 ps->searched_flag = PST_NOT_SEARCHED;
1322 for (partial_symtab *ps : objfile_psymtabs (objfile))
1329 /* We skip shared psymtabs because file-matching doesn't apply
1330 to them; but we search them later in the loop. */
1331 if (ps->user != NULL)
1341 match = file_matcher (ps->filename, false);
1344 /* Before we invoke realpath, which can get expensive when many
1345 files are involved, do a quick comparison of the basenames. */
1346 if (basenames_may_differ
1347 || file_matcher (lbasename (ps->filename), true))
1348 match = file_matcher (psymtab_to_fullname (ps), false);
1354 if (recursively_search_psymtabs (ps, objfile, domain,
1355 lookup_name, symbol_matcher))
1357 struct compunit_symtab *symtab =
1358 psymtab_to_symtab (objfile, ps);
1360 if (expansion_notify != NULL)
1361 expansion_notify (symtab);
1366 /* Psymtab version of has_symbols. See its definition in
1367 the definition of quick_symbol_functions in symfile.h. */
1370 psym_has_symbols (struct objfile *objfile)
1372 return objfile->partial_symtabs->psymtabs != NULL;
1375 /* Helper function for psym_find_compunit_symtab_by_address that fills
1376 in psymbol_map for a given range of psymbols. */
1379 psym_fill_psymbol_map (struct objfile *objfile,
1380 struct partial_symtab *psymtab,
1381 std::set<CORE_ADDR> *seen_addrs,
1382 const std::vector<partial_symbol *> &symbols,
1386 for (int i = 0; i < length; ++i)
1388 struct partial_symbol *psym = symbols[start + i];
1390 if (psym->aclass == LOC_STATIC)
1392 CORE_ADDR addr = psym->address (objfile);
1393 if (seen_addrs->find (addr) == seen_addrs->end ())
1395 seen_addrs->insert (addr);
1396 objfile->psymbol_map.emplace_back (addr, psymtab);
1402 /* See find_compunit_symtab_by_address in quick_symbol_functions, in
1405 static compunit_symtab *
1406 psym_find_compunit_symtab_by_address (struct objfile *objfile,
1409 if (objfile->psymbol_map.empty ())
1411 std::set<CORE_ADDR> seen_addrs;
1413 for (partial_symtab *pst : require_partial_symbols (objfile, 1))
1415 psym_fill_psymbol_map (objfile, pst,
1417 objfile->partial_symtabs->global_psymbols,
1418 pst->globals_offset,
1419 pst->n_global_syms);
1420 psym_fill_psymbol_map (objfile, pst,
1422 objfile->partial_symtabs->static_psymbols,
1423 pst->statics_offset,
1424 pst->n_static_syms);
1427 objfile->psymbol_map.shrink_to_fit ();
1429 std::sort (objfile->psymbol_map.begin (), objfile->psymbol_map.end (),
1430 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1431 const std::pair<CORE_ADDR, partial_symtab *> &b)
1433 return a.first < b.first;
1437 auto iter = std::lower_bound
1438 (objfile->psymbol_map.begin (), objfile->psymbol_map.end (), address,
1439 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1445 if (iter == objfile->psymbol_map.end () || iter->first != address)
1448 return psymtab_to_symtab (objfile, iter->second);
1451 const struct quick_symbol_functions psym_functions =
1454 psym_find_last_source_symtab,
1455 psym_forget_cached_source_info,
1456 psym_map_symtabs_matching_filename,
1460 psym_expand_symtabs_for_function,
1461 psym_expand_all_symtabs,
1462 psym_expand_symtabs_with_fullname,
1463 psym_map_matching_symbols,
1464 psym_expand_symtabs_matching,
1465 psym_find_pc_sect_compunit_symtab,
1466 psym_find_compunit_symtab_by_address,
1467 psym_map_symbol_filenames
1473 sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
1475 /* Sort the global list; don't sort the static list. */
1476 auto begin = objfile->partial_symtabs->global_psymbols.begin ();
1477 std::advance (begin, pst->globals_offset);
1479 /* The psymbols for this partial_symtab are currently at the end of the
1481 auto end = objfile->partial_symtabs->global_psymbols.end ();
1483 std::sort (begin, end, [] (partial_symbol *s1, partial_symbol *s2)
1485 return strcmp_iw_ordered (symbol_search_name (s1),
1486 symbol_search_name (s2)) < 0;
1490 /* Allocate and partially fill a partial symtab. It will be
1491 completely filled at the end of the symbol list.
1493 FILENAME is the name of the symbol-file we are reading from. */
1495 struct partial_symtab *
1496 start_psymtab_common (struct objfile *objfile,
1497 const char *filename,
1500 struct partial_symtab *psymtab;
1502 psymtab = allocate_psymtab (filename, objfile);
1503 psymtab->set_text_low (textlow);
1504 psymtab->set_text_high (psymtab->raw_text_low ()); /* default */
1505 psymtab->globals_offset = objfile->partial_symtabs->global_psymbols.size ();
1506 psymtab->statics_offset = objfile->partial_symtabs->static_psymbols.size ();
1510 /* Perform "finishing up" operations of a partial symtab. */
1513 end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1515 pst->n_global_syms = (objfile->partial_symtabs->global_psymbols.size ()
1516 - pst->globals_offset);
1517 pst->n_static_syms = (objfile->partial_symtabs->static_psymbols.size ()
1518 - pst->statics_offset);
1520 sort_pst_symbols (objfile, pst);
1523 /* Calculate a hash code for the given partial symbol. The hash is
1524 calculated using the symbol's value, language, domain, class
1525 and name. These are the values which are set by
1526 add_psymbol_to_bcache. */
1528 static unsigned long
1529 psymbol_hash (const void *addr, int length)
1531 unsigned long h = 0;
1532 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1533 unsigned int lang = psymbol->language;
1534 unsigned int domain = psymbol->domain;
1535 unsigned int theclass = psymbol->aclass;
1537 h = hash_continue (&psymbol->value, sizeof (psymbol->value), h);
1538 h = hash_continue (&lang, sizeof (unsigned int), h);
1539 h = hash_continue (&domain, sizeof (unsigned int), h);
1540 h = hash_continue (&theclass, sizeof (unsigned int), h);
1541 /* Note that psymbol names are interned via symbol_set_names, so
1542 there's no need to hash the contents of the name here. */
1543 h = hash_continue (&psymbol->name,
1544 sizeof (psymbol->name), h);
1549 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1550 For the comparison this function uses a symbols value,
1551 language, domain, class and name. */
1554 psymbol_compare (const void *addr1, const void *addr2, int length)
1556 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1557 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1559 return (memcmp (&sym1->value, &sym2->value,
1560 sizeof (sym1->value)) == 0
1561 && sym1->language == sym2->language
1562 && sym1->domain == sym2->domain
1563 && sym1->aclass == sym2->aclass
1564 /* Note that psymbol names are interned via
1565 symbol_set_names, so there's no need to compare the
1566 contents of the name here. */
1567 && sym1->name == sym2->name);
1570 /* Initialize a partial symbol bcache. */
1572 struct psymbol_bcache *
1573 psymbol_bcache_init (void)
1575 struct psymbol_bcache *bcache = XCNEW (struct psymbol_bcache);
1577 bcache->bcache = bcache_xmalloc (psymbol_hash, psymbol_compare);
1581 /* Free a partial symbol bcache. */
1584 psymbol_bcache_free (struct psymbol_bcache *bcache)
1589 bcache_xfree (bcache->bcache);
1593 /* Return the internal bcache of the psymbol_bcache BCACHE. */
1596 psymbol_bcache_get_bcache (struct psymbol_bcache *bcache)
1598 return bcache->bcache;
1601 /* Find a copy of the SYM in BCACHE. If BCACHE has never seen this
1602 symbol before, add a copy to BCACHE. In either case, return a pointer
1603 to BCACHE's copy of the symbol. If optional ADDED is not NULL, return
1604 1 in case of new entry or 0 if returning an old entry. */
1606 static struct partial_symbol *
1607 psymbol_bcache_full (struct partial_symbol *sym,
1608 struct psymbol_bcache *bcache,
1611 return ((struct partial_symbol *)
1612 bcache_full (sym, sizeof (struct partial_symbol), bcache->bcache,
1616 /* Helper function, initialises partial symbol structure and stashes
1617 it into objfile's bcache. Note that our caching mechanism will
1618 use all fields of struct partial_symbol to determine hash value of the
1619 structure. In other words, having two symbols with the same name but
1620 different domain (or address) is possible and correct. */
1622 static struct partial_symbol *
1623 add_psymbol_to_bcache (const char *name, int namelength, int copy_name,
1625 enum address_class theclass,
1628 enum language language, struct objfile *objfile,
1631 struct partial_symbol psymbol;
1633 psymbol.set_unrelocated_address (coreaddr);
1634 psymbol.section = section;
1635 psymbol.domain = domain;
1636 psymbol.aclass = theclass;
1638 memset (&psymbol.language_specific, 0, sizeof (psymbol.language_specific));
1639 psymbol.ada_mangled = 0;
1640 symbol_set_language (&psymbol, language, &objfile->objfile_obstack);
1641 symbol_set_names (&psymbol, name, namelength, copy_name, objfile->per_bfd);
1643 /* Stash the partial symbol away in the cache. */
1644 return psymbol_bcache_full (&psymbol,
1645 objfile->partial_symtabs->psymbol_cache,
1649 /* Helper function, adds partial symbol to the given partial symbol list. */
1652 append_psymbol_to_list (std::vector<partial_symbol *> *list,
1653 struct partial_symbol *psym,
1654 struct objfile *objfile)
1656 list->push_back (psym);
1657 OBJSTAT (objfile, n_psyms++);
1660 /* Add a symbol with a long value to a psymtab.
1661 Since one arg is a struct, we pass in a ptr and deref it (sigh).
1662 The only value we need to store for psyms is an address.
1663 For all other psyms pass zero for COREADDR.
1664 Return the partial symbol that has been added. */
1667 add_psymbol_to_list (const char *name, int namelength, int copy_name,
1669 enum address_class theclass,
1671 psymbol_placement where,
1673 enum language language, struct objfile *objfile)
1675 struct partial_symbol *psym;
1679 /* Stash the partial symbol away in the cache. */
1680 psym = add_psymbol_to_bcache (name, namelength, copy_name, domain, theclass,
1681 section, coreaddr, language, objfile, &added);
1683 /* Do not duplicate global partial symbols. */
1684 if (where == psymbol_placement::GLOBAL && !added)
1687 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1688 std::vector<partial_symbol *> *list
1689 = (where == psymbol_placement::STATIC
1690 ? &objfile->partial_symtabs->static_psymbols
1691 : &objfile->partial_symtabs->global_psymbols);
1692 append_psymbol_to_list (list, psym, objfile);
1695 /* See psympriv.h. */
1698 init_psymbol_list (struct objfile *objfile, int total_symbols)
1700 if (objfile->partial_symtabs->global_psymbols.capacity () == 0
1701 && objfile->partial_symtabs->static_psymbols.capacity () == 0)
1703 /* Current best guess is that approximately a twentieth of the
1704 total symbols (in a debugging file) are global or static
1705 oriented symbols, then multiply that by slop factor of
1707 objfile->partial_symtabs->global_psymbols.reserve (total_symbols / 10);
1708 objfile->partial_symtabs->static_psymbols.reserve (total_symbols / 10);
1712 /* See psympriv.h. */
1714 struct partial_symtab *
1715 allocate_psymtab (const char *filename, struct objfile *objfile)
1717 struct partial_symtab *psymtab;
1719 if (objfile->partial_symtabs->free_psymtabs)
1721 psymtab = objfile->partial_symtabs->free_psymtabs;
1722 objfile->partial_symtabs->free_psymtabs = psymtab->next;
1725 psymtab = XOBNEW (&objfile->objfile_obstack, partial_symtab);
1727 memset (psymtab, 0, sizeof (struct partial_symtab));
1729 = (const char *) bcache (filename, strlen (filename) + 1,
1730 objfile->per_bfd->filename_cache);
1731 psymtab->compunit_symtab = NULL;
1733 /* Prepend it to the psymtab list for the objfile it belongs to.
1734 Psymtabs are searched in most recent inserted -> least recent
1737 psymtab->next = objfile->partial_symtabs->psymtabs;
1738 objfile->partial_symtabs->psymtabs = psymtab;
1740 if (symtab_create_debug)
1742 /* Be a bit clever with debugging messages, and don't print objfile
1743 every time, only when it changes. */
1744 static char *last_objfile_name = NULL;
1746 if (last_objfile_name == NULL
1747 || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
1749 xfree (last_objfile_name);
1750 last_objfile_name = xstrdup (objfile_name (objfile));
1751 fprintf_filtered (gdb_stdlog,
1752 "Creating one or more psymtabs for objfile %s ...\n",
1755 fprintf_filtered (gdb_stdlog,
1756 "Created psymtab %s for module %s.\n",
1757 host_address_to_string (psymtab), filename);
1764 psymtab_storage::discard_psymtab (struct partial_symtab *pst)
1766 struct partial_symtab **prev_pst;
1769 Empty psymtabs happen as a result of header files which don't
1770 have any symbols in them. There can be a lot of them. But this
1771 check is wrong, in that a psymtab with N_SLINE entries but
1772 nothing else is not empty, but we don't realize that. Fixing
1773 that without slowing things down might be tricky. */
1775 /* First, snip it out of the psymtab chain. */
1777 prev_pst = &psymtabs;
1778 while ((*prev_pst) != pst)
1779 prev_pst = &((*prev_pst)->next);
1780 (*prev_pst) = pst->next;
1782 /* Next, put it on a free list for recycling. */
1784 pst->next = free_psymtabs;
1785 free_psymtabs = pst;
1790 /* We need to pass a couple of items to the addrmap_foreach function,
1793 struct dump_psymtab_addrmap_data
1795 struct objfile *objfile;
1796 struct partial_symtab *psymtab;
1797 struct ui_file *outfile;
1799 /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1800 If so, we want to print the next one as well (since the next addrmap
1801 entry defines the end of the range). */
1802 int previous_matched;
1805 /* Helper function for dump_psymtab_addrmap to print an addrmap entry. */
1808 dump_psymtab_addrmap_1 (void *datap, CORE_ADDR start_addr, void *obj)
1810 struct dump_psymtab_addrmap_data *data
1811 = (struct dump_psymtab_addrmap_data *) datap;
1812 struct gdbarch *gdbarch = get_objfile_arch (data->objfile);
1813 struct partial_symtab *addrmap_psymtab = (struct partial_symtab *) obj;
1814 const char *psymtab_address_or_end = NULL;
1818 if (data->psymtab == NULL
1819 || data->psymtab == addrmap_psymtab)
1820 psymtab_address_or_end = host_address_to_string (addrmap_psymtab);
1821 else if (data->previous_matched)
1822 psymtab_address_or_end = "<ends here>";
1824 if (data->psymtab == NULL
1825 || data->psymtab == addrmap_psymtab
1826 || data->previous_matched)
1828 fprintf_filtered (data->outfile, " %s%s %s\n",
1829 data->psymtab != NULL ? " " : "",
1830 paddress (gdbarch, start_addr),
1831 psymtab_address_or_end);
1834 data->previous_matched = (data->psymtab == NULL
1835 || data->psymtab == addrmap_psymtab);
1840 /* Helper function for maintenance_print_psymbols to print the addrmap
1841 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1844 dump_psymtab_addrmap (struct objfile *objfile, struct partial_symtab *psymtab,
1845 struct ui_file *outfile)
1847 struct dump_psymtab_addrmap_data addrmap_dump_data;
1849 if ((psymtab == NULL
1850 || psymtab->psymtabs_addrmap_supported)
1851 && objfile->partial_symtabs->psymtabs_addrmap != NULL)
1853 addrmap_dump_data.objfile = objfile;
1854 addrmap_dump_data.psymtab = psymtab;
1855 addrmap_dump_data.outfile = outfile;
1856 addrmap_dump_data.previous_matched = 0;
1857 fprintf_filtered (outfile, "%sddress map:\n",
1858 psymtab == NULL ? "Entire a" : " A");
1859 addrmap_foreach (objfile->partial_symtabs->psymtabs_addrmap,
1860 dump_psymtab_addrmap_1, &addrmap_dump_data);
1865 maintenance_print_psymbols (const char *args, int from_tty)
1867 struct ui_file *outfile = gdb_stdout;
1868 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1869 int i, outfile_idx, found;
1871 struct obj_section *section = NULL;
1875 gdb_argv argv (args);
1877 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1879 if (strcmp (argv[i], "-pc") == 0)
1881 if (argv[i + 1] == NULL)
1882 error (_("Missing pc value"));
1883 address_arg = argv[++i];
1885 else if (strcmp (argv[i], "-source") == 0)
1887 if (argv[i + 1] == NULL)
1888 error (_("Missing source file"));
1889 source_arg = argv[++i];
1891 else if (strcmp (argv[i], "-objfile") == 0)
1893 if (argv[i + 1] == NULL)
1894 error (_("Missing objfile name"));
1895 objfile_arg = argv[++i];
1897 else if (strcmp (argv[i], "--") == 0)
1899 /* End of options. */
1903 else if (argv[i][0] == '-')
1905 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1906 error (_("Unknown option: %s"), argv[i]);
1913 if (address_arg != NULL && source_arg != NULL)
1914 error (_("Must specify at most one of -pc and -source"));
1916 stdio_file arg_outfile;
1918 if (argv != NULL && argv[outfile_idx] != NULL)
1920 if (argv[outfile_idx + 1] != NULL)
1921 error (_("Junk at end of command"));
1922 gdb::unique_xmalloc_ptr<char> outfile_name
1923 (tilde_expand (argv[outfile_idx]));
1924 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1925 perror_with_name (outfile_name.get ());
1926 outfile = &arg_outfile;
1929 if (address_arg != NULL)
1931 pc = parse_and_eval_address (address_arg);
1932 /* If we fail to find a section, that's ok, try the lookup anyway. */
1933 section = find_pc_section (pc);
1937 for (objfile *objfile : all_objfiles (current_program_space))
1939 int printed_objfile_header = 0;
1940 int print_for_objfile = 1;
1943 if (objfile_arg != NULL)
1945 = compare_filenames_for_search (objfile_name (objfile),
1947 if (!print_for_objfile)
1950 if (address_arg != NULL)
1952 struct bound_minimal_symbol msymbol = { NULL, NULL };
1954 /* We don't assume each pc has a unique objfile (this is for
1956 struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc,
1960 if (!printed_objfile_header)
1962 outfile->printf ("\nPartial symtabs for objfile %s\n",
1963 objfile_name (objfile));
1964 printed_objfile_header = 1;
1966 dump_psymtab (objfile, ps, outfile);
1967 dump_psymtab_addrmap (objfile, ps, outfile);
1973 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1975 int print_for_source = 0;
1978 if (source_arg != NULL)
1981 = compare_filenames_for_search (ps->filename, source_arg);
1984 if (source_arg == NULL
1985 || print_for_source)
1987 if (!printed_objfile_header)
1989 outfile->printf ("\nPartial symtabs for objfile %s\n",
1990 objfile_name (objfile));
1991 printed_objfile_header = 1;
1993 dump_psymtab (objfile, ps, outfile);
1994 dump_psymtab_addrmap (objfile, ps, outfile);
1999 /* If we're printing all the objfile's symbols dump the full addrmap. */
2001 if (address_arg == NULL
2002 && source_arg == NULL
2003 && objfile->partial_symtabs->psymtabs_addrmap != NULL)
2005 outfile->puts ("\n");
2006 dump_psymtab_addrmap (objfile, NULL, outfile);
2012 if (address_arg != NULL)
2013 error (_("No partial symtab for address: %s"), address_arg);
2014 if (source_arg != NULL)
2015 error (_("No partial symtab for source file: %s"), source_arg);
2019 /* List all the partial symbol tables whose names match REGEXP (optional). */
2022 maintenance_info_psymtabs (const char *regexp, int from_tty)
2024 struct program_space *pspace;
2029 ALL_PSPACES (pspace)
2030 for (objfile *objfile : all_objfiles (pspace))
2032 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2034 /* We don't want to print anything for this objfile until we
2035 actually find a symtab whose name matches. */
2036 int printed_objfile_start = 0;
2038 for (partial_symtab *psymtab : require_partial_symbols (objfile, 1))
2043 || re_exec (psymtab->filename))
2045 if (! printed_objfile_start)
2047 printf_filtered ("{ objfile %s ", objfile_name (objfile));
2049 printf_filtered ("((struct objfile *) %s)\n",
2050 host_address_to_string (objfile));
2051 printed_objfile_start = 1;
2054 printf_filtered (" { psymtab %s ", psymtab->filename);
2056 printf_filtered ("((struct partial_symtab *) %s)\n",
2057 host_address_to_string (psymtab));
2059 printf_filtered (" readin %s\n",
2060 psymtab->readin ? "yes" : "no");
2061 printf_filtered (" fullname %s\n",
2063 ? psymtab->fullname : "(null)");
2064 printf_filtered (" text addresses ");
2065 fputs_filtered (paddress (gdbarch,
2066 psymtab->text_low (objfile)),
2068 printf_filtered (" -- ");
2069 fputs_filtered (paddress (gdbarch,
2070 psymtab->text_high (objfile)),
2072 printf_filtered ("\n");
2073 printf_filtered (" psymtabs_addrmap_supported %s\n",
2074 (psymtab->psymtabs_addrmap_supported
2076 printf_filtered (" globals ");
2077 if (psymtab->n_global_syms)
2079 auto p = &(objfile->partial_symtabs
2080 ->global_psymbols[psymtab->globals_offset]);
2083 ("(* (struct partial_symbol **) %s @ %d)\n",
2084 host_address_to_string (p),
2085 psymtab->n_global_syms);
2088 printf_filtered ("(none)\n");
2089 printf_filtered (" statics ");
2090 if (psymtab->n_static_syms)
2092 auto p = &(objfile->partial_symtabs
2093 ->static_psymbols[psymtab->statics_offset]);
2096 ("(* (struct partial_symbol **) %s @ %d)\n",
2097 host_address_to_string (p),
2098 psymtab->n_static_syms);
2101 printf_filtered ("(none)\n");
2102 printf_filtered (" dependencies ");
2103 if (psymtab->number_of_dependencies)
2107 printf_filtered ("{\n");
2108 for (i = 0; i < psymtab->number_of_dependencies; i++)
2110 struct partial_symtab *dep = psymtab->dependencies[i];
2112 /* Note the string concatenation there --- no
2114 printf_filtered (" psymtab %s "
2115 "((struct partial_symtab *) %s)\n",
2117 host_address_to_string (dep));
2119 printf_filtered (" }\n");
2122 printf_filtered ("(none)\n");
2123 printf_filtered (" }\n");
2127 if (printed_objfile_start)
2128 printf_filtered ("}\n");
2132 /* Check consistency of currently expanded psymtabs vs symtabs. */
2135 maintenance_check_psymtabs (const char *ignore, int from_tty)
2138 struct compunit_symtab *cust = NULL;
2139 const struct blockvector *bv;
2143 for (objfile *objfile : all_objfiles (current_program_space))
2144 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
2146 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2148 /* We don't call psymtab_to_symtab here because that may cause symtab
2149 expansion. When debugging a problem it helps if checkers leave
2150 things unchanged. */
2151 cust = ps->compunit_symtab;
2153 /* First do some checks that don't require the associated symtab. */
2154 if (ps->text_high (objfile) < ps->text_low (objfile))
2156 printf_filtered ("Psymtab ");
2157 puts_filtered (ps->filename);
2158 printf_filtered (" covers bad range ");
2159 fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2161 printf_filtered (" - ");
2162 fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2164 printf_filtered ("\n");
2168 /* Now do checks requiring the associated symtab. */
2171 bv = COMPUNIT_BLOCKVECTOR (cust);
2172 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
2173 partial_symbol **psym
2174 = &objfile->partial_symtabs->static_psymbols[ps->statics_offset];
2175 length = ps->n_static_syms;
2178 sym = block_lookup_symbol (b, symbol_search_name (*psym),
2179 symbol_name_match_type::SEARCH_NAME,
2183 printf_filtered ("Static symbol `");
2184 puts_filtered ((*psym)->name);
2185 printf_filtered ("' only found in ");
2186 puts_filtered (ps->filename);
2187 printf_filtered (" psymtab\n");
2191 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2192 psym = &objfile->partial_symtabs->global_psymbols[ps->globals_offset];
2193 length = ps->n_global_syms;
2196 sym = block_lookup_symbol (b, symbol_search_name (*psym),
2197 symbol_name_match_type::SEARCH_NAME,
2201 printf_filtered ("Global symbol `");
2202 puts_filtered ((*psym)->name);
2203 printf_filtered ("' only found in ");
2204 puts_filtered (ps->filename);
2205 printf_filtered (" psymtab\n");
2209 if (ps->raw_text_high () != 0
2210 && (ps->text_low (objfile) < BLOCK_START (b)
2211 || ps->text_high (objfile) > BLOCK_END (b)))
2213 printf_filtered ("Psymtab ");
2214 puts_filtered (ps->filename);
2215 printf_filtered (" covers ");
2216 fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2218 printf_filtered (" - ");
2219 fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2221 printf_filtered (" but symtab covers only ");
2222 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2223 printf_filtered (" - ");
2224 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2225 printf_filtered ("\n");
2231 _initialize_psymtab (void)
2233 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2234 Print dump of current partial symbol definitions.\n\
2235 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
2236 mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
2237 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2238 or the terminal if OUTFILE is unspecified.\n\
2239 If ADDRESS is provided, dump only the file for that address.\n\
2240 If SOURCE is provided, dump only that file's symbols.\n\
2241 If OBJFILE is provided, dump only that file's minimal symbols."),
2242 &maintenanceprintlist);
2244 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2245 List the partial symbol tables for all object files.\n\
2246 This does not include information about individual partial symbols,\n\
2247 just the symbol table structures themselves."),
2248 &maintenanceinfolist);
2250 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2252 Check consistency of currently expanded psymtabs versus symtabs."),