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 ()
71 : psymbol_cache (psymbol_bcache_init ())
75 psymtab_storage::~psymtab_storage ()
77 psymbol_bcache_free (psymbol_cache);
82 struct partial_symtab *
83 psymtab_storage::allocate_psymtab ()
85 struct partial_symtab *psymtab;
87 if (free_psymtabs != nullptr)
89 psymtab = free_psymtabs;
90 free_psymtabs = psymtab->next;
93 psymtab = XOBNEW (obstack (), struct partial_symtab);
95 memset (psymtab, 0, sizeof (struct partial_symtab));
97 psymtab->next = psymtabs;
107 psymtab_storage::partial_symtab_range
108 require_partial_symbols (struct objfile *objfile, int verbose)
110 if ((objfile->flags & OBJF_PSYMTABS_READ) == 0)
112 objfile->flags |= OBJF_PSYMTABS_READ;
114 if (objfile->sf->sym_read_psymbols)
117 printf_filtered (_("Reading symbols from %s...\n"),
118 objfile_name (objfile));
119 (*objfile->sf->sym_read_psymbols) (objfile);
121 /* Partial symbols list are not expected to changed after this
123 objfile->partial_symtabs->global_psymbols.shrink_to_fit ();
124 objfile->partial_symtabs->static_psymbols.shrink_to_fit ();
126 if (verbose && !objfile_has_symbols (objfile))
127 printf_filtered (_("(No debugging symbols found in %s)\n"),
128 objfile_name (objfile));
132 return objfile->psymtabs ();
135 /* Helper function for psym_map_symtabs_matching_filename that
136 expands the symtabs and calls the iterator. */
139 partial_map_expand_apply (struct objfile *objfile,
141 const char *real_path,
142 struct partial_symtab *pst,
143 gdb::function_view<bool (symtab *)> callback)
145 struct compunit_symtab *last_made = objfile->compunit_symtabs;
147 /* Shared psymtabs should never be seen here. Instead they should
148 be handled properly by the caller. */
149 gdb_assert (pst->user == NULL);
151 /* Don't visit already-expanded psymtabs. */
155 /* This may expand more than one symtab, and we want to iterate over
157 psymtab_to_symtab (objfile, pst);
159 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
160 last_made, callback);
163 /* Psymtab version of map_symtabs_matching_filename. See its definition in
164 the definition of quick_symbol_functions in symfile.h. */
167 psym_map_symtabs_matching_filename
168 (struct objfile *objfile,
170 const char *real_path,
171 gdb::function_view<bool (symtab *)> callback)
173 const char *name_basename = lbasename (name);
175 for (partial_symtab *pst : require_partial_symbols (objfile, 1))
177 /* We can skip shared psymtabs here, because any file name will be
178 attached to the unshared psymtab. */
179 if (pst->user != NULL)
182 /* Anonymous psymtabs don't have a file name. */
186 if (compare_filenames_for_search (pst->filename, name))
188 if (partial_map_expand_apply (objfile, name, real_path,
194 /* Before we invoke realpath, which can get expensive when many
195 files are involved, do a quick comparison of the basenames. */
196 if (! basenames_may_differ
197 && FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
200 if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
202 if (partial_map_expand_apply (objfile, name, real_path,
208 /* If the user gave us an absolute path, try to find the file in
209 this symtab and use its absolute path. */
210 if (real_path != NULL)
212 gdb_assert (IS_ABSOLUTE_PATH (real_path));
213 gdb_assert (IS_ABSOLUTE_PATH (name));
214 if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
216 if (partial_map_expand_apply (objfile, name, real_path,
227 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
228 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
230 static struct partial_symtab *
231 find_pc_sect_psymtab_closer (struct objfile *objfile,
232 CORE_ADDR pc, struct obj_section *section,
233 struct partial_symtab *pst,
234 struct bound_minimal_symbol msymbol)
236 struct partial_symtab *tpst;
237 struct partial_symtab *best_pst = pst;
238 CORE_ADDR best_addr = pst->text_low (objfile);
240 gdb_assert (!pst->psymtabs_addrmap_supported);
242 /* An objfile that has its functions reordered might have
243 many partial symbol tables containing the PC, but
244 we want the partial symbol table that contains the
245 function containing the PC. */
246 if (!(objfile->flags & OBJF_REORDERED)
247 && section == NULL) /* Can't validate section this way. */
250 if (msymbol.minsym == NULL)
253 /* The code range of partial symtabs sometimes overlap, so, in
254 the loop below, we need to check all partial symtabs and
255 find the one that fits better for the given PC address. We
256 select the partial symtab that contains a symbol whose
257 address is closest to the PC address. By closest we mean
258 that find_pc_sect_symbol returns the symbol with address
259 that is closest and still less than the given PC. */
260 for (tpst = pst; tpst != NULL; tpst = tpst->next)
262 if (pc >= tpst->text_low (objfile) && pc < tpst->text_high (objfile))
264 struct partial_symbol *p;
267 /* NOTE: This assumes that every psymbol has a
268 corresponding msymbol, which is not necessarily
269 true; the debug info might be much richer than the
270 object's symbol table. */
271 p = find_pc_sect_psymbol (objfile, tpst, pc, section);
273 && (p->address (objfile) == BMSYMBOL_VALUE_ADDRESS (msymbol)))
276 /* Also accept the textlow value of a psymtab as a
277 "symbol", to provide some support for partial
278 symbol tables with line information but no debug
279 symbols (e.g. those produced by an assembler). */
281 this_addr = p->address (objfile);
283 this_addr = tpst->text_low (objfile);
285 /* Check whether it is closer than our current
286 BEST_ADDR. Since this symbol address is
287 necessarily lower or equal to PC, the symbol closer
288 to PC is the symbol which address is the highest.
289 This way we return the psymtab which contains such
290 best match symbol. This can help in cases where the
291 symbol information/debuginfo is not complete, like
292 for instance on IRIX6 with gcc, where no debug info
293 is emitted for statics. (See also the nodebug.exp
295 if (this_addr > best_addr)
297 best_addr = this_addr;
305 /* Find which partial symtab contains PC and SECTION. Return NULL if
306 none. We return the psymtab that contains a symbol whose address
307 exactly matches PC, or, if we cannot find an exact match, the
308 psymtab that contains a symbol whose address is closest to PC. */
310 static struct partial_symtab *
311 find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
312 struct obj_section *section,
313 struct bound_minimal_symbol msymbol)
315 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
316 SECT_OFF_TEXT (objfile));
318 /* Try just the PSYMTABS_ADDRMAP mapping first as it has better granularity
319 than the later used TEXTLOW/TEXTHIGH one. */
321 if (objfile->partial_symtabs->psymtabs_addrmap != NULL)
323 struct partial_symtab *pst
324 = ((struct partial_symtab *)
325 addrmap_find (objfile->partial_symtabs->psymtabs_addrmap,
329 /* FIXME: addrmaps currently do not handle overlayed sections,
330 so fall back to the non-addrmap case if we're debugging
331 overlays and the addrmap returned the wrong section. */
332 if (overlay_debugging && msymbol.minsym != NULL && section != NULL)
334 struct partial_symbol *p;
336 /* NOTE: This assumes that every psymbol has a
337 corresponding msymbol, which is not necessarily
338 true; the debug info might be much richer than the
339 object's symbol table. */
340 p = find_pc_sect_psymbol (objfile, pst, pc, section);
342 || (p->address (objfile)
343 != BMSYMBOL_VALUE_ADDRESS (msymbol)))
347 /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
348 PSYMTABS_ADDRMAP we used has already the best 1-byte
349 granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
350 a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
359 /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
360 which still have no corresponding full SYMTABs read. But it is not
361 present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
364 /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
365 its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
366 debug info type in single OBJFILE. */
368 for (partial_symtab *pst : require_partial_symbols (objfile, 1))
369 if (!pst->psymtabs_addrmap_supported
370 && pc >= pst->text_low (objfile) && pc < pst->text_high (objfile))
372 struct partial_symtab *best_pst;
374 best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
376 if (best_pst != NULL)
383 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
384 the definition of quick_symbol_functions in symfile.h. */
386 static struct compunit_symtab *
387 psym_find_pc_sect_compunit_symtab (struct objfile *objfile,
388 struct bound_minimal_symbol msymbol,
390 struct obj_section *section,
393 struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc, section,
397 if (warn_if_readin && ps->readin)
398 /* Might want to error() here (in case symtab is corrupt and
399 will cause a core dump), but maybe we can successfully
400 continue, so let's not. */
402 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
403 paddress (get_objfile_arch (objfile), pc));
404 psymtab_to_symtab (objfile, ps);
405 return ps->compunit_symtab;
410 /* Find which partial symbol within a psymtab matches PC and SECTION.
411 Return NULL if none. */
413 static struct partial_symbol *
414 find_pc_sect_psymbol (struct objfile *objfile,
415 struct partial_symtab *psymtab, CORE_ADDR pc,
416 struct obj_section *section)
418 struct partial_symbol *best = NULL;
420 const CORE_ADDR textlow = psymtab->text_low (objfile);
422 gdb_assert (psymtab != NULL);
424 /* Cope with programs that start at address 0. */
425 best_pc = (textlow != 0) ? textlow - 1 : 0;
427 /* Search the global symbols as well as the static symbols, so that
428 find_pc_partial_function doesn't use a minimal symbol and thus
429 cache a bad endaddr. */
430 for (int i = 0; i < psymtab->n_global_syms; i++)
433 = objfile->partial_symtabs->global_psymbols[psymtab->globals_offset
436 if (p->domain == VAR_DOMAIN
437 && p->aclass == LOC_BLOCK
438 && pc >= p->address (objfile)
439 && (p->address (objfile) > best_pc
440 || (psymtab->text_low (objfile) == 0
441 && best_pc == 0 && p->address (objfile) == 0)))
443 if (section != NULL) /* Match on a specific section. */
445 if (!matching_obj_sections (p->obj_section (objfile),
449 best_pc = p->address (objfile);
454 for (int i = 0; i < psymtab->n_static_syms; i++)
457 = objfile->partial_symtabs->static_psymbols[psymtab->statics_offset
460 if (p->domain == VAR_DOMAIN
461 && p->aclass == LOC_BLOCK
462 && pc >= p->address (objfile)
463 && (p->address (objfile) > best_pc
464 || (psymtab->text_low (objfile) == 0
465 && best_pc == 0 && p->address (objfile) == 0)))
467 if (section != NULL) /* Match on a specific section. */
469 if (!matching_obj_sections (p->obj_section (objfile),
473 best_pc = p->address (objfile);
481 /* Psymtab version of lookup_symbol. See its definition in
482 the definition of quick_symbol_functions in symfile.h. */
484 static struct compunit_symtab *
485 psym_lookup_symbol (struct objfile *objfile,
486 int block_index, const char *name,
487 const domain_enum domain)
489 const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
490 struct compunit_symtab *stab_best = NULL;
492 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
494 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
496 if (!ps->readin && lookup_partial_symbol (objfile, ps, name,
497 psymtab_index, domain))
499 struct symbol *sym, *with_opaque = NULL;
500 struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
501 /* Note: While psymtab_to_symtab can return NULL if the
502 partial symtab is empty, we can assume it won't here
503 because lookup_partial_symbol succeeded. */
504 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
505 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
507 sym = block_find_symbol (block, name, domain,
508 block_find_non_opaque_type_preferred,
511 /* Some caution must be observed with overloaded functions
512 and methods, since the index will not contain any overload
513 information (but NAME might contain it). */
516 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
518 if (with_opaque != NULL
519 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
522 /* Keep looking through other psymtabs. */
529 /* Returns true if PSYM matches LOOKUP_NAME. */
532 psymbol_name_matches (partial_symbol *psym,
533 const lookup_name_info &lookup_name)
535 const language_defn *lang = language_def (psym->ginfo.language);
536 symbol_name_matcher_ftype *name_match
537 = get_symbol_name_matcher (lang, lookup_name);
538 return name_match (symbol_search_name (&psym->ginfo), lookup_name, NULL);
541 /* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
542 the global block of PST if GLOBAL, and otherwise the static block.
543 MATCH is the comparison operation that returns true iff MATCH (s,
544 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
545 non-null, the symbols in the block are assumed to be ordered
546 according to it (allowing binary search). It must be compatible
547 with MATCH. Returns the symbol, if found, and otherwise NULL. */
549 static struct partial_symbol *
550 match_partial_symbol (struct objfile *objfile,
551 struct partial_symtab *pst, int global,
552 const char *name, domain_enum domain,
553 symbol_name_match_type match_type,
554 symbol_compare_ftype *ordered_compare)
556 struct partial_symbol **start, **psym;
557 struct partial_symbol **top, **real_top, **bottom, **center;
558 int length = (global ? pst->n_global_syms : pst->n_static_syms);
559 int do_linear_search = 1;
564 lookup_name_info lookup_name (name, match_type);
567 &objfile->partial_symtabs->global_psymbols[pst->globals_offset] :
568 &objfile->partial_symtabs->static_psymbols[pst->statics_offset]);
570 if (global && ordered_compare) /* Can use a binary search. */
572 do_linear_search = 0;
574 /* Binary search. This search is guaranteed to end with center
575 pointing at the earliest partial symbol whose name might be
576 correct. At that point *all* partial symbols with an
577 appropriate name will be checked against the correct
581 top = start + length - 1;
585 center = bottom + (top - bottom) / 2;
586 gdb_assert (center < top);
588 enum language lang = (*center)->ginfo.language;
590 = lookup_name.language_lookup_name (lang).c_str ();
592 if (ordered_compare (symbol_search_name (&(*center)->ginfo),
598 gdb_assert (top == bottom);
600 while (top <= real_top
601 && psymbol_name_matches (*top, lookup_name))
603 if (symbol_matches_domain ((*top)->ginfo.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)->ginfo.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)->ginfo),
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)->ginfo,
720 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
723 while (top <= real_top && symbol_matches_search_name (&(*top)->ginfo,
726 if (symbol_matches_domain ((*top)->ginfo.language,
727 (*top)->domain, domain))
733 /* Can't use a binary search or else we found during the binary search that
734 we should also do a linear search. */
736 if (do_linear_search)
738 for (psym = start; psym < start + length; psym++)
740 if (symbol_matches_domain ((*psym)->ginfo.language,
741 (*psym)->domain, domain)
742 && symbol_matches_search_name (&(*psym)->ginfo, lookup_name))
750 /* Get the symbol table that corresponds to a partial_symtab.
751 This is fast after the first time you do it.
752 The result will be NULL if the primary symtab has no symbols,
753 which can happen. Otherwise the result is the primary symtab
754 that contains PST. */
756 static struct compunit_symtab *
757 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
759 /* If it is a shared psymtab, find an unshared psymtab that includes
760 it. Any such psymtab will do. */
761 while (pst->user != NULL)
764 /* If it's been looked up before, return it. */
765 if (pst->compunit_symtab)
766 return pst->compunit_symtab;
768 /* If it has not yet been read in, read it. */
771 scoped_restore decrementer = increment_reading_symtab ();
773 (*pst->read_symtab) (pst, objfile);
776 return pst->compunit_symtab;
779 /* Psymtab version of find_last_source_symtab. See its definition in
780 the definition of quick_symbol_functions in symfile.h. */
782 static struct symtab *
783 psym_find_last_source_symtab (struct objfile *ofp)
785 struct partial_symtab *cs_pst = NULL;
787 for (partial_symtab *ps : require_partial_symbols (ofp, 1))
789 const char *name = ps->filename;
790 int len = strlen (name);
792 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
793 || strcmp (name, "<<C++-namespaces>>") == 0)))
801 internal_error (__FILE__, __LINE__,
802 _("select_source_symtab: "
803 "readin pst found and no symtabs."));
807 struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
811 return compunit_primary_filetab (cust);
817 /* Psymtab version of forget_cached_source_info. See its definition in
818 the definition of quick_symbol_functions in symfile.h. */
821 psym_forget_cached_source_info (struct objfile *objfile)
823 for (partial_symtab *pst : require_partial_symbols (objfile, 1))
825 if (pst->fullname != NULL)
827 xfree (pst->fullname);
828 pst->fullname = NULL;
834 print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
835 struct partial_symbol **p, int count, const char *what,
836 struct ui_file *outfile)
838 fprintf_filtered (outfile, " %s partial symbols:\n", what);
842 fprintf_filtered (outfile, " `%s'", (*p)->ginfo.name);
843 if (symbol_demangled_name (&(*p)->ginfo) != NULL)
845 fprintf_filtered (outfile, " `%s'",
846 symbol_demangled_name (&(*p)->ginfo));
848 fputs_filtered (", ", outfile);
849 switch ((*p)->domain)
852 fputs_filtered ("undefined domain, ", outfile);
855 /* This is the usual thing -- don't print it. */
858 fputs_filtered ("struct domain, ", outfile);
861 fputs_filtered ("label domain, ", outfile);
864 fputs_filtered ("<invalid domain>, ", outfile);
867 switch ((*p)->aclass)
870 fputs_filtered ("undefined", outfile);
873 fputs_filtered ("constant int", outfile);
876 fputs_filtered ("static", outfile);
879 fputs_filtered ("register", outfile);
882 fputs_filtered ("pass by value", outfile);
885 fputs_filtered ("pass by reference", outfile);
887 case LOC_REGPARM_ADDR:
888 fputs_filtered ("register address parameter", outfile);
891 fputs_filtered ("stack parameter", outfile);
894 fputs_filtered ("type", outfile);
897 fputs_filtered ("label", outfile);
900 fputs_filtered ("function", outfile);
902 case LOC_CONST_BYTES:
903 fputs_filtered ("constant bytes", outfile);
906 fputs_filtered ("unresolved", outfile);
908 case LOC_OPTIMIZED_OUT:
909 fputs_filtered ("optimized out", outfile);
912 fputs_filtered ("computed at runtime", outfile);
915 fputs_filtered ("<invalid location>", outfile);
918 fputs_filtered (", ", outfile);
919 fputs_filtered (paddress (gdbarch, (*p)->unrelocated_address ()), outfile);
920 fprintf_filtered (outfile, "\n");
926 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
927 struct ui_file *outfile)
929 struct gdbarch *gdbarch = get_objfile_arch (objfile);
932 if (psymtab->anonymous)
934 fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
939 fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
942 fprintf_filtered (outfile, "(object ");
943 gdb_print_host_address (psymtab, outfile);
944 fprintf_filtered (outfile, ")\n\n");
945 fprintf_filtered (outfile, " Read from object file %s (",
946 objfile_name (objfile));
947 gdb_print_host_address (objfile, outfile);
948 fprintf_filtered (outfile, ")\n");
952 fprintf_filtered (outfile,
953 " Full symtab was read (at ");
954 gdb_print_host_address (psymtab->compunit_symtab, outfile);
955 fprintf_filtered (outfile, " by function at ");
956 gdb_print_host_address (psymtab->read_symtab, outfile);
957 fprintf_filtered (outfile, ")\n");
960 fprintf_filtered (outfile, " Symbols cover text addresses ");
961 fputs_filtered (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
962 fprintf_filtered (outfile, "-");
963 fputs_filtered (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
964 fprintf_filtered (outfile, "\n");
965 fprintf_filtered (outfile, " Address map supported - %s.\n",
966 psymtab->psymtabs_addrmap_supported ? "yes" : "no");
967 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
968 psymtab->number_of_dependencies);
969 for (i = 0; i < psymtab->number_of_dependencies; i++)
971 fprintf_filtered (outfile, " %d ", i);
972 gdb_print_host_address (psymtab->dependencies[i], outfile);
973 fprintf_filtered (outfile, " %s\n",
974 psymtab->dependencies[i]->filename);
976 if (psymtab->user != NULL)
978 fprintf_filtered (outfile, " Shared partial symtab with user ");
979 gdb_print_host_address (psymtab->user, outfile);
980 fprintf_filtered (outfile, "\n");
982 if (psymtab->n_global_syms > 0)
984 print_partial_symbols
986 &objfile->partial_symtabs->global_psymbols[psymtab->globals_offset],
987 psymtab->n_global_syms, "Global", outfile);
989 if (psymtab->n_static_syms > 0)
991 print_partial_symbols
993 &objfile->partial_symtabs->static_psymbols[psymtab->statics_offset],
994 psymtab->n_static_syms, "Static", outfile);
996 fprintf_filtered (outfile, "\n");
999 /* Psymtab version of print_stats. See its definition in
1000 the definition of quick_symbol_functions in symfile.h. */
1003 psym_print_stats (struct objfile *objfile)
1008 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1010 if (ps->readin == 0)
1013 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i);
1016 /* Psymtab version of dump. See its definition in
1017 the definition of quick_symbol_functions in symfile.h. */
1020 psym_dump (struct objfile *objfile)
1022 struct partial_symtab *psymtab;
1024 if (objfile->partial_symtabs->psymtabs)
1026 printf_filtered ("Psymtabs:\n");
1027 for (psymtab = objfile->partial_symtabs->psymtabs;
1029 psymtab = psymtab->next)
1031 printf_filtered ("%s at ",
1033 gdb_print_host_address (psymtab, gdb_stdout);
1034 printf_filtered (", ");
1037 printf_filtered ("\n\n");
1041 /* Psymtab version of expand_symtabs_for_function. See its definition in
1042 the definition of quick_symbol_functions in symfile.h. */
1045 psym_expand_symtabs_for_function (struct objfile *objfile,
1046 const char *func_name)
1048 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1053 if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
1055 || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
1057 psymtab_to_symtab (objfile, ps);
1061 /* Psymtab version of expand_all_symtabs. See its definition in
1062 the definition of quick_symbol_functions in symfile.h. */
1065 psym_expand_all_symtabs (struct objfile *objfile)
1067 for (partial_symtab *psymtab : require_partial_symbols (objfile, 1))
1068 psymtab_to_symtab (objfile, psymtab);
1071 /* Psymtab version of expand_symtabs_with_fullname. See its definition in
1072 the definition of quick_symbol_functions in symfile.h. */
1075 psym_expand_symtabs_with_fullname (struct objfile *objfile,
1076 const char *fullname)
1078 for (partial_symtab *p : require_partial_symbols (objfile, 1))
1080 /* Anonymous psymtabs don't have a name of a source file. */
1084 /* psymtab_to_fullname tries to open the file which is slow.
1085 Don't call it if we know the basenames don't match. */
1086 if ((basenames_may_differ
1087 || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1088 && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
1089 psymtab_to_symtab (objfile, p);
1093 /* Psymtab version of map_symbol_filenames. See its definition in
1094 the definition of quick_symbol_functions in symfile.h. */
1097 psym_map_symbol_filenames (struct objfile *objfile,
1098 symbol_filename_ftype *fun, void *data,
1101 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1103 const char *fullname;
1108 /* We can skip shared psymtabs here, because any file name will be
1109 attached to the unshared psymtab. */
1110 if (ps->user != NULL)
1113 /* Anonymous psymtabs don't have a file name. */
1119 fullname = psymtab_to_fullname (ps);
1122 (*fun) (ps->filename, fullname, data);
1126 /* Finds the fullname that a partial_symtab represents.
1128 If this functions finds the fullname, it will save it in ps->fullname
1129 and it will also return the value.
1131 If this function fails to find the file that this partial_symtab represents,
1132 NULL will be returned and ps->fullname will be set to NULL. */
1135 psymtab_to_fullname (struct partial_symtab *ps)
1137 gdb_assert (!ps->anonymous);
1139 /* Use cached copy if we have it.
1140 We rely on forget_cached_source_info being called appropriately
1141 to handle cases like the file being moved. */
1142 if (ps->fullname == NULL)
1144 gdb::unique_xmalloc_ptr<char> fullname;
1145 scoped_fd fd = find_and_open_source (ps->filename, ps->dirname,
1147 ps->fullname = fullname.release ();
1151 /* rewrite_source_path would be applied by find_and_open_source, we
1152 should report the pathname where GDB tried to find the file. */
1154 if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
1155 fullname.reset (xstrdup (ps->filename));
1157 fullname.reset (concat (ps->dirname, SLASH_STRING,
1158 ps->filename, (char *) NULL));
1160 ps->fullname = rewrite_source_path (fullname.get ()).release ();
1161 if (ps->fullname == NULL)
1162 ps->fullname = fullname.release ();
1166 return ps->fullname;
1169 /* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1170 according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1171 BLOCK is assumed to come from OBJFILE. Returns 1 iff CALLBACK
1172 ever returns non-zero, and otherwise returns 0. */
1175 map_block (const char *name, domain_enum domain, struct objfile *objfile,
1176 struct block *block,
1177 int (*callback) (struct block *, struct symbol *, void *),
1178 void *data, symbol_name_match_type match)
1180 struct block_iterator iter;
1183 lookup_name_info lookup_name (name, match);
1185 for (sym = block_iter_match_first (block, lookup_name, &iter);
1187 sym = block_iter_match_next (lookup_name, &iter))
1189 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
1190 SYMBOL_DOMAIN (sym), domain))
1192 if (callback (block, sym, data))
1200 /* Psymtab version of map_matching_symbols. See its definition in
1201 the definition of quick_symbol_functions in symfile.h. */
1204 psym_map_matching_symbols (struct objfile *objfile,
1205 const char *name, domain_enum domain,
1207 int (*callback) (struct block *,
1208 struct symbol *, void *),
1210 symbol_name_match_type match,
1211 symbol_compare_ftype *ordered_compare)
1213 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
1215 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1219 || match_partial_symbol (objfile, ps, global, name, domain, match,
1222 struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
1223 struct block *block;
1227 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
1228 if (map_block (name, domain, objfile, block,
1229 callback, data, match))
1231 if (callback (block, NULL, data))
1237 /* A helper for psym_expand_symtabs_matching that handles searching
1238 included psymtabs. This returns true if a symbol is found, and
1239 false otherwise. It also updates the 'searched_flag' on the
1240 various psymtabs that it searches. */
1243 recursively_search_psymtabs
1244 (struct partial_symtab *ps,
1245 struct objfile *objfile,
1246 enum search_domain domain,
1247 const lookup_name_info &lookup_name,
1248 gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
1251 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
1254 if (ps->searched_flag != PST_NOT_SEARCHED)
1255 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1257 /* Recurse into shared psymtabs first, because they may have already
1258 been searched, and this could save some time. */
1259 for (i = 0; i < ps->number_of_dependencies; ++i)
1263 /* Skip non-shared dependencies, these are handled elsewhere. */
1264 if (ps->dependencies[i]->user == NULL)
1267 r = recursively_search_psymtabs (ps->dependencies[i],
1268 objfile, domain, lookup_name,
1272 ps->searched_flag = PST_SEARCHED_AND_FOUND;
1277 partial_symbol **gbound
1278 = (objfile->partial_symtabs->global_psymbols.data ()
1279 + ps->globals_offset + ps->n_global_syms);
1280 partial_symbol **sbound
1281 = (objfile->partial_symtabs->static_psymbols.data ()
1282 + ps->statics_offset + ps->n_static_syms);
1283 partial_symbol **bound = gbound;
1285 /* Go through all of the symbols stored in a partial
1286 symtab in one loop. */
1287 partial_symbol **psym = (objfile->partial_symtabs->global_psymbols.data ()
1288 + ps->globals_offset);
1293 if (bound == gbound && ps->n_static_syms != 0)
1295 psym = (objfile->partial_symtabs->static_psymbols.data ()
1296 + ps->statics_offset);
1307 if ((domain == ALL_DOMAIN
1308 || (domain == VARIABLES_DOMAIN
1309 && (*psym)->aclass != LOC_TYPEDEF
1310 && (*psym)->aclass != LOC_BLOCK)
1311 || (domain == FUNCTIONS_DOMAIN
1312 && (*psym)->aclass == LOC_BLOCK)
1313 || (domain == TYPES_DOMAIN
1314 && (*psym)->aclass == LOC_TYPEDEF))
1315 && psymbol_name_matches (*psym, lookup_name)
1316 && (sym_matcher == NULL
1317 || sym_matcher (symbol_search_name (&(*psym)->ginfo))))
1319 /* Found a match, so notify our caller. */
1320 result = PST_SEARCHED_AND_FOUND;
1327 ps->searched_flag = result;
1328 return result == PST_SEARCHED_AND_FOUND;
1331 /* Psymtab version of expand_symtabs_matching. See its definition in
1332 the definition of quick_symbol_functions in symfile.h. */
1335 psym_expand_symtabs_matching
1336 (struct objfile *objfile,
1337 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1338 const lookup_name_info &lookup_name_in,
1339 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1340 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1341 enum search_domain domain)
1343 lookup_name_info lookup_name = lookup_name_in.make_ignore_params ();
1345 /* Clear the search flags. */
1346 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1347 ps->searched_flag = PST_NOT_SEARCHED;
1349 for (partial_symtab *ps : objfile->psymtabs ())
1356 /* We skip shared psymtabs because file-matching doesn't apply
1357 to them; but we search them later in the loop. */
1358 if (ps->user != NULL)
1368 match = file_matcher (ps->filename, false);
1371 /* Before we invoke realpath, which can get expensive when many
1372 files are involved, do a quick comparison of the basenames. */
1373 if (basenames_may_differ
1374 || file_matcher (lbasename (ps->filename), true))
1375 match = file_matcher (psymtab_to_fullname (ps), false);
1381 if (recursively_search_psymtabs (ps, objfile, domain,
1382 lookup_name, symbol_matcher))
1384 struct compunit_symtab *symtab =
1385 psymtab_to_symtab (objfile, ps);
1387 if (expansion_notify != NULL)
1388 expansion_notify (symtab);
1393 /* Psymtab version of has_symbols. See its definition in
1394 the definition of quick_symbol_functions in symfile.h. */
1397 psym_has_symbols (struct objfile *objfile)
1399 return objfile->partial_symtabs->psymtabs != NULL;
1402 /* Helper function for psym_find_compunit_symtab_by_address that fills
1403 in psymbol_map for a given range of psymbols. */
1406 psym_fill_psymbol_map (struct objfile *objfile,
1407 struct partial_symtab *psymtab,
1408 std::set<CORE_ADDR> *seen_addrs,
1409 const std::vector<partial_symbol *> &symbols,
1413 for (int i = 0; i < length; ++i)
1415 struct partial_symbol *psym = symbols[start + i];
1417 if (psym->aclass == LOC_STATIC)
1419 CORE_ADDR addr = psym->address (objfile);
1420 if (seen_addrs->find (addr) == seen_addrs->end ())
1422 seen_addrs->insert (addr);
1423 objfile->psymbol_map.emplace_back (addr, psymtab);
1429 /* See find_compunit_symtab_by_address in quick_symbol_functions, in
1432 static compunit_symtab *
1433 psym_find_compunit_symtab_by_address (struct objfile *objfile,
1436 if (objfile->psymbol_map.empty ())
1438 std::set<CORE_ADDR> seen_addrs;
1440 for (partial_symtab *pst : require_partial_symbols (objfile, 1))
1442 psym_fill_psymbol_map (objfile, pst,
1444 objfile->partial_symtabs->global_psymbols,
1445 pst->globals_offset,
1446 pst->n_global_syms);
1447 psym_fill_psymbol_map (objfile, pst,
1449 objfile->partial_symtabs->static_psymbols,
1450 pst->statics_offset,
1451 pst->n_static_syms);
1454 objfile->psymbol_map.shrink_to_fit ();
1456 std::sort (objfile->psymbol_map.begin (), objfile->psymbol_map.end (),
1457 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1458 const std::pair<CORE_ADDR, partial_symtab *> &b)
1460 return a.first < b.first;
1464 auto iter = std::lower_bound
1465 (objfile->psymbol_map.begin (), objfile->psymbol_map.end (), address,
1466 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1472 if (iter == objfile->psymbol_map.end () || iter->first != address)
1475 return psymtab_to_symtab (objfile, iter->second);
1478 const struct quick_symbol_functions psym_functions =
1481 psym_find_last_source_symtab,
1482 psym_forget_cached_source_info,
1483 psym_map_symtabs_matching_filename,
1487 psym_expand_symtabs_for_function,
1488 psym_expand_all_symtabs,
1489 psym_expand_symtabs_with_fullname,
1490 psym_map_matching_symbols,
1491 psym_expand_symtabs_matching,
1492 psym_find_pc_sect_compunit_symtab,
1493 psym_find_compunit_symtab_by_address,
1494 psym_map_symbol_filenames
1500 sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
1502 /* Sort the global list; don't sort the static list. */
1503 auto begin = objfile->partial_symtabs->global_psymbols.begin ();
1504 std::advance (begin, pst->globals_offset);
1506 /* The psymbols for this partial_symtab are currently at the end of the
1508 auto end = objfile->partial_symtabs->global_psymbols.end ();
1510 std::sort (begin, end, [] (partial_symbol *s1, partial_symbol *s2)
1512 return strcmp_iw_ordered (symbol_search_name (&s1->ginfo),
1513 symbol_search_name (&s2->ginfo)) < 0;
1517 /* Allocate and partially fill a partial symtab. It will be
1518 completely filled at the end of the symbol list.
1520 FILENAME is the name of the symbol-file we are reading from. */
1522 struct partial_symtab *
1523 start_psymtab_common (struct objfile *objfile,
1524 const char *filename,
1527 struct partial_symtab *psymtab;
1529 psymtab = allocate_psymtab (filename, objfile);
1530 psymtab->set_text_low (textlow);
1531 psymtab->set_text_high (psymtab->raw_text_low ()); /* default */
1532 psymtab->globals_offset = objfile->partial_symtabs->global_psymbols.size ();
1533 psymtab->statics_offset = objfile->partial_symtabs->static_psymbols.size ();
1537 /* Perform "finishing up" operations of a partial symtab. */
1540 end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1542 pst->n_global_syms = (objfile->partial_symtabs->global_psymbols.size ()
1543 - pst->globals_offset);
1544 pst->n_static_syms = (objfile->partial_symtabs->static_psymbols.size ()
1545 - pst->statics_offset);
1547 sort_pst_symbols (objfile, pst);
1550 /* Calculate a hash code for the given partial symbol. The hash is
1551 calculated using the symbol's value, language, domain, class
1552 and name. These are the values which are set by
1553 add_psymbol_to_bcache. */
1555 static unsigned long
1556 psymbol_hash (const void *addr, int length)
1558 unsigned long h = 0;
1559 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1560 unsigned int lang = psymbol->ginfo.language;
1561 unsigned int domain = psymbol->domain;
1562 unsigned int theclass = psymbol->aclass;
1564 h = hash_continue (&psymbol->ginfo.value, sizeof (psymbol->ginfo.value), h);
1565 h = hash_continue (&lang, sizeof (unsigned int), h);
1566 h = hash_continue (&domain, sizeof (unsigned int), h);
1567 h = hash_continue (&theclass, sizeof (unsigned int), h);
1568 /* Note that psymbol names are interned via symbol_set_names, so
1569 there's no need to hash the contents of the name here. */
1570 h = hash_continue (&psymbol->ginfo.name,
1571 sizeof (psymbol->ginfo.name), h);
1576 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1577 For the comparison this function uses a symbols value,
1578 language, domain, class and name. */
1581 psymbol_compare (const void *addr1, const void *addr2, int length)
1583 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1584 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1586 return (memcmp (&sym1->ginfo.value, &sym2->ginfo.value,
1587 sizeof (sym1->ginfo.value)) == 0
1588 && sym1->ginfo.language == sym2->ginfo.language
1589 && sym1->domain == sym2->domain
1590 && sym1->aclass == sym2->aclass
1591 /* Note that psymbol names are interned via
1592 symbol_set_names, so there's no need to compare the
1593 contents of the name here. */
1594 && sym1->ginfo.name == sym2->ginfo.name);
1597 /* Initialize a partial symbol bcache. */
1599 struct psymbol_bcache *
1600 psymbol_bcache_init (void)
1602 struct psymbol_bcache *bcache = XCNEW (struct psymbol_bcache);
1604 bcache->bcache = bcache_xmalloc (psymbol_hash, psymbol_compare);
1608 /* Free a partial symbol bcache. */
1611 psymbol_bcache_free (struct psymbol_bcache *bcache)
1616 bcache_xfree (bcache->bcache);
1620 /* Return the internal bcache of the psymbol_bcache BCACHE. */
1623 psymbol_bcache_get_bcache (struct psymbol_bcache *bcache)
1625 return bcache->bcache;
1628 /* Find a copy of the SYM in BCACHE. If BCACHE has never seen this
1629 symbol before, add a copy to BCACHE. In either case, return a pointer
1630 to BCACHE's copy of the symbol. If optional ADDED is not NULL, return
1631 1 in case of new entry or 0 if returning an old entry. */
1633 static struct partial_symbol *
1634 psymbol_bcache_full (struct partial_symbol *sym,
1635 struct psymbol_bcache *bcache,
1638 return ((struct partial_symbol *)
1639 bcache_full (sym, sizeof (struct partial_symbol), bcache->bcache,
1643 /* Helper function, initialises partial symbol structure and stashes
1644 it into objfile's bcache. Note that our caching mechanism will
1645 use all fields of struct partial_symbol to determine hash value of the
1646 structure. In other words, having two symbols with the same name but
1647 different domain (or address) is possible and correct. */
1649 static struct partial_symbol *
1650 add_psymbol_to_bcache (const char *name, int namelength, int copy_name,
1652 enum address_class theclass,
1655 enum language language, struct objfile *objfile,
1658 struct partial_symbol psymbol;
1659 memset (&psymbol, 0, sizeof (psymbol));
1661 psymbol.set_unrelocated_address (coreaddr);
1662 psymbol.ginfo.section = section;
1663 psymbol.domain = domain;
1664 psymbol.aclass = theclass;
1665 symbol_set_language (&psymbol.ginfo, language,
1666 objfile->partial_symtabs->obstack ());
1667 symbol_set_names (&psymbol.ginfo, name, namelength, copy_name,
1670 /* Stash the partial symbol away in the cache. */
1671 return psymbol_bcache_full (&psymbol,
1672 objfile->partial_symtabs->psymbol_cache,
1676 /* Helper function, adds partial symbol to the given partial symbol list. */
1679 append_psymbol_to_list (std::vector<partial_symbol *> *list,
1680 struct partial_symbol *psym,
1681 struct objfile *objfile)
1683 list->push_back (psym);
1684 OBJSTAT (objfile, n_psyms++);
1687 /* Add a symbol with a long value to a psymtab.
1688 Since one arg is a struct, we pass in a ptr and deref it (sigh).
1689 The only value we need to store for psyms is an address.
1690 For all other psyms pass zero for COREADDR.
1691 Return the partial symbol that has been added. */
1694 add_psymbol_to_list (const char *name, int namelength, int copy_name,
1696 enum address_class theclass,
1698 psymbol_placement where,
1700 enum language language, struct objfile *objfile)
1702 struct partial_symbol *psym;
1706 /* Stash the partial symbol away in the cache. */
1707 psym = add_psymbol_to_bcache (name, namelength, copy_name, domain, theclass,
1708 section, coreaddr, language, objfile, &added);
1710 /* Do not duplicate global partial symbols. */
1711 if (where == psymbol_placement::GLOBAL && !added)
1714 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1715 std::vector<partial_symbol *> *list
1716 = (where == psymbol_placement::STATIC
1717 ? &objfile->partial_symtabs->static_psymbols
1718 : &objfile->partial_symtabs->global_psymbols);
1719 append_psymbol_to_list (list, psym, objfile);
1722 /* See psympriv.h. */
1725 init_psymbol_list (struct objfile *objfile, int total_symbols)
1727 if (objfile->partial_symtabs->global_psymbols.capacity () == 0
1728 && objfile->partial_symtabs->static_psymbols.capacity () == 0)
1730 /* Current best guess is that approximately a twentieth of the
1731 total symbols (in a debugging file) are global or static
1732 oriented symbols, then multiply that by slop factor of
1734 objfile->partial_symtabs->global_psymbols.reserve (total_symbols / 10);
1735 objfile->partial_symtabs->static_psymbols.reserve (total_symbols / 10);
1739 /* See psympriv.h. */
1741 struct partial_symtab *
1742 allocate_psymtab (const char *filename, struct objfile *objfile)
1744 struct partial_symtab *psymtab
1745 = objfile->partial_symtabs->allocate_psymtab ();
1748 = (const char *) bcache (filename, strlen (filename) + 1,
1749 objfile->per_bfd->filename_cache);
1750 psymtab->compunit_symtab = NULL;
1752 if (symtab_create_debug)
1754 /* Be a bit clever with debugging messages, and don't print objfile
1755 every time, only when it changes. */
1756 static char *last_objfile_name = NULL;
1758 if (last_objfile_name == NULL
1759 || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
1761 xfree (last_objfile_name);
1762 last_objfile_name = xstrdup (objfile_name (objfile));
1763 fprintf_filtered (gdb_stdlog,
1764 "Creating one or more psymtabs for objfile %s ...\n",
1767 fprintf_filtered (gdb_stdlog,
1768 "Created psymtab %s for module %s.\n",
1769 host_address_to_string (psymtab), filename);
1776 psymtab_storage::discard_psymtab (struct partial_symtab *pst)
1778 struct partial_symtab **prev_pst;
1781 Empty psymtabs happen as a result of header files which don't
1782 have any symbols in them. There can be a lot of them. But this
1783 check is wrong, in that a psymtab with N_SLINE entries but
1784 nothing else is not empty, but we don't realize that. Fixing
1785 that without slowing things down might be tricky. */
1787 /* First, snip it out of the psymtab chain. */
1789 prev_pst = &psymtabs;
1790 while ((*prev_pst) != pst)
1791 prev_pst = &((*prev_pst)->next);
1792 (*prev_pst) = pst->next;
1794 /* Next, put it on a free list for recycling. */
1796 pst->next = free_psymtabs;
1797 free_psymtabs = pst;
1802 /* We need to pass a couple of items to the addrmap_foreach function,
1805 struct dump_psymtab_addrmap_data
1807 struct objfile *objfile;
1808 struct partial_symtab *psymtab;
1809 struct ui_file *outfile;
1811 /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1812 If so, we want to print the next one as well (since the next addrmap
1813 entry defines the end of the range). */
1814 int previous_matched;
1817 /* Helper function for dump_psymtab_addrmap to print an addrmap entry. */
1820 dump_psymtab_addrmap_1 (void *datap, CORE_ADDR start_addr, void *obj)
1822 struct dump_psymtab_addrmap_data *data
1823 = (struct dump_psymtab_addrmap_data *) datap;
1824 struct gdbarch *gdbarch = get_objfile_arch (data->objfile);
1825 struct partial_symtab *addrmap_psymtab = (struct partial_symtab *) obj;
1826 const char *psymtab_address_or_end = NULL;
1830 if (data->psymtab == NULL
1831 || data->psymtab == addrmap_psymtab)
1832 psymtab_address_or_end = host_address_to_string (addrmap_psymtab);
1833 else if (data->previous_matched)
1834 psymtab_address_or_end = "<ends here>";
1836 if (data->psymtab == NULL
1837 || data->psymtab == addrmap_psymtab
1838 || data->previous_matched)
1840 fprintf_filtered (data->outfile, " %s%s %s\n",
1841 data->psymtab != NULL ? " " : "",
1842 paddress (gdbarch, start_addr),
1843 psymtab_address_or_end);
1846 data->previous_matched = (data->psymtab == NULL
1847 || data->psymtab == addrmap_psymtab);
1852 /* Helper function for maintenance_print_psymbols to print the addrmap
1853 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1856 dump_psymtab_addrmap (struct objfile *objfile, struct partial_symtab *psymtab,
1857 struct ui_file *outfile)
1859 struct dump_psymtab_addrmap_data addrmap_dump_data;
1861 if ((psymtab == NULL
1862 || psymtab->psymtabs_addrmap_supported)
1863 && objfile->partial_symtabs->psymtabs_addrmap != NULL)
1865 addrmap_dump_data.objfile = objfile;
1866 addrmap_dump_data.psymtab = psymtab;
1867 addrmap_dump_data.outfile = outfile;
1868 addrmap_dump_data.previous_matched = 0;
1869 fprintf_filtered (outfile, "%sddress map:\n",
1870 psymtab == NULL ? "Entire a" : " A");
1871 addrmap_foreach (objfile->partial_symtabs->psymtabs_addrmap,
1872 dump_psymtab_addrmap_1, &addrmap_dump_data);
1877 maintenance_print_psymbols (const char *args, int from_tty)
1879 struct ui_file *outfile = gdb_stdout;
1880 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1881 int i, outfile_idx, found;
1883 struct obj_section *section = NULL;
1887 gdb_argv argv (args);
1889 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1891 if (strcmp (argv[i], "-pc") == 0)
1893 if (argv[i + 1] == NULL)
1894 error (_("Missing pc value"));
1895 address_arg = argv[++i];
1897 else if (strcmp (argv[i], "-source") == 0)
1899 if (argv[i + 1] == NULL)
1900 error (_("Missing source file"));
1901 source_arg = argv[++i];
1903 else if (strcmp (argv[i], "-objfile") == 0)
1905 if (argv[i + 1] == NULL)
1906 error (_("Missing objfile name"));
1907 objfile_arg = argv[++i];
1909 else if (strcmp (argv[i], "--") == 0)
1911 /* End of options. */
1915 else if (argv[i][0] == '-')
1917 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1918 error (_("Unknown option: %s"), argv[i]);
1925 if (address_arg != NULL && source_arg != NULL)
1926 error (_("Must specify at most one of -pc and -source"));
1928 stdio_file arg_outfile;
1930 if (argv != NULL && argv[outfile_idx] != NULL)
1932 if (argv[outfile_idx + 1] != NULL)
1933 error (_("Junk at end of command"));
1934 gdb::unique_xmalloc_ptr<char> outfile_name
1935 (tilde_expand (argv[outfile_idx]));
1936 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1937 perror_with_name (outfile_name.get ());
1938 outfile = &arg_outfile;
1941 if (address_arg != NULL)
1943 pc = parse_and_eval_address (address_arg);
1944 /* If we fail to find a section, that's ok, try the lookup anyway. */
1945 section = find_pc_section (pc);
1949 for (objfile *objfile : current_program_space->objfiles ())
1951 int printed_objfile_header = 0;
1952 int print_for_objfile = 1;
1955 if (objfile_arg != NULL)
1957 = compare_filenames_for_search (objfile_name (objfile),
1959 if (!print_for_objfile)
1962 if (address_arg != NULL)
1964 struct bound_minimal_symbol msymbol = { NULL, NULL };
1966 /* We don't assume each pc has a unique objfile (this is for
1968 struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc,
1972 if (!printed_objfile_header)
1974 outfile->printf ("\nPartial symtabs for objfile %s\n",
1975 objfile_name (objfile));
1976 printed_objfile_header = 1;
1978 dump_psymtab (objfile, ps, outfile);
1979 dump_psymtab_addrmap (objfile, ps, outfile);
1985 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1987 int print_for_source = 0;
1990 if (source_arg != NULL)
1993 = compare_filenames_for_search (ps->filename, source_arg);
1996 if (source_arg == NULL
1997 || print_for_source)
1999 if (!printed_objfile_header)
2001 outfile->printf ("\nPartial symtabs for objfile %s\n",
2002 objfile_name (objfile));
2003 printed_objfile_header = 1;
2005 dump_psymtab (objfile, ps, outfile);
2006 dump_psymtab_addrmap (objfile, ps, outfile);
2011 /* If we're printing all the objfile's symbols dump the full addrmap. */
2013 if (address_arg == NULL
2014 && source_arg == NULL
2015 && objfile->partial_symtabs->psymtabs_addrmap != NULL)
2017 outfile->puts ("\n");
2018 dump_psymtab_addrmap (objfile, NULL, outfile);
2024 if (address_arg != NULL)
2025 error (_("No partial symtab for address: %s"), address_arg);
2026 if (source_arg != NULL)
2027 error (_("No partial symtab for source file: %s"), source_arg);
2031 /* List all the partial symbol tables whose names match REGEXP (optional). */
2034 maintenance_info_psymtabs (const char *regexp, int from_tty)
2036 struct program_space *pspace;
2041 ALL_PSPACES (pspace)
2042 for (objfile *objfile : pspace->objfiles ())
2044 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2046 /* We don't want to print anything for this objfile until we
2047 actually find a symtab whose name matches. */
2048 int printed_objfile_start = 0;
2050 for (partial_symtab *psymtab : require_partial_symbols (objfile, 1))
2055 || re_exec (psymtab->filename))
2057 if (! printed_objfile_start)
2059 printf_filtered ("{ objfile %s ", objfile_name (objfile));
2061 printf_filtered ("((struct objfile *) %s)\n",
2062 host_address_to_string (objfile));
2063 printed_objfile_start = 1;
2066 printf_filtered (" { psymtab %s ", psymtab->filename);
2068 printf_filtered ("((struct partial_symtab *) %s)\n",
2069 host_address_to_string (psymtab));
2071 printf_filtered (" readin %s\n",
2072 psymtab->readin ? "yes" : "no");
2073 printf_filtered (" fullname %s\n",
2075 ? psymtab->fullname : "(null)");
2076 printf_filtered (" text addresses ");
2077 fputs_filtered (paddress (gdbarch,
2078 psymtab->text_low (objfile)),
2080 printf_filtered (" -- ");
2081 fputs_filtered (paddress (gdbarch,
2082 psymtab->text_high (objfile)),
2084 printf_filtered ("\n");
2085 printf_filtered (" psymtabs_addrmap_supported %s\n",
2086 (psymtab->psymtabs_addrmap_supported
2088 printf_filtered (" globals ");
2089 if (psymtab->n_global_syms)
2091 auto p = &(objfile->partial_symtabs
2092 ->global_psymbols[psymtab->globals_offset]);
2095 ("(* (struct partial_symbol **) %s @ %d)\n",
2096 host_address_to_string (p),
2097 psymtab->n_global_syms);
2100 printf_filtered ("(none)\n");
2101 printf_filtered (" statics ");
2102 if (psymtab->n_static_syms)
2104 auto p = &(objfile->partial_symtabs
2105 ->static_psymbols[psymtab->statics_offset]);
2108 ("(* (struct partial_symbol **) %s @ %d)\n",
2109 host_address_to_string (p),
2110 psymtab->n_static_syms);
2113 printf_filtered ("(none)\n");
2114 printf_filtered (" dependencies ");
2115 if (psymtab->number_of_dependencies)
2119 printf_filtered ("{\n");
2120 for (i = 0; i < psymtab->number_of_dependencies; i++)
2122 struct partial_symtab *dep = psymtab->dependencies[i];
2124 /* Note the string concatenation there --- no
2126 printf_filtered (" psymtab %s "
2127 "((struct partial_symtab *) %s)\n",
2129 host_address_to_string (dep));
2131 printf_filtered (" }\n");
2134 printf_filtered ("(none)\n");
2135 printf_filtered (" }\n");
2139 if (printed_objfile_start)
2140 printf_filtered ("}\n");
2144 /* Check consistency of currently expanded psymtabs vs symtabs. */
2147 maintenance_check_psymtabs (const char *ignore, int from_tty)
2150 struct compunit_symtab *cust = NULL;
2151 const struct blockvector *bv;
2155 for (objfile *objfile : current_program_space->objfiles ())
2156 for (partial_symtab *ps : require_partial_symbols (objfile, 1))
2158 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2160 /* We don't call psymtab_to_symtab here because that may cause symtab
2161 expansion. When debugging a problem it helps if checkers leave
2162 things unchanged. */
2163 cust = ps->compunit_symtab;
2165 /* First do some checks that don't require the associated symtab. */
2166 if (ps->text_high (objfile) < ps->text_low (objfile))
2168 printf_filtered ("Psymtab ");
2169 puts_filtered (ps->filename);
2170 printf_filtered (" covers bad range ");
2171 fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2173 printf_filtered (" - ");
2174 fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2176 printf_filtered ("\n");
2180 /* Now do checks requiring the associated symtab. */
2183 bv = COMPUNIT_BLOCKVECTOR (cust);
2184 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
2185 partial_symbol **psym
2186 = &objfile->partial_symtabs->static_psymbols[ps->statics_offset];
2187 length = ps->n_static_syms;
2190 sym = block_lookup_symbol (b, symbol_search_name (&(*psym)->ginfo),
2191 symbol_name_match_type::SEARCH_NAME,
2195 printf_filtered ("Static symbol `");
2196 puts_filtered ((*psym)->ginfo.name);
2197 printf_filtered ("' only found in ");
2198 puts_filtered (ps->filename);
2199 printf_filtered (" psymtab\n");
2203 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2204 psym = &objfile->partial_symtabs->global_psymbols[ps->globals_offset];
2205 length = ps->n_global_syms;
2208 sym = block_lookup_symbol (b, symbol_search_name (&(*psym)->ginfo),
2209 symbol_name_match_type::SEARCH_NAME,
2213 printf_filtered ("Global symbol `");
2214 puts_filtered ((*psym)->ginfo.name);
2215 printf_filtered ("' only found in ");
2216 puts_filtered (ps->filename);
2217 printf_filtered (" psymtab\n");
2221 if (ps->raw_text_high () != 0
2222 && (ps->text_low (objfile) < BLOCK_START (b)
2223 || ps->text_high (objfile) > BLOCK_END (b)))
2225 printf_filtered ("Psymtab ");
2226 puts_filtered (ps->filename);
2227 printf_filtered (" covers ");
2228 fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2230 printf_filtered (" - ");
2231 fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2233 printf_filtered (" but symtab covers only ");
2234 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2235 printf_filtered (" - ");
2236 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2237 printf_filtered ("\n");
2243 _initialize_psymtab (void)
2245 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2246 Print dump of current partial symbol definitions.\n\
2247 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
2248 mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
2249 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2250 or the terminal if OUTFILE is unspecified.\n\
2251 If ADDRESS is provided, dump only the file for that address.\n\
2252 If SOURCE is provided, dump only that file's symbols.\n\
2253 If OBJFILE is provided, dump only that file's minimal symbols."),
2254 &maintenanceprintlist);
2256 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2257 List the partial symbol tables for all object files.\n\
2258 This does not include information about individual partial symbols,\n\
2259 just the symbol table structures themselves."),
2260 &maintenanceinfolist);
2262 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2264 Check consistency of currently expanded psymtabs versus symtabs."),