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 int (*callback) (struct symtab *, void *),
134 struct compunit_symtab *last_made = objfile->compunit_symtabs;
136 /* Shared psymtabs should never be seen here. Instead they should
137 be handled properly by the caller. */
138 gdb_assert (pst->user == NULL);
140 /* Don't visit already-expanded psymtabs. */
144 /* This may expand more than one symtab, and we want to iterate over
146 psymtab_to_symtab (objfile, pst);
148 return iterate_over_some_symtabs (name, real_path, callback, data,
149 objfile->compunit_symtabs, last_made);
152 /* Psymtab version of map_symtabs_matching_filename. See its definition in
153 the definition of quick_symbol_functions in symfile.h. */
156 psym_map_symtabs_matching_filename (struct objfile *objfile,
158 const char *real_path,
159 int (*callback) (struct symtab *,
163 struct partial_symtab *pst;
164 const char *name_basename = lbasename (name);
166 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
168 /* We can skip shared psymtabs here, because any file name will be
169 attached to the unshared psymtab. */
170 if (pst->user != NULL)
173 /* Anonymous psymtabs don't have a file name. */
177 if (compare_filenames_for_search (pst->filename, name))
179 if (partial_map_expand_apply (objfile, name, real_path,
180 pst, callback, data))
185 /* Before we invoke realpath, which can get expensive when many
186 files are involved, do a quick comparison of the basenames. */
187 if (! basenames_may_differ
188 && FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
191 if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
193 if (partial_map_expand_apply (objfile, name, real_path,
194 pst, callback, data))
199 /* If the user gave us an absolute path, try to find the file in
200 this symtab and use its absolute path. */
201 if (real_path != NULL)
203 gdb_assert (IS_ABSOLUTE_PATH (real_path));
204 gdb_assert (IS_ABSOLUTE_PATH (name));
205 if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
207 if (partial_map_expand_apply (objfile, name, real_path,
208 pst, callback, data))
218 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
219 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
221 static struct partial_symtab *
222 find_pc_sect_psymtab_closer (struct objfile *objfile,
223 CORE_ADDR pc, struct obj_section *section,
224 struct partial_symtab *pst,
225 struct bound_minimal_symbol msymbol)
227 struct partial_symtab *tpst;
228 struct partial_symtab *best_pst = pst;
229 CORE_ADDR best_addr = pst->textlow;
231 gdb_assert (!pst->psymtabs_addrmap_supported);
233 /* An objfile that has its functions reordered might have
234 many partial symbol tables containing the PC, but
235 we want the partial symbol table that contains the
236 function containing the PC. */
237 if (!(objfile->flags & OBJF_REORDERED)
238 && section == NULL) /* Can't validate section this way. */
241 if (msymbol.minsym == NULL)
244 /* The code range of partial symtabs sometimes overlap, so, in
245 the loop below, we need to check all partial symtabs and
246 find the one that fits better for the given PC address. We
247 select the partial symtab that contains a symbol whose
248 address is closest to the PC address. By closest we mean
249 that find_pc_sect_symbol returns the symbol with address
250 that is closest and still less than the given PC. */
251 for (tpst = pst; tpst != NULL; tpst = tpst->next)
253 if (pc >= tpst->textlow && pc < tpst->texthigh)
255 struct partial_symbol *p;
258 /* NOTE: This assumes that every psymbol has a
259 corresponding msymbol, which is not necessarily
260 true; the debug info might be much richer than the
261 object's symbol table. */
262 p = find_pc_sect_psymbol (objfile, tpst, pc, section);
264 && (SYMBOL_VALUE_ADDRESS (p)
265 == BMSYMBOL_VALUE_ADDRESS (msymbol)))
268 /* Also accept the textlow value of a psymtab as a
269 "symbol", to provide some support for partial
270 symbol tables with line information but no debug
271 symbols (e.g. those produced by an assembler). */
273 this_addr = SYMBOL_VALUE_ADDRESS (p);
275 this_addr = tpst->textlow;
277 /* Check whether it is closer than our current
278 BEST_ADDR. Since this symbol address is
279 necessarily lower or equal to PC, the symbol closer
280 to PC is the symbol which address is the highest.
281 This way we return the psymtab which contains such
282 best match symbol. This can help in cases where the
283 symbol information/debuginfo is not complete, like
284 for instance on IRIX6 with gcc, where no debug info
285 is emitted for statics. (See also the nodebug.exp
287 if (this_addr > best_addr)
289 best_addr = this_addr;
297 /* Find which partial symtab contains PC and SECTION. Return NULL if
298 none. We return the psymtab that contains a symbol whose address
299 exactly matches PC, or, if we cannot find an exact match, the
300 psymtab that contains a symbol whose address is closest to PC. */
302 static struct partial_symtab *
303 find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
304 struct obj_section *section,
305 struct bound_minimal_symbol msymbol)
307 struct partial_symtab *pst;
309 /* Try just the PSYMTABS_ADDRMAP mapping first as it has better granularity
310 than the later used TEXTLOW/TEXTHIGH one. */
312 if (objfile->psymtabs_addrmap != NULL)
314 pst = ((struct partial_symtab *)
315 addrmap_find (objfile->psymtabs_addrmap, pc));
318 /* FIXME: addrmaps currently do not handle overlayed sections,
319 so fall back to the non-addrmap case if we're debugging
320 overlays and the addrmap returned the wrong section. */
321 if (overlay_debugging && msymbol.minsym != NULL && section != NULL)
323 struct partial_symbol *p;
325 /* NOTE: This assumes that every psymbol has a
326 corresponding msymbol, which is not necessarily
327 true; the debug info might be much richer than the
328 object's symbol table. */
329 p = find_pc_sect_psymbol (objfile, pst, pc, section);
331 || (SYMBOL_VALUE_ADDRESS (p)
332 != BMSYMBOL_VALUE_ADDRESS (msymbol)))
336 /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
337 PSYMTABS_ADDRMAP we used has already the best 1-byte
338 granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
339 a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
348 /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
349 which still have no corresponding full SYMTABs read. But it is not
350 present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
353 /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
354 its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
355 debug info type in single OBJFILE. */
357 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
358 if (!pst->psymtabs_addrmap_supported
359 && pc >= pst->textlow && pc < pst->texthigh)
361 struct partial_symtab *best_pst;
363 best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
365 if (best_pst != NULL)
372 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
373 the definition of quick_symbol_functions in symfile.h. */
375 static struct compunit_symtab *
376 psym_find_pc_sect_compunit_symtab (struct objfile *objfile,
377 struct bound_minimal_symbol msymbol,
379 struct obj_section *section,
382 struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc, section,
386 if (warn_if_readin && ps->readin)
387 /* Might want to error() here (in case symtab is corrupt and
388 will cause a core dump), but maybe we can successfully
389 continue, so let's not. */
391 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
392 paddress (get_objfile_arch (objfile), pc));
393 psymtab_to_symtab (objfile, ps);
394 return ps->compunit_symtab;
399 /* Find which partial symbol within a psymtab matches PC and SECTION.
400 Return NULL if none. */
402 static struct partial_symbol *
403 find_pc_sect_psymbol (struct objfile *objfile,
404 struct partial_symtab *psymtab, CORE_ADDR pc,
405 struct obj_section *section)
407 struct partial_symbol *best = NULL, *p, **pp;
410 gdb_assert (psymtab != NULL);
412 /* Cope with programs that start at address 0. */
413 best_pc = (psymtab->textlow != 0) ? psymtab->textlow - 1 : 0;
415 /* Search the global symbols as well as the static symbols, so that
416 find_pc_partial_function doesn't use a minimal symbol and thus
417 cache a bad endaddr. */
418 for (pp = objfile->global_psymbols.list + psymtab->globals_offset;
419 (pp - (objfile->global_psymbols.list + psymtab->globals_offset)
420 < psymtab->n_global_syms);
424 if (SYMBOL_DOMAIN (p) == VAR_DOMAIN
425 && PSYMBOL_CLASS (p) == LOC_BLOCK
426 && pc >= SYMBOL_VALUE_ADDRESS (p)
427 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
428 || (psymtab->textlow == 0
429 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
431 if (section != NULL) /* Match on a specific section. */
433 fixup_psymbol_section (p, objfile);
434 if (!matching_obj_sections (SYMBOL_OBJ_SECTION (objfile, p),
438 best_pc = SYMBOL_VALUE_ADDRESS (p);
443 for (pp = objfile->static_psymbols.list + psymtab->statics_offset;
444 (pp - (objfile->static_psymbols.list + psymtab->statics_offset)
445 < psymtab->n_static_syms);
449 if (SYMBOL_DOMAIN (p) == VAR_DOMAIN
450 && PSYMBOL_CLASS (p) == LOC_BLOCK
451 && pc >= SYMBOL_VALUE_ADDRESS (p)
452 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
453 || (psymtab->textlow == 0
454 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
456 if (section != NULL) /* Match on a specific section. */
458 fixup_psymbol_section (p, objfile);
459 if (!matching_obj_sections (SYMBOL_OBJ_SECTION (objfile, p),
463 best_pc = SYMBOL_VALUE_ADDRESS (p);
472 fixup_psymbol_section (struct partial_symbol *psym, struct objfile *objfile)
479 if (SYMBOL_SECTION (psym) >= 0)
482 gdb_assert (objfile);
484 switch (PSYMBOL_CLASS (psym))
489 addr = SYMBOL_VALUE_ADDRESS (psym);
492 /* Nothing else will be listed in the minsyms -- no use looking
497 fixup_section (&psym->ginfo, addr, objfile);
500 /* Psymtab version of lookup_symbol. See its definition in
501 the definition of quick_symbol_functions in symfile.h. */
503 static struct compunit_symtab *
504 psym_lookup_symbol (struct objfile *objfile,
505 int block_index, const char *name,
506 const domain_enum domain)
508 struct partial_symtab *ps;
509 const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
510 struct compunit_symtab *stab_best = NULL;
512 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
514 if (!ps->readin && lookup_partial_symbol (objfile, ps, name,
515 psymtab_index, domain))
517 struct symbol *sym, *with_opaque = NULL;
518 struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
519 /* Note: While psymtab_to_symtab can return NULL if the partial symtab
520 is empty, we can assume it won't here because lookup_partial_symbol
522 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
523 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
525 sym = block_find_symbol (block, name, domain,
526 block_find_non_opaque_type_preferred,
529 /* Some caution must be observed with overloaded functions
530 and methods, since the index will not contain any overload
531 information (but NAME might contain it). */
534 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
536 if (with_opaque != NULL
537 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
540 /* Keep looking through other psymtabs. */
547 /* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
548 the global block of PST if GLOBAL, and otherwise the static block.
549 MATCH is the comparison operation that returns true iff MATCH (s,
550 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
551 non-null, the symbols in the block are assumed to be ordered
552 according to it (allowing binary search). It must be compatible
553 with MATCH. Returns the symbol, if found, and otherwise NULL. */
555 static struct partial_symbol *
556 match_partial_symbol (struct objfile *objfile,
557 struct partial_symtab *pst, int global,
558 const char *name, domain_enum domain,
559 symbol_compare_ftype *match,
560 symbol_compare_ftype *ordered_compare)
562 struct partial_symbol **start, **psym;
563 struct partial_symbol **top, **real_top, **bottom, **center;
564 int length = (global ? pst->n_global_syms : pst->n_static_syms);
565 int do_linear_search = 1;
570 objfile->global_psymbols.list + pst->globals_offset :
571 objfile->static_psymbols.list + pst->statics_offset);
573 if (global && ordered_compare) /* Can use a binary search. */
575 do_linear_search = 0;
577 /* Binary search. This search is guaranteed to end with center
578 pointing at the earliest partial symbol whose name might be
579 correct. At that point *all* partial symbols with an
580 appropriate name will be checked against the correct
584 top = start + length - 1;
588 center = bottom + (top - bottom) / 2;
589 gdb_assert (center < top);
590 if (ordered_compare (SYMBOL_SEARCH_NAME (*center), name) >= 0)
595 gdb_assert (top == bottom);
597 while (top <= real_top
598 && match (SYMBOL_SEARCH_NAME (*top), name) == 0)
600 if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
601 SYMBOL_DOMAIN (*top), domain))
607 /* Can't use a binary search or else we found during the binary search that
608 we should also do a linear search. */
610 if (do_linear_search)
612 for (psym = start; psym < start + length; psym++)
614 if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
615 SYMBOL_DOMAIN (*psym), domain)
616 && match (SYMBOL_SEARCH_NAME (*psym), name) == 0)
624 /* Returns the name used to search psymtabs. Unlike symtabs, psymtabs do
625 not contain any method/function instance information (since this would
626 force reading type information while reading psymtabs). Therefore,
627 if NAME contains overload information, it must be stripped before searching
630 The caller is responsible for freeing the return result. */
633 psymtab_search_name (const char *name)
635 switch (current_language->la_language)
639 if (strchr (name, '('))
641 char *ret = cp_remove_params (name);
653 return xstrdup (name);
656 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
657 Check the global symbols if GLOBAL, the static symbols if not. */
659 static struct partial_symbol *
660 lookup_partial_symbol (struct objfile *objfile,
661 struct partial_symtab *pst, const char *name,
662 int global, domain_enum domain)
664 struct partial_symbol **start, **psym;
665 struct partial_symbol **top, **real_top, **bottom, **center;
666 int length = (global ? pst->n_global_syms : pst->n_static_syms);
667 int do_linear_search = 1;
669 struct cleanup *cleanup;
674 search_name = psymtab_search_name (name);
675 cleanup = make_cleanup (xfree, search_name);
677 objfile->global_psymbols.list + pst->globals_offset :
678 objfile->static_psymbols.list + pst->statics_offset);
680 if (global) /* This means we can use a binary search. */
682 do_linear_search = 0;
684 /* Binary search. This search is guaranteed to end with center
685 pointing at the earliest partial symbol whose name might be
686 correct. At that point *all* partial symbols with an
687 appropriate name will be checked against the correct
691 top = start + length - 1;
695 center = bottom + (top - bottom) / 2;
697 internal_error (__FILE__, __LINE__,
698 _("failed internal consistency check"));
699 if (strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*center),
709 if (!(top == bottom))
710 internal_error (__FILE__, __LINE__,
711 _("failed internal consistency check"));
713 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
714 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
715 while (top >= start && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name))
718 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
721 while (top <= real_top && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name))
723 if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
724 SYMBOL_DOMAIN (*top), domain))
726 do_cleanups (cleanup);
733 /* Can't use a binary search or else we found during the binary search that
734 we should also do a linear search. */
736 if (do_linear_search)
738 for (psym = start; psym < start + length; psym++)
740 if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
741 SYMBOL_DOMAIN (*psym), domain)
742 && SYMBOL_MATCHES_SEARCH_NAME (*psym, search_name))
744 do_cleanups (cleanup);
750 do_cleanups (cleanup);
754 /* Get the symbol table that corresponds to a partial_symtab.
755 This is fast after the first time you do it.
756 The result will be NULL if the primary symtab has no symbols,
757 which can happen. Otherwise the result is the primary symtab
758 that contains PST. */
760 static struct compunit_symtab *
761 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
763 /* If it is a shared psymtab, find an unshared psymtab that includes
764 it. Any such psymtab will do. */
765 while (pst->user != NULL)
768 /* If it's been looked up before, return it. */
769 if (pst->compunit_symtab)
770 return pst->compunit_symtab;
772 /* If it has not yet been read in, read it. */
775 struct cleanup *back_to = increment_reading_symtab ();
777 (*pst->read_symtab) (pst, objfile);
778 do_cleanups (back_to);
781 return pst->compunit_symtab;
784 /* Psymtab version of relocate. See its definition in
785 the definition of quick_symbol_functions in symfile.h. */
788 psym_relocate (struct objfile *objfile,
789 const struct section_offsets *new_offsets,
790 const struct section_offsets *delta)
792 struct partial_symbol **psym;
793 struct partial_symtab *p;
795 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
797 p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
798 p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
801 for (psym = objfile->global_psymbols.list;
802 psym < objfile->global_psymbols.next;
805 fixup_psymbol_section (*psym, objfile);
806 if (SYMBOL_SECTION (*psym) >= 0)
807 SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
808 SYMBOL_SECTION (*psym));
810 for (psym = objfile->static_psymbols.list;
811 psym < objfile->static_psymbols.next;
814 fixup_psymbol_section (*psym, objfile);
815 if (SYMBOL_SECTION (*psym) >= 0)
816 SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
817 SYMBOL_SECTION (*psym));
821 /* Psymtab version of find_last_source_symtab. See its definition in
822 the definition of quick_symbol_functions in symfile.h. */
824 static struct symtab *
825 psym_find_last_source_symtab (struct objfile *ofp)
827 struct partial_symtab *ps;
828 struct partial_symtab *cs_pst = NULL;
830 ALL_OBJFILE_PSYMTABS_REQUIRED (ofp, ps)
832 const char *name = ps->filename;
833 int len = strlen (name);
835 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
836 || strcmp (name, "<<C++-namespaces>>") == 0)))
844 internal_error (__FILE__, __LINE__,
845 _("select_source_symtab: "
846 "readin pst found and no symtabs."));
850 struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
854 return compunit_primary_filetab (cust);
860 /* Psymtab version of forget_cached_source_info. See its definition in
861 the definition of quick_symbol_functions in symfile.h. */
864 psym_forget_cached_source_info (struct objfile *objfile)
866 struct partial_symtab *pst;
868 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
870 if (pst->fullname != NULL)
872 xfree (pst->fullname);
873 pst->fullname = NULL;
879 print_partial_symbols (struct gdbarch *gdbarch,
880 struct partial_symbol **p, int count, char *what,
881 struct ui_file *outfile)
883 fprintf_filtered (outfile, " %s partial symbols:\n", what);
887 fprintf_filtered (outfile, " `%s'", SYMBOL_LINKAGE_NAME (*p));
888 if (SYMBOL_DEMANGLED_NAME (*p) != NULL)
890 fprintf_filtered (outfile, " `%s'", SYMBOL_DEMANGLED_NAME (*p));
892 fputs_filtered (", ", outfile);
893 switch (SYMBOL_DOMAIN (*p))
896 fputs_filtered ("undefined domain, ", outfile);
899 /* This is the usual thing -- don't print it. */
902 fputs_filtered ("struct domain, ", outfile);
905 fputs_filtered ("label domain, ", outfile);
908 fputs_filtered ("<invalid domain>, ", outfile);
911 switch (PSYMBOL_CLASS (*p))
914 fputs_filtered ("undefined", outfile);
917 fputs_filtered ("constant int", outfile);
920 fputs_filtered ("static", outfile);
923 fputs_filtered ("register", outfile);
926 fputs_filtered ("pass by value", outfile);
929 fputs_filtered ("pass by reference", outfile);
931 case LOC_REGPARM_ADDR:
932 fputs_filtered ("register address parameter", outfile);
935 fputs_filtered ("stack parameter", outfile);
938 fputs_filtered ("type", outfile);
941 fputs_filtered ("label", outfile);
944 fputs_filtered ("function", outfile);
946 case LOC_CONST_BYTES:
947 fputs_filtered ("constant bytes", outfile);
950 fputs_filtered ("unresolved", outfile);
952 case LOC_OPTIMIZED_OUT:
953 fputs_filtered ("optimized out", outfile);
956 fputs_filtered ("computed at runtime", outfile);
959 fputs_filtered ("<invalid location>", outfile);
962 fputs_filtered (", ", outfile);
963 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (*p)), outfile);
964 fprintf_filtered (outfile, "\n");
970 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
971 struct ui_file *outfile)
973 struct gdbarch *gdbarch = get_objfile_arch (objfile);
976 if (psymtab->anonymous)
978 fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
983 fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
986 fprintf_filtered (outfile, "(object ");
987 gdb_print_host_address (psymtab, outfile);
988 fprintf_filtered (outfile, ")\n\n");
989 fprintf_unfiltered (outfile, " Read from object file %s (",
990 objfile_name (objfile));
991 gdb_print_host_address (objfile, outfile);
992 fprintf_unfiltered (outfile, ")\n");
996 fprintf_filtered (outfile,
997 " Full symtab was read (at ");
998 gdb_print_host_address (psymtab->compunit_symtab, outfile);
999 fprintf_filtered (outfile, " by function at ");
1000 gdb_print_host_address (psymtab->read_symtab, outfile);
1001 fprintf_filtered (outfile, ")\n");
1004 fprintf_filtered (outfile, " Symbols cover text addresses ");
1005 fputs_filtered (paddress (gdbarch, psymtab->textlow), outfile);
1006 fprintf_filtered (outfile, "-");
1007 fputs_filtered (paddress (gdbarch, psymtab->texthigh), outfile);
1008 fprintf_filtered (outfile, "\n");
1009 fprintf_filtered (outfile, " Address map supported - %s.\n",
1010 psymtab->psymtabs_addrmap_supported ? "yes" : "no");
1011 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
1012 psymtab->number_of_dependencies);
1013 for (i = 0; i < psymtab->number_of_dependencies; i++)
1015 fprintf_filtered (outfile, " %d ", i);
1016 gdb_print_host_address (psymtab->dependencies[i], outfile);
1017 fprintf_filtered (outfile, " %s\n",
1018 psymtab->dependencies[i]->filename);
1020 if (psymtab->user != NULL)
1022 fprintf_filtered (outfile, " Shared partial symtab with user ");
1023 gdb_print_host_address (psymtab->user, outfile);
1024 fprintf_filtered (outfile, "\n");
1026 if (psymtab->n_global_syms > 0)
1028 print_partial_symbols (gdbarch,
1029 objfile->global_psymbols.list
1030 + psymtab->globals_offset,
1031 psymtab->n_global_syms, "Global", outfile);
1033 if (psymtab->n_static_syms > 0)
1035 print_partial_symbols (gdbarch,
1036 objfile->static_psymbols.list
1037 + psymtab->statics_offset,
1038 psymtab->n_static_syms, "Static", outfile);
1040 fprintf_filtered (outfile, "\n");
1043 /* Psymtab version of print_stats. See its definition in
1044 the definition of quick_symbol_functions in symfile.h. */
1047 psym_print_stats (struct objfile *objfile)
1050 struct partial_symtab *ps;
1053 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1055 if (ps->readin == 0)
1058 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i);
1061 /* Psymtab version of dump. See its definition in
1062 the definition of quick_symbol_functions in symfile.h. */
1065 psym_dump (struct objfile *objfile)
1067 struct partial_symtab *psymtab;
1069 if (objfile->psymtabs)
1071 printf_filtered ("Psymtabs:\n");
1072 for (psymtab = objfile->psymtabs;
1074 psymtab = psymtab->next)
1076 printf_filtered ("%s at ",
1078 gdb_print_host_address (psymtab, gdb_stdout);
1079 printf_filtered (", ");
1082 printf_filtered ("\n\n");
1086 /* Psymtab version of expand_symtabs_for_function. See its definition in
1087 the definition of quick_symbol_functions in symfile.h. */
1090 psym_expand_symtabs_for_function (struct objfile *objfile,
1091 const char *func_name)
1093 struct partial_symtab *ps;
1095 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1100 if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
1102 || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
1104 psymtab_to_symtab (objfile, ps);
1108 /* Psymtab version of expand_all_symtabs. See its definition in
1109 the definition of quick_symbol_functions in symfile.h. */
1112 psym_expand_all_symtabs (struct objfile *objfile)
1114 struct partial_symtab *psymtab;
1116 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
1118 psymtab_to_symtab (objfile, psymtab);
1122 /* Psymtab version of expand_symtabs_with_fullname. See its definition in
1123 the definition of quick_symbol_functions in symfile.h. */
1126 psym_expand_symtabs_with_fullname (struct objfile *objfile,
1127 const char *fullname)
1129 struct partial_symtab *p;
1131 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
1133 /* Anonymous psymtabs don't have a name of a source file. */
1137 /* psymtab_to_fullname tries to open the file which is slow.
1138 Don't call it if we know the basenames don't match. */
1139 if ((basenames_may_differ
1140 || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1141 && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
1142 psymtab_to_symtab (objfile, p);
1146 /* Psymtab version of map_symbol_filenames. See its definition in
1147 the definition of quick_symbol_functions in symfile.h. */
1150 psym_map_symbol_filenames (struct objfile *objfile,
1151 symbol_filename_ftype *fun, void *data,
1154 struct partial_symtab *ps;
1156 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1158 const char *fullname;
1163 /* We can skip shared psymtabs here, because any file name will be
1164 attached to the unshared psymtab. */
1165 if (ps->user != NULL)
1168 /* Anonymous psymtabs don't have a file name. */
1174 fullname = psymtab_to_fullname (ps);
1177 (*fun) (ps->filename, fullname, data);
1181 /* Finds the fullname that a partial_symtab represents.
1183 If this functions finds the fullname, it will save it in ps->fullname
1184 and it will also return the value.
1186 If this function fails to find the file that this partial_symtab represents,
1187 NULL will be returned and ps->fullname will be set to NULL. */
1190 psymtab_to_fullname (struct partial_symtab *ps)
1192 gdb_assert (!ps->anonymous);
1194 /* Use cached copy if we have it.
1195 We rely on forget_cached_source_info being called appropriately
1196 to handle cases like the file being moved. */
1197 if (ps->fullname == NULL)
1199 int fd = find_and_open_source (ps->filename, ps->dirname, &ps->fullname);
1206 struct cleanup *back_to;
1208 /* rewrite_source_path would be applied by find_and_open_source, we
1209 should report the pathname where GDB tried to find the file. */
1211 if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
1212 fullname = xstrdup (ps->filename);
1214 fullname = concat (ps->dirname, SLASH_STRING,
1215 ps->filename, (char *) NULL);
1217 back_to = make_cleanup (xfree, fullname);
1218 ps->fullname = rewrite_source_path (fullname);
1219 if (ps->fullname == NULL)
1220 ps->fullname = xstrdup (fullname);
1221 do_cleanups (back_to);
1225 return ps->fullname;
1228 /* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1229 according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1230 BLOCK is assumed to come from OBJFILE. Returns 1 iff CALLBACK
1231 ever returns non-zero, and otherwise returns 0. */
1234 map_block (const char *name, domain_enum domain, struct objfile *objfile,
1235 struct block *block,
1236 int (*callback) (struct block *, struct symbol *, void *),
1237 void *data, symbol_compare_ftype *match)
1239 struct block_iterator iter;
1242 for (sym = block_iter_match_first (block, name, match, &iter);
1243 sym != NULL; sym = block_iter_match_next (name, match, &iter))
1245 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
1246 SYMBOL_DOMAIN (sym), domain))
1248 if (callback (block, sym, data))
1256 /* Psymtab version of map_matching_symbols. See its definition in
1257 the definition of quick_symbol_functions in symfile.h. */
1260 psym_map_matching_symbols (struct objfile *objfile,
1261 const char *name, domain_enum domain,
1263 int (*callback) (struct block *,
1264 struct symbol *, void *),
1266 symbol_compare_ftype *match,
1267 symbol_compare_ftype *ordered_compare)
1269 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
1270 struct partial_symtab *ps;
1272 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1276 || match_partial_symbol (objfile, ps, global, name, domain, match,
1279 struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
1280 struct block *block;
1284 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
1285 if (map_block (name, domain, objfile, block,
1286 callback, data, match))
1288 if (callback (block, NULL, data))
1294 /* A helper for psym_expand_symtabs_matching that handles
1295 searching included psymtabs. This returns 1 if a symbol is found,
1296 and zero otherwise. It also updates the 'searched_flag' on the
1297 various psymtabs that it searches. */
1300 recursively_search_psymtabs (struct partial_symtab *ps,
1301 struct objfile *objfile,
1302 enum search_domain kind,
1303 expand_symtabs_symbol_matcher_ftype *sym_matcher,
1306 struct partial_symbol **psym;
1307 struct partial_symbol **bound, **gbound, **sbound;
1309 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
1312 if (ps->searched_flag != PST_NOT_SEARCHED)
1313 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1315 /* Recurse into shared psymtabs first, because they may have already
1316 been searched, and this could save some time. */
1317 for (i = 0; i < ps->number_of_dependencies; ++i)
1321 /* Skip non-shared dependencies, these are handled elsewhere. */
1322 if (ps->dependencies[i]->user == NULL)
1325 r = recursively_search_psymtabs (ps->dependencies[i],
1326 objfile, kind, sym_matcher, data);
1329 ps->searched_flag = PST_SEARCHED_AND_FOUND;
1334 gbound = (objfile->global_psymbols.list
1335 + ps->globals_offset + ps->n_global_syms);
1336 sbound = (objfile->static_psymbols.list
1337 + ps->statics_offset + ps->n_static_syms);
1340 /* Go through all of the symbols stored in a partial
1341 symtab in one loop. */
1342 psym = objfile->global_psymbols.list + ps->globals_offset;
1347 if (bound == gbound && ps->n_static_syms != 0)
1349 psym = objfile->static_psymbols.list + ps->statics_offset;
1360 if ((kind == ALL_DOMAIN
1361 || (kind == VARIABLES_DOMAIN
1362 && PSYMBOL_CLASS (*psym) != LOC_TYPEDEF
1363 && PSYMBOL_CLASS (*psym) != LOC_BLOCK)
1364 || (kind == FUNCTIONS_DOMAIN
1365 && PSYMBOL_CLASS (*psym) == LOC_BLOCK)
1366 || (kind == TYPES_DOMAIN
1367 && PSYMBOL_CLASS (*psym) == LOC_TYPEDEF))
1368 && (*sym_matcher) (SYMBOL_SEARCH_NAME (*psym), data))
1370 /* Found a match, so notify our caller. */
1371 result = PST_SEARCHED_AND_FOUND;
1378 ps->searched_flag = result;
1379 return result == PST_SEARCHED_AND_FOUND;
1382 /* Psymtab version of expand_symtabs_matching. See its definition in
1383 the definition of quick_symbol_functions in symfile.h. */
1386 psym_expand_symtabs_matching
1387 (struct objfile *objfile,
1388 expand_symtabs_file_matcher_ftype *file_matcher,
1389 expand_symtabs_symbol_matcher_ftype *symbol_matcher,
1390 expand_symtabs_exp_notify_ftype *expansion_notify,
1391 enum search_domain kind,
1394 struct partial_symtab *ps;
1396 /* Clear the search flags. */
1397 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1399 ps->searched_flag = PST_NOT_SEARCHED;
1402 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1409 /* We skip shared psymtabs because file-matching doesn't apply
1410 to them; but we search them later in the loop. */
1411 if (ps->user != NULL)
1421 match = (*file_matcher) (ps->filename, data, 0);
1424 /* Before we invoke realpath, which can get expensive when many
1425 files are involved, do a quick comparison of the basenames. */
1426 if (basenames_may_differ
1427 || (*file_matcher) (lbasename (ps->filename), data, 1))
1428 match = (*file_matcher) (psymtab_to_fullname (ps), data, 0);
1434 if (recursively_search_psymtabs (ps, objfile, kind, symbol_matcher, data))
1436 struct compunit_symtab *symtab =
1437 psymtab_to_symtab (objfile, ps);
1439 if (expansion_notify != NULL)
1440 expansion_notify (symtab, data);
1445 /* Psymtab version of has_symbols. See its definition in
1446 the definition of quick_symbol_functions in symfile.h. */
1449 psym_has_symbols (struct objfile *objfile)
1451 return objfile->psymtabs != NULL;
1454 const struct quick_symbol_functions psym_functions =
1457 psym_find_last_source_symtab,
1458 psym_forget_cached_source_info,
1459 psym_map_symtabs_matching_filename,
1464 psym_expand_symtabs_for_function,
1465 psym_expand_all_symtabs,
1466 psym_expand_symtabs_with_fullname,
1467 psym_map_matching_symbols,
1468 psym_expand_symtabs_matching,
1469 psym_find_pc_sect_compunit_symtab,
1470 psym_map_symbol_filenames
1475 /* This compares two partial symbols by names, using strcmp_iw_ordered
1476 for the comparison. */
1479 compare_psymbols (const void *s1p, const void *s2p)
1481 struct partial_symbol *const *s1 = (struct partial_symbol * const*) s1p;
1482 struct partial_symbol *const *s2 = (struct partial_symbol * const*) s2p;
1484 return strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*s1),
1485 SYMBOL_SEARCH_NAME (*s2));
1489 sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
1491 /* Sort the global list; don't sort the static list. */
1493 qsort (objfile->global_psymbols.list + pst->globals_offset,
1494 pst->n_global_syms, sizeof (struct partial_symbol *),
1498 /* Allocate and partially fill a partial symtab. It will be
1499 completely filled at the end of the symbol list.
1501 FILENAME is the name of the symbol-file we are reading from. */
1503 struct partial_symtab *
1504 start_psymtab_common (struct objfile *objfile,
1505 const char *filename,
1506 CORE_ADDR textlow, struct partial_symbol **global_syms,
1507 struct partial_symbol **static_syms)
1509 struct partial_symtab *psymtab;
1511 psymtab = allocate_psymtab (filename, objfile);
1512 psymtab->textlow = textlow;
1513 psymtab->texthigh = psymtab->textlow; /* default */
1514 psymtab->globals_offset = global_syms - objfile->global_psymbols.list;
1515 psymtab->statics_offset = static_syms - objfile->static_psymbols.list;
1519 /* Perform "finishing up" operations of a partial symtab. */
1522 end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1525 = objfile->global_psymbols.next - (objfile->global_psymbols.list
1526 + pst->globals_offset);
1528 = objfile->static_psymbols.next - (objfile->static_psymbols.list
1529 + pst->statics_offset);
1531 sort_pst_symbols (objfile, pst);
1534 /* Calculate a hash code for the given partial symbol. The hash is
1535 calculated using the symbol's value, language, domain, class
1536 and name. These are the values which are set by
1537 add_psymbol_to_bcache. */
1539 static unsigned long
1540 psymbol_hash (const void *addr, int length)
1542 unsigned long h = 0;
1543 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1544 unsigned int lang = psymbol->ginfo.language;
1545 unsigned int domain = PSYMBOL_DOMAIN (psymbol);
1546 unsigned int theclass = PSYMBOL_CLASS (psymbol);
1548 h = hash_continue (&psymbol->ginfo.value, sizeof (psymbol->ginfo.value), h);
1549 h = hash_continue (&lang, sizeof (unsigned int), h);
1550 h = hash_continue (&domain, sizeof (unsigned int), h);
1551 h = hash_continue (&theclass, sizeof (unsigned int), h);
1552 h = hash_continue (psymbol->ginfo.name, strlen (psymbol->ginfo.name), h);
1557 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1558 For the comparison this function uses a symbols value,
1559 language, domain, class and name. */
1562 psymbol_compare (const void *addr1, const void *addr2, int length)
1564 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1565 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1567 return (memcmp (&sym1->ginfo.value, &sym2->ginfo.value,
1568 sizeof (sym1->ginfo.value)) == 0
1569 && sym1->ginfo.language == sym2->ginfo.language
1570 && PSYMBOL_DOMAIN (sym1) == PSYMBOL_DOMAIN (sym2)
1571 && PSYMBOL_CLASS (sym1) == PSYMBOL_CLASS (sym2)
1572 && sym1->ginfo.name == sym2->ginfo.name);
1575 /* Initialize a partial symbol bcache. */
1577 struct psymbol_bcache *
1578 psymbol_bcache_init (void)
1580 struct psymbol_bcache *bcache = XCNEW (struct psymbol_bcache);
1582 bcache->bcache = bcache_xmalloc (psymbol_hash, psymbol_compare);
1586 /* Free a partial symbol bcache. */
1589 psymbol_bcache_free (struct psymbol_bcache *bcache)
1594 bcache_xfree (bcache->bcache);
1598 /* Return the internal bcache of the psymbol_bcache BCACHE. */
1601 psymbol_bcache_get_bcache (struct psymbol_bcache *bcache)
1603 return bcache->bcache;
1606 /* Find a copy of the SYM in BCACHE. If BCACHE has never seen this
1607 symbol before, add a copy to BCACHE. In either case, return a pointer
1608 to BCACHE's copy of the symbol. If optional ADDED is not NULL, return
1609 1 in case of new entry or 0 if returning an old entry. */
1611 static const struct partial_symbol *
1612 psymbol_bcache_full (struct partial_symbol *sym,
1613 struct psymbol_bcache *bcache,
1616 return ((const struct partial_symbol *)
1617 bcache_full (sym, sizeof (struct partial_symbol), bcache->bcache,
1621 /* Helper function, initialises partial symbol structure and stashes
1622 it into objfile's bcache. Note that our caching mechanism will
1623 use all fields of struct partial_symbol to determine hash value of the
1624 structure. In other words, having two symbols with the same name but
1625 different domain (or address) is possible and correct. */
1627 static const struct partial_symbol *
1628 add_psymbol_to_bcache (const char *name, int namelength, int copy_name,
1630 enum address_class theclass,
1632 enum language language, struct objfile *objfile,
1635 struct partial_symbol psymbol;
1637 /* We must ensure that the entire struct has been zeroed before
1638 assigning to it, because an assignment may not touch some of the
1640 memset (&psymbol, 0, sizeof (psymbol));
1642 SYMBOL_VALUE_ADDRESS (&psymbol) = coreaddr;
1643 SYMBOL_SECTION (&psymbol) = -1;
1644 SYMBOL_SET_LANGUAGE (&psymbol, language, &objfile->objfile_obstack);
1645 PSYMBOL_DOMAIN (&psymbol) = domain;
1646 PSYMBOL_CLASS (&psymbol) = theclass;
1648 SYMBOL_SET_NAMES (&psymbol, name, namelength, copy_name, objfile);
1650 /* Stash the partial symbol away in the cache. */
1651 return psymbol_bcache_full (&psymbol, objfile->psymbol_cache, added);
1654 /* Increase the space allocated for LISTP, which is probably
1655 global_psymbols or static_psymbols. This space will eventually
1656 be freed in free_objfile(). */
1659 extend_psymbol_list (struct psymbol_allocation_list *listp,
1660 struct objfile *objfile)
1664 if (listp->size == 0)
1667 listp->list = XNEWVEC (struct partial_symbol *, new_size);
1671 new_size = listp->size * 2;
1672 listp->list = (struct partial_symbol **)
1673 xrealloc ((char *) listp->list,
1674 new_size * sizeof (struct partial_symbol *));
1676 /* Next assumes we only went one over. Should be good if
1677 program works correctly. */
1678 listp->next = listp->list + listp->size;
1679 listp->size = new_size;
1682 /* Helper function, adds partial symbol to the given partial symbol list. */
1685 append_psymbol_to_list (struct psymbol_allocation_list *list,
1686 const struct partial_symbol *psym,
1687 struct objfile *objfile)
1689 if (list->next >= list->list + list->size)
1690 extend_psymbol_list (list, objfile);
1691 *list->next++ = (struct partial_symbol *) psym;
1692 OBJSTAT (objfile, n_psyms++);
1695 /* Add a symbol with a long value to a psymtab.
1696 Since one arg is a struct, we pass in a ptr and deref it (sigh).
1697 The only value we need to store for psyms is an address.
1698 For all other psyms pass zero for COREADDR.
1699 Return the partial symbol that has been added. */
1702 add_psymbol_to_list (const char *name, int namelength, int copy_name,
1704 enum address_class theclass,
1705 struct psymbol_allocation_list *list,
1707 enum language language, struct objfile *objfile)
1709 const struct partial_symbol *psym;
1713 /* Stash the partial symbol away in the cache. */
1714 psym = add_psymbol_to_bcache (name, namelength, copy_name, domain, theclass,
1715 coreaddr, language, objfile, &added);
1717 /* Do not duplicate global partial symbols. */
1718 if (list == &objfile->global_psymbols
1722 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1723 append_psymbol_to_list (list, psym, objfile);
1726 /* Initialize storage for partial symbols. */
1729 init_psymbol_list (struct objfile *objfile, int total_symbols)
1731 /* Free any previously allocated psymbol lists. */
1733 if (objfile->global_psymbols.list)
1734 xfree (objfile->global_psymbols.list);
1735 if (objfile->static_psymbols.list)
1736 xfree (objfile->static_psymbols.list);
1738 /* Current best guess is that approximately a twentieth
1739 of the total symbols (in a debugging file) are global or static
1740 oriented symbols, then multiply that by slop factor of two. */
1742 objfile->global_psymbols.size = total_symbols / 10;
1743 objfile->static_psymbols.size = total_symbols / 10;
1745 if (objfile->global_psymbols.size > 0)
1747 objfile->global_psymbols.next =
1748 objfile->global_psymbols.list =
1749 XNEWVEC (struct partial_symbol *, objfile->global_psymbols.size);
1751 if (objfile->static_psymbols.size > 0)
1753 objfile->static_psymbols.next =
1754 objfile->static_psymbols.list =
1755 XNEWVEC (struct partial_symbol *, objfile->static_psymbols.size);
1759 struct partial_symtab *
1760 allocate_psymtab (const char *filename, struct objfile *objfile)
1762 struct partial_symtab *psymtab;
1764 if (objfile->free_psymtabs)
1766 psymtab = objfile->free_psymtabs;
1767 objfile->free_psymtabs = psymtab->next;
1770 psymtab = (struct partial_symtab *)
1771 obstack_alloc (&objfile->objfile_obstack,
1772 sizeof (struct partial_symtab));
1774 memset (psymtab, 0, sizeof (struct partial_symtab));
1776 = (const char *) bcache (filename, strlen (filename) + 1,
1777 objfile->per_bfd->filename_cache);
1778 psymtab->compunit_symtab = NULL;
1780 /* Prepend it to the psymtab list for the objfile it belongs to.
1781 Psymtabs are searched in most recent inserted -> least recent
1784 psymtab->next = objfile->psymtabs;
1785 objfile->psymtabs = psymtab;
1787 if (symtab_create_debug)
1789 /* Be a bit clever with debugging messages, and don't print objfile
1790 every time, only when it changes. */
1791 static char *last_objfile_name = NULL;
1793 if (last_objfile_name == NULL
1794 || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
1796 xfree (last_objfile_name);
1797 last_objfile_name = xstrdup (objfile_name (objfile));
1798 fprintf_unfiltered (gdb_stdlog,
1799 "Creating one or more psymtabs for objfile %s ...\n",
1802 fprintf_unfiltered (gdb_stdlog,
1803 "Created psymtab %s for module %s.\n",
1804 host_address_to_string (psymtab), filename);
1811 discard_psymtab (struct objfile *objfile, struct partial_symtab *pst)
1813 struct partial_symtab **prev_pst;
1816 Empty psymtabs happen as a result of header files which don't
1817 have any symbols in them. There can be a lot of them. But this
1818 check is wrong, in that a psymtab with N_SLINE entries but
1819 nothing else is not empty, but we don't realize that. Fixing
1820 that without slowing things down might be tricky. */
1822 /* First, snip it out of the psymtab chain. */
1824 prev_pst = &(objfile->psymtabs);
1825 while ((*prev_pst) != pst)
1826 prev_pst = &((*prev_pst)->next);
1827 (*prev_pst) = pst->next;
1829 /* Next, put it on a free list for recycling. */
1831 pst->next = objfile->free_psymtabs;
1832 objfile->free_psymtabs = pst;
1835 /* An object of this type is passed to discard_psymtabs_upto. */
1837 struct psymtab_state
1839 /* The objfile where psymtabs are discarded. */
1841 struct objfile *objfile;
1843 /* The first psymtab to save. */
1845 struct partial_symtab *save;
1848 /* A cleanup function used by make_cleanup_discard_psymtabs. */
1851 discard_psymtabs_upto (void *arg)
1853 struct psymtab_state *state = (struct psymtab_state *) arg;
1855 while (state->objfile->psymtabs != state->save)
1856 discard_psymtab (state->objfile, state->objfile->psymtabs);
1859 /* Return a new cleanup that discards all psymtabs created in OBJFILE
1860 after this function is called. */
1863 make_cleanup_discard_psymtabs (struct objfile *objfile)
1865 struct psymtab_state *state = XNEW (struct psymtab_state);
1867 state->objfile = objfile;
1868 state->save = objfile->psymtabs;
1870 return make_cleanup_dtor (discard_psymtabs_upto, state, xfree);
1875 /* We need to pass a couple of items to the addrmap_foreach function,
1878 struct dump_psymtab_addrmap_data
1880 struct objfile *objfile;
1881 struct partial_symtab *psymtab;
1882 struct ui_file *outfile;
1884 /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1885 If so, we want to print the next one as well (since the next addrmap
1886 entry defines the end of the range). */
1887 int previous_matched;
1890 /* Helper function for dump_psymtab_addrmap to print an addrmap entry. */
1893 dump_psymtab_addrmap_1 (void *datap, CORE_ADDR start_addr, void *obj)
1895 struct dump_psymtab_addrmap_data *data
1896 = (struct dump_psymtab_addrmap_data *) datap;
1897 struct gdbarch *gdbarch = get_objfile_arch (data->objfile);
1898 struct partial_symtab *addrmap_psymtab = (struct partial_symtab *) obj;
1899 const char *psymtab_address_or_end = NULL;
1903 if (data->psymtab == NULL
1904 || data->psymtab == addrmap_psymtab)
1905 psymtab_address_or_end = host_address_to_string (addrmap_psymtab);
1906 else if (data->previous_matched)
1907 psymtab_address_or_end = "<ends here>";
1909 if (data->psymtab == NULL
1910 || data->psymtab == addrmap_psymtab
1911 || data->previous_matched)
1913 fprintf_filtered (data->outfile, " %s%s %s\n",
1914 data->psymtab != NULL ? " " : "",
1915 paddress (gdbarch, start_addr),
1916 psymtab_address_or_end);
1919 data->previous_matched = (data->psymtab == NULL
1920 || data->psymtab == addrmap_psymtab);
1925 /* Helper function for maintenance_print_psymbols to print the addrmap
1926 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1929 dump_psymtab_addrmap (struct objfile *objfile, struct partial_symtab *psymtab,
1930 struct ui_file *outfile)
1932 struct dump_psymtab_addrmap_data addrmap_dump_data;
1934 if ((psymtab == NULL
1935 || psymtab->psymtabs_addrmap_supported)
1936 && objfile->psymtabs_addrmap != NULL)
1938 addrmap_dump_data.objfile = objfile;
1939 addrmap_dump_data.psymtab = psymtab;
1940 addrmap_dump_data.outfile = outfile;
1941 addrmap_dump_data.previous_matched = 0;
1942 fprintf_filtered (outfile, "%sddress map:\n",
1943 psymtab == NULL ? "Entire a" : " A");
1944 addrmap_foreach (objfile->psymtabs_addrmap, dump_psymtab_addrmap_1,
1945 &addrmap_dump_data);
1950 maintenance_print_psymbols (char *args, int from_tty)
1953 struct ui_file *outfile = gdb_stdout;
1954 struct cleanup *cleanups;
1955 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1956 struct objfile *objfile;
1957 struct partial_symtab *ps;
1958 int i, outfile_idx, found;
1960 struct obj_section *section = NULL;
1964 argv = gdb_buildargv (args);
1965 cleanups = make_cleanup_freeargv (argv);
1967 for (i = 0; argv[i] != NULL; ++i)
1969 if (strcmp (argv[i], "-pc") == 0)
1971 if (argv[i + 1] == NULL)
1972 error (_("Missing pc value"));
1973 address_arg = argv[++i];
1975 else if (strcmp (argv[i], "-source") == 0)
1977 if (argv[i + 1] == NULL)
1978 error (_("Missing source file"));
1979 source_arg = argv[++i];
1981 else if (strcmp (argv[i], "-objfile") == 0)
1983 if (argv[i + 1] == NULL)
1984 error (_("Missing objfile name"));
1985 objfile_arg = argv[++i];
1987 else if (strcmp (argv[i], "--") == 0)
1989 /* End of options. */
1993 else if (argv[i][0] == '-')
1995 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1996 error (_("Unknown option: %s"), argv[i]);
2003 if (address_arg != NULL && source_arg != NULL)
2004 error (_("Must specify at most one of -pc and -source"));
2006 if (argv[outfile_idx] != NULL)
2010 if (argv[outfile_idx + 1] != NULL)
2011 error (_("Junk at end of command"));
2012 outfile_name = tilde_expand (argv[outfile_idx]);
2013 make_cleanup (xfree, outfile_name);
2014 outfile = gdb_fopen (outfile_name, FOPEN_WT);
2015 if (outfile == NULL)
2016 perror_with_name (outfile_name);
2017 make_cleanup_ui_file_delete (outfile);
2020 if (address_arg != NULL)
2022 pc = parse_and_eval_address (address_arg);
2023 /* If we fail to find a section, that's ok, try the lookup anyway. */
2024 section = find_pc_section (pc);
2028 ALL_OBJFILES (objfile)
2030 int printed_objfile_header = 0;
2031 int print_for_objfile = 1;
2034 if (objfile_arg != NULL)
2036 = compare_filenames_for_search (objfile_name (objfile),
2038 if (!print_for_objfile)
2041 if (address_arg != NULL)
2043 struct bound_minimal_symbol msymbol = { NULL, NULL };
2045 /* We don't assume each pc has a unique objfile (this is for
2047 ps = find_pc_sect_psymtab (objfile, pc, section, msymbol);
2050 if (!printed_objfile_header)
2052 fprintf_filtered (outfile,
2053 "\nPartial symtabs for objfile %s\n",
2054 objfile_name (objfile));
2055 printed_objfile_header = 1;
2057 dump_psymtab (objfile, ps, outfile);
2058 dump_psymtab_addrmap (objfile, ps, outfile);
2064 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
2066 int print_for_source = 0;
2069 if (source_arg != NULL)
2072 = compare_filenames_for_search (ps->filename, source_arg);
2075 if (source_arg == NULL
2076 || print_for_source)
2078 if (!printed_objfile_header)
2080 fprintf_filtered (outfile,
2081 "\nPartial symtabs for objfile %s\n",
2082 objfile_name (objfile));
2083 printed_objfile_header = 1;
2085 dump_psymtab (objfile, ps, outfile);
2086 dump_psymtab_addrmap (objfile, ps, outfile);
2091 /* If we're printing all the objfile's symbols dump the full addrmap. */
2093 if (address_arg == NULL
2094 && source_arg == NULL
2095 && objfile->psymtabs_addrmap != NULL)
2097 fprintf_filtered (outfile, "\n");
2098 dump_psymtab_addrmap (objfile, NULL, outfile);
2104 if (address_arg != NULL)
2105 error (_("No partial symtab for address: %s"), address_arg);
2106 if (source_arg != NULL)
2107 error (_("No partial symtab for source file: %s"), source_arg);
2110 do_cleanups (cleanups);
2113 /* List all the partial symbol tables whose names match REGEXP (optional). */
2116 maintenance_info_psymtabs (char *regexp, int from_tty)
2118 struct program_space *pspace;
2119 struct objfile *objfile;
2124 ALL_PSPACES (pspace)
2125 ALL_PSPACE_OBJFILES (pspace, objfile)
2127 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2128 struct partial_symtab *psymtab;
2130 /* We don't want to print anything for this objfile until we
2131 actually find a symtab whose name matches. */
2132 int printed_objfile_start = 0;
2134 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
2139 || re_exec (psymtab->filename))
2141 if (! printed_objfile_start)
2143 printf_filtered ("{ objfile %s ", objfile_name (objfile));
2145 printf_filtered ("((struct objfile *) %s)\n",
2146 host_address_to_string (objfile));
2147 printed_objfile_start = 1;
2150 printf_filtered (" { psymtab %s ", psymtab->filename);
2152 printf_filtered ("((struct partial_symtab *) %s)\n",
2153 host_address_to_string (psymtab));
2155 printf_filtered (" readin %s\n",
2156 psymtab->readin ? "yes" : "no");
2157 printf_filtered (" fullname %s\n",
2159 ? psymtab->fullname : "(null)");
2160 printf_filtered (" text addresses ");
2161 fputs_filtered (paddress (gdbarch, psymtab->textlow),
2163 printf_filtered (" -- ");
2164 fputs_filtered (paddress (gdbarch, psymtab->texthigh),
2166 printf_filtered ("\n");
2167 printf_filtered (" psymtabs_addrmap_supported %s\n",
2168 (psymtab->psymtabs_addrmap_supported
2170 printf_filtered (" globals ");
2171 if (psymtab->n_global_syms)
2173 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2174 host_address_to_string (objfile->global_psymbols.list
2175 + psymtab->globals_offset),
2176 psymtab->n_global_syms);
2179 printf_filtered ("(none)\n");
2180 printf_filtered (" statics ");
2181 if (psymtab->n_static_syms)
2183 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2184 host_address_to_string (objfile->static_psymbols.list
2185 + psymtab->statics_offset),
2186 psymtab->n_static_syms);
2189 printf_filtered ("(none)\n");
2190 printf_filtered (" dependencies ");
2191 if (psymtab->number_of_dependencies)
2195 printf_filtered ("{\n");
2196 for (i = 0; i < psymtab->number_of_dependencies; i++)
2198 struct partial_symtab *dep = psymtab->dependencies[i];
2200 /* Note the string concatenation there --- no comma. */
2201 printf_filtered (" psymtab %s "
2202 "((struct partial_symtab *) %s)\n",
2204 host_address_to_string (dep));
2206 printf_filtered (" }\n");
2209 printf_filtered ("(none)\n");
2210 printf_filtered (" }\n");
2214 if (printed_objfile_start)
2215 printf_filtered ("}\n");
2219 /* Check consistency of currently expanded psymtabs vs symtabs. */
2222 maintenance_check_psymtabs (char *ignore, int from_tty)
2225 struct partial_symbol **psym;
2226 struct compunit_symtab *cust = NULL;
2227 struct partial_symtab *ps;
2228 const struct blockvector *bv;
2229 struct objfile *objfile;
2233 ALL_PSYMTABS (objfile, ps)
2235 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2237 /* We don't call psymtab_to_symtab here because that may cause symtab
2238 expansion. When debugging a problem it helps if checkers leave
2239 things unchanged. */
2240 cust = ps->compunit_symtab;
2242 /* First do some checks that don't require the associated symtab. */
2243 if (ps->texthigh < ps->textlow)
2245 printf_filtered ("Psymtab ");
2246 puts_filtered (ps->filename);
2247 printf_filtered (" covers bad range ");
2248 fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
2249 printf_filtered (" - ");
2250 fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
2251 printf_filtered ("\n");
2255 /* Now do checks requiring the associated symtab. */
2258 bv = COMPUNIT_BLOCKVECTOR (cust);
2259 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
2260 psym = objfile->static_psymbols.list + ps->statics_offset;
2261 length = ps->n_static_syms;
2264 sym = block_lookup_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
2265 SYMBOL_DOMAIN (*psym));
2268 printf_filtered ("Static symbol `");
2269 puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
2270 printf_filtered ("' only found in ");
2271 puts_filtered (ps->filename);
2272 printf_filtered (" psymtab\n");
2276 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2277 psym = objfile->global_psymbols.list + ps->globals_offset;
2278 length = ps->n_global_syms;
2281 sym = block_lookup_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
2282 SYMBOL_DOMAIN (*psym));
2285 printf_filtered ("Global symbol `");
2286 puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
2287 printf_filtered ("' only found in ");
2288 puts_filtered (ps->filename);
2289 printf_filtered (" psymtab\n");
2293 if (ps->texthigh != 0
2294 && (ps->textlow < BLOCK_START (b) || ps->texthigh > BLOCK_END (b)))
2296 printf_filtered ("Psymtab ");
2297 puts_filtered (ps->filename);
2298 printf_filtered (" covers ");
2299 fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
2300 printf_filtered (" - ");
2301 fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
2302 printf_filtered (" but symtab covers only ");
2303 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2304 printf_filtered (" - ");
2305 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2306 printf_filtered ("\n");
2313 extern initialize_file_ftype _initialize_psymtab;
2316 _initialize_psymtab (void)
2318 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2319 Print dump of current partial symbol definitions.\n\
2320 Usage: mt print psymbols [-objfile objfile] [-pc address] [--] [outfile]\n\
2321 mt print psymbols [-objfile objfile] [-source source] [--] [outfile]\n\
2322 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2323 or the terminal if OUTFILE is unspecified.\n\
2324 If ADDRESS is provided, dump only the file for that address.\n\
2325 If SOURCE is provided, dump only that file's symbols.\n\
2326 If OBJFILE is provided, dump only that file's minimal symbols."),
2327 &maintenanceprintlist);
2329 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2330 List the partial symbol tables for all object files.\n\
2331 This does not include information about individual partial symbols,\n\
2332 just the symbol table structures themselves."),
2333 &maintenanceinfolist);
2335 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2337 Check consistency of currently expanded psymtabs versus symtabs."),