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