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