Fix a new warning on Cygwin
[binutils-gdb.git] / gdb / psymtab.c
1 /* Partial symbol tables.
2
3 Copyright (C) 2009-2022 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 "objfiles.h"
23 #include "psympriv.h"
24 #include "block.h"
25 #include "filenames.h"
26 #include "source.h"
27 #include "addrmap.h"
28 #include "gdbtypes.h"
29 #include "ui-out.h"
30 #include "command.h"
31 #include "readline/tilde.h"
32 #include "gdbsupport/gdb_regex.h"
33 #include "dictionary.h"
34 #include "language.h"
35 #include "cp-support.h"
36 #include "gdbcmd.h"
37 #include <algorithm>
38 #include <set>
39 #include "gdbsupport/buildargv.h"
40
41 static struct partial_symbol *lookup_partial_symbol (struct objfile *,
42 struct partial_symtab *,
43 const lookup_name_info &,
44 int,
45 domain_enum);
46
47 static const char *psymtab_to_fullname (struct partial_symtab *ps);
48
49 static struct partial_symbol *find_pc_sect_psymbol (struct objfile *,
50 struct partial_symtab *,
51 CORE_ADDR,
52 struct obj_section *);
53
54 static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
55 struct partial_symtab *pst);
56
57 psymtab_storage::~psymtab_storage ()
58 {
59 partial_symtab *iter = psymtabs;
60 while (iter != nullptr)
61 {
62 partial_symtab *next = iter->next;
63 delete iter;
64 iter = next;
65 }
66 }
67
68 /* See psymtab.h. */
69
70 void
71 psymtab_storage::install_psymtab (partial_symtab *pst)
72 {
73 pst->next = psymtabs;
74 psymtabs = pst;
75 }
76
77 \f
78
79 /* See psympriv.h. */
80
81 psymtab_storage::partial_symtab_range
82 psymbol_functions::partial_symbols (struct objfile *objfile)
83 {
84 gdb_assert ((objfile->flags & OBJF_PSYMTABS_READ) != 0);
85 return m_partial_symtabs->range ();
86 }
87
88 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
89 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
90
91 static struct partial_symtab *
92 find_pc_sect_psymtab_closer (struct objfile *objfile,
93 CORE_ADDR pc, struct obj_section *section,
94 struct partial_symtab *pst,
95 struct bound_minimal_symbol msymbol)
96 {
97 struct partial_symtab *tpst;
98 struct partial_symtab *best_pst = pst;
99 CORE_ADDR best_addr = pst->text_low (objfile);
100
101 gdb_assert (!pst->psymtabs_addrmap_supported);
102
103 /* An objfile that has its functions reordered might have
104 many partial symbol tables containing the PC, but
105 we want the partial symbol table that contains the
106 function containing the PC. */
107 if (!(objfile->flags & OBJF_REORDERED)
108 && section == NULL) /* Can't validate section this way. */
109 return pst;
110
111 if (msymbol.minsym == NULL)
112 return pst;
113
114 /* The code range of partial symtabs sometimes overlap, so, in
115 the loop below, we need to check all partial symtabs and
116 find the one that fits better for the given PC address. We
117 select the partial symtab that contains a symbol whose
118 address is closest to the PC address. By closest we mean
119 that find_pc_sect_symbol returns the symbol with address
120 that is closest and still less than the given PC. */
121 for (tpst = pst; tpst != NULL; tpst = tpst->next)
122 {
123 if (pc >= tpst->text_low (objfile) && pc < tpst->text_high (objfile))
124 {
125 struct partial_symbol *p;
126 CORE_ADDR this_addr;
127
128 /* NOTE: This assumes that every psymbol has a
129 corresponding msymbol, which is not necessarily
130 true; the debug info might be much richer than the
131 object's symbol table. */
132 p = find_pc_sect_psymbol (objfile, tpst, pc, section);
133 if (p != NULL
134 && (p->address (objfile) == msymbol.value_address ()))
135 return tpst;
136
137 /* Also accept the textlow value of a psymtab as a
138 "symbol", to provide some support for partial
139 symbol tables with line information but no debug
140 symbols (e.g. those produced by an assembler). */
141 if (p != NULL)
142 this_addr = p->address (objfile);
143 else
144 this_addr = tpst->text_low (objfile);
145
146 /* Check whether it is closer than our current
147 BEST_ADDR. Since this symbol address is
148 necessarily lower or equal to PC, the symbol closer
149 to PC is the symbol which address is the highest.
150 This way we return the psymtab which contains such
151 best match symbol. This can help in cases where the
152 symbol information/debuginfo is not complete, like
153 for instance on IRIX6 with gcc, where no debug info
154 is emitted for statics. (See also the nodebug.exp
155 testcase.) */
156 if (this_addr > best_addr)
157 {
158 best_addr = this_addr;
159 best_pst = tpst;
160 }
161 }
162 }
163 return best_pst;
164 }
165
166 /* See psympriv.h. */
167
168 struct partial_symtab *
169 psymbol_functions::find_pc_sect_psymtab (struct objfile *objfile,
170 CORE_ADDR pc,
171 struct obj_section *section,
172 struct bound_minimal_symbol msymbol)
173 {
174 /* Try just the PSYMTABS_ADDRMAP mapping first as it has better
175 granularity than the later used TEXTLOW/TEXTHIGH one. However, we need
176 to take care as the PSYMTABS_ADDRMAP can hold things other than partial
177 symtabs in some cases.
178
179 This function should only be called for objfiles that are using partial
180 symtabs, not for objfiles that are using indexes (.gdb_index or
181 .debug_names), however 'maintenance print psymbols' calls this function
182 directly for all objfiles. If we assume that PSYMTABS_ADDRMAP contains
183 partial symtabs then we will end up returning a pointer to an object
184 that is not a partial_symtab, which doesn't end well. */
185
186 if (m_partial_symtabs->psymtabs != NULL
187 && m_partial_symtabs->psymtabs_addrmap != NULL)
188 {
189 CORE_ADDR baseaddr = objfile->text_section_offset ();
190
191 struct partial_symtab *pst
192 = ((struct partial_symtab *)
193 addrmap_find (m_partial_symtabs->psymtabs_addrmap,
194 pc - baseaddr));
195 if (pst != NULL)
196 {
197 /* FIXME: addrmaps currently do not handle overlayed sections,
198 so fall back to the non-addrmap case if we're debugging
199 overlays and the addrmap returned the wrong section. */
200 if (overlay_debugging && msymbol.minsym != NULL && section != NULL)
201 {
202 struct partial_symbol *p;
203
204 /* NOTE: This assumes that every psymbol has a
205 corresponding msymbol, which is not necessarily
206 true; the debug info might be much richer than the
207 object's symbol table. */
208 p = find_pc_sect_psymbol (objfile, pst, pc, section);
209 if (p == NULL
210 || (p->address (objfile)
211 != msymbol.value_address ()))
212 goto next;
213 }
214
215 /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
216 PSYMTABS_ADDRMAP we used has already the best 1-byte
217 granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
218 a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
219 overlap. */
220
221 return pst;
222 }
223 }
224
225 next:
226
227 /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
228 which still have no corresponding full SYMTABs read. But it is not
229 present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
230 so far. */
231
232 /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
233 its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
234 debug info type in single OBJFILE. */
235
236 for (partial_symtab *pst : partial_symbols (objfile))
237 if (!pst->psymtabs_addrmap_supported
238 && pc >= pst->text_low (objfile) && pc < pst->text_high (objfile))
239 {
240 struct partial_symtab *best_pst;
241
242 best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
243 msymbol);
244 if (best_pst != NULL)
245 return best_pst;
246 }
247
248 return NULL;
249 }
250
251 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
252 the definition of quick_symbol_functions in symfile.h. */
253
254 struct compunit_symtab *
255 psymbol_functions::find_pc_sect_compunit_symtab
256 (struct objfile *objfile,
257 struct bound_minimal_symbol msymbol,
258 CORE_ADDR pc,
259 struct obj_section *section,
260 int warn_if_readin)
261 {
262 struct partial_symtab *ps = find_pc_sect_psymtab (objfile,
263 pc, section,
264 msymbol);
265 if (ps != NULL)
266 {
267 if (warn_if_readin && ps->readin_p (objfile))
268 /* Might want to error() here (in case symtab is corrupt and
269 will cause a core dump), but maybe we can successfully
270 continue, so let's not. */
271 warning (_("\
272 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
273 paddress (objfile->arch (), pc));
274 psymtab_to_symtab (objfile, ps);
275 return ps->get_compunit_symtab (objfile);
276 }
277 return NULL;
278 }
279
280 /* Find which partial symbol within a psymtab matches PC and SECTION.
281 Return NULL if none. */
282
283 static struct partial_symbol *
284 find_pc_sect_psymbol (struct objfile *objfile,
285 struct partial_symtab *psymtab, CORE_ADDR pc,
286 struct obj_section *section)
287 {
288 struct partial_symbol *best = NULL;
289 CORE_ADDR best_pc;
290 const CORE_ADDR textlow = psymtab->text_low (objfile);
291
292 gdb_assert (psymtab != NULL);
293
294 /* Cope with programs that start at address 0. */
295 best_pc = (textlow != 0) ? textlow - 1 : 0;
296
297 /* Search the global symbols as well as the static symbols, so that
298 find_pc_partial_function doesn't use a minimal symbol and thus
299 cache a bad endaddr. */
300 for (partial_symbol *p : psymtab->global_psymbols)
301 {
302 if (p->domain == VAR_DOMAIN
303 && p->aclass == LOC_BLOCK
304 && pc >= p->address (objfile)
305 && (p->address (objfile) > best_pc
306 || (psymtab->text_low (objfile) == 0
307 && best_pc == 0 && p->address (objfile) == 0)))
308 {
309 if (section != NULL) /* Match on a specific section. */
310 {
311 if (!matching_obj_sections (p->obj_section (objfile),
312 section))
313 continue;
314 }
315 best_pc = p->address (objfile);
316 best = p;
317 }
318 }
319
320 for (partial_symbol *p : psymtab->static_psymbols)
321 {
322 if (p->domain == VAR_DOMAIN
323 && p->aclass == LOC_BLOCK
324 && pc >= p->address (objfile)
325 && (p->address (objfile) > best_pc
326 || (psymtab->text_low (objfile) == 0
327 && best_pc == 0 && p->address (objfile) == 0)))
328 {
329 if (section != NULL) /* Match on a specific section. */
330 {
331 if (!matching_obj_sections (p->obj_section (objfile),
332 section))
333 continue;
334 }
335 best_pc = p->address (objfile);
336 best = p;
337 }
338 }
339
340 return best;
341 }
342
343 /* Psymtab version of lookup_global_symbol_language. See its definition in
344 the definition of quick_symbol_functions in symfile.h. */
345
346 enum language
347 psymbol_functions::lookup_global_symbol_language (struct objfile *objfile,
348 const char *name,
349 domain_enum domain,
350 bool *symbol_found_p)
351 {
352 *symbol_found_p = false;
353 if (objfile->sf == NULL)
354 return language_unknown;
355
356 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
357
358 for (partial_symtab *ps : partial_symbols (objfile))
359 {
360 struct partial_symbol *psym;
361 if (ps->readin_p (objfile))
362 continue;
363
364 psym = lookup_partial_symbol (objfile, ps, lookup_name, 1, domain);
365 if (psym)
366 {
367 *symbol_found_p = true;
368 return psym->ginfo.language ();
369 }
370 }
371
372 return language_unknown;
373 }
374
375 /* Returns true if PSYM matches LOOKUP_NAME. */
376
377 static bool
378 psymbol_name_matches (partial_symbol *psym,
379 const lookup_name_info &lookup_name)
380 {
381 const language_defn *lang = language_def (psym->ginfo.language ());
382 symbol_name_matcher_ftype *name_match
383 = lang->get_symbol_name_matcher (lookup_name);
384 return name_match (psym->ginfo.search_name (), lookup_name, NULL);
385 }
386
387 /* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
388 the global block of PST if GLOBAL, and otherwise the static block.
389 MATCH is the comparison operation that returns true iff MATCH (s,
390 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
391 non-null, the symbols in the block are assumed to be ordered
392 according to it (allowing binary search). It must be compatible
393 with MATCH. Returns the symbol, if found, and otherwise NULL. */
394
395 static struct partial_symbol *
396 match_partial_symbol (struct objfile *objfile,
397 struct partial_symtab *pst, int global,
398 const lookup_name_info &name, domain_enum domain,
399 symbol_compare_ftype *ordered_compare)
400 {
401 struct partial_symbol **start, **psym;
402 struct partial_symbol **top, **real_top, **bottom, **center;
403 int length = (global
404 ? pst->global_psymbols.size ()
405 : pst->static_psymbols.size ());
406 int do_linear_search = 1;
407
408 if (length == 0)
409 return NULL;
410
411 start = (global ?
412 &pst->global_psymbols[0] :
413 &pst->static_psymbols[0]);
414
415 if (global && ordered_compare) /* Can use a binary search. */
416 {
417 do_linear_search = 0;
418
419 /* Binary search. This search is guaranteed to end with center
420 pointing at the earliest partial symbol whose name might be
421 correct. At that point *all* partial symbols with an
422 appropriate name will be checked against the correct
423 domain. */
424
425 bottom = start;
426 top = start + length - 1;
427 real_top = top;
428 while (top > bottom)
429 {
430 center = bottom + (top - bottom) / 2;
431 gdb_assert (center < top);
432
433 enum language lang = (*center)->ginfo.language ();
434 const char *lang_ln = name.language_lookup_name (lang);
435
436 if (ordered_compare ((*center)->ginfo.search_name (),
437 lang_ln) >= 0)
438 top = center;
439 else
440 bottom = center + 1;
441 }
442 gdb_assert (top == bottom);
443
444 while (top <= real_top
445 && psymbol_name_matches (*top, name))
446 {
447 if (symbol_matches_domain ((*top)->ginfo.language (),
448 (*top)->domain, domain))
449 return *top;
450 top++;
451 }
452 }
453
454 /* Can't use a binary search or else we found during the binary search that
455 we should also do a linear search. */
456
457 if (do_linear_search)
458 {
459 for (psym = start; psym < start + length; psym++)
460 {
461 if (symbol_matches_domain ((*psym)->ginfo.language (),
462 (*psym)->domain, domain)
463 && psymbol_name_matches (*psym, name))
464 return *psym;
465 }
466 }
467
468 return NULL;
469 }
470
471 /* Look, in partial_symtab PST, for symbol whose natural name is
472 LOOKUP_NAME. Check the global symbols if GLOBAL, the static
473 symbols if not. */
474
475 static struct partial_symbol *
476 lookup_partial_symbol (struct objfile *objfile,
477 struct partial_symtab *pst,
478 const lookup_name_info &lookup_name,
479 int global, domain_enum domain)
480 {
481 struct partial_symbol **start, **psym;
482 struct partial_symbol **top, **real_top, **bottom, **center;
483 int length = (global
484 ? pst->global_psymbols.size ()
485 : pst->static_psymbols.size ());
486 int do_linear_search = 1;
487
488 if (length == 0)
489 return NULL;
490
491 start = (global ?
492 &pst->global_psymbols[0] :
493 &pst->static_psymbols[0]);
494
495 if (global) /* This means we can use a binary search. */
496 {
497 do_linear_search = 0;
498
499 /* Binary search. This search is guaranteed to end with center
500 pointing at the earliest partial symbol whose name might be
501 correct. At that point *all* partial symbols with an
502 appropriate name will be checked against the correct
503 domain. */
504
505 bottom = start;
506 top = start + length - 1;
507 real_top = top;
508 while (top > bottom)
509 {
510 center = bottom + (top - bottom) / 2;
511
512 gdb_assert (center < top);
513
514 if (strcmp_iw_ordered ((*center)->ginfo.search_name (),
515 lookup_name.c_str ()) >= 0)
516 {
517 top = center;
518 }
519 else
520 {
521 bottom = center + 1;
522 }
523 }
524
525 gdb_assert (top == bottom);
526
527 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
528 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
529 while (top >= start && symbol_matches_search_name (&(*top)->ginfo,
530 lookup_name))
531 top--;
532
533 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
534 top++;
535
536 while (top <= real_top && symbol_matches_search_name (&(*top)->ginfo,
537 lookup_name))
538 {
539 if (symbol_matches_domain ((*top)->ginfo.language (),
540 (*top)->domain, domain))
541 return *top;
542 top++;
543 }
544 }
545
546 /* Can't use a binary search or else we found during the binary search that
547 we should also do a linear search. */
548
549 if (do_linear_search)
550 {
551 for (psym = start; psym < start + length; psym++)
552 {
553 if (symbol_matches_domain ((*psym)->ginfo.language (),
554 (*psym)->domain, domain)
555 && symbol_matches_search_name (&(*psym)->ginfo, lookup_name))
556 return *psym;
557 }
558 }
559
560 return NULL;
561 }
562
563 /* Get the symbol table that corresponds to a partial_symtab.
564 This is fast after the first time you do it.
565 The result will be NULL if the primary symtab has no symbols,
566 which can happen. Otherwise the result is the primary symtab
567 that contains PST. */
568
569 static struct compunit_symtab *
570 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
571 {
572 /* If it is a shared psymtab, find an unshared psymtab that includes
573 it. Any such psymtab will do. */
574 while (pst->user != NULL)
575 pst = pst->user;
576
577 /* If it's been looked up before, return it. */
578 if (pst->get_compunit_symtab (objfile))
579 return pst->get_compunit_symtab (objfile);
580
581 /* If it has not yet been read in, read it. */
582 if (!pst->readin_p (objfile))
583 {
584 scoped_restore decrementer = increment_reading_symtab ();
585
586 if (info_verbose)
587 {
588 gdb_printf (_("Reading in symbols for %s...\n"),
589 pst->filename);
590 gdb_flush (gdb_stdout);
591 }
592
593 pst->read_symtab (objfile);
594 }
595
596 return pst->get_compunit_symtab (objfile);
597 }
598
599 /* Psymtab version of find_last_source_symtab. See its definition in
600 the definition of quick_symbol_functions in symfile.h. */
601
602 struct symtab *
603 psymbol_functions::find_last_source_symtab (struct objfile *ofp)
604 {
605 struct partial_symtab *cs_pst = NULL;
606
607 for (partial_symtab *ps : partial_symbols (ofp))
608 {
609 const char *name = ps->filename;
610 int len = strlen (name);
611
612 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
613 || strcmp (name, "<<C++-namespaces>>") == 0)))
614 cs_pst = ps;
615 }
616
617 if (cs_pst)
618 {
619 if (cs_pst->readin_p (ofp))
620 {
621 internal_error (__FILE__, __LINE__,
622 _("select_source_symtab: "
623 "readin pst found and no symtabs."));
624 }
625 else
626 {
627 struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
628
629 if (cust == NULL)
630 return NULL;
631 return cust->primary_filetab ();
632 }
633 }
634 return NULL;
635 }
636
637 /* Psymtab version of forget_cached_source_info. See its definition in
638 the definition of quick_symbol_functions in symfile.h. */
639
640 void
641 psymbol_functions::forget_cached_source_info (struct objfile *objfile)
642 {
643 for (partial_symtab *pst : partial_symbols (objfile))
644 {
645 if (pst->fullname != NULL)
646 {
647 xfree (pst->fullname);
648 pst->fullname = NULL;
649 }
650 }
651 }
652
653 static void
654 print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
655 const std::vector<partial_symbol *> &symbols,
656 const char *what, struct ui_file *outfile)
657 {
658 gdb_printf (outfile, " %s partial symbols:\n", what);
659 for (partial_symbol *p : symbols)
660 {
661 QUIT;
662 gdb_printf (outfile, " `%s'", p->ginfo.linkage_name ());
663 if (p->ginfo.demangled_name () != NULL)
664 {
665 gdb_printf (outfile, " `%s'",
666 p->ginfo.demangled_name ());
667 }
668 gdb_puts (", ", outfile);
669 switch (p->domain)
670 {
671 case UNDEF_DOMAIN:
672 gdb_puts ("undefined domain, ", outfile);
673 break;
674 case VAR_DOMAIN:
675 /* This is the usual thing -- don't print it. */
676 break;
677 case STRUCT_DOMAIN:
678 gdb_puts ("struct domain, ", outfile);
679 break;
680 case MODULE_DOMAIN:
681 gdb_puts ("module domain, ", outfile);
682 break;
683 case LABEL_DOMAIN:
684 gdb_puts ("label domain, ", outfile);
685 break;
686 case COMMON_BLOCK_DOMAIN:
687 gdb_puts ("common block domain, ", outfile);
688 break;
689 default:
690 gdb_puts ("<invalid domain>, ", outfile);
691 break;
692 }
693 switch (p->aclass)
694 {
695 case LOC_UNDEF:
696 gdb_puts ("undefined", outfile);
697 break;
698 case LOC_CONST:
699 gdb_puts ("constant int", outfile);
700 break;
701 case LOC_STATIC:
702 gdb_puts ("static", outfile);
703 break;
704 case LOC_REGISTER:
705 gdb_puts ("register", outfile);
706 break;
707 case LOC_ARG:
708 gdb_puts ("pass by value", outfile);
709 break;
710 case LOC_REF_ARG:
711 gdb_puts ("pass by reference", outfile);
712 break;
713 case LOC_REGPARM_ADDR:
714 gdb_puts ("register address parameter", outfile);
715 break;
716 case LOC_LOCAL:
717 gdb_puts ("stack parameter", outfile);
718 break;
719 case LOC_TYPEDEF:
720 gdb_puts ("type", outfile);
721 break;
722 case LOC_LABEL:
723 gdb_puts ("label", outfile);
724 break;
725 case LOC_BLOCK:
726 gdb_puts ("function", outfile);
727 break;
728 case LOC_CONST_BYTES:
729 gdb_puts ("constant bytes", outfile);
730 break;
731 case LOC_UNRESOLVED:
732 gdb_puts ("unresolved", outfile);
733 break;
734 case LOC_OPTIMIZED_OUT:
735 gdb_puts ("optimized out", outfile);
736 break;
737 case LOC_COMPUTED:
738 gdb_puts ("computed at runtime", outfile);
739 break;
740 default:
741 gdb_puts ("<invalid location>", outfile);
742 break;
743 }
744 gdb_puts (", ", outfile);
745 gdb_puts (paddress (gdbarch, p->unrelocated_address ()), outfile);
746 gdb_printf (outfile, "\n");
747 }
748 }
749
750 static void
751 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
752 struct ui_file *outfile)
753 {
754 struct gdbarch *gdbarch = objfile->arch ();
755 int i;
756
757 if (psymtab->anonymous)
758 {
759 gdb_printf (outfile, "\nAnonymous partial symtab (%s) ",
760 psymtab->filename);
761 }
762 else
763 {
764 gdb_printf (outfile, "\nPartial symtab for source file %s ",
765 psymtab->filename);
766 }
767 gdb_printf (outfile, "(object %s)\n\n",
768 host_address_to_string (psymtab));
769 gdb_printf (outfile, " Read from object file %s (%s)\n",
770 objfile_name (objfile),
771 host_address_to_string (objfile));
772
773 if (psymtab->readin_p (objfile))
774 gdb_printf
775 (outfile,
776 " Full symtab was read (at %s)\n",
777 host_address_to_string (psymtab->get_compunit_symtab (objfile)));
778
779 gdb_printf (outfile, " Symbols cover text addresses ");
780 gdb_puts (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
781 gdb_printf (outfile, "-");
782 gdb_puts (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
783 gdb_printf (outfile, "\n");
784 gdb_printf (outfile, " Address map supported - %s.\n",
785 psymtab->psymtabs_addrmap_supported ? "yes" : "no");
786 gdb_printf (outfile, " Depends on %d other partial symtabs.\n",
787 psymtab->number_of_dependencies);
788 for (i = 0; i < psymtab->number_of_dependencies; i++)
789 gdb_printf (outfile, " %d %s\n", i,
790 host_address_to_string (psymtab->dependencies[i]));
791 if (psymtab->user != NULL)
792 gdb_printf (outfile, " Shared partial symtab with user %s\n",
793 host_address_to_string (psymtab->user));
794 if (!psymtab->global_psymbols.empty ())
795 {
796 print_partial_symbols
797 (gdbarch, objfile, psymtab->global_psymbols,
798 "Global", outfile);
799 }
800 if (!psymtab->static_psymbols.empty ())
801 {
802 print_partial_symbols
803 (gdbarch, objfile, psymtab->static_psymbols,
804 "Static", outfile);
805 }
806 gdb_printf (outfile, "\n");
807 }
808
809 /* Count the number of partial symbols in OBJFILE. */
810
811 int
812 psymbol_functions::count_psyms ()
813 {
814 int count = 0;
815 for (partial_symtab *pst : m_partial_symtabs->range ())
816 {
817 count += pst->global_psymbols.size ();
818 count += pst->static_psymbols.size ();
819 }
820 return count;
821 }
822
823 /* Psymtab version of print_stats. See its definition in
824 the definition of quick_symbol_functions in symfile.h. */
825
826 void
827 psymbol_functions::print_stats (struct objfile *objfile, bool print_bcache)
828 {
829 int i;
830
831 if (!print_bcache)
832 {
833 int n_psyms = count_psyms ();
834 if (n_psyms > 0)
835 gdb_printf (_(" Number of \"partial\" symbols read: %d\n"),
836 n_psyms);
837
838 i = 0;
839 for (partial_symtab *ps : partial_symbols (objfile))
840 {
841 if (!ps->readin_p (objfile))
842 i++;
843 }
844 gdb_printf (_(" Number of psym tables (not yet expanded): %d\n"),
845 i);
846 gdb_printf (_(" Total memory used for psymbol cache: %d\n"),
847 m_partial_symtabs->psymbol_cache.memory_used ());
848 }
849 else
850 {
851 gdb_printf (_("Psymbol byte cache statistics:\n"));
852 m_partial_symtabs->psymbol_cache.print_statistics
853 ("partial symbol cache");
854 }
855 }
856
857 /* Psymtab version of dump. See its definition in
858 the definition of quick_symbol_functions in symfile.h. */
859
860 void
861 psymbol_functions::dump (struct objfile *objfile)
862 {
863 struct partial_symtab *psymtab;
864
865 if (m_partial_symtabs->psymtabs)
866 {
867 gdb_printf ("Psymtabs:\n");
868 for (psymtab = m_partial_symtabs->psymtabs;
869 psymtab != NULL;
870 psymtab = psymtab->next)
871 gdb_printf ("%s at %s\n",
872 psymtab->filename,
873 host_address_to_string (psymtab));
874 gdb_printf ("\n\n");
875 }
876 }
877
878 /* Psymtab version of expand_all_symtabs. See its definition in
879 the definition of quick_symbol_functions in symfile.h. */
880
881 void
882 psymbol_functions::expand_all_symtabs (struct objfile *objfile)
883 {
884 for (partial_symtab *psymtab : partial_symbols (objfile))
885 psymtab_to_symtab (objfile, psymtab);
886 }
887
888 /* Psymtab version of map_symbol_filenames. See its definition in
889 the definition of quick_symbol_functions in symfile.h. */
890
891 void
892 psymbol_functions::map_symbol_filenames
893 (struct objfile *objfile,
894 gdb::function_view<symbol_filename_ftype> fun,
895 bool need_fullname)
896 {
897 for (partial_symtab *ps : partial_symbols (objfile))
898 {
899 const char *fullname;
900
901 if (ps->readin_p (objfile))
902 continue;
903
904 /* We can skip shared psymtabs here, because any file name will be
905 attached to the unshared psymtab. */
906 if (ps->user != NULL)
907 continue;
908
909 /* Anonymous psymtabs don't have a file name. */
910 if (ps->anonymous)
911 continue;
912
913 QUIT;
914 if (need_fullname)
915 fullname = psymtab_to_fullname (ps);
916 else
917 fullname = NULL;
918 fun (ps->filename, fullname);
919 }
920 }
921
922 /* Finds the fullname that a partial_symtab represents.
923
924 If this functions finds the fullname, it will save it in ps->fullname
925 and it will also return the value.
926
927 If this function fails to find the file that this partial_symtab represents,
928 NULL will be returned and ps->fullname will be set to NULL. */
929
930 static const char *
931 psymtab_to_fullname (struct partial_symtab *ps)
932 {
933 gdb_assert (!ps->anonymous);
934
935 /* Use cached copy if we have it.
936 We rely on forget_cached_source_info being called appropriately
937 to handle cases like the file being moved. */
938 if (ps->fullname == NULL)
939 {
940 gdb::unique_xmalloc_ptr<char> fullname
941 = find_source_or_rewrite (ps->filename, ps->dirname);
942 ps->fullname = fullname.release ();
943 }
944
945 return ps->fullname;
946 }
947
948 /* Psymtab version of expand_matching_symbols. See its definition in
949 the definition of quick_symbol_functions in symfile.h. */
950
951 void
952 psymbol_functions::expand_matching_symbols
953 (struct objfile *objfile,
954 const lookup_name_info &name, domain_enum domain,
955 int global,
956 symbol_compare_ftype *ordered_compare)
957 {
958 for (partial_symtab *ps : partial_symbols (objfile))
959 {
960 QUIT;
961 if (!ps->readin_p (objfile)
962 && match_partial_symbol (objfile, ps, global, name, domain,
963 ordered_compare))
964 psymtab_to_symtab (objfile, ps);
965 }
966 }
967
968 /* A helper for psym_expand_symtabs_matching that handles searching
969 included psymtabs. This returns true if a symbol is found, and
970 false otherwise. It also updates the 'searched_flag' on the
971 various psymtabs that it searches. */
972
973 static bool
974 recursively_search_psymtabs
975 (struct partial_symtab *ps,
976 struct objfile *objfile,
977 block_search_flags search_flags,
978 domain_enum domain,
979 enum search_domain search,
980 const lookup_name_info &lookup_name,
981 gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
982 {
983 int keep_going = 1;
984 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
985 int i;
986
987 if (ps->searched_flag != PST_NOT_SEARCHED)
988 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
989
990 /* Recurse into shared psymtabs first, because they may have already
991 been searched, and this could save some time. */
992 for (i = 0; i < ps->number_of_dependencies; ++i)
993 {
994 int r;
995
996 /* Skip non-shared dependencies, these are handled elsewhere. */
997 if (ps->dependencies[i]->user == NULL)
998 continue;
999
1000 r = recursively_search_psymtabs (ps->dependencies[i],
1001 objfile, search_flags, domain, search,
1002 lookup_name, sym_matcher);
1003 if (r != 0)
1004 {
1005 ps->searched_flag = PST_SEARCHED_AND_FOUND;
1006 return true;
1007 }
1008 }
1009
1010 partial_symbol **gbound = (ps->global_psymbols.data ()
1011 + ps->global_psymbols.size ());
1012 partial_symbol **sbound = (ps->static_psymbols.data ()
1013 + ps->static_psymbols.size ());
1014 partial_symbol **bound = gbound;
1015
1016 /* Go through all of the symbols stored in a partial
1017 symtab in one loop. */
1018 partial_symbol **psym = ps->global_psymbols.data ();
1019
1020 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
1021 {
1022 if (ps->static_psymbols.empty ())
1023 keep_going = 0;
1024 else
1025 {
1026 psym = ps->static_psymbols.data ();
1027 bound = sbound;
1028 }
1029 }
1030
1031 while (keep_going)
1032 {
1033 if (psym >= bound)
1034 {
1035 if (bound == gbound && !ps->static_psymbols.empty ()
1036 && (search_flags & SEARCH_STATIC_BLOCK) != 0)
1037 {
1038 psym = ps->static_psymbols.data ();
1039 bound = sbound;
1040 }
1041 else
1042 keep_going = 0;
1043 continue;
1044 }
1045 else
1046 {
1047 QUIT;
1048
1049 if ((domain == UNDEF_DOMAIN
1050 || symbol_matches_domain ((*psym)->ginfo.language (),
1051 (*psym)->domain, domain))
1052 && (search == ALL_DOMAIN
1053 || (search == MODULES_DOMAIN
1054 && (*psym)->domain == MODULE_DOMAIN)
1055 || (search == VARIABLES_DOMAIN
1056 && (*psym)->aclass != LOC_TYPEDEF
1057 && (*psym)->aclass != LOC_BLOCK)
1058 || (search == FUNCTIONS_DOMAIN
1059 && (*psym)->aclass == LOC_BLOCK)
1060 || (search == TYPES_DOMAIN
1061 && (*psym)->aclass == LOC_TYPEDEF))
1062 && psymbol_name_matches (*psym, lookup_name)
1063 && (sym_matcher == NULL
1064 || sym_matcher ((*psym)->ginfo.search_name ())))
1065 {
1066 /* Found a match, so notify our caller. */
1067 result = PST_SEARCHED_AND_FOUND;
1068 keep_going = 0;
1069 }
1070 }
1071 psym++;
1072 }
1073
1074 ps->searched_flag = result;
1075 return result == PST_SEARCHED_AND_FOUND;
1076 }
1077
1078 /* Psymtab version of expand_symtabs_matching. See its definition in
1079 the definition of quick_symbol_functions in symfile.h. */
1080
1081 bool
1082 psymbol_functions::expand_symtabs_matching
1083 (struct objfile *objfile,
1084 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1085 const lookup_name_info *lookup_name,
1086 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1087 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1088 block_search_flags search_flags,
1089 domain_enum domain,
1090 enum search_domain search)
1091 {
1092 /* Clear the search flags. */
1093 for (partial_symtab *ps : partial_symbols (objfile))
1094 ps->searched_flag = PST_NOT_SEARCHED;
1095
1096 gdb::optional<lookup_name_info> psym_lookup_name;
1097 if (lookup_name != nullptr)
1098 psym_lookup_name = lookup_name->make_ignore_params ();
1099
1100 /* This invariant is documented in quick-functions.h. */
1101 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
1102
1103 for (partial_symtab *ps : m_partial_symtabs->range ())
1104 {
1105 QUIT;
1106
1107 if (ps->readin_p (objfile))
1108 continue;
1109
1110 if (file_matcher)
1111 {
1112 bool match;
1113
1114 if (ps->anonymous)
1115 continue;
1116
1117 match = file_matcher (ps->filename, false);
1118 if (!match)
1119 {
1120 /* Before we invoke realpath, which can get expensive when many
1121 files are involved, do a quick comparison of the basenames. */
1122 if (basenames_may_differ
1123 || file_matcher (lbasename (ps->filename), true))
1124 match = file_matcher (psymtab_to_fullname (ps), false);
1125 }
1126 if (!match)
1127 continue;
1128 }
1129
1130 if (lookup_name == nullptr
1131 || recursively_search_psymtabs (ps, objfile, search_flags,
1132 domain, search,
1133 *psym_lookup_name,
1134 symbol_matcher))
1135 {
1136 compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
1137
1138 if (cust != nullptr && expansion_notify != nullptr)
1139 if (!expansion_notify (cust))
1140 return false;
1141 }
1142 }
1143
1144 return true;
1145 }
1146
1147 /* Psymtab version of has_symbols. See its definition in
1148 the definition of quick_symbol_functions in symfile.h. */
1149
1150 bool
1151 psymbol_functions::has_symbols (struct objfile *objfile)
1152 {
1153 return m_partial_symtabs->psymtabs != NULL;
1154 }
1155
1156 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
1157
1158 bool
1159 psymbol_functions::has_unexpanded_symtabs (struct objfile *objfile)
1160 {
1161 for (partial_symtab *psymtab : partial_symbols (objfile))
1162 {
1163 /* Is this already expanded? */
1164 if (psymtab->readin_p (objfile))
1165 continue;
1166
1167 /* It has not yet been expanded. */
1168 return true;
1169 }
1170
1171 return false;
1172 }
1173
1174 /* Helper function for psym_find_compunit_symtab_by_address that fills
1175 in m_psymbol_map for a given range of psymbols. */
1176
1177 void
1178 psymbol_functions::fill_psymbol_map
1179 (struct objfile *objfile,
1180 struct partial_symtab *psymtab,
1181 std::set<CORE_ADDR> *seen_addrs,
1182 const std::vector<partial_symbol *> &symbols)
1183 {
1184 for (partial_symbol *psym : symbols)
1185 {
1186 if (psym->aclass == LOC_STATIC)
1187 {
1188 CORE_ADDR addr = psym->address (objfile);
1189 if (seen_addrs->find (addr) == seen_addrs->end ())
1190 {
1191 seen_addrs->insert (addr);
1192 m_psymbol_map.emplace_back (addr, psymtab);
1193 }
1194 }
1195 }
1196 }
1197
1198 /* See find_compunit_symtab_by_address in quick_symbol_functions, in
1199 symfile.h. */
1200
1201 compunit_symtab *
1202 psymbol_functions::find_compunit_symtab_by_address (struct objfile *objfile,
1203 CORE_ADDR address)
1204 {
1205 if (m_psymbol_map.empty ())
1206 {
1207 std::set<CORE_ADDR> seen_addrs;
1208
1209 for (partial_symtab *pst : partial_symbols (objfile))
1210 {
1211 fill_psymbol_map (objfile, pst,
1212 &seen_addrs,
1213 pst->global_psymbols);
1214 fill_psymbol_map (objfile, pst,
1215 &seen_addrs,
1216 pst->static_psymbols);
1217 }
1218
1219 m_psymbol_map.shrink_to_fit ();
1220
1221 std::sort (m_psymbol_map.begin (), m_psymbol_map.end (),
1222 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1223 const std::pair<CORE_ADDR, partial_symtab *> &b)
1224 {
1225 return a.first < b.first;
1226 });
1227 }
1228
1229 auto iter = std::lower_bound
1230 (m_psymbol_map.begin (), m_psymbol_map.end (), address,
1231 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1232 CORE_ADDR b)
1233 {
1234 return a.first < b;
1235 });
1236
1237 if (iter == m_psymbol_map.end () || iter->first != address)
1238 return NULL;
1239
1240 return psymtab_to_symtab (objfile, iter->second);
1241 }
1242
1243 \f
1244
1245 /* Partially fill a partial symtab. It will be completely filled at
1246 the end of the symbol list. */
1247
1248 partial_symtab::partial_symtab (const char *filename,
1249 psymtab_storage *partial_symtabs,
1250 objfile_per_bfd_storage *objfile_per_bfd,
1251 CORE_ADDR textlow)
1252 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
1253 {
1254 set_text_low (textlow);
1255 set_text_high (raw_text_low ()); /* default */
1256 }
1257
1258 /* Perform "finishing up" operations of a partial symtab. */
1259
1260 void
1261 partial_symtab::end ()
1262 {
1263 global_psymbols.shrink_to_fit ();
1264 static_psymbols.shrink_to_fit ();
1265
1266 /* Sort the global list; don't sort the static list. */
1267 std::sort (global_psymbols.begin (),
1268 global_psymbols.end (),
1269 [] (partial_symbol *s1, partial_symbol *s2)
1270 {
1271 return strcmp_iw_ordered (s1->ginfo.search_name (),
1272 s2->ginfo.search_name ()) < 0;
1273 });
1274 }
1275
1276 /* See psymtab.h. */
1277
1278 unsigned long
1279 psymbol_bcache::hash (const void *addr, int length)
1280 {
1281 unsigned long h = 0;
1282 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1283 unsigned int lang = psymbol->ginfo.language ();
1284 unsigned int domain = psymbol->domain;
1285 unsigned int theclass = psymbol->aclass;
1286
1287 h = fast_hash (&psymbol->ginfo.m_value, sizeof (psymbol->ginfo.m_value), h);
1288 h = fast_hash (&lang, sizeof (unsigned int), h);
1289 h = fast_hash (&domain, sizeof (unsigned int), h);
1290 h = fast_hash (&theclass, sizeof (unsigned int), h);
1291 /* Note that psymbol names are interned via compute_and_set_names, so
1292 there's no need to hash the contents of the name here. */
1293 h = fast_hash (&psymbol->ginfo.m_name, sizeof (psymbol->ginfo.m_name), h);
1294
1295 return h;
1296 }
1297
1298 /* See psymtab.h. */
1299
1300 int
1301 psymbol_bcache::compare (const void *addr1, const void *addr2, int length)
1302 {
1303 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1304 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1305
1306 return (memcmp (&sym1->ginfo.m_value, &sym2->ginfo.m_value,
1307 sizeof (sym1->ginfo.m_value)) == 0
1308 && sym1->ginfo.language () == sym2->ginfo.language ()
1309 && sym1->domain == sym2->domain
1310 && sym1->aclass == sym2->aclass
1311 /* Note that psymbol names are interned via
1312 compute_and_set_names, so there's no need to compare the
1313 contents of the name here. */
1314 && sym1->ginfo.linkage_name () == sym2->ginfo.linkage_name ());
1315 }
1316
1317 /* See psympriv.h. */
1318
1319 void
1320 partial_symtab::add_psymbol (const partial_symbol &psymbol,
1321 psymbol_placement where,
1322 psymtab_storage *partial_symtabs,
1323 struct objfile *objfile)
1324 {
1325 bool added;
1326
1327 /* Stash the partial symbol away in the cache. */
1328 partial_symbol *psym
1329 = ((struct partial_symbol *)
1330 partial_symtabs->psymbol_cache.insert
1331 (&psymbol, sizeof (struct partial_symbol), &added));
1332
1333 /* Do not duplicate global partial symbols. */
1334 if (where == psymbol_placement::GLOBAL && !added)
1335 return;
1336
1337 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1338 std::vector<partial_symbol *> &list
1339 = (where == psymbol_placement::STATIC
1340 ? static_psymbols
1341 : global_psymbols);
1342 list.push_back (psym);
1343 }
1344
1345 /* See psympriv.h. */
1346
1347 void
1348 partial_symtab::add_psymbol (gdb::string_view name, bool copy_name,
1349 domain_enum domain,
1350 enum address_class theclass,
1351 short section,
1352 psymbol_placement where,
1353 CORE_ADDR coreaddr,
1354 enum language language,
1355 psymtab_storage *partial_symtabs,
1356 struct objfile *objfile)
1357 {
1358 struct partial_symbol psymbol;
1359 memset (&psymbol, 0, sizeof (psymbol));
1360
1361 psymbol.set_unrelocated_address (coreaddr);
1362 psymbol.ginfo.set_section_index (section);
1363 psymbol.domain = domain;
1364 psymbol.aclass = theclass;
1365 psymbol.ginfo.set_language (language, partial_symtabs->obstack ());
1366 psymbol.ginfo.compute_and_set_names (name, copy_name, objfile->per_bfd);
1367
1368 add_psymbol (psymbol, where, partial_symtabs, objfile);
1369 }
1370
1371 /* See psympriv.h. */
1372
1373 partial_symtab::partial_symtab (const char *filename_,
1374 psymtab_storage *partial_symtabs,
1375 objfile_per_bfd_storage *objfile_per_bfd)
1376 : searched_flag (PST_NOT_SEARCHED),
1377 text_low_valid (0),
1378 text_high_valid (0)
1379 {
1380 partial_symtabs->install_psymtab (this);
1381
1382 filename = objfile_per_bfd->intern (filename_);
1383
1384 if (symtab_create_debug)
1385 {
1386 /* Be a bit clever with debugging messages, and don't print objfile
1387 every time, only when it changes. */
1388 static std::string last_bfd_name;
1389 const char *this_bfd_name
1390 = bfd_get_filename (objfile_per_bfd->get_bfd ());
1391
1392 if (last_bfd_name.empty () || last_bfd_name != this_bfd_name)
1393 {
1394 last_bfd_name = this_bfd_name;
1395 gdb_printf (gdb_stdlog,
1396 "Creating one or more psymtabs for %s ...\n",
1397 this_bfd_name);
1398 }
1399 gdb_printf (gdb_stdlog,
1400 "Created psymtab %s for module %s.\n",
1401 host_address_to_string (this), filename);
1402 }
1403 }
1404
1405 /* See psympriv.h. */
1406
1407 void
1408 partial_symtab::expand_dependencies (struct objfile *objfile)
1409 {
1410 for (int i = 0; i < number_of_dependencies; ++i)
1411 {
1412 if (!dependencies[i]->readin_p (objfile)
1413 && dependencies[i]->user == NULL)
1414 {
1415 /* Inform about additional files to be read in. */
1416 if (info_verbose)
1417 {
1418 gdb_puts (" ");
1419 gdb_stdout->wrap_here (0);
1420 gdb_puts ("and ");
1421 gdb_stdout->wrap_here (0);
1422 gdb_printf ("%s...", dependencies[i]->filename);
1423 gdb_flush (gdb_stdout);
1424 }
1425 dependencies[i]->expand_psymtab (objfile);
1426 }
1427 }
1428 }
1429
1430
1431 void
1432 psymtab_storage::discard_psymtab (struct partial_symtab *pst)
1433 {
1434 struct partial_symtab **prev_pst;
1435
1436 /* From dbxread.c:
1437 Empty psymtabs happen as a result of header files which don't
1438 have any symbols in them. There can be a lot of them. But this
1439 check is wrong, in that a psymtab with N_SLINE entries but
1440 nothing else is not empty, but we don't realize that. Fixing
1441 that without slowing things down might be tricky. */
1442
1443 /* First, snip it out of the psymtab chain. */
1444
1445 prev_pst = &psymtabs;
1446 while ((*prev_pst) != pst)
1447 prev_pst = &((*prev_pst)->next);
1448 (*prev_pst) = pst->next;
1449 delete pst;
1450 }
1451
1452 \f
1453
1454 /* Helper function for maintenance_print_psymbols to print the addrmap
1455 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1456
1457 static void
1458 dump_psymtab_addrmap (struct objfile *objfile,
1459 psymtab_storage *partial_symtabs,
1460 struct partial_symtab *psymtab,
1461 struct ui_file *outfile)
1462 {
1463 if ((psymtab == NULL
1464 || psymtab->psymtabs_addrmap_supported)
1465 && partial_symtabs->psymtabs_addrmap != NULL)
1466 {
1467 if (psymtab == nullptr)
1468 gdb_printf (outfile, _("Entire address map:\n"));
1469 else
1470 gdb_printf (outfile, _("Address map:\n"));
1471 addrmap_dump (partial_symtabs->psymtabs_addrmap, outfile, psymtab);
1472 }
1473 }
1474
1475 static void
1476 maintenance_print_psymbols (const char *args, int from_tty)
1477 {
1478 struct ui_file *outfile = gdb_stdout;
1479 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1480 int i, outfile_idx, found;
1481 CORE_ADDR pc = 0;
1482 struct obj_section *section = NULL;
1483
1484 dont_repeat ();
1485
1486 gdb_argv argv (args);
1487
1488 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1489 {
1490 if (strcmp (argv[i], "-pc") == 0)
1491 {
1492 if (argv[i + 1] == NULL)
1493 error (_("Missing pc value"));
1494 address_arg = argv[++i];
1495 }
1496 else if (strcmp (argv[i], "-source") == 0)
1497 {
1498 if (argv[i + 1] == NULL)
1499 error (_("Missing source file"));
1500 source_arg = argv[++i];
1501 }
1502 else if (strcmp (argv[i], "-objfile") == 0)
1503 {
1504 if (argv[i + 1] == NULL)
1505 error (_("Missing objfile name"));
1506 objfile_arg = argv[++i];
1507 }
1508 else if (strcmp (argv[i], "--") == 0)
1509 {
1510 /* End of options. */
1511 ++i;
1512 break;
1513 }
1514 else if (argv[i][0] == '-')
1515 {
1516 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1517 error (_("Unknown option: %s"), argv[i]);
1518 }
1519 else
1520 break;
1521 }
1522 outfile_idx = i;
1523
1524 if (address_arg != NULL && source_arg != NULL)
1525 error (_("Must specify at most one of -pc and -source"));
1526
1527 stdio_file arg_outfile;
1528
1529 if (argv != NULL && argv[outfile_idx] != NULL)
1530 {
1531 if (argv[outfile_idx + 1] != NULL)
1532 error (_("Junk at end of command"));
1533 gdb::unique_xmalloc_ptr<char> outfile_name
1534 (tilde_expand (argv[outfile_idx]));
1535 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1536 perror_with_name (outfile_name.get ());
1537 outfile = &arg_outfile;
1538 }
1539
1540 if (address_arg != NULL)
1541 {
1542 pc = parse_and_eval_address (address_arg);
1543 /* If we fail to find a section, that's ok, try the lookup anyway. */
1544 section = find_pc_section (pc);
1545 }
1546
1547 found = 0;
1548 for (objfile *objfile : current_program_space->objfiles ())
1549 {
1550 int printed_objfile_header = 0;
1551 int print_for_objfile = 1;
1552
1553 QUIT;
1554 if (objfile_arg != NULL)
1555 print_for_objfile
1556 = compare_filenames_for_search (objfile_name (objfile),
1557 objfile_arg);
1558 if (!print_for_objfile)
1559 continue;
1560
1561 for (const auto &iter : objfile->qf)
1562 {
1563 psymbol_functions *psf
1564 = dynamic_cast<psymbol_functions *> (iter.get ());
1565 if (psf == nullptr)
1566 continue;
1567
1568 psymtab_storage *partial_symtabs
1569 = psf->get_partial_symtabs ().get ();
1570
1571 if (address_arg != NULL)
1572 {
1573 struct bound_minimal_symbol msymbol;
1574
1575 /* We don't assume each pc has a unique objfile (this is for
1576 debugging). */
1577 struct partial_symtab *ps
1578 = psf->find_pc_sect_psymtab (objfile, pc, section, msymbol);
1579 if (ps != NULL)
1580 {
1581 if (!printed_objfile_header)
1582 {
1583 outfile->printf ("\nPartial symtabs for objfile %s\n",
1584 objfile_name (objfile));
1585 printed_objfile_header = 1;
1586 }
1587 dump_psymtab (objfile, ps, outfile);
1588 dump_psymtab_addrmap (objfile, partial_symtabs, ps, outfile);
1589 found = 1;
1590 }
1591 }
1592 else
1593 {
1594 for (partial_symtab *ps : psf->partial_symbols (objfile))
1595 {
1596 int print_for_source = 0;
1597
1598 QUIT;
1599 if (source_arg != NULL)
1600 {
1601 print_for_source
1602 = compare_filenames_for_search (ps->filename, source_arg);
1603 found = 1;
1604 }
1605 if (source_arg == NULL
1606 || print_for_source)
1607 {
1608 if (!printed_objfile_header)
1609 {
1610 outfile->printf ("\nPartial symtabs for objfile %s\n",
1611 objfile_name (objfile));
1612 printed_objfile_header = 1;
1613 }
1614 dump_psymtab (objfile, ps, outfile);
1615 dump_psymtab_addrmap (objfile, partial_symtabs, ps,
1616 outfile);
1617 }
1618 }
1619 }
1620
1621 /* If we're printing all the objfile's symbols dump the full addrmap. */
1622
1623 if (address_arg == NULL
1624 && source_arg == NULL
1625 && partial_symtabs->psymtabs_addrmap != NULL)
1626 {
1627 outfile->puts ("\n");
1628 dump_psymtab_addrmap (objfile, partial_symtabs, NULL, outfile);
1629 }
1630 }
1631 }
1632
1633 if (!found)
1634 {
1635 if (address_arg != NULL)
1636 error (_("No partial symtab for address: %s"), address_arg);
1637 if (source_arg != NULL)
1638 error (_("No partial symtab for source file: %s"), source_arg);
1639 }
1640 }
1641
1642 /* List all the partial symbol tables whose names match REGEXP (optional). */
1643
1644 static void
1645 maintenance_info_psymtabs (const char *regexp, int from_tty)
1646 {
1647 if (regexp)
1648 re_comp (regexp);
1649
1650 for (struct program_space *pspace : program_spaces)
1651 for (objfile *objfile : pspace->objfiles ())
1652 {
1653 struct gdbarch *gdbarch = objfile->arch ();
1654
1655 /* We don't want to print anything for this objfile until we
1656 actually find a symtab whose name matches. */
1657 int printed_objfile_start = 0;
1658
1659 for (const auto &iter : objfile->qf)
1660 {
1661 psymbol_functions *psf
1662 = dynamic_cast<psymbol_functions *> (iter.get ());
1663 if (psf == nullptr)
1664 continue;
1665 for (partial_symtab *psymtab : psf->partial_symbols (objfile))
1666 {
1667 QUIT;
1668
1669 if (! regexp
1670 || re_exec (psymtab->filename))
1671 {
1672 if (! printed_objfile_start)
1673 {
1674 gdb_printf ("{ objfile %s ", objfile_name (objfile));
1675 gdb_stdout->wrap_here (2);
1676 gdb_printf ("((struct objfile *) %s)\n",
1677 host_address_to_string (objfile));
1678 printed_objfile_start = 1;
1679 }
1680
1681 gdb_printf (" { psymtab %s ", psymtab->filename);
1682 gdb_stdout->wrap_here (4);
1683 gdb_printf ("((struct partial_symtab *) %s)\n",
1684 host_address_to_string (psymtab));
1685
1686 gdb_printf (" readin %s\n",
1687 psymtab->readin_p (objfile) ? "yes" : "no");
1688 gdb_printf (" fullname %s\n",
1689 psymtab->fullname
1690 ? psymtab->fullname : "(null)");
1691 gdb_printf (" text addresses ");
1692 gdb_puts (paddress (gdbarch,
1693 psymtab->text_low (objfile)));
1694 gdb_printf (" -- ");
1695 gdb_puts (paddress (gdbarch,
1696 psymtab->text_high (objfile)));
1697 gdb_printf ("\n");
1698 gdb_printf (" psymtabs_addrmap_supported %s\n",
1699 (psymtab->psymtabs_addrmap_supported
1700 ? "yes" : "no"));
1701 gdb_printf (" globals ");
1702 if (!psymtab->global_psymbols.empty ())
1703 gdb_printf
1704 ("(* (struct partial_symbol **) %s @ %d)\n",
1705 host_address_to_string (psymtab->global_psymbols.data ()),
1706 (int) psymtab->global_psymbols.size ());
1707 else
1708 gdb_printf ("(none)\n");
1709 gdb_printf (" statics ");
1710 if (!psymtab->static_psymbols.empty ())
1711 gdb_printf
1712 ("(* (struct partial_symbol **) %s @ %d)\n",
1713 host_address_to_string (psymtab->static_psymbols.data ()),
1714 (int) psymtab->static_psymbols.size ());
1715 else
1716 gdb_printf ("(none)\n");
1717 if (psymtab->user)
1718 gdb_printf (" user %s "
1719 "((struct partial_symtab *) %s)\n",
1720 psymtab->user->filename,
1721 host_address_to_string (psymtab->user));
1722 gdb_printf (" dependencies ");
1723 if (psymtab->number_of_dependencies)
1724 {
1725 int i;
1726
1727 gdb_printf ("{\n");
1728 for (i = 0; i < psymtab->number_of_dependencies; i++)
1729 {
1730 struct partial_symtab *dep = psymtab->dependencies[i];
1731
1732 /* Note the string concatenation there --- no
1733 comma. */
1734 gdb_printf (" psymtab %s "
1735 "((struct partial_symtab *) %s)\n",
1736 dep->filename,
1737 host_address_to_string (dep));
1738 }
1739 gdb_printf (" }\n");
1740 }
1741 else
1742 gdb_printf ("(none)\n");
1743 gdb_printf (" }\n");
1744 }
1745 }
1746 }
1747
1748 if (printed_objfile_start)
1749 gdb_printf ("}\n");
1750 }
1751 }
1752
1753 /* Check consistency of currently expanded psymtabs vs symtabs. */
1754
1755 static void
1756 maintenance_check_psymtabs (const char *ignore, int from_tty)
1757 {
1758 struct symbol *sym;
1759 struct compunit_symtab *cust = NULL;
1760 const struct blockvector *bv;
1761 const struct block *b;
1762
1763 for (objfile *objfile : current_program_space->objfiles ())
1764 {
1765 for (const auto &iter : objfile->qf)
1766 {
1767 psymbol_functions *psf
1768 = dynamic_cast<psymbol_functions *> (iter.get ());
1769 if (psf == nullptr)
1770 continue;
1771
1772 for (partial_symtab *ps : psf->partial_symbols (objfile))
1773 {
1774 struct gdbarch *gdbarch = objfile->arch ();
1775
1776 /* We don't call psymtab_to_symtab here because that may cause symtab
1777 expansion. When debugging a problem it helps if checkers leave
1778 things unchanged. */
1779 cust = ps->get_compunit_symtab (objfile);
1780
1781 /* First do some checks that don't require the associated symtab. */
1782 if (ps->text_high (objfile) < ps->text_low (objfile))
1783 {
1784 gdb_printf ("Psymtab ");
1785 gdb_puts (ps->filename);
1786 gdb_printf (" covers bad range ");
1787 gdb_puts (paddress (gdbarch, ps->text_low (objfile)));
1788 gdb_printf (" - ");
1789 gdb_puts (paddress (gdbarch, ps->text_high (objfile)));
1790 gdb_printf ("\n");
1791 continue;
1792 }
1793
1794 /* Now do checks requiring the associated symtab. */
1795 if (cust == NULL)
1796 continue;
1797 bv = cust->blockvector ();
1798 b = bv->static_block ();
1799 for (partial_symbol *psym : ps->static_psymbols)
1800 {
1801 /* Skip symbols for inlined functions without address. These may
1802 or may not have a match in the full symtab. */
1803 if (psym->aclass == LOC_BLOCK
1804 && psym->ginfo.value_address () == 0)
1805 continue;
1806
1807 sym = block_lookup_symbol (b, psym->ginfo.search_name (),
1808 symbol_name_match_type::SEARCH_NAME,
1809 psym->domain);
1810 if (!sym)
1811 {
1812 gdb_printf ("Static symbol `");
1813 gdb_puts (psym->ginfo.linkage_name ());
1814 gdb_printf ("' only found in ");
1815 gdb_puts (ps->filename);
1816 gdb_printf (" psymtab\n");
1817 }
1818 }
1819 b = bv->global_block ();
1820 for (partial_symbol *psym : ps->global_psymbols)
1821 {
1822 sym = block_lookup_symbol (b, psym->ginfo.search_name (),
1823 symbol_name_match_type::SEARCH_NAME,
1824 psym->domain);
1825 if (!sym)
1826 {
1827 gdb_printf ("Global symbol `");
1828 gdb_puts (psym->ginfo.linkage_name ());
1829 gdb_printf ("' only found in ");
1830 gdb_puts (ps->filename);
1831 gdb_printf (" psymtab\n");
1832 }
1833 }
1834 if (ps->raw_text_high () != 0
1835 && (ps->text_low (objfile) < b->start ()
1836 || ps->text_high (objfile) > b->end ()))
1837 {
1838 gdb_printf ("Psymtab ");
1839 gdb_puts (ps->filename);
1840 gdb_printf (" covers ");
1841 gdb_puts (paddress (gdbarch, ps->text_low (objfile)));
1842 gdb_printf (" - ");
1843 gdb_puts (paddress (gdbarch, ps->text_high (objfile)));
1844 gdb_printf (" but symtab covers only ");
1845 gdb_puts (paddress (gdbarch, b->start ()));
1846 gdb_printf (" - ");
1847 gdb_puts (paddress (gdbarch, b->end ()));
1848 gdb_printf ("\n");
1849 }
1850 }
1851 }
1852 }
1853 }
1854
1855 void _initialize_psymtab ();
1856 void
1857 _initialize_psymtab ()
1858 {
1859 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
1860 Print dump of current partial symbol definitions.\n\
1861 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
1862 mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
1863 Entries in the partial symbol table are dumped to file OUTFILE,\n\
1864 or the terminal if OUTFILE is unspecified.\n\
1865 If ADDRESS is provided, dump only the file for that address.\n\
1866 If SOURCE is provided, dump only that file's symbols.\n\
1867 If OBJFILE is provided, dump only that file's minimal symbols."),
1868 &maintenanceprintlist);
1869
1870 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
1871 List the partial symbol tables for all object files.\n\
1872 This does not include information about individual partial symbols,\n\
1873 just the symbol table structures themselves."),
1874 &maintenanceinfolist);
1875
1876 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
1877 _("\
1878 Check consistency of currently expanded psymtabs versus symtabs."),
1879 &maintenancelist);
1880 }