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