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