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/>. */
25 #include "filenames.h"
31 #include "readline/tilde.h"
32 #include "gdbsupport/gdb_regex.h"
33 #include "dictionary.h"
35 #include "cp-support.h"
39 #include "gdbsupport/buildargv.h"
41 static struct partial_symbol
*lookup_partial_symbol (struct objfile
*,
42 struct partial_symtab
*,
43 const lookup_name_info
&,
47 static const char *psymtab_to_fullname (struct partial_symtab
*ps
);
49 static struct partial_symbol
*find_pc_sect_psymbol (struct objfile
*,
50 struct partial_symtab
*,
52 struct obj_section
*);
54 static struct compunit_symtab
*psymtab_to_symtab (struct objfile
*objfile
,
55 struct partial_symtab
*pst
);
57 psymtab_storage::~psymtab_storage ()
59 partial_symtab
*iter
= psymtabs
;
60 while (iter
!= nullptr)
62 partial_symtab
*next
= iter
->next
;
71 psymtab_storage::install_psymtab (partial_symtab
*pst
)
81 psymtab_storage::partial_symtab_range
82 psymbol_functions::partial_symbols (struct objfile
*objfile
)
84 return m_partial_symtabs
->range ();
87 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
88 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
90 static struct partial_symtab
*
91 find_pc_sect_psymtab_closer (struct objfile
*objfile
,
92 CORE_ADDR pc
, struct obj_section
*section
,
93 struct partial_symtab
*pst
,
94 struct bound_minimal_symbol msymbol
)
96 struct partial_symtab
*tpst
;
97 struct partial_symtab
*best_pst
= pst
;
98 CORE_ADDR best_addr
= pst
->text_low (objfile
);
100 /* An objfile that has its functions reordered might have
101 many partial symbol tables containing the PC, but
102 we want the partial symbol table that contains the
103 function containing the PC. */
104 if (section
== nullptr)
107 if (msymbol
.minsym
== NULL
)
110 /* The code range of partial symtabs sometimes overlap, so, in
111 the loop below, we need to check all partial symtabs and
112 find the one that fits better for the given PC address. We
113 select the partial symtab that contains a symbol whose
114 address is closest to the PC address. By closest we mean
115 that find_pc_sect_symbol returns the symbol with address
116 that is closest and still less than the given PC. */
117 for (tpst
= pst
; tpst
!= NULL
; tpst
= tpst
->next
)
119 if (pc
>= tpst
->text_low (objfile
) && pc
< tpst
->text_high (objfile
))
121 struct partial_symbol
*p
;
124 /* NOTE: This assumes that every psymbol has a
125 corresponding msymbol, which is not necessarily
126 true; the debug info might be much richer than the
127 object's symbol table. */
128 p
= find_pc_sect_psymbol (objfile
, tpst
, pc
, section
);
130 && (p
->address (objfile
) == msymbol
.value_address ()))
133 /* Also accept the textlow value of a psymtab as a
134 "symbol", to provide some support for partial
135 symbol tables with line information but no debug
136 symbols (e.g. those produced by an assembler). */
138 this_addr
= p
->address (objfile
);
140 this_addr
= tpst
->text_low (objfile
);
142 /* Check whether it is closer than our current
143 BEST_ADDR. Since this symbol address is
144 necessarily lower or equal to PC, the symbol closer
145 to PC is the symbol which address is the highest.
146 This way we return the psymtab which contains such
147 best match symbol. This can help in cases where the
148 symbol information/debuginfo is not complete, like
149 for instance on IRIX6 with gcc, where no debug info
150 is emitted for statics. (See also the nodebug.exp
152 if (this_addr
> best_addr
)
154 best_addr
= this_addr
;
164 struct partial_symtab
*
165 psymbol_functions::find_pc_sect_psymtab (struct objfile
*objfile
,
167 struct obj_section
*section
,
168 struct bound_minimal_symbol msymbol
)
170 for (partial_symtab
*pst
: partial_symbols (objfile
))
171 if (pc
>= pst
->text_low (objfile
) && pc
< pst
->text_high (objfile
))
173 struct partial_symtab
*best_pst
;
175 best_pst
= find_pc_sect_psymtab_closer (objfile
, pc
, section
, pst
,
177 if (best_pst
!= NULL
)
184 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
185 the definition of quick_symbol_functions in symfile.h. */
187 struct compunit_symtab
*
188 psymbol_functions::find_pc_sect_compunit_symtab
189 (struct objfile
*objfile
,
190 struct bound_minimal_symbol msymbol
,
192 struct obj_section
*section
,
195 struct partial_symtab
*ps
= find_pc_sect_psymtab (objfile
,
200 if (warn_if_readin
&& ps
->readin_p (objfile
))
201 /* Might want to error() here (in case symtab is corrupt and
202 will cause a core dump), but maybe we can successfully
203 continue, so let's not. */
205 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
206 paddress (objfile
->arch (), pc
));
207 psymtab_to_symtab (objfile
, ps
);
208 return ps
->get_compunit_symtab (objfile
);
213 /* Find which partial symbol within a psymtab matches PC and SECTION.
214 Return NULL if none. */
216 static struct partial_symbol
*
217 find_pc_sect_psymbol (struct objfile
*objfile
,
218 struct partial_symtab
*psymtab
, CORE_ADDR pc
,
219 struct obj_section
*section
)
221 struct partial_symbol
*best
= NULL
;
223 const CORE_ADDR textlow
= psymtab
->text_low (objfile
);
225 gdb_assert (psymtab
!= NULL
);
227 /* Cope with programs that start at address 0. */
228 best_pc
= (textlow
!= 0) ? textlow
- 1 : 0;
230 /* Search the global symbols as well as the static symbols, so that
231 find_pc_partial_function doesn't use a minimal symbol and thus
232 cache a bad endaddr. */
233 for (partial_symbol
*p
: psymtab
->global_psymbols
)
235 if (p
->domain
== VAR_DOMAIN
236 && p
->aclass
== LOC_BLOCK
237 && pc
>= p
->address (objfile
)
238 && (p
->address (objfile
) > best_pc
239 || (psymtab
->text_low (objfile
) == 0
240 && best_pc
== 0 && p
->address (objfile
) == 0)))
242 if (section
!= NULL
) /* Match on a specific section. */
244 if (!matching_obj_sections (p
->obj_section (objfile
),
248 best_pc
= p
->address (objfile
);
253 for (partial_symbol
*p
: psymtab
->static_psymbols
)
255 if (p
->domain
== VAR_DOMAIN
256 && p
->aclass
== LOC_BLOCK
257 && pc
>= p
->address (objfile
)
258 && (p
->address (objfile
) > best_pc
259 || (psymtab
->text_low (objfile
) == 0
260 && best_pc
== 0 && p
->address (objfile
) == 0)))
262 if (section
!= NULL
) /* Match on a specific section. */
264 if (!matching_obj_sections (p
->obj_section (objfile
),
268 best_pc
= p
->address (objfile
);
276 /* Psymtab version of lookup_global_symbol_language. See its definition in
277 the definition of quick_symbol_functions in symfile.h. */
280 psymbol_functions::lookup_global_symbol_language (struct objfile
*objfile
,
282 domain_search_flags domain
,
283 bool *symbol_found_p
)
285 *symbol_found_p
= false;
286 if (objfile
->sf
== NULL
)
287 return language_unknown
;
289 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
291 for (partial_symtab
*ps
: partial_symbols (objfile
))
293 struct partial_symbol
*psym
;
294 if (ps
->readin_p (objfile
))
297 psym
= lookup_partial_symbol (objfile
, ps
, lookup_name
, 1, domain
);
300 *symbol_found_p
= true;
301 return psym
->ginfo
.language ();
305 return language_unknown
;
308 /* Returns true if PSYM matches LOOKUP_NAME. */
311 psymbol_name_matches (partial_symbol
*psym
,
312 const lookup_name_info
&lookup_name
)
314 const language_defn
*lang
= language_def (psym
->ginfo
.language ());
315 symbol_name_matcher_ftype
*name_match
316 = lang
->get_symbol_name_matcher (lookup_name
);
317 return name_match (psym
->ginfo
.search_name (), lookup_name
, NULL
);
320 /* Look, in partial_symtab PST, for symbol whose natural name is
321 LOOKUP_NAME. Check the global symbols if GLOBAL, the static
324 static struct partial_symbol
*
325 lookup_partial_symbol (struct objfile
*objfile
,
326 struct partial_symtab
*pst
,
327 const lookup_name_info
&lookup_name
,
328 int global
, domain_search_flags domain
)
330 struct partial_symbol
**start
, **psym
;
331 struct partial_symbol
**top
, **real_top
, **bottom
, **center
;
333 ? pst
->global_psymbols
.size ()
334 : pst
->static_psymbols
.size ());
335 int do_linear_search
= 1;
341 &pst
->global_psymbols
[0] :
342 &pst
->static_psymbols
[0]);
344 if (global
) /* This means we can use a binary search. */
346 do_linear_search
= 0;
348 /* Binary search. This search is guaranteed to end with center
349 pointing at the earliest partial symbol whose name might be
350 correct. At that point *all* partial symbols with an
351 appropriate name will be checked against the correct
355 top
= start
+ length
- 1;
359 center
= bottom
+ (top
- bottom
) / 2;
361 gdb_assert (center
< top
);
363 if (strcmp_iw_ordered ((*center
)->ginfo
.search_name (),
364 lookup_name
.c_str ()) >= 0)
374 gdb_assert (top
== bottom
);
376 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
377 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
378 while (top
>= start
&& symbol_matches_search_name (&(*top
)->ginfo
,
382 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
385 while (top
<= real_top
&& symbol_matches_search_name (&(*top
)->ginfo
,
388 if (search_flags_matches (domain
, (*top
)->domain
))
394 /* Can't use a binary search or else we found during the binary search that
395 we should also do a linear search. */
397 if (do_linear_search
)
399 for (psym
= start
; psym
< start
+ length
; psym
++)
401 if (search_flags_matches (domain
, (*psym
)->domain
)
402 && symbol_matches_search_name (&(*psym
)->ginfo
, lookup_name
))
410 /* Get the symbol table that corresponds to a partial_symtab.
411 This is fast after the first time you do it.
412 The result will be NULL if the primary symtab has no symbols,
413 which can happen. Otherwise the result is the primary symtab
414 that contains PST. */
416 static struct compunit_symtab
*
417 psymtab_to_symtab (struct objfile
*objfile
, struct partial_symtab
*pst
)
419 /* If it is a shared psymtab, find an unshared psymtab that includes
420 it. Any such psymtab will do. */
421 while (pst
->user
!= NULL
)
424 /* If it's been looked up before, return it. */
425 if (pst
->get_compunit_symtab (objfile
))
426 return pst
->get_compunit_symtab (objfile
);
428 /* If it has not yet been read in, read it. */
429 if (!pst
->readin_p (objfile
))
431 scoped_restore decrementer
= increment_reading_symtab ();
435 gdb_printf (_("Reading in symbols for %s...\n"),
437 gdb_flush (gdb_stdout
);
440 pst
->read_symtab (objfile
);
443 return pst
->get_compunit_symtab (objfile
);
446 /* Psymtab version of find_last_source_symtab. See its definition in
447 the definition of quick_symbol_functions in symfile.h. */
450 psymbol_functions::find_last_source_symtab (struct objfile
*ofp
)
452 struct partial_symtab
*cs_pst
= NULL
;
454 for (partial_symtab
*ps
: partial_symbols (ofp
))
456 const char *name
= ps
->filename
;
457 int len
= strlen (name
);
459 if (!(len
> 2 && (strcmp (&name
[len
- 2], ".h") == 0
460 || strcmp (name
, "<<C++-namespaces>>") == 0)))
466 if (cs_pst
->readin_p (ofp
))
468 internal_error (_("select_source_symtab: "
469 "readin pst found and no symtabs."));
473 struct compunit_symtab
*cust
= psymtab_to_symtab (ofp
, cs_pst
);
477 return cust
->primary_filetab ();
483 /* Psymtab version of forget_cached_source_info. See its definition in
484 the definition of quick_symbol_functions in symfile.h. */
487 psymbol_functions::forget_cached_source_info (struct objfile
*objfile
)
489 for (partial_symtab
*pst
: partial_symbols (objfile
))
491 if (pst
->fullname
!= NULL
)
493 xfree (pst
->fullname
);
494 pst
->fullname
= NULL
;
500 print_partial_symbols (struct gdbarch
*gdbarch
, struct objfile
*objfile
,
501 const std::vector
<partial_symbol
*> &symbols
,
502 const char *what
, struct ui_file
*outfile
)
504 gdb_printf (outfile
, " %s partial symbols:\n", what
);
505 for (partial_symbol
*p
: symbols
)
508 gdb_printf (outfile
, " `%s'", p
->ginfo
.linkage_name ());
509 if (p
->ginfo
.demangled_name () != NULL
)
511 gdb_printf (outfile
, " `%s'",
512 p
->ginfo
.demangled_name ());
514 gdb_puts (", ", outfile
);
518 gdb_puts ("undefined domain, ", outfile
);
521 /* This is the usual thing -- don't print it. */
524 gdb_puts ("struct domain, ", outfile
);
527 gdb_puts ("module domain, ", outfile
);
530 gdb_puts ("label domain, ", outfile
);
532 case COMMON_BLOCK_DOMAIN
:
533 gdb_puts ("common block domain, ", outfile
);
536 gdb_puts ("<invalid domain>, ", outfile
);
542 gdb_puts ("undefined", outfile
);
545 gdb_puts ("constant int", outfile
);
548 gdb_puts ("static", outfile
);
551 gdb_puts ("register", outfile
);
554 gdb_puts ("pass by value", outfile
);
557 gdb_puts ("pass by reference", outfile
);
559 case LOC_REGPARM_ADDR
:
560 gdb_puts ("register address parameter", outfile
);
563 gdb_puts ("stack parameter", outfile
);
566 gdb_puts ("type", outfile
);
569 gdb_puts ("label", outfile
);
572 gdb_puts ("function", outfile
);
574 case LOC_CONST_BYTES
:
575 gdb_puts ("constant bytes", outfile
);
578 gdb_puts ("unresolved", outfile
);
580 case LOC_OPTIMIZED_OUT
:
581 gdb_puts ("optimized out", outfile
);
584 gdb_puts ("computed at runtime", outfile
);
587 gdb_puts ("<invalid location>", outfile
);
590 gdb_puts (", ", outfile
);
591 gdb_puts (paddress (gdbarch
, CORE_ADDR (p
->unrelocated_address ())),
593 gdb_printf (outfile
, "\n");
598 dump_psymtab (struct objfile
*objfile
, struct partial_symtab
*psymtab
,
599 struct ui_file
*outfile
)
601 struct gdbarch
*gdbarch
= objfile
->arch ();
604 if (psymtab
->anonymous
)
606 gdb_printf (outfile
, "\nAnonymous partial symtab (%s) ",
611 gdb_printf (outfile
, "\nPartial symtab for source file %s ",
614 gdb_printf (outfile
, "(object %s)\n\n",
615 host_address_to_string (psymtab
));
616 gdb_printf (outfile
, " Read from object file %s (%s)\n",
617 objfile_name (objfile
),
618 host_address_to_string (objfile
));
620 if (psymtab
->readin_p (objfile
))
623 " Full symtab was read (at %s)\n",
624 host_address_to_string (psymtab
->get_compunit_symtab (objfile
)));
626 gdb_printf (outfile
, " Symbols cover text addresses ");
627 gdb_puts (paddress (gdbarch
, psymtab
->text_low (objfile
)), outfile
);
628 gdb_printf (outfile
, "-");
629 gdb_puts (paddress (gdbarch
, psymtab
->text_high (objfile
)), outfile
);
630 gdb_printf (outfile
, "\n");
631 gdb_printf (outfile
, " Depends on %d other partial symtabs.\n",
632 psymtab
->number_of_dependencies
);
633 for (i
= 0; i
< psymtab
->number_of_dependencies
; i
++)
634 gdb_printf (outfile
, " %d %s\n", i
,
635 host_address_to_string (psymtab
->dependencies
[i
]));
636 if (psymtab
->user
!= NULL
)
637 gdb_printf (outfile
, " Shared partial symtab with user %s\n",
638 host_address_to_string (psymtab
->user
));
639 if (!psymtab
->global_psymbols
.empty ())
641 print_partial_symbols
642 (gdbarch
, objfile
, psymtab
->global_psymbols
,
645 if (!psymtab
->static_psymbols
.empty ())
647 print_partial_symbols
648 (gdbarch
, objfile
, psymtab
->static_psymbols
,
651 gdb_printf (outfile
, "\n");
654 /* Count the number of partial symbols in OBJFILE. */
657 psymbol_functions::count_psyms ()
660 for (partial_symtab
*pst
: m_partial_symtabs
->range ())
662 count
+= pst
->global_psymbols
.size ();
663 count
+= pst
->static_psymbols
.size ();
668 /* Psymtab version of print_stats. See its definition in
669 the definition of quick_symbol_functions in symfile.h. */
672 psymbol_functions::print_stats (struct objfile
*objfile
, bool print_bcache
)
678 int n_psyms
= count_psyms ();
680 gdb_printf (_(" Number of \"partial\" symbols read: %d\n"),
684 for (partial_symtab
*ps
: partial_symbols (objfile
))
686 if (!ps
->readin_p (objfile
))
689 gdb_printf (_(" Number of psym tables (not yet expanded): %d\n"),
691 gdb_printf (_(" Total memory used for psymbol cache: %d\n"),
692 m_partial_symtabs
->psymbol_cache
.memory_used ());
696 gdb_printf (_("Psymbol byte cache statistics:\n"));
697 m_partial_symtabs
->psymbol_cache
.print_statistics
698 ("partial symbol cache");
702 /* Psymtab version of dump. See its definition in
703 the definition of quick_symbol_functions in symfile.h. */
706 psymbol_functions::dump (struct objfile
*objfile
)
708 struct partial_symtab
*psymtab
;
710 if (m_partial_symtabs
->psymtabs
)
712 gdb_printf ("Psymtabs:\n");
713 for (psymtab
= m_partial_symtabs
->psymtabs
;
715 psymtab
= psymtab
->next
)
716 gdb_printf ("%s at %s\n",
718 host_address_to_string (psymtab
));
723 /* Psymtab version of expand_all_symtabs. See its definition in
724 the definition of quick_symbol_functions in symfile.h. */
727 psymbol_functions::expand_all_symtabs (struct objfile
*objfile
)
729 for (partial_symtab
*psymtab
: partial_symbols (objfile
))
730 psymtab_to_symtab (objfile
, psymtab
);
733 /* Psymtab version of map_symbol_filenames. See its definition in
734 the definition of quick_symbol_functions in symfile.h. */
737 psymbol_functions::map_symbol_filenames
738 (struct objfile
*objfile
,
739 gdb::function_view
<symbol_filename_ftype
> fun
,
742 for (partial_symtab
*ps
: partial_symbols (objfile
))
744 const char *fullname
;
746 if (ps
->readin_p (objfile
))
749 /* We can skip shared psymtabs here, because any file name will be
750 attached to the unshared psymtab. */
751 if (ps
->user
!= NULL
)
754 /* Anonymous psymtabs don't have a file name. */
760 fullname
= psymtab_to_fullname (ps
);
763 fun (ps
->filename
, fullname
);
767 /* Finds the fullname that a partial_symtab represents.
769 If this functions finds the fullname, it will save it in ps->fullname
770 and it will also return the value.
772 If this function fails to find the file that this partial_symtab represents,
773 NULL will be returned and ps->fullname will be set to NULL. */
776 psymtab_to_fullname (struct partial_symtab
*ps
)
778 gdb_assert (!ps
->anonymous
);
780 /* Use cached copy if we have it.
781 We rely on forget_cached_source_info being called appropriately
782 to handle cases like the file being moved. */
783 if (ps
->fullname
== NULL
)
785 gdb::unique_xmalloc_ptr
<char> fullname
786 = find_source_or_rewrite (ps
->filename
, ps
->dirname
);
787 ps
->fullname
= fullname
.release ();
793 /* A helper for psym_expand_symtabs_matching that handles searching
794 included psymtabs. This returns true if a symbol is found, and
795 false otherwise. It also updates the 'searched_flag' on the
796 various psymtabs that it searches. */
799 recursively_search_psymtabs
800 (struct partial_symtab
*ps
,
801 struct objfile
*objfile
,
802 block_search_flags search_flags
,
803 domain_search_flags domain
,
804 const lookup_name_info
&lookup_name
,
805 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> sym_matcher
)
808 enum psymtab_search_status result
= PST_SEARCHED_AND_NOT_FOUND
;
811 if (ps
->searched_flag
!= PST_NOT_SEARCHED
)
812 return ps
->searched_flag
== PST_SEARCHED_AND_FOUND
;
814 /* Recurse into shared psymtabs first, because they may have already
815 been searched, and this could save some time. */
816 for (i
= 0; i
< ps
->number_of_dependencies
; ++i
)
820 /* Skip non-shared dependencies, these are handled elsewhere. */
821 if (ps
->dependencies
[i
]->user
== NULL
)
824 r
= recursively_search_psymtabs (ps
->dependencies
[i
],
825 objfile
, search_flags
, domain
,
826 lookup_name
, sym_matcher
);
829 ps
->searched_flag
= PST_SEARCHED_AND_FOUND
;
834 partial_symbol
**gbound
= (ps
->global_psymbols
.data ()
835 + ps
->global_psymbols
.size ());
836 partial_symbol
**sbound
= (ps
->static_psymbols
.data ()
837 + ps
->static_psymbols
.size ());
838 partial_symbol
**bound
= gbound
;
840 /* Go through all of the symbols stored in a partial
841 symtab in one loop. */
842 partial_symbol
**psym
= ps
->global_psymbols
.data ();
844 if ((search_flags
& SEARCH_GLOBAL_BLOCK
) == 0)
846 if (ps
->static_psymbols
.empty ())
850 psym
= ps
->static_psymbols
.data ();
859 if (bound
== gbound
&& !ps
->static_psymbols
.empty ()
860 && (search_flags
& SEARCH_STATIC_BLOCK
) != 0)
862 psym
= ps
->static_psymbols
.data ();
873 if (search_flags_matches (domain
, (*psym
)->domain
)
874 && psymbol_name_matches (*psym
, lookup_name
)
875 && (sym_matcher
== NULL
876 || sym_matcher ((*psym
)->ginfo
.search_name ())))
878 /* Found a match, so notify our caller. */
879 result
= PST_SEARCHED_AND_FOUND
;
886 ps
->searched_flag
= result
;
887 return result
== PST_SEARCHED_AND_FOUND
;
890 /* Psymtab version of expand_symtabs_matching. See its definition in
891 the definition of quick_symbol_functions in symfile.h. */
894 psymbol_functions::expand_symtabs_matching
895 (struct objfile
*objfile
,
896 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
897 const lookup_name_info
*lookup_name
,
898 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
899 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
900 block_search_flags search_flags
,
901 domain_search_flags domain
)
903 /* Clear the search flags. */
904 for (partial_symtab
*ps
: partial_symbols (objfile
))
905 ps
->searched_flag
= PST_NOT_SEARCHED
;
907 std::optional
<lookup_name_info
> psym_lookup_name
;
908 if (lookup_name
!= nullptr)
909 psym_lookup_name
= lookup_name
->make_ignore_params ();
911 /* This invariant is documented in quick-functions.h. */
912 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
914 for (partial_symtab
*ps
: m_partial_symtabs
->range ())
918 if (ps
->readin_p (objfile
))
928 match
= file_matcher (ps
->filename
, false);
931 /* Before we invoke realpath, which can get expensive when many
932 files are involved, do a quick comparison of the basenames. */
933 if (basenames_may_differ
934 || file_matcher (lbasename (ps
->filename
), true))
935 match
= file_matcher (psymtab_to_fullname (ps
), false);
941 if (lookup_name
== nullptr
942 || recursively_search_psymtabs (ps
, objfile
, search_flags
,
943 domain
, *psym_lookup_name
,
946 compunit_symtab
*cust
= psymtab_to_symtab (objfile
, ps
);
948 if (cust
!= nullptr && expansion_notify
!= nullptr)
949 if (!expansion_notify (cust
))
957 /* Psymtab version of has_symbols. See its definition in
958 the definition of quick_symbol_functions in symfile.h. */
961 psymbol_functions::has_symbols (struct objfile
*objfile
)
963 return m_partial_symtabs
->psymtabs
!= NULL
;
966 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
969 psymbol_functions::has_unexpanded_symtabs (struct objfile
*objfile
)
971 for (partial_symtab
*psymtab
: partial_symbols (objfile
))
973 /* Is this already expanded? */
974 if (psymtab
->readin_p (objfile
))
977 /* It has not yet been expanded. */
986 /* Partially fill a partial symtab. It will be completely filled at
987 the end of the symbol list. */
989 partial_symtab::partial_symtab (const char *filename
,
990 psymtab_storage
*partial_symtabs
,
991 objfile_per_bfd_storage
*objfile_per_bfd
,
992 unrelocated_addr textlow
)
993 : partial_symtab (filename
, partial_symtabs
, objfile_per_bfd
)
995 set_text_low (textlow
);
996 set_text_high (unrelocated_text_low ()); /* default */
999 /* Perform "finishing up" operations of a partial symtab. */
1002 partial_symtab::end ()
1004 global_psymbols
.shrink_to_fit ();
1005 static_psymbols
.shrink_to_fit ();
1007 /* Sort the global list; don't sort the static list. */
1008 std::sort (global_psymbols
.begin (),
1009 global_psymbols
.end (),
1010 [] (partial_symbol
*s1
, partial_symbol
*s2
)
1012 return strcmp_iw_ordered (s1
->ginfo
.search_name (),
1013 s2
->ginfo
.search_name ()) < 0;
1017 /* See psymtab.h. */
1020 psymbol_bcache::hash (const void *addr
, int length
)
1022 unsigned long h
= 0;
1023 struct partial_symbol
*psymbol
= (struct partial_symbol
*) addr
;
1024 unsigned int lang
= psymbol
->ginfo
.language ();
1025 unsigned int domain
= psymbol
->domain
;
1026 unsigned int theclass
= psymbol
->aclass
;
1028 h
= fast_hash (&psymbol
->ginfo
.m_value
, sizeof (psymbol
->ginfo
.m_value
), h
);
1029 h
= fast_hash (&lang
, sizeof (unsigned int), h
);
1030 h
= fast_hash (&domain
, sizeof (unsigned int), h
);
1031 h
= fast_hash (&theclass
, sizeof (unsigned int), h
);
1032 /* Note that psymbol names are interned via compute_and_set_names, so
1033 there's no need to hash the contents of the name here. */
1034 h
= fast_hash (&psymbol
->ginfo
.m_name
, sizeof (psymbol
->ginfo
.m_name
), h
);
1039 /* See psymtab.h. */
1042 psymbol_bcache::compare (const void *addr1
, const void *addr2
, int length
)
1044 struct partial_symbol
*sym1
= (struct partial_symbol
*) addr1
;
1045 struct partial_symbol
*sym2
= (struct partial_symbol
*) addr2
;
1047 return (memcmp (&sym1
->ginfo
.m_value
, &sym2
->ginfo
.m_value
,
1048 sizeof (sym1
->ginfo
.m_value
)) == 0
1049 && sym1
->ginfo
.language () == sym2
->ginfo
.language ()
1050 && sym1
->domain
== sym2
->domain
1051 && sym1
->aclass
== sym2
->aclass
1052 /* Note that psymbol names are interned via
1053 compute_and_set_names, so there's no need to compare the
1054 contents of the name here. */
1055 && sym1
->ginfo
.linkage_name () == sym2
->ginfo
.linkage_name ());
1058 /* See psymtab.h. */
1061 partial_symtab::add_psymbol (const partial_symbol
&psymbol
,
1062 psymbol_placement where
,
1063 psymtab_storage
*partial_symtabs
,
1064 struct objfile
*objfile
)
1068 /* Stash the partial symbol away in the cache. */
1069 partial_symbol
*psym
1070 = ((struct partial_symbol
*)
1071 partial_symtabs
->psymbol_cache
.insert
1072 (&psymbol
, sizeof (struct partial_symbol
), &added
));
1074 /* Do not duplicate global partial symbols. */
1075 if (where
== psymbol_placement::GLOBAL
&& !added
)
1078 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1079 std::vector
<partial_symbol
*> &list
1080 = (where
== psymbol_placement::STATIC
1083 list
.push_back (psym
);
1086 /* See psymtab.h. */
1089 partial_symtab::add_psymbol (std::string_view name
, bool copy_name
,
1091 enum address_class theclass
,
1093 psymbol_placement where
,
1094 unrelocated_addr coreaddr
,
1095 enum language language
,
1096 psymtab_storage
*partial_symtabs
,
1097 struct objfile
*objfile
)
1099 struct partial_symbol psymbol
;
1100 memset (&psymbol
, 0, sizeof (psymbol
));
1102 psymbol
.set_unrelocated_address (coreaddr
);
1103 psymbol
.ginfo
.set_section_index (section
);
1104 psymbol
.domain
= domain
;
1105 psymbol
.aclass
= theclass
;
1106 psymbol
.ginfo
.set_language (language
, partial_symtabs
->obstack ());
1107 psymbol
.ginfo
.compute_and_set_names (name
, copy_name
, objfile
->per_bfd
);
1109 add_psymbol (psymbol
, where
, partial_symtabs
, objfile
);
1112 /* See psymtab.h. */
1114 partial_symtab::partial_symtab (const char *filename_
,
1115 psymtab_storage
*partial_symtabs
,
1116 objfile_per_bfd_storage
*objfile_per_bfd
)
1117 : searched_flag (PST_NOT_SEARCHED
),
1121 partial_symtabs
->install_psymtab (this);
1123 filename
= objfile_per_bfd
->intern (filename_
);
1125 if (symtab_create_debug
>= 1)
1127 /* Be a bit clever with debugging messages, and don't print objfile
1128 every time, only when it changes. */
1129 static std::string last_bfd_name
;
1130 const char *this_bfd_name
1131 = bfd_get_filename (objfile_per_bfd
->get_bfd ());
1133 if (last_bfd_name
.empty () || last_bfd_name
!= this_bfd_name
)
1135 last_bfd_name
= this_bfd_name
;
1137 symtab_create_debug_printf ("creating one or more psymtabs for %s",
1141 symtab_create_debug_printf ("created psymtab %s for module %s",
1142 host_address_to_string (this), filename
);
1146 /* See psymtab.h. */
1149 partial_symtab::expand_dependencies (struct objfile
*objfile
)
1151 for (int i
= 0; i
< number_of_dependencies
; ++i
)
1153 if (!dependencies
[i
]->readin_p (objfile
)
1154 && dependencies
[i
]->user
== NULL
)
1156 /* Inform about additional files to be read in. */
1160 gdb_stdout
->wrap_here (0);
1162 gdb_stdout
->wrap_here (0);
1163 gdb_printf ("%s...", dependencies
[i
]->filename
);
1164 gdb_flush (gdb_stdout
);
1166 dependencies
[i
]->expand_psymtab (objfile
);
1173 psymtab_storage::discard_psymtab (struct partial_symtab
*pst
)
1175 struct partial_symtab
**prev_pst
;
1178 Empty psymtabs happen as a result of header files which don't
1179 have any symbols in them. There can be a lot of them. But this
1180 check is wrong, in that a psymtab with N_SLINE entries but
1181 nothing else is not empty, but we don't realize that. Fixing
1182 that without slowing things down might be tricky. */
1184 /* First, snip it out of the psymtab chain. */
1186 prev_pst
= &psymtabs
;
1187 while ((*prev_pst
) != pst
)
1188 prev_pst
= &((*prev_pst
)->next
);
1189 (*prev_pst
) = pst
->next
;
1196 maintenance_print_psymbols (const char *args
, int from_tty
)
1198 struct ui_file
*outfile
= gdb_stdout
;
1199 char *address_arg
= NULL
, *source_arg
= NULL
, *objfile_arg
= NULL
;
1200 int i
, outfile_idx
, found
;
1202 struct obj_section
*section
= NULL
;
1206 gdb_argv
argv (args
);
1208 for (i
= 0; argv
!= NULL
&& argv
[i
] != NULL
; ++i
)
1210 if (strcmp (argv
[i
], "-pc") == 0)
1212 if (argv
[i
+ 1] == NULL
)
1213 error (_("Missing pc value"));
1214 address_arg
= argv
[++i
];
1216 else if (strcmp (argv
[i
], "-source") == 0)
1218 if (argv
[i
+ 1] == NULL
)
1219 error (_("Missing source file"));
1220 source_arg
= argv
[++i
];
1222 else if (strcmp (argv
[i
], "-objfile") == 0)
1224 if (argv
[i
+ 1] == NULL
)
1225 error (_("Missing objfile name"));
1226 objfile_arg
= argv
[++i
];
1228 else if (strcmp (argv
[i
], "--") == 0)
1230 /* End of options. */
1234 else if (argv
[i
][0] == '-')
1236 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1237 error (_("Unknown option: %s"), argv
[i
]);
1244 if (address_arg
!= NULL
&& source_arg
!= NULL
)
1245 error (_("Must specify at most one of -pc and -source"));
1247 stdio_file arg_outfile
;
1249 if (argv
!= NULL
&& argv
[outfile_idx
] != NULL
)
1251 if (argv
[outfile_idx
+ 1] != NULL
)
1252 error (_("Junk at end of command"));
1253 gdb::unique_xmalloc_ptr
<char> outfile_name
1254 (tilde_expand (argv
[outfile_idx
]));
1255 if (!arg_outfile
.open (outfile_name
.get (), FOPEN_WT
))
1256 perror_with_name (outfile_name
.get ());
1257 outfile
= &arg_outfile
;
1260 if (address_arg
!= NULL
)
1262 pc
= parse_and_eval_address (address_arg
);
1263 /* If we fail to find a section, that's ok, try the lookup anyway. */
1264 section
= find_pc_section (pc
);
1268 for (objfile
*objfile
: current_program_space
->objfiles ())
1270 int printed_objfile_header
= 0;
1271 int print_for_objfile
= 1;
1274 if (objfile_arg
!= NULL
)
1276 = compare_filenames_for_search (objfile_name (objfile
),
1278 if (!print_for_objfile
)
1281 for (const auto &iter
: objfile
->qf
)
1283 psymbol_functions
*psf
1284 = dynamic_cast<psymbol_functions
*> (iter
.get ());
1288 if (address_arg
!= NULL
)
1290 struct bound_minimal_symbol msymbol
;
1292 /* We don't assume each pc has a unique objfile (this is for
1294 struct partial_symtab
*ps
1295 = psf
->find_pc_sect_psymtab (objfile
, pc
, section
, msymbol
);
1298 if (!printed_objfile_header
)
1300 outfile
->printf ("\nPartial symtabs for objfile %s\n",
1301 objfile_name (objfile
));
1302 printed_objfile_header
= 1;
1304 dump_psymtab (objfile
, ps
, outfile
);
1310 for (partial_symtab
*ps
: psf
->partial_symbols (objfile
))
1312 int print_for_source
= 0;
1315 if (source_arg
!= NULL
)
1318 = compare_filenames_for_search (ps
->filename
, source_arg
);
1321 if (source_arg
== NULL
1322 || print_for_source
)
1324 if (!printed_objfile_header
)
1326 outfile
->printf ("\nPartial symtabs for objfile %s\n",
1327 objfile_name (objfile
));
1328 printed_objfile_header
= 1;
1330 dump_psymtab (objfile
, ps
, outfile
);
1339 if (address_arg
!= NULL
)
1340 error (_("No partial symtab for address: %s"), address_arg
);
1341 if (source_arg
!= NULL
)
1342 error (_("No partial symtab for source file: %s"), source_arg
);
1346 /* List all the partial symbol tables whose names match REGEXP (optional). */
1349 maintenance_info_psymtabs (const char *regexp
, int from_tty
)
1354 for (struct program_space
*pspace
: program_spaces
)
1355 for (objfile
*objfile
: pspace
->objfiles ())
1357 struct gdbarch
*gdbarch
= objfile
->arch ();
1359 /* We don't want to print anything for this objfile until we
1360 actually find a symtab whose name matches. */
1361 int printed_objfile_start
= 0;
1363 for (const auto &iter
: objfile
->qf
)
1365 psymbol_functions
*psf
1366 = dynamic_cast<psymbol_functions
*> (iter
.get ());
1369 for (partial_symtab
*psymtab
: psf
->partial_symbols (objfile
))
1374 || re_exec (psymtab
->filename
))
1376 if (! printed_objfile_start
)
1378 gdb_printf ("{ objfile %s ", objfile_name (objfile
));
1379 gdb_stdout
->wrap_here (2);
1380 gdb_printf ("((struct objfile *) %s)\n",
1381 host_address_to_string (objfile
));
1382 printed_objfile_start
= 1;
1385 gdb_printf (" { psymtab %s ", psymtab
->filename
);
1386 gdb_stdout
->wrap_here (4);
1387 gdb_printf ("((struct partial_symtab *) %s)\n",
1388 host_address_to_string (psymtab
));
1390 gdb_printf (" readin %s\n",
1391 psymtab
->readin_p (objfile
) ? "yes" : "no");
1392 gdb_printf (" fullname %s\n",
1394 ? psymtab
->fullname
: "(null)");
1395 gdb_printf (" text addresses ");
1396 gdb_puts (paddress (gdbarch
,
1397 psymtab
->text_low (objfile
)));
1398 gdb_printf (" -- ");
1399 gdb_puts (paddress (gdbarch
,
1400 psymtab
->text_high (objfile
)));
1402 gdb_printf (" globals ");
1403 if (!psymtab
->global_psymbols
.empty ())
1405 ("(* (struct partial_symbol **) %s @ %d)\n",
1406 host_address_to_string (psymtab
->global_psymbols
.data ()),
1407 (int) psymtab
->global_psymbols
.size ());
1409 gdb_printf ("(none)\n");
1410 gdb_printf (" statics ");
1411 if (!psymtab
->static_psymbols
.empty ())
1413 ("(* (struct partial_symbol **) %s @ %d)\n",
1414 host_address_to_string (psymtab
->static_psymbols
.data ()),
1415 (int) psymtab
->static_psymbols
.size ());
1417 gdb_printf ("(none)\n");
1419 gdb_printf (" user %s "
1420 "((struct partial_symtab *) %s)\n",
1421 psymtab
->user
->filename
,
1422 host_address_to_string (psymtab
->user
));
1423 gdb_printf (" dependencies ");
1424 if (psymtab
->number_of_dependencies
)
1429 for (i
= 0; i
< psymtab
->number_of_dependencies
; i
++)
1431 struct partial_symtab
*dep
= psymtab
->dependencies
[i
];
1433 /* Note the string concatenation there --- no
1435 gdb_printf (" psymtab %s "
1436 "((struct partial_symtab *) %s)\n",
1438 host_address_to_string (dep
));
1440 gdb_printf (" }\n");
1443 gdb_printf ("(none)\n");
1444 gdb_printf (" }\n");
1449 if (printed_objfile_start
)
1454 /* Check consistency of currently expanded psymtabs vs symtabs. */
1457 maintenance_check_psymtabs (const char *ignore
, int from_tty
)
1460 struct compunit_symtab
*cust
= NULL
;
1461 const struct blockvector
*bv
;
1462 const struct block
*b
;
1464 for (objfile
*objfile
: current_program_space
->objfiles ())
1466 for (const auto &iter
: objfile
->qf
)
1468 psymbol_functions
*psf
1469 = dynamic_cast<psymbol_functions
*> (iter
.get ());
1473 for (partial_symtab
*ps
: psf
->partial_symbols (objfile
))
1475 struct gdbarch
*gdbarch
= objfile
->arch ();
1477 /* We don't call psymtab_to_symtab here because that may cause symtab
1478 expansion. When debugging a problem it helps if checkers leave
1479 things unchanged. */
1480 cust
= ps
->get_compunit_symtab (objfile
);
1482 /* First do some checks that don't require the associated symtab. */
1483 if (ps
->text_high (objfile
) < ps
->text_low (objfile
))
1485 gdb_printf ("Psymtab ");
1486 gdb_puts (ps
->filename
);
1487 gdb_printf (" covers bad range ");
1488 gdb_puts (paddress (gdbarch
, ps
->text_low (objfile
)));
1490 gdb_puts (paddress (gdbarch
, ps
->text_high (objfile
)));
1495 /* Now do checks requiring the associated symtab. */
1498 bv
= cust
->blockvector ();
1499 b
= bv
->static_block ();
1500 for (partial_symbol
*psym
: ps
->static_psymbols
)
1502 /* Skip symbols for inlined functions without address. These may
1503 or may not have a match in the full symtab. */
1504 if (psym
->aclass
== LOC_BLOCK
1505 && psym
->ginfo
.value_address () == 0)
1508 sym
= block_lookup_symbol (b
, psym
->ginfo
.search_name (),
1509 symbol_name_match_type::SEARCH_NAME
,
1510 to_search_flags (psym
->domain
));
1513 gdb_printf ("Static symbol `");
1514 gdb_puts (psym
->ginfo
.linkage_name ());
1515 gdb_printf ("' only found in ");
1516 gdb_puts (ps
->filename
);
1517 gdb_printf (" psymtab\n");
1520 b
= bv
->global_block ();
1521 for (partial_symbol
*psym
: ps
->global_psymbols
)
1523 sym
= block_lookup_symbol (b
, psym
->ginfo
.search_name (),
1524 symbol_name_match_type::SEARCH_NAME
,
1525 to_search_flags (psym
->domain
));
1528 gdb_printf ("Global symbol `");
1529 gdb_puts (psym
->ginfo
.linkage_name ());
1530 gdb_printf ("' only found in ");
1531 gdb_puts (ps
->filename
);
1532 gdb_printf (" psymtab\n");
1535 if (ps
->unrelocated_text_high () != unrelocated_addr (0)
1536 && (ps
->text_low (objfile
) < b
->start ()
1537 || ps
->text_high (objfile
) > b
->end ()))
1539 gdb_printf ("Psymtab ");
1540 gdb_puts (ps
->filename
);
1541 gdb_printf (" covers ");
1542 gdb_puts (paddress (gdbarch
, ps
->text_low (objfile
)));
1544 gdb_puts (paddress (gdbarch
, ps
->text_high (objfile
)));
1545 gdb_printf (" but symtab covers only ");
1546 gdb_puts (paddress (gdbarch
, b
->start ()));
1548 gdb_puts (paddress (gdbarch
, b
->end ()));
1556 void _initialize_psymtab ();
1558 _initialize_psymtab ()
1560 add_cmd ("psymbols", class_maintenance
, maintenance_print_psymbols
, _("\
1561 Print dump of current partial symbol definitions.\n\
1562 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
1563 mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
1564 Entries in the partial symbol table are dumped to file OUTFILE,\n\
1565 or the terminal if OUTFILE is unspecified.\n\
1566 If ADDRESS is provided, dump only the symbols for the file with code at that address.\n\
1567 If SOURCE is provided, dump only that file's symbols.\n\
1568 If OBJFILE is provided, dump only that object file's symbols."),
1569 &maintenanceprintlist
);
1571 add_cmd ("psymtabs", class_maintenance
, maintenance_info_psymtabs
, _("\
1572 List the partial symbol tables for all object files.\n\
1573 This does not include information about individual partial symbols,\n\
1574 just the symbol table structures themselves."),
1575 &maintenanceinfolist
);
1577 add_cmd ("check-psymtabs", class_maintenance
, maintenance_check_psymtabs
,
1579 Check consistency of currently expanded psymtabs versus symtabs."),