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