1 /* Partial symbol tables.
3 Copyright (C) 2009-2024 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/>. */
20 #include "event-top.h"
25 #include "filenames.h"
30 #include "readline/tilde.h"
31 #include "gdbsupport/gdb_regex.h"
32 #include "dictionary.h"
34 #include "cp-support.h"
35 #include "cli/cli-cmds.h"
38 #include "gdbsupport/buildargv.h"
40 static const struct partial_symbol
*lookup_partial_symbol
41 (struct objfile
*, struct partial_symtab
*, const lookup_name_info
&,
42 int, domain_search_flags
);
44 static const char *psymtab_to_fullname (struct partial_symtab
*ps
);
46 static const struct partial_symbol
*find_pc_sect_psymbol
47 (struct objfile
*, struct partial_symtab
*, CORE_ADDR
,
48 struct obj_section
*);
50 static struct compunit_symtab
*psymtab_to_symtab (struct objfile
*objfile
,
51 struct partial_symtab
*pst
);
53 psymtab_storage::~psymtab_storage ()
55 partial_symtab
*iter
= psymtabs
;
56 while (iter
!= nullptr)
58 partial_symtab
*next
= iter
->next
;
67 psymtab_storage::install_psymtab (partial_symtab
*pst
)
77 psymtab_storage::partial_symtab_range
78 psymbol_functions::partial_symbols (struct objfile
*objfile
)
80 return m_partial_symtabs
->range ();
83 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
84 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
86 static struct partial_symtab
*
87 find_pc_sect_psymtab_closer (struct objfile
*objfile
,
88 CORE_ADDR pc
, struct obj_section
*section
,
89 struct partial_symtab
*pst
,
90 bound_minimal_symbol msymbol
)
92 struct partial_symtab
*tpst
;
93 struct partial_symtab
*best_pst
= pst
;
94 CORE_ADDR best_addr
= pst
->text_low (objfile
);
96 /* An objfile that has its functions reordered might have
97 many partial symbol tables containing the PC, but
98 we want the partial symbol table that contains the
99 function containing the PC. */
100 if (section
== nullptr)
103 if (msymbol
.minsym
== NULL
)
106 /* The code range of partial symtabs sometimes overlap, so, in
107 the loop below, we need to check all partial symtabs and
108 find the one that fits better for the given PC address. We
109 select the partial symtab that contains a symbol whose
110 address is closest to the PC address. By closest we mean
111 that find_pc_sect_symbol returns the symbol with address
112 that is closest and still less than the given PC. */
113 for (tpst
= pst
; tpst
!= NULL
; tpst
= tpst
->next
)
115 if (pc
>= tpst
->text_low (objfile
) && pc
< tpst
->text_high (objfile
))
117 const struct partial_symbol
*p
;
120 /* NOTE: This assumes that every psymbol has a
121 corresponding msymbol, which is not necessarily
122 true; the debug info might be much richer than the
123 object's symbol table. */
124 p
= find_pc_sect_psymbol (objfile
, tpst
, pc
, section
);
126 && (p
->address (objfile
) == msymbol
.value_address ()))
129 /* Also accept the textlow value of a psymtab as a
130 "symbol", to provide some support for partial
131 symbol tables with line information but no debug
132 symbols (e.g. those produced by an assembler). */
134 this_addr
= p
->address (objfile
);
136 this_addr
= tpst
->text_low (objfile
);
138 /* Check whether it is closer than our current
139 BEST_ADDR. Since this symbol address is
140 necessarily lower or equal to PC, the symbol closer
141 to PC is the symbol which address is the highest.
142 This way we return the psymtab which contains such
143 best match symbol. This can help in cases where the
144 symbol information/debuginfo is not complete, like
145 for instance on IRIX6 with gcc, where no debug info
146 is emitted for statics. (See also the nodebug.exp
148 if (this_addr
> best_addr
)
150 best_addr
= this_addr
;
160 struct partial_symtab
*
161 psymbol_functions::find_pc_sect_psymtab (struct objfile
*objfile
,
163 struct obj_section
*section
,
164 bound_minimal_symbol msymbol
)
166 for (partial_symtab
*pst
: partial_symbols (objfile
))
167 if (pc
>= pst
->text_low (objfile
) && pc
< pst
->text_high (objfile
))
169 struct partial_symtab
*best_pst
;
171 best_pst
= find_pc_sect_psymtab_closer (objfile
, pc
, section
, pst
,
173 if (best_pst
!= NULL
)
180 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
181 the definition of quick_symbol_functions in symfile.h. */
183 struct compunit_symtab
*
184 psymbol_functions::find_pc_sect_compunit_symtab (struct objfile
*objfile
,
185 bound_minimal_symbol msymbol
,
187 struct obj_section
*section
,
190 struct partial_symtab
*ps
= find_pc_sect_psymtab (objfile
,
195 if (warn_if_readin
&& ps
->readin_p (objfile
))
196 /* Might want to error() here (in case symtab is corrupt and
197 will cause a core dump), but maybe we can successfully
198 continue, so let's not. */
200 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
201 paddress (objfile
->arch (), pc
));
202 psymtab_to_symtab (objfile
, ps
);
203 return ps
->get_compunit_symtab (objfile
);
208 /* Find which partial symbol within a psymtab matches PC and SECTION.
209 Return NULL if none. */
211 static const struct partial_symbol
*
212 find_pc_sect_psymbol (struct objfile
*objfile
,
213 struct partial_symtab
*psymtab
, CORE_ADDR pc
,
214 struct obj_section
*section
)
216 const struct partial_symbol
*best
= NULL
;
218 const CORE_ADDR textlow
= psymtab
->text_low (objfile
);
220 gdb_assert (psymtab
!= NULL
);
222 /* Cope with programs that start at address 0. */
223 best_pc
= (textlow
!= 0) ? textlow
- 1 : 0;
225 /* Search the global symbols as well as the static symbols, so that
226 find_pc_partial_function doesn't use a minimal symbol and thus
227 cache a bad endaddr. */
228 for (const partial_symbol
*p
: psymtab
->global_psymbols
)
230 if (p
->domain
== VAR_DOMAIN
231 && p
->aclass
== LOC_BLOCK
232 && pc
>= p
->address (objfile
)
233 && (p
->address (objfile
) > best_pc
234 || (psymtab
->text_low (objfile
) == 0
235 && best_pc
== 0 && p
->address (objfile
) == 0)))
237 if (section
!= NULL
) /* Match on a specific section. */
239 if (!matching_obj_sections (p
->obj_section (objfile
),
243 best_pc
= p
->address (objfile
);
248 for (const partial_symbol
*p
: psymtab
->static_psymbols
)
250 if (p
->domain
== VAR_DOMAIN
251 && p
->aclass
== LOC_BLOCK
252 && pc
>= p
->address (objfile
)
253 && (p
->address (objfile
) > best_pc
254 || (psymtab
->text_low (objfile
) == 0
255 && best_pc
== 0 && p
->address (objfile
) == 0)))
257 if (section
!= NULL
) /* Match on a specific section. */
259 if (!matching_obj_sections (p
->obj_section (objfile
),
263 best_pc
= p
->address (objfile
);
271 /* Psymtab version of lookup_global_symbol_language. See its definition in
272 the definition of quick_symbol_functions in symfile.h. */
275 psymbol_functions::lookup_global_symbol_language (struct objfile
*objfile
,
277 domain_search_flags domain
,
278 bool *symbol_found_p
)
280 *symbol_found_p
= false;
281 if (objfile
->sf
== NULL
)
282 return language_unknown
;
284 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
286 for (partial_symtab
*ps
: partial_symbols (objfile
))
288 const struct partial_symbol
*psym
;
289 if (ps
->readin_p (objfile
))
292 psym
= lookup_partial_symbol (objfile
, ps
, lookup_name
, 1, domain
);
295 *symbol_found_p
= true;
296 return psym
->ginfo
.language ();
300 return language_unknown
;
303 /* Returns true if PSYM matches LOOKUP_NAME. */
306 psymbol_name_matches (const partial_symbol
*psym
,
307 const lookup_name_info
&lookup_name
)
309 const language_defn
*lang
= language_def (psym
->ginfo
.language ());
310 symbol_name_matcher_ftype
*name_match
311 = lang
->get_symbol_name_matcher (lookup_name
);
312 return name_match (psym
->ginfo
.search_name (), lookup_name
, NULL
);
315 /* Look, in partial_symtab PST, for symbol whose natural name is
316 LOOKUP_NAME. Check the global symbols if GLOBAL, the static
319 static const struct partial_symbol
*
320 lookup_partial_symbol (struct objfile
*objfile
,
321 struct partial_symtab
*pst
,
322 const lookup_name_info
&lookup_name
,
323 int global
, domain_search_flags domain
)
325 const struct partial_symbol
**start
, **psym
;
326 const struct partial_symbol
**top
, **real_top
, **bottom
, **center
;
328 ? pst
->global_psymbols
.size ()
329 : pst
->static_psymbols
.size ());
330 int do_linear_search
= 1;
336 &pst
->global_psymbols
[0] :
337 &pst
->static_psymbols
[0]);
339 if (global
) /* This means we can use a binary search. */
341 do_linear_search
= 0;
343 /* Binary search. This search is guaranteed to end with center
344 pointing at the earliest partial symbol whose name might be
345 correct. At that point *all* partial symbols with an
346 appropriate name will be checked against the correct
350 top
= start
+ length
- 1;
354 center
= bottom
+ (top
- bottom
) / 2;
356 gdb_assert (center
< top
);
358 if (strcmp_iw_ordered ((*center
)->ginfo
.search_name (),
359 lookup_name
.c_str ()) >= 0)
369 gdb_assert (top
== bottom
);
371 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
372 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
373 while (top
>= start
&& symbol_matches_search_name (&(*top
)->ginfo
,
377 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
380 while (top
<= real_top
&& symbol_matches_search_name (&(*top
)->ginfo
,
383 if (search_flags_matches (domain
, (*top
)->domain
))
389 /* Can't use a binary search or else we found during the binary search that
390 we should also do a linear search. */
392 if (do_linear_search
)
394 for (psym
= start
; psym
< start
+ length
; psym
++)
396 if (search_flags_matches (domain
, (*psym
)->domain
)
397 && symbol_matches_search_name (&(*psym
)->ginfo
, lookup_name
))
405 /* Get the symbol table that corresponds to a partial_symtab.
406 This is fast after the first time you do it.
407 The result will be NULL if the primary symtab has no symbols,
408 which can happen. Otherwise the result is the primary symtab
409 that contains PST. */
411 static struct compunit_symtab
*
412 psymtab_to_symtab (struct objfile
*objfile
, struct partial_symtab
*pst
)
414 /* If it is a shared psymtab, find an unshared psymtab that includes
415 it. Any such psymtab will do. */
416 while (pst
->user
!= NULL
)
419 /* If it's been looked up before, return it. */
420 if (pst
->get_compunit_symtab (objfile
))
421 return pst
->get_compunit_symtab (objfile
);
423 /* If it has not yet been read in, read it. */
424 if (!pst
->readin_p (objfile
))
426 scoped_restore decrementer
= increment_reading_symtab ();
430 gdb_printf (_("Reading in symbols for %s...\n"),
432 gdb_flush (gdb_stdout
);
435 pst
->read_symtab (objfile
);
438 return pst
->get_compunit_symtab (objfile
);
441 /* Psymtab version of find_last_source_symtab. See its definition in
442 the definition of quick_symbol_functions in symfile.h. */
445 psymbol_functions::find_last_source_symtab (struct objfile
*ofp
)
447 struct partial_symtab
*cs_pst
= NULL
;
449 for (partial_symtab
*ps
: partial_symbols (ofp
))
451 const char *name
= ps
->filename
;
452 int len
= strlen (name
);
454 if (!(len
> 2 && (strcmp (&name
[len
- 2], ".h") == 0
455 || strcmp (name
, "<<C++-namespaces>>") == 0)))
461 if (cs_pst
->readin_p (ofp
))
463 internal_error (_("select_source_symtab: "
464 "readin pst found and no symtabs."));
468 struct compunit_symtab
*cust
= psymtab_to_symtab (ofp
, cs_pst
);
472 return cust
->primary_filetab ();
478 /* Psymtab version of forget_cached_source_info. See its definition in
479 the definition of quick_symbol_functions in symfile.h. */
482 psymbol_functions::forget_cached_source_info (struct objfile
*objfile
)
484 for (partial_symtab
*pst
: partial_symbols (objfile
))
486 if (pst
->fullname
!= NULL
)
488 xfree (pst
->fullname
);
489 pst
->fullname
= NULL
;
495 print_partial_symbols (struct gdbarch
*gdbarch
, struct objfile
*objfile
,
496 const std::vector
<const partial_symbol
*> &symbols
,
497 const char *what
, struct ui_file
*outfile
)
499 gdb_printf (outfile
, " %s partial symbols:\n", what
);
500 for (const partial_symbol
*p
: symbols
)
503 gdb_printf (outfile
, " `%s'", p
->ginfo
.linkage_name ());
504 if (p
->ginfo
.demangled_name () != NULL
)
506 gdb_printf (outfile
, " `%s'",
507 p
->ginfo
.demangled_name ());
509 gdb_puts (", ", outfile
);
513 gdb_puts ("undefined domain, ", outfile
);
516 /* This is the usual thing -- don't print it. */
519 gdb_puts ("struct domain, ", outfile
);
522 gdb_puts ("module domain, ", outfile
);
525 gdb_puts ("label domain, ", outfile
);
527 case COMMON_BLOCK_DOMAIN
:
528 gdb_puts ("common block domain, ", outfile
);
531 gdb_puts ("<invalid domain>, ", outfile
);
537 gdb_puts ("undefined", outfile
);
540 gdb_puts ("constant int", outfile
);
543 gdb_puts ("static", outfile
);
546 gdb_puts ("register", outfile
);
549 gdb_puts ("pass by value", outfile
);
552 gdb_puts ("pass by reference", outfile
);
554 case LOC_REGPARM_ADDR
:
555 gdb_puts ("register address parameter", outfile
);
558 gdb_puts ("stack parameter", outfile
);
561 gdb_puts ("type", outfile
);
564 gdb_puts ("label", outfile
);
567 gdb_puts ("function", outfile
);
569 case LOC_CONST_BYTES
:
570 gdb_puts ("constant bytes", outfile
);
573 gdb_puts ("unresolved", outfile
);
575 case LOC_OPTIMIZED_OUT
:
576 gdb_puts ("optimized out", outfile
);
579 gdb_puts ("computed at runtime", outfile
);
582 gdb_puts ("<invalid location>", outfile
);
585 gdb_puts (", ", outfile
);
586 gdb_puts (paddress (gdbarch
, CORE_ADDR (p
->unrelocated_address ())),
588 gdb_printf (outfile
, "\n");
593 dump_psymtab (struct objfile
*objfile
, struct partial_symtab
*psymtab
,
594 struct ui_file
*outfile
)
596 struct gdbarch
*gdbarch
= objfile
->arch ();
599 if (psymtab
->anonymous
)
601 gdb_printf (outfile
, "\nAnonymous partial symtab (%s) ",
606 gdb_printf (outfile
, "\nPartial symtab for source file %s ",
609 gdb_printf (outfile
, "(object %s)\n\n",
610 host_address_to_string (psymtab
));
611 gdb_printf (outfile
, " Read from object file %s (%s)\n",
612 objfile_name (objfile
),
613 host_address_to_string (objfile
));
615 if (psymtab
->readin_p (objfile
))
618 " Full symtab was read (at %s)\n",
619 host_address_to_string (psymtab
->get_compunit_symtab (objfile
)));
621 gdb_printf (outfile
, " Symbols cover text addresses ");
622 gdb_puts (paddress (gdbarch
, psymtab
->text_low (objfile
)), outfile
);
623 gdb_printf (outfile
, "-");
624 gdb_puts (paddress (gdbarch
, psymtab
->text_high (objfile
)), outfile
);
625 gdb_printf (outfile
, "\n");
626 gdb_printf (outfile
, " Depends on %d other partial symtabs.\n",
627 psymtab
->number_of_dependencies
);
628 for (i
= 0; i
< psymtab
->number_of_dependencies
; i
++)
629 gdb_printf (outfile
, " %d %s\n", i
,
630 host_address_to_string (psymtab
->dependencies
[i
]));
631 if (psymtab
->user
!= NULL
)
632 gdb_printf (outfile
, " Shared partial symtab with user %s\n",
633 host_address_to_string (psymtab
->user
));
634 if (!psymtab
->global_psymbols
.empty ())
636 print_partial_symbols
637 (gdbarch
, objfile
, psymtab
->global_psymbols
,
640 if (!psymtab
->static_psymbols
.empty ())
642 print_partial_symbols
643 (gdbarch
, objfile
, psymtab
->static_psymbols
,
646 gdb_printf (outfile
, "\n");
649 /* Count the number of partial symbols in OBJFILE. */
652 psymbol_functions::count_psyms ()
655 for (partial_symtab
*pst
: m_partial_symtabs
->range ())
657 count
+= pst
->global_psymbols
.size ();
658 count
+= pst
->static_psymbols
.size ();
663 /* Psymtab version of print_stats. See its definition in
664 the definition of quick_symbol_functions in symfile.h. */
667 psymbol_functions::print_stats (struct objfile
*objfile
, bool print_bcache
)
673 int n_psyms
= count_psyms ();
675 gdb_printf (_(" Number of \"partial\" symbols read: %d\n"),
679 for (partial_symtab
*ps
: partial_symbols (objfile
))
681 if (!ps
->readin_p (objfile
))
684 gdb_printf (_(" Number of psym tables (not yet expanded): %d\n"),
686 gdb_printf (_(" Total memory used for psymbol cache: %d\n"),
687 m_partial_symtabs
->psymbol_cache
.memory_used ());
691 gdb_printf (_("Psymbol byte cache statistics:\n"));
692 m_partial_symtabs
->psymbol_cache
.print_statistics
693 ("partial symbol cache");
697 /* Psymtab version of dump. See its definition in
698 the definition of quick_symbol_functions in symfile.h. */
701 psymbol_functions::dump (struct objfile
*objfile
)
703 struct partial_symtab
*psymtab
;
705 if (m_partial_symtabs
->psymtabs
)
707 gdb_printf ("Psymtabs:\n");
708 for (psymtab
= m_partial_symtabs
->psymtabs
;
710 psymtab
= psymtab
->next
)
711 gdb_printf ("%s at %s\n",
713 host_address_to_string (psymtab
));
718 /* Psymtab version of expand_all_symtabs. See its definition in
719 the definition of quick_symbol_functions in symfile.h. */
722 psymbol_functions::expand_all_symtabs (struct objfile
*objfile
)
724 for (partial_symtab
*psymtab
: partial_symbols (objfile
))
725 psymtab_to_symtab (objfile
, psymtab
);
728 /* Psymtab version of map_symbol_filenames. See its definition in
729 the definition of quick_symbol_functions in symfile.h. */
732 psymbol_functions::map_symbol_filenames
733 (struct objfile
*objfile
,
734 gdb::function_view
<symbol_filename_ftype
> fun
,
737 for (partial_symtab
*ps
: partial_symbols (objfile
))
739 const char *fullname
;
741 if (ps
->readin_p (objfile
))
744 /* We can skip shared psymtabs here, because any file name will be
745 attached to the unshared psymtab. */
746 if (ps
->user
!= NULL
)
749 /* Anonymous psymtabs don't have a file name. */
755 fullname
= psymtab_to_fullname (ps
);
758 fun (ps
->filename
, fullname
);
762 /* Finds the fullname that a partial_symtab represents.
764 If this functions finds the fullname, it will save it in ps->fullname
765 and it will also return the value.
767 If this function fails to find the file that this partial_symtab represents,
768 NULL will be returned and ps->fullname will be set to NULL. */
771 psymtab_to_fullname (struct partial_symtab
*ps
)
773 gdb_assert (!ps
->anonymous
);
775 /* Use cached copy if we have it.
776 We rely on forget_cached_source_info being called appropriately
777 to handle cases like the file being moved. */
778 if (ps
->fullname
== NULL
)
780 gdb::unique_xmalloc_ptr
<char> fullname
781 = find_source_or_rewrite (ps
->filename
, ps
->dirname
);
782 ps
->fullname
= fullname
.release ();
788 /* A helper for psym_expand_symtabs_matching that handles searching
789 included psymtabs. This returns true if a symbol is found, and
790 false otherwise. It also updates the 'searched_flag' on the
791 various psymtabs that it searches. */
794 recursively_search_psymtabs
795 (struct partial_symtab
*ps
,
796 struct objfile
*objfile
,
797 block_search_flags search_flags
,
798 domain_search_flags domain
,
799 const lookup_name_info
&lookup_name
,
800 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> sym_matcher
)
803 enum psymtab_search_status result
= PST_SEARCHED_AND_NOT_FOUND
;
806 if (ps
->searched_flag
!= PST_NOT_SEARCHED
)
807 return ps
->searched_flag
== PST_SEARCHED_AND_FOUND
;
809 /* Recurse into shared psymtabs first, because they may have already
810 been searched, and this could save some time. */
811 for (i
= 0; i
< ps
->number_of_dependencies
; ++i
)
815 /* Skip non-shared dependencies, these are handled elsewhere. */
816 if (ps
->dependencies
[i
]->user
== NULL
)
819 r
= recursively_search_psymtabs (ps
->dependencies
[i
],
820 objfile
, search_flags
, domain
,
821 lookup_name
, sym_matcher
);
824 ps
->searched_flag
= PST_SEARCHED_AND_FOUND
;
829 const partial_symbol
**gbound
= (ps
->global_psymbols
.data ()
830 + ps
->global_psymbols
.size ());
831 const partial_symbol
**sbound
= (ps
->static_psymbols
.data ()
832 + ps
->static_psymbols
.size ());
833 const partial_symbol
**bound
= gbound
;
835 /* Go through all of the symbols stored in a partial
836 symtab in one loop. */
837 const partial_symbol
**psym
= ps
->global_psymbols
.data ();
839 if ((search_flags
& SEARCH_GLOBAL_BLOCK
) == 0)
841 if (ps
->static_psymbols
.empty ())
845 psym
= ps
->static_psymbols
.data ();
854 if (bound
== gbound
&& !ps
->static_psymbols
.empty ()
855 && (search_flags
& SEARCH_STATIC_BLOCK
) != 0)
857 psym
= ps
->static_psymbols
.data ();
868 if (search_flags_matches (domain
, (*psym
)->domain
)
869 && psymbol_name_matches (*psym
, lookup_name
)
870 && (sym_matcher
== NULL
871 || sym_matcher ((*psym
)->ginfo
.search_name ())))
873 /* Found a match, so notify our caller. */
874 result
= PST_SEARCHED_AND_FOUND
;
881 ps
->searched_flag
= result
;
882 return result
== PST_SEARCHED_AND_FOUND
;
885 /* Psymtab version of expand_symtabs_matching. See its definition in
886 the definition of quick_symbol_functions in symfile.h. */
889 psymbol_functions::expand_symtabs_matching
890 (struct objfile
*objfile
,
891 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
892 const lookup_name_info
*lookup_name
,
893 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
894 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
895 block_search_flags search_flags
,
896 domain_search_flags domain
,
897 gdb::function_view
<expand_symtabs_lang_matcher_ftype
>
898 lang_matcher ATTRIBUTE_UNUSED
)
900 /* Clear the search flags. */
901 for (partial_symtab
*ps
: partial_symbols (objfile
))
902 ps
->searched_flag
= PST_NOT_SEARCHED
;
904 std::optional
<lookup_name_info
> psym_lookup_name
;
905 if (lookup_name
!= nullptr)
906 psym_lookup_name
= lookup_name
->make_ignore_params ();
908 /* This invariant is documented in quick-functions.h. */
909 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
911 for (partial_symtab
*ps
: m_partial_symtabs
->range ())
915 if (ps
->readin_p (objfile
))
925 match
= file_matcher (ps
->filename
, false);
928 /* Before we invoke realpath, which can get expensive when many
929 files are involved, do a quick comparison of the basenames. */
930 if (basenames_may_differ
931 || file_matcher (lbasename (ps
->filename
), true))
932 match
= file_matcher (psymtab_to_fullname (ps
), false);
938 if (lookup_name
== nullptr
939 || recursively_search_psymtabs (ps
, objfile
, search_flags
,
940 domain
, *psym_lookup_name
,
943 compunit_symtab
*cust
= psymtab_to_symtab (objfile
, ps
);
945 if (cust
!= nullptr && expansion_notify
!= nullptr)
946 if (!expansion_notify (cust
))
954 /* Psymtab version of has_symbols. See its definition in
955 the definition of quick_symbol_functions in symfile.h. */
958 psymbol_functions::has_symbols (struct objfile
*objfile
)
960 return m_partial_symtabs
->psymtabs
!= NULL
;
963 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
966 psymbol_functions::has_unexpanded_symtabs (struct objfile
*objfile
)
968 for (partial_symtab
*psymtab
: partial_symbols (objfile
))
970 /* Is this already expanded? */
971 if (psymtab
->readin_p (objfile
))
974 /* It has not yet been expanded. */
983 /* Partially fill a partial symtab. It will be completely filled at
984 the end of the symbol list. */
986 partial_symtab::partial_symtab (const char *filename
,
987 psymtab_storage
*partial_symtabs
,
988 objfile_per_bfd_storage
*objfile_per_bfd
,
989 unrelocated_addr textlow
)
990 : partial_symtab (filename
, partial_symtabs
, objfile_per_bfd
)
992 set_text_low (textlow
);
993 set_text_high (unrelocated_text_low ()); /* default */
996 /* Perform "finishing up" operations of a partial symtab. */
999 partial_symtab::end ()
1001 global_psymbols
.shrink_to_fit ();
1002 static_psymbols
.shrink_to_fit ();
1004 /* Sort the global list; don't sort the static list. */
1005 std::sort (global_psymbols
.begin (),
1006 global_psymbols
.end (),
1007 [] (const partial_symbol
*s1
, const partial_symbol
*s2
)
1009 return strcmp_iw_ordered (s1
->ginfo
.search_name (),
1010 s2
->ginfo
.search_name ()) < 0;
1014 /* See psymtab.h. */
1017 psymbol_bcache::hash (const void *addr
, int length
)
1019 unsigned long h
= 0;
1020 struct partial_symbol
*psymbol
= (struct partial_symbol
*) addr
;
1021 unsigned int lang
= psymbol
->ginfo
.language ();
1022 unsigned int domain
= psymbol
->domain
;
1023 unsigned int theclass
= psymbol
->aclass
;
1025 h
= fast_hash (&psymbol
->ginfo
.m_value
, sizeof (psymbol
->ginfo
.m_value
), h
);
1026 h
= fast_hash (&lang
, sizeof (unsigned int), h
);
1027 h
= fast_hash (&domain
, sizeof (unsigned int), h
);
1028 h
= fast_hash (&theclass
, sizeof (unsigned int), h
);
1029 /* Note that psymbol names are interned via compute_and_set_names, so
1030 there's no need to hash the contents of the name here. */
1031 h
= fast_hash (&psymbol
->ginfo
.m_name
, sizeof (psymbol
->ginfo
.m_name
), h
);
1036 /* See psymtab.h. */
1039 psymbol_bcache::compare (const void *addr1
, const void *addr2
, int length
)
1041 struct partial_symbol
*sym1
= (struct partial_symbol
*) addr1
;
1042 struct partial_symbol
*sym2
= (struct partial_symbol
*) addr2
;
1044 return (memcmp (&sym1
->ginfo
.m_value
, &sym2
->ginfo
.m_value
,
1045 sizeof (sym1
->ginfo
.m_value
)) == 0
1046 && sym1
->ginfo
.language () == sym2
->ginfo
.language ()
1047 && sym1
->domain
== sym2
->domain
1048 && sym1
->aclass
== sym2
->aclass
1049 /* Note that psymbol names are interned via
1050 compute_and_set_names, so there's no need to compare the
1051 contents of the name here. */
1052 && sym1
->ginfo
.linkage_name () == sym2
->ginfo
.linkage_name ());
1055 /* See psymtab.h. */
1058 partial_symtab::add_psymbol (const partial_symbol
&psymbol
,
1059 psymbol_placement where
,
1060 psymtab_storage
*partial_symtabs
,
1061 struct objfile
*objfile
)
1065 /* Stash the partial symbol away in the cache. */
1066 const partial_symbol
*psym
= partial_symtabs
->psymbol_cache
.insert (psymbol
,
1069 /* Do not duplicate global partial symbols. */
1070 if (where
== psymbol_placement::GLOBAL
&& !added
)
1073 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1074 std::vector
<const partial_symbol
*> &list
1075 = (where
== psymbol_placement::STATIC
1078 list
.push_back (psym
);
1081 /* See psymtab.h. */
1084 partial_symtab::add_psymbol (std::string_view name
, bool copy_name
,
1086 enum address_class theclass
,
1088 psymbol_placement where
,
1089 unrelocated_addr coreaddr
,
1090 enum language language
,
1091 psymtab_storage
*partial_symtabs
,
1092 struct objfile
*objfile
)
1094 struct partial_symbol psymbol
;
1095 memset (&psymbol
, 0, sizeof (psymbol
));
1097 psymbol
.set_unrelocated_address (coreaddr
);
1098 psymbol
.ginfo
.set_section_index (section
);
1099 psymbol
.domain
= domain
;
1100 psymbol
.aclass
= theclass
;
1101 psymbol
.ginfo
.set_language (language
, partial_symtabs
->obstack ());
1102 psymbol
.ginfo
.compute_and_set_names (name
, copy_name
, objfile
->per_bfd
);
1104 add_psymbol (psymbol
, where
, partial_symtabs
, objfile
);
1107 /* See psymtab.h. */
1109 partial_symtab::partial_symtab (const char *filename_
,
1110 psymtab_storage
*partial_symtabs
,
1111 objfile_per_bfd_storage
*objfile_per_bfd
)
1112 : searched_flag (PST_NOT_SEARCHED
),
1116 partial_symtabs
->install_psymtab (this);
1118 filename
= objfile_per_bfd
->intern (filename_
);
1120 if (symtab_create_debug
>= 1)
1122 /* Be a bit clever with debugging messages, and don't print objfile
1123 every time, only when it changes. */
1124 static std::string last_bfd_name
;
1125 const char *this_bfd_name
1126 = bfd_get_filename (objfile_per_bfd
->get_bfd ());
1128 if (last_bfd_name
.empty () || last_bfd_name
!= this_bfd_name
)
1130 last_bfd_name
= this_bfd_name
;
1132 symtab_create_debug_printf ("creating one or more psymtabs for %s",
1136 symtab_create_debug_printf ("created psymtab %s for module %s",
1137 host_address_to_string (this), filename
);
1141 /* See psymtab.h. */
1144 partial_symtab::expand_dependencies (struct objfile
*objfile
)
1146 for (int i
= 0; i
< number_of_dependencies
; ++i
)
1148 if (!dependencies
[i
]->readin_p (objfile
)
1149 && dependencies
[i
]->user
== NULL
)
1151 /* Inform about additional files to be read in. */
1155 gdb_stdout
->wrap_here (0);
1157 gdb_stdout
->wrap_here (0);
1158 gdb_printf ("%s...", dependencies
[i
]->filename
);
1159 gdb_flush (gdb_stdout
);
1161 dependencies
[i
]->expand_psymtab (objfile
);
1168 psymtab_storage::discard_psymtab (struct partial_symtab
*pst
)
1170 struct partial_symtab
**prev_pst
;
1173 Empty psymtabs happen as a result of header files which don't
1174 have any symbols in them. There can be a lot of them. But this
1175 check is wrong, in that a psymtab with N_SLINE entries but
1176 nothing else is not empty, but we don't realize that. Fixing
1177 that without slowing things down might be tricky. */
1179 /* First, snip it out of the psymtab chain. */
1181 prev_pst
= &psymtabs
;
1182 while ((*prev_pst
) != pst
)
1183 prev_pst
= &((*prev_pst
)->next
);
1184 (*prev_pst
) = pst
->next
;
1191 maintenance_print_psymbols (const char *args
, int from_tty
)
1193 struct ui_file
*outfile
= gdb_stdout
;
1194 char *address_arg
= NULL
, *source_arg
= NULL
, *objfile_arg
= NULL
;
1195 int i
, outfile_idx
, found
;
1197 struct obj_section
*section
= NULL
;
1201 gdb_argv
argv (args
);
1203 for (i
= 0; argv
!= NULL
&& argv
[i
] != NULL
; ++i
)
1205 if (strcmp (argv
[i
], "-pc") == 0)
1207 if (argv
[i
+ 1] == NULL
)
1208 error (_("Missing pc value"));
1209 address_arg
= argv
[++i
];
1211 else if (strcmp (argv
[i
], "-source") == 0)
1213 if (argv
[i
+ 1] == NULL
)
1214 error (_("Missing source file"));
1215 source_arg
= argv
[++i
];
1217 else if (strcmp (argv
[i
], "-objfile") == 0)
1219 if (argv
[i
+ 1] == NULL
)
1220 error (_("Missing objfile name"));
1221 objfile_arg
= argv
[++i
];
1223 else if (strcmp (argv
[i
], "--") == 0)
1225 /* End of options. */
1229 else if (argv
[i
][0] == '-')
1231 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1232 error (_("Unknown option: %s"), argv
[i
]);
1239 if (address_arg
!= NULL
&& source_arg
!= NULL
)
1240 error (_("Must specify at most one of -pc and -source"));
1242 stdio_file arg_outfile
;
1244 if (argv
!= NULL
&& argv
[outfile_idx
] != NULL
)
1246 if (argv
[outfile_idx
+ 1] != NULL
)
1247 error (_("Junk at end of command"));
1248 gdb::unique_xmalloc_ptr
<char> outfile_name
1249 (tilde_expand (argv
[outfile_idx
]));
1250 if (!arg_outfile
.open (outfile_name
.get (), FOPEN_WT
))
1251 perror_with_name (outfile_name
.get ());
1252 outfile
= &arg_outfile
;
1255 if (address_arg
!= NULL
)
1257 pc
= parse_and_eval_address (address_arg
);
1258 /* If we fail to find a section, that's ok, try the lookup anyway. */
1259 section
= find_pc_section (pc
);
1263 for (objfile
*objfile
: current_program_space
->objfiles ())
1265 int printed_objfile_header
= 0;
1266 int print_for_objfile
= 1;
1269 if (objfile_arg
!= NULL
)
1271 = compare_filenames_for_search (objfile_name (objfile
),
1273 if (!print_for_objfile
)
1276 for (const auto &iter
: objfile
->qf
)
1278 psymbol_functions
*psf
1279 = dynamic_cast<psymbol_functions
*> (iter
.get ());
1283 if (address_arg
!= NULL
)
1285 bound_minimal_symbol msymbol
;
1287 /* We don't assume each pc has a unique objfile (this is for
1289 struct partial_symtab
*ps
1290 = psf
->find_pc_sect_psymtab (objfile
, pc
, section
, msymbol
);
1293 if (!printed_objfile_header
)
1295 outfile
->printf ("\nPartial symtabs for objfile %s\n",
1296 objfile_name (objfile
));
1297 printed_objfile_header
= 1;
1299 dump_psymtab (objfile
, ps
, outfile
);
1305 for (partial_symtab
*ps
: psf
->partial_symbols (objfile
))
1307 int print_for_source
= 0;
1310 if (source_arg
!= NULL
)
1313 = compare_filenames_for_search (ps
->filename
, source_arg
);
1316 if (source_arg
== NULL
1317 || print_for_source
)
1319 if (!printed_objfile_header
)
1321 outfile
->printf ("\nPartial symtabs for objfile %s\n",
1322 objfile_name (objfile
));
1323 printed_objfile_header
= 1;
1325 dump_psymtab (objfile
, ps
, outfile
);
1334 if (address_arg
!= NULL
)
1335 error (_("No partial symtab for address: %s"), address_arg
);
1336 if (source_arg
!= NULL
)
1337 error (_("No partial symtab for source file: %s"), source_arg
);
1341 /* List all the partial symbol tables whose names match REGEXP (optional). */
1344 maintenance_info_psymtabs (const char *regexp
, int from_tty
)
1349 for (struct program_space
*pspace
: program_spaces
)
1350 for (objfile
*objfile
: pspace
->objfiles ())
1352 struct gdbarch
*gdbarch
= objfile
->arch ();
1354 /* We don't want to print anything for this objfile until we
1355 actually find a symtab whose name matches. */
1356 int printed_objfile_start
= 0;
1358 for (const auto &iter
: objfile
->qf
)
1360 psymbol_functions
*psf
1361 = dynamic_cast<psymbol_functions
*> (iter
.get ());
1364 for (partial_symtab
*psymtab
: psf
->partial_symbols (objfile
))
1369 || re_exec (psymtab
->filename
))
1371 if (! printed_objfile_start
)
1373 gdb_printf ("{ objfile %s ", objfile_name (objfile
));
1374 gdb_stdout
->wrap_here (2);
1375 gdb_printf ("((struct objfile *) %s)\n",
1376 host_address_to_string (objfile
));
1377 printed_objfile_start
= 1;
1380 gdb_printf (" { psymtab %s ", psymtab
->filename
);
1381 gdb_stdout
->wrap_here (4);
1382 gdb_printf ("((struct partial_symtab *) %s)\n",
1383 host_address_to_string (psymtab
));
1385 gdb_printf (" readin %s\n",
1386 psymtab
->readin_p (objfile
) ? "yes" : "no");
1387 gdb_printf (" fullname %s\n",
1389 ? psymtab
->fullname
: "(null)");
1390 gdb_printf (" text addresses ");
1391 gdb_puts (paddress (gdbarch
,
1392 psymtab
->text_low (objfile
)));
1393 gdb_printf (" -- ");
1394 gdb_puts (paddress (gdbarch
,
1395 psymtab
->text_high (objfile
)));
1397 gdb_printf (" globals ");
1398 if (!psymtab
->global_psymbols
.empty ())
1400 ("(* (struct partial_symbol **) %s @ %d)\n",
1401 host_address_to_string (psymtab
->global_psymbols
.data ()),
1402 (int) psymtab
->global_psymbols
.size ());
1404 gdb_printf ("(none)\n");
1405 gdb_printf (" statics ");
1406 if (!psymtab
->static_psymbols
.empty ())
1408 ("(* (struct partial_symbol **) %s @ %d)\n",
1409 host_address_to_string (psymtab
->static_psymbols
.data ()),
1410 (int) psymtab
->static_psymbols
.size ());
1412 gdb_printf ("(none)\n");
1414 gdb_printf (" user %s "
1415 "((struct partial_symtab *) %s)\n",
1416 psymtab
->user
->filename
,
1417 host_address_to_string (psymtab
->user
));
1418 gdb_printf (" dependencies ");
1419 if (psymtab
->number_of_dependencies
)
1424 for (i
= 0; i
< psymtab
->number_of_dependencies
; i
++)
1426 struct partial_symtab
*dep
= psymtab
->dependencies
[i
];
1428 /* Note the string concatenation there --- no
1430 gdb_printf (" psymtab %s "
1431 "((struct partial_symtab *) %s)\n",
1433 host_address_to_string (dep
));
1435 gdb_printf (" }\n");
1438 gdb_printf ("(none)\n");
1439 gdb_printf (" }\n");
1444 if (printed_objfile_start
)
1449 /* Check consistency of currently expanded psymtabs vs symtabs. */
1452 maintenance_check_psymtabs (const char *ignore
, int from_tty
)
1455 struct compunit_symtab
*cust
= NULL
;
1456 const struct blockvector
*bv
;
1457 const struct block
*b
;
1459 for (objfile
*objfile
: current_program_space
->objfiles ())
1461 for (const auto &iter
: objfile
->qf
)
1463 psymbol_functions
*psf
1464 = dynamic_cast<psymbol_functions
*> (iter
.get ());
1468 for (partial_symtab
*ps
: psf
->partial_symbols (objfile
))
1470 struct gdbarch
*gdbarch
= objfile
->arch ();
1472 /* We don't call psymtab_to_symtab here because that may cause symtab
1473 expansion. When debugging a problem it helps if checkers leave
1474 things unchanged. */
1475 cust
= ps
->get_compunit_symtab (objfile
);
1477 /* First do some checks that don't require the associated symtab. */
1478 if (ps
->text_high (objfile
) < ps
->text_low (objfile
))
1480 gdb_printf ("Psymtab ");
1481 gdb_puts (ps
->filename
);
1482 gdb_printf (" covers bad range ");
1483 gdb_puts (paddress (gdbarch
, ps
->text_low (objfile
)));
1485 gdb_puts (paddress (gdbarch
, ps
->text_high (objfile
)));
1490 /* Now do checks requiring the associated symtab. */
1493 bv
= cust
->blockvector ();
1494 b
= bv
->static_block ();
1495 for (const partial_symbol
*psym
: ps
->static_psymbols
)
1497 /* Skip symbols for inlined functions without address. These may
1498 or may not have a match in the full symtab. */
1499 if (psym
->aclass
== LOC_BLOCK
1500 && psym
->ginfo
.value_address () == 0)
1503 lookup_name_info lookup_name
1504 (psym
->ginfo
.search_name (), symbol_name_match_type::SEARCH_NAME
);
1505 sym
= block_lookup_symbol (b
, lookup_name
,
1506 to_search_flags (psym
->domain
));
1509 gdb_printf ("Static symbol `");
1510 gdb_puts (psym
->ginfo
.linkage_name ());
1511 gdb_printf ("' only found in ");
1512 gdb_puts (ps
->filename
);
1513 gdb_printf (" psymtab\n");
1516 b
= bv
->global_block ();
1517 for (const partial_symbol
*psym
: ps
->global_psymbols
)
1519 lookup_name_info lookup_name
1520 (psym
->ginfo
.search_name (), symbol_name_match_type::SEARCH_NAME
);
1521 sym
= block_lookup_symbol (b
, lookup_name
,
1522 to_search_flags (psym
->domain
));
1525 gdb_printf ("Global symbol `");
1526 gdb_puts (psym
->ginfo
.linkage_name ());
1527 gdb_printf ("' only found in ");
1528 gdb_puts (ps
->filename
);
1529 gdb_printf (" psymtab\n");
1532 if (ps
->unrelocated_text_high () != unrelocated_addr (0)
1533 && (ps
->text_low (objfile
) < b
->start ()
1534 || ps
->text_high (objfile
) > b
->end ()))
1536 gdb_printf ("Psymtab ");
1537 gdb_puts (ps
->filename
);
1538 gdb_printf (" covers ");
1539 gdb_puts (paddress (gdbarch
, ps
->text_low (objfile
)));
1541 gdb_puts (paddress (gdbarch
, ps
->text_high (objfile
)));
1542 gdb_printf (" but symtab covers only ");
1543 gdb_puts (paddress (gdbarch
, b
->start ()));
1545 gdb_puts (paddress (gdbarch
, b
->end ()));
1553 void _initialize_psymtab ();
1555 _initialize_psymtab ()
1557 add_cmd ("psymbols", class_maintenance
, maintenance_print_psymbols
, _("\
1558 Print dump of current partial symbol definitions.\n\
1559 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
1560 mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
1561 Entries in the partial symbol table are dumped to file OUTFILE,\n\
1562 or the terminal if OUTFILE is unspecified.\n\
1563 If ADDRESS is provided, dump only the symbols for the file with code at that address.\n\
1564 If SOURCE is provided, dump only that file's symbols.\n\
1565 If OBJFILE is provided, dump only that object file's symbols."),
1566 &maintenanceprintlist
);
1568 add_cmd ("psymtabs", class_maintenance
, maintenance_info_psymtabs
, _("\
1569 List the partial symbol tables for all object files.\n\
1570 This does not include information about individual partial symbols,\n\
1571 just the symbol table structures themselves."),
1572 &maintenanceinfolist
);
1574 add_cmd ("check-psymtabs", class_maintenance
, maintenance_check_psymtabs
,
1576 Check consistency of currently expanded psymtabs versus symtabs."),