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