1 /* Do various things to symbol tables (other than lookup)), for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 static void free_symtab ();
30 /* Free all the symtabs that are currently installed,
31 and all storage associated with them.
32 Leaves us in a consistent state with no symtabs installed. */
37 register struct symtab
*s
, *snext
;
39 /* All values will be invalid because their types will be! */
41 clear_value_history ();
43 clear_internalvars ();
45 set_default_breakpoint (0, 0, 0, 0);
47 current_source_symtab
= 0;
49 for (s
= symtab_list
; s
; s
= snext
)
55 obstack_free (symbol_obstack
, 0);
56 obstack_init (symbol_obstack
);
58 if (misc_function_vector
)
59 free (misc_function_vector
);
60 misc_function_count
= 0;
61 misc_function_vector
= 0;
64 /* Free a struct block <- B and all the symbols defined in that block. */
72 for (i
= 0; i
< n
; i
++)
74 free (SYMBOL_NAME (BLOCK_SYM (b
, i
)));
75 free (BLOCK_SYM (b
, i
));
80 /* Free all the storage associated with the struct symtab <- S.
81 Note that some symtabs have contents malloc'ed structure by structure,
82 while some have contents that all live inside one big block of memory,
83 and some share the contents of another symbol table and so you should
84 not free the contents on their behalf (except sometimes the linetable,
85 which maybe per symtab even when the rest is not).
86 It is s->free_code that says which alternative to use. */
90 register struct symtab
*s
;
93 register struct blockvector
*bv
;
94 register struct type
*type
;
95 register struct typevector
*tv
;
100 /* All the contents are part of a big block of memory
101 and some other symtab is in charge of freeing that block.
102 Therefore, do nothing. */
106 /* Here all the contents were malloc'ed structure by structure
107 and must be freed that way. */
108 /* First free the blocks (and their symbols. */
109 bv
= BLOCKVECTOR (s
);
110 n
= BLOCKVECTOR_NBLOCKS (bv
);
111 for (i
= 0; i
< n
; i
++)
112 free_symtab_block (BLOCKVECTOR_BLOCK (bv
, i
));
113 /* Free the blockvector itself. */
115 /* Free the type vector. */
118 /* Also free the linetable. */
121 /* Everything will be freed either by our `free_ptr'
122 or by some other symbatb, except for our linetable.
124 free (LINETABLE (s
));
128 /* If there is a single block of memory to free, free it. */
133 free (s
->line_charpos
);
138 /* Convert a raw symbol-segment to a struct symtab,
139 and relocate its internal pointers so that it is valid. */
141 /* This is how to relocate one pointer, given a name for it.
142 Works independent of the type of object pointed to. */
143 #define RELOCATE(slot) (slot ? (* (char **) &slot += relocation) : 0)
145 /* This is the inverse of RELOCATE. We use it when storing
146 a core address into a slot that has yet to be relocated. */
147 #define UNRELOCATE(slot) (slot ? (* (char **) &slot -= relocation) : 0)
149 /* During the process of relocation, this holds the amount to relocate by
150 (the address of the file's symtab data, in core in the debugger). */
151 static int relocation
;
153 #define CORE_RELOCATE(slot) \
154 ((slot) += (((slot) < data_start) ? text_relocation \
155 : ((slot) < bss_start) ? data_relocation : bss_relocation))
157 #define TEXT_RELOCATE(slot) ((slot) += text_relocation)
159 /* Relocation amounts for addresses in the program's core image. */
160 static int text_relocation
, data_relocation
, bss_relocation
;
162 /* Boundaries that divide program core addresses into text, data and bss;
163 used to determine which relocation amount to use. */
164 static int data_start
, bss_start
;
166 static void relocate_typevector ();
167 static void relocate_blockvector ();
168 static void relocate_type ();
169 static void relocate_block ();
170 static void relocate_symbol ();
171 static void relocate_source ();
173 /* Relocate a file's symseg so that all the pointers are valid C pointers.
174 Value is a `struct symtab'; but it is not suitable for direct
175 insertion into the `symtab_list' because it describes several files. */
177 static struct symtab
*
178 relocate_symtab (root
)
179 struct symbol_root
*root
;
181 struct symtab
*sp
= (struct symtab
*) xmalloc (sizeof (struct symtab
));
182 bzero (sp
, sizeof (struct symtab
));
184 relocation
= (int) root
;
185 text_relocation
= root
->textrel
;
186 data_relocation
= root
->datarel
;
187 bss_relocation
= root
->bssrel
;
188 data_start
= root
->databeg
;
189 bss_start
= root
->bssbeg
;
191 sp
->filename
= root
->filename
;
192 sp
->ldsymoff
= root
->ldsymoff
;
193 sp
->language
= root
->language
;
194 sp
->compilation
= root
->compilation
;
195 sp
->version
= root
->version
;
196 sp
->blockvector
= root
->blockvector
;
197 sp
->typevector
= root
->typevector
;
199 RELOCATE (TYPEVECTOR (sp
));
200 RELOCATE (BLOCKVECTOR (sp
));
201 RELOCATE (sp
->version
);
202 RELOCATE (sp
->compilation
);
203 RELOCATE (sp
->filename
);
205 relocate_typevector (TYPEVECTOR (sp
));
206 relocate_blockvector (BLOCKVECTOR (sp
));
212 relocate_blockvector (blp
)
213 register struct blockvector
*blp
;
215 register int nblocks
= BLOCKVECTOR_NBLOCKS (blp
);
217 for (i
= 0; i
< nblocks
; i
++)
218 RELOCATE (BLOCKVECTOR_BLOCK (blp
, i
));
219 for (i
= 0; i
< nblocks
; i
++)
220 relocate_block (BLOCKVECTOR_BLOCK (blp
, i
));
225 register struct block
*bp
;
227 register int nsyms
= BLOCK_NSYMS (bp
);
230 TEXT_RELOCATE (BLOCK_START (bp
));
231 TEXT_RELOCATE (BLOCK_END (bp
));
233 /* These two should not be recursively processed.
234 The superblock need not be because all blocks are
235 processed from relocate_blockvector.
236 The function need not be because it will be processed
237 under the block which is its scope. */
238 RELOCATE (BLOCK_SUPERBLOCK (bp
));
239 RELOCATE (BLOCK_FUNCTION (bp
));
241 for (i
= 0; i
< nsyms
; i
++)
242 RELOCATE (BLOCK_SYM (bp
, i
));
244 for (i
= 0; i
< nsyms
; i
++)
245 relocate_symbol (BLOCK_SYM (bp
, i
));
250 register struct symbol
*sp
;
252 RELOCATE (SYMBOL_NAME (sp
));
253 if (SYMBOL_CLASS (sp
) == LOC_BLOCK
)
255 RELOCATE (SYMBOL_BLOCK_VALUE (sp
));
256 /* We can assume the block that belongs to this symbol
257 is not relocated yet, since it comes after
258 the block that contains this symbol. */
259 BLOCK_FUNCTION (SYMBOL_BLOCK_VALUE (sp
)) = sp
;
260 UNRELOCATE (BLOCK_FUNCTION (SYMBOL_BLOCK_VALUE (sp
)));
262 else if (SYMBOL_CLASS (sp
) == LOC_STATIC
)
263 CORE_RELOCATE (SYMBOL_VALUE (sp
));
264 else if (SYMBOL_CLASS (sp
) == LOC_LABEL
)
265 TEXT_RELOCATE (SYMBOL_VALUE (sp
));
266 RELOCATE (SYMBOL_TYPE (sp
));
270 relocate_typevector (tv
)
271 struct typevector
*tv
;
273 register int ntypes
= TYPEVECTOR_NTYPES (tv
);
276 for (i
= 0; i
< ntypes
; i
++)
277 RELOCATE (TYPEVECTOR_TYPE (tv
, i
));
278 for (i
= 0; i
< ntypes
; i
++)
279 relocate_type (TYPEVECTOR_TYPE (tv
, i
));
282 /* We cannot come up with an a priori spanning tree
283 for the network of types, since types can be used
284 for many symbols and also as components of other types.
285 Therefore, we need to be able to mark types that we
286 already have relocated (or are already in the middle of relocating)
287 as in a garbage collector. */
291 register struct type
*tp
;
293 register int nfields
= TYPE_NFIELDS (tp
);
296 RELOCATE (TYPE_NAME (tp
));
297 RELOCATE (TYPE_TARGET_TYPE (tp
));
298 RELOCATE (TYPE_FIELDS (tp
));
299 RELOCATE (TYPE_POINTER_TYPE (tp
));
301 for (i
= 0; i
< nfields
; i
++)
303 RELOCATE (TYPE_FIELD_TYPE (tp
, i
));
304 RELOCATE (TYPE_FIELD_NAME (tp
, i
));
309 relocate_sourcevector (svp
)
310 register struct sourcevector
*svp
;
312 register int nfiles
= svp
->length
;
314 for (i
= 0; i
< nfiles
; i
++)
315 RELOCATE (svp
->source
[i
]);
316 for (i
= 0; i
< nfiles
; i
++)
317 relocate_source (svp
->source
[i
]);
322 register struct source
*sp
;
324 register int nitems
= sp
->contents
.nitems
;
328 for (i
= 0; i
< nitems
; i
++)
329 TEXT_RELOCATE (sp
->contents
.item
[i
].pc
);
332 /* Read symsegs from file named NAME open on DESC,
333 make symtabs from them, and return a chain of them.
334 These symtabs are not suitable for direct use in `symtab_list'
335 because each one describes a single object file, perhaps many source files.
336 `symbol_file_command' takes each of these, makes many real symtabs
337 from it, and then frees it.
339 We assume DESC is prepositioned at the end of the string table,
340 just before the symsegs if there are any. */
343 read_symsegs (desc
, name
)
347 struct symbol_root root
;
349 register struct symtab
*sp
, *sp1
, *chain
= 0;
354 len
= myread (desc
, &root
, sizeof root
);
355 if (len
== 0 || root
.format
== 0)
357 /* format 1 was ok for the original gdb, but since the size of the
358 type structure changed when C++ support was added, it can no
359 longer be used. Accept only format 2. */
360 if (root
.format
!= 2 ||
361 root
.length
< sizeof root
)
362 error ("\nInvalid symbol segment format code");
363 data
= (char *) xmalloc (root
.length
);
364 bcopy (&root
, data
, sizeof root
);
365 len
= myread (desc
, data
+ sizeof root
,
366 root
.length
- sizeof root
);
367 sp
= relocate_symtab (data
);
368 RELOCATE (((struct symbol_root
*)data
)->sourcevector
);
369 relocate_sourcevector (((struct symbol_root
*)data
)->sourcevector
);
372 sp
->linetable
= (struct linetable
*) ((struct symbol_root
*)data
)->sourcevector
;
378 static int block_depth ();
379 void print_spaces ();
380 static void print_symbol ();
383 print_symtabs (filename
)
387 register struct symtab
*s
;
390 register struct linetable
*l
;
391 struct blockvector
*bv
;
392 register struct block
*b
;
394 struct cleanup
*cleanups
;
398 error_no_arg ("file to write symbol data in");
400 filename
= tilde_expand (filename
);
401 make_cleanup (free
, filename
);
403 outfile
= fopen (filename
, "w");
405 perror_with_name (filename
);
407 cleanups
= make_cleanup (fclose
, outfile
);
410 for (s
= symtab_list
; s
; s
= s
->next
)
412 /* First print the line table. */
413 fprintf (outfile
, "Symtab for file %s\n\n", s
->filename
);
414 fprintf (outfile
, "Line table:\n\n");
417 for (i
= 0; i
< len
; i
++)
418 fprintf (outfile
, " line %d at %x\n", l
->item
[i
].line
,
420 /* Now print the block info. */
421 fprintf (outfile
, "\nBlockvector:\n\n");
422 bv
= BLOCKVECTOR (s
);
423 len
= BLOCKVECTOR_NBLOCKS (bv
);
424 for (i
= 0; i
< len
; i
++)
426 b
= BLOCKVECTOR_BLOCK (bv
, i
);
427 depth
= block_depth (b
) * 2;
428 print_spaces (depth
, outfile
);
429 fprintf (outfile
, "block #%03d (object 0x%x) ", i
, b
);
430 fprintf (outfile
, "[0x%x..0x%x]", BLOCK_START (b
), BLOCK_END (b
));
431 if (BLOCK_SUPERBLOCK (b
))
432 fprintf (outfile
, " (under 0x%x)", BLOCK_SUPERBLOCK (b
));
433 if (BLOCK_FUNCTION (b
))
434 fprintf (outfile
, " %s", SYMBOL_NAME (BLOCK_FUNCTION (b
)));
435 fputc ('\n', outfile
);
436 blen
= BLOCK_NSYMS (b
);
437 for (j
= 0; j
< blen
; j
++)
439 print_symbol (BLOCK_SYM (b
, j
), depth
+ 1, outfile
);
443 fprintf (outfile
, "\n\n");
447 do_cleanups (cleanups
);
451 print_symbol (symbol
, depth
, outfile
)
452 struct symbol
*symbol
;
456 print_spaces (depth
, outfile
);
457 if (SYMBOL_NAMESPACE (symbol
) == LABEL_NAMESPACE
)
459 fprintf (outfile
, "label %s at 0x%x\n", SYMBOL_NAME (symbol
),
460 SYMBOL_VALUE (symbol
));
463 if (SYMBOL_NAMESPACE (symbol
) == STRUCT_NAMESPACE
)
465 if (TYPE_NAME (SYMBOL_TYPE (symbol
)))
467 type_print_1 (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
);
471 fprintf (outfile
, "%s %s = ",
472 (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_ENUM
474 : (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_STRUCT
475 ? "struct" : "union")),
476 SYMBOL_NAME (symbol
));
477 type_print_1 (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
);
479 fprintf (outfile
, ";\n");
483 if (SYMBOL_CLASS (symbol
) == LOC_TYPEDEF
)
484 fprintf (outfile
, "typedef ");
485 if (SYMBOL_TYPE (symbol
))
487 type_print_1 (SYMBOL_TYPE (symbol
), SYMBOL_NAME (symbol
),
489 fprintf (outfile
, "; ");
492 fprintf (outfile
, "%s ", SYMBOL_NAME (symbol
));
494 switch (SYMBOL_CLASS (symbol
))
497 fprintf (outfile
, "const %d (0x%x),",
498 SYMBOL_VALUE (symbol
), SYMBOL_VALUE (symbol
));
501 case LOC_CONST_BYTES
:
502 fprintf (outfile
, "const %d hex bytes:",
503 TYPE_LENGTH (SYMBOL_TYPE (symbol
)));
506 for (i
= 0; i
< TYPE_LENGTH (SYMBOL_TYPE (symbol
)); i
++)
507 fprintf (outfile
, " %2x", SYMBOL_VALUE_BYTES (symbol
) [i
]);
508 fprintf (outfile
, ",");
513 fprintf (outfile
, "static at 0x%x,", SYMBOL_VALUE (symbol
));
517 fprintf (outfile
, "register %d,", SYMBOL_VALUE (symbol
));
521 fprintf (outfile
, "arg at 0x%x,", SYMBOL_VALUE (symbol
));
525 fprintf (outfile
, "reference arg at 0x%x,", SYMBOL_VALUE (symbol
));
529 fprintf (outfile
, "parameter register %d,", SYMBOL_VALUE (symbol
));
533 fprintf (outfile
, "local at 0x%x,", SYMBOL_VALUE (symbol
));
540 fprintf (outfile
, "label at 0x%x", SYMBOL_VALUE (symbol
));
544 fprintf (outfile
, "block (object 0x%x) starting at 0x%x,",
545 SYMBOL_VALUE (symbol
),
546 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
)));
550 fprintf (outfile
, "\n");
553 /* Return the nexting depth of a block within other blocks in its symtab. */
560 while (block
= BLOCK_SUPERBLOCK (block
)) i
++;
565 * Free all partial_symtab storage.
570 obstack_free (psymbol_obstack
, 0);
571 obstack_init (psymbol_obstack
);
572 partial_symtab_list
= (struct partial_symtab
*) 0;
576 _initialize_symmisc ()
578 symtab_list
= (struct symtab
*) 0;
579 partial_symtab_list
= (struct partial_symtab
*) 0;
581 add_com ("printsyms", class_obscure
, print_symtabs
,
582 "Print dump of current symbol definitions to file OUTFILE.");