]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/symmisc.c
24f9176a8a12c8f7353bf6d399457605eb7f285a
[thirdparty/binutils-gdb.git] / gdb / symmisc.c
1 /* Do various things to symbol tables (other than lookup), for GDB.
2
3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "bfd.h"
24 #include "filenames.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "breakpoint.h"
28 #include "command.h"
29 #include "gdb_obstack.h"
30 #include "language.h"
31 #include "bcache.h"
32 #include "block.h"
33 #include "gdb_regex.h"
34 #include <sys/stat.h>
35 #include "dictionary.h"
36 #include "typeprint.h"
37 #include "gdbcmd.h"
38 #include "source.h"
39 #include "readline/readline.h"
40
41 #include "psymtab.h"
42
43 /* Unfortunately for debugging, stderr is usually a macro. This is painful
44 when calling functions that take FILE *'s from the debugger.
45 So we make a variable which has the same value and which is accessible when
46 debugging GDB with itself. Because stdin et al need not be constants,
47 we initialize them in the _initialize_symmisc function at the bottom
48 of the file. */
49 FILE *std_in;
50 FILE *std_out;
51 FILE *std_err;
52
53 /* Prototypes for local functions */
54
55 static int block_depth (struct block *);
56
57 static void print_symbol (struct gdbarch *gdbarch, struct symbol *symbol,
58 int depth, ui_file *outfile);
59 \f
60
61 void
62 print_symbol_bcache_statistics (void)
63 {
64 struct program_space *pspace;
65
66 ALL_PSPACES (pspace)
67 for (objfile *objfile : all_objfiles (pspace))
68 {
69 QUIT;
70 printf_filtered (_("Byte cache statistics for '%s':\n"),
71 objfile_name (objfile));
72 print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
73 "partial symbol cache");
74 print_bcache_statistics (objfile->per_bfd->macro_cache,
75 "preprocessor macro cache");
76 print_bcache_statistics (objfile->per_bfd->filename_cache,
77 "file name cache");
78 }
79 }
80
81 void
82 print_objfile_statistics (void)
83 {
84 struct program_space *pspace;
85 struct compunit_symtab *cu;
86 struct symtab *s;
87 int i, linetables, blockvectors;
88
89 ALL_PSPACES (pspace)
90 for (objfile *objfile : all_objfiles (pspace))
91 {
92 QUIT;
93 printf_filtered (_("Statistics for '%s':\n"), objfile_name (objfile));
94 if (OBJSTAT (objfile, n_stabs) > 0)
95 printf_filtered (_(" Number of \"stab\" symbols read: %d\n"),
96 OBJSTAT (objfile, n_stabs));
97 if (objfile->per_bfd->n_minsyms > 0)
98 printf_filtered (_(" Number of \"minimal\" symbols read: %d\n"),
99 objfile->per_bfd->n_minsyms);
100 if (OBJSTAT (objfile, n_psyms) > 0)
101 printf_filtered (_(" Number of \"partial\" symbols read: %d\n"),
102 OBJSTAT (objfile, n_psyms));
103 if (OBJSTAT (objfile, n_syms) > 0)
104 printf_filtered (_(" Number of \"full\" symbols read: %d\n"),
105 OBJSTAT (objfile, n_syms));
106 if (OBJSTAT (objfile, n_types) > 0)
107 printf_filtered (_(" Number of \"types\" defined: %d\n"),
108 OBJSTAT (objfile, n_types));
109 if (objfile->sf)
110 objfile->sf->qf->print_stats (objfile);
111 i = linetables = blockvectors = 0;
112 ALL_OBJFILE_FILETABS (objfile, cu, s)
113 {
114 i++;
115 if (SYMTAB_LINETABLE (s) != NULL)
116 linetables++;
117 }
118 ALL_OBJFILE_COMPUNITS (objfile, cu)
119 blockvectors++;
120 printf_filtered (_(" Number of symbol tables: %d\n"), i);
121 printf_filtered (_(" Number of symbol tables with line tables: %d\n"),
122 linetables);
123 printf_filtered (_(" Number of symbol tables with blockvectors: %d\n"),
124 blockvectors);
125
126 if (OBJSTAT (objfile, sz_strtab) > 0)
127 printf_filtered (_(" Space used by string tables: %d\n"),
128 OBJSTAT (objfile, sz_strtab));
129 printf_filtered (_(" Total memory used for objfile obstack: %s\n"),
130 pulongest (obstack_memory_used (&objfile
131 ->objfile_obstack)));
132 printf_filtered (_(" Total memory used for BFD obstack: %s\n"),
133 pulongest (obstack_memory_used (&objfile->per_bfd
134 ->storage_obstack)));
135 printf_filtered (_(" Total memory used for psymbol cache: %d\n"),
136 bcache_memory_used (psymbol_bcache_get_bcache
137 (objfile->psymbol_cache)));
138 printf_filtered (_(" Total memory used for macro cache: %d\n"),
139 bcache_memory_used (objfile->per_bfd->macro_cache));
140 printf_filtered (_(" Total memory used for file name cache: %d\n"),
141 bcache_memory_used (objfile->per_bfd->filename_cache));
142 }
143 }
144
145 static void
146 dump_objfile (struct objfile *objfile)
147 {
148 struct compunit_symtab *cust;
149 struct symtab *symtab;
150
151 printf_filtered ("\nObject file %s: ", objfile_name (objfile));
152 printf_filtered ("Objfile at ");
153 gdb_print_host_address (objfile, gdb_stdout);
154 printf_filtered (", bfd at ");
155 gdb_print_host_address (objfile->obfd, gdb_stdout);
156 printf_filtered (", %d minsyms\n\n",
157 objfile->per_bfd->minimal_symbol_count);
158
159 if (objfile->sf)
160 objfile->sf->qf->dump (objfile);
161
162 if (objfile->compunit_symtabs != NULL)
163 {
164 printf_filtered ("Symtabs:\n");
165 ALL_OBJFILE_FILETABS (objfile, cust, symtab)
166 {
167 printf_filtered ("%s at ", symtab_to_filename_for_display (symtab));
168 gdb_print_host_address (symtab, gdb_stdout);
169 printf_filtered (", ");
170 if (SYMTAB_OBJFILE (symtab) != objfile)
171 {
172 printf_filtered ("NOT ON CHAIN! ");
173 }
174 wrap_here (" ");
175 }
176 printf_filtered ("\n\n");
177 }
178 }
179
180 /* Print minimal symbols from this objfile. */
181
182 static void
183 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
184 {
185 struct gdbarch *gdbarch = get_objfile_arch (objfile);
186 int index;
187 char ms_type;
188
189 fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile_name (objfile));
190 if (objfile->per_bfd->minimal_symbol_count == 0)
191 {
192 fprintf_filtered (outfile, "No minimal symbols found.\n");
193 return;
194 }
195 index = 0;
196 for (minimal_symbol *msymbol : objfile_msymbols (objfile))
197 {
198 struct obj_section *section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
199
200 switch (MSYMBOL_TYPE (msymbol))
201 {
202 case mst_unknown:
203 ms_type = 'u';
204 break;
205 case mst_text:
206 ms_type = 'T';
207 break;
208 case mst_text_gnu_ifunc:
209 case mst_data_gnu_ifunc:
210 ms_type = 'i';
211 break;
212 case mst_solib_trampoline:
213 ms_type = 'S';
214 break;
215 case mst_data:
216 ms_type = 'D';
217 break;
218 case mst_bss:
219 ms_type = 'B';
220 break;
221 case mst_abs:
222 ms_type = 'A';
223 break;
224 case mst_file_text:
225 ms_type = 't';
226 break;
227 case mst_file_data:
228 ms_type = 'd';
229 break;
230 case mst_file_bss:
231 ms_type = 'b';
232 break;
233 default:
234 ms_type = '?';
235 break;
236 }
237 fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
238 fputs_filtered (paddress (gdbarch, MSYMBOL_VALUE_ADDRESS (objfile,
239 msymbol)),
240 outfile);
241 fprintf_filtered (outfile, " %s", MSYMBOL_LINKAGE_NAME (msymbol));
242 if (section)
243 {
244 if (section->the_bfd_section != NULL)
245 fprintf_filtered (outfile, " section %s",
246 bfd_section_name (objfile->obfd,
247 section->the_bfd_section));
248 else
249 fprintf_filtered (outfile, " spurious section %ld",
250 (long) (section - objfile->sections));
251 }
252 if (MSYMBOL_DEMANGLED_NAME (msymbol) != NULL)
253 {
254 fprintf_filtered (outfile, " %s", MSYMBOL_DEMANGLED_NAME (msymbol));
255 }
256 if (msymbol->filename)
257 fprintf_filtered (outfile, " %s", msymbol->filename);
258 fputs_filtered ("\n", outfile);
259 index++;
260 }
261 if (objfile->per_bfd->minimal_symbol_count != index)
262 {
263 warning (_("internal error: minimal symbol count %d != %d"),
264 objfile->per_bfd->minimal_symbol_count, index);
265 }
266 fprintf_filtered (outfile, "\n");
267 }
268
269 static void
270 dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
271 {
272 struct objfile *objfile = SYMTAB_OBJFILE (symtab);
273 struct gdbarch *gdbarch = get_objfile_arch (objfile);
274 int i;
275 struct dict_iterator iter;
276 int len;
277 struct linetable *l;
278 const struct blockvector *bv;
279 struct symbol *sym;
280 struct block *b;
281 int depth;
282
283 fprintf_filtered (outfile, "\nSymtab for file %s\n",
284 symtab_to_filename_for_display (symtab));
285 if (SYMTAB_DIRNAME (symtab) != NULL)
286 fprintf_filtered (outfile, "Compilation directory is %s\n",
287 SYMTAB_DIRNAME (symtab));
288 fprintf_filtered (outfile, "Read from object file %s (",
289 objfile_name (objfile));
290 gdb_print_host_address (objfile, outfile);
291 fprintf_filtered (outfile, ")\n");
292 fprintf_filtered (outfile, "Language: %s\n",
293 language_str (symtab->language));
294
295 /* First print the line table. */
296 l = SYMTAB_LINETABLE (symtab);
297 if (l)
298 {
299 fprintf_filtered (outfile, "\nLine table:\n\n");
300 len = l->nitems;
301 for (i = 0; i < len; i++)
302 {
303 fprintf_filtered (outfile, " line %d at ", l->item[i].line);
304 fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
305 fprintf_filtered (outfile, "\n");
306 }
307 }
308 /* Now print the block info, but only for compunit symtabs since we will
309 print lots of duplicate info otherwise. */
310 if (symtab == COMPUNIT_FILETABS (SYMTAB_COMPUNIT (symtab)))
311 {
312 fprintf_filtered (outfile, "\nBlockvector:\n\n");
313 bv = SYMTAB_BLOCKVECTOR (symtab);
314 len = BLOCKVECTOR_NBLOCKS (bv);
315 for (i = 0; i < len; i++)
316 {
317 b = BLOCKVECTOR_BLOCK (bv, i);
318 depth = block_depth (b) * 2;
319 print_spaces (depth, outfile);
320 fprintf_filtered (outfile, "block #%03d, object at ", i);
321 gdb_print_host_address (b, outfile);
322 if (BLOCK_SUPERBLOCK (b))
323 {
324 fprintf_filtered (outfile, " under ");
325 gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
326 }
327 /* drow/2002-07-10: We could save the total symbols count
328 even if we're using a hashtable, but nothing else but this message
329 wants it. */
330 fprintf_filtered (outfile, ", %d syms/buckets in ",
331 dict_size (BLOCK_DICT (b)));
332 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
333 fprintf_filtered (outfile, "..");
334 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
335 if (BLOCK_FUNCTION (b))
336 {
337 fprintf_filtered (outfile, ", function %s",
338 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
339 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
340 {
341 fprintf_filtered (outfile, ", %s",
342 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
343 }
344 }
345 fprintf_filtered (outfile, "\n");
346 /* Now print each symbol in this block (in no particular order, if
347 we're using a hashtable). Note that we only want this
348 block, not any blocks from included symtabs. */
349 ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
350 {
351 TRY
352 {
353 print_symbol (gdbarch, sym, depth + 1, outfile);
354 }
355 CATCH (ex, RETURN_MASK_ERROR)
356 {
357 exception_fprintf (gdb_stderr, ex,
358 "Error printing symbol:\n");
359 }
360 END_CATCH
361 }
362 }
363 fprintf_filtered (outfile, "\n");
364 }
365 else
366 {
367 const char *compunit_filename
368 = symtab_to_filename_for_display (COMPUNIT_FILETABS (SYMTAB_COMPUNIT (symtab)));
369
370 fprintf_filtered (outfile,
371 "\nBlockvector same as owning compunit: %s\n\n",
372 compunit_filename);
373 }
374 }
375
376 static void
377 dump_symtab (struct symtab *symtab, struct ui_file *outfile)
378 {
379 /* Set the current language to the language of the symtab we're dumping
380 because certain routines used during dump_symtab() use the current
381 language to print an image of the symbol. We'll restore it later.
382 But use only real languages, not placeholders. */
383 if (symtab->language != language_unknown
384 && symtab->language != language_auto)
385 {
386 enum language saved_lang;
387
388 saved_lang = set_language (symtab->language);
389
390 dump_symtab_1 (symtab, outfile);
391
392 set_language (saved_lang);
393 }
394 else
395 dump_symtab_1 (symtab, outfile);
396 }
397
398 static void
399 maintenance_print_symbols (const char *args, int from_tty)
400 {
401 struct ui_file *outfile = gdb_stdout;
402 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
403 int i, outfile_idx;
404
405 dont_repeat ();
406
407 gdb_argv argv (args);
408
409 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
410 {
411 if (strcmp (argv[i], "-pc") == 0)
412 {
413 if (argv[i + 1] == NULL)
414 error (_("Missing pc value"));
415 address_arg = argv[++i];
416 }
417 else if (strcmp (argv[i], "-source") == 0)
418 {
419 if (argv[i + 1] == NULL)
420 error (_("Missing source file"));
421 source_arg = argv[++i];
422 }
423 else if (strcmp (argv[i], "-objfile") == 0)
424 {
425 if (argv[i + 1] == NULL)
426 error (_("Missing objfile name"));
427 objfile_arg = argv[++i];
428 }
429 else if (strcmp (argv[i], "--") == 0)
430 {
431 /* End of options. */
432 ++i;
433 break;
434 }
435 else if (argv[i][0] == '-')
436 {
437 /* Future proofing: Don't allow OUTFILE to begin with "-". */
438 error (_("Unknown option: %s"), argv[i]);
439 }
440 else
441 break;
442 }
443 outfile_idx = i;
444
445 if (address_arg != NULL && source_arg != NULL)
446 error (_("Must specify at most one of -pc and -source"));
447
448 stdio_file arg_outfile;
449
450 if (argv != NULL && argv[outfile_idx] != NULL)
451 {
452 if (argv[outfile_idx + 1] != NULL)
453 error (_("Junk at end of command"));
454 gdb::unique_xmalloc_ptr<char> outfile_name
455 (tilde_expand (argv[outfile_idx]));
456 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
457 perror_with_name (outfile_name.get ());
458 outfile = &arg_outfile;
459 }
460
461 if (address_arg != NULL)
462 {
463 CORE_ADDR pc = parse_and_eval_address (address_arg);
464 struct symtab *s = find_pc_line_symtab (pc);
465
466 if (s == NULL)
467 error (_("No symtab for address: %s"), address_arg);
468 dump_symtab (s, outfile);
469 }
470 else
471 {
472 struct compunit_symtab *cu;
473 struct symtab *s;
474 int found = 0;
475
476 for (objfile *objfile : all_objfiles (current_program_space))
477 {
478 int print_for_objfile = 1;
479
480 if (objfile_arg != NULL)
481 print_for_objfile
482 = compare_filenames_for_search (objfile_name (objfile),
483 objfile_arg);
484 if (!print_for_objfile)
485 continue;
486
487 ALL_OBJFILE_FILETABS (objfile, cu, s)
488 {
489 int print_for_source = 0;
490
491 QUIT;
492 if (source_arg != NULL)
493 {
494 print_for_source
495 = compare_filenames_for_search
496 (symtab_to_filename_for_display (s), source_arg);
497 found = 1;
498 }
499 if (source_arg == NULL
500 || print_for_source)
501 dump_symtab (s, outfile);
502 }
503 }
504
505 if (source_arg != NULL && !found)
506 error (_("No symtab for source file: %s"), source_arg);
507 }
508 }
509
510 /* Print symbol SYMBOL on OUTFILE. DEPTH says how far to indent. */
511
512 static void
513 print_symbol (struct gdbarch *gdbarch, struct symbol *symbol,
514 int depth, ui_file *outfile)
515 {
516 struct obj_section *section;
517
518 if (SYMBOL_OBJFILE_OWNED (symbol))
519 section = SYMBOL_OBJ_SECTION (symbol_objfile (symbol), symbol);
520 else
521 section = NULL;
522
523 print_spaces (depth, outfile);
524 if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
525 {
526 fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
527 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
528 outfile);
529 if (section)
530 fprintf_filtered (outfile, " section %s\n",
531 bfd_section_name (section->the_bfd_section->owner,
532 section->the_bfd_section));
533 else
534 fprintf_filtered (outfile, "\n");
535 return;
536 }
537
538 if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
539 {
540 if (TYPE_NAME (SYMBOL_TYPE (symbol)))
541 {
542 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
543 &type_print_raw_options);
544 }
545 else
546 {
547 fprintf_filtered (outfile, "%s %s = ",
548 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
549 ? "enum"
550 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
551 ? "struct" : "union")),
552 SYMBOL_LINKAGE_NAME (symbol));
553 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
554 &type_print_raw_options);
555 }
556 fprintf_filtered (outfile, ";\n");
557 }
558 else
559 {
560 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
561 fprintf_filtered (outfile, "typedef ");
562 if (SYMBOL_TYPE (symbol))
563 {
564 /* Print details of types, except for enums where it's clutter. */
565 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
566 outfile,
567 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
568 depth,
569 &type_print_raw_options);
570 fprintf_filtered (outfile, "; ");
571 }
572 else
573 fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
574
575 switch (SYMBOL_CLASS (symbol))
576 {
577 case LOC_CONST:
578 fprintf_filtered (outfile, "const %s (%s)",
579 plongest (SYMBOL_VALUE (symbol)),
580 hex_string (SYMBOL_VALUE (symbol)));
581 break;
582
583 case LOC_CONST_BYTES:
584 {
585 unsigned i;
586 struct type *type = check_typedef (SYMBOL_TYPE (symbol));
587
588 fprintf_filtered (outfile, "const %u hex bytes:",
589 TYPE_LENGTH (type));
590 for (i = 0; i < TYPE_LENGTH (type); i++)
591 fprintf_filtered (outfile, " %02x",
592 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
593 }
594 break;
595
596 case LOC_STATIC:
597 fprintf_filtered (outfile, "static at ");
598 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
599 outfile);
600 if (section)
601 fprintf_filtered (outfile, " section %s",
602 bfd_section_name (section->the_bfd_section->owner,
603 section->the_bfd_section));
604 break;
605
606 case LOC_REGISTER:
607 if (SYMBOL_IS_ARGUMENT (symbol))
608 fprintf_filtered (outfile, "parameter register %s",
609 plongest (SYMBOL_VALUE (symbol)));
610 else
611 fprintf_filtered (outfile, "register %s",
612 plongest (SYMBOL_VALUE (symbol)));
613 break;
614
615 case LOC_ARG:
616 fprintf_filtered (outfile, "arg at offset %s",
617 hex_string (SYMBOL_VALUE (symbol)));
618 break;
619
620 case LOC_REF_ARG:
621 fprintf_filtered (outfile, "reference arg at %s",
622 hex_string (SYMBOL_VALUE (symbol)));
623 break;
624
625 case LOC_REGPARM_ADDR:
626 fprintf_filtered (outfile, "address parameter register %s",
627 plongest (SYMBOL_VALUE (symbol)));
628 break;
629
630 case LOC_LOCAL:
631 fprintf_filtered (outfile, "local at offset %s",
632 hex_string (SYMBOL_VALUE (symbol)));
633 break;
634
635 case LOC_TYPEDEF:
636 break;
637
638 case LOC_LABEL:
639 fprintf_filtered (outfile, "label at ");
640 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
641 outfile);
642 if (section)
643 fprintf_filtered (outfile, " section %s",
644 bfd_section_name (section->the_bfd_section->owner,
645 section->the_bfd_section));
646 break;
647
648 case LOC_BLOCK:
649 fprintf_filtered (outfile, "block object ");
650 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
651 fprintf_filtered (outfile, ", ");
652 fputs_filtered (paddress (gdbarch,
653 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
654 outfile);
655 fprintf_filtered (outfile, "..");
656 fputs_filtered (paddress (gdbarch,
657 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
658 outfile);
659 if (section)
660 fprintf_filtered (outfile, " section %s",
661 bfd_section_name (section->the_bfd_section->owner,
662 section->the_bfd_section));
663 break;
664
665 case LOC_COMPUTED:
666 fprintf_filtered (outfile, "computed at runtime");
667 break;
668
669 case LOC_UNRESOLVED:
670 fprintf_filtered (outfile, "unresolved");
671 break;
672
673 case LOC_OPTIMIZED_OUT:
674 fprintf_filtered (outfile, "optimized out");
675 break;
676
677 default:
678 fprintf_filtered (outfile, "botched symbol class %x",
679 SYMBOL_CLASS (symbol));
680 break;
681 }
682 }
683 fprintf_filtered (outfile, "\n");
684 }
685
686 static void
687 maintenance_print_msymbols (const char *args, int from_tty)
688 {
689 struct ui_file *outfile = gdb_stdout;
690 char *objfile_arg = NULL;
691 int i, outfile_idx;
692
693 dont_repeat ();
694
695 gdb_argv argv (args);
696
697 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
698 {
699 if (strcmp (argv[i], "-objfile") == 0)
700 {
701 if (argv[i + 1] == NULL)
702 error (_("Missing objfile name"));
703 objfile_arg = argv[++i];
704 }
705 else if (strcmp (argv[i], "--") == 0)
706 {
707 /* End of options. */
708 ++i;
709 break;
710 }
711 else if (argv[i][0] == '-')
712 {
713 /* Future proofing: Don't allow OUTFILE to begin with "-". */
714 error (_("Unknown option: %s"), argv[i]);
715 }
716 else
717 break;
718 }
719 outfile_idx = i;
720
721 stdio_file arg_outfile;
722
723 if (argv != NULL && argv[outfile_idx] != NULL)
724 {
725 if (argv[outfile_idx + 1] != NULL)
726 error (_("Junk at end of command"));
727 gdb::unique_xmalloc_ptr<char> outfile_name
728 (tilde_expand (argv[outfile_idx]));
729 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
730 perror_with_name (outfile_name.get ());
731 outfile = &arg_outfile;
732 }
733
734 for (objfile *objfile : all_objfiles (current_program_space))
735 {
736 QUIT;
737 if (objfile_arg == NULL
738 || compare_filenames_for_search (objfile_name (objfile), objfile_arg))
739 dump_msymbols (objfile, outfile);
740 }
741 }
742
743 static void
744 maintenance_print_objfiles (const char *regexp, int from_tty)
745 {
746 struct program_space *pspace;
747
748 dont_repeat ();
749
750 if (regexp)
751 re_comp (regexp);
752
753 ALL_PSPACES (pspace)
754 for (objfile *objfile : all_objfiles (pspace))
755 {
756 QUIT;
757 if (! regexp
758 || re_exec (objfile_name (objfile)))
759 dump_objfile (objfile);
760 }
761 }
762
763 /* List all the symbol tables whose names match REGEXP (optional). */
764
765 static void
766 maintenance_info_symtabs (const char *regexp, int from_tty)
767 {
768 struct program_space *pspace;
769
770 dont_repeat ();
771
772 if (regexp)
773 re_comp (regexp);
774
775 ALL_PSPACES (pspace)
776 for (objfile *objfile : all_objfiles (pspace))
777 {
778 struct compunit_symtab *cust;
779 struct symtab *symtab;
780
781 /* We don't want to print anything for this objfile until we
782 actually find a symtab whose name matches. */
783 int printed_objfile_start = 0;
784
785 ALL_OBJFILE_COMPUNITS (objfile, cust)
786 {
787 int printed_compunit_symtab_start = 0;
788
789 ALL_COMPUNIT_FILETABS (cust, symtab)
790 {
791 QUIT;
792
793 if (! regexp
794 || re_exec (symtab_to_filename_for_display (symtab)))
795 {
796 if (! printed_objfile_start)
797 {
798 printf_filtered ("{ objfile %s ", objfile_name (objfile));
799 wrap_here (" ");
800 printf_filtered ("((struct objfile *) %s)\n",
801 host_address_to_string (objfile));
802 printed_objfile_start = 1;
803 }
804 if (! printed_compunit_symtab_start)
805 {
806 printf_filtered (" { ((struct compunit_symtab *) %s)\n",
807 host_address_to_string (cust));
808 printf_filtered (" debugformat %s\n",
809 COMPUNIT_DEBUGFORMAT (cust));
810 printf_filtered (" producer %s\n",
811 COMPUNIT_PRODUCER (cust) != NULL
812 ? COMPUNIT_PRODUCER (cust)
813 : "(null)");
814 printf_filtered (" dirname %s\n",
815 COMPUNIT_DIRNAME (cust) != NULL
816 ? COMPUNIT_DIRNAME (cust)
817 : "(null)");
818 printf_filtered (" blockvector"
819 " ((struct blockvector *) %s)\n",
820 host_address_to_string
821 (COMPUNIT_BLOCKVECTOR (cust)));
822 printed_compunit_symtab_start = 1;
823 }
824
825 printf_filtered ("\t{ symtab %s ",
826 symtab_to_filename_for_display (symtab));
827 wrap_here (" ");
828 printf_filtered ("((struct symtab *) %s)\n",
829 host_address_to_string (symtab));
830 printf_filtered ("\t fullname %s\n",
831 symtab->fullname != NULL
832 ? symtab->fullname
833 : "(null)");
834 printf_filtered ("\t "
835 "linetable ((struct linetable *) %s)\n",
836 host_address_to_string (symtab->linetable));
837 printf_filtered ("\t}\n");
838 }
839 }
840
841 if (printed_compunit_symtab_start)
842 printf_filtered (" }\n");
843 }
844
845 if (printed_objfile_start)
846 printf_filtered ("}\n");
847 }
848 }
849
850 /* Check consistency of symtabs.
851 An example of what this checks for is NULL blockvectors.
852 They can happen if there's a bug during debug info reading.
853 GDB assumes they are always non-NULL.
854
855 Note: This does not check for psymtab vs symtab consistency.
856 Use "maint check-psymtabs" for that. */
857
858 static void
859 maintenance_check_symtabs (const char *ignore, int from_tty)
860 {
861 struct program_space *pspace;
862
863 ALL_PSPACES (pspace)
864 for (objfile *objfile : all_objfiles (pspace))
865 {
866 struct compunit_symtab *cust;
867
868 /* We don't want to print anything for this objfile until we
869 actually find something worth printing. */
870 int printed_objfile_start = 0;
871
872 ALL_OBJFILE_COMPUNITS (objfile, cust)
873 {
874 int found_something = 0;
875 struct symtab *symtab = compunit_primary_filetab (cust);
876
877 QUIT;
878
879 if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
880 found_something = 1;
881 /* Add more checks here. */
882
883 if (found_something)
884 {
885 if (! printed_objfile_start)
886 {
887 printf_filtered ("{ objfile %s ", objfile_name (objfile));
888 wrap_here (" ");
889 printf_filtered ("((struct objfile *) %s)\n",
890 host_address_to_string (objfile));
891 printed_objfile_start = 1;
892 }
893 printf_filtered (" { symtab %s\n",
894 symtab_to_filename_for_display (symtab));
895 if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
896 printf_filtered (" NULL blockvector\n");
897 printf_filtered (" }\n");
898 }
899 }
900
901 if (printed_objfile_start)
902 printf_filtered ("}\n");
903 }
904 }
905
906 /* Expand all symbol tables whose name matches an optional regexp. */
907
908 static void
909 maintenance_expand_symtabs (const char *args, int from_tty)
910 {
911 struct program_space *pspace;
912 char *regexp = NULL;
913
914 /* We use buildargv here so that we handle spaces in the regexp
915 in a way that allows adding more arguments later. */
916 gdb_argv argv (args);
917
918 if (argv != NULL)
919 {
920 if (argv[0] != NULL)
921 {
922 regexp = argv[0];
923 if (argv[1] != NULL)
924 error (_("Extra arguments after regexp."));
925 }
926 }
927
928 if (regexp)
929 re_comp (regexp);
930
931 ALL_PSPACES (pspace)
932 for (objfile *objfile : all_objfiles (pspace))
933 {
934 if (objfile->sf)
935 {
936 objfile->sf->qf->expand_symtabs_matching
937 (objfile,
938 [&] (const char *filename, bool basenames)
939 {
940 /* KISS: Only apply the regexp to the complete file name. */
941 return (!basenames
942 && (regexp == NULL || re_exec (filename)));
943 },
944 lookup_name_info::match_any (),
945 [] (const char *symname)
946 {
947 /* Since we're not searching on symbols, just return true. */
948 return true;
949 },
950 NULL,
951 ALL_DOMAIN);
952 }
953 }
954 }
955 \f
956
957 /* Return the nexting depth of a block within other blocks in its symtab. */
958
959 static int
960 block_depth (struct block *block)
961 {
962 int i = 0;
963
964 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
965 {
966 i++;
967 }
968 return i;
969 }
970 \f
971
972 /* Used by MAINTENANCE_INFO_LINE_TABLES to print the information about a
973 single line table. */
974
975 static int
976 maintenance_print_one_line_table (struct symtab *symtab, void *data)
977 {
978 struct linetable *linetable;
979 struct objfile *objfile;
980
981 objfile = symtab->compunit_symtab->objfile;
982 printf_filtered (_("objfile: %s ((struct objfile *) %s)\n"),
983 objfile_name (objfile),
984 host_address_to_string (objfile));
985 printf_filtered (_("compunit_symtab: ((struct compunit_symtab *) %s)\n"),
986 host_address_to_string (symtab->compunit_symtab));
987 printf_filtered (_("symtab: %s ((struct symtab *) %s)\n"),
988 symtab_to_fullname (symtab),
989 host_address_to_string (symtab));
990 linetable = SYMTAB_LINETABLE (symtab);
991 printf_filtered (_("linetable: ((struct linetable *) %s):\n"),
992 host_address_to_string (linetable));
993
994 if (linetable == NULL)
995 printf_filtered (_("No line table.\n"));
996 else if (linetable->nitems <= 0)
997 printf_filtered (_("Line table has no lines.\n"));
998 else
999 {
1000 int i;
1001
1002 /* Leave space for 6 digits of index and line number. After that the
1003 tables will just not format as well. */
1004 printf_filtered (_("%-6s %6s %s\n"),
1005 _("INDEX"), _("LINE"), _("ADDRESS"));
1006
1007 for (i = 0; i < linetable->nitems; ++i)
1008 {
1009 struct linetable_entry *item;
1010
1011 item = &linetable->item [i];
1012 printf_filtered (_("%-6d %6d %s\n"), i, item->line,
1013 core_addr_to_string (item->pc));
1014 }
1015 }
1016
1017 return 0;
1018 }
1019
1020 /* Implement the 'maint info line-table' command. */
1021
1022 static void
1023 maintenance_info_line_tables (const char *regexp, int from_tty)
1024 {
1025 struct program_space *pspace;
1026
1027 dont_repeat ();
1028
1029 if (regexp != NULL)
1030 re_comp (regexp);
1031
1032 ALL_PSPACES (pspace)
1033 for (objfile *objfile : all_objfiles (pspace))
1034 {
1035 struct compunit_symtab *cust;
1036 struct symtab *symtab;
1037
1038 ALL_OBJFILE_COMPUNITS (objfile, cust)
1039 {
1040 ALL_COMPUNIT_FILETABS (cust, symtab)
1041 {
1042 QUIT;
1043
1044 if (regexp == NULL
1045 || re_exec (symtab_to_filename_for_display (symtab)))
1046 maintenance_print_one_line_table (symtab, NULL);
1047 }
1048 }
1049 }
1050 }
1051
1052 \f
1053
1054 /* Do early runtime initializations. */
1055
1056 void
1057 _initialize_symmisc (void)
1058 {
1059 std_in = stdin;
1060 std_out = stdout;
1061 std_err = stderr;
1062
1063 add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
1064 Print dump of current symbol definitions.\n\
1065 Usage: mt print symbols [-pc ADDRESS] [--] [OUTFILE]\n\
1066 mt print symbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
1067 Entries in the full symbol table are dumped to file OUTFILE,\n\
1068 or the terminal if OUTFILE is unspecified.\n\
1069 If ADDRESS is provided, dump only the file for that address.\n\
1070 If SOURCE is provided, dump only that file's symbols.\n\
1071 If OBJFILE is provided, dump only that file's minimal symbols."),
1072 &maintenanceprintlist);
1073
1074 add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
1075 Print dump of current minimal symbol definitions.\n\
1076 Usage: mt print msymbols [-objfile OBJFILE] [--] [OUTFILE]\n\
1077 Entries in the minimal symbol table are dumped to file OUTFILE,\n\
1078 or the terminal if OUTFILE is unspecified.\n\
1079 If OBJFILE is provided, dump only that file's minimal symbols."),
1080 &maintenanceprintlist);
1081
1082 add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
1083 _("Print dump of current object file definitions.\n\
1084 With an argument REGEXP, list the object files with matching names."),
1085 &maintenanceprintlist);
1086
1087 add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
1088 List the full symbol tables for all object files.\n\
1089 This does not include information about individual symbols, blocks, or\n\
1090 linetables --- just the symbol table structures themselves.\n\
1091 With an argument REGEXP, list the symbol tables with matching names."),
1092 &maintenanceinfolist);
1093
1094 add_cmd ("line-table", class_maintenance, maintenance_info_line_tables, _("\
1095 List the contents of all line tables, from all symbol tables.\n\
1096 With an argument REGEXP, list just the line tables for the symbol\n\
1097 tables with matching names."),
1098 &maintenanceinfolist);
1099
1100 add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
1101 _("\
1102 Check consistency of currently expanded symtabs."),
1103 &maintenancelist);
1104
1105 add_cmd ("expand-symtabs", class_maintenance, maintenance_expand_symtabs,
1106 _("Expand symbol tables.\n\
1107 With an argument REGEXP, only expand the symbol tables with matching names."),
1108 &maintenancelist);
1109 }