1 /* Partial symbol tables.
3 Copyright (C) 2009-2018 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);
68 /* Ensure that the partial symbols for OBJFILE have been loaded. This
69 function always returns its argument, as a convenience. */
72 require_partial_symbols (struct objfile *objfile, int verbose)
74 if ((objfile->flags & OBJF_PSYMTABS_READ) == 0)
76 objfile->flags |= OBJF_PSYMTABS_READ;
78 if (objfile->sf->sym_read_psymbols)
82 printf_filtered (_("Reading symbols from %s..."),
83 objfile_name (objfile));
84 gdb_flush (gdb_stdout);
86 (*objfile->sf->sym_read_psymbols) (objfile);
88 /* Partial symbols list are not expected to changed after this
90 objfile->global_psymbols.shrink_to_fit ();
91 objfile->static_psymbols.shrink_to_fit ();
95 if (!objfile_has_symbols (objfile))
98 printf_filtered (_("(no debugging symbols found)..."));
102 printf_filtered (_("done.\n"));
110 /* Traverse all psymtabs in one objfile, requiring that the psymtabs
113 #define ALL_OBJFILE_PSYMTABS_REQUIRED(objfile, p) \
114 for ((p) = require_partial_symbols (objfile, 1)->psymtabs; \
118 /* We want to make sure this file always requires psymtabs. */
120 #undef ALL_OBJFILE_PSYMTABS
122 /* Traverse all psymtabs in all objfiles. */
124 #define ALL_PSYMTABS(objfile, p) \
125 ALL_OBJFILES (objfile) \
126 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
128 /* Helper function for psym_map_symtabs_matching_filename that
129 expands the symtabs and calls the iterator. */
132 partial_map_expand_apply (struct objfile *objfile,
134 const char *real_path,
135 struct partial_symtab *pst,
136 gdb::function_view<bool (symtab *)> callback)
138 struct compunit_symtab *last_made = objfile->compunit_symtabs;
140 /* Shared psymtabs should never be seen here. Instead they should
141 be handled properly by the caller. */
142 gdb_assert (pst->user == NULL);
144 /* Don't visit already-expanded psymtabs. */
148 /* This may expand more than one symtab, and we want to iterate over
150 psymtab_to_symtab (objfile, pst);
152 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
153 last_made, callback);
156 /* Psymtab version of map_symtabs_matching_filename. See its definition in
157 the definition of quick_symbol_functions in symfile.h. */
160 psym_map_symtabs_matching_filename
161 (struct objfile *objfile,
163 const char *real_path,
164 gdb::function_view<bool (symtab *)> callback)
166 struct partial_symtab *pst;
167 const char *name_basename = lbasename (name);
169 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
171 /* We can skip shared psymtabs here, because any file name will be
172 attached to the unshared psymtab. */
173 if (pst->user != NULL)
176 /* Anonymous psymtabs don't have a file name. */
180 if (compare_filenames_for_search (pst->filename, name))
182 if (partial_map_expand_apply (objfile, name, real_path,
188 /* Before we invoke realpath, which can get expensive when many
189 files are involved, do a quick comparison of the basenames. */
190 if (! basenames_may_differ
191 && FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
194 if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
196 if (partial_map_expand_apply (objfile, name, real_path,
202 /* If the user gave us an absolute path, try to find the file in
203 this symtab and use its absolute path. */
204 if (real_path != NULL)
206 gdb_assert (IS_ABSOLUTE_PATH (real_path));
207 gdb_assert (IS_ABSOLUTE_PATH (name));
208 if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
210 if (partial_map_expand_apply (objfile, name, real_path,
221 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
222 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
224 static struct partial_symtab *
225 find_pc_sect_psymtab_closer (struct objfile *objfile,
226 CORE_ADDR pc, struct obj_section *section,
227 struct partial_symtab *pst,
228 struct bound_minimal_symbol msymbol)
230 struct partial_symtab *tpst;
231 struct partial_symtab *best_pst = pst;
232 CORE_ADDR best_addr = pst->text_low (objfile);
234 gdb_assert (!pst->psymtabs_addrmap_supported);
236 /* An objfile that has its functions reordered might have
237 many partial symbol tables containing the PC, but
238 we want the partial symbol table that contains the
239 function containing the PC. */
240 if (!(objfile->flags & OBJF_REORDERED)
241 && section == NULL) /* Can't validate section this way. */
244 if (msymbol.minsym == NULL)
247 /* The code range of partial symtabs sometimes overlap, so, in
248 the loop below, we need to check all partial symtabs and
249 find the one that fits better for the given PC address. We
250 select the partial symtab that contains a symbol whose
251 address is closest to the PC address. By closest we mean
252 that find_pc_sect_symbol returns the symbol with address
253 that is closest and still less than the given PC. */
254 for (tpst = pst; tpst != NULL; tpst = tpst->next)
256 if (pc >= tpst->text_low (objfile) && pc < tpst->text_high (objfile))
258 struct partial_symbol *p;
261 /* NOTE: This assumes that every psymbol has a
262 corresponding msymbol, which is not necessarily
263 true; the debug info might be much richer than the
264 object's symbol table. */
265 p = find_pc_sect_psymbol (objfile, tpst, pc, section);
267 && (p->address (objfile) == BMSYMBOL_VALUE_ADDRESS (msymbol)))
270 /* Also accept the textlow value of a psymtab as a
271 "symbol", to provide some support for partial
272 symbol tables with line information but no debug
273 symbols (e.g. those produced by an assembler). */
275 this_addr = p->address (objfile);
277 this_addr = tpst->text_low (objfile);
279 /* Check whether it is closer than our current
280 BEST_ADDR. Since this symbol address is
281 necessarily lower or equal to PC, the symbol closer
282 to PC is the symbol which address is the highest.
283 This way we return the psymtab which contains such
284 best match symbol. This can help in cases where the
285 symbol information/debuginfo is not complete, like
286 for instance on IRIX6 with gcc, where no debug info
287 is emitted for statics. (See also the nodebug.exp
289 if (this_addr > best_addr)
291 best_addr = this_addr;
299 /* Find which partial symtab contains PC and SECTION. Return NULL if
300 none. We return the psymtab that contains a symbol whose address
301 exactly matches PC, or, if we cannot find an exact match, the
302 psymtab that contains a symbol whose address is closest to PC. */
304 static struct partial_symtab *
305 find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
306 struct obj_section *section,
307 struct bound_minimal_symbol msymbol)
309 struct partial_symtab *pst;
310 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
311 SECT_OFF_TEXT (objfile));
313 /* Try just the PSYMTABS_ADDRMAP mapping first as it has better granularity
314 than the later used TEXTLOW/TEXTHIGH one. */
316 if (objfile->psymtabs_addrmap != NULL)
318 pst = ((struct partial_symtab *)
319 addrmap_find (objfile->psymtabs_addrmap, pc - baseaddr));
322 /* FIXME: addrmaps currently do not handle overlayed sections,
323 so fall back to the non-addrmap case if we're debugging
324 overlays and the addrmap returned the wrong section. */
325 if (overlay_debugging && msymbol.minsym != NULL && section != NULL)
327 struct partial_symbol *p;
329 /* NOTE: This assumes that every psymbol has a
330 corresponding msymbol, which is not necessarily
331 true; the debug info might be much richer than the
332 object's symbol table. */
333 p = find_pc_sect_psymbol (objfile, pst, pc, section);
335 || (p->address (objfile)
336 != BMSYMBOL_VALUE_ADDRESS (msymbol)))
340 /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
341 PSYMTABS_ADDRMAP we used has already the best 1-byte
342 granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
343 a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
352 /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
353 which still have no corresponding full SYMTABs read. But it is not
354 present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
357 /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
358 its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
359 debug info type in single OBJFILE. */
361 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
362 if (!pst->psymtabs_addrmap_supported
363 && pc >= pst->text_low (objfile) && pc < pst->text_high (objfile))
365 struct partial_symtab *best_pst;
367 best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
369 if (best_pst != NULL)
376 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
377 the definition of quick_symbol_functions in symfile.h. */
379 static struct compunit_symtab *
380 psym_find_pc_sect_compunit_symtab (struct objfile *objfile,
381 struct bound_minimal_symbol msymbol,
383 struct obj_section *section,
386 struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc, section,
390 if (warn_if_readin && ps->readin)
391 /* Might want to error() here (in case symtab is corrupt and
392 will cause a core dump), but maybe we can successfully
393 continue, so let's not. */
395 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
396 paddress (get_objfile_arch (objfile), pc));
397 psymtab_to_symtab (objfile, ps);
398 return ps->compunit_symtab;
403 /* Find which partial symbol within a psymtab matches PC and SECTION.
404 Return NULL if none. */
406 static struct partial_symbol *
407 find_pc_sect_psymbol (struct objfile *objfile,
408 struct partial_symtab *psymtab, CORE_ADDR pc,
409 struct obj_section *section)
411 struct partial_symbol *best = NULL;
413 const CORE_ADDR textlow = psymtab->text_low (objfile);
415 gdb_assert (psymtab != NULL);
417 /* Cope with programs that start at address 0. */
418 best_pc = (textlow != 0) ? textlow - 1 : 0;
420 /* Search the global symbols as well as the static symbols, so that
421 find_pc_partial_function doesn't use a minimal symbol and thus
422 cache a bad endaddr. */
423 for (int i = 0; i < psymtab->n_global_syms; i++)
425 partial_symbol *p = objfile->global_psymbols[psymtab->globals_offset + i];
427 if (p->domain == VAR_DOMAIN
428 && p->aclass == LOC_BLOCK
429 && pc >= p->address (objfile)
430 && (p->address (objfile) > best_pc
431 || (psymtab->text_low (objfile) == 0
432 && best_pc == 0 && p->address (objfile) == 0)))
434 if (section != NULL) /* Match on a specific section. */
436 if (!matching_obj_sections (p->obj_section (objfile),
440 best_pc = p->address (objfile);
445 for (int i = 0; i < psymtab->n_static_syms; i++)
447 partial_symbol *p = objfile->static_psymbols[psymtab->statics_offset + i];
449 if (p->domain == VAR_DOMAIN
450 && p->aclass == LOC_BLOCK
451 && pc >= p->address (objfile)
452 && (p->address (objfile) > best_pc
453 || (psymtab->text_low (objfile) == 0
454 && best_pc == 0 && p->address (objfile) == 0)))
456 if (section != NULL) /* Match on a specific section. */
458 if (!matching_obj_sections (p->obj_section (objfile),
462 best_pc = p->address (objfile);
470 /* Psymtab version of lookup_symbol. See its definition in
471 the definition of quick_symbol_functions in symfile.h. */
473 static struct compunit_symtab *
474 psym_lookup_symbol (struct objfile *objfile,
475 int block_index, const char *name,
476 const domain_enum domain)
478 struct partial_symtab *ps;
479 const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
480 struct compunit_symtab *stab_best = NULL;
482 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
484 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
486 if (!ps->readin && lookup_partial_symbol (objfile, ps, name,
487 psymtab_index, domain))
489 struct symbol *sym, *with_opaque = NULL;
490 struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
491 /* Note: While psymtab_to_symtab can return NULL if the partial symtab
492 is empty, we can assume it won't here because lookup_partial_symbol
494 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
495 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
497 sym = block_find_symbol (block, name, domain,
498 block_find_non_opaque_type_preferred,
501 /* Some caution must be observed with overloaded functions
502 and methods, since the index will not contain any overload
503 information (but NAME might contain it). */
506 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
508 if (with_opaque != NULL
509 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
512 /* Keep looking through other psymtabs. */
519 /* Returns true if PSYM matches LOOKUP_NAME. */
522 psymbol_name_matches (partial_symbol *psym,
523 const lookup_name_info &lookup_name)
525 const language_defn *lang = language_def (psym->language);
526 symbol_name_matcher_ftype *name_match
527 = get_symbol_name_matcher (lang, lookup_name);
528 return name_match (symbol_search_name (psym), lookup_name, NULL);
531 /* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
532 the global block of PST if GLOBAL, and otherwise the static block.
533 MATCH is the comparison operation that returns true iff MATCH (s,
534 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
535 non-null, the symbols in the block are assumed to be ordered
536 according to it (allowing binary search). It must be compatible
537 with MATCH. Returns the symbol, if found, and otherwise NULL. */
539 static struct partial_symbol *
540 match_partial_symbol (struct objfile *objfile,
541 struct partial_symtab *pst, int global,
542 const char *name, domain_enum domain,
543 symbol_name_match_type match_type,
544 symbol_compare_ftype *ordered_compare)
546 struct partial_symbol **start, **psym;
547 struct partial_symbol **top, **real_top, **bottom, **center;
548 int length = (global ? pst->n_global_syms : pst->n_static_syms);
549 int do_linear_search = 1;
554 lookup_name_info lookup_name (name, match_type);
557 &objfile->global_psymbols[pst->globals_offset] :
558 &objfile->static_psymbols[pst->statics_offset]);
560 if (global && ordered_compare) /* Can use a binary search. */
562 do_linear_search = 0;
564 /* Binary search. This search is guaranteed to end with center
565 pointing at the earliest partial symbol whose name might be
566 correct. At that point *all* partial symbols with an
567 appropriate name will be checked against the correct
571 top = start + length - 1;
575 center = bottom + (top - bottom) / 2;
576 gdb_assert (center < top);
578 enum language lang = (*center)->language;
580 = lookup_name.language_lookup_name (lang).c_str ();
582 if (ordered_compare (symbol_search_name (*center), lang_ln) >= 0)
587 gdb_assert (top == bottom);
589 while (top <= real_top
590 && psymbol_name_matches (*top, lookup_name))
592 if (symbol_matches_domain ((*top)->language,
593 (*top)->domain, domain))
599 /* Can't use a binary search or else we found during the binary search that
600 we should also do a linear search. */
602 if (do_linear_search)
604 for (psym = start; psym < start + length; psym++)
606 if (symbol_matches_domain ((*psym)->language,
607 (*psym)->domain, domain)
608 && psymbol_name_matches (*psym, lookup_name))
616 /* Returns the name used to search psymtabs. Unlike symtabs, psymtabs do
617 not contain any method/function instance information (since this would
618 force reading type information while reading psymtabs). Therefore,
619 if NAME contains overload information, it must be stripped before searching
622 static gdb::unique_xmalloc_ptr<char>
623 psymtab_search_name (const char *name)
625 switch (current_language->la_language)
629 if (strchr (name, '('))
631 gdb::unique_xmalloc_ptr<char> ret = cp_remove_params (name);
643 return gdb::unique_xmalloc_ptr<char> (xstrdup (name));
646 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
647 Check the global symbols if GLOBAL, the static symbols if not. */
649 static struct partial_symbol *
650 lookup_partial_symbol (struct objfile *objfile,
651 struct partial_symtab *pst, const char *name,
652 int global, domain_enum domain)
654 struct partial_symbol **start, **psym;
655 struct partial_symbol **top, **real_top, **bottom, **center;
656 int length = (global ? pst->n_global_syms : pst->n_static_syms);
657 int do_linear_search = 1;
662 gdb::unique_xmalloc_ptr<char> search_name = psymtab_search_name (name);
664 lookup_name_info lookup_name (search_name.get (), symbol_name_match_type::FULL);
667 &objfile->global_psymbols[pst->globals_offset] :
668 &objfile->static_psymbols[pst->statics_offset]);
670 if (global) /* This means we can use a binary search. */
672 do_linear_search = 0;
674 /* Binary search. This search is guaranteed to end with center
675 pointing at the earliest partial symbol whose name might be
676 correct. At that point *all* partial symbols with an
677 appropriate name will be checked against the correct
681 top = start + length - 1;
685 center = bottom + (top - bottom) / 2;
687 internal_error (__FILE__, __LINE__,
688 _("failed internal consistency check"));
689 if (strcmp_iw_ordered (symbol_search_name (*center),
690 search_name.get ()) >= 0)
699 if (!(top == bottom))
700 internal_error (__FILE__, __LINE__,
701 _("failed internal consistency check"));
703 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
704 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
705 while (top >= start && symbol_matches_search_name (*top, lookup_name))
708 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
711 while (top <= real_top && symbol_matches_search_name (*top, lookup_name))
713 if (symbol_matches_domain ((*top)->language,
714 (*top)->domain, domain))
720 /* Can't use a binary search or else we found during the binary search that
721 we should also do a linear search. */
723 if (do_linear_search)
725 for (psym = start; psym < start + length; psym++)
727 if (symbol_matches_domain ((*psym)->language,
728 (*psym)->domain, domain)
729 && symbol_matches_search_name (*psym, lookup_name))
737 /* Get the symbol table that corresponds to a partial_symtab.
738 This is fast after the first time you do it.
739 The result will be NULL if the primary symtab has no symbols,
740 which can happen. Otherwise the result is the primary symtab
741 that contains PST. */
743 static struct compunit_symtab *
744 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
746 /* If it is a shared psymtab, find an unshared psymtab that includes
747 it. Any such psymtab will do. */
748 while (pst->user != NULL)
751 /* If it's been looked up before, return it. */
752 if (pst->compunit_symtab)
753 return pst->compunit_symtab;
755 /* If it has not yet been read in, read it. */
758 scoped_restore decrementer = increment_reading_symtab ();
760 (*pst->read_symtab) (pst, objfile);
763 return pst->compunit_symtab;
766 /* Psymtab version of find_last_source_symtab. See its definition in
767 the definition of quick_symbol_functions in symfile.h. */
769 static struct symtab *
770 psym_find_last_source_symtab (struct objfile *ofp)
772 struct partial_symtab *ps;
773 struct partial_symtab *cs_pst = NULL;
775 ALL_OBJFILE_PSYMTABS_REQUIRED (ofp, ps)
777 const char *name = ps->filename;
778 int len = strlen (name);
780 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
781 || strcmp (name, "<<C++-namespaces>>") == 0)))
789 internal_error (__FILE__, __LINE__,
790 _("select_source_symtab: "
791 "readin pst found and no symtabs."));
795 struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
799 return compunit_primary_filetab (cust);
805 /* Psymtab version of forget_cached_source_info. See its definition in
806 the definition of quick_symbol_functions in symfile.h. */
809 psym_forget_cached_source_info (struct objfile *objfile)
811 struct partial_symtab *pst;
813 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
815 if (pst->fullname != NULL)
817 xfree (pst->fullname);
818 pst->fullname = NULL;
824 print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
825 struct partial_symbol **p, int count, const char *what,
826 struct ui_file *outfile)
828 fprintf_filtered (outfile, " %s partial symbols:\n", what);
832 fprintf_filtered (outfile, " `%s'", (*p)->name);
833 if (symbol_demangled_name (*p) != NULL)
835 fprintf_filtered (outfile, " `%s'", symbol_demangled_name (*p));
837 fputs_filtered (", ", outfile);
838 switch ((*p)->domain)
841 fputs_filtered ("undefined domain, ", outfile);
844 /* This is the usual thing -- don't print it. */
847 fputs_filtered ("struct domain, ", outfile);
850 fputs_filtered ("label domain, ", outfile);
853 fputs_filtered ("<invalid domain>, ", outfile);
856 switch ((*p)->aclass)
859 fputs_filtered ("undefined", outfile);
862 fputs_filtered ("constant int", outfile);
865 fputs_filtered ("static", outfile);
868 fputs_filtered ("register", outfile);
871 fputs_filtered ("pass by value", outfile);
874 fputs_filtered ("pass by reference", outfile);
876 case LOC_REGPARM_ADDR:
877 fputs_filtered ("register address parameter", outfile);
880 fputs_filtered ("stack parameter", outfile);
883 fputs_filtered ("type", outfile);
886 fputs_filtered ("label", outfile);
889 fputs_filtered ("function", outfile);
891 case LOC_CONST_BYTES:
892 fputs_filtered ("constant bytes", outfile);
895 fputs_filtered ("unresolved", outfile);
897 case LOC_OPTIMIZED_OUT:
898 fputs_filtered ("optimized out", outfile);
901 fputs_filtered ("computed at runtime", outfile);
904 fputs_filtered ("<invalid location>", outfile);
907 fputs_filtered (", ", outfile);
908 fputs_filtered (paddress (gdbarch, (*p)->unrelocated_address ()), outfile);
909 fprintf_filtered (outfile, "\n");
915 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
916 struct ui_file *outfile)
918 struct gdbarch *gdbarch = get_objfile_arch (objfile);
921 if (psymtab->anonymous)
923 fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
928 fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
931 fprintf_filtered (outfile, "(object ");
932 gdb_print_host_address (psymtab, outfile);
933 fprintf_filtered (outfile, ")\n\n");
934 fprintf_filtered (outfile, " Read from object file %s (",
935 objfile_name (objfile));
936 gdb_print_host_address (objfile, outfile);
937 fprintf_filtered (outfile, ")\n");
941 fprintf_filtered (outfile,
942 " Full symtab was read (at ");
943 gdb_print_host_address (psymtab->compunit_symtab, outfile);
944 fprintf_filtered (outfile, " by function at ");
945 gdb_print_host_address (psymtab->read_symtab, outfile);
946 fprintf_filtered (outfile, ")\n");
949 fprintf_filtered (outfile, " Symbols cover text addresses ");
950 fputs_filtered (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
951 fprintf_filtered (outfile, "-");
952 fputs_filtered (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
953 fprintf_filtered (outfile, "\n");
954 fprintf_filtered (outfile, " Address map supported - %s.\n",
955 psymtab->psymtabs_addrmap_supported ? "yes" : "no");
956 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
957 psymtab->number_of_dependencies);
958 for (i = 0; i < psymtab->number_of_dependencies; i++)
960 fprintf_filtered (outfile, " %d ", i);
961 gdb_print_host_address (psymtab->dependencies[i], outfile);
962 fprintf_filtered (outfile, " %s\n",
963 psymtab->dependencies[i]->filename);
965 if (psymtab->user != NULL)
967 fprintf_filtered (outfile, " Shared partial symtab with user ");
968 gdb_print_host_address (psymtab->user, outfile);
969 fprintf_filtered (outfile, "\n");
971 if (psymtab->n_global_syms > 0)
973 print_partial_symbols (gdbarch, objfile,
974 &objfile->global_psymbols[psymtab->globals_offset],
975 psymtab->n_global_syms, "Global", outfile);
977 if (psymtab->n_static_syms > 0)
979 print_partial_symbols (gdbarch, objfile,
980 &objfile->static_psymbols[psymtab->statics_offset],
981 psymtab->n_static_syms, "Static", outfile);
983 fprintf_filtered (outfile, "\n");
986 /* Psymtab version of print_stats. See its definition in
987 the definition of quick_symbol_functions in symfile.h. */
990 psym_print_stats (struct objfile *objfile)
993 struct partial_symtab *ps;
996 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1001 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i);
1004 /* Psymtab version of dump. See its definition in
1005 the definition of quick_symbol_functions in symfile.h. */
1008 psym_dump (struct objfile *objfile)
1010 struct partial_symtab *psymtab;
1012 if (objfile->psymtabs)
1014 printf_filtered ("Psymtabs:\n");
1015 for (psymtab = objfile->psymtabs;
1017 psymtab = psymtab->next)
1019 printf_filtered ("%s at ",
1021 gdb_print_host_address (psymtab, gdb_stdout);
1022 printf_filtered (", ");
1025 printf_filtered ("\n\n");
1029 /* Psymtab version of expand_symtabs_for_function. See its definition in
1030 the definition of quick_symbol_functions in symfile.h. */
1033 psym_expand_symtabs_for_function (struct objfile *objfile,
1034 const char *func_name)
1036 struct partial_symtab *ps;
1038 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1043 if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
1045 || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
1047 psymtab_to_symtab (objfile, ps);
1051 /* Psymtab version of expand_all_symtabs. See its definition in
1052 the definition of quick_symbol_functions in symfile.h. */
1055 psym_expand_all_symtabs (struct objfile *objfile)
1057 struct partial_symtab *psymtab;
1059 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
1061 psymtab_to_symtab (objfile, psymtab);
1065 /* Psymtab version of expand_symtabs_with_fullname. See its definition in
1066 the definition of quick_symbol_functions in symfile.h. */
1069 psym_expand_symtabs_with_fullname (struct objfile *objfile,
1070 const char *fullname)
1072 struct partial_symtab *p;
1074 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
1076 /* Anonymous psymtabs don't have a name of a source file. */
1080 /* psymtab_to_fullname tries to open the file which is slow.
1081 Don't call it if we know the basenames don't match. */
1082 if ((basenames_may_differ
1083 || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1084 && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
1085 psymtab_to_symtab (objfile, p);
1089 /* Psymtab version of map_symbol_filenames. See its definition in
1090 the definition of quick_symbol_functions in symfile.h. */
1093 psym_map_symbol_filenames (struct objfile *objfile,
1094 symbol_filename_ftype *fun, void *data,
1097 struct partial_symtab *ps;
1099 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1101 const char *fullname;
1106 /* We can skip shared psymtabs here, because any file name will be
1107 attached to the unshared psymtab. */
1108 if (ps->user != NULL)
1111 /* Anonymous psymtabs don't have a file name. */
1117 fullname = psymtab_to_fullname (ps);
1120 (*fun) (ps->filename, fullname, data);
1124 /* Finds the fullname that a partial_symtab represents.
1126 If this functions finds the fullname, it will save it in ps->fullname
1127 and it will also return the value.
1129 If this function fails to find the file that this partial_symtab represents,
1130 NULL will be returned and ps->fullname will be set to NULL. */
1133 psymtab_to_fullname (struct partial_symtab *ps)
1135 gdb_assert (!ps->anonymous);
1137 /* Use cached copy if we have it.
1138 We rely on forget_cached_source_info being called appropriately
1139 to handle cases like the file being moved. */
1140 if (ps->fullname == NULL)
1142 gdb::unique_xmalloc_ptr<char> fullname;
1143 int fd = find_and_open_source (ps->filename, ps->dirname, &fullname);
1144 ps->fullname = fullname.release ();
1150 /* rewrite_source_path would be applied by find_and_open_source, we
1151 should report the pathname where GDB tried to find the file. */
1153 if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
1154 fullname.reset (xstrdup (ps->filename));
1156 fullname.reset (concat (ps->dirname, SLASH_STRING,
1157 ps->filename, (char *) NULL));
1159 ps->fullname = rewrite_source_path (fullname.get ()).release ();
1160 if (ps->fullname == NULL)
1161 ps->fullname = fullname.release ();
1165 return ps->fullname;
1168 /* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1169 according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1170 BLOCK is assumed to come from OBJFILE. Returns 1 iff CALLBACK
1171 ever returns non-zero, and otherwise returns 0. */
1174 map_block (const char *name, domain_enum domain, struct objfile *objfile,
1175 struct block *block,
1176 int (*callback) (struct block *, struct symbol *, void *),
1177 void *data, symbol_name_match_type match)
1179 struct block_iterator iter;
1182 lookup_name_info lookup_name (name, match);
1184 for (sym = block_iter_match_first (block, lookup_name, &iter);
1186 sym = block_iter_match_next (lookup_name, &iter))
1188 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
1189 SYMBOL_DOMAIN (sym), domain))
1191 if (callback (block, sym, data))
1199 /* Psymtab version of map_matching_symbols. See its definition in
1200 the definition of quick_symbol_functions in symfile.h. */
1203 psym_map_matching_symbols (struct objfile *objfile,
1204 const char *name, domain_enum domain,
1206 int (*callback) (struct block *,
1207 struct symbol *, void *),
1209 symbol_name_match_type match,
1210 symbol_compare_ftype *ordered_compare)
1212 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
1213 struct partial_symtab *ps;
1215 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
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, struct objfile *objfile, enum search_domain domain,
1245 const lookup_name_info &lookup_name,
1246 gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
1249 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
1252 if (ps->searched_flag != PST_NOT_SEARCHED)
1253 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1255 /* Recurse into shared psymtabs first, because they may have already
1256 been searched, and this could save some time. */
1257 for (i = 0; i < ps->number_of_dependencies; ++i)
1261 /* Skip non-shared dependencies, these are handled elsewhere. */
1262 if (ps->dependencies[i]->user == NULL)
1265 r = recursively_search_psymtabs (ps->dependencies[i],
1266 objfile, domain, lookup_name,
1270 ps->searched_flag = PST_SEARCHED_AND_FOUND;
1275 partial_symbol **gbound
1276 = objfile->global_psymbols.data () + ps->globals_offset + ps->n_global_syms;
1277 partial_symbol **sbound
1278 = objfile->static_psymbols.data () + ps->statics_offset + ps->n_static_syms;
1279 partial_symbol **bound = gbound;
1281 /* Go through all of the symbols stored in a partial
1282 symtab in one loop. */
1283 partial_symbol **psym = objfile->global_psymbols.data () + ps->globals_offset;
1288 if (bound == gbound && ps->n_static_syms != 0)
1290 psym = objfile->static_psymbols.data () + ps->statics_offset;
1301 if ((domain == ALL_DOMAIN
1302 || (domain == VARIABLES_DOMAIN
1303 && (*psym)->aclass != LOC_TYPEDEF
1304 && (*psym)->aclass != LOC_BLOCK)
1305 || (domain == FUNCTIONS_DOMAIN
1306 && (*psym)->aclass == LOC_BLOCK)
1307 || (domain == TYPES_DOMAIN
1308 && (*psym)->aclass == LOC_TYPEDEF))
1309 && psymbol_name_matches (*psym, lookup_name)
1310 && (sym_matcher == NULL || sym_matcher (symbol_search_name (*psym))))
1312 /* Found a match, so notify our caller. */
1313 result = PST_SEARCHED_AND_FOUND;
1320 ps->searched_flag = result;
1321 return result == PST_SEARCHED_AND_FOUND;
1324 /* Psymtab version of expand_symtabs_matching. See its definition in
1325 the definition of quick_symbol_functions in symfile.h. */
1328 psym_expand_symtabs_matching
1329 (struct objfile *objfile,
1330 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1331 const lookup_name_info &lookup_name_in,
1332 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1333 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1334 enum search_domain domain)
1336 struct partial_symtab *ps;
1338 lookup_name_info lookup_name = lookup_name_in.make_ignore_params ();
1340 /* Clear the search flags. */
1341 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1343 ps->searched_flag = PST_NOT_SEARCHED;
1346 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1353 /* We skip shared psymtabs because file-matching doesn't apply
1354 to them; but we search them later in the loop. */
1355 if (ps->user != NULL)
1365 match = file_matcher (ps->filename, false);
1368 /* Before we invoke realpath, which can get expensive when many
1369 files are involved, do a quick comparison of the basenames. */
1370 if (basenames_may_differ
1371 || file_matcher (lbasename (ps->filename), true))
1372 match = file_matcher (psymtab_to_fullname (ps), false);
1378 if (recursively_search_psymtabs (ps, objfile, domain,
1379 lookup_name, symbol_matcher))
1381 struct compunit_symtab *symtab =
1382 psymtab_to_symtab (objfile, ps);
1384 if (expansion_notify != NULL)
1385 expansion_notify (symtab);
1390 /* Psymtab version of has_symbols. See its definition in
1391 the definition of quick_symbol_functions in symfile.h. */
1394 psym_has_symbols (struct objfile *objfile)
1396 return objfile->psymtabs != NULL;
1399 /* Helper function for psym_find_compunit_symtab_by_address that fills
1400 in psymbol_map for a given range of psymbols. */
1403 psym_fill_psymbol_map (struct objfile *objfile,
1404 struct partial_symtab *psymtab,
1405 std::set<CORE_ADDR> *seen_addrs,
1406 const std::vector<partial_symbol *> &symbols,
1410 for (int i = 0; i < length; ++i)
1412 struct partial_symbol *psym = symbols[start + i];
1414 if (psym->aclass == LOC_STATIC)
1416 CORE_ADDR addr = psym->address (objfile);
1417 if (seen_addrs->find (addr) == seen_addrs->end ())
1419 seen_addrs->insert (addr);
1420 objfile->psymbol_map.emplace_back (addr, psymtab);
1426 /* See find_compunit_symtab_by_address in quick_symbol_functions, in
1429 static compunit_symtab *
1430 psym_find_compunit_symtab_by_address (struct objfile *objfile,
1433 if (objfile->psymbol_map.empty ())
1435 struct partial_symtab *pst;
1437 std::set<CORE_ADDR> seen_addrs;
1439 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
1441 psym_fill_psymbol_map (objfile, pst,
1443 objfile->global_psymbols,
1444 pst->globals_offset,
1445 pst->n_global_syms);
1446 psym_fill_psymbol_map (objfile, pst,
1448 objfile->static_psymbols,
1449 pst->statics_offset,
1450 pst->n_static_syms);
1453 objfile->psymbol_map.shrink_to_fit ();
1455 std::sort (objfile->psymbol_map.begin (), objfile->psymbol_map.end (),
1456 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1457 const std::pair<CORE_ADDR, partial_symtab *> &b)
1459 return a.first < b.first;
1463 auto iter = std::lower_bound
1464 (objfile->psymbol_map.begin (), objfile->psymbol_map.end (), address,
1465 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1471 if (iter == objfile->psymbol_map.end () || iter->first != address)
1474 return psymtab_to_symtab (objfile, iter->second);
1477 const struct quick_symbol_functions psym_functions =
1480 psym_find_last_source_symtab,
1481 psym_forget_cached_source_info,
1482 psym_map_symtabs_matching_filename,
1486 psym_expand_symtabs_for_function,
1487 psym_expand_all_symtabs,
1488 psym_expand_symtabs_with_fullname,
1489 psym_map_matching_symbols,
1490 psym_expand_symtabs_matching,
1491 psym_find_pc_sect_compunit_symtab,
1492 psym_find_compunit_symtab_by_address,
1493 psym_map_symbol_filenames
1499 sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
1501 /* Sort the global list; don't sort the static list. */
1502 auto begin = objfile->global_psymbols.begin ();
1503 std::advance (begin, pst->globals_offset);
1505 /* The psymbols for this partial_symtab are currently at the end of the
1507 auto end = objfile->global_psymbols.end ();
1509 std::sort (begin, end, [] (partial_symbol *s1, partial_symbol *s2)
1511 return strcmp_iw_ordered (symbol_search_name (s1),
1512 symbol_search_name (s2)) < 0;
1516 /* Allocate and partially fill a partial symtab. It will be
1517 completely filled at the end of the symbol list.
1519 FILENAME is the name of the symbol-file we are reading from. */
1521 struct partial_symtab *
1522 start_psymtab_common (struct objfile *objfile,
1523 const char *filename,
1525 std::vector<partial_symbol *> &global_psymbols,
1526 std::vector<partial_symbol *> &static_psymbols)
1528 struct partial_symtab *psymtab;
1530 psymtab = allocate_psymtab (filename, objfile);
1531 psymtab->set_text_low (textlow);
1532 psymtab->set_text_high (psymtab->raw_text_low ()); /* default */
1533 psymtab->globals_offset = global_psymbols.size ();
1534 psymtab->statics_offset = static_psymbols.size ();
1538 /* Perform "finishing up" operations of a partial symtab. */
1541 end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1543 pst->n_global_syms = objfile->global_psymbols.size () - pst->globals_offset;
1544 pst->n_static_syms = objfile->static_psymbols.size () - pst->statics_offset;
1546 sort_pst_symbols (objfile, pst);
1549 /* Calculate a hash code for the given partial symbol. The hash is
1550 calculated using the symbol's value, language, domain, class
1551 and name. These are the values which are set by
1552 add_psymbol_to_bcache. */
1554 static unsigned long
1555 psymbol_hash (const void *addr, int length)
1557 unsigned long h = 0;
1558 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1559 unsigned int lang = psymbol->language;
1560 unsigned int domain = psymbol->domain;
1561 unsigned int theclass = psymbol->aclass;
1563 h = hash_continue (&psymbol->value, sizeof (psymbol->value), h);
1564 h = hash_continue (&lang, sizeof (unsigned int), h);
1565 h = hash_continue (&domain, sizeof (unsigned int), h);
1566 h = hash_continue (&theclass, sizeof (unsigned int), h);
1567 /* Note that psymbol names are interned via symbol_set_names, so
1568 there's no need to hash the contents of the name here. */
1569 h = hash_continue (&psymbol->name,
1570 sizeof (psymbol->name), h);
1575 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1576 For the comparison this function uses a symbols value,
1577 language, domain, class and name. */
1580 psymbol_compare (const void *addr1, const void *addr2, int length)
1582 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1583 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1585 return (memcmp (&sym1->value, &sym2->value,
1586 sizeof (sym1->value)) == 0
1587 && sym1->language == sym2->language
1588 && sym1->domain == sym2->domain
1589 && sym1->aclass == sym2->aclass
1590 /* Note that psymbol names are interned via
1591 symbol_set_names, so there's no need to compare the
1592 contents of the name here. */
1593 && sym1->name == sym2->name);
1596 /* Initialize a partial symbol bcache. */
1598 struct psymbol_bcache *
1599 psymbol_bcache_init (void)
1601 struct psymbol_bcache *bcache = XCNEW (struct psymbol_bcache);
1603 bcache->bcache = bcache_xmalloc (psymbol_hash, psymbol_compare);
1607 /* Free a partial symbol bcache. */
1610 psymbol_bcache_free (struct psymbol_bcache *bcache)
1615 bcache_xfree (bcache->bcache);
1619 /* Return the internal bcache of the psymbol_bcache BCACHE. */
1622 psymbol_bcache_get_bcache (struct psymbol_bcache *bcache)
1624 return bcache->bcache;
1627 /* Find a copy of the SYM in BCACHE. If BCACHE has never seen this
1628 symbol before, add a copy to BCACHE. In either case, return a pointer
1629 to BCACHE's copy of the symbol. If optional ADDED is not NULL, return
1630 1 in case of new entry or 0 if returning an old entry. */
1632 static struct partial_symbol *
1633 psymbol_bcache_full (struct partial_symbol *sym,
1634 struct psymbol_bcache *bcache,
1637 return ((struct partial_symbol *)
1638 bcache_full (sym, sizeof (struct partial_symbol), bcache->bcache,
1642 /* Helper function, initialises partial symbol structure and stashes
1643 it into objfile's bcache. Note that our caching mechanism will
1644 use all fields of struct partial_symbol to determine hash value of the
1645 structure. In other words, having two symbols with the same name but
1646 different domain (or address) is possible and correct. */
1648 static struct partial_symbol *
1649 add_psymbol_to_bcache (const char *name, int namelength, int copy_name,
1651 enum address_class theclass,
1654 enum language language, struct objfile *objfile,
1657 struct partial_symbol psymbol;
1659 psymbol.set_unrelocated_address (coreaddr);
1660 psymbol.section = section;
1661 psymbol.domain = domain;
1662 psymbol.aclass = theclass;
1664 memset (&psymbol.language_specific, 0, sizeof (psymbol.language_specific));
1665 psymbol.ada_mangled = 0;
1666 symbol_set_language (&psymbol, language, &objfile->objfile_obstack);
1667 symbol_set_names (&psymbol, name, namelength, copy_name, objfile);
1669 /* Stash the partial symbol away in the cache. */
1670 return psymbol_bcache_full (&psymbol, objfile->psymbol_cache, added);
1673 /* Helper function, adds partial symbol to the given partial symbol list. */
1676 append_psymbol_to_list (std::vector<partial_symbol *> *list,
1677 struct partial_symbol *psym,
1678 struct objfile *objfile)
1680 list->push_back (psym);
1681 OBJSTAT (objfile, n_psyms++);
1684 /* Add a symbol with a long value to a psymtab.
1685 Since one arg is a struct, we pass in a ptr and deref it (sigh).
1686 The only value we need to store for psyms is an address.
1687 For all other psyms pass zero for COREADDR.
1688 Return the partial symbol that has been added. */
1691 add_psymbol_to_list (const char *name, int namelength, int copy_name,
1693 enum address_class theclass,
1695 std::vector<partial_symbol *> *list,
1697 enum language language, struct objfile *objfile)
1699 struct partial_symbol *psym;
1703 /* Stash the partial symbol away in the cache. */
1704 psym = add_psymbol_to_bcache (name, namelength, copy_name, domain, theclass,
1705 section, coreaddr, language, objfile, &added);
1707 /* Do not duplicate global partial symbols. */
1708 if (list == &objfile->global_psymbols
1712 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1713 append_psymbol_to_list (list, psym, objfile);
1716 /* Initialize storage for partial symbols. */
1719 init_psymbol_list (struct objfile *objfile, int total_symbols)
1721 /* Free any previously allocated psymbol lists. */
1722 objfile->global_psymbols.clear ();
1723 objfile->static_psymbols.clear ();
1725 /* Current best guess is that approximately a twentieth
1726 of the total symbols (in a debugging file) are global or static
1727 oriented symbols, then multiply that by slop factor of two. */
1728 objfile->global_psymbols.reserve (total_symbols / 10);
1729 objfile->static_psymbols.reserve (total_symbols / 10);
1732 struct partial_symtab *
1733 allocate_psymtab (const char *filename, struct objfile *objfile)
1735 struct partial_symtab *psymtab;
1737 if (objfile->free_psymtabs)
1739 psymtab = objfile->free_psymtabs;
1740 objfile->free_psymtabs = psymtab->next;
1743 psymtab = XOBNEW (&objfile->objfile_obstack, partial_symtab);
1745 memset (psymtab, 0, sizeof (struct partial_symtab));
1747 = (const char *) bcache (filename, strlen (filename) + 1,
1748 objfile->per_bfd->filename_cache);
1749 psymtab->compunit_symtab = NULL;
1751 /* Prepend it to the psymtab list for the objfile it belongs to.
1752 Psymtabs are searched in most recent inserted -> least recent
1755 psymtab->next = objfile->psymtabs;
1756 objfile->psymtabs = psymtab;
1758 if (symtab_create_debug)
1760 /* Be a bit clever with debugging messages, and don't print objfile
1761 every time, only when it changes. */
1762 static char *last_objfile_name = NULL;
1764 if (last_objfile_name == NULL
1765 || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
1767 xfree (last_objfile_name);
1768 last_objfile_name = xstrdup (objfile_name (objfile));
1769 fprintf_filtered (gdb_stdlog,
1770 "Creating one or more psymtabs for objfile %s ...\n",
1773 fprintf_filtered (gdb_stdlog,
1774 "Created psymtab %s for module %s.\n",
1775 host_address_to_string (psymtab), filename);
1782 discard_psymtab (struct objfile *objfile, struct partial_symtab *pst)
1784 struct partial_symtab **prev_pst;
1787 Empty psymtabs happen as a result of header files which don't
1788 have any symbols in them. There can be a lot of them. But this
1789 check is wrong, in that a psymtab with N_SLINE entries but
1790 nothing else is not empty, but we don't realize that. Fixing
1791 that without slowing things down might be tricky. */
1793 /* First, snip it out of the psymtab chain. */
1795 prev_pst = &(objfile->psymtabs);
1796 while ((*prev_pst) != pst)
1797 prev_pst = &((*prev_pst)->next);
1798 (*prev_pst) = pst->next;
1800 /* Next, put it on a free list for recycling. */
1802 pst->next = objfile->free_psymtabs;
1803 objfile->free_psymtabs = pst;
1808 /* We need to pass a couple of items to the addrmap_foreach function,
1811 struct dump_psymtab_addrmap_data
1813 struct objfile *objfile;
1814 struct partial_symtab *psymtab;
1815 struct ui_file *outfile;
1817 /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1818 If so, we want to print the next one as well (since the next addrmap
1819 entry defines the end of the range). */
1820 int previous_matched;
1823 /* Helper function for dump_psymtab_addrmap to print an addrmap entry. */
1826 dump_psymtab_addrmap_1 (void *datap, CORE_ADDR start_addr, void *obj)
1828 struct dump_psymtab_addrmap_data *data
1829 = (struct dump_psymtab_addrmap_data *) datap;
1830 struct gdbarch *gdbarch = get_objfile_arch (data->objfile);
1831 struct partial_symtab *addrmap_psymtab = (struct partial_symtab *) obj;
1832 const char *psymtab_address_or_end = NULL;
1836 if (data->psymtab == NULL
1837 || data->psymtab == addrmap_psymtab)
1838 psymtab_address_or_end = host_address_to_string (addrmap_psymtab);
1839 else if (data->previous_matched)
1840 psymtab_address_or_end = "<ends here>";
1842 if (data->psymtab == NULL
1843 || data->psymtab == addrmap_psymtab
1844 || data->previous_matched)
1846 fprintf_filtered (data->outfile, " %s%s %s\n",
1847 data->psymtab != NULL ? " " : "",
1848 paddress (gdbarch, start_addr),
1849 psymtab_address_or_end);
1852 data->previous_matched = (data->psymtab == NULL
1853 || data->psymtab == addrmap_psymtab);
1858 /* Helper function for maintenance_print_psymbols to print the addrmap
1859 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1862 dump_psymtab_addrmap (struct objfile *objfile, struct partial_symtab *psymtab,
1863 struct ui_file *outfile)
1865 struct dump_psymtab_addrmap_data addrmap_dump_data;
1867 if ((psymtab == NULL
1868 || psymtab->psymtabs_addrmap_supported)
1869 && objfile->psymtabs_addrmap != NULL)
1871 addrmap_dump_data.objfile = objfile;
1872 addrmap_dump_data.psymtab = psymtab;
1873 addrmap_dump_data.outfile = outfile;
1874 addrmap_dump_data.previous_matched = 0;
1875 fprintf_filtered (outfile, "%sddress map:\n",
1876 psymtab == NULL ? "Entire a" : " A");
1877 addrmap_foreach (objfile->psymtabs_addrmap, dump_psymtab_addrmap_1,
1878 &addrmap_dump_data);
1883 maintenance_print_psymbols (const char *args, int from_tty)
1885 struct ui_file *outfile = gdb_stdout;
1886 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1887 struct objfile *objfile;
1888 struct partial_symtab *ps;
1889 int i, outfile_idx, found;
1891 struct obj_section *section = NULL;
1895 gdb_argv argv (args);
1897 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1899 if (strcmp (argv[i], "-pc") == 0)
1901 if (argv[i + 1] == NULL)
1902 error (_("Missing pc value"));
1903 address_arg = argv[++i];
1905 else if (strcmp (argv[i], "-source") == 0)
1907 if (argv[i + 1] == NULL)
1908 error (_("Missing source file"));
1909 source_arg = argv[++i];
1911 else if (strcmp (argv[i], "-objfile") == 0)
1913 if (argv[i + 1] == NULL)
1914 error (_("Missing objfile name"));
1915 objfile_arg = argv[++i];
1917 else if (strcmp (argv[i], "--") == 0)
1919 /* End of options. */
1923 else if (argv[i][0] == '-')
1925 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1926 error (_("Unknown option: %s"), argv[i]);
1933 if (address_arg != NULL && source_arg != NULL)
1934 error (_("Must specify at most one of -pc and -source"));
1936 stdio_file arg_outfile;
1938 if (argv != NULL && argv[outfile_idx] != NULL)
1940 if (argv[outfile_idx + 1] != NULL)
1941 error (_("Junk at end of command"));
1942 gdb::unique_xmalloc_ptr<char> outfile_name
1943 (tilde_expand (argv[outfile_idx]));
1944 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1945 perror_with_name (outfile_name.get ());
1946 outfile = &arg_outfile;
1949 if (address_arg != NULL)
1951 pc = parse_and_eval_address (address_arg);
1952 /* If we fail to find a section, that's ok, try the lookup anyway. */
1953 section = find_pc_section (pc);
1957 ALL_OBJFILES (objfile)
1959 int printed_objfile_header = 0;
1960 int print_for_objfile = 1;
1963 if (objfile_arg != NULL)
1965 = compare_filenames_for_search (objfile_name (objfile),
1967 if (!print_for_objfile)
1970 if (address_arg != NULL)
1972 struct bound_minimal_symbol msymbol = { NULL, NULL };
1974 /* We don't assume each pc has a unique objfile (this is for
1976 ps = find_pc_sect_psymtab (objfile, pc, section, msymbol);
1979 if (!printed_objfile_header)
1981 outfile->printf ("\nPartial symtabs for objfile %s\n",
1982 objfile_name (objfile));
1983 printed_objfile_header = 1;
1985 dump_psymtab (objfile, ps, outfile);
1986 dump_psymtab_addrmap (objfile, ps, outfile);
1992 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1994 int print_for_source = 0;
1997 if (source_arg != NULL)
2000 = compare_filenames_for_search (ps->filename, source_arg);
2003 if (source_arg == NULL
2004 || print_for_source)
2006 if (!printed_objfile_header)
2008 outfile->printf ("\nPartial symtabs for objfile %s\n",
2009 objfile_name (objfile));
2010 printed_objfile_header = 1;
2012 dump_psymtab (objfile, ps, outfile);
2013 dump_psymtab_addrmap (objfile, ps, outfile);
2018 /* If we're printing all the objfile's symbols dump the full addrmap. */
2020 if (address_arg == NULL
2021 && source_arg == NULL
2022 && objfile->psymtabs_addrmap != NULL)
2024 outfile->puts ("\n");
2025 dump_psymtab_addrmap (objfile, NULL, outfile);
2031 if (address_arg != NULL)
2032 error (_("No partial symtab for address: %s"), address_arg);
2033 if (source_arg != NULL)
2034 error (_("No partial symtab for source file: %s"), source_arg);
2038 /* List all the partial symbol tables whose names match REGEXP (optional). */
2041 maintenance_info_psymtabs (const char *regexp, int from_tty)
2043 struct program_space *pspace;
2044 struct objfile *objfile;
2049 ALL_PSPACES (pspace)
2050 ALL_PSPACE_OBJFILES (pspace, objfile)
2052 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2053 struct partial_symtab *psymtab;
2055 /* We don't want to print anything for this objfile until we
2056 actually find a symtab whose name matches. */
2057 int printed_objfile_start = 0;
2059 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
2064 || re_exec (psymtab->filename))
2066 if (! printed_objfile_start)
2068 printf_filtered ("{ objfile %s ", objfile_name (objfile));
2070 printf_filtered ("((struct objfile *) %s)\n",
2071 host_address_to_string (objfile));
2072 printed_objfile_start = 1;
2075 printf_filtered (" { psymtab %s ", psymtab->filename);
2077 printf_filtered ("((struct partial_symtab *) %s)\n",
2078 host_address_to_string (psymtab));
2080 printf_filtered (" readin %s\n",
2081 psymtab->readin ? "yes" : "no");
2082 printf_filtered (" fullname %s\n",
2084 ? psymtab->fullname : "(null)");
2085 printf_filtered (" text addresses ");
2086 fputs_filtered (paddress (gdbarch, psymtab->text_low (objfile)),
2088 printf_filtered (" -- ");
2089 fputs_filtered (paddress (gdbarch, psymtab->text_high (objfile)),
2091 printf_filtered ("\n");
2092 printf_filtered (" psymtabs_addrmap_supported %s\n",
2093 (psymtab->psymtabs_addrmap_supported
2095 printf_filtered (" globals ");
2096 if (psymtab->n_global_syms)
2098 auto p = &objfile->global_psymbols[psymtab->globals_offset];
2100 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2101 host_address_to_string (p),
2102 psymtab->n_global_syms);
2105 printf_filtered ("(none)\n");
2106 printf_filtered (" statics ");
2107 if (psymtab->n_static_syms)
2109 auto p = &objfile->static_psymbols[psymtab->statics_offset];
2111 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2112 host_address_to_string (p),
2113 psymtab->n_static_syms);
2116 printf_filtered ("(none)\n");
2117 printf_filtered (" dependencies ");
2118 if (psymtab->number_of_dependencies)
2122 printf_filtered ("{\n");
2123 for (i = 0; i < psymtab->number_of_dependencies; i++)
2125 struct partial_symtab *dep = psymtab->dependencies[i];
2127 /* Note the string concatenation there --- no comma. */
2128 printf_filtered (" psymtab %s "
2129 "((struct partial_symtab *) %s)\n",
2131 host_address_to_string (dep));
2133 printf_filtered (" }\n");
2136 printf_filtered ("(none)\n");
2137 printf_filtered (" }\n");
2141 if (printed_objfile_start)
2142 printf_filtered ("}\n");
2146 /* Check consistency of currently expanded psymtabs vs symtabs. */
2149 maintenance_check_psymtabs (const char *ignore, int from_tty)
2152 struct compunit_symtab *cust = NULL;
2153 struct partial_symtab *ps;
2154 const struct blockvector *bv;
2155 struct objfile *objfile;
2159 ALL_PSYMTABS (objfile, ps)
2161 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2163 /* We don't call psymtab_to_symtab here because that may cause symtab
2164 expansion. When debugging a problem it helps if checkers leave
2165 things unchanged. */
2166 cust = ps->compunit_symtab;
2168 /* First do some checks that don't require the associated symtab. */
2169 if (ps->text_high (objfile) < ps->text_low (objfile))
2171 printf_filtered ("Psymtab ");
2172 puts_filtered (ps->filename);
2173 printf_filtered (" covers bad range ");
2174 fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2176 printf_filtered (" - ");
2177 fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2179 printf_filtered ("\n");
2183 /* Now do checks requiring the associated symtab. */
2186 bv = COMPUNIT_BLOCKVECTOR (cust);
2187 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
2188 partial_symbol **psym = &objfile->static_psymbols[ps->statics_offset];
2189 length = ps->n_static_syms;
2192 sym = block_lookup_symbol (b, symbol_search_name (*psym),
2193 symbol_name_match_type::SEARCH_NAME,
2197 printf_filtered ("Static symbol `");
2198 puts_filtered ((*psym)->name);
2199 printf_filtered ("' only found in ");
2200 puts_filtered (ps->filename);
2201 printf_filtered (" psymtab\n");
2205 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2206 psym = &objfile->global_psymbols[ps->globals_offset];
2207 length = ps->n_global_syms;
2210 sym = block_lookup_symbol (b, symbol_search_name (*psym),
2211 symbol_name_match_type::SEARCH_NAME,
2215 printf_filtered ("Global symbol `");
2216 puts_filtered ((*psym)->name);
2217 printf_filtered ("' only found in ");
2218 puts_filtered (ps->filename);
2219 printf_filtered (" psymtab\n");
2223 if (ps->raw_text_high () != 0
2224 && (ps->text_low (objfile) < BLOCK_START (b)
2225 || ps->text_high (objfile) > BLOCK_END (b)))
2227 printf_filtered ("Psymtab ");
2228 puts_filtered (ps->filename);
2229 printf_filtered (" covers ");
2230 fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2232 printf_filtered (" - ");
2233 fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2235 printf_filtered (" but symtab covers only ");
2236 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2237 printf_filtered (" - ");
2238 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2239 printf_filtered ("\n");
2245 _initialize_psymtab (void)
2247 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2248 Print dump of current partial symbol definitions.\n\
2249 Usage: mt print psymbols [-objfile objfile] [-pc address] [--] [outfile]\n\
2250 mt print psymbols [-objfile objfile] [-source source] [--] [outfile]\n\
2251 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2252 or the terminal if OUTFILE is unspecified.\n\
2253 If ADDRESS is provided, dump only the file for that address.\n\
2254 If SOURCE is provided, dump only that file's symbols.\n\
2255 If OBJFILE is provided, dump only that file's minimal symbols."),
2256 &maintenanceprintlist);
2258 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2259 List the partial symbol tables for all object files.\n\
2260 This does not include information about individual partial symbols,\n\
2261 just the symbol table structures themselves."),
2262 &maintenanceinfolist);
2264 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2266 Check consistency of currently expanded psymtabs versus symtabs."),