1 /* Partial symbol tables.
3 Copyright (C) 2009-2017 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"
41 struct bcache *bcache;
44 static struct partial_symbol *match_partial_symbol (struct objfile *,
45 struct partial_symtab *,
47 const char *, domain_enum,
48 symbol_compare_ftype *,
49 symbol_compare_ftype *);
51 static struct partial_symbol *lookup_partial_symbol (struct objfile *,
52 struct partial_symtab *,
56 static const char *psymtab_to_fullname (struct partial_symtab *ps);
58 static struct partial_symbol *find_pc_sect_psymbol (struct objfile *,
59 struct partial_symtab *,
61 struct obj_section *);
63 static void fixup_psymbol_section (struct partial_symbol *psym,
64 struct objfile *objfile);
66 static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
67 struct partial_symtab *pst);
69 /* Ensure that the partial symbols for OBJFILE have been loaded. This
70 function always returns its argument, as a convenience. */
73 require_partial_symbols (struct objfile *objfile, int verbose)
75 if ((objfile->flags & OBJF_PSYMTABS_READ) == 0)
77 objfile->flags |= OBJF_PSYMTABS_READ;
79 if (objfile->sf->sym_read_psymbols)
83 printf_unfiltered (_("Reading symbols from %s..."),
84 objfile_name (objfile));
85 gdb_flush (gdb_stdout);
87 (*objfile->sf->sym_read_psymbols) (objfile);
90 if (!objfile_has_symbols (objfile))
93 printf_unfiltered (_("(no debugging symbols found)..."));
97 printf_unfiltered (_("done.\n"));
105 /* Traverse all psymtabs in one objfile, requiring that the psymtabs
108 #define ALL_OBJFILE_PSYMTABS_REQUIRED(objfile, p) \
109 for ((p) = require_partial_symbols (objfile, 1)->psymtabs; \
113 /* We want to make sure this file always requires psymtabs. */
115 #undef ALL_OBJFILE_PSYMTABS
117 /* Traverse all psymtabs in all objfiles. */
119 #define ALL_PSYMTABS(objfile, p) \
120 ALL_OBJFILES (objfile) \
121 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
123 /* Helper function for psym_map_symtabs_matching_filename that
124 expands the symtabs and calls the iterator. */
127 partial_map_expand_apply (struct objfile *objfile,
129 const char *real_path,
130 struct partial_symtab *pst,
131 gdb::function_view<bool (symtab *)> callback)
133 struct compunit_symtab *last_made = objfile->compunit_symtabs;
135 /* Shared psymtabs should never be seen here. Instead they should
136 be handled properly by the caller. */
137 gdb_assert (pst->user == NULL);
139 /* Don't visit already-expanded psymtabs. */
143 /* This may expand more than one symtab, and we want to iterate over
145 psymtab_to_symtab (objfile, pst);
147 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
148 last_made, callback);
151 /* Psymtab version of map_symtabs_matching_filename. See its definition in
152 the definition of quick_symbol_functions in symfile.h. */
155 psym_map_symtabs_matching_filename
156 (struct objfile *objfile,
158 const char *real_path,
159 gdb::function_view<bool (symtab *)> callback)
161 struct partial_symtab *pst;
162 const char *name_basename = lbasename (name);
164 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
166 /* We can skip shared psymtabs here, because any file name will be
167 attached to the unshared psymtab. */
168 if (pst->user != NULL)
171 /* Anonymous psymtabs don't have a file name. */
175 if (compare_filenames_for_search (pst->filename, name))
177 if (partial_map_expand_apply (objfile, name, real_path,
183 /* Before we invoke realpath, which can get expensive when many
184 files are involved, do a quick comparison of the basenames. */
185 if (! basenames_may_differ
186 && FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
189 if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
191 if (partial_map_expand_apply (objfile, name, real_path,
197 /* If the user gave us an absolute path, try to find the file in
198 this symtab and use its absolute path. */
199 if (real_path != NULL)
201 gdb_assert (IS_ABSOLUTE_PATH (real_path));
202 gdb_assert (IS_ABSOLUTE_PATH (name));
203 if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
205 if (partial_map_expand_apply (objfile, name, real_path,
216 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
217 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
219 static struct partial_symtab *
220 find_pc_sect_psymtab_closer (struct objfile *objfile,
221 CORE_ADDR pc, struct obj_section *section,
222 struct partial_symtab *pst,
223 struct bound_minimal_symbol msymbol)
225 struct partial_symtab *tpst;
226 struct partial_symtab *best_pst = pst;
227 CORE_ADDR best_addr = pst->textlow;
229 gdb_assert (!pst->psymtabs_addrmap_supported);
231 /* An objfile that has its functions reordered might have
232 many partial symbol tables containing the PC, but
233 we want the partial symbol table that contains the
234 function containing the PC. */
235 if (!(objfile->flags & OBJF_REORDERED)
236 && section == NULL) /* Can't validate section this way. */
239 if (msymbol.minsym == NULL)
242 /* The code range of partial symtabs sometimes overlap, so, in
243 the loop below, we need to check all partial symtabs and
244 find the one that fits better for the given PC address. We
245 select the partial symtab that contains a symbol whose
246 address is closest to the PC address. By closest we mean
247 that find_pc_sect_symbol returns the symbol with address
248 that is closest and still less than the given PC. */
249 for (tpst = pst; tpst != NULL; tpst = tpst->next)
251 if (pc >= tpst->textlow && pc < tpst->texthigh)
253 struct partial_symbol *p;
256 /* NOTE: This assumes that every psymbol has a
257 corresponding msymbol, which is not necessarily
258 true; the debug info might be much richer than the
259 object's symbol table. */
260 p = find_pc_sect_psymbol (objfile, tpst, pc, section);
262 && (SYMBOL_VALUE_ADDRESS (p)
263 == BMSYMBOL_VALUE_ADDRESS (msymbol)))
266 /* Also accept the textlow value of a psymtab as a
267 "symbol", to provide some support for partial
268 symbol tables with line information but no debug
269 symbols (e.g. those produced by an assembler). */
271 this_addr = SYMBOL_VALUE_ADDRESS (p);
273 this_addr = tpst->textlow;
275 /* Check whether it is closer than our current
276 BEST_ADDR. Since this symbol address is
277 necessarily lower or equal to PC, the symbol closer
278 to PC is the symbol which address is the highest.
279 This way we return the psymtab which contains such
280 best match symbol. This can help in cases where the
281 symbol information/debuginfo is not complete, like
282 for instance on IRIX6 with gcc, where no debug info
283 is emitted for statics. (See also the nodebug.exp
285 if (this_addr > best_addr)
287 best_addr = this_addr;
295 /* Find which partial symtab contains PC and SECTION. Return NULL if
296 none. We return the psymtab that contains a symbol whose address
297 exactly matches PC, or, if we cannot find an exact match, the
298 psymtab that contains a symbol whose address is closest to PC. */
300 static struct partial_symtab *
301 find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
302 struct obj_section *section,
303 struct bound_minimal_symbol msymbol)
305 struct partial_symtab *pst;
307 /* Try just the PSYMTABS_ADDRMAP mapping first as it has better granularity
308 than the later used TEXTLOW/TEXTHIGH one. */
310 if (objfile->psymtabs_addrmap != NULL)
312 pst = ((struct partial_symtab *)
313 addrmap_find (objfile->psymtabs_addrmap, pc));
316 /* FIXME: addrmaps currently do not handle overlayed sections,
317 so fall back to the non-addrmap case if we're debugging
318 overlays and the addrmap returned the wrong section. */
319 if (overlay_debugging && msymbol.minsym != NULL && section != NULL)
321 struct partial_symbol *p;
323 /* NOTE: This assumes that every psymbol has a
324 corresponding msymbol, which is not necessarily
325 true; the debug info might be much richer than the
326 object's symbol table. */
327 p = find_pc_sect_psymbol (objfile, pst, pc, section);
329 || (SYMBOL_VALUE_ADDRESS (p)
330 != BMSYMBOL_VALUE_ADDRESS (msymbol)))
334 /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
335 PSYMTABS_ADDRMAP we used has already the best 1-byte
336 granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
337 a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
346 /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
347 which still have no corresponding full SYMTABs read. But it is not
348 present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
351 /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
352 its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
353 debug info type in single OBJFILE. */
355 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
356 if (!pst->psymtabs_addrmap_supported
357 && pc >= pst->textlow && pc < pst->texthigh)
359 struct partial_symtab *best_pst;
361 best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
363 if (best_pst != NULL)
370 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
371 the definition of quick_symbol_functions in symfile.h. */
373 static struct compunit_symtab *
374 psym_find_pc_sect_compunit_symtab (struct objfile *objfile,
375 struct bound_minimal_symbol msymbol,
377 struct obj_section *section,
380 struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc, section,
384 if (warn_if_readin && ps->readin)
385 /* Might want to error() here (in case symtab is corrupt and
386 will cause a core dump), but maybe we can successfully
387 continue, so let's not. */
389 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
390 paddress (get_objfile_arch (objfile), pc));
391 psymtab_to_symtab (objfile, ps);
392 return ps->compunit_symtab;
397 /* Find which partial symbol within a psymtab matches PC and SECTION.
398 Return NULL if none. */
400 static struct partial_symbol *
401 find_pc_sect_psymbol (struct objfile *objfile,
402 struct partial_symtab *psymtab, CORE_ADDR pc,
403 struct obj_section *section)
405 struct partial_symbol *best = NULL, *p, **pp;
408 gdb_assert (psymtab != NULL);
410 /* Cope with programs that start at address 0. */
411 best_pc = (psymtab->textlow != 0) ? psymtab->textlow - 1 : 0;
413 /* Search the global symbols as well as the static symbols, so that
414 find_pc_partial_function doesn't use a minimal symbol and thus
415 cache a bad endaddr. */
416 for (pp = objfile->global_psymbols.list + psymtab->globals_offset;
417 (pp - (objfile->global_psymbols.list + psymtab->globals_offset)
418 < psymtab->n_global_syms);
422 if (SYMBOL_DOMAIN (p) == VAR_DOMAIN
423 && PSYMBOL_CLASS (p) == LOC_BLOCK
424 && pc >= SYMBOL_VALUE_ADDRESS (p)
425 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
426 || (psymtab->textlow == 0
427 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
429 if (section != NULL) /* Match on a specific section. */
431 fixup_psymbol_section (p, objfile);
432 if (!matching_obj_sections (SYMBOL_OBJ_SECTION (objfile, p),
436 best_pc = SYMBOL_VALUE_ADDRESS (p);
441 for (pp = objfile->static_psymbols.list + psymtab->statics_offset;
442 (pp - (objfile->static_psymbols.list + psymtab->statics_offset)
443 < psymtab->n_static_syms);
447 if (SYMBOL_DOMAIN (p) == VAR_DOMAIN
448 && PSYMBOL_CLASS (p) == LOC_BLOCK
449 && pc >= SYMBOL_VALUE_ADDRESS (p)
450 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
451 || (psymtab->textlow == 0
452 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
454 if (section != NULL) /* Match on a specific section. */
456 fixup_psymbol_section (p, objfile);
457 if (!matching_obj_sections (SYMBOL_OBJ_SECTION (objfile, p),
461 best_pc = SYMBOL_VALUE_ADDRESS (p);
470 fixup_psymbol_section (struct partial_symbol *psym, struct objfile *objfile)
477 if (SYMBOL_SECTION (psym) >= 0)
480 gdb_assert (objfile);
482 switch (PSYMBOL_CLASS (psym))
487 addr = SYMBOL_VALUE_ADDRESS (psym);
490 /* Nothing else will be listed in the minsyms -- no use looking
495 fixup_section (&psym->ginfo, addr, objfile);
498 /* Psymtab version of lookup_symbol. See its definition in
499 the definition of quick_symbol_functions in symfile.h. */
501 static struct compunit_symtab *
502 psym_lookup_symbol (struct objfile *objfile,
503 int block_index, const char *name,
504 const domain_enum domain)
506 struct partial_symtab *ps;
507 const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
508 struct compunit_symtab *stab_best = NULL;
510 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
512 if (!ps->readin && lookup_partial_symbol (objfile, ps, name,
513 psymtab_index, domain))
515 struct symbol *sym, *with_opaque = NULL;
516 struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
517 /* Note: While psymtab_to_symtab can return NULL if the partial symtab
518 is empty, we can assume it won't here because lookup_partial_symbol
520 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
521 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
523 sym = block_find_symbol (block, name, domain,
524 block_find_non_opaque_type_preferred,
527 /* Some caution must be observed with overloaded functions
528 and methods, since the index will not contain any overload
529 information (but NAME might contain it). */
532 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
534 if (with_opaque != NULL
535 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
538 /* Keep looking through other psymtabs. */
545 /* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
546 the global block of PST if GLOBAL, and otherwise the static block.
547 MATCH is the comparison operation that returns true iff MATCH (s,
548 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
549 non-null, the symbols in the block are assumed to be ordered
550 according to it (allowing binary search). It must be compatible
551 with MATCH. Returns the symbol, if found, and otherwise NULL. */
553 static struct partial_symbol *
554 match_partial_symbol (struct objfile *objfile,
555 struct partial_symtab *pst, int global,
556 const char *name, domain_enum domain,
557 symbol_compare_ftype *match,
558 symbol_compare_ftype *ordered_compare)
560 struct partial_symbol **start, **psym;
561 struct partial_symbol **top, **real_top, **bottom, **center;
562 int length = (global ? pst->n_global_syms : pst->n_static_syms);
563 int do_linear_search = 1;
568 objfile->global_psymbols.list + pst->globals_offset :
569 objfile->static_psymbols.list + pst->statics_offset);
571 if (global && ordered_compare) /* Can use a binary search. */
573 do_linear_search = 0;
575 /* Binary search. This search is guaranteed to end with center
576 pointing at the earliest partial symbol whose name might be
577 correct. At that point *all* partial symbols with an
578 appropriate name will be checked against the correct
582 top = start + length - 1;
586 center = bottom + (top - bottom) / 2;
587 gdb_assert (center < top);
588 if (ordered_compare (SYMBOL_SEARCH_NAME (*center), name) >= 0)
593 gdb_assert (top == bottom);
595 while (top <= real_top
596 && match (SYMBOL_SEARCH_NAME (*top), name) == 0)
598 if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
599 SYMBOL_DOMAIN (*top), domain))
605 /* Can't use a binary search or else we found during the binary search that
606 we should also do a linear search. */
608 if (do_linear_search)
610 for (psym = start; psym < start + length; psym++)
612 if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
613 SYMBOL_DOMAIN (*psym), domain)
614 && match (SYMBOL_SEARCH_NAME (*psym), name) == 0)
622 /* Returns the name used to search psymtabs. Unlike symtabs, psymtabs do
623 not contain any method/function instance information (since this would
624 force reading type information while reading psymtabs). Therefore,
625 if NAME contains overload information, it must be stripped before searching
628 The caller is responsible for freeing the return result. */
631 psymtab_search_name (const char *name)
633 switch (current_language->la_language)
637 if (strchr (name, '('))
639 char *ret = cp_remove_params (name);
651 return xstrdup (name);
654 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
655 Check the global symbols if GLOBAL, the static symbols if not. */
657 static struct partial_symbol *
658 lookup_partial_symbol (struct objfile *objfile,
659 struct partial_symtab *pst, const char *name,
660 int global, domain_enum domain)
662 struct partial_symbol **start, **psym;
663 struct partial_symbol **top, **real_top, **bottom, **center;
664 int length = (global ? pst->n_global_syms : pst->n_static_syms);
665 int do_linear_search = 1;
667 struct cleanup *cleanup;
672 search_name = psymtab_search_name (name);
673 cleanup = make_cleanup (xfree, search_name);
675 objfile->global_psymbols.list + pst->globals_offset :
676 objfile->static_psymbols.list + pst->statics_offset);
678 if (global) /* This means we can use a binary search. */
680 do_linear_search = 0;
682 /* Binary search. This search is guaranteed to end with center
683 pointing at the earliest partial symbol whose name might be
684 correct. At that point *all* partial symbols with an
685 appropriate name will be checked against the correct
689 top = start + length - 1;
693 center = bottom + (top - bottom) / 2;
695 internal_error (__FILE__, __LINE__,
696 _("failed internal consistency check"));
697 if (strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*center),
707 if (!(top == bottom))
708 internal_error (__FILE__, __LINE__,
709 _("failed internal consistency check"));
711 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
712 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
713 while (top >= start && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name))
716 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
719 while (top <= real_top && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name))
721 if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
722 SYMBOL_DOMAIN (*top), domain))
724 do_cleanups (cleanup);
731 /* Can't use a binary search or else we found during the binary search that
732 we should also do a linear search. */
734 if (do_linear_search)
736 for (psym = start; psym < start + length; psym++)
738 if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
739 SYMBOL_DOMAIN (*psym), domain)
740 && SYMBOL_MATCHES_SEARCH_NAME (*psym, search_name))
742 do_cleanups (cleanup);
748 do_cleanups (cleanup);
752 /* Get the symbol table that corresponds to a partial_symtab.
753 This is fast after the first time you do it.
754 The result will be NULL if the primary symtab has no symbols,
755 which can happen. Otherwise the result is the primary symtab
756 that contains PST. */
758 static struct compunit_symtab *
759 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
761 /* If it is a shared psymtab, find an unshared psymtab that includes
762 it. Any such psymtab will do. */
763 while (pst->user != NULL)
766 /* If it's been looked up before, return it. */
767 if (pst->compunit_symtab)
768 return pst->compunit_symtab;
770 /* If it has not yet been read in, read it. */
773 scoped_restore decrementer = increment_reading_symtab ();
775 (*pst->read_symtab) (pst, objfile);
778 return pst->compunit_symtab;
781 /* Psymtab version of relocate. See its definition in
782 the definition of quick_symbol_functions in symfile.h. */
785 psym_relocate (struct objfile *objfile,
786 const struct section_offsets *new_offsets,
787 const struct section_offsets *delta)
789 struct partial_symbol **psym;
790 struct partial_symtab *p;
792 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
794 p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
795 p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
798 for (psym = objfile->global_psymbols.list;
799 psym < objfile->global_psymbols.next;
802 fixup_psymbol_section (*psym, objfile);
803 if (SYMBOL_SECTION (*psym) >= 0)
804 SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
805 SYMBOL_SECTION (*psym));
807 for (psym = objfile->static_psymbols.list;
808 psym < objfile->static_psymbols.next;
811 fixup_psymbol_section (*psym, objfile);
812 if (SYMBOL_SECTION (*psym) >= 0)
813 SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
814 SYMBOL_SECTION (*psym));
818 /* Psymtab version of find_last_source_symtab. See its definition in
819 the definition of quick_symbol_functions in symfile.h. */
821 static struct symtab *
822 psym_find_last_source_symtab (struct objfile *ofp)
824 struct partial_symtab *ps;
825 struct partial_symtab *cs_pst = NULL;
827 ALL_OBJFILE_PSYMTABS_REQUIRED (ofp, ps)
829 const char *name = ps->filename;
830 int len = strlen (name);
832 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
833 || strcmp (name, "<<C++-namespaces>>") == 0)))
841 internal_error (__FILE__, __LINE__,
842 _("select_source_symtab: "
843 "readin pst found and no symtabs."));
847 struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
851 return compunit_primary_filetab (cust);
857 /* Psymtab version of forget_cached_source_info. See its definition in
858 the definition of quick_symbol_functions in symfile.h. */
861 psym_forget_cached_source_info (struct objfile *objfile)
863 struct partial_symtab *pst;
865 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
867 if (pst->fullname != NULL)
869 xfree (pst->fullname);
870 pst->fullname = NULL;
876 print_partial_symbols (struct gdbarch *gdbarch,
877 struct partial_symbol **p, int count, const char *what,
878 struct ui_file *outfile)
880 fprintf_filtered (outfile, " %s partial symbols:\n", what);
884 fprintf_filtered (outfile, " `%s'", SYMBOL_LINKAGE_NAME (*p));
885 if (SYMBOL_DEMANGLED_NAME (*p) != NULL)
887 fprintf_filtered (outfile, " `%s'", SYMBOL_DEMANGLED_NAME (*p));
889 fputs_filtered (", ", outfile);
890 switch (SYMBOL_DOMAIN (*p))
893 fputs_filtered ("undefined domain, ", outfile);
896 /* This is the usual thing -- don't print it. */
899 fputs_filtered ("struct domain, ", outfile);
902 fputs_filtered ("label domain, ", outfile);
905 fputs_filtered ("<invalid domain>, ", outfile);
908 switch (PSYMBOL_CLASS (*p))
911 fputs_filtered ("undefined", outfile);
914 fputs_filtered ("constant int", outfile);
917 fputs_filtered ("static", outfile);
920 fputs_filtered ("register", outfile);
923 fputs_filtered ("pass by value", outfile);
926 fputs_filtered ("pass by reference", outfile);
928 case LOC_REGPARM_ADDR:
929 fputs_filtered ("register address parameter", outfile);
932 fputs_filtered ("stack parameter", outfile);
935 fputs_filtered ("type", outfile);
938 fputs_filtered ("label", outfile);
941 fputs_filtered ("function", outfile);
943 case LOC_CONST_BYTES:
944 fputs_filtered ("constant bytes", outfile);
947 fputs_filtered ("unresolved", outfile);
949 case LOC_OPTIMIZED_OUT:
950 fputs_filtered ("optimized out", outfile);
953 fputs_filtered ("computed at runtime", outfile);
956 fputs_filtered ("<invalid location>", outfile);
959 fputs_filtered (", ", outfile);
960 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (*p)), outfile);
961 fprintf_filtered (outfile, "\n");
967 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
968 struct ui_file *outfile)
970 struct gdbarch *gdbarch = get_objfile_arch (objfile);
973 if (psymtab->anonymous)
975 fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
980 fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
983 fprintf_filtered (outfile, "(object ");
984 gdb_print_host_address (psymtab, outfile);
985 fprintf_filtered (outfile, ")\n\n");
986 fprintf_unfiltered (outfile, " Read from object file %s (",
987 objfile_name (objfile));
988 gdb_print_host_address (objfile, outfile);
989 fprintf_unfiltered (outfile, ")\n");
993 fprintf_filtered (outfile,
994 " Full symtab was read (at ");
995 gdb_print_host_address (psymtab->compunit_symtab, outfile);
996 fprintf_filtered (outfile, " by function at ");
997 gdb_print_host_address (psymtab->read_symtab, outfile);
998 fprintf_filtered (outfile, ")\n");
1001 fprintf_filtered (outfile, " Symbols cover text addresses ");
1002 fputs_filtered (paddress (gdbarch, psymtab->textlow), outfile);
1003 fprintf_filtered (outfile, "-");
1004 fputs_filtered (paddress (gdbarch, psymtab->texthigh), outfile);
1005 fprintf_filtered (outfile, "\n");
1006 fprintf_filtered (outfile, " Address map supported - %s.\n",
1007 psymtab->psymtabs_addrmap_supported ? "yes" : "no");
1008 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
1009 psymtab->number_of_dependencies);
1010 for (i = 0; i < psymtab->number_of_dependencies; i++)
1012 fprintf_filtered (outfile, " %d ", i);
1013 gdb_print_host_address (psymtab->dependencies[i], outfile);
1014 fprintf_filtered (outfile, " %s\n",
1015 psymtab->dependencies[i]->filename);
1017 if (psymtab->user != NULL)
1019 fprintf_filtered (outfile, " Shared partial symtab with user ");
1020 gdb_print_host_address (psymtab->user, outfile);
1021 fprintf_filtered (outfile, "\n");
1023 if (psymtab->n_global_syms > 0)
1025 print_partial_symbols (gdbarch,
1026 objfile->global_psymbols.list
1027 + psymtab->globals_offset,
1028 psymtab->n_global_syms, "Global", outfile);
1030 if (psymtab->n_static_syms > 0)
1032 print_partial_symbols (gdbarch,
1033 objfile->static_psymbols.list
1034 + psymtab->statics_offset,
1035 psymtab->n_static_syms, "Static", outfile);
1037 fprintf_filtered (outfile, "\n");
1040 /* Psymtab version of print_stats. See its definition in
1041 the definition of quick_symbol_functions in symfile.h. */
1044 psym_print_stats (struct objfile *objfile)
1047 struct partial_symtab *ps;
1050 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1052 if (ps->readin == 0)
1055 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i);
1058 /* Psymtab version of dump. See its definition in
1059 the definition of quick_symbol_functions in symfile.h. */
1062 psym_dump (struct objfile *objfile)
1064 struct partial_symtab *psymtab;
1066 if (objfile->psymtabs)
1068 printf_filtered ("Psymtabs:\n");
1069 for (psymtab = objfile->psymtabs;
1071 psymtab = psymtab->next)
1073 printf_filtered ("%s at ",
1075 gdb_print_host_address (psymtab, gdb_stdout);
1076 printf_filtered (", ");
1079 printf_filtered ("\n\n");
1083 /* Psymtab version of expand_symtabs_for_function. See its definition in
1084 the definition of quick_symbol_functions in symfile.h. */
1087 psym_expand_symtabs_for_function (struct objfile *objfile,
1088 const char *func_name)
1090 struct partial_symtab *ps;
1092 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1097 if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
1099 || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
1101 psymtab_to_symtab (objfile, ps);
1105 /* Psymtab version of expand_all_symtabs. See its definition in
1106 the definition of quick_symbol_functions in symfile.h. */
1109 psym_expand_all_symtabs (struct objfile *objfile)
1111 struct partial_symtab *psymtab;
1113 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
1115 psymtab_to_symtab (objfile, psymtab);
1119 /* Psymtab version of expand_symtabs_with_fullname. See its definition in
1120 the definition of quick_symbol_functions in symfile.h. */
1123 psym_expand_symtabs_with_fullname (struct objfile *objfile,
1124 const char *fullname)
1126 struct partial_symtab *p;
1128 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
1130 /* Anonymous psymtabs don't have a name of a source file. */
1134 /* psymtab_to_fullname tries to open the file which is slow.
1135 Don't call it if we know the basenames don't match. */
1136 if ((basenames_may_differ
1137 || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1138 && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
1139 psymtab_to_symtab (objfile, p);
1143 /* Psymtab version of map_symbol_filenames. See its definition in
1144 the definition of quick_symbol_functions in symfile.h. */
1147 psym_map_symbol_filenames (struct objfile *objfile,
1148 symbol_filename_ftype *fun, void *data,
1151 struct partial_symtab *ps;
1153 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1155 const char *fullname;
1160 /* We can skip shared psymtabs here, because any file name will be
1161 attached to the unshared psymtab. */
1162 if (ps->user != NULL)
1165 /* Anonymous psymtabs don't have a file name. */
1171 fullname = psymtab_to_fullname (ps);
1174 (*fun) (ps->filename, fullname, data);
1178 /* Finds the fullname that a partial_symtab represents.
1180 If this functions finds the fullname, it will save it in ps->fullname
1181 and it will also return the value.
1183 If this function fails to find the file that this partial_symtab represents,
1184 NULL will be returned and ps->fullname will be set to NULL. */
1187 psymtab_to_fullname (struct partial_symtab *ps)
1189 gdb_assert (!ps->anonymous);
1191 /* Use cached copy if we have it.
1192 We rely on forget_cached_source_info being called appropriately
1193 to handle cases like the file being moved. */
1194 if (ps->fullname == NULL)
1196 int fd = find_and_open_source (ps->filename, ps->dirname, &ps->fullname);
1203 struct cleanup *back_to;
1205 /* rewrite_source_path would be applied by find_and_open_source, we
1206 should report the pathname where GDB tried to find the file. */
1208 if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
1209 fullname = xstrdup (ps->filename);
1211 fullname = concat (ps->dirname, SLASH_STRING,
1212 ps->filename, (char *) NULL);
1214 back_to = make_cleanup (xfree, fullname);
1215 ps->fullname = rewrite_source_path (fullname);
1216 if (ps->fullname == NULL)
1217 ps->fullname = xstrdup (fullname);
1218 do_cleanups (back_to);
1222 return ps->fullname;
1225 /* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1226 according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1227 BLOCK is assumed to come from OBJFILE. Returns 1 iff CALLBACK
1228 ever returns non-zero, and otherwise returns 0. */
1231 map_block (const char *name, domain_enum domain, struct objfile *objfile,
1232 struct block *block,
1233 int (*callback) (struct block *, struct symbol *, void *),
1234 void *data, symbol_compare_ftype *match)
1236 struct block_iterator iter;
1239 for (sym = block_iter_match_first (block, name, match, &iter);
1240 sym != NULL; sym = block_iter_match_next (name, match, &iter))
1242 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
1243 SYMBOL_DOMAIN (sym), domain))
1245 if (callback (block, sym, data))
1253 /* Psymtab version of map_matching_symbols. See its definition in
1254 the definition of quick_symbol_functions in symfile.h. */
1257 psym_map_matching_symbols (struct objfile *objfile,
1258 const char *name, domain_enum domain,
1260 int (*callback) (struct block *,
1261 struct symbol *, void *),
1263 symbol_compare_ftype *match,
1264 symbol_compare_ftype *ordered_compare)
1266 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
1267 struct partial_symtab *ps;
1269 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1273 || match_partial_symbol (objfile, ps, global, name, domain, match,
1276 struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
1277 struct block *block;
1281 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
1282 if (map_block (name, domain, objfile, block,
1283 callback, data, match))
1285 if (callback (block, NULL, data))
1291 /* A helper for psym_expand_symtabs_matching that handles searching
1292 included psymtabs. This returns true if a symbol is found, and
1293 false otherwise. It also updates the 'searched_flag' on the
1294 various psymtabs that it searches. */
1297 recursively_search_psymtabs
1298 (struct partial_symtab *ps, struct objfile *objfile, enum search_domain kind,
1299 gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
1301 struct partial_symbol **psym;
1302 struct partial_symbol **bound, **gbound, **sbound;
1304 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
1307 if (ps->searched_flag != PST_NOT_SEARCHED)
1308 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1310 /* Recurse into shared psymtabs first, because they may have already
1311 been searched, and this could save some time. */
1312 for (i = 0; i < ps->number_of_dependencies; ++i)
1316 /* Skip non-shared dependencies, these are handled elsewhere. */
1317 if (ps->dependencies[i]->user == NULL)
1320 r = recursively_search_psymtabs (ps->dependencies[i],
1321 objfile, kind, sym_matcher);
1324 ps->searched_flag = PST_SEARCHED_AND_FOUND;
1329 gbound = (objfile->global_psymbols.list
1330 + ps->globals_offset + ps->n_global_syms);
1331 sbound = (objfile->static_psymbols.list
1332 + ps->statics_offset + ps->n_static_syms);
1335 /* Go through all of the symbols stored in a partial
1336 symtab in one loop. */
1337 psym = objfile->global_psymbols.list + ps->globals_offset;
1342 if (bound == gbound && ps->n_static_syms != 0)
1344 psym = objfile->static_psymbols.list + ps->statics_offset;
1355 if ((kind == ALL_DOMAIN
1356 || (kind == VARIABLES_DOMAIN
1357 && PSYMBOL_CLASS (*psym) != LOC_TYPEDEF
1358 && PSYMBOL_CLASS (*psym) != LOC_BLOCK)
1359 || (kind == FUNCTIONS_DOMAIN
1360 && PSYMBOL_CLASS (*psym) == LOC_BLOCK)
1361 || (kind == TYPES_DOMAIN
1362 && PSYMBOL_CLASS (*psym) == LOC_TYPEDEF))
1363 && sym_matcher (SYMBOL_SEARCH_NAME (*psym)))
1365 /* Found a match, so notify our caller. */
1366 result = PST_SEARCHED_AND_FOUND;
1373 ps->searched_flag = result;
1374 return result == PST_SEARCHED_AND_FOUND;
1377 /* Psymtab version of expand_symtabs_matching. See its definition in
1378 the definition of quick_symbol_functions in symfile.h. */
1381 psym_expand_symtabs_matching
1382 (struct objfile *objfile,
1383 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1384 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1385 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1386 enum search_domain kind)
1388 struct partial_symtab *ps;
1390 /* Clear the search flags. */
1391 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1393 ps->searched_flag = PST_NOT_SEARCHED;
1396 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1403 /* We skip shared psymtabs because file-matching doesn't apply
1404 to them; but we search them later in the loop. */
1405 if (ps->user != NULL)
1415 match = file_matcher (ps->filename, false);
1418 /* Before we invoke realpath, which can get expensive when many
1419 files are involved, do a quick comparison of the basenames. */
1420 if (basenames_may_differ
1421 || file_matcher (lbasename (ps->filename), true))
1422 match = file_matcher (psymtab_to_fullname (ps), false);
1428 if (recursively_search_psymtabs (ps, objfile, kind, symbol_matcher))
1430 struct compunit_symtab *symtab =
1431 psymtab_to_symtab (objfile, ps);
1433 if (expansion_notify != NULL)
1434 expansion_notify (symtab);
1439 /* Psymtab version of has_symbols. See its definition in
1440 the definition of quick_symbol_functions in symfile.h. */
1443 psym_has_symbols (struct objfile *objfile)
1445 return objfile->psymtabs != NULL;
1448 const struct quick_symbol_functions psym_functions =
1451 psym_find_last_source_symtab,
1452 psym_forget_cached_source_info,
1453 psym_map_symtabs_matching_filename,
1458 psym_expand_symtabs_for_function,
1459 psym_expand_all_symtabs,
1460 psym_expand_symtabs_with_fullname,
1461 psym_map_matching_symbols,
1462 psym_expand_symtabs_matching,
1463 psym_find_pc_sect_compunit_symtab,
1464 psym_map_symbol_filenames
1469 /* This compares two partial symbols by names, using strcmp_iw_ordered
1470 for the comparison. */
1473 compare_psymbols (const void *s1p, const void *s2p)
1475 struct partial_symbol *const *s1 = (struct partial_symbol * const*) s1p;
1476 struct partial_symbol *const *s2 = (struct partial_symbol * const*) s2p;
1478 return strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*s1),
1479 SYMBOL_SEARCH_NAME (*s2));
1483 sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
1485 /* Sort the global list; don't sort the static list. */
1487 qsort (objfile->global_psymbols.list + pst->globals_offset,
1488 pst->n_global_syms, sizeof (struct partial_symbol *),
1492 /* Allocate and partially fill a partial symtab. It will be
1493 completely filled at the end of the symbol list.
1495 FILENAME is the name of the symbol-file we are reading from. */
1497 struct partial_symtab *
1498 start_psymtab_common (struct objfile *objfile,
1499 const char *filename,
1500 CORE_ADDR textlow, struct partial_symbol **global_syms,
1501 struct partial_symbol **static_syms)
1503 struct partial_symtab *psymtab;
1505 psymtab = allocate_psymtab (filename, objfile);
1506 psymtab->textlow = textlow;
1507 psymtab->texthigh = psymtab->textlow; /* default */
1508 psymtab->globals_offset = global_syms - objfile->global_psymbols.list;
1509 psymtab->statics_offset = static_syms - objfile->static_psymbols.list;
1513 /* Perform "finishing up" operations of a partial symtab. */
1516 end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1519 = objfile->global_psymbols.next - (objfile->global_psymbols.list
1520 + pst->globals_offset);
1522 = objfile->static_psymbols.next - (objfile->static_psymbols.list
1523 + pst->statics_offset);
1525 sort_pst_symbols (objfile, pst);
1528 /* Calculate a hash code for the given partial symbol. The hash is
1529 calculated using the symbol's value, language, domain, class
1530 and name. These are the values which are set by
1531 add_psymbol_to_bcache. */
1533 static unsigned long
1534 psymbol_hash (const void *addr, int length)
1536 unsigned long h = 0;
1537 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1538 unsigned int lang = psymbol->ginfo.language;
1539 unsigned int domain = PSYMBOL_DOMAIN (psymbol);
1540 unsigned int theclass = PSYMBOL_CLASS (psymbol);
1542 h = hash_continue (&psymbol->ginfo.value, sizeof (psymbol->ginfo.value), h);
1543 h = hash_continue (&lang, sizeof (unsigned int), h);
1544 h = hash_continue (&domain, sizeof (unsigned int), h);
1545 h = hash_continue (&theclass, sizeof (unsigned int), h);
1546 h = hash_continue (psymbol->ginfo.name, strlen (psymbol->ginfo.name), h);
1551 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1552 For the comparison this function uses a symbols value,
1553 language, domain, class and name. */
1556 psymbol_compare (const void *addr1, const void *addr2, int length)
1558 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1559 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1561 return (memcmp (&sym1->ginfo.value, &sym2->ginfo.value,
1562 sizeof (sym1->ginfo.value)) == 0
1563 && sym1->ginfo.language == sym2->ginfo.language
1564 && PSYMBOL_DOMAIN (sym1) == PSYMBOL_DOMAIN (sym2)
1565 && PSYMBOL_CLASS (sym1) == PSYMBOL_CLASS (sym2)
1566 && sym1->ginfo.name == sym2->ginfo.name);
1569 /* Initialize a partial symbol bcache. */
1571 struct psymbol_bcache *
1572 psymbol_bcache_init (void)
1574 struct psymbol_bcache *bcache = XCNEW (struct psymbol_bcache);
1576 bcache->bcache = bcache_xmalloc (psymbol_hash, psymbol_compare);
1580 /* Free a partial symbol bcache. */
1583 psymbol_bcache_free (struct psymbol_bcache *bcache)
1588 bcache_xfree (bcache->bcache);
1592 /* Return the internal bcache of the psymbol_bcache BCACHE. */
1595 psymbol_bcache_get_bcache (struct psymbol_bcache *bcache)
1597 return bcache->bcache;
1600 /* Find a copy of the SYM in BCACHE. If BCACHE has never seen this
1601 symbol before, add a copy to BCACHE. In either case, return a pointer
1602 to BCACHE's copy of the symbol. If optional ADDED is not NULL, return
1603 1 in case of new entry or 0 if returning an old entry. */
1605 static const struct partial_symbol *
1606 psymbol_bcache_full (struct partial_symbol *sym,
1607 struct psymbol_bcache *bcache,
1610 return ((const struct partial_symbol *)
1611 bcache_full (sym, sizeof (struct partial_symbol), bcache->bcache,
1615 /* Helper function, initialises partial symbol structure and stashes
1616 it into objfile's bcache. Note that our caching mechanism will
1617 use all fields of struct partial_symbol to determine hash value of the
1618 structure. In other words, having two symbols with the same name but
1619 different domain (or address) is possible and correct. */
1621 static const struct partial_symbol *
1622 add_psymbol_to_bcache (const char *name, int namelength, int copy_name,
1624 enum address_class theclass,
1626 enum language language, struct objfile *objfile,
1629 struct partial_symbol psymbol;
1631 /* We must ensure that the entire struct has been zeroed before
1632 assigning to it, because an assignment may not touch some of the
1634 memset (&psymbol, 0, sizeof (psymbol));
1636 SYMBOL_VALUE_ADDRESS (&psymbol) = coreaddr;
1637 SYMBOL_SECTION (&psymbol) = -1;
1638 SYMBOL_SET_LANGUAGE (&psymbol, language, &objfile->objfile_obstack);
1639 PSYMBOL_DOMAIN (&psymbol) = domain;
1640 PSYMBOL_CLASS (&psymbol) = theclass;
1642 SYMBOL_SET_NAMES (&psymbol, name, namelength, copy_name, objfile);
1644 /* Stash the partial symbol away in the cache. */
1645 return psymbol_bcache_full (&psymbol, objfile->psymbol_cache, added);
1648 /* Increase the space allocated for LISTP, which is probably
1649 global_psymbols or static_psymbols. This space will eventually
1650 be freed in free_objfile(). */
1653 extend_psymbol_list (struct psymbol_allocation_list *listp,
1654 struct objfile *objfile)
1658 if (listp->size == 0)
1661 listp->list = XNEWVEC (struct partial_symbol *, new_size);
1665 new_size = listp->size * 2;
1666 listp->list = (struct partial_symbol **)
1667 xrealloc ((char *) listp->list,
1668 new_size * sizeof (struct partial_symbol *));
1670 /* Next assumes we only went one over. Should be good if
1671 program works correctly. */
1672 listp->next = listp->list + listp->size;
1673 listp->size = new_size;
1676 /* Helper function, adds partial symbol to the given partial symbol list. */
1679 append_psymbol_to_list (struct psymbol_allocation_list *list,
1680 const struct partial_symbol *psym,
1681 struct objfile *objfile)
1683 if (list->next >= list->list + list->size)
1684 extend_psymbol_list (list, objfile);
1685 *list->next++ = (struct partial_symbol *) psym;
1686 OBJSTAT (objfile, n_psyms++);
1689 /* Add a symbol with a long value to a psymtab.
1690 Since one arg is a struct, we pass in a ptr and deref it (sigh).
1691 The only value we need to store for psyms is an address.
1692 For all other psyms pass zero for COREADDR.
1693 Return the partial symbol that has been added. */
1696 add_psymbol_to_list (const char *name, int namelength, int copy_name,
1698 enum address_class theclass,
1699 struct psymbol_allocation_list *list,
1701 enum language language, struct objfile *objfile)
1703 const struct partial_symbol *psym;
1707 /* Stash the partial symbol away in the cache. */
1708 psym = add_psymbol_to_bcache (name, namelength, copy_name, domain, theclass,
1709 coreaddr, language, objfile, &added);
1711 /* Do not duplicate global partial symbols. */
1712 if (list == &objfile->global_psymbols
1716 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1717 append_psymbol_to_list (list, psym, objfile);
1720 /* Initialize storage for partial symbols. */
1723 init_psymbol_list (struct objfile *objfile, int total_symbols)
1725 /* Free any previously allocated psymbol lists. */
1727 if (objfile->global_psymbols.list)
1728 xfree (objfile->global_psymbols.list);
1729 if (objfile->static_psymbols.list)
1730 xfree (objfile->static_psymbols.list);
1732 /* Current best guess is that approximately a twentieth
1733 of the total symbols (in a debugging file) are global or static
1734 oriented symbols, then multiply that by slop factor of two. */
1736 objfile->global_psymbols.size = total_symbols / 10;
1737 objfile->static_psymbols.size = total_symbols / 10;
1739 if (objfile->global_psymbols.size > 0)
1741 objfile->global_psymbols.next =
1742 objfile->global_psymbols.list =
1743 XNEWVEC (struct partial_symbol *, objfile->global_psymbols.size);
1745 if (objfile->static_psymbols.size > 0)
1747 objfile->static_psymbols.next =
1748 objfile->static_psymbols.list =
1749 XNEWVEC (struct partial_symbol *, objfile->static_psymbols.size);
1753 struct partial_symtab *
1754 allocate_psymtab (const char *filename, struct objfile *objfile)
1756 struct partial_symtab *psymtab;
1758 if (objfile->free_psymtabs)
1760 psymtab = objfile->free_psymtabs;
1761 objfile->free_psymtabs = psymtab->next;
1764 psymtab = (struct partial_symtab *)
1765 obstack_alloc (&objfile->objfile_obstack,
1766 sizeof (struct partial_symtab));
1768 memset (psymtab, 0, sizeof (struct partial_symtab));
1770 = (const char *) bcache (filename, strlen (filename) + 1,
1771 objfile->per_bfd->filename_cache);
1772 psymtab->compunit_symtab = NULL;
1774 /* Prepend it to the psymtab list for the objfile it belongs to.
1775 Psymtabs are searched in most recent inserted -> least recent
1778 psymtab->next = objfile->psymtabs;
1779 objfile->psymtabs = psymtab;
1781 if (symtab_create_debug)
1783 /* Be a bit clever with debugging messages, and don't print objfile
1784 every time, only when it changes. */
1785 static char *last_objfile_name = NULL;
1787 if (last_objfile_name == NULL
1788 || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
1790 xfree (last_objfile_name);
1791 last_objfile_name = xstrdup (objfile_name (objfile));
1792 fprintf_unfiltered (gdb_stdlog,
1793 "Creating one or more psymtabs for objfile %s ...\n",
1796 fprintf_unfiltered (gdb_stdlog,
1797 "Created psymtab %s for module %s.\n",
1798 host_address_to_string (psymtab), filename);
1805 discard_psymtab (struct objfile *objfile, struct partial_symtab *pst)
1807 struct partial_symtab **prev_pst;
1810 Empty psymtabs happen as a result of header files which don't
1811 have any symbols in them. There can be a lot of them. But this
1812 check is wrong, in that a psymtab with N_SLINE entries but
1813 nothing else is not empty, but we don't realize that. Fixing
1814 that without slowing things down might be tricky. */
1816 /* First, snip it out of the psymtab chain. */
1818 prev_pst = &(objfile->psymtabs);
1819 while ((*prev_pst) != pst)
1820 prev_pst = &((*prev_pst)->next);
1821 (*prev_pst) = pst->next;
1823 /* Next, put it on a free list for recycling. */
1825 pst->next = objfile->free_psymtabs;
1826 objfile->free_psymtabs = pst;
1831 /* We need to pass a couple of items to the addrmap_foreach function,
1834 struct dump_psymtab_addrmap_data
1836 struct objfile *objfile;
1837 struct partial_symtab *psymtab;
1838 struct ui_file *outfile;
1840 /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1841 If so, we want to print the next one as well (since the next addrmap
1842 entry defines the end of the range). */
1843 int previous_matched;
1846 /* Helper function for dump_psymtab_addrmap to print an addrmap entry. */
1849 dump_psymtab_addrmap_1 (void *datap, CORE_ADDR start_addr, void *obj)
1851 struct dump_psymtab_addrmap_data *data
1852 = (struct dump_psymtab_addrmap_data *) datap;
1853 struct gdbarch *gdbarch = get_objfile_arch (data->objfile);
1854 struct partial_symtab *addrmap_psymtab = (struct partial_symtab *) obj;
1855 const char *psymtab_address_or_end = NULL;
1859 if (data->psymtab == NULL
1860 || data->psymtab == addrmap_psymtab)
1861 psymtab_address_or_end = host_address_to_string (addrmap_psymtab);
1862 else if (data->previous_matched)
1863 psymtab_address_or_end = "<ends here>";
1865 if (data->psymtab == NULL
1866 || data->psymtab == addrmap_psymtab
1867 || data->previous_matched)
1869 fprintf_filtered (data->outfile, " %s%s %s\n",
1870 data->psymtab != NULL ? " " : "",
1871 paddress (gdbarch, start_addr),
1872 psymtab_address_or_end);
1875 data->previous_matched = (data->psymtab == NULL
1876 || data->psymtab == addrmap_psymtab);
1881 /* Helper function for maintenance_print_psymbols to print the addrmap
1882 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1885 dump_psymtab_addrmap (struct objfile *objfile, struct partial_symtab *psymtab,
1886 struct ui_file *outfile)
1888 struct dump_psymtab_addrmap_data addrmap_dump_data;
1890 if ((psymtab == NULL
1891 || psymtab->psymtabs_addrmap_supported)
1892 && objfile->psymtabs_addrmap != NULL)
1894 addrmap_dump_data.objfile = objfile;
1895 addrmap_dump_data.psymtab = psymtab;
1896 addrmap_dump_data.outfile = outfile;
1897 addrmap_dump_data.previous_matched = 0;
1898 fprintf_filtered (outfile, "%sddress map:\n",
1899 psymtab == NULL ? "Entire a" : " A");
1900 addrmap_foreach (objfile->psymtabs_addrmap, dump_psymtab_addrmap_1,
1901 &addrmap_dump_data);
1906 maintenance_print_psymbols (char *args, int from_tty)
1909 struct ui_file *outfile = gdb_stdout;
1910 struct cleanup *cleanups;
1911 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1912 struct objfile *objfile;
1913 struct partial_symtab *ps;
1914 int i, outfile_idx, found;
1916 struct obj_section *section = NULL;
1920 argv = gdb_buildargv (args);
1921 cleanups = make_cleanup_freeargv (argv);
1923 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1925 if (strcmp (argv[i], "-pc") == 0)
1927 if (argv[i + 1] == NULL)
1928 error (_("Missing pc value"));
1929 address_arg = argv[++i];
1931 else if (strcmp (argv[i], "-source") == 0)
1933 if (argv[i + 1] == NULL)
1934 error (_("Missing source file"));
1935 source_arg = argv[++i];
1937 else if (strcmp (argv[i], "-objfile") == 0)
1939 if (argv[i + 1] == NULL)
1940 error (_("Missing objfile name"));
1941 objfile_arg = argv[++i];
1943 else if (strcmp (argv[i], "--") == 0)
1945 /* End of options. */
1949 else if (argv[i][0] == '-')
1951 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1952 error (_("Unknown option: %s"), argv[i]);
1959 if (address_arg != NULL && source_arg != NULL)
1960 error (_("Must specify at most one of -pc and -source"));
1962 stdio_file arg_outfile;
1964 if (argv != NULL && argv[outfile_idx] != NULL)
1968 if (argv[outfile_idx + 1] != NULL)
1969 error (_("Junk at end of command"));
1970 outfile_name = tilde_expand (argv[outfile_idx]);
1971 make_cleanup (xfree, outfile_name);
1972 if (!arg_outfile.open (outfile_name, FOPEN_WT))
1973 perror_with_name (outfile_name);
1974 outfile = &arg_outfile;
1977 if (address_arg != NULL)
1979 pc = parse_and_eval_address (address_arg);
1980 /* If we fail to find a section, that's ok, try the lookup anyway. */
1981 section = find_pc_section (pc);
1985 ALL_OBJFILES (objfile)
1987 int printed_objfile_header = 0;
1988 int print_for_objfile = 1;
1991 if (objfile_arg != NULL)
1993 = compare_filenames_for_search (objfile_name (objfile),
1995 if (!print_for_objfile)
1998 if (address_arg != NULL)
2000 struct bound_minimal_symbol msymbol = { NULL, NULL };
2002 /* We don't assume each pc has a unique objfile (this is for
2004 ps = find_pc_sect_psymtab (objfile, pc, section, msymbol);
2007 if (!printed_objfile_header)
2009 outfile->printf ("\nPartial symtabs for objfile %s\n",
2010 objfile_name (objfile));
2011 printed_objfile_header = 1;
2013 dump_psymtab (objfile, ps, outfile);
2014 dump_psymtab_addrmap (objfile, ps, outfile);
2020 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
2022 int print_for_source = 0;
2025 if (source_arg != NULL)
2028 = compare_filenames_for_search (ps->filename, source_arg);
2031 if (source_arg == NULL
2032 || print_for_source)
2034 if (!printed_objfile_header)
2036 outfile->printf ("\nPartial symtabs for objfile %s\n",
2037 objfile_name (objfile));
2038 printed_objfile_header = 1;
2040 dump_psymtab (objfile, ps, outfile);
2041 dump_psymtab_addrmap (objfile, ps, outfile);
2046 /* If we're printing all the objfile's symbols dump the full addrmap. */
2048 if (address_arg == NULL
2049 && source_arg == NULL
2050 && objfile->psymtabs_addrmap != NULL)
2052 outfile->puts ("\n");
2053 dump_psymtab_addrmap (objfile, NULL, outfile);
2059 if (address_arg != NULL)
2060 error (_("No partial symtab for address: %s"), address_arg);
2061 if (source_arg != NULL)
2062 error (_("No partial symtab for source file: %s"), source_arg);
2065 do_cleanups (cleanups);
2068 /* List all the partial symbol tables whose names match REGEXP (optional). */
2071 maintenance_info_psymtabs (char *regexp, int from_tty)
2073 struct program_space *pspace;
2074 struct objfile *objfile;
2079 ALL_PSPACES (pspace)
2080 ALL_PSPACE_OBJFILES (pspace, objfile)
2082 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2083 struct partial_symtab *psymtab;
2085 /* We don't want to print anything for this objfile until we
2086 actually find a symtab whose name matches. */
2087 int printed_objfile_start = 0;
2089 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
2094 || re_exec (psymtab->filename))
2096 if (! printed_objfile_start)
2098 printf_filtered ("{ objfile %s ", objfile_name (objfile));
2100 printf_filtered ("((struct objfile *) %s)\n",
2101 host_address_to_string (objfile));
2102 printed_objfile_start = 1;
2105 printf_filtered (" { psymtab %s ", psymtab->filename);
2107 printf_filtered ("((struct partial_symtab *) %s)\n",
2108 host_address_to_string (psymtab));
2110 printf_filtered (" readin %s\n",
2111 psymtab->readin ? "yes" : "no");
2112 printf_filtered (" fullname %s\n",
2114 ? psymtab->fullname : "(null)");
2115 printf_filtered (" text addresses ");
2116 fputs_filtered (paddress (gdbarch, psymtab->textlow),
2118 printf_filtered (" -- ");
2119 fputs_filtered (paddress (gdbarch, psymtab->texthigh),
2121 printf_filtered ("\n");
2122 printf_filtered (" psymtabs_addrmap_supported %s\n",
2123 (psymtab->psymtabs_addrmap_supported
2125 printf_filtered (" globals ");
2126 if (psymtab->n_global_syms)
2128 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2129 host_address_to_string (objfile->global_psymbols.list
2130 + psymtab->globals_offset),
2131 psymtab->n_global_syms);
2134 printf_filtered ("(none)\n");
2135 printf_filtered (" statics ");
2136 if (psymtab->n_static_syms)
2138 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2139 host_address_to_string (objfile->static_psymbols.list
2140 + psymtab->statics_offset),
2141 psymtab->n_static_syms);
2144 printf_filtered ("(none)\n");
2145 printf_filtered (" dependencies ");
2146 if (psymtab->number_of_dependencies)
2150 printf_filtered ("{\n");
2151 for (i = 0; i < psymtab->number_of_dependencies; i++)
2153 struct partial_symtab *dep = psymtab->dependencies[i];
2155 /* Note the string concatenation there --- no comma. */
2156 printf_filtered (" psymtab %s "
2157 "((struct partial_symtab *) %s)\n",
2159 host_address_to_string (dep));
2161 printf_filtered (" }\n");
2164 printf_filtered ("(none)\n");
2165 printf_filtered (" }\n");
2169 if (printed_objfile_start)
2170 printf_filtered ("}\n");
2174 /* Check consistency of currently expanded psymtabs vs symtabs. */
2177 maintenance_check_psymtabs (char *ignore, int from_tty)
2180 struct partial_symbol **psym;
2181 struct compunit_symtab *cust = NULL;
2182 struct partial_symtab *ps;
2183 const struct blockvector *bv;
2184 struct objfile *objfile;
2188 ALL_PSYMTABS (objfile, ps)
2190 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2192 /* We don't call psymtab_to_symtab here because that may cause symtab
2193 expansion. When debugging a problem it helps if checkers leave
2194 things unchanged. */
2195 cust = ps->compunit_symtab;
2197 /* First do some checks that don't require the associated symtab. */
2198 if (ps->texthigh < ps->textlow)
2200 printf_filtered ("Psymtab ");
2201 puts_filtered (ps->filename);
2202 printf_filtered (" covers bad range ");
2203 fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
2204 printf_filtered (" - ");
2205 fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
2206 printf_filtered ("\n");
2210 /* Now do checks requiring the associated symtab. */
2213 bv = COMPUNIT_BLOCKVECTOR (cust);
2214 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
2215 psym = objfile->static_psymbols.list + ps->statics_offset;
2216 length = ps->n_static_syms;
2219 sym = block_lookup_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
2220 SYMBOL_DOMAIN (*psym));
2223 printf_filtered ("Static symbol `");
2224 puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
2225 printf_filtered ("' only found in ");
2226 puts_filtered (ps->filename);
2227 printf_filtered (" psymtab\n");
2231 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2232 psym = objfile->global_psymbols.list + ps->globals_offset;
2233 length = ps->n_global_syms;
2236 sym = block_lookup_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
2237 SYMBOL_DOMAIN (*psym));
2240 printf_filtered ("Global symbol `");
2241 puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
2242 printf_filtered ("' only found in ");
2243 puts_filtered (ps->filename);
2244 printf_filtered (" psymtab\n");
2248 if (ps->texthigh != 0
2249 && (ps->textlow < BLOCK_START (b) || ps->texthigh > BLOCK_END (b)))
2251 printf_filtered ("Psymtab ");
2252 puts_filtered (ps->filename);
2253 printf_filtered (" covers ");
2254 fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
2255 printf_filtered (" - ");
2256 fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
2257 printf_filtered (" but symtab covers only ");
2258 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2259 printf_filtered (" - ");
2260 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2261 printf_filtered ("\n");
2268 extern initialize_file_ftype _initialize_psymtab;
2271 _initialize_psymtab (void)
2273 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2274 Print dump of current partial symbol definitions.\n\
2275 Usage: mt print psymbols [-objfile objfile] [-pc address] [--] [outfile]\n\
2276 mt print psymbols [-objfile objfile] [-source source] [--] [outfile]\n\
2277 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2278 or the terminal if OUTFILE is unspecified.\n\
2279 If ADDRESS is provided, dump only the file for that address.\n\
2280 If SOURCE is provided, dump only that file's symbols.\n\
2281 If OBJFILE is provided, dump only that file's minimal symbols."),
2282 &maintenanceprintlist);
2284 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2285 List the partial symbol tables for all object files.\n\
2286 This does not include information about individual partial symbols,\n\
2287 just the symbol table structures themselves."),
2288 &maintenanceinfolist);
2290 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2292 Check consistency of currently expanded psymtabs versus symtabs."),