Automatic date update in version.in
[binutils-gdb.git] / gdb / psymtab.c
blob7d6648c4666c4bfcf3c76baf3df988a965608b7c
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"
21 #include "symtab.h"
22 #include "objfiles.h"
23 #include "psymtab.h"
24 #include "block.h"
25 #include "filenames.h"
26 #include "source.h"
27 #include "gdbtypes.h"
28 #include "ui-out.h"
29 #include "command.h"
30 #include "readline/tilde.h"
31 #include "gdbsupport/gdb_regex.h"
32 #include "dictionary.h"
33 #include "language.h"
34 #include "cp-support.h"
35 #include "cli/cli-cmds.h"
36 #include <algorithm>
37 #include <set>
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;
59 delete iter;
60 iter = next;
64 /* See psymtab.h. */
66 void
67 psymtab_storage::install_psymtab (partial_symtab *pst)
69 pst->next = psymtabs;
70 psymtabs = pst;
75 /* See psymtab.h. */
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 struct 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)
101 return pst;
103 if (msymbol.minsym == NULL)
104 return pst;
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;
118 CORE_ADDR this_addr;
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);
125 if (p != NULL
126 && (p->address (objfile) == msymbol.value_address ()))
127 return tpst;
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). */
133 if (p != NULL)
134 this_addr = p->address (objfile);
135 else
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
147 testcase.) */
148 if (this_addr > best_addr)
150 best_addr = this_addr;
151 best_pst = tpst;
155 return best_pst;
158 /* See psymtab.h. */
160 struct partial_symtab *
161 psymbol_functions::find_pc_sect_psymtab (struct objfile *objfile,
162 CORE_ADDR pc,
163 struct obj_section *section,
164 struct 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,
172 msymbol);
173 if (best_pst != NULL)
174 return best_pst;
177 return 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
185 (struct objfile *objfile,
186 struct bound_minimal_symbol msymbol,
187 CORE_ADDR pc,
188 struct obj_section *section,
189 int warn_if_readin)
191 struct partial_symtab *ps = find_pc_sect_psymtab (objfile,
192 pc, section,
193 msymbol);
194 if (ps != NULL)
196 if (warn_if_readin && ps->readin_p (objfile))
197 /* Might want to error() here (in case symtab is corrupt and
198 will cause a core dump), but maybe we can successfully
199 continue, so let's not. */
200 warning (_("\
201 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
202 paddress (objfile->arch (), pc));
203 psymtab_to_symtab (objfile, ps);
204 return ps->get_compunit_symtab (objfile);
206 return NULL;
209 /* Find which partial symbol within a psymtab matches PC and SECTION.
210 Return NULL if none. */
212 static const struct partial_symbol *
213 find_pc_sect_psymbol (struct objfile *objfile,
214 struct partial_symtab *psymtab, CORE_ADDR pc,
215 struct obj_section *section)
217 const struct partial_symbol *best = NULL;
218 CORE_ADDR best_pc;
219 const CORE_ADDR textlow = psymtab->text_low (objfile);
221 gdb_assert (psymtab != NULL);
223 /* Cope with programs that start at address 0. */
224 best_pc = (textlow != 0) ? textlow - 1 : 0;
226 /* Search the global symbols as well as the static symbols, so that
227 find_pc_partial_function doesn't use a minimal symbol and thus
228 cache a bad endaddr. */
229 for (const partial_symbol *p : psymtab->global_psymbols)
231 if (p->domain == VAR_DOMAIN
232 && p->aclass == LOC_BLOCK
233 && pc >= p->address (objfile)
234 && (p->address (objfile) > best_pc
235 || (psymtab->text_low (objfile) == 0
236 && best_pc == 0 && p->address (objfile) == 0)))
238 if (section != NULL) /* Match on a specific section. */
240 if (!matching_obj_sections (p->obj_section (objfile),
241 section))
242 continue;
244 best_pc = p->address (objfile);
245 best = p;
249 for (const partial_symbol *p : psymtab->static_psymbols)
251 if (p->domain == VAR_DOMAIN
252 && p->aclass == LOC_BLOCK
253 && pc >= p->address (objfile)
254 && (p->address (objfile) > best_pc
255 || (psymtab->text_low (objfile) == 0
256 && best_pc == 0 && p->address (objfile) == 0)))
258 if (section != NULL) /* Match on a specific section. */
260 if (!matching_obj_sections (p->obj_section (objfile),
261 section))
262 continue;
264 best_pc = p->address (objfile);
265 best = p;
269 return best;
272 /* Psymtab version of lookup_global_symbol_language. See its definition in
273 the definition of quick_symbol_functions in symfile.h. */
275 enum language
276 psymbol_functions::lookup_global_symbol_language (struct objfile *objfile,
277 const char *name,
278 domain_search_flags domain,
279 bool *symbol_found_p)
281 *symbol_found_p = false;
282 if (objfile->sf == NULL)
283 return language_unknown;
285 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
287 for (partial_symtab *ps : partial_symbols (objfile))
289 const struct partial_symbol *psym;
290 if (ps->readin_p (objfile))
291 continue;
293 psym = lookup_partial_symbol (objfile, ps, lookup_name, 1, domain);
294 if (psym)
296 *symbol_found_p = true;
297 return psym->ginfo.language ();
301 return language_unknown;
304 /* Returns true if PSYM matches LOOKUP_NAME. */
306 static bool
307 psymbol_name_matches (const partial_symbol *psym,
308 const lookup_name_info &lookup_name)
310 const language_defn *lang = language_def (psym->ginfo.language ());
311 symbol_name_matcher_ftype *name_match
312 = lang->get_symbol_name_matcher (lookup_name);
313 return name_match (psym->ginfo.search_name (), lookup_name, NULL);
316 /* Look, in partial_symtab PST, for symbol whose natural name is
317 LOOKUP_NAME. Check the global symbols if GLOBAL, the static
318 symbols if not. */
320 static const struct partial_symbol *
321 lookup_partial_symbol (struct objfile *objfile,
322 struct partial_symtab *pst,
323 const lookup_name_info &lookup_name,
324 int global, domain_search_flags domain)
326 const struct partial_symbol **start, **psym;
327 const struct partial_symbol **top, **real_top, **bottom, **center;
328 int length = (global
329 ? pst->global_psymbols.size ()
330 : pst->static_psymbols.size ());
331 int do_linear_search = 1;
333 if (length == 0)
334 return NULL;
336 start = (global ?
337 &pst->global_psymbols[0] :
338 &pst->static_psymbols[0]);
340 if (global) /* This means we can use a binary search. */
342 do_linear_search = 0;
344 /* Binary search. This search is guaranteed to end with center
345 pointing at the earliest partial symbol whose name might be
346 correct. At that point *all* partial symbols with an
347 appropriate name will be checked against the correct
348 domain. */
350 bottom = start;
351 top = start + length - 1;
352 real_top = top;
353 while (top > bottom)
355 center = bottom + (top - bottom) / 2;
357 gdb_assert (center < top);
359 if (strcmp_iw_ordered ((*center)->ginfo.search_name (),
360 lookup_name.c_str ()) >= 0)
362 top = center;
364 else
366 bottom = center + 1;
370 gdb_assert (top == bottom);
372 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
373 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
374 while (top >= start && symbol_matches_search_name (&(*top)->ginfo,
375 lookup_name))
376 top--;
378 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
379 top++;
381 while (top <= real_top && symbol_matches_search_name (&(*top)->ginfo,
382 lookup_name))
384 if (search_flags_matches (domain, (*top)->domain))
385 return *top;
386 top++;
390 /* Can't use a binary search or else we found during the binary search that
391 we should also do a linear search. */
393 if (do_linear_search)
395 for (psym = start; psym < start + length; psym++)
397 if (search_flags_matches (domain, (*psym)->domain)
398 && symbol_matches_search_name (&(*psym)->ginfo, lookup_name))
399 return *psym;
403 return NULL;
406 /* Get the symbol table that corresponds to a partial_symtab.
407 This is fast after the first time you do it.
408 The result will be NULL if the primary symtab has no symbols,
409 which can happen. Otherwise the result is the primary symtab
410 that contains PST. */
412 static struct compunit_symtab *
413 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
415 /* If it is a shared psymtab, find an unshared psymtab that includes
416 it. Any such psymtab will do. */
417 while (pst->user != NULL)
418 pst = pst->user;
420 /* If it's been looked up before, return it. */
421 if (pst->get_compunit_symtab (objfile))
422 return pst->get_compunit_symtab (objfile);
424 /* If it has not yet been read in, read it. */
425 if (!pst->readin_p (objfile))
427 scoped_restore decrementer = increment_reading_symtab ();
429 if (info_verbose)
431 gdb_printf (_("Reading in symbols for %s...\n"),
432 pst->filename);
433 gdb_flush (gdb_stdout);
436 pst->read_symtab (objfile);
439 return pst->get_compunit_symtab (objfile);
442 /* Psymtab version of find_last_source_symtab. See its definition in
443 the definition of quick_symbol_functions in symfile.h. */
445 struct symtab *
446 psymbol_functions::find_last_source_symtab (struct objfile *ofp)
448 struct partial_symtab *cs_pst = NULL;
450 for (partial_symtab *ps : partial_symbols (ofp))
452 const char *name = ps->filename;
453 int len = strlen (name);
455 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
456 || strcmp (name, "<<C++-namespaces>>") == 0)))
457 cs_pst = ps;
460 if (cs_pst)
462 if (cs_pst->readin_p (ofp))
464 internal_error (_("select_source_symtab: "
465 "readin pst found and no symtabs."));
467 else
469 struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
471 if (cust == NULL)
472 return NULL;
473 return cust->primary_filetab ();
476 return NULL;
479 /* Psymtab version of forget_cached_source_info. See its definition in
480 the definition of quick_symbol_functions in symfile.h. */
482 void
483 psymbol_functions::forget_cached_source_info (struct objfile *objfile)
485 for (partial_symtab *pst : partial_symbols (objfile))
487 if (pst->fullname != NULL)
489 xfree (pst->fullname);
490 pst->fullname = NULL;
495 static void
496 print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
497 const std::vector<const partial_symbol *> &symbols,
498 const char *what, struct ui_file *outfile)
500 gdb_printf (outfile, " %s partial symbols:\n", what);
501 for (const partial_symbol *p : symbols)
503 QUIT;
504 gdb_printf (outfile, " `%s'", p->ginfo.linkage_name ());
505 if (p->ginfo.demangled_name () != NULL)
507 gdb_printf (outfile, " `%s'",
508 p->ginfo.demangled_name ());
510 gdb_puts (", ", outfile);
511 switch (p->domain)
513 case UNDEF_DOMAIN:
514 gdb_puts ("undefined domain, ", outfile);
515 break;
516 case VAR_DOMAIN:
517 /* This is the usual thing -- don't print it. */
518 break;
519 case STRUCT_DOMAIN:
520 gdb_puts ("struct domain, ", outfile);
521 break;
522 case MODULE_DOMAIN:
523 gdb_puts ("module domain, ", outfile);
524 break;
525 case LABEL_DOMAIN:
526 gdb_puts ("label domain, ", outfile);
527 break;
528 case COMMON_BLOCK_DOMAIN:
529 gdb_puts ("common block domain, ", outfile);
530 break;
531 default:
532 gdb_puts ("<invalid domain>, ", outfile);
533 break;
535 switch (p->aclass)
537 case LOC_UNDEF:
538 gdb_puts ("undefined", outfile);
539 break;
540 case LOC_CONST:
541 gdb_puts ("constant int", outfile);
542 break;
543 case LOC_STATIC:
544 gdb_puts ("static", outfile);
545 break;
546 case LOC_REGISTER:
547 gdb_puts ("register", outfile);
548 break;
549 case LOC_ARG:
550 gdb_puts ("pass by value", outfile);
551 break;
552 case LOC_REF_ARG:
553 gdb_puts ("pass by reference", outfile);
554 break;
555 case LOC_REGPARM_ADDR:
556 gdb_puts ("register address parameter", outfile);
557 break;
558 case LOC_LOCAL:
559 gdb_puts ("stack parameter", outfile);
560 break;
561 case LOC_TYPEDEF:
562 gdb_puts ("type", outfile);
563 break;
564 case LOC_LABEL:
565 gdb_puts ("label", outfile);
566 break;
567 case LOC_BLOCK:
568 gdb_puts ("function", outfile);
569 break;
570 case LOC_CONST_BYTES:
571 gdb_puts ("constant bytes", outfile);
572 break;
573 case LOC_UNRESOLVED:
574 gdb_puts ("unresolved", outfile);
575 break;
576 case LOC_OPTIMIZED_OUT:
577 gdb_puts ("optimized out", outfile);
578 break;
579 case LOC_COMPUTED:
580 gdb_puts ("computed at runtime", outfile);
581 break;
582 default:
583 gdb_puts ("<invalid location>", outfile);
584 break;
586 gdb_puts (", ", outfile);
587 gdb_puts (paddress (gdbarch, CORE_ADDR (p->unrelocated_address ())),
588 outfile);
589 gdb_printf (outfile, "\n");
593 static void
594 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
595 struct ui_file *outfile)
597 struct gdbarch *gdbarch = objfile->arch ();
598 int i;
600 if (psymtab->anonymous)
602 gdb_printf (outfile, "\nAnonymous partial symtab (%s) ",
603 psymtab->filename);
605 else
607 gdb_printf (outfile, "\nPartial symtab for source file %s ",
608 psymtab->filename);
610 gdb_printf (outfile, "(object %s)\n\n",
611 host_address_to_string (psymtab));
612 gdb_printf (outfile, " Read from object file %s (%s)\n",
613 objfile_name (objfile),
614 host_address_to_string (objfile));
616 if (psymtab->readin_p (objfile))
617 gdb_printf
618 (outfile,
619 " Full symtab was read (at %s)\n",
620 host_address_to_string (psymtab->get_compunit_symtab (objfile)));
622 gdb_printf (outfile, " Symbols cover text addresses ");
623 gdb_puts (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
624 gdb_printf (outfile, "-");
625 gdb_puts (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
626 gdb_printf (outfile, "\n");
627 gdb_printf (outfile, " Depends on %d other partial symtabs.\n",
628 psymtab->number_of_dependencies);
629 for (i = 0; i < psymtab->number_of_dependencies; i++)
630 gdb_printf (outfile, " %d %s\n", i,
631 host_address_to_string (psymtab->dependencies[i]));
632 if (psymtab->user != NULL)
633 gdb_printf (outfile, " Shared partial symtab with user %s\n",
634 host_address_to_string (psymtab->user));
635 if (!psymtab->global_psymbols.empty ())
637 print_partial_symbols
638 (gdbarch, objfile, psymtab->global_psymbols,
639 "Global", outfile);
641 if (!psymtab->static_psymbols.empty ())
643 print_partial_symbols
644 (gdbarch, objfile, psymtab->static_psymbols,
645 "Static", outfile);
647 gdb_printf (outfile, "\n");
650 /* Count the number of partial symbols in OBJFILE. */
653 psymbol_functions::count_psyms ()
655 int count = 0;
656 for (partial_symtab *pst : m_partial_symtabs->range ())
658 count += pst->global_psymbols.size ();
659 count += pst->static_psymbols.size ();
661 return count;
664 /* Psymtab version of print_stats. See its definition in
665 the definition of quick_symbol_functions in symfile.h. */
667 void
668 psymbol_functions::print_stats (struct objfile *objfile, bool print_bcache)
670 int i;
672 if (!print_bcache)
674 int n_psyms = count_psyms ();
675 if (n_psyms > 0)
676 gdb_printf (_(" Number of \"partial\" symbols read: %d\n"),
677 n_psyms);
679 i = 0;
680 for (partial_symtab *ps : partial_symbols (objfile))
682 if (!ps->readin_p (objfile))
683 i++;
685 gdb_printf (_(" Number of psym tables (not yet expanded): %d\n"),
687 gdb_printf (_(" Total memory used for psymbol cache: %d\n"),
688 m_partial_symtabs->psymbol_cache.memory_used ());
690 else
692 gdb_printf (_("Psymbol byte cache statistics:\n"));
693 m_partial_symtabs->psymbol_cache.print_statistics
694 ("partial symbol cache");
698 /* Psymtab version of dump. See its definition in
699 the definition of quick_symbol_functions in symfile.h. */
701 void
702 psymbol_functions::dump (struct objfile *objfile)
704 struct partial_symtab *psymtab;
706 if (m_partial_symtabs->psymtabs)
708 gdb_printf ("Psymtabs:\n");
709 for (psymtab = m_partial_symtabs->psymtabs;
710 psymtab != NULL;
711 psymtab = psymtab->next)
712 gdb_printf ("%s at %s\n",
713 psymtab->filename,
714 host_address_to_string (psymtab));
715 gdb_printf ("\n\n");
719 /* Psymtab version of expand_all_symtabs. See its definition in
720 the definition of quick_symbol_functions in symfile.h. */
722 void
723 psymbol_functions::expand_all_symtabs (struct objfile *objfile)
725 for (partial_symtab *psymtab : partial_symbols (objfile))
726 psymtab_to_symtab (objfile, psymtab);
729 /* Psymtab version of map_symbol_filenames. See its definition in
730 the definition of quick_symbol_functions in symfile.h. */
732 void
733 psymbol_functions::map_symbol_filenames
734 (struct objfile *objfile,
735 gdb::function_view<symbol_filename_ftype> fun,
736 bool need_fullname)
738 for (partial_symtab *ps : partial_symbols (objfile))
740 const char *fullname;
742 if (ps->readin_p (objfile))
743 continue;
745 /* We can skip shared psymtabs here, because any file name will be
746 attached to the unshared psymtab. */
747 if (ps->user != NULL)
748 continue;
750 /* Anonymous psymtabs don't have a file name. */
751 if (ps->anonymous)
752 continue;
754 QUIT;
755 if (need_fullname)
756 fullname = psymtab_to_fullname (ps);
757 else
758 fullname = NULL;
759 fun (ps->filename, fullname);
763 /* Finds the fullname that a partial_symtab represents.
765 If this functions finds the fullname, it will save it in ps->fullname
766 and it will also return the value.
768 If this function fails to find the file that this partial_symtab represents,
769 NULL will be returned and ps->fullname will be set to NULL. */
771 static const char *
772 psymtab_to_fullname (struct partial_symtab *ps)
774 gdb_assert (!ps->anonymous);
776 /* Use cached copy if we have it.
777 We rely on forget_cached_source_info being called appropriately
778 to handle cases like the file being moved. */
779 if (ps->fullname == NULL)
781 gdb::unique_xmalloc_ptr<char> fullname
782 = find_source_or_rewrite (ps->filename, ps->dirname);
783 ps->fullname = fullname.release ();
786 return ps->fullname;
789 /* A helper for psym_expand_symtabs_matching that handles searching
790 included psymtabs. This returns true if a symbol is found, and
791 false otherwise. It also updates the 'searched_flag' on the
792 various psymtabs that it searches. */
794 static bool
795 recursively_search_psymtabs
796 (struct partial_symtab *ps,
797 struct objfile *objfile,
798 block_search_flags search_flags,
799 domain_search_flags domain,
800 const lookup_name_info &lookup_name,
801 gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
803 int keep_going = 1;
804 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
805 int i;
807 if (ps->searched_flag != PST_NOT_SEARCHED)
808 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
810 /* Recurse into shared psymtabs first, because they may have already
811 been searched, and this could save some time. */
812 for (i = 0; i < ps->number_of_dependencies; ++i)
814 int r;
816 /* Skip non-shared dependencies, these are handled elsewhere. */
817 if (ps->dependencies[i]->user == NULL)
818 continue;
820 r = recursively_search_psymtabs (ps->dependencies[i],
821 objfile, search_flags, domain,
822 lookup_name, sym_matcher);
823 if (r != 0)
825 ps->searched_flag = PST_SEARCHED_AND_FOUND;
826 return true;
830 const partial_symbol **gbound = (ps->global_psymbols.data ()
831 + ps->global_psymbols.size ());
832 const partial_symbol **sbound = (ps->static_psymbols.data ()
833 + ps->static_psymbols.size ());
834 const partial_symbol **bound = gbound;
836 /* Go through all of the symbols stored in a partial
837 symtab in one loop. */
838 const partial_symbol **psym = ps->global_psymbols.data ();
840 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
842 if (ps->static_psymbols.empty ())
843 keep_going = 0;
844 else
846 psym = ps->static_psymbols.data ();
847 bound = sbound;
851 while (keep_going)
853 if (psym >= bound)
855 if (bound == gbound && !ps->static_psymbols.empty ()
856 && (search_flags & SEARCH_STATIC_BLOCK) != 0)
858 psym = ps->static_psymbols.data ();
859 bound = sbound;
861 else
862 keep_going = 0;
863 continue;
865 else
867 QUIT;
869 if (search_flags_matches (domain, (*psym)->domain)
870 && psymbol_name_matches (*psym, lookup_name)
871 && (sym_matcher == NULL
872 || sym_matcher ((*psym)->ginfo.search_name ())))
874 /* Found a match, so notify our caller. */
875 result = PST_SEARCHED_AND_FOUND;
876 keep_going = 0;
879 psym++;
882 ps->searched_flag = result;
883 return result == PST_SEARCHED_AND_FOUND;
886 /* Psymtab version of expand_symtabs_matching. See its definition in
887 the definition of quick_symbol_functions in symfile.h. */
889 bool
890 psymbol_functions::expand_symtabs_matching
891 (struct objfile *objfile,
892 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
893 const lookup_name_info *lookup_name,
894 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
895 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
896 block_search_flags search_flags,
897 domain_search_flags domain)
899 /* Clear the search flags. */
900 for (partial_symtab *ps : partial_symbols (objfile))
901 ps->searched_flag = PST_NOT_SEARCHED;
903 std::optional<lookup_name_info> psym_lookup_name;
904 if (lookup_name != nullptr)
905 psym_lookup_name = lookup_name->make_ignore_params ();
907 /* This invariant is documented in quick-functions.h. */
908 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
910 for (partial_symtab *ps : m_partial_symtabs->range ())
912 QUIT;
914 if (ps->readin_p (objfile))
915 continue;
917 if (file_matcher)
919 bool match;
921 if (ps->anonymous)
922 continue;
924 match = file_matcher (ps->filename, false);
925 if (!match)
927 /* Before we invoke realpath, which can get expensive when many
928 files are involved, do a quick comparison of the basenames. */
929 if (basenames_may_differ
930 || file_matcher (lbasename (ps->filename), true))
931 match = file_matcher (psymtab_to_fullname (ps), false);
933 if (!match)
934 continue;
937 if (lookup_name == nullptr
938 || recursively_search_psymtabs (ps, objfile, search_flags,
939 domain, *psym_lookup_name,
940 symbol_matcher))
942 compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
944 if (cust != nullptr && expansion_notify != nullptr)
945 if (!expansion_notify (cust))
946 return false;
950 return true;
953 /* Psymtab version of has_symbols. See its definition in
954 the definition of quick_symbol_functions in symfile.h. */
956 bool
957 psymbol_functions::has_symbols (struct objfile *objfile)
959 return m_partial_symtabs->psymtabs != NULL;
962 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
964 bool
965 psymbol_functions::has_unexpanded_symtabs (struct objfile *objfile)
967 for (partial_symtab *psymtab : partial_symbols (objfile))
969 /* Is this already expanded? */
970 if (psymtab->readin_p (objfile))
971 continue;
973 /* It has not yet been expanded. */
974 return true;
977 return false;
982 /* Partially fill a partial symtab. It will be completely filled at
983 the end of the symbol list. */
985 partial_symtab::partial_symtab (const char *filename,
986 psymtab_storage *partial_symtabs,
987 objfile_per_bfd_storage *objfile_per_bfd,
988 unrelocated_addr textlow)
989 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
991 set_text_low (textlow);
992 set_text_high (unrelocated_text_low ()); /* default */
995 /* Perform "finishing up" operations of a partial symtab. */
997 void
998 partial_symtab::end ()
1000 global_psymbols.shrink_to_fit ();
1001 static_psymbols.shrink_to_fit ();
1003 /* Sort the global list; don't sort the static list. */
1004 std::sort (global_psymbols.begin (),
1005 global_psymbols.end (),
1006 [] (const partial_symbol *s1, const partial_symbol *s2)
1008 return strcmp_iw_ordered (s1->ginfo.search_name (),
1009 s2->ginfo.search_name ()) < 0;
1013 /* See psymtab.h. */
1015 unsigned long
1016 psymbol_bcache::hash (const void *addr, int length)
1018 unsigned long h = 0;
1019 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1020 unsigned int lang = psymbol->ginfo.language ();
1021 unsigned int domain = psymbol->domain;
1022 unsigned int theclass = psymbol->aclass;
1024 h = fast_hash (&psymbol->ginfo.m_value, sizeof (psymbol->ginfo.m_value), h);
1025 h = fast_hash (&lang, sizeof (unsigned int), h);
1026 h = fast_hash (&domain, sizeof (unsigned int), h);
1027 h = fast_hash (&theclass, sizeof (unsigned int), h);
1028 /* Note that psymbol names are interned via compute_and_set_names, so
1029 there's no need to hash the contents of the name here. */
1030 h = fast_hash (&psymbol->ginfo.m_name, sizeof (psymbol->ginfo.m_name), h);
1032 return h;
1035 /* See psymtab.h. */
1038 psymbol_bcache::compare (const void *addr1, const void *addr2, int length)
1040 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1041 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1043 return (memcmp (&sym1->ginfo.m_value, &sym2->ginfo.m_value,
1044 sizeof (sym1->ginfo.m_value)) == 0
1045 && sym1->ginfo.language () == sym2->ginfo.language ()
1046 && sym1->domain == sym2->domain
1047 && sym1->aclass == sym2->aclass
1048 /* Note that psymbol names are interned via
1049 compute_and_set_names, so there's no need to compare the
1050 contents of the name here. */
1051 && sym1->ginfo.linkage_name () == sym2->ginfo.linkage_name ());
1054 /* See psymtab.h. */
1056 void
1057 partial_symtab::add_psymbol (const partial_symbol &psymbol,
1058 psymbol_placement where,
1059 psymtab_storage *partial_symtabs,
1060 struct objfile *objfile)
1062 bool added;
1064 /* Stash the partial symbol away in the cache. */
1065 const partial_symbol *psym = partial_symtabs->psymbol_cache.insert (psymbol,
1066 &added);
1068 /* Do not duplicate global partial symbols. */
1069 if (where == psymbol_placement::GLOBAL && !added)
1070 return;
1072 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1073 std::vector<const partial_symbol *> &list
1074 = (where == psymbol_placement::STATIC
1075 ? static_psymbols
1076 : global_psymbols);
1077 list.push_back (psym);
1080 /* See psymtab.h. */
1082 void
1083 partial_symtab::add_psymbol (std::string_view name, bool copy_name,
1084 domain_enum domain,
1085 enum address_class theclass,
1086 short section,
1087 psymbol_placement where,
1088 unrelocated_addr coreaddr,
1089 enum language language,
1090 psymtab_storage *partial_symtabs,
1091 struct objfile *objfile)
1093 struct partial_symbol psymbol;
1094 memset (&psymbol, 0, sizeof (psymbol));
1096 psymbol.set_unrelocated_address (coreaddr);
1097 psymbol.ginfo.set_section_index (section);
1098 psymbol.domain = domain;
1099 psymbol.aclass = theclass;
1100 psymbol.ginfo.set_language (language, partial_symtabs->obstack ());
1101 psymbol.ginfo.compute_and_set_names (name, copy_name, objfile->per_bfd);
1103 add_psymbol (psymbol, where, partial_symtabs, objfile);
1106 /* See psymtab.h. */
1108 partial_symtab::partial_symtab (const char *filename_,
1109 psymtab_storage *partial_symtabs,
1110 objfile_per_bfd_storage *objfile_per_bfd)
1111 : searched_flag (PST_NOT_SEARCHED),
1112 text_low_valid (0),
1113 text_high_valid (0)
1115 partial_symtabs->install_psymtab (this);
1117 filename = objfile_per_bfd->intern (filename_);
1119 if (symtab_create_debug >= 1)
1121 /* Be a bit clever with debugging messages, and don't print objfile
1122 every time, only when it changes. */
1123 static std::string last_bfd_name;
1124 const char *this_bfd_name
1125 = bfd_get_filename (objfile_per_bfd->get_bfd ());
1127 if (last_bfd_name.empty () || last_bfd_name != this_bfd_name)
1129 last_bfd_name = this_bfd_name;
1131 symtab_create_debug_printf ("creating one or more psymtabs for %s",
1132 this_bfd_name);
1135 symtab_create_debug_printf ("created psymtab %s for module %s",
1136 host_address_to_string (this), filename);
1140 /* See psymtab.h. */
1142 void
1143 partial_symtab::expand_dependencies (struct objfile *objfile)
1145 for (int i = 0; i < number_of_dependencies; ++i)
1147 if (!dependencies[i]->readin_p (objfile)
1148 && dependencies[i]->user == NULL)
1150 /* Inform about additional files to be read in. */
1151 if (info_verbose)
1153 gdb_puts (" ");
1154 gdb_stdout->wrap_here (0);
1155 gdb_puts ("and ");
1156 gdb_stdout->wrap_here (0);
1157 gdb_printf ("%s...", dependencies[i]->filename);
1158 gdb_flush (gdb_stdout);
1160 dependencies[i]->expand_psymtab (objfile);
1166 void
1167 psymtab_storage::discard_psymtab (struct partial_symtab *pst)
1169 struct partial_symtab **prev_pst;
1171 /* From dbxread.c:
1172 Empty psymtabs happen as a result of header files which don't
1173 have any symbols in them. There can be a lot of them. But this
1174 check is wrong, in that a psymtab with N_SLINE entries but
1175 nothing else is not empty, but we don't realize that. Fixing
1176 that without slowing things down might be tricky. */
1178 /* First, snip it out of the psymtab chain. */
1180 prev_pst = &psymtabs;
1181 while ((*prev_pst) != pst)
1182 prev_pst = &((*prev_pst)->next);
1183 (*prev_pst) = pst->next;
1184 delete pst;
1189 static void
1190 maintenance_print_psymbols (const char *args, int from_tty)
1192 struct ui_file *outfile = gdb_stdout;
1193 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1194 int i, outfile_idx, found;
1195 CORE_ADDR pc = 0;
1196 struct obj_section *section = NULL;
1198 dont_repeat ();
1200 gdb_argv argv (args);
1202 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1204 if (strcmp (argv[i], "-pc") == 0)
1206 if (argv[i + 1] == NULL)
1207 error (_("Missing pc value"));
1208 address_arg = argv[++i];
1210 else if (strcmp (argv[i], "-source") == 0)
1212 if (argv[i + 1] == NULL)
1213 error (_("Missing source file"));
1214 source_arg = argv[++i];
1216 else if (strcmp (argv[i], "-objfile") == 0)
1218 if (argv[i + 1] == NULL)
1219 error (_("Missing objfile name"));
1220 objfile_arg = argv[++i];
1222 else if (strcmp (argv[i], "--") == 0)
1224 /* End of options. */
1225 ++i;
1226 break;
1228 else if (argv[i][0] == '-')
1230 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1231 error (_("Unknown option: %s"), argv[i]);
1233 else
1234 break;
1236 outfile_idx = i;
1238 if (address_arg != NULL && source_arg != NULL)
1239 error (_("Must specify at most one of -pc and -source"));
1241 stdio_file arg_outfile;
1243 if (argv != NULL && argv[outfile_idx] != NULL)
1245 if (argv[outfile_idx + 1] != NULL)
1246 error (_("Junk at end of command"));
1247 gdb::unique_xmalloc_ptr<char> outfile_name
1248 (tilde_expand (argv[outfile_idx]));
1249 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1250 perror_with_name (outfile_name.get ());
1251 outfile = &arg_outfile;
1254 if (address_arg != NULL)
1256 pc = parse_and_eval_address (address_arg);
1257 /* If we fail to find a section, that's ok, try the lookup anyway. */
1258 section = find_pc_section (pc);
1261 found = 0;
1262 for (objfile *objfile : current_program_space->objfiles ())
1264 int printed_objfile_header = 0;
1265 int print_for_objfile = 1;
1267 QUIT;
1268 if (objfile_arg != NULL)
1269 print_for_objfile
1270 = compare_filenames_for_search (objfile_name (objfile),
1271 objfile_arg);
1272 if (!print_for_objfile)
1273 continue;
1275 for (const auto &iter : objfile->qf)
1277 psymbol_functions *psf
1278 = dynamic_cast<psymbol_functions *> (iter.get ());
1279 if (psf == nullptr)
1280 continue;
1282 if (address_arg != NULL)
1284 struct bound_minimal_symbol msymbol;
1286 /* We don't assume each pc has a unique objfile (this is for
1287 debugging). */
1288 struct partial_symtab *ps
1289 = psf->find_pc_sect_psymtab (objfile, pc, section, msymbol);
1290 if (ps != NULL)
1292 if (!printed_objfile_header)
1294 outfile->printf ("\nPartial symtabs for objfile %s\n",
1295 objfile_name (objfile));
1296 printed_objfile_header = 1;
1298 dump_psymtab (objfile, ps, outfile);
1299 found = 1;
1302 else
1304 for (partial_symtab *ps : psf->partial_symbols (objfile))
1306 int print_for_source = 0;
1308 QUIT;
1309 if (source_arg != NULL)
1311 print_for_source
1312 = compare_filenames_for_search (ps->filename, source_arg);
1313 found = 1;
1315 if (source_arg == NULL
1316 || print_for_source)
1318 if (!printed_objfile_header)
1320 outfile->printf ("\nPartial symtabs for objfile %s\n",
1321 objfile_name (objfile));
1322 printed_objfile_header = 1;
1324 dump_psymtab (objfile, ps, outfile);
1331 if (!found)
1333 if (address_arg != NULL)
1334 error (_("No partial symtab for address: %s"), address_arg);
1335 if (source_arg != NULL)
1336 error (_("No partial symtab for source file: %s"), source_arg);
1340 /* List all the partial symbol tables whose names match REGEXP (optional). */
1342 static void
1343 maintenance_info_psymtabs (const char *regexp, int from_tty)
1345 if (regexp)
1346 re_comp (regexp);
1348 for (struct program_space *pspace : program_spaces)
1349 for (objfile *objfile : pspace->objfiles ())
1351 struct gdbarch *gdbarch = objfile->arch ();
1353 /* We don't want to print anything for this objfile until we
1354 actually find a symtab whose name matches. */
1355 int printed_objfile_start = 0;
1357 for (const auto &iter : objfile->qf)
1359 psymbol_functions *psf
1360 = dynamic_cast<psymbol_functions *> (iter.get ());
1361 if (psf == nullptr)
1362 continue;
1363 for (partial_symtab *psymtab : psf->partial_symbols (objfile))
1365 QUIT;
1367 if (! regexp
1368 || re_exec (psymtab->filename))
1370 if (! printed_objfile_start)
1372 gdb_printf ("{ objfile %s ", objfile_name (objfile));
1373 gdb_stdout->wrap_here (2);
1374 gdb_printf ("((struct objfile *) %s)\n",
1375 host_address_to_string (objfile));
1376 printed_objfile_start = 1;
1379 gdb_printf (" { psymtab %s ", psymtab->filename);
1380 gdb_stdout->wrap_here (4);
1381 gdb_printf ("((struct partial_symtab *) %s)\n",
1382 host_address_to_string (psymtab));
1384 gdb_printf (" readin %s\n",
1385 psymtab->readin_p (objfile) ? "yes" : "no");
1386 gdb_printf (" fullname %s\n",
1387 psymtab->fullname
1388 ? psymtab->fullname : "(null)");
1389 gdb_printf (" text addresses ");
1390 gdb_puts (paddress (gdbarch,
1391 psymtab->text_low (objfile)));
1392 gdb_printf (" -- ");
1393 gdb_puts (paddress (gdbarch,
1394 psymtab->text_high (objfile)));
1395 gdb_printf ("\n");
1396 gdb_printf (" globals ");
1397 if (!psymtab->global_psymbols.empty ())
1398 gdb_printf
1399 ("(* (struct partial_symbol **) %s @ %d)\n",
1400 host_address_to_string (psymtab->global_psymbols.data ()),
1401 (int) psymtab->global_psymbols.size ());
1402 else
1403 gdb_printf ("(none)\n");
1404 gdb_printf (" statics ");
1405 if (!psymtab->static_psymbols.empty ())
1406 gdb_printf
1407 ("(* (struct partial_symbol **) %s @ %d)\n",
1408 host_address_to_string (psymtab->static_psymbols.data ()),
1409 (int) psymtab->static_psymbols.size ());
1410 else
1411 gdb_printf ("(none)\n");
1412 if (psymtab->user)
1413 gdb_printf (" user %s "
1414 "((struct partial_symtab *) %s)\n",
1415 psymtab->user->filename,
1416 host_address_to_string (psymtab->user));
1417 gdb_printf (" dependencies ");
1418 if (psymtab->number_of_dependencies)
1420 int i;
1422 gdb_printf ("{\n");
1423 for (i = 0; i < psymtab->number_of_dependencies; i++)
1425 struct partial_symtab *dep = psymtab->dependencies[i];
1427 /* Note the string concatenation there --- no
1428 comma. */
1429 gdb_printf (" psymtab %s "
1430 "((struct partial_symtab *) %s)\n",
1431 dep->filename,
1432 host_address_to_string (dep));
1434 gdb_printf (" }\n");
1436 else
1437 gdb_printf ("(none)\n");
1438 gdb_printf (" }\n");
1443 if (printed_objfile_start)
1444 gdb_printf ("}\n");
1448 /* Check consistency of currently expanded psymtabs vs symtabs. */
1450 static void
1451 maintenance_check_psymtabs (const char *ignore, int from_tty)
1453 struct symbol *sym;
1454 struct compunit_symtab *cust = NULL;
1455 const struct blockvector *bv;
1456 const struct block *b;
1458 for (objfile *objfile : current_program_space->objfiles ())
1460 for (const auto &iter : objfile->qf)
1462 psymbol_functions *psf
1463 = dynamic_cast<psymbol_functions *> (iter.get ());
1464 if (psf == nullptr)
1465 continue;
1467 for (partial_symtab *ps : psf->partial_symbols (objfile))
1469 struct gdbarch *gdbarch = objfile->arch ();
1471 /* We don't call psymtab_to_symtab here because that may cause symtab
1472 expansion. When debugging a problem it helps if checkers leave
1473 things unchanged. */
1474 cust = ps->get_compunit_symtab (objfile);
1476 /* First do some checks that don't require the associated symtab. */
1477 if (ps->text_high (objfile) < ps->text_low (objfile))
1479 gdb_printf ("Psymtab ");
1480 gdb_puts (ps->filename);
1481 gdb_printf (" covers bad range ");
1482 gdb_puts (paddress (gdbarch, ps->text_low (objfile)));
1483 gdb_printf (" - ");
1484 gdb_puts (paddress (gdbarch, ps->text_high (objfile)));
1485 gdb_printf ("\n");
1486 continue;
1489 /* Now do checks requiring the associated symtab. */
1490 if (cust == NULL)
1491 continue;
1492 bv = cust->blockvector ();
1493 b = bv->static_block ();
1494 for (const partial_symbol *psym : ps->static_psymbols)
1496 /* Skip symbols for inlined functions without address. These may
1497 or may not have a match in the full symtab. */
1498 if (psym->aclass == LOC_BLOCK
1499 && psym->ginfo.value_address () == 0)
1500 continue;
1502 lookup_name_info lookup_name
1503 (psym->ginfo.search_name (), symbol_name_match_type::SEARCH_NAME);
1504 sym = block_lookup_symbol (b, lookup_name,
1505 to_search_flags (psym->domain));
1506 if (!sym)
1508 gdb_printf ("Static symbol `");
1509 gdb_puts (psym->ginfo.linkage_name ());
1510 gdb_printf ("' only found in ");
1511 gdb_puts (ps->filename);
1512 gdb_printf (" psymtab\n");
1515 b = bv->global_block ();
1516 for (const partial_symbol *psym : ps->global_psymbols)
1518 lookup_name_info lookup_name
1519 (psym->ginfo.search_name (), symbol_name_match_type::SEARCH_NAME);
1520 sym = block_lookup_symbol (b, lookup_name,
1521 to_search_flags (psym->domain));
1522 if (!sym)
1524 gdb_printf ("Global symbol `");
1525 gdb_puts (psym->ginfo.linkage_name ());
1526 gdb_printf ("' only found in ");
1527 gdb_puts (ps->filename);
1528 gdb_printf (" psymtab\n");
1531 if (ps->unrelocated_text_high () != unrelocated_addr (0)
1532 && (ps->text_low (objfile) < b->start ()
1533 || ps->text_high (objfile) > b->end ()))
1535 gdb_printf ("Psymtab ");
1536 gdb_puts (ps->filename);
1537 gdb_printf (" covers ");
1538 gdb_puts (paddress (gdbarch, ps->text_low (objfile)));
1539 gdb_printf (" - ");
1540 gdb_puts (paddress (gdbarch, ps->text_high (objfile)));
1541 gdb_printf (" but symtab covers only ");
1542 gdb_puts (paddress (gdbarch, b->start ()));
1543 gdb_printf (" - ");
1544 gdb_puts (paddress (gdbarch, b->end ()));
1545 gdb_printf ("\n");
1552 void _initialize_psymtab ();
1553 void
1554 _initialize_psymtab ()
1556 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
1557 Print dump of current partial symbol definitions.\n\
1558 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
1559 mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
1560 Entries in the partial symbol table are dumped to file OUTFILE,\n\
1561 or the terminal if OUTFILE is unspecified.\n\
1562 If ADDRESS is provided, dump only the symbols for the file with code at that address.\n\
1563 If SOURCE is provided, dump only that file's symbols.\n\
1564 If OBJFILE is provided, dump only that object file's symbols."),
1565 &maintenanceprintlist);
1567 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
1568 List the partial symbol tables for all object files.\n\
1569 This does not include information about individual partial symbols,\n\
1570 just the symbol table structures themselves."),
1571 &maintenanceinfolist);
1573 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
1574 _("\
1575 Check consistency of currently expanded psymtabs versus symtabs."),
1576 &maintenancelist);