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