* symtab.h (enum address_class): Remove LOC_INDIRECT and
[binutils-gdb.git] / gdb / symmisc.c
1 /* Do various things to symbol tables (other than lookup), for GDB.
2
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2007, 2008
5 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "bfd.h"
26 #include "symfile.h"
27 #include "objfiles.h"
28 #include "breakpoint.h"
29 #include "command.h"
30 #include "gdb_obstack.h"
31 #include "exceptions.h"
32 #include "language.h"
33 #include "bcache.h"
34 #include "block.h"
35 #include "gdb_regex.h"
36 #include "gdb_stat.h"
37 #include "dictionary.h"
38
39 #include "gdb_string.h"
40 #include "readline/readline.h"
41
42 #ifndef DEV_TTY
43 #define DEV_TTY "/dev/tty"
44 #endif
45
46 /* Unfortunately for debugging, stderr is usually a macro. This is painful
47 when calling functions that take FILE *'s from the debugger.
48 So we make a variable which has the same value and which is accessible when
49 debugging GDB with itself. Because stdin et al need not be constants,
50 we initialize them in the _initialize_symmisc function at the bottom
51 of the file. */
52 FILE *std_in;
53 FILE *std_out;
54 FILE *std_err;
55
56 /* Prototypes for local functions */
57
58 static void dump_symtab (struct objfile *, struct symtab *,
59 struct ui_file *);
60
61 static void dump_psymtab (struct objfile *, struct partial_symtab *,
62 struct ui_file *);
63
64 static void dump_msymbols (struct objfile *, struct ui_file *);
65
66 static void dump_objfile (struct objfile *);
67
68 static int block_depth (struct block *);
69
70 static void print_partial_symbols (struct partial_symbol **, int,
71 char *, struct ui_file *);
72
73 void _initialize_symmisc (void);
74
75 struct print_symbol_args
76 {
77 struct symbol *symbol;
78 int depth;
79 struct ui_file *outfile;
80 };
81
82 static int print_symbol (void *);
83 \f
84 /* Free all the storage associated with the struct symtab <- S.
85 Note that some symtabs have contents that all live inside one big block of
86 memory, and some share the contents of another symbol table and so you
87 should not free the contents on their behalf (except sometimes the
88 linetable, which maybe per symtab even when the rest is not).
89 It is s->free_code that says which alternative to use. */
90
91 void
92 free_symtab (struct symtab *s)
93 {
94 int i, n;
95 struct blockvector *bv;
96
97 switch (s->free_code)
98 {
99 case free_nothing:
100 /* All the contents are part of a big block of memory (an obstack),
101 and some other symtab is in charge of freeing that block.
102 Therefore, do nothing. */
103 break;
104
105 case free_linetable:
106 /* Everything will be freed either by our `free_func'
107 or by some other symtab, except for our linetable.
108 Free that now. */
109 if (LINETABLE (s))
110 xfree (LINETABLE (s));
111 break;
112 }
113
114 /* If there is a single block of memory to free, free it. */
115 if (s->free_func != NULL)
116 s->free_func (s);
117
118 /* Free source-related stuff */
119 if (s->line_charpos != NULL)
120 xfree (s->line_charpos);
121 if (s->fullname != NULL)
122 xfree (s->fullname);
123 if (s->debugformat != NULL)
124 xfree (s->debugformat);
125 xfree (s);
126 }
127
128 void
129 print_symbol_bcache_statistics (void)
130 {
131 struct objfile *objfile;
132
133 immediate_quit++;
134 ALL_OBJFILES (objfile)
135 {
136 printf_filtered (_("Byte cache statistics for '%s':\n"), objfile->name);
137 print_bcache_statistics (objfile->psymbol_cache, "partial symbol cache");
138 }
139 immediate_quit--;
140 }
141
142 void
143 print_objfile_statistics (void)
144 {
145 struct objfile *objfile;
146 struct symtab *s;
147 struct partial_symtab *ps;
148 int i, linetables, blockvectors;
149
150 immediate_quit++;
151 ALL_OBJFILES (objfile)
152 {
153 printf_filtered (_("Statistics for '%s':\n"), objfile->name);
154 if (OBJSTAT (objfile, n_stabs) > 0)
155 printf_filtered (_(" Number of \"stab\" symbols read: %d\n"),
156 OBJSTAT (objfile, n_stabs));
157 if (OBJSTAT (objfile, n_minsyms) > 0)
158 printf_filtered (_(" Number of \"minimal\" symbols read: %d\n"),
159 OBJSTAT (objfile, n_minsyms));
160 if (OBJSTAT (objfile, n_psyms) > 0)
161 printf_filtered (_(" Number of \"partial\" symbols read: %d\n"),
162 OBJSTAT (objfile, n_psyms));
163 if (OBJSTAT (objfile, n_syms) > 0)
164 printf_filtered (_(" Number of \"full\" symbols read: %d\n"),
165 OBJSTAT (objfile, n_syms));
166 if (OBJSTAT (objfile, n_types) > 0)
167 printf_filtered (_(" Number of \"types\" defined: %d\n"),
168 OBJSTAT (objfile, n_types));
169 i = 0;
170 ALL_OBJFILE_PSYMTABS (objfile, ps)
171 {
172 if (ps->readin == 0)
173 i++;
174 }
175 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i);
176 i = linetables = blockvectors = 0;
177 ALL_OBJFILE_SYMTABS (objfile, s)
178 {
179 i++;
180 if (s->linetable != NULL)
181 linetables++;
182 if (s->primary == 1)
183 blockvectors++;
184 }
185 printf_filtered (_(" Number of symbol tables: %d\n"), i);
186 printf_filtered (_(" Number of symbol tables with line tables: %d\n"),
187 linetables);
188 printf_filtered (_(" Number of symbol tables with blockvectors: %d\n"),
189 blockvectors);
190
191 if (OBJSTAT (objfile, sz_strtab) > 0)
192 printf_filtered (_(" Space used by a.out string tables: %d\n"),
193 OBJSTAT (objfile, sz_strtab));
194 printf_filtered (_(" Total memory used for objfile obstack: %d\n"),
195 obstack_memory_used (&objfile->objfile_obstack));
196 printf_filtered (_(" Total memory used for psymbol cache: %d\n"),
197 bcache_memory_used (objfile->psymbol_cache));
198 printf_filtered (_(" Total memory used for macro cache: %d\n"),
199 bcache_memory_used (objfile->macro_cache));
200 }
201 immediate_quit--;
202 }
203
204 static void
205 dump_objfile (struct objfile *objfile)
206 {
207 struct symtab *symtab;
208 struct partial_symtab *psymtab;
209
210 printf_filtered ("\nObject file %s: ", objfile->name);
211 printf_filtered ("Objfile at ");
212 gdb_print_host_address (objfile, gdb_stdout);
213 printf_filtered (", bfd at ");
214 gdb_print_host_address (objfile->obfd, gdb_stdout);
215 printf_filtered (", %d minsyms\n\n",
216 objfile->minimal_symbol_count);
217
218 if (objfile->psymtabs)
219 {
220 printf_filtered ("Psymtabs:\n");
221 for (psymtab = objfile->psymtabs;
222 psymtab != NULL;
223 psymtab = psymtab->next)
224 {
225 printf_filtered ("%s at ",
226 psymtab->filename);
227 gdb_print_host_address (psymtab, gdb_stdout);
228 printf_filtered (", ");
229 if (psymtab->objfile != objfile)
230 {
231 printf_filtered ("NOT ON CHAIN! ");
232 }
233 wrap_here (" ");
234 }
235 printf_filtered ("\n\n");
236 }
237
238 if (objfile->symtabs)
239 {
240 printf_filtered ("Symtabs:\n");
241 for (symtab = objfile->symtabs;
242 symtab != NULL;
243 symtab = symtab->next)
244 {
245 printf_filtered ("%s at ", symtab->filename);
246 gdb_print_host_address (symtab, gdb_stdout);
247 printf_filtered (", ");
248 if (symtab->objfile != objfile)
249 {
250 printf_filtered ("NOT ON CHAIN! ");
251 }
252 wrap_here (" ");
253 }
254 printf_filtered ("\n\n");
255 }
256 }
257
258 /* Print minimal symbols from this objfile. */
259
260 static void
261 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
262 {
263 struct minimal_symbol *msymbol;
264 int index;
265 char ms_type;
266
267 fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile->name);
268 if (objfile->minimal_symbol_count == 0)
269 {
270 fprintf_filtered (outfile, "No minimal symbols found.\n");
271 return;
272 }
273 for (index = 0, msymbol = objfile->msymbols;
274 DEPRECATED_SYMBOL_NAME (msymbol) != NULL; msymbol++, index++)
275 {
276 switch (msymbol->type)
277 {
278 case mst_unknown:
279 ms_type = 'u';
280 break;
281 case mst_text:
282 ms_type = 'T';
283 break;
284 case mst_solib_trampoline:
285 ms_type = 'S';
286 break;
287 case mst_data:
288 ms_type = 'D';
289 break;
290 case mst_bss:
291 ms_type = 'B';
292 break;
293 case mst_abs:
294 ms_type = 'A';
295 break;
296 case mst_file_text:
297 ms_type = 't';
298 break;
299 case mst_file_data:
300 ms_type = 'd';
301 break;
302 case mst_file_bss:
303 ms_type = 'b';
304 break;
305 default:
306 ms_type = '?';
307 break;
308 }
309 fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
310 fputs_filtered (paddress (SYMBOL_VALUE_ADDRESS (msymbol)), outfile);
311 fprintf_filtered (outfile, " %s", DEPRECATED_SYMBOL_NAME (msymbol));
312 if (SYMBOL_BFD_SECTION (msymbol))
313 fprintf_filtered (outfile, " section %s",
314 bfd_section_name (objfile->obfd,
315 SYMBOL_BFD_SECTION (msymbol)));
316 if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
317 {
318 fprintf_filtered (outfile, " %s", SYMBOL_DEMANGLED_NAME (msymbol));
319 }
320 if (msymbol->filename)
321 fprintf_filtered (outfile, " %s", msymbol->filename);
322 fputs_filtered ("\n", outfile);
323 }
324 if (objfile->minimal_symbol_count != index)
325 {
326 warning (_("internal error: minimal symbol count %d != %d"),
327 objfile->minimal_symbol_count, index);
328 }
329 fprintf_filtered (outfile, "\n");
330 }
331
332 static void
333 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
334 struct ui_file *outfile)
335 {
336 int i;
337
338 fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
339 psymtab->filename);
340 fprintf_filtered (outfile, "(object ");
341 gdb_print_host_address (psymtab, outfile);
342 fprintf_filtered (outfile, ")\n\n");
343 fprintf_unfiltered (outfile, " Read from object file %s (",
344 objfile->name);
345 gdb_print_host_address (objfile, outfile);
346 fprintf_unfiltered (outfile, ")\n");
347
348 if (psymtab->readin)
349 {
350 fprintf_filtered (outfile,
351 " Full symtab was read (at ");
352 gdb_print_host_address (psymtab->symtab, outfile);
353 fprintf_filtered (outfile, " by function at ");
354 gdb_print_host_address (psymtab->read_symtab, outfile);
355 fprintf_filtered (outfile, ")\n");
356 }
357
358 fprintf_filtered (outfile, " Relocate symbols by ");
359 for (i = 0; i < psymtab->objfile->num_sections; ++i)
360 {
361 if (i != 0)
362 fprintf_filtered (outfile, ", ");
363 wrap_here (" ");
364 fputs_filtered (paddress (ANOFFSET (psymtab->section_offsets, i)),
365 outfile);
366 }
367 fprintf_filtered (outfile, "\n");
368
369 fprintf_filtered (outfile, " Symbols cover text addresses ");
370 fputs_filtered (paddress (psymtab->textlow), outfile);
371 fprintf_filtered (outfile, "-");
372 fputs_filtered (paddress (psymtab->texthigh), outfile);
373 fprintf_filtered (outfile, "\n");
374 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
375 psymtab->number_of_dependencies);
376 for (i = 0; i < psymtab->number_of_dependencies; i++)
377 {
378 fprintf_filtered (outfile, " %d ", i);
379 gdb_print_host_address (psymtab->dependencies[i], outfile);
380 fprintf_filtered (outfile, " %s\n",
381 psymtab->dependencies[i]->filename);
382 }
383 if (psymtab->n_global_syms > 0)
384 {
385 print_partial_symbols (objfile->global_psymbols.list
386 + psymtab->globals_offset,
387 psymtab->n_global_syms, "Global", outfile);
388 }
389 if (psymtab->n_static_syms > 0)
390 {
391 print_partial_symbols (objfile->static_psymbols.list
392 + psymtab->statics_offset,
393 psymtab->n_static_syms, "Static", outfile);
394 }
395 fprintf_filtered (outfile, "\n");
396 }
397
398 static void
399 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
400 struct ui_file *outfile)
401 {
402 int i;
403 struct dict_iterator iter;
404 int len, blen;
405 struct linetable *l;
406 struct blockvector *bv;
407 struct symbol *sym;
408 struct block *b;
409 int depth;
410
411 fprintf_filtered (outfile, "\nSymtab for file %s\n", symtab->filename);
412 if (symtab->dirname)
413 fprintf_filtered (outfile, "Compilation directory is %s\n",
414 symtab->dirname);
415 fprintf_filtered (outfile, "Read from object file %s (", objfile->name);
416 gdb_print_host_address (objfile, outfile);
417 fprintf_filtered (outfile, ")\n");
418 fprintf_filtered (outfile, "Language: %s\n", language_str (symtab->language));
419
420 /* First print the line table. */
421 l = LINETABLE (symtab);
422 if (l)
423 {
424 fprintf_filtered (outfile, "\nLine table:\n\n");
425 len = l->nitems;
426 for (i = 0; i < len; i++)
427 {
428 fprintf_filtered (outfile, " line %d at ", l->item[i].line);
429 fputs_filtered (paddress (l->item[i].pc), outfile);
430 fprintf_filtered (outfile, "\n");
431 }
432 }
433 /* Now print the block info, but only for primary symtabs since we will
434 print lots of duplicate info otherwise. */
435 if (symtab->primary)
436 {
437 fprintf_filtered (outfile, "\nBlockvector:\n\n");
438 bv = BLOCKVECTOR (symtab);
439 len = BLOCKVECTOR_NBLOCKS (bv);
440 for (i = 0; i < len; i++)
441 {
442 b = BLOCKVECTOR_BLOCK (bv, i);
443 depth = block_depth (b) * 2;
444 print_spaces (depth, outfile);
445 fprintf_filtered (outfile, "block #%03d, object at ", i);
446 gdb_print_host_address (b, outfile);
447 if (BLOCK_SUPERBLOCK (b))
448 {
449 fprintf_filtered (outfile, " under ");
450 gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
451 }
452 /* drow/2002-07-10: We could save the total symbols count
453 even if we're using a hashtable, but nothing else but this message
454 wants it. */
455 fprintf_filtered (outfile, ", %d syms/buckets in ",
456 dict_size (BLOCK_DICT (b)));
457 fputs_filtered (paddress (BLOCK_START (b)), outfile);
458 fprintf_filtered (outfile, "..");
459 fputs_filtered (paddress (BLOCK_END (b)), outfile);
460 if (BLOCK_FUNCTION (b))
461 {
462 fprintf_filtered (outfile, ", function %s", DEPRECATED_SYMBOL_NAME (BLOCK_FUNCTION (b)));
463 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
464 {
465 fprintf_filtered (outfile, ", %s",
466 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
467 }
468 }
469 fprintf_filtered (outfile, "\n");
470 /* Now print each symbol in this block (in no particular order, if
471 we're using a hashtable). */
472 ALL_BLOCK_SYMBOLS (b, iter, sym)
473 {
474 struct print_symbol_args s;
475 s.symbol = sym;
476 s.depth = depth + 1;
477 s.outfile = outfile;
478 catch_errors (print_symbol, &s, "Error printing symbol:\n",
479 RETURN_MASK_ERROR);
480 }
481 }
482 fprintf_filtered (outfile, "\n");
483 }
484 else
485 {
486 fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
487 }
488 }
489
490 static void
491 dump_symtab (struct objfile *objfile, struct symtab *symtab,
492 struct ui_file *outfile)
493 {
494 enum language saved_lang;
495
496 /* Set the current language to the language of the symtab we're dumping
497 because certain routines used during dump_symtab() use the current
498 language to print an image of the symbol. We'll restore it later. */
499 saved_lang = set_language (symtab->language);
500
501 dump_symtab_1 (objfile, symtab, outfile);
502
503 set_language (saved_lang);
504 }
505
506 void
507 maintenance_print_symbols (char *args, int from_tty)
508 {
509 char **argv;
510 struct ui_file *outfile;
511 struct cleanup *cleanups;
512 char *symname = NULL;
513 char *filename = DEV_TTY;
514 struct objfile *objfile;
515 struct symtab *s;
516
517 dont_repeat ();
518
519 if (args == NULL)
520 {
521 error (_("\
522 Arguments missing: an output file name and an optional symbol file name"));
523 }
524 else if ((argv = buildargv (args)) == NULL)
525 {
526 nomem (0);
527 }
528 cleanups = make_cleanup_freeargv (argv);
529
530 if (argv[0] != NULL)
531 {
532 filename = argv[0];
533 /* If a second arg is supplied, it is a source file name to match on */
534 if (argv[1] != NULL)
535 {
536 symname = argv[1];
537 }
538 }
539
540 filename = tilde_expand (filename);
541 make_cleanup (xfree, filename);
542
543 outfile = gdb_fopen (filename, FOPEN_WT);
544 if (outfile == 0)
545 perror_with_name (filename);
546 make_cleanup_ui_file_delete (outfile);
547
548 immediate_quit++;
549 ALL_SYMTABS (objfile, s)
550 if (symname == NULL || strcmp (symname, s->filename) == 0)
551 dump_symtab (objfile, s, outfile);
552 immediate_quit--;
553 do_cleanups (cleanups);
554 }
555
556 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
557 far to indent. ARGS is really a struct print_symbol_args *, but is
558 declared as char * to get it past catch_errors. Returns 0 for error,
559 1 for success. */
560
561 static int
562 print_symbol (void *args)
563 {
564 struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
565 int depth = ((struct print_symbol_args *) args)->depth;
566 struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
567
568 print_spaces (depth, outfile);
569 if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
570 {
571 fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
572 fputs_filtered (paddress (SYMBOL_VALUE_ADDRESS (symbol)), outfile);
573 if (SYMBOL_BFD_SECTION (symbol))
574 fprintf_filtered (outfile, " section %s\n",
575 bfd_section_name (SYMBOL_BFD_SECTION (symbol)->owner,
576 SYMBOL_BFD_SECTION (symbol)));
577 else
578 fprintf_filtered (outfile, "\n");
579 return 1;
580 }
581 if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
582 {
583 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
584 {
585 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
586 }
587 else
588 {
589 fprintf_filtered (outfile, "%s %s = ",
590 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
591 ? "enum"
592 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
593 ? "struct" : "union")),
594 DEPRECATED_SYMBOL_NAME (symbol));
595 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
596 }
597 fprintf_filtered (outfile, ";\n");
598 }
599 else
600 {
601 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
602 fprintf_filtered (outfile, "typedef ");
603 if (SYMBOL_TYPE (symbol))
604 {
605 /* Print details of types, except for enums where it's clutter. */
606 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
607 outfile,
608 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
609 depth);
610 fprintf_filtered (outfile, "; ");
611 }
612 else
613 fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
614
615 switch (SYMBOL_CLASS (symbol))
616 {
617 case LOC_CONST:
618 fprintf_filtered (outfile, "const %ld (0x%lx)",
619 SYMBOL_VALUE (symbol),
620 SYMBOL_VALUE (symbol));
621 break;
622
623 case LOC_CONST_BYTES:
624 {
625 unsigned i;
626 struct type *type = check_typedef (SYMBOL_TYPE (symbol));
627 fprintf_filtered (outfile, "const %u hex bytes:",
628 TYPE_LENGTH (type));
629 for (i = 0; i < TYPE_LENGTH (type); i++)
630 fprintf_filtered (outfile, " %02x",
631 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
632 }
633 break;
634
635 case LOC_STATIC:
636 fprintf_filtered (outfile, "static at ");
637 fputs_filtered (paddress (SYMBOL_VALUE_ADDRESS (symbol)), outfile);
638 if (SYMBOL_BFD_SECTION (symbol))
639 fprintf_filtered (outfile, " section %s",
640 bfd_section_name
641 (SYMBOL_BFD_SECTION (symbol)->owner,
642 SYMBOL_BFD_SECTION (symbol)));
643 break;
644
645 case LOC_REGISTER:
646 fprintf_filtered (outfile, "register %ld", SYMBOL_VALUE (symbol));
647 break;
648
649 case LOC_ARG:
650 fprintf_filtered (outfile, "arg at offset 0x%lx",
651 SYMBOL_VALUE (symbol));
652 break;
653
654 case LOC_LOCAL_ARG:
655 fprintf_filtered (outfile, "arg at offset 0x%lx from fp",
656 SYMBOL_VALUE (symbol));
657 break;
658
659 case LOC_REF_ARG:
660 fprintf_filtered (outfile, "reference arg at 0x%lx", SYMBOL_VALUE (symbol));
661 break;
662
663 case LOC_REGPARM:
664 fprintf_filtered (outfile, "parameter register %ld", SYMBOL_VALUE (symbol));
665 break;
666
667 case LOC_REGPARM_ADDR:
668 fprintf_filtered (outfile, "address parameter register %ld", SYMBOL_VALUE (symbol));
669 break;
670
671 case LOC_LOCAL:
672 fprintf_filtered (outfile, "local at offset 0x%lx",
673 SYMBOL_VALUE (symbol));
674 break;
675
676 case LOC_BASEREG:
677 fprintf_filtered (outfile, "local at 0x%lx from register %d",
678 SYMBOL_VALUE (symbol), SYMBOL_BASEREG (symbol));
679 break;
680
681 case LOC_BASEREG_ARG:
682 fprintf_filtered (outfile, "arg at 0x%lx from register %d",
683 SYMBOL_VALUE (symbol), SYMBOL_BASEREG (symbol));
684 break;
685
686 case LOC_TYPEDEF:
687 break;
688
689 case LOC_LABEL:
690 fprintf_filtered (outfile, "label at ");
691 fputs_filtered (paddress (SYMBOL_VALUE_ADDRESS (symbol)), outfile);
692 if (SYMBOL_BFD_SECTION (symbol))
693 fprintf_filtered (outfile, " section %s",
694 bfd_section_name
695 (SYMBOL_BFD_SECTION (symbol)->owner,
696 SYMBOL_BFD_SECTION (symbol)));
697 break;
698
699 case LOC_BLOCK:
700 fprintf_filtered (outfile, "block object ");
701 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
702 fprintf_filtered (outfile, ", ");
703 fputs_filtered (paddress (BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
704 outfile);
705 fprintf_filtered (outfile, "..");
706 fputs_filtered (paddress (BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
707 outfile);
708 if (SYMBOL_BFD_SECTION (symbol))
709 fprintf_filtered (outfile, " section %s",
710 bfd_section_name
711 (SYMBOL_BFD_SECTION (symbol)->owner,
712 SYMBOL_BFD_SECTION (symbol)));
713 break;
714
715 case LOC_COMPUTED:
716 case LOC_COMPUTED_ARG:
717 fprintf_filtered (outfile, "computed at runtime");
718 break;
719
720 case LOC_UNRESOLVED:
721 fprintf_filtered (outfile, "unresolved");
722 break;
723
724 case LOC_OPTIMIZED_OUT:
725 fprintf_filtered (outfile, "optimized out");
726 break;
727
728 default:
729 fprintf_filtered (outfile, "botched symbol class %x",
730 SYMBOL_CLASS (symbol));
731 break;
732 }
733 }
734 fprintf_filtered (outfile, "\n");
735 return 1;
736 }
737
738 void
739 maintenance_print_psymbols (char *args, int from_tty)
740 {
741 char **argv;
742 struct ui_file *outfile;
743 struct cleanup *cleanups;
744 char *symname = NULL;
745 char *filename = DEV_TTY;
746 struct objfile *objfile;
747 struct partial_symtab *ps;
748
749 dont_repeat ();
750
751 if (args == NULL)
752 {
753 error (_("print-psymbols takes an output file name and optional symbol file name"));
754 }
755 else if ((argv = buildargv (args)) == NULL)
756 {
757 nomem (0);
758 }
759 cleanups = make_cleanup_freeargv (argv);
760
761 if (argv[0] != NULL)
762 {
763 filename = argv[0];
764 /* If a second arg is supplied, it is a source file name to match on */
765 if (argv[1] != NULL)
766 {
767 symname = argv[1];
768 }
769 }
770
771 filename = tilde_expand (filename);
772 make_cleanup (xfree, filename);
773
774 outfile = gdb_fopen (filename, FOPEN_WT);
775 if (outfile == 0)
776 perror_with_name (filename);
777 make_cleanup_ui_file_delete (outfile);
778
779 immediate_quit++;
780 ALL_PSYMTABS (objfile, ps)
781 if (symname == NULL || strcmp (symname, ps->filename) == 0)
782 dump_psymtab (objfile, ps, outfile);
783 immediate_quit--;
784 do_cleanups (cleanups);
785 }
786
787 static void
788 print_partial_symbols (struct partial_symbol **p, int count, char *what,
789 struct ui_file *outfile)
790 {
791 fprintf_filtered (outfile, " %s partial symbols:\n", what);
792 while (count-- > 0)
793 {
794 fprintf_filtered (outfile, " `%s'", DEPRECATED_SYMBOL_NAME (*p));
795 if (SYMBOL_DEMANGLED_NAME (*p) != NULL)
796 {
797 fprintf_filtered (outfile, " `%s'", SYMBOL_DEMANGLED_NAME (*p));
798 }
799 fputs_filtered (", ", outfile);
800 switch (SYMBOL_DOMAIN (*p))
801 {
802 case UNDEF_DOMAIN:
803 fputs_filtered ("undefined domain, ", outfile);
804 break;
805 case VAR_DOMAIN:
806 /* This is the usual thing -- don't print it */
807 break;
808 case STRUCT_DOMAIN:
809 fputs_filtered ("struct domain, ", outfile);
810 break;
811 case LABEL_DOMAIN:
812 fputs_filtered ("label domain, ", outfile);
813 break;
814 default:
815 fputs_filtered ("<invalid domain>, ", outfile);
816 break;
817 }
818 switch (SYMBOL_CLASS (*p))
819 {
820 case LOC_UNDEF:
821 fputs_filtered ("undefined", outfile);
822 break;
823 case LOC_CONST:
824 fputs_filtered ("constant int", outfile);
825 break;
826 case LOC_STATIC:
827 fputs_filtered ("static", outfile);
828 break;
829 case LOC_REGISTER:
830 fputs_filtered ("register", outfile);
831 break;
832 case LOC_ARG:
833 fputs_filtered ("pass by value", outfile);
834 break;
835 case LOC_REF_ARG:
836 fputs_filtered ("pass by reference", outfile);
837 break;
838 case LOC_REGPARM:
839 fputs_filtered ("register parameter", outfile);
840 break;
841 case LOC_REGPARM_ADDR:
842 fputs_filtered ("register address parameter", outfile);
843 break;
844 case LOC_LOCAL:
845 fputs_filtered ("stack parameter", outfile);
846 break;
847 case LOC_TYPEDEF:
848 fputs_filtered ("type", outfile);
849 break;
850 case LOC_LABEL:
851 fputs_filtered ("label", outfile);
852 break;
853 case LOC_BLOCK:
854 fputs_filtered ("function", outfile);
855 break;
856 case LOC_CONST_BYTES:
857 fputs_filtered ("constant bytes", outfile);
858 break;
859 case LOC_LOCAL_ARG:
860 fputs_filtered ("shuffled arg", outfile);
861 break;
862 case LOC_UNRESOLVED:
863 fputs_filtered ("unresolved", outfile);
864 break;
865 case LOC_OPTIMIZED_OUT:
866 fputs_filtered ("optimized out", outfile);
867 break;
868 case LOC_COMPUTED:
869 case LOC_COMPUTED_ARG:
870 fputs_filtered ("computed at runtime", outfile);
871 break;
872 default:
873 fputs_filtered ("<invalid location>", outfile);
874 break;
875 }
876 fputs_filtered (", ", outfile);
877 fputs_filtered (paddress (SYMBOL_VALUE_ADDRESS (*p)), outfile);
878 fprintf_filtered (outfile, "\n");
879 p++;
880 }
881 }
882
883 void
884 maintenance_print_msymbols (char *args, int from_tty)
885 {
886 char **argv;
887 struct ui_file *outfile;
888 struct cleanup *cleanups;
889 char *filename = DEV_TTY;
890 char *symname = NULL;
891 struct objfile *objfile;
892
893 struct stat sym_st, obj_st;
894
895 dont_repeat ();
896
897 if (args == NULL)
898 {
899 error (_("print-msymbols takes an output file name and optional symbol file name"));
900 }
901 else if ((argv = buildargv (args)) == NULL)
902 {
903 nomem (0);
904 }
905 cleanups = make_cleanup_freeargv (argv);
906
907 if (argv[0] != NULL)
908 {
909 filename = argv[0];
910 /* If a second arg is supplied, it is a source file name to match on */
911 if (argv[1] != NULL)
912 {
913 symname = xfullpath (argv[1]);
914 make_cleanup (xfree, symname);
915 if (symname && stat (symname, &sym_st))
916 perror_with_name (symname);
917 }
918 }
919
920 filename = tilde_expand (filename);
921 make_cleanup (xfree, filename);
922
923 outfile = gdb_fopen (filename, FOPEN_WT);
924 if (outfile == 0)
925 perror_with_name (filename);
926 make_cleanup_ui_file_delete (outfile);
927
928 immediate_quit++;
929 ALL_OBJFILES (objfile)
930 if (symname == NULL
931 || (!stat (objfile->name, &obj_st) && sym_st.st_ino == obj_st.st_ino))
932 dump_msymbols (objfile, outfile);
933 immediate_quit--;
934 fprintf_filtered (outfile, "\n\n");
935 do_cleanups (cleanups);
936 }
937
938 void
939 maintenance_print_objfiles (char *ignore, int from_tty)
940 {
941 struct objfile *objfile;
942
943 dont_repeat ();
944
945 immediate_quit++;
946 ALL_OBJFILES (objfile)
947 dump_objfile (objfile);
948 immediate_quit--;
949 }
950
951
952 /* List all the symbol tables whose names match REGEXP (optional). */
953 void
954 maintenance_info_symtabs (char *regexp, int from_tty)
955 {
956 struct objfile *objfile;
957
958 if (regexp)
959 re_comp (regexp);
960
961 ALL_OBJFILES (objfile)
962 {
963 struct symtab *symtab;
964
965 /* We don't want to print anything for this objfile until we
966 actually find a symtab whose name matches. */
967 int printed_objfile_start = 0;
968
969 ALL_OBJFILE_SYMTABS (objfile, symtab)
970 if (! regexp
971 || re_exec (symtab->filename))
972 {
973 if (! printed_objfile_start)
974 {
975 printf_filtered ("{ objfile %s ", objfile->name);
976 wrap_here (" ");
977 printf_filtered ("((struct objfile *) %p)\n", objfile);
978 printed_objfile_start = 1;
979 }
980
981 printf_filtered (" { symtab %s ", symtab->filename);
982 wrap_here (" ");
983 printf_filtered ("((struct symtab *) %p)\n", symtab);
984 printf_filtered (" dirname %s\n",
985 symtab->dirname ? symtab->dirname : "(null)");
986 printf_filtered (" fullname %s\n",
987 symtab->fullname ? symtab->fullname : "(null)");
988 printf_filtered (" blockvector ((struct blockvector *) %p)%s\n",
989 symtab->blockvector,
990 symtab->primary ? " (primary)" : "");
991 printf_filtered (" linetable ((struct linetable *) %p)\n",
992 symtab->linetable);
993 printf_filtered (" debugformat %s\n", symtab->debugformat);
994 printf_filtered (" }\n");
995 }
996
997 if (printed_objfile_start)
998 printf_filtered ("}\n");
999 }
1000 }
1001
1002
1003 /* List all the partial symbol tables whose names match REGEXP (optional). */
1004 void
1005 maintenance_info_psymtabs (char *regexp, int from_tty)
1006 {
1007 struct objfile *objfile;
1008
1009 if (regexp)
1010 re_comp (regexp);
1011
1012 ALL_OBJFILES (objfile)
1013 {
1014 struct partial_symtab *psymtab;
1015
1016 /* We don't want to print anything for this objfile until we
1017 actually find a symtab whose name matches. */
1018 int printed_objfile_start = 0;
1019
1020 ALL_OBJFILE_PSYMTABS (objfile, psymtab)
1021 if (! regexp
1022 || re_exec (psymtab->filename))
1023 {
1024 if (! printed_objfile_start)
1025 {
1026 printf_filtered ("{ objfile %s ", objfile->name);
1027 wrap_here (" ");
1028 printf_filtered ("((struct objfile *) %p)\n", objfile);
1029 printed_objfile_start = 1;
1030 }
1031
1032 printf_filtered (" { psymtab %s ", psymtab->filename);
1033 wrap_here (" ");
1034 printf_filtered ("((struct partial_symtab *) %p)\n", psymtab);
1035 printf_filtered (" readin %s\n",
1036 psymtab->readin ? "yes" : "no");
1037 printf_filtered (" fullname %s\n",
1038 psymtab->fullname ? psymtab->fullname : "(null)");
1039 printf_filtered (" text addresses ");
1040 fputs_filtered (paddress (psymtab->textlow), gdb_stdout);
1041 printf_filtered (" -- ");
1042 fputs_filtered (paddress (psymtab->texthigh), gdb_stdout);
1043 printf_filtered ("\n");
1044 printf_filtered (" globals ");
1045 if (psymtab->n_global_syms)
1046 {
1047 printf_filtered ("(* (struct partial_symbol **) %p @ %d)\n",
1048 (psymtab->objfile->global_psymbols.list
1049 + psymtab->globals_offset),
1050 psymtab->n_global_syms);
1051 }
1052 else
1053 printf_filtered ("(none)\n");
1054 printf_filtered (" statics ");
1055 if (psymtab->n_static_syms)
1056 {
1057 printf_filtered ("(* (struct partial_symbol **) %p @ %d)\n",
1058 (psymtab->objfile->static_psymbols.list
1059 + psymtab->statics_offset),
1060 psymtab->n_static_syms);
1061 }
1062 else
1063 printf_filtered ("(none)\n");
1064 printf_filtered (" dependencies ");
1065 if (psymtab->number_of_dependencies)
1066 {
1067 int i;
1068
1069 printf_filtered ("{\n");
1070 for (i = 0; i < psymtab->number_of_dependencies; i++)
1071 {
1072 struct partial_symtab *dep = psymtab->dependencies[i];
1073
1074 /* Note the string concatenation there --- no comma. */
1075 printf_filtered (" psymtab %s "
1076 "((struct partial_symtab *) %p)\n",
1077 dep->filename, dep);
1078 }
1079 printf_filtered (" }\n");
1080 }
1081 else
1082 printf_filtered ("(none)\n");
1083 printf_filtered (" }\n");
1084 }
1085
1086 if (printed_objfile_start)
1087 printf_filtered ("}\n");
1088 }
1089 }
1090
1091
1092 /* Check consistency of psymtabs and symtabs. */
1093
1094 void
1095 maintenance_check_symtabs (char *ignore, int from_tty)
1096 {
1097 struct symbol *sym;
1098 struct partial_symbol **psym;
1099 struct symtab *s = NULL;
1100 struct partial_symtab *ps;
1101 struct blockvector *bv;
1102 struct objfile *objfile;
1103 struct block *b;
1104 int length;
1105
1106 ALL_PSYMTABS (objfile, ps)
1107 {
1108 s = PSYMTAB_TO_SYMTAB (ps);
1109 if (s == NULL)
1110 continue;
1111 bv = BLOCKVECTOR (s);
1112 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1113 psym = ps->objfile->static_psymbols.list + ps->statics_offset;
1114 length = ps->n_static_syms;
1115 while (length--)
1116 {
1117 sym = lookup_block_symbol (b, DEPRECATED_SYMBOL_NAME (*psym),
1118 NULL, SYMBOL_DOMAIN (*psym));
1119 if (!sym)
1120 {
1121 printf_filtered ("Static symbol `");
1122 puts_filtered (DEPRECATED_SYMBOL_NAME (*psym));
1123 printf_filtered ("' only found in ");
1124 puts_filtered (ps->filename);
1125 printf_filtered (" psymtab\n");
1126 }
1127 psym++;
1128 }
1129 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1130 psym = ps->objfile->global_psymbols.list + ps->globals_offset;
1131 length = ps->n_global_syms;
1132 while (length--)
1133 {
1134 sym = lookup_block_symbol (b, DEPRECATED_SYMBOL_NAME (*psym),
1135 NULL, SYMBOL_DOMAIN (*psym));
1136 if (!sym)
1137 {
1138 printf_filtered ("Global symbol `");
1139 puts_filtered (DEPRECATED_SYMBOL_NAME (*psym));
1140 printf_filtered ("' only found in ");
1141 puts_filtered (ps->filename);
1142 printf_filtered (" psymtab\n");
1143 }
1144 psym++;
1145 }
1146 if (ps->texthigh < ps->textlow)
1147 {
1148 printf_filtered ("Psymtab ");
1149 puts_filtered (ps->filename);
1150 printf_filtered (" covers bad range ");
1151 fputs_filtered (paddress (ps->textlow), gdb_stdout);
1152 printf_filtered (" - ");
1153 fputs_filtered (paddress (ps->texthigh), gdb_stdout);
1154 printf_filtered ("\n");
1155 continue;
1156 }
1157 if (ps->texthigh == 0)
1158 continue;
1159 if (ps->textlow < BLOCK_START (b) || ps->texthigh > BLOCK_END (b))
1160 {
1161 printf_filtered ("Psymtab ");
1162 puts_filtered (ps->filename);
1163 printf_filtered (" covers ");
1164 fputs_filtered (paddress (ps->textlow), gdb_stdout);
1165 printf_filtered (" - ");
1166 fputs_filtered (paddress (ps->texthigh), gdb_stdout);
1167 printf_filtered (" but symtab covers only ");
1168 fputs_filtered (paddress (BLOCK_START (b)), gdb_stdout);
1169 printf_filtered (" - ");
1170 fputs_filtered (paddress (BLOCK_END (b)), gdb_stdout);
1171 printf_filtered ("\n");
1172 }
1173 }
1174 }
1175 \f
1176
1177 /* Return the nexting depth of a block within other blocks in its symtab. */
1178
1179 static int
1180 block_depth (struct block *block)
1181 {
1182 int i = 0;
1183 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
1184 {
1185 i++;
1186 }
1187 return i;
1188 }
1189 \f
1190
1191 /* Increase the space allocated for LISTP, which is probably
1192 global_psymbols or static_psymbols. This space will eventually
1193 be freed in free_objfile(). */
1194
1195 void
1196 extend_psymbol_list (struct psymbol_allocation_list *listp,
1197 struct objfile *objfile)
1198 {
1199 int new_size;
1200 if (listp->size == 0)
1201 {
1202 new_size = 255;
1203 listp->list = (struct partial_symbol **)
1204 xmalloc (new_size * sizeof (struct partial_symbol *));
1205 }
1206 else
1207 {
1208 new_size = listp->size * 2;
1209 listp->list = (struct partial_symbol **)
1210 xrealloc ((char *) listp->list,
1211 new_size * sizeof (struct partial_symbol *));
1212 }
1213 /* Next assumes we only went one over. Should be good if
1214 program works correctly */
1215 listp->next = listp->list + listp->size;
1216 listp->size = new_size;
1217 }
1218
1219
1220 /* Do early runtime initializations. */
1221 void
1222 _initialize_symmisc (void)
1223 {
1224 std_in = stdin;
1225 std_out = stdout;
1226 std_err = stderr;
1227 }