1 /* Partial symbol tables.
3 Copyright (C) 2009-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "filenames.h"
32 #include "readline/readline.h"
33 #include "gdb_regex.h"
34 #include "dictionary.h"
36 #include "cp-support.h"
43 struct bcache
*bcache
;
46 static struct partial_symbol
*match_partial_symbol (struct objfile
*,
47 struct partial_symtab
*,
49 const char *, domain_enum
,
50 symbol_name_match_type
,
51 symbol_compare_ftype
*);
53 static struct partial_symbol
*lookup_partial_symbol (struct objfile
*,
54 struct partial_symtab
*,
58 static const char *psymtab_to_fullname (struct partial_symtab
*ps
);
60 static struct partial_symbol
*find_pc_sect_psymbol (struct objfile
*,
61 struct partial_symtab
*,
63 struct obj_section
*);
65 static struct compunit_symtab
*psymtab_to_symtab (struct objfile
*objfile
,
66 struct partial_symtab
*pst
);
68 /* Ensure that the partial symbols for OBJFILE have been loaded. This
69 function always returns its argument, as a convenience. */
72 require_partial_symbols (struct objfile
*objfile
, int verbose
)
74 if ((objfile
->flags
& OBJF_PSYMTABS_READ
) == 0)
76 objfile
->flags
|= OBJF_PSYMTABS_READ
;
78 if (objfile
->sf
->sym_read_psymbols
)
81 printf_filtered (_("Reading symbols from %s...\n"),
82 objfile_name (objfile
));
83 (*objfile
->sf
->sym_read_psymbols
) (objfile
);
85 /* Partial symbols list are not expected to changed after this
87 objfile
->global_psymbols
.shrink_to_fit ();
88 objfile
->static_psymbols
.shrink_to_fit ();
90 if (verbose
&& !objfile_has_symbols (objfile
))
91 printf_filtered (_("(No debugging symbols found in %s)\n"),
92 objfile_name (objfile
));
99 /* Traverse all psymtabs in one objfile, requiring that the psymtabs
102 #define ALL_OBJFILE_PSYMTABS_REQUIRED(objfile, p) \
103 for ((p) = require_partial_symbols (objfile, 1)->psymtabs; \
107 /* We want to make sure this file always requires psymtabs. */
109 #undef ALL_OBJFILE_PSYMTABS
111 /* Helper function for psym_map_symtabs_matching_filename that
112 expands the symtabs and calls the iterator. */
115 partial_map_expand_apply (struct objfile
*objfile
,
117 const char *real_path
,
118 struct partial_symtab
*pst
,
119 gdb::function_view
<bool (symtab
*)> callback
)
121 struct compunit_symtab
*last_made
= objfile
->compunit_symtabs
;
123 /* Shared psymtabs should never be seen here. Instead they should
124 be handled properly by the caller. */
125 gdb_assert (pst
->user
== NULL
);
127 /* Don't visit already-expanded psymtabs. */
131 /* This may expand more than one symtab, and we want to iterate over
133 psymtab_to_symtab (objfile
, pst
);
135 return iterate_over_some_symtabs (name
, real_path
, objfile
->compunit_symtabs
,
136 last_made
, callback
);
139 /* Psymtab version of map_symtabs_matching_filename. See its definition in
140 the definition of quick_symbol_functions in symfile.h. */
143 psym_map_symtabs_matching_filename
144 (struct objfile
*objfile
,
146 const char *real_path
,
147 gdb::function_view
<bool (symtab
*)> callback
)
149 struct partial_symtab
*pst
;
150 const char *name_basename
= lbasename (name
);
152 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, pst
)
154 /* We can skip shared psymtabs here, because any file name will be
155 attached to the unshared psymtab. */
156 if (pst
->user
!= NULL
)
159 /* Anonymous psymtabs don't have a file name. */
163 if (compare_filenames_for_search (pst
->filename
, name
))
165 if (partial_map_expand_apply (objfile
, name
, real_path
,
171 /* Before we invoke realpath, which can get expensive when many
172 files are involved, do a quick comparison of the basenames. */
173 if (! basenames_may_differ
174 && FILENAME_CMP (name_basename
, lbasename (pst
->filename
)) != 0)
177 if (compare_filenames_for_search (psymtab_to_fullname (pst
), name
))
179 if (partial_map_expand_apply (objfile
, name
, real_path
,
185 /* If the user gave us an absolute path, try to find the file in
186 this symtab and use its absolute path. */
187 if (real_path
!= NULL
)
189 gdb_assert (IS_ABSOLUTE_PATH (real_path
));
190 gdb_assert (IS_ABSOLUTE_PATH (name
));
191 if (filename_cmp (psymtab_to_fullname (pst
), real_path
) == 0)
193 if (partial_map_expand_apply (objfile
, name
, real_path
,
204 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
205 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
207 static struct partial_symtab
*
208 find_pc_sect_psymtab_closer (struct objfile
*objfile
,
209 CORE_ADDR pc
, struct obj_section
*section
,
210 struct partial_symtab
*pst
,
211 struct bound_minimal_symbol msymbol
)
213 struct partial_symtab
*tpst
;
214 struct partial_symtab
*best_pst
= pst
;
215 CORE_ADDR best_addr
= pst
->text_low (objfile
);
217 gdb_assert (!pst
->psymtabs_addrmap_supported
);
219 /* An objfile that has its functions reordered might have
220 many partial symbol tables containing the PC, but
221 we want the partial symbol table that contains the
222 function containing the PC. */
223 if (!(objfile
->flags
& OBJF_REORDERED
)
224 && section
== NULL
) /* Can't validate section this way. */
227 if (msymbol
.minsym
== NULL
)
230 /* The code range of partial symtabs sometimes overlap, so, in
231 the loop below, we need to check all partial symtabs and
232 find the one that fits better for the given PC address. We
233 select the partial symtab that contains a symbol whose
234 address is closest to the PC address. By closest we mean
235 that find_pc_sect_symbol returns the symbol with address
236 that is closest and still less than the given PC. */
237 for (tpst
= pst
; tpst
!= NULL
; tpst
= tpst
->next
)
239 if (pc
>= tpst
->text_low (objfile
) && pc
< tpst
->text_high (objfile
))
241 struct partial_symbol
*p
;
244 /* NOTE: This assumes that every psymbol has a
245 corresponding msymbol, which is not necessarily
246 true; the debug info might be much richer than the
247 object's symbol table. */
248 p
= find_pc_sect_psymbol (objfile
, tpst
, pc
, section
);
250 && (p
->address (objfile
) == BMSYMBOL_VALUE_ADDRESS (msymbol
)))
253 /* Also accept the textlow value of a psymtab as a
254 "symbol", to provide some support for partial
255 symbol tables with line information but no debug
256 symbols (e.g. those produced by an assembler). */
258 this_addr
= p
->address (objfile
);
260 this_addr
= tpst
->text_low (objfile
);
262 /* Check whether it is closer than our current
263 BEST_ADDR. Since this symbol address is
264 necessarily lower or equal to PC, the symbol closer
265 to PC is the symbol which address is the highest.
266 This way we return the psymtab which contains such
267 best match symbol. This can help in cases where the
268 symbol information/debuginfo is not complete, like
269 for instance on IRIX6 with gcc, where no debug info
270 is emitted for statics. (See also the nodebug.exp
272 if (this_addr
> best_addr
)
274 best_addr
= this_addr
;
282 /* Find which partial symtab contains PC and SECTION. Return NULL if
283 none. We return the psymtab that contains a symbol whose address
284 exactly matches PC, or, if we cannot find an exact match, the
285 psymtab that contains a symbol whose address is closest to PC. */
287 static struct partial_symtab
*
288 find_pc_sect_psymtab (struct objfile
*objfile
, CORE_ADDR pc
,
289 struct obj_section
*section
,
290 struct bound_minimal_symbol msymbol
)
292 struct partial_symtab
*pst
;
293 CORE_ADDR baseaddr
= ANOFFSET (objfile
->section_offsets
,
294 SECT_OFF_TEXT (objfile
));
296 /* Try just the PSYMTABS_ADDRMAP mapping first as it has better granularity
297 than the later used TEXTLOW/TEXTHIGH one. */
299 if (objfile
->psymtabs_addrmap
!= NULL
)
301 pst
= ((struct partial_symtab
*)
302 addrmap_find (objfile
->psymtabs_addrmap
, pc
- baseaddr
));
305 /* FIXME: addrmaps currently do not handle overlayed sections,
306 so fall back to the non-addrmap case if we're debugging
307 overlays and the addrmap returned the wrong section. */
308 if (overlay_debugging
&& msymbol
.minsym
!= NULL
&& section
!= NULL
)
310 struct partial_symbol
*p
;
312 /* NOTE: This assumes that every psymbol has a
313 corresponding msymbol, which is not necessarily
314 true; the debug info might be much richer than the
315 object's symbol table. */
316 p
= find_pc_sect_psymbol (objfile
, pst
, pc
, section
);
318 || (p
->address (objfile
)
319 != BMSYMBOL_VALUE_ADDRESS (msymbol
)))
323 /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
324 PSYMTABS_ADDRMAP we used has already the best 1-byte
325 granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
326 a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
335 /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
336 which still have no corresponding full SYMTABs read. But it is not
337 present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
340 /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
341 its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
342 debug info type in single OBJFILE. */
344 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, pst
)
345 if (!pst
->psymtabs_addrmap_supported
346 && pc
>= pst
->text_low (objfile
) && pc
< pst
->text_high (objfile
))
348 struct partial_symtab
*best_pst
;
350 best_pst
= find_pc_sect_psymtab_closer (objfile
, pc
, section
, pst
,
352 if (best_pst
!= NULL
)
359 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
360 the definition of quick_symbol_functions in symfile.h. */
362 static struct compunit_symtab
*
363 psym_find_pc_sect_compunit_symtab (struct objfile
*objfile
,
364 struct bound_minimal_symbol msymbol
,
366 struct obj_section
*section
,
369 struct partial_symtab
*ps
= find_pc_sect_psymtab (objfile
, pc
, section
,
373 if (warn_if_readin
&& ps
->readin
)
374 /* Might want to error() here (in case symtab is corrupt and
375 will cause a core dump), but maybe we can successfully
376 continue, so let's not. */
378 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
379 paddress (get_objfile_arch (objfile
), pc
));
380 psymtab_to_symtab (objfile
, ps
);
381 return ps
->compunit_symtab
;
386 /* Find which partial symbol within a psymtab matches PC and SECTION.
387 Return NULL if none. */
389 static struct partial_symbol
*
390 find_pc_sect_psymbol (struct objfile
*objfile
,
391 struct partial_symtab
*psymtab
, CORE_ADDR pc
,
392 struct obj_section
*section
)
394 struct partial_symbol
*best
= NULL
;
396 const CORE_ADDR textlow
= psymtab
->text_low (objfile
);
398 gdb_assert (psymtab
!= NULL
);
400 /* Cope with programs that start at address 0. */
401 best_pc
= (textlow
!= 0) ? textlow
- 1 : 0;
403 /* Search the global symbols as well as the static symbols, so that
404 find_pc_partial_function doesn't use a minimal symbol and thus
405 cache a bad endaddr. */
406 for (int i
= 0; i
< psymtab
->n_global_syms
; i
++)
408 partial_symbol
*p
= objfile
->global_psymbols
[psymtab
->globals_offset
+ i
];
410 if (p
->domain
== VAR_DOMAIN
411 && p
->aclass
== LOC_BLOCK
412 && pc
>= p
->address (objfile
)
413 && (p
->address (objfile
) > best_pc
414 || (psymtab
->text_low (objfile
) == 0
415 && best_pc
== 0 && p
->address (objfile
) == 0)))
417 if (section
!= NULL
) /* Match on a specific section. */
419 if (!matching_obj_sections (p
->obj_section (objfile
),
423 best_pc
= p
->address (objfile
);
428 for (int i
= 0; i
< psymtab
->n_static_syms
; i
++)
430 partial_symbol
*p
= objfile
->static_psymbols
[psymtab
->statics_offset
+ i
];
432 if (p
->domain
== VAR_DOMAIN
433 && p
->aclass
== LOC_BLOCK
434 && pc
>= p
->address (objfile
)
435 && (p
->address (objfile
) > best_pc
436 || (psymtab
->text_low (objfile
) == 0
437 && best_pc
== 0 && p
->address (objfile
) == 0)))
439 if (section
!= NULL
) /* Match on a specific section. */
441 if (!matching_obj_sections (p
->obj_section (objfile
),
445 best_pc
= p
->address (objfile
);
453 /* Psymtab version of lookup_symbol. See its definition in
454 the definition of quick_symbol_functions in symfile.h. */
456 static struct compunit_symtab
*
457 psym_lookup_symbol (struct objfile
*objfile
,
458 int block_index
, const char *name
,
459 const domain_enum domain
)
461 struct partial_symtab
*ps
;
462 const int psymtab_index
= (block_index
== GLOBAL_BLOCK
? 1 : 0);
463 struct compunit_symtab
*stab_best
= NULL
;
465 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
467 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
469 if (!ps
->readin
&& lookup_partial_symbol (objfile
, ps
, name
,
470 psymtab_index
, domain
))
472 struct symbol
*sym
, *with_opaque
= NULL
;
473 struct compunit_symtab
*stab
= psymtab_to_symtab (objfile
, ps
);
474 /* Note: While psymtab_to_symtab can return NULL if the partial symtab
475 is empty, we can assume it won't here because lookup_partial_symbol
477 const struct blockvector
*bv
= COMPUNIT_BLOCKVECTOR (stab
);
478 struct block
*block
= BLOCKVECTOR_BLOCK (bv
, block_index
);
480 sym
= block_find_symbol (block
, name
, domain
,
481 block_find_non_opaque_type_preferred
,
484 /* Some caution must be observed with overloaded functions
485 and methods, since the index will not contain any overload
486 information (but NAME might contain it). */
489 && SYMBOL_MATCHES_SEARCH_NAME (sym
, lookup_name
))
491 if (with_opaque
!= NULL
492 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque
, lookup_name
))
495 /* Keep looking through other psymtabs. */
502 /* Returns true if PSYM matches LOOKUP_NAME. */
505 psymbol_name_matches (partial_symbol
*psym
,
506 const lookup_name_info
&lookup_name
)
508 const language_defn
*lang
= language_def (psym
->language
);
509 symbol_name_matcher_ftype
*name_match
510 = get_symbol_name_matcher (lang
, lookup_name
);
511 return name_match (symbol_search_name (psym
), lookup_name
, NULL
);
514 /* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
515 the global block of PST if GLOBAL, and otherwise the static block.
516 MATCH is the comparison operation that returns true iff MATCH (s,
517 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
518 non-null, the symbols in the block are assumed to be ordered
519 according to it (allowing binary search). It must be compatible
520 with MATCH. Returns the symbol, if found, and otherwise NULL. */
522 static struct partial_symbol
*
523 match_partial_symbol (struct objfile
*objfile
,
524 struct partial_symtab
*pst
, int global
,
525 const char *name
, domain_enum domain
,
526 symbol_name_match_type match_type
,
527 symbol_compare_ftype
*ordered_compare
)
529 struct partial_symbol
**start
, **psym
;
530 struct partial_symbol
**top
, **real_top
, **bottom
, **center
;
531 int length
= (global
? pst
->n_global_syms
: pst
->n_static_syms
);
532 int do_linear_search
= 1;
537 lookup_name_info
lookup_name (name
, match_type
);
540 &objfile
->global_psymbols
[pst
->globals_offset
] :
541 &objfile
->static_psymbols
[pst
->statics_offset
]);
543 if (global
&& ordered_compare
) /* Can use a binary search. */
545 do_linear_search
= 0;
547 /* Binary search. This search is guaranteed to end with center
548 pointing at the earliest partial symbol whose name might be
549 correct. At that point *all* partial symbols with an
550 appropriate name will be checked against the correct
554 top
= start
+ length
- 1;
558 center
= bottom
+ (top
- bottom
) / 2;
559 gdb_assert (center
< top
);
561 enum language lang
= (*center
)->language
;
563 = lookup_name
.language_lookup_name (lang
).c_str ();
565 if (ordered_compare (symbol_search_name (*center
), lang_ln
) >= 0)
570 gdb_assert (top
== bottom
);
572 while (top
<= real_top
573 && psymbol_name_matches (*top
, lookup_name
))
575 if (symbol_matches_domain ((*top
)->language
,
576 (*top
)->domain
, domain
))
582 /* Can't use a binary search or else we found during the binary search that
583 we should also do a linear search. */
585 if (do_linear_search
)
587 for (psym
= start
; psym
< start
+ length
; psym
++)
589 if (symbol_matches_domain ((*psym
)->language
,
590 (*psym
)->domain
, domain
)
591 && psymbol_name_matches (*psym
, lookup_name
))
599 /* Returns the name used to search psymtabs. Unlike symtabs, psymtabs do
600 not contain any method/function instance information (since this would
601 force reading type information while reading psymtabs). Therefore,
602 if NAME contains overload information, it must be stripped before searching
605 static gdb::unique_xmalloc_ptr
<char>
606 psymtab_search_name (const char *name
)
608 switch (current_language
->la_language
)
612 if (strchr (name
, '('))
614 gdb::unique_xmalloc_ptr
<char> ret
= cp_remove_params (name
);
626 return gdb::unique_xmalloc_ptr
<char> (xstrdup (name
));
629 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
630 Check the global symbols if GLOBAL, the static symbols if not. */
632 static struct partial_symbol
*
633 lookup_partial_symbol (struct objfile
*objfile
,
634 struct partial_symtab
*pst
, const char *name
,
635 int global
, domain_enum domain
)
637 struct partial_symbol
**start
, **psym
;
638 struct partial_symbol
**top
, **real_top
, **bottom
, **center
;
639 int length
= (global
? pst
->n_global_syms
: pst
->n_static_syms
);
640 int do_linear_search
= 1;
645 gdb::unique_xmalloc_ptr
<char> search_name
= psymtab_search_name (name
);
647 lookup_name_info
lookup_name (search_name
.get (), symbol_name_match_type::FULL
);
650 &objfile
->global_psymbols
[pst
->globals_offset
] :
651 &objfile
->static_psymbols
[pst
->statics_offset
]);
653 if (global
) /* This means we can use a binary search. */
655 do_linear_search
= 0;
657 /* Binary search. This search is guaranteed to end with center
658 pointing at the earliest partial symbol whose name might be
659 correct. At that point *all* partial symbols with an
660 appropriate name will be checked against the correct
664 top
= start
+ length
- 1;
668 center
= bottom
+ (top
- bottom
) / 2;
670 internal_error (__FILE__
, __LINE__
,
671 _("failed internal consistency check"));
672 if (strcmp_iw_ordered (symbol_search_name (*center
),
673 search_name
.get ()) >= 0)
682 if (!(top
== bottom
))
683 internal_error (__FILE__
, __LINE__
,
684 _("failed internal consistency check"));
686 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
687 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
688 while (top
>= start
&& symbol_matches_search_name (*top
, lookup_name
))
691 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
694 while (top
<= real_top
&& symbol_matches_search_name (*top
, lookup_name
))
696 if (symbol_matches_domain ((*top
)->language
,
697 (*top
)->domain
, domain
))
703 /* Can't use a binary search or else we found during the binary search that
704 we should also do a linear search. */
706 if (do_linear_search
)
708 for (psym
= start
; psym
< start
+ length
; psym
++)
710 if (symbol_matches_domain ((*psym
)->language
,
711 (*psym
)->domain
, domain
)
712 && symbol_matches_search_name (*psym
, lookup_name
))
720 /* Get the symbol table that corresponds to a partial_symtab.
721 This is fast after the first time you do it.
722 The result will be NULL if the primary symtab has no symbols,
723 which can happen. Otherwise the result is the primary symtab
724 that contains PST. */
726 static struct compunit_symtab
*
727 psymtab_to_symtab (struct objfile
*objfile
, struct partial_symtab
*pst
)
729 /* If it is a shared psymtab, find an unshared psymtab that includes
730 it. Any such psymtab will do. */
731 while (pst
->user
!= NULL
)
734 /* If it's been looked up before, return it. */
735 if (pst
->compunit_symtab
)
736 return pst
->compunit_symtab
;
738 /* If it has not yet been read in, read it. */
741 scoped_restore decrementer
= increment_reading_symtab ();
743 (*pst
->read_symtab
) (pst
, objfile
);
746 return pst
->compunit_symtab
;
749 /* Psymtab version of find_last_source_symtab. See its definition in
750 the definition of quick_symbol_functions in symfile.h. */
752 static struct symtab
*
753 psym_find_last_source_symtab (struct objfile
*ofp
)
755 struct partial_symtab
*ps
;
756 struct partial_symtab
*cs_pst
= NULL
;
758 ALL_OBJFILE_PSYMTABS_REQUIRED (ofp
, ps
)
760 const char *name
= ps
->filename
;
761 int len
= strlen (name
);
763 if (!(len
> 2 && (strcmp (&name
[len
- 2], ".h") == 0
764 || strcmp (name
, "<<C++-namespaces>>") == 0)))
772 internal_error (__FILE__
, __LINE__
,
773 _("select_source_symtab: "
774 "readin pst found and no symtabs."));
778 struct compunit_symtab
*cust
= psymtab_to_symtab (ofp
, cs_pst
);
782 return compunit_primary_filetab (cust
);
788 /* Psymtab version of forget_cached_source_info. See its definition in
789 the definition of quick_symbol_functions in symfile.h. */
792 psym_forget_cached_source_info (struct objfile
*objfile
)
794 struct partial_symtab
*pst
;
796 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, pst
)
798 if (pst
->fullname
!= NULL
)
800 xfree (pst
->fullname
);
801 pst
->fullname
= NULL
;
807 print_partial_symbols (struct gdbarch
*gdbarch
, struct objfile
*objfile
,
808 struct partial_symbol
**p
, int count
, const char *what
,
809 struct ui_file
*outfile
)
811 fprintf_filtered (outfile
, " %s partial symbols:\n", what
);
815 fprintf_filtered (outfile
, " `%s'", (*p
)->name
);
816 if (symbol_demangled_name (*p
) != NULL
)
818 fprintf_filtered (outfile
, " `%s'", symbol_demangled_name (*p
));
820 fputs_filtered (", ", outfile
);
821 switch ((*p
)->domain
)
824 fputs_filtered ("undefined domain, ", outfile
);
827 /* This is the usual thing -- don't print it. */
830 fputs_filtered ("struct domain, ", outfile
);
833 fputs_filtered ("label domain, ", outfile
);
836 fputs_filtered ("<invalid domain>, ", outfile
);
839 switch ((*p
)->aclass
)
842 fputs_filtered ("undefined", outfile
);
845 fputs_filtered ("constant int", outfile
);
848 fputs_filtered ("static", outfile
);
851 fputs_filtered ("register", outfile
);
854 fputs_filtered ("pass by value", outfile
);
857 fputs_filtered ("pass by reference", outfile
);
859 case LOC_REGPARM_ADDR
:
860 fputs_filtered ("register address parameter", outfile
);
863 fputs_filtered ("stack parameter", outfile
);
866 fputs_filtered ("type", outfile
);
869 fputs_filtered ("label", outfile
);
872 fputs_filtered ("function", outfile
);
874 case LOC_CONST_BYTES
:
875 fputs_filtered ("constant bytes", outfile
);
878 fputs_filtered ("unresolved", outfile
);
880 case LOC_OPTIMIZED_OUT
:
881 fputs_filtered ("optimized out", outfile
);
884 fputs_filtered ("computed at runtime", outfile
);
887 fputs_filtered ("<invalid location>", outfile
);
890 fputs_filtered (", ", outfile
);
891 fputs_filtered (paddress (gdbarch
, (*p
)->unrelocated_address ()), outfile
);
892 fprintf_filtered (outfile
, "\n");
898 dump_psymtab (struct objfile
*objfile
, struct partial_symtab
*psymtab
,
899 struct ui_file
*outfile
)
901 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
904 if (psymtab
->anonymous
)
906 fprintf_filtered (outfile
, "\nAnonymous partial symtab (%s) ",
911 fprintf_filtered (outfile
, "\nPartial symtab for source file %s ",
914 fprintf_filtered (outfile
, "(object ");
915 gdb_print_host_address (psymtab
, outfile
);
916 fprintf_filtered (outfile
, ")\n\n");
917 fprintf_filtered (outfile
, " Read from object file %s (",
918 objfile_name (objfile
));
919 gdb_print_host_address (objfile
, outfile
);
920 fprintf_filtered (outfile
, ")\n");
924 fprintf_filtered (outfile
,
925 " Full symtab was read (at ");
926 gdb_print_host_address (psymtab
->compunit_symtab
, outfile
);
927 fprintf_filtered (outfile
, " by function at ");
928 gdb_print_host_address (psymtab
->read_symtab
, outfile
);
929 fprintf_filtered (outfile
, ")\n");
932 fprintf_filtered (outfile
, " Symbols cover text addresses ");
933 fputs_filtered (paddress (gdbarch
, psymtab
->text_low (objfile
)), outfile
);
934 fprintf_filtered (outfile
, "-");
935 fputs_filtered (paddress (gdbarch
, psymtab
->text_high (objfile
)), outfile
);
936 fprintf_filtered (outfile
, "\n");
937 fprintf_filtered (outfile
, " Address map supported - %s.\n",
938 psymtab
->psymtabs_addrmap_supported
? "yes" : "no");
939 fprintf_filtered (outfile
, " Depends on %d other partial symtabs.\n",
940 psymtab
->number_of_dependencies
);
941 for (i
= 0; i
< psymtab
->number_of_dependencies
; i
++)
943 fprintf_filtered (outfile
, " %d ", i
);
944 gdb_print_host_address (psymtab
->dependencies
[i
], outfile
);
945 fprintf_filtered (outfile
, " %s\n",
946 psymtab
->dependencies
[i
]->filename
);
948 if (psymtab
->user
!= NULL
)
950 fprintf_filtered (outfile
, " Shared partial symtab with user ");
951 gdb_print_host_address (psymtab
->user
, outfile
);
952 fprintf_filtered (outfile
, "\n");
954 if (psymtab
->n_global_syms
> 0)
956 print_partial_symbols (gdbarch
, objfile
,
957 &objfile
->global_psymbols
[psymtab
->globals_offset
],
958 psymtab
->n_global_syms
, "Global", outfile
);
960 if (psymtab
->n_static_syms
> 0)
962 print_partial_symbols (gdbarch
, objfile
,
963 &objfile
->static_psymbols
[psymtab
->statics_offset
],
964 psymtab
->n_static_syms
, "Static", outfile
);
966 fprintf_filtered (outfile
, "\n");
969 /* Psymtab version of print_stats. See its definition in
970 the definition of quick_symbol_functions in symfile.h. */
973 psym_print_stats (struct objfile
*objfile
)
976 struct partial_symtab
*ps
;
979 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
984 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i
);
987 /* Psymtab version of dump. See its definition in
988 the definition of quick_symbol_functions in symfile.h. */
991 psym_dump (struct objfile
*objfile
)
993 struct partial_symtab
*psymtab
;
995 if (objfile
->psymtabs
)
997 printf_filtered ("Psymtabs:\n");
998 for (psymtab
= objfile
->psymtabs
;
1000 psymtab
= psymtab
->next
)
1002 printf_filtered ("%s at ",
1004 gdb_print_host_address (psymtab
, gdb_stdout
);
1005 printf_filtered (", ");
1008 printf_filtered ("\n\n");
1012 /* Psymtab version of expand_symtabs_for_function. See its definition in
1013 the definition of quick_symbol_functions in symfile.h. */
1016 psym_expand_symtabs_for_function (struct objfile
*objfile
,
1017 const char *func_name
)
1019 struct partial_symtab
*ps
;
1021 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
1026 if ((lookup_partial_symbol (objfile
, ps
, func_name
, 1, VAR_DOMAIN
)
1028 || (lookup_partial_symbol (objfile
, ps
, func_name
, 0, VAR_DOMAIN
)
1030 psymtab_to_symtab (objfile
, ps
);
1034 /* Psymtab version of expand_all_symtabs. See its definition in
1035 the definition of quick_symbol_functions in symfile.h. */
1038 psym_expand_all_symtabs (struct objfile
*objfile
)
1040 struct partial_symtab
*psymtab
;
1042 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, psymtab
)
1044 psymtab_to_symtab (objfile
, psymtab
);
1048 /* Psymtab version of expand_symtabs_with_fullname. See its definition in
1049 the definition of quick_symbol_functions in symfile.h. */
1052 psym_expand_symtabs_with_fullname (struct objfile
*objfile
,
1053 const char *fullname
)
1055 struct partial_symtab
*p
;
1057 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, p
)
1059 /* Anonymous psymtabs don't have a name of a source file. */
1063 /* psymtab_to_fullname tries to open the file which is slow.
1064 Don't call it if we know the basenames don't match. */
1065 if ((basenames_may_differ
1066 || filename_cmp (lbasename (fullname
), lbasename (p
->filename
)) == 0)
1067 && filename_cmp (fullname
, psymtab_to_fullname (p
)) == 0)
1068 psymtab_to_symtab (objfile
, p
);
1072 /* Psymtab version of map_symbol_filenames. See its definition in
1073 the definition of quick_symbol_functions in symfile.h. */
1076 psym_map_symbol_filenames (struct objfile
*objfile
,
1077 symbol_filename_ftype
*fun
, void *data
,
1080 struct partial_symtab
*ps
;
1082 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
1084 const char *fullname
;
1089 /* We can skip shared psymtabs here, because any file name will be
1090 attached to the unshared psymtab. */
1091 if (ps
->user
!= NULL
)
1094 /* Anonymous psymtabs don't have a file name. */
1100 fullname
= psymtab_to_fullname (ps
);
1103 (*fun
) (ps
->filename
, fullname
, data
);
1107 /* Finds the fullname that a partial_symtab represents.
1109 If this functions finds the fullname, it will save it in ps->fullname
1110 and it will also return the value.
1112 If this function fails to find the file that this partial_symtab represents,
1113 NULL will be returned and ps->fullname will be set to NULL. */
1116 psymtab_to_fullname (struct partial_symtab
*ps
)
1118 gdb_assert (!ps
->anonymous
);
1120 /* Use cached copy if we have it.
1121 We rely on forget_cached_source_info being called appropriately
1122 to handle cases like the file being moved. */
1123 if (ps
->fullname
== NULL
)
1125 gdb::unique_xmalloc_ptr
<char> fullname
;
1126 scoped_fd fd
= find_and_open_source (ps
->filename
, ps
->dirname
,
1128 ps
->fullname
= fullname
.release ();
1132 /* rewrite_source_path would be applied by find_and_open_source, we
1133 should report the pathname where GDB tried to find the file. */
1135 if (ps
->dirname
== NULL
|| IS_ABSOLUTE_PATH (ps
->filename
))
1136 fullname
.reset (xstrdup (ps
->filename
));
1138 fullname
.reset (concat (ps
->dirname
, SLASH_STRING
,
1139 ps
->filename
, (char *) NULL
));
1141 ps
->fullname
= rewrite_source_path (fullname
.get ()).release ();
1142 if (ps
->fullname
== NULL
)
1143 ps
->fullname
= fullname
.release ();
1147 return ps
->fullname
;
1150 /* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1151 according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1152 BLOCK is assumed to come from OBJFILE. Returns 1 iff CALLBACK
1153 ever returns non-zero, and otherwise returns 0. */
1156 map_block (const char *name
, domain_enum domain
, struct objfile
*objfile
,
1157 struct block
*block
,
1158 int (*callback
) (struct block
*, struct symbol
*, void *),
1159 void *data
, symbol_name_match_type match
)
1161 struct block_iterator iter
;
1164 lookup_name_info
lookup_name (name
, match
);
1166 for (sym
= block_iter_match_first (block
, lookup_name
, &iter
);
1168 sym
= block_iter_match_next (lookup_name
, &iter
))
1170 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
1171 SYMBOL_DOMAIN (sym
), domain
))
1173 if (callback (block
, sym
, data
))
1181 /* Psymtab version of map_matching_symbols. See its definition in
1182 the definition of quick_symbol_functions in symfile.h. */
1185 psym_map_matching_symbols (struct objfile
*objfile
,
1186 const char *name
, domain_enum domain
,
1188 int (*callback
) (struct block
*,
1189 struct symbol
*, void *),
1191 symbol_name_match_type match
,
1192 symbol_compare_ftype
*ordered_compare
)
1194 const int block_kind
= global
? GLOBAL_BLOCK
: STATIC_BLOCK
;
1195 struct partial_symtab
*ps
;
1197 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
1201 || match_partial_symbol (objfile
, ps
, global
, name
, domain
, match
,
1204 struct compunit_symtab
*cust
= psymtab_to_symtab (objfile
, ps
);
1205 struct block
*block
;
1209 block
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust
), block_kind
);
1210 if (map_block (name
, domain
, objfile
, block
,
1211 callback
, data
, match
))
1213 if (callback (block
, NULL
, data
))
1219 /* A helper for psym_expand_symtabs_matching that handles searching
1220 included psymtabs. This returns true if a symbol is found, and
1221 false otherwise. It also updates the 'searched_flag' on the
1222 various psymtabs that it searches. */
1225 recursively_search_psymtabs
1226 (struct partial_symtab
*ps
,
1227 struct objfile
*objfile
,
1228 enum search_domain domain
,
1229 const lookup_name_info
&lookup_name
,
1230 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> sym_matcher
)
1233 enum psymtab_search_status result
= PST_SEARCHED_AND_NOT_FOUND
;
1236 if (ps
->searched_flag
!= PST_NOT_SEARCHED
)
1237 return ps
->searched_flag
== PST_SEARCHED_AND_FOUND
;
1239 /* Recurse into shared psymtabs first, because they may have already
1240 been searched, and this could save some time. */
1241 for (i
= 0; i
< ps
->number_of_dependencies
; ++i
)
1245 /* Skip non-shared dependencies, these are handled elsewhere. */
1246 if (ps
->dependencies
[i
]->user
== NULL
)
1249 r
= recursively_search_psymtabs (ps
->dependencies
[i
],
1250 objfile
, domain
, lookup_name
,
1254 ps
->searched_flag
= PST_SEARCHED_AND_FOUND
;
1259 partial_symbol
**gbound
1260 = objfile
->global_psymbols
.data () + ps
->globals_offset
+ ps
->n_global_syms
;
1261 partial_symbol
**sbound
1262 = objfile
->static_psymbols
.data () + ps
->statics_offset
+ ps
->n_static_syms
;
1263 partial_symbol
**bound
= gbound
;
1265 /* Go through all of the symbols stored in a partial
1266 symtab in one loop. */
1267 partial_symbol
**psym
= objfile
->global_psymbols
.data () + ps
->globals_offset
;
1272 if (bound
== gbound
&& ps
->n_static_syms
!= 0)
1274 psym
= objfile
->static_psymbols
.data () + ps
->statics_offset
;
1285 if ((domain
== ALL_DOMAIN
1286 || (domain
== VARIABLES_DOMAIN
1287 && (*psym
)->aclass
!= LOC_TYPEDEF
1288 && (*psym
)->aclass
!= LOC_BLOCK
)
1289 || (domain
== FUNCTIONS_DOMAIN
1290 && (*psym
)->aclass
== LOC_BLOCK
)
1291 || (domain
== TYPES_DOMAIN
1292 && (*psym
)->aclass
== LOC_TYPEDEF
))
1293 && psymbol_name_matches (*psym
, lookup_name
)
1294 && (sym_matcher
== NULL
|| sym_matcher (symbol_search_name (*psym
))))
1296 /* Found a match, so notify our caller. */
1297 result
= PST_SEARCHED_AND_FOUND
;
1304 ps
->searched_flag
= result
;
1305 return result
== PST_SEARCHED_AND_FOUND
;
1308 /* Psymtab version of expand_symtabs_matching. See its definition in
1309 the definition of quick_symbol_functions in symfile.h. */
1312 psym_expand_symtabs_matching
1313 (struct objfile
*objfile
,
1314 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1315 const lookup_name_info
&lookup_name_in
,
1316 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1317 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1318 enum search_domain domain
)
1320 struct partial_symtab
*ps
;
1322 lookup_name_info lookup_name
= lookup_name_in
.make_ignore_params ();
1324 /* Clear the search flags. */
1325 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
1327 ps
->searched_flag
= PST_NOT_SEARCHED
;
1330 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
1337 /* We skip shared psymtabs because file-matching doesn't apply
1338 to them; but we search them later in the loop. */
1339 if (ps
->user
!= NULL
)
1349 match
= file_matcher (ps
->filename
, false);
1352 /* Before we invoke realpath, which can get expensive when many
1353 files are involved, do a quick comparison of the basenames. */
1354 if (basenames_may_differ
1355 || file_matcher (lbasename (ps
->filename
), true))
1356 match
= file_matcher (psymtab_to_fullname (ps
), false);
1362 if (recursively_search_psymtabs (ps
, objfile
, domain
,
1363 lookup_name
, symbol_matcher
))
1365 struct compunit_symtab
*symtab
=
1366 psymtab_to_symtab (objfile
, ps
);
1368 if (expansion_notify
!= NULL
)
1369 expansion_notify (symtab
);
1374 /* Psymtab version of has_symbols. See its definition in
1375 the definition of quick_symbol_functions in symfile.h. */
1378 psym_has_symbols (struct objfile
*objfile
)
1380 return objfile
->psymtabs
!= NULL
;
1383 /* Helper function for psym_find_compunit_symtab_by_address that fills
1384 in psymbol_map for a given range of psymbols. */
1387 psym_fill_psymbol_map (struct objfile
*objfile
,
1388 struct partial_symtab
*psymtab
,
1389 std::set
<CORE_ADDR
> *seen_addrs
,
1390 const std::vector
<partial_symbol
*> &symbols
,
1394 for (int i
= 0; i
< length
; ++i
)
1396 struct partial_symbol
*psym
= symbols
[start
+ i
];
1398 if (psym
->aclass
== LOC_STATIC
)
1400 CORE_ADDR addr
= psym
->address (objfile
);
1401 if (seen_addrs
->find (addr
) == seen_addrs
->end ())
1403 seen_addrs
->insert (addr
);
1404 objfile
->psymbol_map
.emplace_back (addr
, psymtab
);
1410 /* See find_compunit_symtab_by_address in quick_symbol_functions, in
1413 static compunit_symtab
*
1414 psym_find_compunit_symtab_by_address (struct objfile
*objfile
,
1417 if (objfile
->psymbol_map
.empty ())
1419 struct partial_symtab
*pst
;
1421 std::set
<CORE_ADDR
> seen_addrs
;
1423 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, pst
)
1425 psym_fill_psymbol_map (objfile
, pst
,
1427 objfile
->global_psymbols
,
1428 pst
->globals_offset
,
1429 pst
->n_global_syms
);
1430 psym_fill_psymbol_map (objfile
, pst
,
1432 objfile
->static_psymbols
,
1433 pst
->statics_offset
,
1434 pst
->n_static_syms
);
1437 objfile
->psymbol_map
.shrink_to_fit ();
1439 std::sort (objfile
->psymbol_map
.begin (), objfile
->psymbol_map
.end (),
1440 [] (const std::pair
<CORE_ADDR
, partial_symtab
*> &a
,
1441 const std::pair
<CORE_ADDR
, partial_symtab
*> &b
)
1443 return a
.first
< b
.first
;
1447 auto iter
= std::lower_bound
1448 (objfile
->psymbol_map
.begin (), objfile
->psymbol_map
.end (), address
,
1449 [] (const std::pair
<CORE_ADDR
, partial_symtab
*> &a
,
1455 if (iter
== objfile
->psymbol_map
.end () || iter
->first
!= address
)
1458 return psymtab_to_symtab (objfile
, iter
->second
);
1461 const struct quick_symbol_functions psym_functions
=
1464 psym_find_last_source_symtab
,
1465 psym_forget_cached_source_info
,
1466 psym_map_symtabs_matching_filename
,
1470 psym_expand_symtabs_for_function
,
1471 psym_expand_all_symtabs
,
1472 psym_expand_symtabs_with_fullname
,
1473 psym_map_matching_symbols
,
1474 psym_expand_symtabs_matching
,
1475 psym_find_pc_sect_compunit_symtab
,
1476 psym_find_compunit_symtab_by_address
,
1477 psym_map_symbol_filenames
1483 sort_pst_symbols (struct objfile
*objfile
, struct partial_symtab
*pst
)
1485 /* Sort the global list; don't sort the static list. */
1486 auto begin
= objfile
->global_psymbols
.begin ();
1487 std::advance (begin
, pst
->globals_offset
);
1489 /* The psymbols for this partial_symtab are currently at the end of the
1491 auto end
= objfile
->global_psymbols
.end ();
1493 std::sort (begin
, end
, [] (partial_symbol
*s1
, partial_symbol
*s2
)
1495 return strcmp_iw_ordered (symbol_search_name (s1
),
1496 symbol_search_name (s2
)) < 0;
1500 /* Allocate and partially fill a partial symtab. It will be
1501 completely filled at the end of the symbol list.
1503 FILENAME is the name of the symbol-file we are reading from. */
1505 struct partial_symtab
*
1506 start_psymtab_common (struct objfile
*objfile
,
1507 const char *filename
,
1509 std::vector
<partial_symbol
*> &global_psymbols
,
1510 std::vector
<partial_symbol
*> &static_psymbols
)
1512 struct partial_symtab
*psymtab
;
1514 psymtab
= allocate_psymtab (filename
, objfile
);
1515 psymtab
->set_text_low (textlow
);
1516 psymtab
->set_text_high (psymtab
->raw_text_low ()); /* default */
1517 psymtab
->globals_offset
= global_psymbols
.size ();
1518 psymtab
->statics_offset
= static_psymbols
.size ();
1522 /* Perform "finishing up" operations of a partial symtab. */
1525 end_psymtab_common (struct objfile
*objfile
, struct partial_symtab
*pst
)
1527 pst
->n_global_syms
= objfile
->global_psymbols
.size () - pst
->globals_offset
;
1528 pst
->n_static_syms
= objfile
->static_psymbols
.size () - pst
->statics_offset
;
1530 sort_pst_symbols (objfile
, pst
);
1533 /* Calculate a hash code for the given partial symbol. The hash is
1534 calculated using the symbol's value, language, domain, class
1535 and name. These are the values which are set by
1536 add_psymbol_to_bcache. */
1538 static unsigned long
1539 psymbol_hash (const void *addr
, int length
)
1541 unsigned long h
= 0;
1542 struct partial_symbol
*psymbol
= (struct partial_symbol
*) addr
;
1543 unsigned int lang
= psymbol
->language
;
1544 unsigned int domain
= psymbol
->domain
;
1545 unsigned int theclass
= psymbol
->aclass
;
1547 h
= hash_continue (&psymbol
->value
, sizeof (psymbol
->value
), h
);
1548 h
= hash_continue (&lang
, sizeof (unsigned int), h
);
1549 h
= hash_continue (&domain
, sizeof (unsigned int), h
);
1550 h
= hash_continue (&theclass
, sizeof (unsigned int), h
);
1551 /* Note that psymbol names are interned via symbol_set_names, so
1552 there's no need to hash the contents of the name here. */
1553 h
= hash_continue (&psymbol
->name
,
1554 sizeof (psymbol
->name
), h
);
1559 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1560 For the comparison this function uses a symbols value,
1561 language, domain, class and name. */
1564 psymbol_compare (const void *addr1
, const void *addr2
, int length
)
1566 struct partial_symbol
*sym1
= (struct partial_symbol
*) addr1
;
1567 struct partial_symbol
*sym2
= (struct partial_symbol
*) addr2
;
1569 return (memcmp (&sym1
->value
, &sym2
->value
,
1570 sizeof (sym1
->value
)) == 0
1571 && sym1
->language
== sym2
->language
1572 && sym1
->domain
== sym2
->domain
1573 && sym1
->aclass
== sym2
->aclass
1574 /* Note that psymbol names are interned via
1575 symbol_set_names, so there's no need to compare the
1576 contents of the name here. */
1577 && sym1
->name
== sym2
->name
);
1580 /* Initialize a partial symbol bcache. */
1582 struct psymbol_bcache
*
1583 psymbol_bcache_init (void)
1585 struct psymbol_bcache
*bcache
= XCNEW (struct psymbol_bcache
);
1587 bcache
->bcache
= bcache_xmalloc (psymbol_hash
, psymbol_compare
);
1591 /* Free a partial symbol bcache. */
1594 psymbol_bcache_free (struct psymbol_bcache
*bcache
)
1599 bcache_xfree (bcache
->bcache
);
1603 /* Return the internal bcache of the psymbol_bcache BCACHE. */
1606 psymbol_bcache_get_bcache (struct psymbol_bcache
*bcache
)
1608 return bcache
->bcache
;
1611 /* Find a copy of the SYM in BCACHE. If BCACHE has never seen this
1612 symbol before, add a copy to BCACHE. In either case, return a pointer
1613 to BCACHE's copy of the symbol. If optional ADDED is not NULL, return
1614 1 in case of new entry or 0 if returning an old entry. */
1616 static struct partial_symbol
*
1617 psymbol_bcache_full (struct partial_symbol
*sym
,
1618 struct psymbol_bcache
*bcache
,
1621 return ((struct partial_symbol
*)
1622 bcache_full (sym
, sizeof (struct partial_symbol
), bcache
->bcache
,
1626 /* Helper function, initialises partial symbol structure and stashes
1627 it into objfile's bcache. Note that our caching mechanism will
1628 use all fields of struct partial_symbol to determine hash value of the
1629 structure. In other words, having two symbols with the same name but
1630 different domain (or address) is possible and correct. */
1632 static struct partial_symbol
*
1633 add_psymbol_to_bcache (const char *name
, int namelength
, int copy_name
,
1635 enum address_class theclass
,
1638 enum language language
, struct objfile
*objfile
,
1641 struct partial_symbol psymbol
;
1643 psymbol
.set_unrelocated_address (coreaddr
);
1644 psymbol
.section
= section
;
1645 psymbol
.domain
= domain
;
1646 psymbol
.aclass
= theclass
;
1648 memset (&psymbol
.language_specific
, 0, sizeof (psymbol
.language_specific
));
1649 psymbol
.ada_mangled
= 0;
1650 symbol_set_language (&psymbol
, language
, &objfile
->objfile_obstack
);
1651 symbol_set_names (&psymbol
, name
, namelength
, copy_name
, objfile
);
1653 /* Stash the partial symbol away in the cache. */
1654 return psymbol_bcache_full (&psymbol
, objfile
->psymbol_cache
, added
);
1657 /* Helper function, adds partial symbol to the given partial symbol list. */
1660 append_psymbol_to_list (std::vector
<partial_symbol
*> *list
,
1661 struct partial_symbol
*psym
,
1662 struct objfile
*objfile
)
1664 list
->push_back (psym
);
1665 OBJSTAT (objfile
, n_psyms
++);
1668 /* Add a symbol with a long value to a psymtab.
1669 Since one arg is a struct, we pass in a ptr and deref it (sigh).
1670 The only value we need to store for psyms is an address.
1671 For all other psyms pass zero for COREADDR.
1672 Return the partial symbol that has been added. */
1675 add_psymbol_to_list (const char *name
, int namelength
, int copy_name
,
1677 enum address_class theclass
,
1679 std::vector
<partial_symbol
*> *list
,
1681 enum language language
, struct objfile
*objfile
)
1683 struct partial_symbol
*psym
;
1687 /* Stash the partial symbol away in the cache. */
1688 psym
= add_psymbol_to_bcache (name
, namelength
, copy_name
, domain
, theclass
,
1689 section
, coreaddr
, language
, objfile
, &added
);
1691 /* Do not duplicate global partial symbols. */
1692 if (list
== &objfile
->global_psymbols
1696 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1697 append_psymbol_to_list (list
, psym
, objfile
);
1700 /* Initialize storage for partial symbols. */
1703 init_psymbol_list (struct objfile
*objfile
, int total_symbols
)
1705 /* Free any previously allocated psymbol lists. */
1706 objfile
->global_psymbols
.clear ();
1707 objfile
->static_psymbols
.clear ();
1709 /* Current best guess is that approximately a twentieth
1710 of the total symbols (in a debugging file) are global or static
1711 oriented symbols, then multiply that by slop factor of two. */
1712 objfile
->global_psymbols
.reserve (total_symbols
/ 10);
1713 objfile
->static_psymbols
.reserve (total_symbols
/ 10);
1716 struct partial_symtab
*
1717 allocate_psymtab (const char *filename
, struct objfile
*objfile
)
1719 struct partial_symtab
*psymtab
;
1721 if (objfile
->free_psymtabs
)
1723 psymtab
= objfile
->free_psymtabs
;
1724 objfile
->free_psymtabs
= psymtab
->next
;
1727 psymtab
= XOBNEW (&objfile
->objfile_obstack
, partial_symtab
);
1729 memset (psymtab
, 0, sizeof (struct partial_symtab
));
1731 = (const char *) bcache (filename
, strlen (filename
) + 1,
1732 objfile
->per_bfd
->filename_cache
);
1733 psymtab
->compunit_symtab
= NULL
;
1735 /* Prepend it to the psymtab list for the objfile it belongs to.
1736 Psymtabs are searched in most recent inserted -> least recent
1739 psymtab
->next
= objfile
->psymtabs
;
1740 objfile
->psymtabs
= psymtab
;
1742 if (symtab_create_debug
)
1744 /* Be a bit clever with debugging messages, and don't print objfile
1745 every time, only when it changes. */
1746 static char *last_objfile_name
= NULL
;
1748 if (last_objfile_name
== NULL
1749 || strcmp (last_objfile_name
, objfile_name (objfile
)) != 0)
1751 xfree (last_objfile_name
);
1752 last_objfile_name
= xstrdup (objfile_name (objfile
));
1753 fprintf_filtered (gdb_stdlog
,
1754 "Creating one or more psymtabs for objfile %s ...\n",
1757 fprintf_filtered (gdb_stdlog
,
1758 "Created psymtab %s for module %s.\n",
1759 host_address_to_string (psymtab
), filename
);
1766 discard_psymtab (struct objfile
*objfile
, struct partial_symtab
*pst
)
1768 struct partial_symtab
**prev_pst
;
1771 Empty psymtabs happen as a result of header files which don't
1772 have any symbols in them. There can be a lot of them. But this
1773 check is wrong, in that a psymtab with N_SLINE entries but
1774 nothing else is not empty, but we don't realize that. Fixing
1775 that without slowing things down might be tricky. */
1777 /* First, snip it out of the psymtab chain. */
1779 prev_pst
= &(objfile
->psymtabs
);
1780 while ((*prev_pst
) != pst
)
1781 prev_pst
= &((*prev_pst
)->next
);
1782 (*prev_pst
) = pst
->next
;
1784 /* Next, put it on a free list for recycling. */
1786 pst
->next
= objfile
->free_psymtabs
;
1787 objfile
->free_psymtabs
= pst
;
1792 /* We need to pass a couple of items to the addrmap_foreach function,
1795 struct dump_psymtab_addrmap_data
1797 struct objfile
*objfile
;
1798 struct partial_symtab
*psymtab
;
1799 struct ui_file
*outfile
;
1801 /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1802 If so, we want to print the next one as well (since the next addrmap
1803 entry defines the end of the range). */
1804 int previous_matched
;
1807 /* Helper function for dump_psymtab_addrmap to print an addrmap entry. */
1810 dump_psymtab_addrmap_1 (void *datap
, CORE_ADDR start_addr
, void *obj
)
1812 struct dump_psymtab_addrmap_data
*data
1813 = (struct dump_psymtab_addrmap_data
*) datap
;
1814 struct gdbarch
*gdbarch
= get_objfile_arch (data
->objfile
);
1815 struct partial_symtab
*addrmap_psymtab
= (struct partial_symtab
*) obj
;
1816 const char *psymtab_address_or_end
= NULL
;
1820 if (data
->psymtab
== NULL
1821 || data
->psymtab
== addrmap_psymtab
)
1822 psymtab_address_or_end
= host_address_to_string (addrmap_psymtab
);
1823 else if (data
->previous_matched
)
1824 psymtab_address_or_end
= "<ends here>";
1826 if (data
->psymtab
== NULL
1827 || data
->psymtab
== addrmap_psymtab
1828 || data
->previous_matched
)
1830 fprintf_filtered (data
->outfile
, " %s%s %s\n",
1831 data
->psymtab
!= NULL
? " " : "",
1832 paddress (gdbarch
, start_addr
),
1833 psymtab_address_or_end
);
1836 data
->previous_matched
= (data
->psymtab
== NULL
1837 || data
->psymtab
== addrmap_psymtab
);
1842 /* Helper function for maintenance_print_psymbols to print the addrmap
1843 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1846 dump_psymtab_addrmap (struct objfile
*objfile
, struct partial_symtab
*psymtab
,
1847 struct ui_file
*outfile
)
1849 struct dump_psymtab_addrmap_data addrmap_dump_data
;
1851 if ((psymtab
== NULL
1852 || psymtab
->psymtabs_addrmap_supported
)
1853 && objfile
->psymtabs_addrmap
!= NULL
)
1855 addrmap_dump_data
.objfile
= objfile
;
1856 addrmap_dump_data
.psymtab
= psymtab
;
1857 addrmap_dump_data
.outfile
= outfile
;
1858 addrmap_dump_data
.previous_matched
= 0;
1859 fprintf_filtered (outfile
, "%sddress map:\n",
1860 psymtab
== NULL
? "Entire a" : " A");
1861 addrmap_foreach (objfile
->psymtabs_addrmap
, dump_psymtab_addrmap_1
,
1862 &addrmap_dump_data
);
1867 maintenance_print_psymbols (const char *args
, int from_tty
)
1869 struct ui_file
*outfile
= gdb_stdout
;
1870 char *address_arg
= NULL
, *source_arg
= NULL
, *objfile_arg
= NULL
;
1871 struct partial_symtab
*ps
;
1872 int i
, outfile_idx
, found
;
1874 struct obj_section
*section
= NULL
;
1878 gdb_argv
argv (args
);
1880 for (i
= 0; argv
!= NULL
&& argv
[i
] != NULL
; ++i
)
1882 if (strcmp (argv
[i
], "-pc") == 0)
1884 if (argv
[i
+ 1] == NULL
)
1885 error (_("Missing pc value"));
1886 address_arg
= argv
[++i
];
1888 else if (strcmp (argv
[i
], "-source") == 0)
1890 if (argv
[i
+ 1] == NULL
)
1891 error (_("Missing source file"));
1892 source_arg
= argv
[++i
];
1894 else if (strcmp (argv
[i
], "-objfile") == 0)
1896 if (argv
[i
+ 1] == NULL
)
1897 error (_("Missing objfile name"));
1898 objfile_arg
= argv
[++i
];
1900 else if (strcmp (argv
[i
], "--") == 0)
1902 /* End of options. */
1906 else if (argv
[i
][0] == '-')
1908 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1909 error (_("Unknown option: %s"), argv
[i
]);
1916 if (address_arg
!= NULL
&& source_arg
!= NULL
)
1917 error (_("Must specify at most one of -pc and -source"));
1919 stdio_file arg_outfile
;
1921 if (argv
!= NULL
&& argv
[outfile_idx
] != NULL
)
1923 if (argv
[outfile_idx
+ 1] != NULL
)
1924 error (_("Junk at end of command"));
1925 gdb::unique_xmalloc_ptr
<char> outfile_name
1926 (tilde_expand (argv
[outfile_idx
]));
1927 if (!arg_outfile
.open (outfile_name
.get (), FOPEN_WT
))
1928 perror_with_name (outfile_name
.get ());
1929 outfile
= &arg_outfile
;
1932 if (address_arg
!= NULL
)
1934 pc
= parse_and_eval_address (address_arg
);
1935 /* If we fail to find a section, that's ok, try the lookup anyway. */
1936 section
= find_pc_section (pc
);
1940 for (objfile
*objfile
: all_objfiles (current_program_space
))
1942 int printed_objfile_header
= 0;
1943 int print_for_objfile
= 1;
1946 if (objfile_arg
!= NULL
)
1948 = compare_filenames_for_search (objfile_name (objfile
),
1950 if (!print_for_objfile
)
1953 if (address_arg
!= NULL
)
1955 struct bound_minimal_symbol msymbol
= { NULL
, NULL
};
1957 /* We don't assume each pc has a unique objfile (this is for
1959 ps
= find_pc_sect_psymtab (objfile
, pc
, section
, msymbol
);
1962 if (!printed_objfile_header
)
1964 outfile
->printf ("\nPartial symtabs for objfile %s\n",
1965 objfile_name (objfile
));
1966 printed_objfile_header
= 1;
1968 dump_psymtab (objfile
, ps
, outfile
);
1969 dump_psymtab_addrmap (objfile
, ps
, outfile
);
1975 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
1977 int print_for_source
= 0;
1980 if (source_arg
!= NULL
)
1983 = compare_filenames_for_search (ps
->filename
, source_arg
);
1986 if (source_arg
== NULL
1987 || print_for_source
)
1989 if (!printed_objfile_header
)
1991 outfile
->printf ("\nPartial symtabs for objfile %s\n",
1992 objfile_name (objfile
));
1993 printed_objfile_header
= 1;
1995 dump_psymtab (objfile
, ps
, outfile
);
1996 dump_psymtab_addrmap (objfile
, ps
, outfile
);
2001 /* If we're printing all the objfile's symbols dump the full addrmap. */
2003 if (address_arg
== NULL
2004 && source_arg
== NULL
2005 && objfile
->psymtabs_addrmap
!= NULL
)
2007 outfile
->puts ("\n");
2008 dump_psymtab_addrmap (objfile
, NULL
, outfile
);
2014 if (address_arg
!= NULL
)
2015 error (_("No partial symtab for address: %s"), address_arg
);
2016 if (source_arg
!= NULL
)
2017 error (_("No partial symtab for source file: %s"), source_arg
);
2021 /* List all the partial symbol tables whose names match REGEXP (optional). */
2024 maintenance_info_psymtabs (const char *regexp
, int from_tty
)
2026 struct program_space
*pspace
;
2031 ALL_PSPACES (pspace
)
2032 for (objfile
*objfile
: all_objfiles (pspace
))
2034 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
2035 struct partial_symtab
*psymtab
;
2037 /* We don't want to print anything for this objfile until we
2038 actually find a symtab whose name matches. */
2039 int printed_objfile_start
= 0;
2041 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, psymtab
)
2046 || re_exec (psymtab
->filename
))
2048 if (! printed_objfile_start
)
2050 printf_filtered ("{ objfile %s ", objfile_name (objfile
));
2052 printf_filtered ("((struct objfile *) %s)\n",
2053 host_address_to_string (objfile
));
2054 printed_objfile_start
= 1;
2057 printf_filtered (" { psymtab %s ", psymtab
->filename
);
2059 printf_filtered ("((struct partial_symtab *) %s)\n",
2060 host_address_to_string (psymtab
));
2062 printf_filtered (" readin %s\n",
2063 psymtab
->readin
? "yes" : "no");
2064 printf_filtered (" fullname %s\n",
2066 ? psymtab
->fullname
: "(null)");
2067 printf_filtered (" text addresses ");
2068 fputs_filtered (paddress (gdbarch
,
2069 psymtab
->text_low (objfile
)),
2071 printf_filtered (" -- ");
2072 fputs_filtered (paddress (gdbarch
,
2073 psymtab
->text_high (objfile
)),
2075 printf_filtered ("\n");
2076 printf_filtered (" psymtabs_addrmap_supported %s\n",
2077 (psymtab
->psymtabs_addrmap_supported
2079 printf_filtered (" globals ");
2080 if (psymtab
->n_global_syms
)
2083 = &objfile
->global_psymbols
[psymtab
->globals_offset
];
2086 ("(* (struct partial_symbol **) %s @ %d)\n",
2087 host_address_to_string (p
),
2088 psymtab
->n_global_syms
);
2091 printf_filtered ("(none)\n");
2092 printf_filtered (" statics ");
2093 if (psymtab
->n_static_syms
)
2096 = &objfile
->static_psymbols
[psymtab
->statics_offset
];
2099 ("(* (struct partial_symbol **) %s @ %d)\n",
2100 host_address_to_string (p
),
2101 psymtab
->n_static_syms
);
2104 printf_filtered ("(none)\n");
2105 printf_filtered (" dependencies ");
2106 if (psymtab
->number_of_dependencies
)
2110 printf_filtered ("{\n");
2111 for (i
= 0; i
< psymtab
->number_of_dependencies
; i
++)
2113 struct partial_symtab
*dep
= psymtab
->dependencies
[i
];
2115 /* Note the string concatenation there --- no
2117 printf_filtered (" psymtab %s "
2118 "((struct partial_symtab *) %s)\n",
2120 host_address_to_string (dep
));
2122 printf_filtered (" }\n");
2125 printf_filtered ("(none)\n");
2126 printf_filtered (" }\n");
2130 if (printed_objfile_start
)
2131 printf_filtered ("}\n");
2135 /* Check consistency of currently expanded psymtabs vs symtabs. */
2138 maintenance_check_psymtabs (const char *ignore
, int from_tty
)
2141 struct compunit_symtab
*cust
= NULL
;
2142 struct partial_symtab
*ps
;
2143 const struct blockvector
*bv
;
2147 for (objfile
*objfile
: all_objfiles (current_program_space
))
2148 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
2150 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
2152 /* We don't call psymtab_to_symtab here because that may cause symtab
2153 expansion. When debugging a problem it helps if checkers leave
2154 things unchanged. */
2155 cust
= ps
->compunit_symtab
;
2157 /* First do some checks that don't require the associated symtab. */
2158 if (ps
->text_high (objfile
) < ps
->text_low (objfile
))
2160 printf_filtered ("Psymtab ");
2161 puts_filtered (ps
->filename
);
2162 printf_filtered (" covers bad range ");
2163 fputs_filtered (paddress (gdbarch
, ps
->text_low (objfile
)),
2165 printf_filtered (" - ");
2166 fputs_filtered (paddress (gdbarch
, ps
->text_high (objfile
)),
2168 printf_filtered ("\n");
2172 /* Now do checks requiring the associated symtab. */
2175 bv
= COMPUNIT_BLOCKVECTOR (cust
);
2176 b
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
2177 partial_symbol
**psym
= &objfile
->static_psymbols
[ps
->statics_offset
];
2178 length
= ps
->n_static_syms
;
2181 sym
= block_lookup_symbol (b
, symbol_search_name (*psym
),
2182 symbol_name_match_type::SEARCH_NAME
,
2186 printf_filtered ("Static symbol `");
2187 puts_filtered ((*psym
)->name
);
2188 printf_filtered ("' only found in ");
2189 puts_filtered (ps
->filename
);
2190 printf_filtered (" psymtab\n");
2194 b
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
2195 psym
= &objfile
->global_psymbols
[ps
->globals_offset
];
2196 length
= ps
->n_global_syms
;
2199 sym
= block_lookup_symbol (b
, symbol_search_name (*psym
),
2200 symbol_name_match_type::SEARCH_NAME
,
2204 printf_filtered ("Global symbol `");
2205 puts_filtered ((*psym
)->name
);
2206 printf_filtered ("' only found in ");
2207 puts_filtered (ps
->filename
);
2208 printf_filtered (" psymtab\n");
2212 if (ps
->raw_text_high () != 0
2213 && (ps
->text_low (objfile
) < BLOCK_START (b
)
2214 || ps
->text_high (objfile
) > BLOCK_END (b
)))
2216 printf_filtered ("Psymtab ");
2217 puts_filtered (ps
->filename
);
2218 printf_filtered (" covers ");
2219 fputs_filtered (paddress (gdbarch
, ps
->text_low (objfile
)),
2221 printf_filtered (" - ");
2222 fputs_filtered (paddress (gdbarch
, ps
->text_high (objfile
)),
2224 printf_filtered (" but symtab covers only ");
2225 fputs_filtered (paddress (gdbarch
, BLOCK_START (b
)), gdb_stdout
);
2226 printf_filtered (" - ");
2227 fputs_filtered (paddress (gdbarch
, BLOCK_END (b
)), gdb_stdout
);
2228 printf_filtered ("\n");
2234 _initialize_psymtab (void)
2236 add_cmd ("psymbols", class_maintenance
, maintenance_print_psymbols
, _("\
2237 Print dump of current partial symbol definitions.\n\
2238 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
2239 mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
2240 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2241 or the terminal if OUTFILE is unspecified.\n\
2242 If ADDRESS is provided, dump only the file for that address.\n\
2243 If SOURCE is provided, dump only that file's symbols.\n\
2244 If OBJFILE is provided, dump only that file's minimal symbols."),
2245 &maintenanceprintlist
);
2247 add_cmd ("psymtabs", class_maintenance
, maintenance_info_psymtabs
, _("\
2248 List the partial symbol tables for all object files.\n\
2249 This does not include information about individual partial symbols,\n\
2250 just the symbol table structures themselves."),
2251 &maintenanceinfolist
);
2253 add_cmd ("check-psymtabs", class_maintenance
, maintenance_check_psymtabs
,
2255 Check consistency of currently expanded psymtabs versus symtabs."),