1 /* Partial symbol tables.
3 Copyright (C) 2009-2015 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"
40 #define DEV_TTY "/dev/tty"
45 struct bcache *bcache;
48 static struct partial_symbol *match_partial_symbol (struct objfile *,
49 struct partial_symtab *,
51 const char *, domain_enum,
52 symbol_compare_ftype *,
53 symbol_compare_ftype *);
55 static struct partial_symbol *lookup_partial_symbol (struct objfile *,
56 struct partial_symtab *,
60 static const char *psymtab_to_fullname (struct partial_symtab *ps);
62 static struct partial_symbol *find_pc_sect_psymbol (struct objfile *,
63 struct partial_symtab *,
65 struct obj_section *);
67 static void fixup_psymbol_section (struct partial_symbol *psym,
68 struct objfile *objfile);
70 static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
71 struct partial_symtab *pst);
73 /* Ensure that the partial symbols for OBJFILE have been loaded. This
74 function always returns its argument, as a convenience. */
77 require_partial_symbols (struct objfile *objfile, int verbose)
79 if ((objfile->flags & OBJF_PSYMTABS_READ) == 0)
81 objfile->flags |= OBJF_PSYMTABS_READ;
83 if (objfile->sf->sym_read_psymbols)
87 printf_unfiltered (_("Reading symbols from %s..."),
88 objfile_name (objfile));
89 gdb_flush (gdb_stdout);
91 (*objfile->sf->sym_read_psymbols) (objfile);
94 if (!objfile_has_symbols (objfile))
97 printf_unfiltered (_("(no debugging symbols found)..."));
101 printf_unfiltered (_("done.\n"));
109 /* Traverse all psymtabs in one objfile, requiring that the psymtabs
112 #define ALL_OBJFILE_PSYMTABS_REQUIRED(objfile, p) \
113 for ((p) = require_partial_symbols (objfile, 1)->psymtabs; \
117 /* We want to make sure this file always requires psymtabs. */
119 #undef ALL_OBJFILE_PSYMTABS
121 /* Traverse all psymtabs in all objfiles. */
123 #define ALL_PSYMTABS(objfile, p) \
124 ALL_OBJFILES (objfile) \
125 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
127 /* Helper function for psym_map_symtabs_matching_filename that
128 expands the symtabs and calls the iterator. */
131 partial_map_expand_apply (struct objfile *objfile,
133 const char *real_path,
134 struct partial_symtab *pst,
135 int (*callback) (struct symtab *, void *),
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, callback, data,
153 objfile->compunit_symtabs, last_made);
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 (struct objfile *objfile,
162 const char *real_path,
163 int (*callback) (struct symtab *,
167 struct partial_symtab *pst;
168 const char *name_basename = lbasename (name);
170 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
172 /* We can skip shared psymtabs here, because any file name will be
173 attached to the unshared psymtab. */
174 if (pst->user != NULL)
177 /* Anonymous psymtabs don't have a file name. */
181 if (compare_filenames_for_search (pst->filename, name))
183 if (partial_map_expand_apply (objfile, name, real_path,
184 pst, callback, data))
189 /* Before we invoke realpath, which can get expensive when many
190 files are involved, do a quick comparison of the basenames. */
191 if (! basenames_may_differ
192 && FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
195 if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
197 if (partial_map_expand_apply (objfile, name, real_path,
198 pst, callback, data))
203 /* If the user gave us an absolute path, try to find the file in
204 this symtab and use its absolute path. */
205 if (real_path != NULL)
207 gdb_assert (IS_ABSOLUTE_PATH (real_path));
208 gdb_assert (IS_ABSOLUTE_PATH (name));
209 if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
211 if (partial_map_expand_apply (objfile, name, real_path,
212 pst, callback, data))
222 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
223 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
225 static struct partial_symtab *
226 find_pc_sect_psymtab_closer (struct objfile *objfile,
227 CORE_ADDR pc, struct obj_section *section,
228 struct partial_symtab *pst,
229 struct bound_minimal_symbol msymbol)
231 struct partial_symtab *tpst;
232 struct partial_symtab *best_pst = pst;
233 CORE_ADDR best_addr = pst->textlow;
235 gdb_assert (!pst->psymtabs_addrmap_supported);
237 /* An objfile that has its functions reordered might have
238 many partial symbol tables containing the PC, but
239 we want the partial symbol table that contains the
240 function containing the PC. */
241 if (!(objfile->flags & OBJF_REORDERED)
242 && section == NULL) /* Can't validate section this way. */
245 if (msymbol.minsym == NULL)
248 /* The code range of partial symtabs sometimes overlap, so, in
249 the loop below, we need to check all partial symtabs and
250 find the one that fits better for the given PC address. We
251 select the partial symtab that contains a symbol whose
252 address is closest to the PC address. By closest we mean
253 that find_pc_sect_symbol returns the symbol with address
254 that is closest and still less than the given PC. */
255 for (tpst = pst; tpst != NULL; tpst = tpst->next)
257 if (pc >= tpst->textlow && pc < tpst->texthigh)
259 struct partial_symbol *p;
262 /* NOTE: This assumes that every psymbol has a
263 corresponding msymbol, which is not necessarily
264 true; the debug info might be much richer than the
265 object's symbol table. */
266 p = find_pc_sect_psymbol (objfile, tpst, pc, section);
268 && (SYMBOL_VALUE_ADDRESS (p)
269 == BMSYMBOL_VALUE_ADDRESS (msymbol)))
272 /* Also accept the textlow value of a psymtab as a
273 "symbol", to provide some support for partial
274 symbol tables with line information but no debug
275 symbols (e.g. those produced by an assembler). */
277 this_addr = SYMBOL_VALUE_ADDRESS (p);
279 this_addr = tpst->textlow;
281 /* Check whether it is closer than our current
282 BEST_ADDR. Since this symbol address is
283 necessarily lower or equal to PC, the symbol closer
284 to PC is the symbol which address is the highest.
285 This way we return the psymtab which contains such
286 best match symbol. This can help in cases where the
287 symbol information/debuginfo is not complete, like
288 for instance on IRIX6 with gcc, where no debug info
289 is emitted for statics. (See also the nodebug.exp
291 if (this_addr > best_addr)
293 best_addr = this_addr;
301 /* Find which partial symtab contains PC and SECTION. Return NULL if
302 none. We return the psymtab that contains a symbol whose address
303 exactly matches PC, or, if we cannot find an exact match, the
304 psymtab that contains a symbol whose address is closest to PC. */
306 static struct partial_symtab *
307 find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
308 struct obj_section *section,
309 struct bound_minimal_symbol msymbol)
311 struct partial_symtab *pst;
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 = addrmap_find (objfile->psymtabs_addrmap, pc);
321 /* FIXME: addrmaps currently do not handle overlayed sections,
322 so fall back to the non-addrmap case if we're debugging
323 overlays and the addrmap returned the wrong section. */
324 if (overlay_debugging && msymbol.minsym != NULL && section != NULL)
326 struct partial_symbol *p;
328 /* NOTE: This assumes that every psymbol has a
329 corresponding msymbol, which is not necessarily
330 true; the debug info might be much richer than the
331 object's symbol table. */
332 p = find_pc_sect_psymbol (objfile, pst, pc, section);
334 || (SYMBOL_VALUE_ADDRESS (p)
335 != BMSYMBOL_VALUE_ADDRESS (msymbol)))
339 /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
340 PSYMTABS_ADDRMAP we used has already the best 1-byte
341 granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
342 a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
351 /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
352 which still have no corresponding full SYMTABs read. But it is not
353 present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
356 /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
357 its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
358 debug info type in single OBJFILE. */
360 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
361 if (!pst->psymtabs_addrmap_supported
362 && pc >= pst->textlow && pc < pst->texthigh)
364 struct partial_symtab *best_pst;
366 best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
368 if (best_pst != NULL)
375 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
376 the definition of quick_symbol_functions in symfile.h. */
378 static struct compunit_symtab *
379 psym_find_pc_sect_compunit_symtab (struct objfile *objfile,
380 struct bound_minimal_symbol msymbol,
382 struct obj_section *section,
385 struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc, section,
389 if (warn_if_readin && ps->readin)
390 /* Might want to error() here (in case symtab is corrupt and
391 will cause a core dump), but maybe we can successfully
392 continue, so let's not. */
394 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
395 paddress (get_objfile_arch (objfile), pc));
396 psymtab_to_symtab (objfile, ps);
397 return ps->compunit_symtab;
402 /* Find which partial symbol within a psymtab matches PC and SECTION.
403 Return NULL if none. */
405 static struct partial_symbol *
406 find_pc_sect_psymbol (struct objfile *objfile,
407 struct partial_symtab *psymtab, CORE_ADDR pc,
408 struct obj_section *section)
410 struct partial_symbol *best = NULL, *p, **pp;
413 gdb_assert (psymtab != NULL);
415 /* Cope with programs that start at address 0. */
416 best_pc = (psymtab->textlow != 0) ? psymtab->textlow - 1 : 0;
418 /* Search the global symbols as well as the static symbols, so that
419 find_pc_partial_function doesn't use a minimal symbol and thus
420 cache a bad endaddr. */
421 for (pp = objfile->global_psymbols.list + psymtab->globals_offset;
422 (pp - (objfile->global_psymbols.list + psymtab->globals_offset)
423 < psymtab->n_global_syms);
427 if (SYMBOL_DOMAIN (p) == VAR_DOMAIN
428 && PSYMBOL_CLASS (p) == LOC_BLOCK
429 && pc >= SYMBOL_VALUE_ADDRESS (p)
430 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
431 || (psymtab->textlow == 0
432 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
434 if (section != NULL) /* Match on a specific section. */
436 fixup_psymbol_section (p, objfile);
437 if (!matching_obj_sections (SYMBOL_OBJ_SECTION (objfile, p),
441 best_pc = SYMBOL_VALUE_ADDRESS (p);
446 for (pp = objfile->static_psymbols.list + psymtab->statics_offset;
447 (pp - (objfile->static_psymbols.list + psymtab->statics_offset)
448 < psymtab->n_static_syms);
452 if (SYMBOL_DOMAIN (p) == VAR_DOMAIN
453 && PSYMBOL_CLASS (p) == LOC_BLOCK
454 && pc >= SYMBOL_VALUE_ADDRESS (p)
455 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
456 || (psymtab->textlow == 0
457 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
459 if (section != NULL) /* Match on a specific section. */
461 fixup_psymbol_section (p, objfile);
462 if (!matching_obj_sections (SYMBOL_OBJ_SECTION (objfile, p),
466 best_pc = SYMBOL_VALUE_ADDRESS (p);
475 fixup_psymbol_section (struct partial_symbol *psym, struct objfile *objfile)
482 if (SYMBOL_SECTION (psym) >= 0)
485 gdb_assert (objfile);
487 switch (PSYMBOL_CLASS (psym))
492 addr = SYMBOL_VALUE_ADDRESS (psym);
495 /* Nothing else will be listed in the minsyms -- no use looking
500 fixup_section (&psym->ginfo, addr, objfile);
503 /* Psymtab version of lookup_symbol. See its definition in
504 the definition of quick_symbol_functions in symfile.h. */
506 static struct compunit_symtab *
507 psym_lookup_symbol (struct objfile *objfile,
508 int block_index, const char *name,
509 const domain_enum domain)
511 struct partial_symtab *ps;
512 const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
513 struct compunit_symtab *stab_best = NULL;
515 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
517 if (!ps->readin && lookup_partial_symbol (objfile, ps, name,
518 psymtab_index, domain))
520 struct symbol *sym, *with_opaque = NULL;
521 struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
522 /* Note: While psymtab_to_symtab can return NULL if the partial symtab
523 is empty, we can assume it won't here because lookup_partial_symbol
525 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
526 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
528 sym = block_find_symbol (block, name, domain,
529 block_find_non_opaque_type_preferred,
532 /* Some caution must be observed with overloaded functions
533 and methods, since the index will not contain any overload
534 information (but NAME might contain it). */
537 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
539 if (with_opaque != NULL
540 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
543 /* Keep looking through other psymtabs. */
550 /* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
551 the global block of PST if GLOBAL, and otherwise the static block.
552 MATCH is the comparison operation that returns true iff MATCH (s,
553 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
554 non-null, the symbols in the block are assumed to be ordered
555 according to it (allowing binary search). It must be compatible
556 with MATCH. Returns the symbol, if found, and otherwise NULL. */
558 static struct partial_symbol *
559 match_partial_symbol (struct objfile *objfile,
560 struct partial_symtab *pst, int global,
561 const char *name, domain_enum domain,
562 symbol_compare_ftype *match,
563 symbol_compare_ftype *ordered_compare)
565 struct partial_symbol **start, **psym;
566 struct partial_symbol **top, **real_top, **bottom, **center;
567 int length = (global ? pst->n_global_syms : pst->n_static_syms);
568 int do_linear_search = 1;
573 objfile->global_psymbols.list + pst->globals_offset :
574 objfile->static_psymbols.list + pst->statics_offset);
576 if (global && ordered_compare) /* Can use a binary search. */
578 do_linear_search = 0;
580 /* Binary search. This search is guaranteed to end with center
581 pointing at the earliest partial symbol whose name might be
582 correct. At that point *all* partial symbols with an
583 appropriate name will be checked against the correct
587 top = start + length - 1;
591 center = bottom + (top - bottom) / 2;
592 gdb_assert (center < top);
593 if (!do_linear_search
594 && (SYMBOL_LANGUAGE (*center) == language_java))
595 do_linear_search = 1;
596 if (ordered_compare (SYMBOL_SEARCH_NAME (*center), name) >= 0)
601 gdb_assert (top == bottom);
603 while (top <= real_top
604 && match (SYMBOL_SEARCH_NAME (*top), name) == 0)
606 if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
607 SYMBOL_DOMAIN (*top), domain))
613 /* Can't use a binary search or else we found during the binary search that
614 we should also do a linear search. */
616 if (do_linear_search)
618 for (psym = start; psym < start + length; psym++)
620 if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
621 SYMBOL_DOMAIN (*psym), domain)
622 && match (SYMBOL_SEARCH_NAME (*psym), name) == 0)
630 /* Returns the name used to search psymtabs. Unlike symtabs, psymtabs do
631 not contain any method/function instance information (since this would
632 force reading type information while reading psymtabs). Therefore,
633 if NAME contains overload information, it must be stripped before searching
636 The caller is responsible for freeing the return result. */
639 psymtab_search_name (const char *name)
641 switch (current_language->la_language)
646 if (strchr (name, '('))
648 char *ret = cp_remove_params (name);
660 return xstrdup (name);
663 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
664 Check the global symbols if GLOBAL, the static symbols if not. */
666 static struct partial_symbol *
667 lookup_partial_symbol (struct objfile *objfile,
668 struct partial_symtab *pst, const char *name,
669 int global, domain_enum domain)
671 struct partial_symbol **start, **psym;
672 struct partial_symbol **top, **real_top, **bottom, **center;
673 int length = (global ? pst->n_global_syms : pst->n_static_syms);
674 int do_linear_search = 1;
676 struct cleanup *cleanup;
681 search_name = psymtab_search_name (name);
682 cleanup = make_cleanup (xfree, search_name);
684 objfile->global_psymbols.list + pst->globals_offset :
685 objfile->static_psymbols.list + pst->statics_offset);
687 if (global) /* This means we can use a binary search. */
689 do_linear_search = 0;
691 /* Binary search. This search is guaranteed to end with center
692 pointing at the earliest partial symbol whose name might be
693 correct. At that point *all* partial symbols with an
694 appropriate name will be checked against the correct
698 top = start + length - 1;
702 center = bottom + (top - bottom) / 2;
704 internal_error (__FILE__, __LINE__,
705 _("failed internal consistency check"));
706 if (!do_linear_search
707 && SYMBOL_LANGUAGE (*center) == language_java)
709 do_linear_search = 1;
711 if (strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*center),
721 if (!(top == bottom))
722 internal_error (__FILE__, __LINE__,
723 _("failed internal consistency check"));
725 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
726 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
727 while (top >= start && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name))
730 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
733 while (top <= real_top && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name))
735 if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
736 SYMBOL_DOMAIN (*top), domain))
738 do_cleanups (cleanup);
745 /* Can't use a binary search or else we found during the binary search that
746 we should also do a linear search. */
748 if (do_linear_search)
750 for (psym = start; psym < start + length; psym++)
752 if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
753 SYMBOL_DOMAIN (*psym), domain)
754 && SYMBOL_MATCHES_SEARCH_NAME (*psym, search_name))
756 do_cleanups (cleanup);
762 do_cleanups (cleanup);
766 /* Get the symbol table that corresponds to a partial_symtab.
767 This is fast after the first time you do it.
768 The result will be NULL if the primary symtab has no symbols,
769 which can happen. Otherwise the result is the primary symtab
770 that contains PST. */
772 static struct compunit_symtab *
773 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
775 /* If it is a shared psymtab, find an unshared psymtab that includes
776 it. Any such psymtab will do. */
777 while (pst->user != NULL)
780 /* If it's been looked up before, return it. */
781 if (pst->compunit_symtab)
782 return pst->compunit_symtab;
784 /* If it has not yet been read in, read it. */
787 struct cleanup *back_to = increment_reading_symtab ();
789 (*pst->read_symtab) (pst, objfile);
790 do_cleanups (back_to);
793 return pst->compunit_symtab;
796 /* Psymtab version of relocate. See its definition in
797 the definition of quick_symbol_functions in symfile.h. */
800 psym_relocate (struct objfile *objfile,
801 const struct section_offsets *new_offsets,
802 const struct section_offsets *delta)
804 struct partial_symbol **psym;
805 struct partial_symtab *p;
807 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
809 p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
810 p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
813 for (psym = objfile->global_psymbols.list;
814 psym < objfile->global_psymbols.next;
817 fixup_psymbol_section (*psym, objfile);
818 if (SYMBOL_SECTION (*psym) >= 0)
819 SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
820 SYMBOL_SECTION (*psym));
822 for (psym = objfile->static_psymbols.list;
823 psym < objfile->static_psymbols.next;
826 fixup_psymbol_section (*psym, objfile);
827 if (SYMBOL_SECTION (*psym) >= 0)
828 SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
829 SYMBOL_SECTION (*psym));
833 /* Psymtab version of find_last_source_symtab. See its definition in
834 the definition of quick_symbol_functions in symfile.h. */
836 static struct symtab *
837 psym_find_last_source_symtab (struct objfile *ofp)
839 struct partial_symtab *ps;
840 struct partial_symtab *cs_pst = NULL;
842 ALL_OBJFILE_PSYMTABS_REQUIRED (ofp, ps)
844 const char *name = ps->filename;
845 int len = strlen (name);
847 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
848 || strcmp (name, "<<C++-namespaces>>") == 0)))
856 internal_error (__FILE__, __LINE__,
857 _("select_source_symtab: "
858 "readin pst found and no symtabs."));
862 struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
866 return compunit_primary_filetab (cust);
872 /* Psymtab version of forget_cached_source_info. See its definition in
873 the definition of quick_symbol_functions in symfile.h. */
876 psym_forget_cached_source_info (struct objfile *objfile)
878 struct partial_symtab *pst;
880 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
882 if (pst->fullname != NULL)
884 xfree (pst->fullname);
885 pst->fullname = NULL;
891 print_partial_symbols (struct gdbarch *gdbarch,
892 struct partial_symbol **p, int count, char *what,
893 struct ui_file *outfile)
895 fprintf_filtered (outfile, " %s partial symbols:\n", what);
899 fprintf_filtered (outfile, " `%s'", SYMBOL_LINKAGE_NAME (*p));
900 if (SYMBOL_DEMANGLED_NAME (*p) != NULL)
902 fprintf_filtered (outfile, " `%s'", SYMBOL_DEMANGLED_NAME (*p));
904 fputs_filtered (", ", outfile);
905 switch (SYMBOL_DOMAIN (*p))
908 fputs_filtered ("undefined domain, ", outfile);
911 /* This is the usual thing -- don't print it. */
914 fputs_filtered ("struct domain, ", outfile);
917 fputs_filtered ("label domain, ", outfile);
920 fputs_filtered ("<invalid domain>, ", outfile);
923 switch (PSYMBOL_CLASS (*p))
926 fputs_filtered ("undefined", outfile);
929 fputs_filtered ("constant int", outfile);
932 fputs_filtered ("static", outfile);
935 fputs_filtered ("register", outfile);
938 fputs_filtered ("pass by value", outfile);
941 fputs_filtered ("pass by reference", outfile);
943 case LOC_REGPARM_ADDR:
944 fputs_filtered ("register address parameter", outfile);
947 fputs_filtered ("stack parameter", outfile);
950 fputs_filtered ("type", outfile);
953 fputs_filtered ("label", outfile);
956 fputs_filtered ("function", outfile);
958 case LOC_CONST_BYTES:
959 fputs_filtered ("constant bytes", outfile);
962 fputs_filtered ("unresolved", outfile);
964 case LOC_OPTIMIZED_OUT:
965 fputs_filtered ("optimized out", outfile);
968 fputs_filtered ("computed at runtime", outfile);
971 fputs_filtered ("<invalid location>", outfile);
974 fputs_filtered (", ", outfile);
975 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (*p)), outfile);
976 fprintf_filtered (outfile, "\n");
982 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
983 struct ui_file *outfile)
985 struct gdbarch *gdbarch = get_objfile_arch (objfile);
988 if (psymtab->anonymous)
990 fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
995 fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
998 fprintf_filtered (outfile, "(object ");
999 gdb_print_host_address (psymtab, outfile);
1000 fprintf_filtered (outfile, ")\n\n");
1001 fprintf_unfiltered (outfile, " Read from object file %s (",
1002 objfile_name (objfile));
1003 gdb_print_host_address (objfile, outfile);
1004 fprintf_unfiltered (outfile, ")\n");
1006 if (psymtab->readin)
1008 fprintf_filtered (outfile,
1009 " Full symtab was read (at ");
1010 gdb_print_host_address (psymtab->compunit_symtab, outfile);
1011 fprintf_filtered (outfile, " by function at ");
1012 gdb_print_host_address (psymtab->read_symtab, outfile);
1013 fprintf_filtered (outfile, ")\n");
1016 fprintf_filtered (outfile, " Symbols cover text addresses ");
1017 fputs_filtered (paddress (gdbarch, psymtab->textlow), outfile);
1018 fprintf_filtered (outfile, "-");
1019 fputs_filtered (paddress (gdbarch, psymtab->texthigh), outfile);
1020 fprintf_filtered (outfile, "\n");
1021 fprintf_filtered (outfile, " Address map supported - %s.\n",
1022 psymtab->psymtabs_addrmap_supported ? "yes" : "no");
1023 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
1024 psymtab->number_of_dependencies);
1025 for (i = 0; i < psymtab->number_of_dependencies; i++)
1027 fprintf_filtered (outfile, " %d ", i);
1028 gdb_print_host_address (psymtab->dependencies[i], outfile);
1029 fprintf_filtered (outfile, " %s\n",
1030 psymtab->dependencies[i]->filename);
1032 if (psymtab->user != NULL)
1034 fprintf_filtered (outfile, " Shared partial symtab with user ");
1035 gdb_print_host_address (psymtab->user, outfile);
1036 fprintf_filtered (outfile, "\n");
1038 if (psymtab->n_global_syms > 0)
1040 print_partial_symbols (gdbarch,
1041 objfile->global_psymbols.list
1042 + psymtab->globals_offset,
1043 psymtab->n_global_syms, "Global", outfile);
1045 if (psymtab->n_static_syms > 0)
1047 print_partial_symbols (gdbarch,
1048 objfile->static_psymbols.list
1049 + psymtab->statics_offset,
1050 psymtab->n_static_syms, "Static", outfile);
1052 fprintf_filtered (outfile, "\n");
1055 /* Psymtab version of print_stats. See its definition in
1056 the definition of quick_symbol_functions in symfile.h. */
1059 psym_print_stats (struct objfile *objfile)
1062 struct partial_symtab *ps;
1065 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1067 if (ps->readin == 0)
1070 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i);
1073 /* Psymtab version of dump. See its definition in
1074 the definition of quick_symbol_functions in symfile.h. */
1077 psym_dump (struct objfile *objfile)
1079 struct partial_symtab *psymtab;
1081 if (objfile->psymtabs)
1083 printf_filtered ("Psymtabs:\n");
1084 for (psymtab = objfile->psymtabs;
1086 psymtab = psymtab->next)
1088 printf_filtered ("%s at ",
1090 gdb_print_host_address (psymtab, gdb_stdout);
1091 printf_filtered (", ");
1094 printf_filtered ("\n\n");
1098 /* Psymtab version of expand_symtabs_for_function. See its definition in
1099 the definition of quick_symbol_functions in symfile.h. */
1102 psym_expand_symtabs_for_function (struct objfile *objfile,
1103 const char *func_name)
1105 struct partial_symtab *ps;
1107 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1112 if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
1114 || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
1116 psymtab_to_symtab (objfile, ps);
1120 /* Psymtab version of expand_all_symtabs. See its definition in
1121 the definition of quick_symbol_functions in symfile.h. */
1124 psym_expand_all_symtabs (struct objfile *objfile)
1126 struct partial_symtab *psymtab;
1128 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
1130 psymtab_to_symtab (objfile, psymtab);
1134 /* Psymtab version of expand_symtabs_with_fullname. See its definition in
1135 the definition of quick_symbol_functions in symfile.h. */
1138 psym_expand_symtabs_with_fullname (struct objfile *objfile,
1139 const char *fullname)
1141 struct partial_symtab *p;
1143 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
1145 /* Anonymous psymtabs don't have a name of a source file. */
1149 /* psymtab_to_fullname tries to open the file which is slow.
1150 Don't call it if we know the basenames don't match. */
1151 if ((basenames_may_differ
1152 || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1153 && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
1154 psymtab_to_symtab (objfile, p);
1158 /* Psymtab version of map_symbol_filenames. See its definition in
1159 the definition of quick_symbol_functions in symfile.h. */
1162 psym_map_symbol_filenames (struct objfile *objfile,
1163 symbol_filename_ftype *fun, void *data,
1166 struct partial_symtab *ps;
1168 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1170 const char *fullname;
1175 /* We can skip shared psymtabs here, because any file name will be
1176 attached to the unshared psymtab. */
1177 if (ps->user != NULL)
1180 /* Anonymous psymtabs don't have a file name. */
1186 fullname = psymtab_to_fullname (ps);
1189 (*fun) (ps->filename, fullname, data);
1193 /* Finds the fullname that a partial_symtab represents.
1195 If this functions finds the fullname, it will save it in ps->fullname
1196 and it will also return the value.
1198 If this function fails to find the file that this partial_symtab represents,
1199 NULL will be returned and ps->fullname will be set to NULL. */
1202 psymtab_to_fullname (struct partial_symtab *ps)
1204 gdb_assert (!ps->anonymous);
1206 /* Use cached copy if we have it.
1207 We rely on forget_cached_source_info being called appropriately
1208 to handle cases like the file being moved. */
1209 if (ps->fullname == NULL)
1211 int fd = find_and_open_source (ps->filename, ps->dirname, &ps->fullname);
1218 struct cleanup *back_to;
1220 /* rewrite_source_path would be applied by find_and_open_source, we
1221 should report the pathname where GDB tried to find the file. */
1223 if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
1224 fullname = xstrdup (ps->filename);
1226 fullname = concat (ps->dirname, SLASH_STRING, ps->filename, NULL);
1228 back_to = make_cleanup (xfree, fullname);
1229 ps->fullname = rewrite_source_path (fullname);
1230 if (ps->fullname == NULL)
1231 ps->fullname = xstrdup (fullname);
1232 do_cleanups (back_to);
1236 return ps->fullname;
1239 /* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1240 according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1241 BLOCK is assumed to come from OBJFILE. Returns 1 iff CALLBACK
1242 ever returns non-zero, and otherwise returns 0. */
1245 map_block (const char *name, domain_enum domain, struct objfile *objfile,
1246 struct block *block,
1247 int (*callback) (struct block *, struct symbol *, void *),
1248 void *data, symbol_compare_ftype *match)
1250 struct block_iterator iter;
1253 for (sym = block_iter_match_first (block, name, match, &iter);
1254 sym != NULL; sym = block_iter_match_next (name, match, &iter))
1256 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
1257 SYMBOL_DOMAIN (sym), domain))
1259 if (callback (block, sym, data))
1267 /* Psymtab version of map_matching_symbols. See its definition in
1268 the definition of quick_symbol_functions in symfile.h. */
1271 psym_map_matching_symbols (struct objfile *objfile,
1272 const char *name, domain_enum domain,
1274 int (*callback) (struct block *,
1275 struct symbol *, void *),
1277 symbol_compare_ftype *match,
1278 symbol_compare_ftype *ordered_compare)
1280 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
1281 struct partial_symtab *ps;
1283 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1287 || match_partial_symbol (objfile, ps, global, name, domain, match,
1290 struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
1291 struct block *block;
1295 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
1296 if (map_block (name, domain, objfile, block,
1297 callback, data, match))
1299 if (callback (block, NULL, data))
1305 /* A helper for psym_expand_symtabs_matching that handles
1306 searching included psymtabs. This returns 1 if a symbol is found,
1307 and zero otherwise. It also updates the 'searched_flag' on the
1308 various psymtabs that it searches. */
1311 recursively_search_psymtabs (struct partial_symtab *ps,
1312 struct objfile *objfile,
1313 enum search_domain kind,
1314 expand_symtabs_symbol_matcher_ftype *sym_matcher,
1317 struct partial_symbol **psym;
1318 struct partial_symbol **bound, **gbound, **sbound;
1320 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
1323 if (ps->searched_flag != PST_NOT_SEARCHED)
1324 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1326 /* Recurse into shared psymtabs first, because they may have already
1327 been searched, and this could save some time. */
1328 for (i = 0; i < ps->number_of_dependencies; ++i)
1332 /* Skip non-shared dependencies, these are handled elsewhere. */
1333 if (ps->dependencies[i]->user == NULL)
1336 r = recursively_search_psymtabs (ps->dependencies[i],
1337 objfile, kind, sym_matcher, data);
1340 ps->searched_flag = PST_SEARCHED_AND_FOUND;
1345 gbound = (objfile->global_psymbols.list
1346 + ps->globals_offset + ps->n_global_syms);
1347 sbound = (objfile->static_psymbols.list
1348 + ps->statics_offset + ps->n_static_syms);
1351 /* Go through all of the symbols stored in a partial
1352 symtab in one loop. */
1353 psym = objfile->global_psymbols.list + ps->globals_offset;
1358 if (bound == gbound && ps->n_static_syms != 0)
1360 psym = objfile->static_psymbols.list + ps->statics_offset;
1371 if ((kind == ALL_DOMAIN
1372 || (kind == VARIABLES_DOMAIN
1373 && PSYMBOL_CLASS (*psym) != LOC_TYPEDEF
1374 && PSYMBOL_CLASS (*psym) != LOC_BLOCK)
1375 || (kind == FUNCTIONS_DOMAIN
1376 && PSYMBOL_CLASS (*psym) == LOC_BLOCK)
1377 || (kind == TYPES_DOMAIN
1378 && PSYMBOL_CLASS (*psym) == LOC_TYPEDEF))
1379 && (*sym_matcher) (SYMBOL_SEARCH_NAME (*psym), data))
1381 /* Found a match, so notify our caller. */
1382 result = PST_SEARCHED_AND_FOUND;
1389 ps->searched_flag = result;
1390 return result == PST_SEARCHED_AND_FOUND;
1393 /* Psymtab version of expand_symtabs_matching. See its definition in
1394 the definition of quick_symbol_functions in symfile.h. */
1397 psym_expand_symtabs_matching
1398 (struct objfile *objfile,
1399 expand_symtabs_file_matcher_ftype *file_matcher,
1400 expand_symtabs_symbol_matcher_ftype *symbol_matcher,
1401 expand_symtabs_exp_notify_ftype *expansion_notify,
1402 enum search_domain kind,
1405 struct partial_symtab *ps;
1407 /* Clear the search flags. */
1408 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1410 ps->searched_flag = PST_NOT_SEARCHED;
1413 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1420 /* We skip shared psymtabs because file-matching doesn't apply
1421 to them; but we search them later in the loop. */
1422 if (ps->user != NULL)
1432 match = (*file_matcher) (ps->filename, data, 0);
1435 /* Before we invoke realpath, which can get expensive when many
1436 files are involved, do a quick comparison of the basenames. */
1437 if (basenames_may_differ
1438 || (*file_matcher) (lbasename (ps->filename), data, 1))
1439 match = (*file_matcher) (psymtab_to_fullname (ps), data, 0);
1445 if (recursively_search_psymtabs (ps, objfile, kind, symbol_matcher, data))
1447 struct compunit_symtab *symtab =
1448 psymtab_to_symtab (objfile, ps);
1450 if (expansion_notify != NULL)
1451 expansion_notify (symtab, data);
1456 /* Psymtab version of has_symbols. See its definition in
1457 the definition of quick_symbol_functions in symfile.h. */
1460 psym_has_symbols (struct objfile *objfile)
1462 return objfile->psymtabs != NULL;
1465 const struct quick_symbol_functions psym_functions =
1468 psym_find_last_source_symtab,
1469 psym_forget_cached_source_info,
1470 psym_map_symtabs_matching_filename,
1475 psym_expand_symtabs_for_function,
1476 psym_expand_all_symtabs,
1477 psym_expand_symtabs_with_fullname,
1478 psym_map_matching_symbols,
1479 psym_expand_symtabs_matching,
1480 psym_find_pc_sect_compunit_symtab,
1481 psym_map_symbol_filenames
1486 /* This compares two partial symbols by names, using strcmp_iw_ordered
1487 for the comparison. */
1490 compare_psymbols (const void *s1p, const void *s2p)
1492 struct partial_symbol *const *s1 = s1p;
1493 struct partial_symbol *const *s2 = s2p;
1495 return strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*s1),
1496 SYMBOL_SEARCH_NAME (*s2));
1500 sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
1502 /* Sort the global list; don't sort the static list. */
1504 qsort (objfile->global_psymbols.list + pst->globals_offset,
1505 pst->n_global_syms, sizeof (struct partial_symbol *),
1509 /* Allocate and partially fill a partial symtab. It will be
1510 completely filled at the end of the symbol list.
1512 FILENAME is the name of the symbol-file we are reading from. */
1514 struct partial_symtab *
1515 start_psymtab_common (struct objfile *objfile,
1516 const char *filename,
1517 CORE_ADDR textlow, struct partial_symbol **global_syms,
1518 struct partial_symbol **static_syms)
1520 struct partial_symtab *psymtab;
1522 psymtab = allocate_psymtab (filename, objfile);
1523 psymtab->textlow = textlow;
1524 psymtab->texthigh = psymtab->textlow; /* default */
1525 psymtab->globals_offset = global_syms - objfile->global_psymbols.list;
1526 psymtab->statics_offset = static_syms - objfile->static_psymbols.list;
1530 /* Perform "finishing up" operations of a partial symtab. */
1533 end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1536 = objfile->global_psymbols.next - (objfile->global_psymbols.list
1537 + pst->globals_offset);
1539 = objfile->static_psymbols.next - (objfile->static_psymbols.list
1540 + pst->statics_offset);
1542 sort_pst_symbols (objfile, pst);
1545 /* Calculate a hash code for the given partial symbol. The hash is
1546 calculated using the symbol's value, language, domain, class
1547 and name. These are the values which are set by
1548 add_psymbol_to_bcache. */
1550 static unsigned long
1551 psymbol_hash (const void *addr, int length)
1553 unsigned long h = 0;
1554 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1555 unsigned int lang = psymbol->ginfo.language;
1556 unsigned int domain = PSYMBOL_DOMAIN (psymbol);
1557 unsigned int theclass = PSYMBOL_CLASS (psymbol);
1559 h = hash_continue (&psymbol->ginfo.value, sizeof (psymbol->ginfo.value), h);
1560 h = hash_continue (&lang, sizeof (unsigned int), h);
1561 h = hash_continue (&domain, sizeof (unsigned int), h);
1562 h = hash_continue (&theclass, sizeof (unsigned int), h);
1563 h = hash_continue (psymbol->ginfo.name, strlen (psymbol->ginfo.name), h);
1568 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1569 For the comparison this function uses a symbols value,
1570 language, domain, class and name. */
1573 psymbol_compare (const void *addr1, const void *addr2, int length)
1575 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1576 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1578 return (memcmp (&sym1->ginfo.value, &sym1->ginfo.value,
1579 sizeof (sym1->ginfo.value)) == 0
1580 && sym1->ginfo.language == sym2->ginfo.language
1581 && PSYMBOL_DOMAIN (sym1) == PSYMBOL_DOMAIN (sym2)
1582 && PSYMBOL_CLASS (sym1) == PSYMBOL_CLASS (sym2)
1583 && sym1->ginfo.name == sym2->ginfo.name);
1586 /* Initialize a partial symbol bcache. */
1588 struct psymbol_bcache *
1589 psymbol_bcache_init (void)
1591 struct psymbol_bcache *bcache = XCNEW (struct psymbol_bcache);
1593 bcache->bcache = bcache_xmalloc (psymbol_hash, psymbol_compare);
1597 /* Free a partial symbol bcache. */
1600 psymbol_bcache_free (struct psymbol_bcache *bcache)
1605 bcache_xfree (bcache->bcache);
1609 /* Return the internal bcache of the psymbol_bcache BCACHE. */
1612 psymbol_bcache_get_bcache (struct psymbol_bcache *bcache)
1614 return bcache->bcache;
1617 /* Find a copy of the SYM in BCACHE. If BCACHE has never seen this
1618 symbol before, add a copy to BCACHE. In either case, return a pointer
1619 to BCACHE's copy of the symbol. If optional ADDED is not NULL, return
1620 1 in case of new entry or 0 if returning an old entry. */
1622 static const struct partial_symbol *
1623 psymbol_bcache_full (struct partial_symbol *sym,
1624 struct psymbol_bcache *bcache,
1627 return bcache_full (sym,
1628 sizeof (struct partial_symbol),
1633 /* Helper function, initialises partial symbol structure and stashes
1634 it into objfile's bcache. Note that our caching mechanism will
1635 use all fields of struct partial_symbol to determine hash value of the
1636 structure. In other words, having two symbols with the same name but
1637 different domain (or address) is possible and correct. */
1639 static const struct partial_symbol *
1640 add_psymbol_to_bcache (const char *name, int namelength, int copy_name,
1642 enum address_class theclass,
1643 long val, /* Value as a long */
1644 CORE_ADDR coreaddr, /* Value as a CORE_ADDR */
1645 enum language language, struct objfile *objfile,
1648 struct partial_symbol psymbol;
1650 /* We must ensure that the entire struct has been zeroed before
1651 assigning to it, because an assignment may not touch some of the
1653 memset (&psymbol, 0, sizeof (psymbol));
1655 /* val and coreaddr are mutually exclusive, one of them *will* be zero. */
1658 SYMBOL_VALUE (&psymbol) = val;
1662 SYMBOL_VALUE_ADDRESS (&psymbol) = coreaddr;
1664 SYMBOL_SECTION (&psymbol) = -1;
1665 SYMBOL_SET_LANGUAGE (&psymbol, language, &objfile->objfile_obstack);
1666 PSYMBOL_DOMAIN (&psymbol) = domain;
1667 PSYMBOL_CLASS (&psymbol) = theclass;
1669 SYMBOL_SET_NAMES (&psymbol, name, namelength, copy_name, objfile);
1671 /* Stash the partial symbol away in the cache. */
1672 return psymbol_bcache_full (&psymbol, objfile->psymbol_cache, added);
1675 /* Increase the space allocated for LISTP, which is probably
1676 global_psymbols or static_psymbols. This space will eventually
1677 be freed in free_objfile(). */
1680 extend_psymbol_list (struct psymbol_allocation_list *listp,
1681 struct objfile *objfile)
1685 if (listp->size == 0)
1688 listp->list = (struct partial_symbol **)
1689 xmalloc (new_size * sizeof (struct partial_symbol *));
1693 new_size = listp->size * 2;
1694 listp->list = (struct partial_symbol **)
1695 xrealloc ((char *) listp->list,
1696 new_size * sizeof (struct partial_symbol *));
1698 /* Next assumes we only went one over. Should be good if
1699 program works correctly. */
1700 listp->next = listp->list + listp->size;
1701 listp->size = new_size;
1704 /* Helper function, adds partial symbol to the given partial symbol list. */
1707 append_psymbol_to_list (struct psymbol_allocation_list *list,
1708 const struct partial_symbol *psym,
1709 struct objfile *objfile)
1711 if (list->next >= list->list + list->size)
1712 extend_psymbol_list (list, objfile);
1713 *list->next++ = (struct partial_symbol *) psym;
1714 OBJSTAT (objfile, n_psyms++);
1717 /* Add a symbol with a long value to a psymtab.
1718 Since one arg is a struct, we pass in a ptr and deref it (sigh).
1719 Return the partial symbol that has been added. */
1722 add_psymbol_to_list (const char *name, int namelength, int copy_name,
1724 enum address_class theclass,
1725 struct psymbol_allocation_list *list,
1726 long val, /* Value as a long */
1727 CORE_ADDR coreaddr, /* Value as a CORE_ADDR */
1728 enum language language, struct objfile *objfile)
1730 const struct partial_symbol *psym;
1734 /* Stash the partial symbol away in the cache. */
1735 psym = add_psymbol_to_bcache (name, namelength, copy_name, domain, theclass,
1736 val, coreaddr, language, objfile, &added);
1738 /* Do not duplicate global partial symbols. */
1739 if (list == &objfile->global_psymbols
1743 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1744 append_psymbol_to_list (list, psym, objfile);
1747 /* Initialize storage for partial symbols. */
1750 init_psymbol_list (struct objfile *objfile, int total_symbols)
1752 /* Free any previously allocated psymbol lists. */
1754 if (objfile->global_psymbols.list)
1755 xfree (objfile->global_psymbols.list);
1756 if (objfile->static_psymbols.list)
1757 xfree (objfile->static_psymbols.list);
1759 /* Current best guess is that approximately a twentieth
1760 of the total symbols (in a debugging file) are global or static
1761 oriented symbols, then multiply that by slop factor of two. */
1763 objfile->global_psymbols.size = total_symbols / 10;
1764 objfile->static_psymbols.size = total_symbols / 10;
1766 if (objfile->global_psymbols.size > 0)
1768 objfile->global_psymbols.next =
1769 objfile->global_psymbols.list = (struct partial_symbol **)
1770 xmalloc ((objfile->global_psymbols.size
1771 * sizeof (struct partial_symbol *)));
1773 if (objfile->static_psymbols.size > 0)
1775 objfile->static_psymbols.next =
1776 objfile->static_psymbols.list = (struct partial_symbol **)
1777 xmalloc ((objfile->static_psymbols.size
1778 * sizeof (struct partial_symbol *)));
1782 struct partial_symtab *
1783 allocate_psymtab (const char *filename, struct objfile *objfile)
1785 struct partial_symtab *psymtab;
1787 if (objfile->free_psymtabs)
1789 psymtab = objfile->free_psymtabs;
1790 objfile->free_psymtabs = psymtab->next;
1793 psymtab = (struct partial_symtab *)
1794 obstack_alloc (&objfile->objfile_obstack,
1795 sizeof (struct partial_symtab));
1797 memset (psymtab, 0, sizeof (struct partial_symtab));
1798 psymtab->filename = bcache (filename, strlen (filename) + 1,
1799 objfile->per_bfd->filename_cache);
1800 psymtab->compunit_symtab = NULL;
1802 /* Prepend it to the psymtab list for the objfile it belongs to.
1803 Psymtabs are searched in most recent inserted -> least recent
1806 psymtab->next = objfile->psymtabs;
1807 objfile->psymtabs = psymtab;
1809 if (symtab_create_debug)
1811 /* Be a bit clever with debugging messages, and don't print objfile
1812 every time, only when it changes. */
1813 static char *last_objfile_name = NULL;
1815 if (last_objfile_name == NULL
1816 || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
1818 xfree (last_objfile_name);
1819 last_objfile_name = xstrdup (objfile_name (objfile));
1820 fprintf_unfiltered (gdb_stdlog,
1821 "Creating one or more psymtabs for objfile %s ...\n",
1824 fprintf_unfiltered (gdb_stdlog,
1825 "Created psymtab %s for module %s.\n",
1826 host_address_to_string (psymtab), filename);
1833 discard_psymtab (struct objfile *objfile, struct partial_symtab *pst)
1835 struct partial_symtab **prev_pst;
1838 Empty psymtabs happen as a result of header files which don't
1839 have any symbols in them. There can be a lot of them. But this
1840 check is wrong, in that a psymtab with N_SLINE entries but
1841 nothing else is not empty, but we don't realize that. Fixing
1842 that without slowing things down might be tricky. */
1844 /* First, snip it out of the psymtab chain. */
1846 prev_pst = &(objfile->psymtabs);
1847 while ((*prev_pst) != pst)
1848 prev_pst = &((*prev_pst)->next);
1849 (*prev_pst) = pst->next;
1851 /* Next, put it on a free list for recycling. */
1853 pst->next = objfile->free_psymtabs;
1854 objfile->free_psymtabs = pst;
1857 /* An object of this type is passed to discard_psymtabs_upto. */
1859 struct psymtab_state
1861 /* The objfile where psymtabs are discarded. */
1863 struct objfile *objfile;
1865 /* The first psymtab to save. */
1867 struct partial_symtab *save;
1870 /* A cleanup function used by make_cleanup_discard_psymtabs. */
1873 discard_psymtabs_upto (void *arg)
1875 struct psymtab_state *state = arg;
1877 while (state->objfile->psymtabs != state->save)
1878 discard_psymtab (state->objfile, state->objfile->psymtabs);
1881 /* Return a new cleanup that discards all psymtabs created in OBJFILE
1882 after this function is called. */
1885 make_cleanup_discard_psymtabs (struct objfile *objfile)
1887 struct psymtab_state *state = XNEW (struct psymtab_state);
1889 state->objfile = objfile;
1890 state->save = objfile->psymtabs;
1892 return make_cleanup_dtor (discard_psymtabs_upto, state, xfree);
1898 maintenance_print_psymbols (char *args, int from_tty)
1901 struct ui_file *outfile;
1902 struct cleanup *cleanups;
1903 char *symname = NULL;
1904 char *filename = DEV_TTY;
1905 struct objfile *objfile;
1906 struct partial_symtab *ps;
1913 print-psymbols takes an output file name and optional symbol file name"));
1915 argv = gdb_buildargv (args);
1916 cleanups = make_cleanup_freeargv (argv);
1918 if (argv[0] != NULL)
1921 /* If a second arg is supplied, it is a source file name to match on. */
1922 if (argv[1] != NULL)
1928 filename = tilde_expand (filename);
1929 make_cleanup (xfree, filename);
1931 outfile = gdb_fopen (filename, FOPEN_WT);
1932 if (outfile == NULL)
1933 perror_with_name (filename);
1934 make_cleanup_ui_file_delete (outfile);
1936 ALL_PSYMTABS (objfile, ps)
1939 if (symname == NULL || filename_cmp (symname, ps->filename) == 0)
1940 dump_psymtab (objfile, ps, outfile);
1942 do_cleanups (cleanups);
1945 /* List all the partial symbol tables whose names match REGEXP (optional). */
1948 maintenance_info_psymtabs (char *regexp, int from_tty)
1950 struct program_space *pspace;
1951 struct objfile *objfile;
1956 ALL_PSPACES (pspace)
1957 ALL_PSPACE_OBJFILES (pspace, objfile)
1959 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1960 struct partial_symtab *psymtab;
1962 /* We don't want to print anything for this objfile until we
1963 actually find a symtab whose name matches. */
1964 int printed_objfile_start = 0;
1966 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
1971 || re_exec (psymtab->filename))
1973 if (! printed_objfile_start)
1975 printf_filtered ("{ objfile %s ", objfile_name (objfile));
1977 printf_filtered ("((struct objfile *) %s)\n",
1978 host_address_to_string (objfile));
1979 printed_objfile_start = 1;
1982 printf_filtered (" { psymtab %s ", psymtab->filename);
1984 printf_filtered ("((struct partial_symtab *) %s)\n",
1985 host_address_to_string (psymtab));
1987 printf_filtered (" readin %s\n",
1988 psymtab->readin ? "yes" : "no");
1989 printf_filtered (" fullname %s\n",
1991 ? psymtab->fullname : "(null)");
1992 printf_filtered (" text addresses ");
1993 fputs_filtered (paddress (gdbarch, psymtab->textlow),
1995 printf_filtered (" -- ");
1996 fputs_filtered (paddress (gdbarch, psymtab->texthigh),
1998 printf_filtered ("\n");
1999 printf_filtered (" psymtabs_addrmap_supported %s\n",
2000 (psymtab->psymtabs_addrmap_supported
2002 printf_filtered (" globals ");
2003 if (psymtab->n_global_syms)
2005 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2006 host_address_to_string (objfile->global_psymbols.list
2007 + psymtab->globals_offset),
2008 psymtab->n_global_syms);
2011 printf_filtered ("(none)\n");
2012 printf_filtered (" statics ");
2013 if (psymtab->n_static_syms)
2015 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2016 host_address_to_string (objfile->static_psymbols.list
2017 + psymtab->statics_offset),
2018 psymtab->n_static_syms);
2021 printf_filtered ("(none)\n");
2022 printf_filtered (" dependencies ");
2023 if (psymtab->number_of_dependencies)
2027 printf_filtered ("{\n");
2028 for (i = 0; i < psymtab->number_of_dependencies; i++)
2030 struct partial_symtab *dep = psymtab->dependencies[i];
2032 /* Note the string concatenation there --- no comma. */
2033 printf_filtered (" psymtab %s "
2034 "((struct partial_symtab *) %s)\n",
2036 host_address_to_string (dep));
2038 printf_filtered (" }\n");
2041 printf_filtered ("(none)\n");
2042 printf_filtered (" }\n");
2046 if (printed_objfile_start)
2047 printf_filtered ("}\n");
2051 /* Check consistency of currently expanded psymtabs vs symtabs. */
2054 maintenance_check_psymtabs (char *ignore, int from_tty)
2057 struct partial_symbol **psym;
2058 struct compunit_symtab *cust = NULL;
2059 struct partial_symtab *ps;
2060 const struct blockvector *bv;
2061 struct objfile *objfile;
2065 ALL_PSYMTABS (objfile, ps)
2067 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2069 /* We don't call psymtab_to_symtab here because that may cause symtab
2070 expansion. When debugging a problem it helps if checkers leave
2071 things unchanged. */
2072 cust = ps->compunit_symtab;
2074 /* First do some checks that don't require the associated symtab. */
2075 if (ps->texthigh < ps->textlow)
2077 printf_filtered ("Psymtab ");
2078 puts_filtered (ps->filename);
2079 printf_filtered (" covers bad range ");
2080 fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
2081 printf_filtered (" - ");
2082 fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
2083 printf_filtered ("\n");
2087 /* Now do checks requiring the associated symtab. */
2090 bv = COMPUNIT_BLOCKVECTOR (cust);
2091 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
2092 psym = objfile->static_psymbols.list + ps->statics_offset;
2093 length = ps->n_static_syms;
2096 sym = block_lookup_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
2097 SYMBOL_DOMAIN (*psym));
2100 printf_filtered ("Static symbol `");
2101 puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
2102 printf_filtered ("' only found in ");
2103 puts_filtered (ps->filename);
2104 printf_filtered (" psymtab\n");
2108 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2109 psym = objfile->global_psymbols.list + ps->globals_offset;
2110 length = ps->n_global_syms;
2113 sym = block_lookup_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
2114 SYMBOL_DOMAIN (*psym));
2117 printf_filtered ("Global symbol `");
2118 puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
2119 printf_filtered ("' only found in ");
2120 puts_filtered (ps->filename);
2121 printf_filtered (" psymtab\n");
2125 if (ps->texthigh != 0
2126 && (ps->textlow < BLOCK_START (b) || ps->texthigh > BLOCK_END (b)))
2128 printf_filtered ("Psymtab ");
2129 puts_filtered (ps->filename);
2130 printf_filtered (" covers ");
2131 fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
2132 printf_filtered (" - ");
2133 fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
2134 printf_filtered (" but symtab covers only ");
2135 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2136 printf_filtered (" - ");
2137 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2138 printf_filtered ("\n");
2145 extern initialize_file_ftype _initialize_psymtab;
2148 _initialize_psymtab (void)
2150 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2151 Print dump of current partial symbol definitions.\n\
2152 Entries in the partial symbol table are dumped to file OUTFILE.\n\
2153 If a SOURCE file is specified, dump only that file's partial symbols."),
2154 &maintenanceprintlist);
2156 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2157 List the partial symbol tables for all object files.\n\
2158 This does not include information about individual partial symbols,\n\
2159 just the symbol table structures themselves."),
2160 &maintenanceinfolist);
2162 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2164 Check consistency of currently expanded psymtabs versus symtabs."),