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