Various fixes to improve g++ debugging. See ChangeLog.
[binutils-gdb.git] / gdb / symfile.c
1 /* Generic symbol file reading for the GNU debugger, GDB.
2 Copyright 1990, 1991 Free Software Foundation, Inc.
3 Contributed by Cygnus Support, using pieces from other GDB modules.
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include <stdio.h>
22 #include "defs.h"
23 #include "symtab.h"
24 #include "param.h"
25 #include "gdbcore.h"
26 #include "frame.h"
27 #include "target.h"
28 #include "value.h"
29 #include "symfile.h"
30 #include "gdbcmd.h"
31 #include "breakpoint.h"
32
33 #include <obstack.h>
34 #include <assert.h>
35
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <string.h>
39 #include <sys/stat.h>
40
41 extern int info_verbose;
42
43 extern void qsort ();
44 extern char *getenv ();
45 extern char *rindex ();
46
47 /* Functions this file defines */
48 static bfd *symfile_open();
49 static struct sym_fns *symfile_init();
50 static void clear_symtab_users_once();
51
52 /* List of all available sym_fns. */
53
54 struct sym_fns *symtab_fns = NULL;
55
56 /* Saves the sym_fns of the current symbol table, so we can call
57 the right XXX_new_init function when we free it. FIXME. This
58 should be extended to calling the new_init function for each
59 existing symtab or psymtab, since the main symbol file and
60 subsequent added symbol files can have different types. */
61
62 static struct sym_fns *symfile_fns;
63
64 /* Allocate an obstack to hold objects that should be freed
65 when we load a new symbol table.
66 This includes the symbols made by dbxread
67 and the types that are not permanent. */
68
69 struct obstack obstack1;
70
71 struct obstack *symbol_obstack = &obstack1;
72
73 /* This obstack will be used for partial_symbol objects. It can
74 probably actually be the same as the symbol_obstack above, but I'd
75 like to keep them seperate for now. If I want to later, I'll
76 replace one with the other. */
77
78 struct obstack obstack2;
79
80 struct obstack *psymbol_obstack = &obstack2;
81
82 /* File name symbols were loaded from. */
83
84 char *symfile = 0;
85
86 /* The modification date of the file when they were loaded. */
87
88 long /* really time_t */ symfile_mtime = 0;
89
90 /* Structures with which to manage partial symbol allocation. */
91
92 struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
93
94 /* Flag for whether user will be reloading symbols multiple times.
95 Defaults to ON for VxWorks, otherwise OFF. */
96
97 #ifdef SYMBOL_RELOADING_DEFAULT
98 int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
99 #else
100 int symbol_reloading = 0;
101 #endif
102
103 /* Structure to manage complaints about symbol file contents. */
104
105 struct complaint complaint_root[1] = {
106 {(char *)0, 0, complaint_root},
107 };
108
109 /* Some actual complaints. */
110
111 struct complaint oldsyms_complaint = {
112 "Replacing old symbols for `%s'", 0, 0 };
113
114 struct complaint empty_symtab_complaint = {
115 "Empty symbol table found for `%s'", 0, 0 };
116
117 \f
118 /* In the following sort, we always make sure that
119 register debug symbol declarations always come before regular
120 debug symbol declarations (as might happen when parameters are
121 then put into registers by the compiler). */
122
123 static int
124 compare_symbols (s1, s2)
125 struct symbol **s1, **s2;
126 {
127 register int namediff;
128
129 /* Compare the initial characters. */
130 namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
131 if (namediff != 0) return namediff;
132
133 /* If they match, compare the rest of the names. */
134 namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
135 if (namediff != 0) return namediff;
136
137 /* For symbols of the same name, registers should come first. */
138 return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
139 - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
140 }
141
142 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
143
144 void
145 sort_block_syms (b)
146 register struct block *b;
147 {
148 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
149 sizeof (struct symbol *), compare_symbols);
150 }
151
152 /* Call sort_symtab_syms to sort alphabetically
153 the symbols of each block of one symtab. */
154
155 void
156 sort_symtab_syms (s)
157 register struct symtab *s;
158 {
159 register struct blockvector *bv = BLOCKVECTOR (s);
160 int nbl = BLOCKVECTOR_NBLOCKS (bv);
161 int i;
162 register struct block *b;
163
164 for (i = 0; i < nbl; i++)
165 {
166 b = BLOCKVECTOR_BLOCK (bv, i);
167 if (BLOCK_SHOULD_SORT (b))
168 sort_block_syms (b);
169 }
170 }
171
172 void
173 sort_all_symtab_syms ()
174 {
175 register struct symtab *s;
176
177 for (s = symtab_list; s; s = s->next)
178 {
179 sort_symtab_syms (s);
180 }
181 }
182
183 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
184 (and add a null character at the end in the copy).
185 Returns the address of the copy. */
186
187 char *
188 obsavestring (ptr, size)
189 char *ptr;
190 int size;
191 {
192 register char *p = (char *) obstack_alloc (symbol_obstack, size + 1);
193 /* Open-coded bcopy--saves function call time.
194 These strings are usually short. */
195 {
196 register char *p1 = ptr;
197 register char *p2 = p;
198 char *end = ptr + size;
199 while (p1 != end)
200 *p2++ = *p1++;
201 }
202 p[size] = 0;
203 return p;
204 }
205
206 /* Concatenate strings S1, S2 and S3; return the new string.
207 Space is found in the symbol_obstack. */
208
209 char *
210 obconcat (s1, s2, s3)
211 char *s1, *s2, *s3;
212 {
213 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
214 register char *val = (char *) obstack_alloc (symbol_obstack, len);
215 strcpy (val, s1);
216 strcat (val, s2);
217 strcat (val, s3);
218 return val;
219 }
220 \f
221 /* Accumulate the misc functions in bunches of 127.
222 At the end, copy them all into one newly allocated structure. */
223
224 #define MISC_BUNCH_SIZE 127
225
226 struct misc_bunch
227 {
228 struct misc_bunch *next;
229 struct misc_function contents[MISC_BUNCH_SIZE];
230 };
231
232 /* Bunch currently being filled up.
233 The next field points to chain of filled bunches. */
234
235 static struct misc_bunch *misc_bunch;
236
237 /* Number of slots filled in current bunch. */
238
239 static int misc_bunch_index;
240
241 /* Total number of misc functions recorded so far. */
242
243 static int misc_count;
244
245 void
246 init_misc_bunches ()
247 {
248 misc_count = 0;
249 misc_bunch = 0;
250 misc_bunch_index = MISC_BUNCH_SIZE;
251 }
252
253 void
254 prim_record_misc_function (name, address, misc_type)
255 char *name;
256 CORE_ADDR address;
257 enum misc_function_type misc_type;
258 {
259 register struct misc_bunch *new;
260
261 if (misc_bunch_index == MISC_BUNCH_SIZE)
262 {
263 new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
264 misc_bunch_index = 0;
265 new->next = misc_bunch;
266 misc_bunch = new;
267 }
268 misc_bunch->contents[misc_bunch_index].name = name;
269 misc_bunch->contents[misc_bunch_index].address = address;
270 misc_bunch->contents[misc_bunch_index].type = misc_type;
271 misc_bunch->contents[misc_bunch_index].misc_info = 0;
272 misc_bunch_index++;
273 misc_count++;
274 }
275
276 static int
277 compare_misc_functions (fn1, fn2)
278 struct misc_function *fn1, *fn2;
279 {
280 /* Return a signed result based on unsigned comparisons
281 so that we sort into unsigned numeric order. */
282 if (fn1->address < fn2->address)
283 return -1;
284 if (fn1->address > fn2->address)
285 return 1;
286 return 0;
287 }
288
289 /* ARGSUSED */
290 void
291 discard_misc_bunches (foo)
292 int foo;
293 {
294 register struct misc_bunch *next;
295
296 while (misc_bunch)
297 {
298 next = misc_bunch->next;
299 free (misc_bunch);
300 misc_bunch = next;
301 }
302 }
303
304 /* INCLINK nonzero means bunches are from an incrementally-linked file.
305 Add them to the existing bunches.
306 Otherwise INCLINK is zero, and we start from scratch. */
307 void
308 condense_misc_bunches (inclink)
309 int inclink;
310 {
311 register int i, j;
312 register struct misc_bunch *bunch;
313
314 if (inclink)
315 {
316 misc_function_vector
317 = (struct misc_function *)
318 xrealloc (misc_function_vector, (misc_count + misc_function_count)
319 * sizeof (struct misc_function));
320 j = misc_function_count;
321 }
322 else
323 {
324 misc_function_vector
325 = (struct misc_function *)
326 xmalloc (misc_count * sizeof (struct misc_function));
327 j = 0;
328 }
329
330 bunch = misc_bunch;
331 while (bunch)
332 {
333 for (i = 0; i < misc_bunch_index; i++, j++)
334 {
335 misc_function_vector[j] = bunch->contents[i];
336 #ifdef NAMES_HAVE_UNDERSCORE
337 if (misc_function_vector[j].name[0] == '_')
338 misc_function_vector[j].name++;
339 #endif
340 }
341 bunch = bunch->next;
342 misc_bunch_index = MISC_BUNCH_SIZE;
343 }
344
345 if (misc_function_count + misc_count != j) /* DEBUG */
346 printf_filtered ("Function counts are off! %d + %d != %d\n",
347 misc_function_count, misc_count, j);
348
349 misc_function_count = j;
350
351 /* Sort the misc functions by address. */
352
353 qsort (misc_function_vector, misc_function_count,
354 sizeof (struct misc_function),
355 compare_misc_functions);
356 }
357
358
359 /* Get the symbol table that corresponds to a partial_symtab.
360 This is fast after the first time you do it. In fact, there
361 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
362 case inline. */
363
364 struct symtab *
365 psymtab_to_symtab (pst)
366 register struct partial_symtab *pst;
367 {
368 /* If it's been looked up before, return it. */
369 if (pst->symtab)
370 return pst->symtab;
371
372 /* If it has not yet been read in, read it. */
373 if (!pst->readin)
374 {
375 (*pst->read_symtab) (pst);
376 }
377
378 return pst->symtab;
379 }
380
381 /* Process a symbol file, as either the main file or as a dynamically
382 loaded file.
383
384 NAME is the file name (which will be tilde-expanded and made
385 absolute herein) (but we don't free or modify NAME itself).
386 FROM_TTY says how verbose to be. MAINLINE specifies whether this
387 is the main symbol file, or whether it's an extra symbol file such
388 as dynamically loaded code. If !mainline, ADDR is the address
389 where the text segment was loaded. */
390
391 void
392 symbol_file_add (name, from_tty, addr, mainline)
393 char *name;
394 int from_tty;
395 CORE_ADDR addr;
396 int mainline;
397 {
398 bfd *sym_bfd;
399 asection *text_sect;
400 struct sym_fns *sf;
401 char *realname;
402
403 sym_bfd = symfile_open (name);
404
405 entry_point = bfd_get_start_address (sym_bfd);
406
407 if (mainline)
408 symfile_mtime = bfd_get_mtime (sym_bfd);
409
410 /* There is a distinction between having no symbol table
411 (we refuse to read the file, leaving the old set of symbols around)
412 and having no debugging symbols in your symbol table (we read
413 the file and end up with a mostly empty symbol table). */
414
415 if (!(bfd_get_file_flags (sym_bfd) & HAS_SYMS))
416 {
417 error ("%s has no symbol-table", name);
418 }
419
420 if ((symtab_list || partial_symtab_list)
421 && mainline
422 && from_tty
423 && !query ("Load new symbol table from \"%s\"? ", name))
424 error ("Not confirmed.");
425
426 if (from_tty)
427 {
428 printf_filtered ("Reading symbols from %s...", name);
429 wrap_here ("");
430 fflush (stdout);
431 }
432
433 sf = symfile_init (sym_bfd);
434 realname = bfd_get_filename (sym_bfd);
435 realname = savestring (realname, strlen (realname));
436 /* FIXME, this probably creates a storage leak... */
437
438 if (mainline)
439 {
440 /* Since no error yet, throw away the old symbol table. */
441
442 if (symfile)
443 free (symfile);
444 symfile = 0;
445 free_all_symtabs ();
446 free_all_psymtabs ();
447
448 (*sf->sym_new_init) ();
449
450 /* For mainline, caller didn't know the specified address of the
451 text section. We fix that here. */
452 text_sect = bfd_get_section_by_name (sym_bfd, ".text");
453 addr = bfd_section_vma (sym_bfd, text_sect);
454 }
455
456 clear_complaints(); /* Allow complaints to appear for this new file. */
457
458 (*sf->sym_read) (sf, addr, mainline);
459
460 /* Don't allow char * to have a typename (else would get caddr_t.) */
461 /* Ditto void *. FIXME should do this for all the builtin types. */
462
463 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
464 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
465
466 if (mainline)
467 {
468 /* OK, make it the "real" symbol file. */
469 symfile = realname;
470 symfile_fns = sf;
471 }
472
473 /* If we have wiped out any old symbol tables, clean up. */
474 clear_symtab_users_once ();
475
476 if (from_tty)
477 {
478 printf_filtered ("done.\n");
479 fflush (stdout);
480 }
481 }
482
483 /* This is the symbol-file command. Read the file, analyze its symbols,
484 and add a struct symtab to symtab_list. */
485
486 void
487 symbol_file_command (name, from_tty)
488 char *name;
489 int from_tty;
490 {
491
492 dont_repeat ();
493
494 if (name == 0)
495 {
496 if ((symtab_list || partial_symtab_list)
497 && from_tty
498 && !query ("Discard symbol table from `%s'? ", symfile))
499 error ("Not confirmed.");
500 if (symfile)
501 free (symfile);
502 symfile = 0;
503 free_all_symtabs ();
504 free_all_psymtabs ();
505 /* FIXME, this does not account for the main file and subsequent
506 files (shared libs, dynloads, etc) having different formats.
507 It only calls the cleanup routine for the main file's format. */
508 if (symfile_fns) {
509 (*symfile_fns->sym_new_init) ();
510 free (symfile_fns);
511 symfile_fns = 0;
512 }
513 return;
514 }
515
516 /* Getting new symbols may change our opinion about what is
517 frameless. */
518 reinit_frame_cache ();
519
520 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1);
521 }
522
523 /* Open NAME and hand it off to BFD for preliminary analysis. Result
524 is a BFD *, which includes a new copy of NAME dynamically allocated
525 (which will be freed by the cleanup chain). In case of trouble,
526 error() is called. */
527
528 static bfd *
529 symfile_open (name)
530 char *name;
531 {
532 bfd *sym_bfd;
533 int desc;
534 char *absolute_name;
535
536 name = tilde_expand (name);
537 make_cleanup (free, name);
538
539 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
540 if (desc < 0)
541 perror_with_name (name);
542 else
543 {
544 make_cleanup (free, absolute_name);
545 name = absolute_name;
546 }
547
548 sym_bfd = bfd_fdopenr (name, NULL, desc);
549 if (!sym_bfd)
550 {
551 close (desc);
552 error ("Could not open `%s' to read symbols: %s",
553 name, bfd_errmsg (bfd_error));
554 }
555 make_cleanup (bfd_close, sym_bfd);
556
557 if (!bfd_check_format (sym_bfd, bfd_object))
558 error ("\"%s\": can't read symbols: %s.",
559 name, bfd_errmsg (bfd_error));
560
561 return sym_bfd;
562 }
563
564 /* Link a new symtab_fns into the global symtab_fns list.
565 Called by various _initialize routines. */
566
567 void
568 add_symtab_fns (sf)
569 struct sym_fns *sf;
570 {
571 sf->next = symtab_fns;
572 symtab_fns = sf;
573 }
574
575
576 /* Initialize to read symbols from the symbol file sym_bfd. It either
577 returns or calls error(). The result is a malloc'd struct sym_fns
578 that contains cached information about the symbol file. */
579
580 static struct sym_fns *
581 symfile_init (sym_bfd)
582 bfd *sym_bfd;
583 {
584 struct sym_fns *sf, *sf2;
585
586 for (sf = symtab_fns; sf != NULL; sf = sf->next)
587 {
588 if (!strncmp (bfd_get_target (sym_bfd), sf->sym_name, sf->sym_namelen))
589 {
590 sf2 = (struct sym_fns *)xmalloc (sizeof (*sf2));
591 /* FIXME, who frees this? */
592 *sf2 = *sf;
593 sf2->sym_bfd = sym_bfd;
594 sf2->sym_private = 0; /* Not alloc'd yet */
595 (*sf2->sym_init) (sf2);
596 return sf2;
597 }
598 }
599 error ("I'm sorry, Dave, I can't do that. Symbol format unknown.");
600 return 0; /* Appease lint. */
601 }
602 \f
603 /* This function runs the load command of our current target. */
604
605 void
606 load_command (arg, from_tty)
607 char *arg;
608 int from_tty;
609 {
610 target_load (arg, from_tty);
611 }
612
613 /* This function allows the addition of incrementally linked object files.
614 It does not modify any state in the target, only in the debugger. */
615
616 /* ARGSUSED */
617 void
618 add_symbol_file_command (arg_string, from_tty)
619 char *arg_string;
620 int from_tty;
621 {
622 char *name;
623 CORE_ADDR text_addr;
624
625 /* Getting new symbols may change our opinion about what is
626 frameless. */
627 reinit_frame_cache ();
628
629 if (arg_string == 0)
630 error ("add-symbol-file takes a file name and an address");
631
632 arg_string = tilde_expand (arg_string);
633 make_cleanup (free, arg_string);
634
635 for( ; *arg_string == ' '; arg_string++ );
636 name = arg_string;
637 for( ; *arg_string && *arg_string != ' ' ; arg_string++ );
638 *arg_string++ = (char) 0;
639
640 if (name[0] == 0)
641 error ("add-symbol-file takes a file name and an address");
642
643 text_addr = parse_and_eval_address (arg_string);
644
645 dont_repeat ();
646
647 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
648 name, local_hex_string (text_addr)))
649 error ("Not confirmed.");
650
651 symbol_file_add (name, 0, text_addr, 0);
652 }
653 \f
654 /* Re-read symbols if the symbol-file has changed. */
655 void
656 reread_symbols ()
657 {
658 struct stat symstat;
659
660 /* With the addition of shared libraries, this should be modified,
661 the load time should be saved in the partial symbol tables, since
662 different tables may come from different source files. FIXME.
663 This routine should then walk down each partial symbol table
664 and see if the symbol table that it originates from has been changed
665 */
666
667 if (stat (symfile, &symstat) < 0)
668 /* Can't read symbol-file. Assume it is up to date. */
669 return;
670
671 if (symstat.st_mtime > symfile_mtime)
672 {
673 printf_filtered ("Symbol file has changed; re-reading symbols.\n");
674 symbol_file_command (symfile, 0);
675 breakpoint_re_set ();
676 }
677 }
678
679 /* This function is really horrible, but to avoid it, there would need
680 to be more filling in of forward references. */
681 void
682 fill_in_vptr_fieldno (type)
683 struct type *type;
684 {
685 if (TYPE_VPTR_FIELDNO (type) < 0)
686 {
687 int i;
688 for (i = 1; i < TYPE_N_BASECLASSES (type); i++)
689 {
690 fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
691 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
692 {
693 TYPE_VPTR_FIELDNO (type)
694 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
695 TYPE_VPTR_BASETYPE (type)
696 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
697 break;
698 }
699 }
700 }
701 }
702 \f
703 /* Functions to handle complaints during symbol reading. */
704
705 /* How many complaints about a particular thing should be printed before
706 we stop whining about it? Default is no whining at all, since so many
707 systems have ill-constructed symbol files. */
708
709 static unsigned stop_whining = 0;
710
711 /* Print a complaint about the input symbols, and link the complaint block
712 into a chain for later handling. Result is 1 if the complaint was
713 printed, 0 if it was suppressed. */
714
715 int
716 complain (complaint, val)
717 struct complaint *complaint;
718 char *val;
719 {
720 complaint->counter++;
721 if (complaint->next == 0) {
722 complaint->next = complaint_root->next;
723 complaint_root->next = complaint;
724 }
725 if (complaint->counter > stop_whining)
726 return 0;
727 wrap_here ("");
728 if (!info_verbose) {
729 puts_filtered ("During symbol reading...");
730 }
731 printf_filtered (complaint->message, val);
732 puts_filtered ("...");
733 wrap_here("");
734 if (!info_verbose)
735 puts_filtered ("\n");
736 return 1;
737 }
738
739 /* Clear out all complaint counters that have ever been incremented. */
740
741 void
742 clear_complaints ()
743 {
744 struct complaint *p;
745
746 for (p = complaint_root->next; p != complaint_root; p = p->next)
747 p->counter = 0;
748 }
749 \f
750 /* allocate_symtab:
751
752 Allocate and partly initialize a new symbol table. Return a pointer
753 to it. error() if no space.
754
755 Caller must set these fields:
756 LINETABLE(symtab)
757 symtab->blockvector
758 symtab->typevector
759 symtab->dirname
760 symtab->free_code
761 symtab->free_ptr
762 initialize any EXTRA_SYMTAB_INFO
763 possibly free_named_symtabs (symtab->filename);
764 symtab->next = symtab_list;
765 symtab_list = symtab;
766 */
767
768 struct symtab *
769 allocate_symtab(name)
770 char *name;
771 {
772 register struct symtab *symtab;
773 char *c;
774
775 symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
776 bzero (symtab, sizeof (*symtab));
777 symtab->filename = name;
778 symtab->fullname = NULL;
779 symtab->nlines = 0;
780 symtab->line_charpos = 0;
781 symtab->version = 0;
782 symtab->language = language_unknown; /* default */
783
784 c = rindex (name, '.');
785
786 if (!c) {
787 ; /* Don't know language of file. */
788 } else if(!strcmp(c,".mod")) {
789 symtab->language = language_m2;
790 } else if(!strcmp(c,".c") || !strcmp(c,".cc")) {
791 symtab->language = language_c;
792 }
793
794 return symtab;
795 }
796 \f
797 /* clear_symtab_users_once:
798
799 This function is run after symbol reading, or from a cleanup.
800 If an old symbol table was obsoleted, the old symbol table
801 has been blown away, but the other GDB data structures that may
802 reference it have not yet been cleared or re-directed. (The old
803 symtab was zapped, and the cleanup queued, in free_named_symtab()
804 below.)
805
806 This function can be queued N times as a cleanup, or called
807 directly; it will do all the work the first time, and then will be a
808 no-op until the next time it is queued. This works by bumping a
809 counter at queueing time. Much later when the cleanup is run, or at
810 the end of symbol processing (in case the cleanup is discarded), if
811 the queued count is greater than the "done-count", we do the work
812 and set the done-count to the queued count. If the queued count is
813 less than or equal to the done-count, we just ignore the call. This
814 is needed because reading a single .o file will often replace many
815 symtabs (one per .h file, for example), and we don't want to reset
816 the breakpoints N times in the user's face.
817
818 The reason we both queue a cleanup, and call it directly after symbol
819 reading, is because the cleanup protects us in case of errors, but is
820 discarded if symbol reading is successful. */
821
822 static int clear_symtab_users_queued;
823 static int clear_symtab_users_done;
824
825 static void
826 clear_symtab_users_once ()
827 {
828 /* Enforce once-per-`do_cleanups'-semantics */
829 if (clear_symtab_users_queued <= clear_symtab_users_done)
830 return;
831 clear_symtab_users_done = clear_symtab_users_queued;
832
833 printf ("Resetting debugger state after updating old symbol tables\n");
834
835 /* Someday, we should do better than this, by only blowing away
836 the things that really need to be blown. */
837 clear_value_history ();
838 clear_displays ();
839 clear_internalvars ();
840 breakpoint_re_set ();
841 set_default_breakpoint (0, 0, 0, 0);
842 current_source_symtab = 0;
843 }
844
845 /* Delete the specified psymtab, and any others that reference it. */
846
847 static void
848 cashier_psymtab (pst)
849 struct partial_symtab *pst;
850 {
851 struct partial_symtab *ps, *pprev;
852 int i;
853
854 /* Find its previous psymtab in the chain */
855 for (ps = partial_symtab_list; ps; ps = ps->next) {
856 if (ps == pst)
857 break;
858 pprev = ps;
859 }
860
861 if (ps) {
862 /* Unhook it from the chain. */
863 if (ps == partial_symtab_list)
864 partial_symtab_list = ps->next;
865 else
866 pprev->next = ps->next;
867
868 /* FIXME, we can't conveniently deallocate the entries in the
869 partial_symbol lists (global_psymbols/static_psymbols) that
870 this psymtab points to. These just take up space until all
871 the psymtabs are reclaimed. Ditto the dependencies list and
872 filename, which are all in the psymbol_obstack. */
873
874 /* We need to cashier any psymtab that has this one as a dependency... */
875 again:
876 for (ps = partial_symtab_list; ps; ps = ps->next) {
877 for (i = 0; i < ps->number_of_dependencies; i++) {
878 if (ps->dependencies[i] == pst) {
879 cashier_psymtab (ps);
880 goto again; /* Must restart, chain has been munged. */
881 }
882 }
883 }
884 }
885 }
886
887 /* If a symtab or psymtab for filename NAME is found, free it along
888 with any dependent breakpoints, displays, etc.
889 Used when loading new versions of object modules with the "add-file"
890 command. This is only called on the top-level symtab or psymtab's name;
891 it is not called for subsidiary files such as .h files.
892
893 Return value is 1 if we blew away the environment, 0 if not.
894
895 FIXME. I think this is not the best way to do this. We should
896 work on being gentler to the environment while still cleaning up
897 all stray pointers into the freed symtab. */
898
899 int
900 free_named_symtabs (name)
901 char *name;
902 {
903 register struct symtab *s;
904 register struct symtab *prev;
905 register struct partial_symtab *ps;
906 struct blockvector *bv;
907 int blewit = 0;
908
909 /* We only wack things if the symbol-reload switch is set. */
910 if (!symbol_reloading)
911 return 0;
912
913 /* Some symbol formats have trouble providing file names... */
914 if (name == 0 || *name == '\0')
915 return 0;
916
917 /* Look for a psymtab with the specified name. */
918
919 again2:
920 for (ps = partial_symtab_list; ps; ps = ps->next) {
921 if (!strcmp (name, ps->filename)) {
922 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
923 goto again2; /* Must restart, chain has been munged */
924 }
925 }
926
927 /* Look for a symtab with the specified name. */
928
929 for (s = symtab_list; s; s = s->next)
930 {
931 if (!strcmp (name, s->filename))
932 break;
933 prev = s;
934 }
935
936 if (s)
937 {
938 if (s == symtab_list)
939 symtab_list = s->next;
940 else
941 prev->next = s->next;
942
943 /* For now, queue a delete for all breakpoints, displays, etc., whether
944 or not they depend on the symtab being freed. This should be
945 changed so that only those data structures affected are deleted. */
946
947 /* But don't delete anything if the symtab is empty.
948 This test is necessary due to a bug in "dbxread.c" that
949 causes empty symtabs to be created for N_SO symbols that
950 contain the pathname of the object file. (This problem
951 has been fixed in GDB 3.9x). */
952
953 bv = BLOCKLIST (s);
954 if (BLOCKLIST_NBLOCKS (bv) > 2
955 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
956 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
957 {
958 complain (&oldsyms_complaint, name);
959
960 clear_symtab_users_queued++;
961 make_cleanup (clear_symtab_users_once, 0);
962 blewit = 1;
963 } else {
964 complain (&empty_symtab_complaint, name);
965 }
966
967 free_symtab (s);
968 }
969 else
970 {
971 /* It is still possible that some breakpoints will be affected
972 even though no symtab was found, since the file might have
973 been compiled without debugging, and hence not be associated
974 with a symtab. In order to handle this correctly, we would need
975 to keep a list of text address ranges for undebuggable files.
976 For now, we do nothing, since this is a fairly obscure case. */
977 ;
978 }
979
980 /* FIXME, what about the misc function vector? */
981 return blewit;
982 }
983 \f
984 void
985 _initialize_symfile ()
986 {
987
988 add_com ("symbol-file", class_files, symbol_file_command,
989 "Load symbol table from executable file FILE.\n\
990 The `file' command can also load symbol tables, as well as setting the file\n\
991 to execute.");
992
993 add_com ("add-symbol-file", class_files, add_symbol_file_command,
994 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
995 The second argument provides the starting address of the file's text.");
996
997 add_com ("load", class_files, load_command,
998 "Dynamically load FILE into the running program, and record its symbols\n\
999 for access from GDB.");
1000
1001 add_show_from_set
1002 (add_set_cmd ("complaints", class_support, var_uinteger,
1003 (char *)&stop_whining,
1004 "Set max number of complaints about incorrect symbols.",
1005 &setlist),
1006 &showlist);
1007
1008 add_show_from_set
1009 (add_set_cmd ("symbol-reloading", class_support, var_boolean,
1010 (char *)&symbol_reloading,
1011 "Set dynamic symbol table reloading multiple times in one run.",
1012 &setlist),
1013 &showlist);
1014
1015 obstack_init (symbol_obstack);
1016 obstack_init (psymbol_obstack);
1017 }