Clean up "Reading symbols" output
[external/binutils.git] / gdb / psymtab.c
1 /* Partial symbol tables.
2
3    Copyright (C) 2009-2018 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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.
11
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.
16
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/>.  */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "psympriv.h"
23 #include "objfiles.h"
24 #include "block.h"
25 #include "filenames.h"
26 #include "source.h"
27 #include "addrmap.h"
28 #include "gdbtypes.h"
29 #include "bcache.h"
30 #include "ui-out.h"
31 #include "command.h"
32 #include "readline/readline.h"
33 #include "gdb_regex.h"
34 #include "dictionary.h"
35 #include "language.h"
36 #include "cp-support.h"
37 #include "gdbcmd.h"
38 #include <algorithm>
39 #include <set>
40
41 struct psymbol_bcache
42 {
43   struct bcache *bcache;
44 };
45
46 static struct partial_symbol *match_partial_symbol (struct objfile *,
47                                                     struct partial_symtab *,
48                                                     int,
49                                                     const char *, domain_enum,
50                                                     symbol_name_match_type,
51                                                     symbol_compare_ftype *);
52
53 static struct partial_symbol *lookup_partial_symbol (struct objfile *,
54                                                      struct partial_symtab *,
55                                                      const char *, int,
56                                                      domain_enum);
57
58 static const char *psymtab_to_fullname (struct partial_symtab *ps);
59
60 static struct partial_symbol *find_pc_sect_psymbol (struct objfile *,
61                                                     struct partial_symtab *,
62                                                     CORE_ADDR,
63                                                     struct obj_section *);
64
65 static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
66                                                   struct partial_symtab *pst);
67
68 /* Ensure that the partial symbols for OBJFILE have been loaded.  This
69    function always returns its argument, as a convenience.  */
70
71 struct objfile *
72 require_partial_symbols (struct objfile *objfile, int verbose)
73 {
74   if ((objfile->flags & OBJF_PSYMTABS_READ) == 0)
75     {
76       objfile->flags |= OBJF_PSYMTABS_READ;
77
78       if (objfile->sf->sym_read_psymbols)
79         {
80           if (verbose)
81             printf_filtered (_("Reading symbols from %s...\n"),
82                              objfile_name (objfile));
83           (*objfile->sf->sym_read_psymbols) (objfile);
84
85           /* Partial symbols list are not expected to changed after this
86              point.  */
87           objfile->global_psymbols.shrink_to_fit ();
88           objfile->static_psymbols.shrink_to_fit ();
89
90           if (verbose && !objfile_has_symbols (objfile))
91             printf_filtered (_("(No debugging symbols found in %s)\n"),
92                              objfile_name (objfile));
93         }
94     }
95
96   return objfile;
97 }
98
99 /* Traverse all psymtabs in one objfile, requiring that the psymtabs
100    be read in.  */
101
102 #define ALL_OBJFILE_PSYMTABS_REQUIRED(objfile, p)               \
103     for ((p) = require_partial_symbols (objfile, 1)->psymtabs;  \
104          (p) != NULL;                                           \
105          (p) = (p)->next)
106
107 /* We want to make sure this file always requires psymtabs.  */
108
109 #undef ALL_OBJFILE_PSYMTABS
110
111 /* Traverse all psymtabs in all objfiles.  */
112
113 #define ALL_PSYMTABS(objfile, p) \
114   ALL_OBJFILES (objfile)         \
115     ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
116
117 /* Helper function for psym_map_symtabs_matching_filename that
118    expands the symtabs and calls the iterator.  */
119
120 static bool
121 partial_map_expand_apply (struct objfile *objfile,
122                           const char *name,
123                           const char *real_path,
124                           struct partial_symtab *pst,
125                           gdb::function_view<bool (symtab *)> callback)
126 {
127   struct compunit_symtab *last_made = objfile->compunit_symtabs;
128
129   /* Shared psymtabs should never be seen here.  Instead they should
130      be handled properly by the caller.  */
131   gdb_assert (pst->user == NULL);
132
133   /* Don't visit already-expanded psymtabs.  */
134   if (pst->readin)
135     return 0;
136
137   /* This may expand more than one symtab, and we want to iterate over
138      all of them.  */
139   psymtab_to_symtab (objfile, pst);
140
141   return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
142                                     last_made, callback);
143 }
144
145 /*  Psymtab version of map_symtabs_matching_filename.  See its definition in
146     the definition of quick_symbol_functions in symfile.h.  */
147
148 static bool
149 psym_map_symtabs_matching_filename
150   (struct objfile *objfile,
151    const char *name,
152    const char *real_path,
153    gdb::function_view<bool (symtab *)> callback)
154 {
155   struct partial_symtab *pst;
156   const char *name_basename = lbasename (name);
157
158   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
159   {
160     /* We can skip shared psymtabs here, because any file name will be
161        attached to the unshared psymtab.  */
162     if (pst->user != NULL)
163       continue;
164
165     /* Anonymous psymtabs don't have a file name.  */
166     if (pst->anonymous)
167       continue;
168
169     if (compare_filenames_for_search (pst->filename, name))
170       {
171         if (partial_map_expand_apply (objfile, name, real_path,
172                                       pst, callback))
173           return true;
174         continue;
175       }
176
177     /* Before we invoke realpath, which can get expensive when many
178        files are involved, do a quick comparison of the basenames.  */
179     if (! basenames_may_differ
180         && FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
181       continue;
182
183     if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
184       {
185         if (partial_map_expand_apply (objfile, name, real_path,
186                                       pst, callback))
187           return true;
188         continue;
189       }
190
191     /* If the user gave us an absolute path, try to find the file in
192        this symtab and use its absolute path.  */
193     if (real_path != NULL)
194       {
195         gdb_assert (IS_ABSOLUTE_PATH (real_path));
196         gdb_assert (IS_ABSOLUTE_PATH (name));
197         if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
198           {
199             if (partial_map_expand_apply (objfile, name, real_path,
200                                           pst, callback))
201               return true;
202             continue;
203           }
204       }
205   }
206
207   return false;
208 }
209
210 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
211    We may find a different psymtab than PST.  See FIND_PC_SECT_PSYMTAB.  */
212
213 static struct partial_symtab *
214 find_pc_sect_psymtab_closer (struct objfile *objfile,
215                              CORE_ADDR pc, struct obj_section *section,
216                              struct partial_symtab *pst,
217                              struct bound_minimal_symbol msymbol)
218 {
219   struct partial_symtab *tpst;
220   struct partial_symtab *best_pst = pst;
221   CORE_ADDR best_addr = pst->text_low (objfile);
222
223   gdb_assert (!pst->psymtabs_addrmap_supported);
224
225   /* An objfile that has its functions reordered might have
226      many partial symbol tables containing the PC, but
227      we want the partial symbol table that contains the
228      function containing the PC.  */
229   if (!(objfile->flags & OBJF_REORDERED)
230       && section == NULL)  /* Can't validate section this way.  */
231     return pst;
232
233   if (msymbol.minsym == NULL)
234     return pst;
235
236   /* The code range of partial symtabs sometimes overlap, so, in
237      the loop below, we need to check all partial symtabs and
238      find the one that fits better for the given PC address.  We
239      select the partial symtab that contains a symbol whose
240      address is closest to the PC address.  By closest we mean
241      that find_pc_sect_symbol returns the symbol with address
242      that is closest and still less than the given PC.  */
243   for (tpst = pst; tpst != NULL; tpst = tpst->next)
244     {
245       if (pc >= tpst->text_low (objfile) && pc < tpst->text_high (objfile))
246         {
247           struct partial_symbol *p;
248           CORE_ADDR this_addr;
249
250           /* NOTE: This assumes that every psymbol has a
251              corresponding msymbol, which is not necessarily
252              true; the debug info might be much richer than the
253              object's symbol table.  */
254           p = find_pc_sect_psymbol (objfile, tpst, pc, section);
255           if (p != NULL
256               && (p->address (objfile) == BMSYMBOL_VALUE_ADDRESS (msymbol)))
257             return tpst;
258
259           /* Also accept the textlow value of a psymtab as a
260              "symbol", to provide some support for partial
261              symbol tables with line information but no debug
262              symbols (e.g. those produced by an assembler).  */
263           if (p != NULL)
264             this_addr = p->address (objfile);
265           else
266             this_addr = tpst->text_low (objfile);
267
268           /* Check whether it is closer than our current
269              BEST_ADDR.  Since this symbol address is
270              necessarily lower or equal to PC, the symbol closer
271              to PC is the symbol which address is the highest.
272              This way we return the psymtab which contains such
273              best match symbol.  This can help in cases where the
274              symbol information/debuginfo is not complete, like
275              for instance on IRIX6 with gcc, where no debug info
276              is emitted for statics.  (See also the nodebug.exp
277              testcase.)  */
278           if (this_addr > best_addr)
279             {
280               best_addr = this_addr;
281               best_pst = tpst;
282             }
283         }
284     }
285   return best_pst;
286 }
287
288 /* Find which partial symtab contains PC and SECTION.  Return NULL if
289    none.  We return the psymtab that contains a symbol whose address
290    exactly matches PC, or, if we cannot find an exact match, the
291    psymtab that contains a symbol whose address is closest to PC.  */
292
293 static struct partial_symtab *
294 find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
295                       struct obj_section *section,
296                       struct bound_minimal_symbol msymbol)
297 {
298   struct partial_symtab *pst;
299   CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
300                                  SECT_OFF_TEXT (objfile));
301
302   /* Try just the PSYMTABS_ADDRMAP mapping first as it has better granularity
303      than the later used TEXTLOW/TEXTHIGH one.  */
304
305   if (objfile->psymtabs_addrmap != NULL)
306     {
307       pst = ((struct partial_symtab *)
308              addrmap_find (objfile->psymtabs_addrmap, pc - baseaddr));
309       if (pst != NULL)
310         {
311           /* FIXME: addrmaps currently do not handle overlayed sections,
312              so fall back to the non-addrmap case if we're debugging
313              overlays and the addrmap returned the wrong section.  */
314           if (overlay_debugging && msymbol.minsym != NULL && section != NULL)
315             {
316               struct partial_symbol *p;
317
318               /* NOTE: This assumes that every psymbol has a
319                  corresponding msymbol, which is not necessarily
320                  true; the debug info might be much richer than the
321                  object's symbol table.  */
322               p = find_pc_sect_psymbol (objfile, pst, pc, section);
323               if (p == NULL
324                   || (p->address (objfile)
325                       != BMSYMBOL_VALUE_ADDRESS (msymbol)))
326                 goto next;
327             }
328
329           /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
330              PSYMTABS_ADDRMAP we used has already the best 1-byte
331              granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
332              a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
333              overlap.  */
334
335           return pst;
336         }
337     }
338
339  next:
340
341   /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
342      which still have no corresponding full SYMTABs read.  But it is not
343      present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
344      so far.  */
345
346   /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
347      its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
348      debug info type in single OBJFILE.  */
349
350   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
351     if (!pst->psymtabs_addrmap_supported
352         && pc >= pst->text_low (objfile) && pc < pst->text_high (objfile))
353       {
354         struct partial_symtab *best_pst;
355
356         best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
357                                                 msymbol);
358         if (best_pst != NULL)
359           return best_pst;
360       }
361
362   return NULL;
363 }
364
365 /* Psymtab version of find_pc_sect_compunit_symtab.  See its definition in
366    the definition of quick_symbol_functions in symfile.h.  */
367
368 static struct compunit_symtab *
369 psym_find_pc_sect_compunit_symtab (struct objfile *objfile,
370                                    struct bound_minimal_symbol msymbol,
371                                    CORE_ADDR pc,
372                                    struct obj_section *section,
373                                    int warn_if_readin)
374 {
375   struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc, section,
376                                                     msymbol);
377   if (ps != NULL)
378     {
379       if (warn_if_readin && ps->readin)
380         /* Might want to error() here (in case symtab is corrupt and
381            will cause a core dump), but maybe we can successfully
382            continue, so let's not.  */
383         warning (_("\
384 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
385                  paddress (get_objfile_arch (objfile), pc));
386       psymtab_to_symtab (objfile, ps);
387       return ps->compunit_symtab;
388     }
389   return NULL;
390 }
391
392 /* Find which partial symbol within a psymtab matches PC and SECTION.
393    Return NULL if none.  */
394
395 static struct partial_symbol *
396 find_pc_sect_psymbol (struct objfile *objfile,
397                       struct partial_symtab *psymtab, CORE_ADDR pc,
398                       struct obj_section *section)
399 {
400   struct partial_symbol *best = NULL;
401   CORE_ADDR best_pc;
402   const CORE_ADDR textlow = psymtab->text_low (objfile);
403
404   gdb_assert (psymtab != NULL);
405
406   /* Cope with programs that start at address 0.  */
407   best_pc = (textlow != 0) ? textlow - 1 : 0;
408
409   /* Search the global symbols as well as the static symbols, so that
410      find_pc_partial_function doesn't use a minimal symbol and thus
411      cache a bad endaddr.  */
412   for (int i = 0; i < psymtab->n_global_syms; i++)
413     {
414       partial_symbol *p = objfile->global_psymbols[psymtab->globals_offset + i];
415
416       if (p->domain == VAR_DOMAIN
417           && p->aclass == LOC_BLOCK
418           && pc >= p->address (objfile)
419           && (p->address (objfile) > best_pc
420               || (psymtab->text_low (objfile) == 0
421                   && best_pc == 0 && p->address (objfile) == 0)))
422         {
423           if (section != NULL)  /* Match on a specific section.  */
424             {
425               if (!matching_obj_sections (p->obj_section (objfile),
426                                           section))
427                 continue;
428             }
429           best_pc = p->address (objfile);
430           best = p;
431         }
432     }
433
434   for (int i = 0; i < psymtab->n_static_syms; i++)
435     {
436       partial_symbol *p = objfile->static_psymbols[psymtab->statics_offset + i];
437
438       if (p->domain == VAR_DOMAIN
439           && p->aclass == LOC_BLOCK
440           && pc >= p->address (objfile)
441           && (p->address (objfile) > best_pc
442               || (psymtab->text_low (objfile) == 0
443                   && best_pc == 0 && p->address (objfile) == 0)))
444         {
445           if (section != NULL)  /* Match on a specific section.  */
446             {
447               if (!matching_obj_sections (p->obj_section (objfile),
448                                           section))
449                 continue;
450             }
451           best_pc = p->address (objfile);
452           best = p;
453         }
454     }
455
456   return best;
457 }
458
459 /* Psymtab version of lookup_symbol.  See its definition in
460    the definition of quick_symbol_functions in symfile.h.  */
461
462 static struct compunit_symtab *
463 psym_lookup_symbol (struct objfile *objfile,
464                     int block_index, const char *name,
465                     const domain_enum domain)
466 {
467   struct partial_symtab *ps;
468   const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
469   struct compunit_symtab *stab_best = NULL;
470
471   lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
472
473   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
474   {
475     if (!ps->readin && lookup_partial_symbol (objfile, ps, name,
476                                               psymtab_index, domain))
477       {
478         struct symbol *sym, *with_opaque = NULL;
479         struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
480         /* Note: While psymtab_to_symtab can return NULL if the partial symtab
481            is empty, we can assume it won't here because lookup_partial_symbol
482            succeeded.  */
483         const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
484         struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
485
486         sym = block_find_symbol (block, name, domain,
487                                  block_find_non_opaque_type_preferred,
488                                  &with_opaque);
489
490         /* Some caution must be observed with overloaded functions
491            and methods, since the index will not contain any overload
492            information (but NAME might contain it).  */
493
494         if (sym != NULL
495             && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
496           return stab;
497         if (with_opaque != NULL
498             && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
499           stab_best = stab;
500
501         /* Keep looking through other psymtabs.  */
502       }
503   }
504
505   return stab_best;
506 }
507
508 /* Returns true if PSYM matches LOOKUP_NAME.  */
509
510 static bool
511 psymbol_name_matches (partial_symbol *psym,
512                       const lookup_name_info &lookup_name)
513 {
514   const language_defn *lang = language_def (psym->language);
515   symbol_name_matcher_ftype *name_match
516     = get_symbol_name_matcher (lang, lookup_name);
517   return name_match (symbol_search_name (psym), lookup_name, NULL);
518 }
519
520 /* Look in PST for a symbol in DOMAIN whose name matches NAME.  Search
521    the global block of PST if GLOBAL, and otherwise the static block.
522    MATCH is the comparison operation that returns true iff MATCH (s,
523    NAME), where s is a SYMBOL_SEARCH_NAME.  If ORDERED_COMPARE is
524    non-null, the symbols in the block are assumed to be ordered
525    according to it (allowing binary search).  It must be compatible
526    with MATCH.  Returns the symbol, if found, and otherwise NULL.  */
527
528 static struct partial_symbol *
529 match_partial_symbol (struct objfile *objfile,
530                       struct partial_symtab *pst, int global,
531                       const char *name, domain_enum domain,
532                       symbol_name_match_type match_type,
533                       symbol_compare_ftype *ordered_compare)
534 {
535   struct partial_symbol **start, **psym;
536   struct partial_symbol **top, **real_top, **bottom, **center;
537   int length = (global ? pst->n_global_syms : pst->n_static_syms);
538   int do_linear_search = 1;
539
540   if (length == 0)
541     return NULL;
542
543   lookup_name_info lookup_name (name, match_type);
544
545   start = (global ?
546            &objfile->global_psymbols[pst->globals_offset] :
547            &objfile->static_psymbols[pst->statics_offset]);
548
549   if (global && ordered_compare)  /* Can use a binary search.  */
550     {
551       do_linear_search = 0;
552
553       /* Binary search.  This search is guaranteed to end with center
554          pointing at the earliest partial symbol whose name might be
555          correct.  At that point *all* partial symbols with an
556          appropriate name will be checked against the correct
557          domain.  */
558
559       bottom = start;
560       top = start + length - 1;
561       real_top = top;
562       while (top > bottom)
563         {
564           center = bottom + (top - bottom) / 2;
565           gdb_assert (center < top);
566
567           enum language lang = (*center)->language;
568           const char *lang_ln
569             = lookup_name.language_lookup_name (lang).c_str ();
570
571           if (ordered_compare (symbol_search_name (*center), lang_ln) >= 0)
572             top = center;
573           else
574             bottom = center + 1;
575         }
576       gdb_assert (top == bottom);
577
578       while (top <= real_top
579              && psymbol_name_matches (*top, lookup_name))
580         {
581           if (symbol_matches_domain ((*top)->language,
582                                      (*top)->domain, domain))
583             return *top;
584           top++;
585         }
586     }
587
588   /* Can't use a binary search or else we found during the binary search that
589      we should also do a linear search.  */
590
591   if (do_linear_search)
592     {
593       for (psym = start; psym < start + length; psym++)
594         {
595           if (symbol_matches_domain ((*psym)->language,
596                                      (*psym)->domain, domain)
597               && psymbol_name_matches (*psym, lookup_name))
598             return *psym;
599         }
600     }
601
602   return NULL;
603 }
604
605 /* Returns the name used to search psymtabs.  Unlike symtabs, psymtabs do
606    not contain any method/function instance information (since this would
607    force reading type information while reading psymtabs).  Therefore,
608    if NAME contains overload information, it must be stripped before searching
609    psymtabs.  */
610
611 static gdb::unique_xmalloc_ptr<char>
612 psymtab_search_name (const char *name)
613 {
614   switch (current_language->la_language)
615     {
616     case language_cplus:
617       {
618         if (strchr (name, '('))
619           {
620             gdb::unique_xmalloc_ptr<char> ret = cp_remove_params (name);
621
622             if (ret)
623               return ret;
624           }
625       }
626       break;
627
628     default:
629       break;
630     }
631
632   return gdb::unique_xmalloc_ptr<char> (xstrdup (name));
633 }
634
635 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
636    Check the global symbols if GLOBAL, the static symbols if not.  */
637
638 static struct partial_symbol *
639 lookup_partial_symbol (struct objfile *objfile,
640                        struct partial_symtab *pst, const char *name,
641                        int global, domain_enum domain)
642 {
643   struct partial_symbol **start, **psym;
644   struct partial_symbol **top, **real_top, **bottom, **center;
645   int length = (global ? pst->n_global_syms : pst->n_static_syms);
646   int do_linear_search = 1;
647
648   if (length == 0)
649     return NULL;
650
651   gdb::unique_xmalloc_ptr<char> search_name = psymtab_search_name (name);
652
653   lookup_name_info lookup_name (search_name.get (), symbol_name_match_type::FULL);
654
655   start = (global ?
656            &objfile->global_psymbols[pst->globals_offset] :
657            &objfile->static_psymbols[pst->statics_offset]);
658
659   if (global)                   /* This means we can use a binary search.  */
660     {
661       do_linear_search = 0;
662
663       /* Binary search.  This search is guaranteed to end with center
664          pointing at the earliest partial symbol whose name might be
665          correct.  At that point *all* partial symbols with an
666          appropriate name will be checked against the correct
667          domain.  */
668
669       bottom = start;
670       top = start + length - 1;
671       real_top = top;
672       while (top > bottom)
673         {
674           center = bottom + (top - bottom) / 2;
675           if (!(center < top))
676             internal_error (__FILE__, __LINE__,
677                             _("failed internal consistency check"));
678           if (strcmp_iw_ordered (symbol_search_name (*center),
679                                  search_name.get ()) >= 0)
680             {
681               top = center;
682             }
683           else
684             {
685               bottom = center + 1;
686             }
687         }
688       if (!(top == bottom))
689         internal_error (__FILE__, __LINE__,
690                         _("failed internal consistency check"));
691
692       /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
693          search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME.  */
694       while (top >= start && symbol_matches_search_name (*top, lookup_name))
695         top--;
696
697       /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME.  */
698       top++;
699
700       while (top <= real_top && symbol_matches_search_name (*top, lookup_name))
701         {
702           if (symbol_matches_domain ((*top)->language,
703                                      (*top)->domain, domain))
704             return *top;
705           top++;
706         }
707     }
708
709   /* Can't use a binary search or else we found during the binary search that
710      we should also do a linear search.  */
711
712   if (do_linear_search)
713     {
714       for (psym = start; psym < start + length; psym++)
715         {
716           if (symbol_matches_domain ((*psym)->language,
717                                      (*psym)->domain, domain)
718               && symbol_matches_search_name (*psym, lookup_name))
719             return *psym;
720         }
721     }
722
723   return NULL;
724 }
725
726 /* Get the symbol table that corresponds to a partial_symtab.
727    This is fast after the first time you do it.
728    The result will be NULL if the primary symtab has no symbols,
729    which can happen.  Otherwise the result is the primary symtab
730    that contains PST.  */
731
732 static struct compunit_symtab *
733 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
734 {
735   /* If it is a shared psymtab, find an unshared psymtab that includes
736      it.  Any such psymtab will do.  */
737   while (pst->user != NULL)
738     pst = pst->user;
739
740   /* If it's been looked up before, return it.  */
741   if (pst->compunit_symtab)
742     return pst->compunit_symtab;
743
744   /* If it has not yet been read in, read it.  */
745   if (!pst->readin)
746     {
747       scoped_restore decrementer = increment_reading_symtab ();
748
749       (*pst->read_symtab) (pst, objfile);
750     }
751
752   return pst->compunit_symtab;
753 }
754
755 /* Psymtab version of find_last_source_symtab.  See its definition in
756    the definition of quick_symbol_functions in symfile.h.  */
757
758 static struct symtab *
759 psym_find_last_source_symtab (struct objfile *ofp)
760 {
761   struct partial_symtab *ps;
762   struct partial_symtab *cs_pst = NULL;
763
764   ALL_OBJFILE_PSYMTABS_REQUIRED (ofp, ps)
765     {
766       const char *name = ps->filename;
767       int len = strlen (name);
768
769       if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
770                         || strcmp (name, "<<C++-namespaces>>") == 0)))
771         cs_pst = ps;
772     }
773
774   if (cs_pst)
775     {
776       if (cs_pst->readin)
777         {
778           internal_error (__FILE__, __LINE__,
779                           _("select_source_symtab: "
780                           "readin pst found and no symtabs."));
781         }
782       else
783         {
784           struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
785
786           if (cust == NULL)
787             return NULL;
788           return compunit_primary_filetab (cust);
789         }
790     }
791   return NULL;
792 }
793
794 /* Psymtab version of forget_cached_source_info.  See its definition in
795    the definition of quick_symbol_functions in symfile.h.  */
796
797 static void
798 psym_forget_cached_source_info (struct objfile *objfile)
799 {
800   struct partial_symtab *pst;
801
802   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
803     {
804       if (pst->fullname != NULL)
805         {
806           xfree (pst->fullname);
807           pst->fullname = NULL;
808         }
809     }
810 }
811
812 static void
813 print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
814                        struct partial_symbol **p, int count, const char *what,
815                        struct ui_file *outfile)
816 {
817   fprintf_filtered (outfile, "  %s partial symbols:\n", what);
818   while (count-- > 0)
819     {
820       QUIT;
821       fprintf_filtered (outfile, "    `%s'", (*p)->name);
822       if (symbol_demangled_name (*p) != NULL)
823         {
824           fprintf_filtered (outfile, "  `%s'", symbol_demangled_name (*p));
825         }
826       fputs_filtered (", ", outfile);
827       switch ((*p)->domain)
828         {
829         case UNDEF_DOMAIN:
830           fputs_filtered ("undefined domain, ", outfile);
831           break;
832         case VAR_DOMAIN:
833           /* This is the usual thing -- don't print it.  */
834           break;
835         case STRUCT_DOMAIN:
836           fputs_filtered ("struct domain, ", outfile);
837           break;
838         case LABEL_DOMAIN:
839           fputs_filtered ("label domain, ", outfile);
840           break;
841         default:
842           fputs_filtered ("<invalid domain>, ", outfile);
843           break;
844         }
845       switch ((*p)->aclass)
846         {
847         case LOC_UNDEF:
848           fputs_filtered ("undefined", outfile);
849           break;
850         case LOC_CONST:
851           fputs_filtered ("constant int", outfile);
852           break;
853         case LOC_STATIC:
854           fputs_filtered ("static", outfile);
855           break;
856         case LOC_REGISTER:
857           fputs_filtered ("register", outfile);
858           break;
859         case LOC_ARG:
860           fputs_filtered ("pass by value", outfile);
861           break;
862         case LOC_REF_ARG:
863           fputs_filtered ("pass by reference", outfile);
864           break;
865         case LOC_REGPARM_ADDR:
866           fputs_filtered ("register address parameter", outfile);
867           break;
868         case LOC_LOCAL:
869           fputs_filtered ("stack parameter", outfile);
870           break;
871         case LOC_TYPEDEF:
872           fputs_filtered ("type", outfile);
873           break;
874         case LOC_LABEL:
875           fputs_filtered ("label", outfile);
876           break;
877         case LOC_BLOCK:
878           fputs_filtered ("function", outfile);
879           break;
880         case LOC_CONST_BYTES:
881           fputs_filtered ("constant bytes", outfile);
882           break;
883         case LOC_UNRESOLVED:
884           fputs_filtered ("unresolved", outfile);
885           break;
886         case LOC_OPTIMIZED_OUT:
887           fputs_filtered ("optimized out", outfile);
888           break;
889         case LOC_COMPUTED:
890           fputs_filtered ("computed at runtime", outfile);
891           break;
892         default:
893           fputs_filtered ("<invalid location>", outfile);
894           break;
895         }
896       fputs_filtered (", ", outfile);
897       fputs_filtered (paddress (gdbarch, (*p)->unrelocated_address ()), outfile);
898       fprintf_filtered (outfile, "\n");
899       p++;
900     }
901 }
902
903 static void
904 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
905               struct ui_file *outfile)
906 {
907   struct gdbarch *gdbarch = get_objfile_arch (objfile);
908   int i;
909
910   if (psymtab->anonymous)
911     {
912       fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
913                         psymtab->filename);
914     }
915   else
916     {
917       fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
918                         psymtab->filename);
919     }
920   fprintf_filtered (outfile, "(object ");
921   gdb_print_host_address (psymtab, outfile);
922   fprintf_filtered (outfile, ")\n\n");
923   fprintf_filtered (outfile, "  Read from object file %s (",
924                     objfile_name (objfile));
925   gdb_print_host_address (objfile, outfile);
926   fprintf_filtered (outfile, ")\n");
927
928   if (psymtab->readin)
929     {
930       fprintf_filtered (outfile,
931                         "  Full symtab was read (at ");
932       gdb_print_host_address (psymtab->compunit_symtab, outfile);
933       fprintf_filtered (outfile, " by function at ");
934       gdb_print_host_address (psymtab->read_symtab, outfile);
935       fprintf_filtered (outfile, ")\n");
936     }
937
938   fprintf_filtered (outfile, "  Symbols cover text addresses ");
939   fputs_filtered (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
940   fprintf_filtered (outfile, "-");
941   fputs_filtered (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
942   fprintf_filtered (outfile, "\n");
943   fprintf_filtered (outfile, "  Address map supported - %s.\n",
944                     psymtab->psymtabs_addrmap_supported ? "yes" : "no");
945   fprintf_filtered (outfile, "  Depends on %d other partial symtabs.\n",
946                     psymtab->number_of_dependencies);
947   for (i = 0; i < psymtab->number_of_dependencies; i++)
948     {
949       fprintf_filtered (outfile, "    %d ", i);
950       gdb_print_host_address (psymtab->dependencies[i], outfile);
951       fprintf_filtered (outfile, " %s\n",
952                         psymtab->dependencies[i]->filename);
953     }
954   if (psymtab->user != NULL)
955     {
956       fprintf_filtered (outfile, "  Shared partial symtab with user ");
957       gdb_print_host_address (psymtab->user, outfile);
958       fprintf_filtered (outfile, "\n");
959     }
960   if (psymtab->n_global_syms > 0)
961     {
962       print_partial_symbols (gdbarch, objfile,
963                              &objfile->global_psymbols[psymtab->globals_offset],
964                              psymtab->n_global_syms, "Global", outfile);
965     }
966   if (psymtab->n_static_syms > 0)
967     {
968       print_partial_symbols (gdbarch, objfile,
969                              &objfile->static_psymbols[psymtab->statics_offset],
970                              psymtab->n_static_syms, "Static", outfile);
971     }
972   fprintf_filtered (outfile, "\n");
973 }
974
975 /* Psymtab version of print_stats.  See its definition in
976    the definition of quick_symbol_functions in symfile.h.  */
977
978 static void
979 psym_print_stats (struct objfile *objfile)
980 {
981   int i;
982   struct partial_symtab *ps;
983
984   i = 0;
985   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
986     {
987       if (ps->readin == 0)
988         i++;
989     }
990   printf_filtered (_("  Number of psym tables (not yet expanded): %d\n"), i);
991 }
992
993 /* Psymtab version of dump.  See its definition in
994    the definition of quick_symbol_functions in symfile.h.  */
995
996 static void
997 psym_dump (struct objfile *objfile)
998 {
999   struct partial_symtab *psymtab;
1000
1001   if (objfile->psymtabs)
1002     {
1003       printf_filtered ("Psymtabs:\n");
1004       for (psymtab = objfile->psymtabs;
1005            psymtab != NULL;
1006            psymtab = psymtab->next)
1007         {
1008           printf_filtered ("%s at ",
1009                            psymtab->filename);
1010           gdb_print_host_address (psymtab, gdb_stdout);
1011           printf_filtered (", ");
1012           wrap_here ("  ");
1013         }
1014       printf_filtered ("\n\n");
1015     }
1016 }
1017
1018 /* Psymtab version of expand_symtabs_for_function.  See its definition in
1019    the definition of quick_symbol_functions in symfile.h.  */
1020
1021 static void
1022 psym_expand_symtabs_for_function (struct objfile *objfile,
1023                                   const char *func_name)
1024 {
1025   struct partial_symtab *ps;
1026
1027   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1028   {
1029     if (ps->readin)
1030       continue;
1031
1032     if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
1033          != NULL)
1034         || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
1035             != NULL))
1036       psymtab_to_symtab (objfile, ps);
1037   }
1038 }
1039
1040 /* Psymtab version of expand_all_symtabs.  See its definition in
1041    the definition of quick_symbol_functions in symfile.h.  */
1042
1043 static void
1044 psym_expand_all_symtabs (struct objfile *objfile)
1045 {
1046   struct partial_symtab *psymtab;
1047
1048   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
1049     {
1050       psymtab_to_symtab (objfile, psymtab);
1051     }
1052 }
1053
1054 /* Psymtab version of expand_symtabs_with_fullname.  See its definition in
1055    the definition of quick_symbol_functions in symfile.h.  */
1056
1057 static void
1058 psym_expand_symtabs_with_fullname (struct objfile *objfile,
1059                                    const char *fullname)
1060 {
1061   struct partial_symtab *p;
1062
1063   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
1064     {
1065       /* Anonymous psymtabs don't have a name of a source file.  */
1066       if (p->anonymous)
1067         continue;
1068
1069       /* psymtab_to_fullname tries to open the file which is slow.
1070          Don't call it if we know the basenames don't match.  */
1071       if ((basenames_may_differ
1072            || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1073           && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
1074         psymtab_to_symtab (objfile, p);
1075     }
1076 }
1077
1078 /* Psymtab version of map_symbol_filenames.  See its definition in
1079    the definition of quick_symbol_functions in symfile.h.  */
1080
1081 static void
1082 psym_map_symbol_filenames (struct objfile *objfile,
1083                            symbol_filename_ftype *fun, void *data,
1084                            int need_fullname)
1085 {
1086   struct partial_symtab *ps;
1087
1088   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1089     {
1090       const char *fullname;
1091
1092       if (ps->readin)
1093         continue;
1094
1095       /* We can skip shared psymtabs here, because any file name will be
1096          attached to the unshared psymtab.  */
1097       if (ps->user != NULL)
1098         continue;
1099
1100       /* Anonymous psymtabs don't have a file name.  */
1101       if (ps->anonymous)
1102         continue;
1103
1104       QUIT;
1105       if (need_fullname)
1106         fullname = psymtab_to_fullname (ps);
1107       else
1108         fullname = NULL;
1109       (*fun) (ps->filename, fullname, data);
1110     }
1111 }
1112
1113 /* Finds the fullname that a partial_symtab represents.
1114
1115    If this functions finds the fullname, it will save it in ps->fullname
1116    and it will also return the value.
1117
1118    If this function fails to find the file that this partial_symtab represents,
1119    NULL will be returned and ps->fullname will be set to NULL.  */
1120
1121 static const char *
1122 psymtab_to_fullname (struct partial_symtab *ps)
1123 {
1124   gdb_assert (!ps->anonymous);
1125
1126   /* Use cached copy if we have it.
1127      We rely on forget_cached_source_info being called appropriately
1128      to handle cases like the file being moved.  */
1129   if (ps->fullname == NULL)
1130     {
1131       gdb::unique_xmalloc_ptr<char> fullname;
1132       int fd = find_and_open_source (ps->filename, ps->dirname, &fullname);
1133       ps->fullname = fullname.release ();
1134
1135       if (fd >= 0)
1136         close (fd);
1137       else
1138         {
1139           /* rewrite_source_path would be applied by find_and_open_source, we
1140              should report the pathname where GDB tried to find the file.  */
1141
1142           if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
1143             fullname.reset (xstrdup (ps->filename));
1144           else
1145             fullname.reset (concat (ps->dirname, SLASH_STRING,
1146                                     ps->filename, (char *) NULL));
1147
1148           ps->fullname = rewrite_source_path (fullname.get ()).release ();
1149           if (ps->fullname == NULL)
1150             ps->fullname = fullname.release ();
1151         }
1152     }
1153
1154   return ps->fullname;
1155 }
1156
1157 /* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1158    according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1159    BLOCK is assumed to come from OBJFILE.  Returns 1 iff CALLBACK
1160    ever returns non-zero, and otherwise returns 0.  */
1161
1162 static int
1163 map_block (const char *name, domain_enum domain, struct objfile *objfile,
1164            struct block *block,
1165            int (*callback) (struct block *, struct symbol *, void *),
1166            void *data, symbol_name_match_type match)
1167 {
1168   struct block_iterator iter;
1169   struct symbol *sym;
1170
1171   lookup_name_info lookup_name (name, match);
1172
1173   for (sym = block_iter_match_first (block, lookup_name, &iter);
1174        sym != NULL;
1175        sym = block_iter_match_next (lookup_name, &iter))
1176     {
1177       if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
1178                                  SYMBOL_DOMAIN (sym), domain))
1179         {
1180           if (callback (block, sym, data))
1181             return 1;
1182         }
1183     }
1184
1185   return 0;
1186 }
1187
1188 /* Psymtab version of map_matching_symbols.  See its definition in
1189    the definition of quick_symbol_functions in symfile.h.  */
1190
1191 static void
1192 psym_map_matching_symbols (struct objfile *objfile,
1193                            const char *name, domain_enum domain,
1194                            int global,
1195                            int (*callback) (struct block *,
1196                                             struct symbol *, void *),
1197                            void *data,
1198                            symbol_name_match_type match,
1199                            symbol_compare_ftype *ordered_compare)
1200 {
1201   const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
1202   struct partial_symtab *ps;
1203
1204   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1205     {
1206       QUIT;
1207       if (ps->readin
1208           || match_partial_symbol (objfile, ps, global, name, domain, match,
1209                                    ordered_compare))
1210         {
1211           struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
1212           struct block *block;
1213
1214           if (cust == NULL)
1215             continue;
1216           block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
1217           if (map_block (name, domain, objfile, block,
1218                          callback, data, match))
1219             return;
1220           if (callback (block, NULL, data))
1221             return;
1222         }
1223     }
1224 }
1225
1226 /* A helper for psym_expand_symtabs_matching that handles searching
1227    included psymtabs.  This returns true if a symbol is found, and
1228    false otherwise.  It also updates the 'searched_flag' on the
1229    various psymtabs that it searches.  */
1230
1231 static bool
1232 recursively_search_psymtabs
1233   (struct partial_symtab *ps, struct objfile *objfile, enum search_domain domain,
1234    const lookup_name_info &lookup_name,
1235    gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
1236 {
1237   int keep_going = 1;
1238   enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
1239   int i;
1240
1241   if (ps->searched_flag != PST_NOT_SEARCHED)
1242     return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1243
1244   /* Recurse into shared psymtabs first, because they may have already
1245      been searched, and this could save some time.  */
1246   for (i = 0; i < ps->number_of_dependencies; ++i)
1247     {
1248       int r;
1249
1250       /* Skip non-shared dependencies, these are handled elsewhere.  */
1251       if (ps->dependencies[i]->user == NULL)
1252         continue;
1253
1254       r = recursively_search_psymtabs (ps->dependencies[i],
1255                                        objfile, domain, lookup_name,
1256                                        sym_matcher);
1257       if (r != 0)
1258         {
1259           ps->searched_flag = PST_SEARCHED_AND_FOUND;
1260           return true;
1261         }
1262     }
1263
1264   partial_symbol **gbound
1265     = objfile->global_psymbols.data () + ps->globals_offset + ps->n_global_syms;
1266   partial_symbol **sbound
1267     = objfile->static_psymbols.data () + ps->statics_offset + ps->n_static_syms;
1268   partial_symbol **bound = gbound;
1269
1270   /* Go through all of the symbols stored in a partial
1271      symtab in one loop.  */
1272   partial_symbol **psym = objfile->global_psymbols.data () + ps->globals_offset;
1273   while (keep_going)
1274     {
1275       if (psym >= bound)
1276         {
1277           if (bound == gbound && ps->n_static_syms != 0)
1278             {
1279               psym = objfile->static_psymbols.data () + ps->statics_offset;
1280               bound = sbound;
1281             }
1282           else
1283             keep_going = 0;
1284           continue;
1285         }
1286       else
1287         {
1288           QUIT;
1289
1290           if ((domain == ALL_DOMAIN
1291                || (domain == VARIABLES_DOMAIN
1292                    && (*psym)->aclass != LOC_TYPEDEF
1293                    && (*psym)->aclass != LOC_BLOCK)
1294                || (domain == FUNCTIONS_DOMAIN
1295                    && (*psym)->aclass == LOC_BLOCK)
1296                || (domain == TYPES_DOMAIN
1297                    && (*psym)->aclass == LOC_TYPEDEF))
1298               && psymbol_name_matches (*psym, lookup_name)
1299               && (sym_matcher == NULL || sym_matcher (symbol_search_name (*psym))))
1300             {
1301               /* Found a match, so notify our caller.  */
1302               result = PST_SEARCHED_AND_FOUND;
1303               keep_going = 0;
1304             }
1305         }
1306       psym++;
1307     }
1308
1309   ps->searched_flag = result;
1310   return result == PST_SEARCHED_AND_FOUND;
1311 }
1312
1313 /* Psymtab version of expand_symtabs_matching.  See its definition in
1314    the definition of quick_symbol_functions in symfile.h.  */
1315
1316 static void
1317 psym_expand_symtabs_matching
1318   (struct objfile *objfile,
1319    gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1320    const lookup_name_info &lookup_name_in,
1321    gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1322    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1323    enum search_domain domain)
1324 {
1325   struct partial_symtab *ps;
1326
1327   lookup_name_info lookup_name = lookup_name_in.make_ignore_params ();
1328
1329   /* Clear the search flags.  */
1330   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1331     {
1332       ps->searched_flag = PST_NOT_SEARCHED;
1333     }
1334
1335   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1336     {
1337       QUIT;
1338
1339       if (ps->readin)
1340         continue;
1341
1342       /* We skip shared psymtabs because file-matching doesn't apply
1343          to them; but we search them later in the loop.  */
1344       if (ps->user != NULL)
1345         continue;
1346
1347       if (file_matcher)
1348         {
1349           bool match;
1350
1351           if (ps->anonymous)
1352             continue;
1353
1354           match = file_matcher (ps->filename, false);
1355           if (!match)
1356             {
1357               /* Before we invoke realpath, which can get expensive when many
1358                  files are involved, do a quick comparison of the basenames.  */
1359               if (basenames_may_differ
1360                   || file_matcher (lbasename (ps->filename), true))
1361                 match = file_matcher (psymtab_to_fullname (ps), false);
1362             }
1363           if (!match)
1364             continue;
1365         }
1366
1367       if (recursively_search_psymtabs (ps, objfile, domain,
1368                                        lookup_name, symbol_matcher))
1369         {
1370           struct compunit_symtab *symtab =
1371             psymtab_to_symtab (objfile, ps);
1372
1373           if (expansion_notify != NULL)
1374             expansion_notify (symtab);
1375         }
1376     }
1377 }
1378
1379 /* Psymtab version of has_symbols.  See its definition in
1380    the definition of quick_symbol_functions in symfile.h.  */
1381
1382 static int
1383 psym_has_symbols (struct objfile *objfile)
1384 {
1385   return objfile->psymtabs != NULL;
1386 }
1387
1388 /* Helper function for psym_find_compunit_symtab_by_address that fills
1389    in psymbol_map for a given range of psymbols.  */
1390
1391 static void
1392 psym_fill_psymbol_map (struct objfile *objfile,
1393                        struct partial_symtab *psymtab,
1394                        std::set<CORE_ADDR> *seen_addrs,
1395                        const std::vector<partial_symbol *> &symbols,
1396                        int start,
1397                        int length)
1398 {
1399   for (int i = 0; i < length; ++i)
1400     {
1401       struct partial_symbol *psym = symbols[start + i];
1402
1403       if (psym->aclass == LOC_STATIC)
1404         {
1405           CORE_ADDR addr = psym->address (objfile);
1406           if (seen_addrs->find (addr) == seen_addrs->end ())
1407             {
1408               seen_addrs->insert (addr);
1409               objfile->psymbol_map.emplace_back (addr, psymtab);
1410             }
1411         }
1412     }
1413 }
1414
1415 /* See find_compunit_symtab_by_address in quick_symbol_functions, in
1416    symfile.h.  */
1417
1418 static compunit_symtab *
1419 psym_find_compunit_symtab_by_address (struct objfile *objfile,
1420                                       CORE_ADDR address)
1421 {
1422   if (objfile->psymbol_map.empty ())
1423     {
1424       struct partial_symtab *pst;
1425
1426       std::set<CORE_ADDR> seen_addrs;
1427
1428       ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
1429       {
1430         psym_fill_psymbol_map (objfile, pst,
1431                                &seen_addrs,
1432                                objfile->global_psymbols,
1433                                pst->globals_offset,
1434                                pst->n_global_syms);
1435         psym_fill_psymbol_map (objfile, pst,
1436                                &seen_addrs,
1437                                objfile->static_psymbols,
1438                                pst->statics_offset,
1439                                pst->n_static_syms);
1440       }
1441
1442       objfile->psymbol_map.shrink_to_fit ();
1443
1444       std::sort (objfile->psymbol_map.begin (), objfile->psymbol_map.end (),
1445                  [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1446                      const std::pair<CORE_ADDR, partial_symtab *> &b)
1447                  {
1448                    return a.first < b.first;
1449                  });
1450     }
1451
1452   auto iter = std::lower_bound
1453     (objfile->psymbol_map.begin (), objfile->psymbol_map.end (), address,
1454      [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1455          CORE_ADDR b)
1456      {
1457        return a.first < b;
1458      });
1459
1460   if (iter == objfile->psymbol_map.end () || iter->first != address)
1461     return NULL;
1462
1463   return psymtab_to_symtab (objfile, iter->second);
1464 }
1465
1466 const struct quick_symbol_functions psym_functions =
1467 {
1468   psym_has_symbols,
1469   psym_find_last_source_symtab,
1470   psym_forget_cached_source_info,
1471   psym_map_symtabs_matching_filename,
1472   psym_lookup_symbol,
1473   psym_print_stats,
1474   psym_dump,
1475   psym_expand_symtabs_for_function,
1476   psym_expand_all_symtabs,
1477   psym_expand_symtabs_with_fullname,
1478   psym_map_matching_symbols,
1479   psym_expand_symtabs_matching,
1480   psym_find_pc_sect_compunit_symtab,
1481   psym_find_compunit_symtab_by_address,
1482   psym_map_symbol_filenames
1483 };
1484
1485 \f
1486
1487 static void
1488 sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
1489 {
1490   /* Sort the global list; don't sort the static list.  */
1491   auto begin = objfile->global_psymbols.begin ();
1492   std::advance (begin, pst->globals_offset);
1493
1494   /* The psymbols for this partial_symtab are currently at the end of the
1495      vector.  */
1496   auto end = objfile->global_psymbols.end ();
1497
1498   std::sort (begin, end, [] (partial_symbol *s1, partial_symbol *s2)
1499     {
1500       return strcmp_iw_ordered (symbol_search_name (s1),
1501                                 symbol_search_name (s2)) < 0;
1502     });
1503 }
1504
1505 /* Allocate and partially fill a partial symtab.  It will be
1506    completely filled at the end of the symbol list.
1507
1508    FILENAME is the name of the symbol-file we are reading from.  */
1509
1510 struct partial_symtab *
1511 start_psymtab_common (struct objfile *objfile,
1512                       const char *filename,
1513                       CORE_ADDR textlow,
1514                       std::vector<partial_symbol *> &global_psymbols,
1515                       std::vector<partial_symbol *> &static_psymbols)
1516 {
1517   struct partial_symtab *psymtab;
1518
1519   psymtab = allocate_psymtab (filename, objfile);
1520   psymtab->set_text_low (textlow);
1521   psymtab->set_text_high (psymtab->raw_text_low ()); /* default */
1522   psymtab->globals_offset = global_psymbols.size ();
1523   psymtab->statics_offset = static_psymbols.size ();
1524   return psymtab;
1525 }
1526
1527 /* Perform "finishing up" operations of a partial symtab.  */
1528
1529 void
1530 end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1531 {
1532   pst->n_global_syms = objfile->global_psymbols.size () - pst->globals_offset;
1533   pst->n_static_syms = objfile->static_psymbols.size () - pst->statics_offset;
1534
1535   sort_pst_symbols (objfile, pst);
1536 }
1537
1538 /* Calculate a hash code for the given partial symbol.  The hash is
1539    calculated using the symbol's value, language, domain, class
1540    and name.  These are the values which are set by
1541    add_psymbol_to_bcache.  */
1542
1543 static unsigned long
1544 psymbol_hash (const void *addr, int length)
1545 {
1546   unsigned long h = 0;
1547   struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1548   unsigned int lang = psymbol->language;
1549   unsigned int domain = psymbol->domain;
1550   unsigned int theclass = psymbol->aclass;
1551
1552   h = hash_continue (&psymbol->value, sizeof (psymbol->value), h);
1553   h = hash_continue (&lang, sizeof (unsigned int), h);
1554   h = hash_continue (&domain, sizeof (unsigned int), h);
1555   h = hash_continue (&theclass, sizeof (unsigned int), h);
1556   /* Note that psymbol names are interned via symbol_set_names, so
1557      there's no need to hash the contents of the name here.  */
1558   h = hash_continue (&psymbol->name,
1559                      sizeof (psymbol->name), h);
1560
1561   return h;
1562 }
1563
1564 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1565    For the comparison this function uses a symbols value,
1566    language, domain, class and name.  */
1567
1568 static int
1569 psymbol_compare (const void *addr1, const void *addr2, int length)
1570 {
1571   struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1572   struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1573
1574   return (memcmp (&sym1->value, &sym2->value,
1575                   sizeof (sym1->value)) == 0
1576           && sym1->language == sym2->language
1577           && sym1->domain == sym2->domain
1578           && sym1->aclass == sym2->aclass
1579           /* Note that psymbol names are interned via
1580              symbol_set_names, so there's no need to compare the
1581              contents of the name here.  */
1582           && sym1->name == sym2->name);
1583 }
1584
1585 /* Initialize a partial symbol bcache.  */
1586
1587 struct psymbol_bcache *
1588 psymbol_bcache_init (void)
1589 {
1590   struct psymbol_bcache *bcache = XCNEW (struct psymbol_bcache);
1591
1592   bcache->bcache = bcache_xmalloc (psymbol_hash, psymbol_compare);
1593   return bcache;
1594 }
1595
1596 /* Free a partial symbol bcache.  */
1597
1598 void
1599 psymbol_bcache_free (struct psymbol_bcache *bcache)
1600 {
1601   if (bcache == NULL)
1602     return;
1603
1604   bcache_xfree (bcache->bcache);
1605   xfree (bcache);
1606 }
1607
1608 /* Return the internal bcache of the psymbol_bcache BCACHE.  */
1609
1610 struct bcache *
1611 psymbol_bcache_get_bcache (struct psymbol_bcache *bcache)
1612 {
1613   return bcache->bcache;
1614 }
1615
1616 /* Find a copy of the SYM in BCACHE.  If BCACHE has never seen this
1617    symbol before, add a copy to BCACHE.  In either case, return a pointer
1618    to BCACHE's copy of the symbol.  If optional ADDED is not NULL, return
1619    1 in case of new entry or 0 if returning an old entry.  */
1620
1621 static struct partial_symbol *
1622 psymbol_bcache_full (struct partial_symbol *sym,
1623                      struct psymbol_bcache *bcache,
1624                      int *added)
1625 {
1626   return ((struct partial_symbol *)
1627           bcache_full (sym, sizeof (struct partial_symbol), bcache->bcache,
1628                        added));
1629 }
1630
1631 /* Helper function, initialises partial symbol structure and stashes
1632    it into objfile's bcache.  Note that our caching mechanism will
1633    use all fields of struct partial_symbol to determine hash value of the
1634    structure.  In other words, having two symbols with the same name but
1635    different domain (or address) is possible and correct.  */
1636
1637 static struct partial_symbol *
1638 add_psymbol_to_bcache (const char *name, int namelength, int copy_name,
1639                        domain_enum domain,
1640                        enum address_class theclass,
1641                        short section,
1642                        CORE_ADDR coreaddr,
1643                        enum language language, struct objfile *objfile,
1644                        int *added)
1645 {
1646   struct partial_symbol psymbol;
1647
1648   psymbol.set_unrelocated_address (coreaddr);
1649   psymbol.section = section;
1650   psymbol.domain = domain;
1651   psymbol.aclass = theclass;
1652
1653   memset (&psymbol.language_specific, 0, sizeof (psymbol.language_specific));
1654   psymbol.ada_mangled = 0;
1655   symbol_set_language (&psymbol, language, &objfile->objfile_obstack);
1656   symbol_set_names (&psymbol, name, namelength, copy_name, objfile);
1657
1658   /* Stash the partial symbol away in the cache.  */
1659   return psymbol_bcache_full (&psymbol, objfile->psymbol_cache, added);
1660 }
1661
1662 /* Helper function, adds partial symbol to the given partial symbol list.  */
1663
1664 static void
1665 append_psymbol_to_list (std::vector<partial_symbol *> *list,
1666                         struct partial_symbol *psym,
1667                         struct objfile *objfile)
1668 {
1669   list->push_back (psym);
1670   OBJSTAT (objfile, n_psyms++);
1671 }
1672
1673 /* Add a symbol with a long value to a psymtab.
1674    Since one arg is a struct, we pass in a ptr and deref it (sigh).
1675    The only value we need to store for psyms is an address.
1676    For all other psyms pass zero for COREADDR.
1677    Return the partial symbol that has been added.  */
1678
1679 void
1680 add_psymbol_to_list (const char *name, int namelength, int copy_name,
1681                      domain_enum domain,
1682                      enum address_class theclass,
1683                      short section,
1684                      std::vector<partial_symbol *> *list,
1685                      CORE_ADDR coreaddr,
1686                      enum language language, struct objfile *objfile)
1687 {
1688   struct partial_symbol *psym;
1689
1690   int added;
1691
1692   /* Stash the partial symbol away in the cache.  */
1693   psym = add_psymbol_to_bcache (name, namelength, copy_name, domain, theclass,
1694                                 section, coreaddr, language, objfile, &added);
1695
1696   /* Do not duplicate global partial symbols.  */
1697   if (list == &objfile->global_psymbols
1698       && !added)
1699     return;
1700
1701   /* Save pointer to partial symbol in psymtab, growing symtab if needed.  */
1702   append_psymbol_to_list (list, psym, objfile);
1703 }
1704
1705 /* Initialize storage for partial symbols.  */
1706
1707 void
1708 init_psymbol_list (struct objfile *objfile, int total_symbols)
1709 {
1710   /* Free any previously allocated psymbol lists.  */
1711   objfile->global_psymbols.clear ();
1712   objfile->static_psymbols.clear ();
1713
1714   /* Current best guess is that approximately a twentieth
1715      of the total symbols (in a debugging file) are global or static
1716      oriented symbols, then multiply that by slop factor of two.  */
1717   objfile->global_psymbols.reserve (total_symbols / 10);
1718   objfile->static_psymbols.reserve (total_symbols / 10);
1719 }
1720
1721 struct partial_symtab *
1722 allocate_psymtab (const char *filename, struct objfile *objfile)
1723 {
1724   struct partial_symtab *psymtab;
1725
1726   if (objfile->free_psymtabs)
1727     {
1728       psymtab = objfile->free_psymtabs;
1729       objfile->free_psymtabs = psymtab->next;
1730     }
1731   else
1732     psymtab = XOBNEW (&objfile->objfile_obstack, partial_symtab);
1733
1734   memset (psymtab, 0, sizeof (struct partial_symtab));
1735   psymtab->filename
1736     = (const char *) bcache (filename, strlen (filename) + 1,
1737                              objfile->per_bfd->filename_cache);
1738   psymtab->compunit_symtab = NULL;
1739
1740   /* Prepend it to the psymtab list for the objfile it belongs to.
1741      Psymtabs are searched in most recent inserted -> least recent
1742      inserted order.  */
1743
1744   psymtab->next = objfile->psymtabs;
1745   objfile->psymtabs = psymtab;
1746
1747   if (symtab_create_debug)
1748     {
1749       /* Be a bit clever with debugging messages, and don't print objfile
1750          every time, only when it changes.  */
1751       static char *last_objfile_name = NULL;
1752
1753       if (last_objfile_name == NULL
1754           || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
1755         {
1756           xfree (last_objfile_name);
1757           last_objfile_name = xstrdup (objfile_name (objfile));
1758           fprintf_filtered (gdb_stdlog,
1759                             "Creating one or more psymtabs for objfile %s ...\n",
1760                             last_objfile_name);
1761         }
1762       fprintf_filtered (gdb_stdlog,
1763                         "Created psymtab %s for module %s.\n",
1764                         host_address_to_string (psymtab), filename);
1765     }
1766
1767   return psymtab;
1768 }
1769
1770 void
1771 discard_psymtab (struct objfile *objfile, struct partial_symtab *pst)
1772 {
1773   struct partial_symtab **prev_pst;
1774
1775   /* From dbxread.c:
1776      Empty psymtabs happen as a result of header files which don't
1777      have any symbols in them.  There can be a lot of them.  But this
1778      check is wrong, in that a psymtab with N_SLINE entries but
1779      nothing else is not empty, but we don't realize that.  Fixing
1780      that without slowing things down might be tricky.  */
1781
1782   /* First, snip it out of the psymtab chain.  */
1783
1784   prev_pst = &(objfile->psymtabs);
1785   while ((*prev_pst) != pst)
1786     prev_pst = &((*prev_pst)->next);
1787   (*prev_pst) = pst->next;
1788
1789   /* Next, put it on a free list for recycling.  */
1790
1791   pst->next = objfile->free_psymtabs;
1792   objfile->free_psymtabs = pst;
1793 }
1794
1795 \f
1796
1797 /* We need to pass a couple of items to the addrmap_foreach function,
1798    so use a struct.  */
1799
1800 struct dump_psymtab_addrmap_data
1801 {
1802   struct objfile *objfile;
1803   struct partial_symtab *psymtab;
1804   struct ui_file *outfile;
1805
1806   /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1807      If so, we want to print the next one as well (since the next addrmap
1808      entry defines the end of the range).  */
1809   int previous_matched;
1810 };
1811
1812 /* Helper function for dump_psymtab_addrmap to print an addrmap entry.  */
1813
1814 static int
1815 dump_psymtab_addrmap_1 (void *datap, CORE_ADDR start_addr, void *obj)
1816 {
1817   struct dump_psymtab_addrmap_data *data
1818     = (struct dump_psymtab_addrmap_data *) datap;
1819   struct gdbarch *gdbarch = get_objfile_arch (data->objfile);
1820   struct partial_symtab *addrmap_psymtab = (struct partial_symtab *) obj;
1821   const char *psymtab_address_or_end = NULL;
1822
1823   QUIT;
1824
1825   if (data->psymtab == NULL
1826       || data->psymtab == addrmap_psymtab)
1827     psymtab_address_or_end = host_address_to_string (addrmap_psymtab);
1828   else if (data->previous_matched)
1829     psymtab_address_or_end = "<ends here>";
1830
1831   if (data->psymtab == NULL
1832       || data->psymtab == addrmap_psymtab
1833       || data->previous_matched)
1834     {
1835       fprintf_filtered (data->outfile, "  %s%s %s\n",
1836                         data->psymtab != NULL ? "  " : "",
1837                         paddress (gdbarch, start_addr),
1838                         psymtab_address_or_end);
1839     }
1840
1841   data->previous_matched = (data->psymtab == NULL
1842                             || data->psymtab == addrmap_psymtab);
1843
1844   return 0;
1845 }
1846
1847 /* Helper function for maintenance_print_psymbols to print the addrmap
1848    of PSYMTAB.  If PSYMTAB is NULL print the entire addrmap.  */
1849
1850 static void
1851 dump_psymtab_addrmap (struct objfile *objfile, struct partial_symtab *psymtab,
1852                       struct ui_file *outfile)
1853 {
1854   struct dump_psymtab_addrmap_data addrmap_dump_data;
1855
1856   if ((psymtab == NULL
1857        || psymtab->psymtabs_addrmap_supported)
1858       && objfile->psymtabs_addrmap != NULL)
1859     {
1860       addrmap_dump_data.objfile = objfile;
1861       addrmap_dump_data.psymtab = psymtab;
1862       addrmap_dump_data.outfile = outfile;
1863       addrmap_dump_data.previous_matched = 0;
1864       fprintf_filtered (outfile, "%sddress map:\n",
1865                         psymtab == NULL ? "Entire a" : "  A");
1866       addrmap_foreach (objfile->psymtabs_addrmap, dump_psymtab_addrmap_1,
1867                        &addrmap_dump_data);
1868     }
1869 }
1870
1871 static void
1872 maintenance_print_psymbols (const char *args, int from_tty)
1873 {
1874   struct ui_file *outfile = gdb_stdout;
1875   char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1876   struct objfile *objfile;
1877   struct partial_symtab *ps;
1878   int i, outfile_idx, found;
1879   CORE_ADDR pc = 0;
1880   struct obj_section *section = NULL;
1881
1882   dont_repeat ();
1883
1884   gdb_argv argv (args);
1885
1886   for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1887     {
1888       if (strcmp (argv[i], "-pc") == 0)
1889         {
1890           if (argv[i + 1] == NULL)
1891             error (_("Missing pc value"));
1892           address_arg = argv[++i];
1893         }
1894       else if (strcmp (argv[i], "-source") == 0)
1895         {
1896           if (argv[i + 1] == NULL)
1897             error (_("Missing source file"));
1898           source_arg = argv[++i];
1899         }
1900       else if (strcmp (argv[i], "-objfile") == 0)
1901         {
1902           if (argv[i + 1] == NULL)
1903             error (_("Missing objfile name"));
1904           objfile_arg = argv[++i];
1905         }
1906       else if (strcmp (argv[i], "--") == 0)
1907         {
1908           /* End of options.  */
1909           ++i;
1910           break;
1911         }
1912       else if (argv[i][0] == '-')
1913         {
1914           /* Future proofing: Don't allow OUTFILE to begin with "-".  */
1915           error (_("Unknown option: %s"), argv[i]);
1916         }
1917       else
1918         break;
1919     }
1920   outfile_idx = i;
1921
1922   if (address_arg != NULL && source_arg != NULL)
1923     error (_("Must specify at most one of -pc and -source"));
1924
1925   stdio_file arg_outfile;
1926
1927   if (argv != NULL && argv[outfile_idx] != NULL)
1928     {
1929       if (argv[outfile_idx + 1] != NULL)
1930         error (_("Junk at end of command"));
1931       gdb::unique_xmalloc_ptr<char> outfile_name
1932         (tilde_expand (argv[outfile_idx]));
1933       if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1934         perror_with_name (outfile_name.get ());
1935       outfile = &arg_outfile;
1936     }
1937
1938   if (address_arg != NULL)
1939     {
1940       pc = parse_and_eval_address (address_arg);
1941       /* If we fail to find a section, that's ok, try the lookup anyway.  */
1942       section = find_pc_section (pc);
1943     }
1944
1945   found = 0;
1946   ALL_OBJFILES (objfile)
1947     {
1948       int printed_objfile_header = 0;
1949       int print_for_objfile = 1;
1950
1951       QUIT;
1952       if (objfile_arg != NULL)
1953         print_for_objfile
1954           = compare_filenames_for_search (objfile_name (objfile),
1955                                           objfile_arg);
1956       if (!print_for_objfile)
1957         continue;
1958
1959       if (address_arg != NULL)
1960         {
1961           struct bound_minimal_symbol msymbol = { NULL, NULL };
1962
1963           /* We don't assume each pc has a unique objfile (this is for
1964              debugging).  */
1965           ps = find_pc_sect_psymtab (objfile, pc, section, msymbol);
1966           if (ps != NULL)
1967             {
1968               if (!printed_objfile_header)
1969                 {
1970                   outfile->printf ("\nPartial symtabs for objfile %s\n",
1971                                   objfile_name (objfile));
1972                   printed_objfile_header = 1;
1973                 }
1974               dump_psymtab (objfile, ps, outfile);
1975               dump_psymtab_addrmap (objfile, ps, outfile);
1976               found = 1;
1977             }
1978         }
1979       else
1980         {
1981           ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1982             {
1983               int print_for_source = 0;
1984
1985               QUIT;
1986               if (source_arg != NULL)
1987                 {
1988                   print_for_source
1989                     = compare_filenames_for_search (ps->filename, source_arg);
1990                   found = 1;
1991                 }
1992               if (source_arg == NULL
1993                   || print_for_source)
1994                 {
1995                   if (!printed_objfile_header)
1996                     {
1997                       outfile->printf ("\nPartial symtabs for objfile %s\n",
1998                                        objfile_name (objfile));
1999                       printed_objfile_header = 1;
2000                     }
2001                   dump_psymtab (objfile, ps, outfile);
2002                   dump_psymtab_addrmap (objfile, ps, outfile);
2003                 }
2004             }
2005         }
2006
2007       /* If we're printing all the objfile's symbols dump the full addrmap.  */
2008
2009       if (address_arg == NULL
2010           && source_arg == NULL
2011           && objfile->psymtabs_addrmap != NULL)
2012         {
2013           outfile->puts ("\n");
2014           dump_psymtab_addrmap (objfile, NULL, outfile);
2015         }
2016     }
2017
2018   if (!found)
2019     {
2020       if (address_arg != NULL)
2021         error (_("No partial symtab for address: %s"), address_arg);
2022       if (source_arg != NULL)
2023         error (_("No partial symtab for source file: %s"), source_arg);
2024     }
2025 }
2026
2027 /* List all the partial symbol tables whose names match REGEXP (optional).  */
2028
2029 static void
2030 maintenance_info_psymtabs (const char *regexp, int from_tty)
2031 {
2032   struct program_space *pspace;
2033   struct objfile *objfile;
2034
2035   if (regexp)
2036     re_comp (regexp);
2037
2038   ALL_PSPACES (pspace)
2039     ALL_PSPACE_OBJFILES (pspace, objfile)
2040     {
2041       struct gdbarch *gdbarch = get_objfile_arch (objfile);
2042       struct partial_symtab *psymtab;
2043
2044       /* We don't want to print anything for this objfile until we
2045          actually find a symtab whose name matches.  */
2046       int printed_objfile_start = 0;
2047
2048       ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
2049         {
2050           QUIT;
2051
2052           if (! regexp
2053               || re_exec (psymtab->filename))
2054             {
2055               if (! printed_objfile_start)
2056                 {
2057                   printf_filtered ("{ objfile %s ", objfile_name (objfile));
2058                   wrap_here ("  ");
2059                   printf_filtered ("((struct objfile *) %s)\n",
2060                                    host_address_to_string (objfile));
2061                   printed_objfile_start = 1;
2062                 }
2063
2064               printf_filtered ("  { psymtab %s ", psymtab->filename);
2065               wrap_here ("    ");
2066               printf_filtered ("((struct partial_symtab *) %s)\n",
2067                                host_address_to_string (psymtab));
2068
2069               printf_filtered ("    readin %s\n",
2070                                psymtab->readin ? "yes" : "no");
2071               printf_filtered ("    fullname %s\n",
2072                                psymtab->fullname
2073                                ? psymtab->fullname : "(null)");
2074               printf_filtered ("    text addresses ");
2075               fputs_filtered (paddress (gdbarch, psymtab->text_low (objfile)),
2076                               gdb_stdout);
2077               printf_filtered (" -- ");
2078               fputs_filtered (paddress (gdbarch, psymtab->text_high (objfile)),
2079                               gdb_stdout);
2080               printf_filtered ("\n");
2081               printf_filtered ("    psymtabs_addrmap_supported %s\n",
2082                                (psymtab->psymtabs_addrmap_supported
2083                                 ? "yes" : "no"));
2084               printf_filtered ("    globals ");
2085               if (psymtab->n_global_syms)
2086                 {
2087                   auto p = &objfile->global_psymbols[psymtab->globals_offset];
2088
2089                   printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2090                                    host_address_to_string (p),
2091                                    psymtab->n_global_syms);
2092                 }
2093               else
2094                 printf_filtered ("(none)\n");
2095               printf_filtered ("    statics ");
2096               if (psymtab->n_static_syms)
2097                 {
2098                   auto p = &objfile->static_psymbols[psymtab->statics_offset];
2099
2100                   printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2101                                    host_address_to_string (p),
2102                                    psymtab->n_static_syms);
2103                 }
2104               else
2105                 printf_filtered ("(none)\n");
2106               printf_filtered ("    dependencies ");
2107               if (psymtab->number_of_dependencies)
2108                 {
2109                   int i;
2110
2111                   printf_filtered ("{\n");
2112                   for (i = 0; i < psymtab->number_of_dependencies; i++)
2113                     {
2114                       struct partial_symtab *dep = psymtab->dependencies[i];
2115
2116                       /* Note the string concatenation there --- no comma.  */
2117                       printf_filtered ("      psymtab %s "
2118                                        "((struct partial_symtab *) %s)\n",
2119                                        dep->filename,
2120                                        host_address_to_string (dep));
2121                     }
2122                   printf_filtered ("    }\n");
2123                 }
2124               else
2125                 printf_filtered ("(none)\n");
2126               printf_filtered ("  }\n");
2127             }
2128         }
2129
2130       if (printed_objfile_start)
2131         printf_filtered ("}\n");
2132     }
2133 }
2134
2135 /* Check consistency of currently expanded psymtabs vs symtabs.  */
2136
2137 static void
2138 maintenance_check_psymtabs (const char *ignore, int from_tty)
2139 {
2140   struct symbol *sym;
2141   struct compunit_symtab *cust = NULL;
2142   struct partial_symtab *ps;
2143   const struct blockvector *bv;
2144   struct objfile *objfile;
2145   struct block *b;
2146   int length;
2147
2148   ALL_PSYMTABS (objfile, ps)
2149   {
2150     struct gdbarch *gdbarch = get_objfile_arch (objfile);
2151
2152     /* We don't call psymtab_to_symtab here because that may cause symtab
2153        expansion.  When debugging a problem it helps if checkers leave
2154        things unchanged.  */
2155     cust = ps->compunit_symtab;
2156
2157     /* First do some checks that don't require the associated symtab.  */
2158     if (ps->text_high (objfile) < ps->text_low (objfile))
2159       {
2160         printf_filtered ("Psymtab ");
2161         puts_filtered (ps->filename);
2162         printf_filtered (" covers bad range ");
2163         fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2164                         gdb_stdout);
2165         printf_filtered (" - ");
2166         fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2167                         gdb_stdout);
2168         printf_filtered ("\n");
2169         continue;
2170       }
2171
2172     /* Now do checks requiring the associated symtab.  */
2173     if (cust == NULL)
2174       continue;
2175     bv = COMPUNIT_BLOCKVECTOR (cust);
2176     b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
2177     partial_symbol **psym = &objfile->static_psymbols[ps->statics_offset];
2178     length = ps->n_static_syms;
2179     while (length--)
2180       {
2181         sym = block_lookup_symbol (b, symbol_search_name (*psym),
2182                                    symbol_name_match_type::SEARCH_NAME,
2183                                    (*psym)->domain);
2184         if (!sym)
2185           {
2186             printf_filtered ("Static symbol `");
2187             puts_filtered ((*psym)->name);
2188             printf_filtered ("' only found in ");
2189             puts_filtered (ps->filename);
2190             printf_filtered (" psymtab\n");
2191           }
2192         psym++;
2193       }
2194     b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2195     psym = &objfile->global_psymbols[ps->globals_offset];
2196     length = ps->n_global_syms;
2197     while (length--)
2198       {
2199         sym = block_lookup_symbol (b, symbol_search_name (*psym),
2200                                    symbol_name_match_type::SEARCH_NAME,
2201                                    (*psym)->domain);
2202         if (!sym)
2203           {
2204             printf_filtered ("Global symbol `");
2205             puts_filtered ((*psym)->name);
2206             printf_filtered ("' only found in ");
2207             puts_filtered (ps->filename);
2208             printf_filtered (" psymtab\n");
2209           }
2210         psym++;
2211       }
2212     if (ps->raw_text_high () != 0
2213         && (ps->text_low (objfile) < BLOCK_START (b)
2214             || ps->text_high (objfile) > BLOCK_END (b)))
2215       {
2216         printf_filtered ("Psymtab ");
2217         puts_filtered (ps->filename);
2218         printf_filtered (" covers ");
2219         fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2220                         gdb_stdout);
2221         printf_filtered (" - ");
2222         fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2223                         gdb_stdout);
2224         printf_filtered (" but symtab covers only ");
2225         fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2226         printf_filtered (" - ");
2227         fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2228         printf_filtered ("\n");
2229       }
2230   }
2231 }
2232
2233 void
2234 _initialize_psymtab (void)
2235 {
2236   add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2237 Print dump of current partial symbol definitions.\n\
2238 Usage: mt print psymbols [-objfile objfile] [-pc address] [--] [outfile]\n\
2239        mt print psymbols [-objfile objfile] [-source source] [--] [outfile]\n\
2240 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2241 or the terminal if OUTFILE is unspecified.\n\
2242 If ADDRESS is provided, dump only the file for that address.\n\
2243 If SOURCE is provided, dump only that file's symbols.\n\
2244 If OBJFILE is provided, dump only that file's minimal symbols."),
2245            &maintenanceprintlist);
2246
2247   add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2248 List the partial symbol tables for all object files.\n\
2249 This does not include information about individual partial symbols,\n\
2250 just the symbol table structures themselves."),
2251            &maintenanceinfolist);
2252
2253   add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2254            _("\
2255 Check consistency of currently expanded psymtabs versus symtabs."),
2256            &maintenancelist);
2257 }