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