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