Document the GDB 10.2 release in gdb/ChangeLog
[binutils-gdb.git] / gdb / symmisc.c
blobd992c67163554f16cee3a0a71c9b034868448454
1 /* Do various things to symbol tables (other than lookup), for GDB.
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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/tilde.h"
41 /* Prototypes for local functions */
43 static int block_depth (const struct block *);
45 static void print_symbol (struct gdbarch *gdbarch, struct symbol *symbol,
46 int depth, ui_file *outfile);
49 void
50 print_objfile_statistics (void)
52 int i, linetables, blockvectors;
54 for (struct program_space *pspace : program_spaces)
55 for (objfile *objfile : pspace->objfiles ())
57 QUIT;
58 printf_filtered (_("Statistics for '%s':\n"), objfile_name (objfile));
59 if (OBJSTAT (objfile, n_stabs) > 0)
60 printf_filtered (_(" Number of \"stab\" symbols read: %d\n"),
61 OBJSTAT (objfile, n_stabs));
62 if (objfile->per_bfd->n_minsyms > 0)
63 printf_filtered (_(" Number of \"minimal\" symbols read: %d\n"),
64 objfile->per_bfd->n_minsyms);
65 if (OBJSTAT (objfile, n_syms) > 0)
66 printf_filtered (_(" Number of \"full\" symbols read: %d\n"),
67 OBJSTAT (objfile, n_syms));
68 if (OBJSTAT (objfile, n_types) > 0)
69 printf_filtered (_(" Number of \"types\" defined: %d\n"),
70 OBJSTAT (objfile, n_types));
72 i = linetables = 0;
73 for (compunit_symtab *cu : objfile->compunits ())
75 for (symtab *s : compunit_filetabs (cu))
77 i++;
78 if (SYMTAB_LINETABLE (s) != NULL)
79 linetables++;
82 blockvectors = std::distance (objfile->compunits ().begin (),
83 objfile->compunits ().end ());
84 printf_filtered (_(" Number of symbol tables: %d\n"), i);
85 printf_filtered (_(" Number of symbol tables with line tables: %d\n"),
86 linetables);
87 printf_filtered (_(" Number of symbol tables with blockvectors: %d\n"),
88 blockvectors);
90 objfile->print_stats (false);
92 if (OBJSTAT (objfile, sz_strtab) > 0)
93 printf_filtered (_(" Space used by string tables: %d\n"),
94 OBJSTAT (objfile, sz_strtab));
95 printf_filtered (_(" Total memory used for objfile obstack: %s\n"),
96 pulongest (obstack_memory_used (&objfile
97 ->objfile_obstack)));
98 printf_filtered (_(" Total memory used for BFD obstack: %s\n"),
99 pulongest (obstack_memory_used (&objfile->per_bfd
100 ->storage_obstack)));
102 printf_filtered (_(" Total memory used for string cache: %d\n"),
103 objfile->per_bfd->string_cache.memory_used ());
104 printf_filtered (_("Byte cache statistics for '%s':\n"),
105 objfile_name (objfile));
106 objfile->per_bfd->string_cache.print_statistics ("string cache");
107 objfile->print_stats (true);
111 static void
112 dump_objfile (struct objfile *objfile)
114 printf_filtered ("\nObject file %s: ", objfile_name (objfile));
115 printf_filtered ("Objfile at ");
116 gdb_print_host_address (objfile, gdb_stdout);
117 printf_filtered (", bfd at ");
118 gdb_print_host_address (objfile->obfd, gdb_stdout);
119 printf_filtered (", %d minsyms\n\n",
120 objfile->per_bfd->minimal_symbol_count);
122 objfile->dump ();
124 if (objfile->compunit_symtabs != NULL)
126 printf_filtered ("Symtabs:\n");
127 for (compunit_symtab *cu : objfile->compunits ())
129 for (symtab *symtab : compunit_filetabs (cu))
131 printf_filtered ("%s at ",
132 symtab_to_filename_for_display (symtab));
133 gdb_print_host_address (symtab, gdb_stdout);
134 printf_filtered (", ");
135 if (SYMTAB_OBJFILE (symtab) != objfile)
137 printf_filtered ("NOT ON CHAIN! ");
139 wrap_here (" ");
142 printf_filtered ("\n\n");
146 /* Print minimal symbols from this objfile. */
148 static void
149 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
151 struct gdbarch *gdbarch = objfile->arch ();
152 int index;
153 char ms_type;
155 fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile_name (objfile));
156 if (objfile->per_bfd->minimal_symbol_count == 0)
158 fprintf_filtered (outfile, "No minimal symbols found.\n");
159 return;
161 index = 0;
162 for (minimal_symbol *msymbol : objfile->msymbols ())
164 struct obj_section *section = msymbol->obj_section (objfile);
166 switch (MSYMBOL_TYPE (msymbol))
168 case mst_unknown:
169 ms_type = 'u';
170 break;
171 case mst_text:
172 ms_type = 'T';
173 break;
174 case mst_text_gnu_ifunc:
175 case mst_data_gnu_ifunc:
176 ms_type = 'i';
177 break;
178 case mst_solib_trampoline:
179 ms_type = 'S';
180 break;
181 case mst_data:
182 ms_type = 'D';
183 break;
184 case mst_bss:
185 ms_type = 'B';
186 break;
187 case mst_abs:
188 ms_type = 'A';
189 break;
190 case mst_file_text:
191 ms_type = 't';
192 break;
193 case mst_file_data:
194 ms_type = 'd';
195 break;
196 case mst_file_bss:
197 ms_type = 'b';
198 break;
199 default:
200 ms_type = '?';
201 break;
203 fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
205 /* Use the relocated address as shown in the symbol here -- do
206 not try to respect copy relocations. */
207 CORE_ADDR addr = (msymbol->value.address
208 + objfile->section_offsets[msymbol->section_index ()]);
209 fputs_filtered (paddress (gdbarch, addr), outfile);
210 fprintf_filtered (outfile, " %s", msymbol->linkage_name ());
211 if (section)
213 if (section->the_bfd_section != NULL)
214 fprintf_filtered (outfile, " section %s",
215 bfd_section_name (section->the_bfd_section));
216 else
217 fprintf_filtered (outfile, " spurious section %ld",
218 (long) (section - objfile->sections));
220 if (msymbol->demangled_name () != NULL)
222 fprintf_filtered (outfile, " %s", msymbol->demangled_name ());
224 if (msymbol->filename)
225 fprintf_filtered (outfile, " %s", msymbol->filename);
226 fputs_filtered ("\n", outfile);
227 index++;
229 if (objfile->per_bfd->minimal_symbol_count != index)
231 warning (_("internal error: minimal symbol count %d != %d"),
232 objfile->per_bfd->minimal_symbol_count, index);
234 fprintf_filtered (outfile, "\n");
237 static void
238 dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
240 struct objfile *objfile = SYMTAB_OBJFILE (symtab);
241 struct gdbarch *gdbarch = objfile->arch ();
242 int i;
243 struct mdict_iterator miter;
244 int len;
245 struct linetable *l;
246 const struct blockvector *bv;
247 struct symbol *sym;
248 const struct block *b;
249 int depth;
251 fprintf_filtered (outfile, "\nSymtab for file %s at %s\n",
252 symtab_to_filename_for_display (symtab),
253 host_address_to_string (symtab));
255 if (SYMTAB_DIRNAME (symtab) != NULL)
256 fprintf_filtered (outfile, "Compilation directory is %s\n",
257 SYMTAB_DIRNAME (symtab));
258 fprintf_filtered (outfile, "Read from object file %s (",
259 objfile_name (objfile));
260 gdb_print_host_address (objfile, outfile);
261 fprintf_filtered (outfile, ")\n");
262 fprintf_filtered (outfile, "Language: %s\n",
263 language_str (symtab->language));
265 /* First print the line table. */
266 l = SYMTAB_LINETABLE (symtab);
267 if (l)
269 fprintf_filtered (outfile, "\nLine table:\n\n");
270 len = l->nitems;
271 for (i = 0; i < len; i++)
273 fprintf_filtered (outfile, " line %d at ", l->item[i].line);
274 fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
275 if (l->item[i].is_stmt)
276 fprintf_filtered (outfile, "\t(stmt)");
277 fprintf_filtered (outfile, "\n");
280 /* Now print the block info, but only for compunit symtabs since we will
281 print lots of duplicate info otherwise. */
282 if (is_main_symtab_of_compunit_symtab (symtab))
284 fprintf_filtered (outfile, "\nBlockvector:\n\n");
285 bv = SYMTAB_BLOCKVECTOR (symtab);
286 len = BLOCKVECTOR_NBLOCKS (bv);
287 for (i = 0; i < len; i++)
289 b = BLOCKVECTOR_BLOCK (bv, i);
290 depth = block_depth (b) * 2;
291 print_spaces (depth, outfile);
292 fprintf_filtered (outfile, "block #%03d, object at ", i);
293 gdb_print_host_address (b, outfile);
294 if (BLOCK_SUPERBLOCK (b))
296 fprintf_filtered (outfile, " under ");
297 gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
299 /* drow/2002-07-10: We could save the total symbols count
300 even if we're using a hashtable, but nothing else but this message
301 wants it. */
302 fprintf_filtered (outfile, ", %d syms/buckets in ",
303 mdict_size (BLOCK_MULTIDICT (b)));
304 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
305 fprintf_filtered (outfile, "..");
306 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
307 if (BLOCK_FUNCTION (b))
309 fprintf_filtered (outfile, ", function %s",
310 BLOCK_FUNCTION (b)->linkage_name ());
311 if (BLOCK_FUNCTION (b)->demangled_name () != NULL)
313 fprintf_filtered (outfile, ", %s",
314 BLOCK_FUNCTION (b)->demangled_name ());
317 fprintf_filtered (outfile, "\n");
318 /* Now print each symbol in this block (in no particular order, if
319 we're using a hashtable). Note that we only want this
320 block, not any blocks from included symtabs. */
321 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (b), miter, sym)
325 print_symbol (gdbarch, sym, depth + 1, outfile);
327 catch (const gdb_exception_error &ex)
329 exception_fprintf (gdb_stderr, ex,
330 "Error printing symbol:\n");
334 fprintf_filtered (outfile, "\n");
336 else
338 const char *compunit_filename
339 = symtab_to_filename_for_display (COMPUNIT_FILETABS (SYMTAB_COMPUNIT (symtab)));
341 fprintf_filtered (outfile,
342 "\nBlockvector same as owning compunit: %s\n\n",
343 compunit_filename);
346 /* Print info about the user of this compunit_symtab, and the
347 compunit_symtabs included by this one. */
348 if (is_main_symtab_of_compunit_symtab (symtab))
350 struct compunit_symtab *cust = SYMTAB_COMPUNIT (symtab);
352 if (cust->user != nullptr)
354 const char *addr
355 = host_address_to_string (COMPUNIT_FILETABS (cust->user));
356 fprintf_filtered (outfile, "Compunit user: %s\n", addr);
358 if (cust->includes != nullptr)
359 for (i = 0; ; ++i)
361 struct compunit_symtab *include = cust->includes[i];
362 if (include == nullptr)
363 break;
364 const char *addr
365 = host_address_to_string (COMPUNIT_FILETABS (include));
366 fprintf_filtered (outfile, "Compunit include: %s\n", addr);
371 static void
372 dump_symtab (struct symtab *symtab, struct ui_file *outfile)
374 /* Set the current language to the language of the symtab we're dumping
375 because certain routines used during dump_symtab() use the current
376 language to print an image of the symbol. We'll restore it later.
377 But use only real languages, not placeholders. */
378 if (symtab->language != language_unknown
379 && symtab->language != language_auto)
381 scoped_restore_current_language save_lang;
382 set_language (symtab->language);
383 dump_symtab_1 (symtab, outfile);
385 else
386 dump_symtab_1 (symtab, outfile);
389 static void
390 maintenance_print_symbols (const char *args, int from_tty)
392 struct ui_file *outfile = gdb_stdout;
393 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
394 int i, outfile_idx;
396 dont_repeat ();
398 gdb_argv argv (args);
400 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
402 if (strcmp (argv[i], "-pc") == 0)
404 if (argv[i + 1] == NULL)
405 error (_("Missing pc value"));
406 address_arg = argv[++i];
408 else if (strcmp (argv[i], "-source") == 0)
410 if (argv[i + 1] == NULL)
411 error (_("Missing source file"));
412 source_arg = argv[++i];
414 else if (strcmp (argv[i], "-objfile") == 0)
416 if (argv[i + 1] == NULL)
417 error (_("Missing objfile name"));
418 objfile_arg = argv[++i];
420 else if (strcmp (argv[i], "--") == 0)
422 /* End of options. */
423 ++i;
424 break;
426 else if (argv[i][0] == '-')
428 /* Future proofing: Don't allow OUTFILE to begin with "-". */
429 error (_("Unknown option: %s"), argv[i]);
431 else
432 break;
434 outfile_idx = i;
436 if (address_arg != NULL && source_arg != NULL)
437 error (_("Must specify at most one of -pc and -source"));
439 stdio_file arg_outfile;
441 if (argv != NULL && argv[outfile_idx] != NULL)
443 if (argv[outfile_idx + 1] != NULL)
444 error (_("Junk at end of command"));
445 gdb::unique_xmalloc_ptr<char> outfile_name
446 (tilde_expand (argv[outfile_idx]));
447 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
448 perror_with_name (outfile_name.get ());
449 outfile = &arg_outfile;
452 if (address_arg != NULL)
454 CORE_ADDR pc = parse_and_eval_address (address_arg);
455 struct symtab *s = find_pc_line_symtab (pc);
457 if (s == NULL)
458 error (_("No symtab for address: %s"), address_arg);
459 dump_symtab (s, outfile);
461 else
463 int found = 0;
465 for (objfile *objfile : current_program_space->objfiles ())
467 int print_for_objfile = 1;
469 if (objfile_arg != NULL)
470 print_for_objfile
471 = compare_filenames_for_search (objfile_name (objfile),
472 objfile_arg);
473 if (!print_for_objfile)
474 continue;
476 for (compunit_symtab *cu : objfile->compunits ())
478 for (symtab *s : compunit_filetabs (cu))
480 int print_for_source = 0;
482 QUIT;
483 if (source_arg != NULL)
485 print_for_source
486 = compare_filenames_for_search
487 (symtab_to_filename_for_display (s), source_arg);
488 found = 1;
490 if (source_arg == NULL
491 || print_for_source)
492 dump_symtab (s, outfile);
497 if (source_arg != NULL && !found)
498 error (_("No symtab for source file: %s"), source_arg);
502 /* Print symbol SYMBOL on OUTFILE. DEPTH says how far to indent. */
504 static void
505 print_symbol (struct gdbarch *gdbarch, struct symbol *symbol,
506 int depth, ui_file *outfile)
508 struct obj_section *section;
510 if (SYMBOL_OBJFILE_OWNED (symbol))
511 section = symbol->obj_section (symbol_objfile (symbol));
512 else
513 section = NULL;
515 print_spaces (depth, outfile);
516 if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
518 fprintf_filtered (outfile, "label %s at ", symbol->print_name ());
519 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
520 outfile);
521 if (section)
522 fprintf_filtered (outfile, " section %s\n",
523 bfd_section_name (section->the_bfd_section));
524 else
525 fprintf_filtered (outfile, "\n");
526 return;
529 if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
531 if (SYMBOL_TYPE (symbol)->name ())
533 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
534 &type_print_raw_options);
536 else
538 fprintf_filtered (outfile, "%s %s = ",
539 (SYMBOL_TYPE (symbol)->code () == TYPE_CODE_ENUM
540 ? "enum"
541 : (SYMBOL_TYPE (symbol)->code () == TYPE_CODE_STRUCT
542 ? "struct" : "union")),
543 symbol->linkage_name ());
544 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
545 &type_print_raw_options);
547 fprintf_filtered (outfile, ";\n");
549 else
551 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
552 fprintf_filtered (outfile, "typedef ");
553 if (SYMBOL_TYPE (symbol))
555 /* Print details of types, except for enums where it's clutter. */
556 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), symbol->print_name (),
557 outfile,
558 SYMBOL_TYPE (symbol)->code () != TYPE_CODE_ENUM,
559 depth,
560 &type_print_raw_options);
561 fprintf_filtered (outfile, "; ");
563 else
564 fprintf_filtered (outfile, "%s ", symbol->print_name ());
566 switch (SYMBOL_CLASS (symbol))
568 case LOC_CONST:
569 fprintf_filtered (outfile, "const %s (%s)",
570 plongest (SYMBOL_VALUE (symbol)),
571 hex_string (SYMBOL_VALUE (symbol)));
572 break;
574 case LOC_CONST_BYTES:
576 unsigned i;
577 struct type *type = check_typedef (SYMBOL_TYPE (symbol));
579 fprintf_filtered (outfile, "const %s hex bytes:",
580 pulongest (TYPE_LENGTH (type)));
581 for (i = 0; i < TYPE_LENGTH (type); i++)
582 fprintf_filtered (outfile, " %02x",
583 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
585 break;
587 case LOC_STATIC:
588 fprintf_filtered (outfile, "static at ");
589 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
590 outfile);
591 if (section)
592 fprintf_filtered (outfile, " section %s",
593 bfd_section_name (section->the_bfd_section));
594 break;
596 case LOC_REGISTER:
597 if (SYMBOL_IS_ARGUMENT (symbol))
598 fprintf_filtered (outfile, "parameter register %s",
599 plongest (SYMBOL_VALUE (symbol)));
600 else
601 fprintf_filtered (outfile, "register %s",
602 plongest (SYMBOL_VALUE (symbol)));
603 break;
605 case LOC_ARG:
606 fprintf_filtered (outfile, "arg at offset %s",
607 hex_string (SYMBOL_VALUE (symbol)));
608 break;
610 case LOC_REF_ARG:
611 fprintf_filtered (outfile, "reference arg at %s",
612 hex_string (SYMBOL_VALUE (symbol)));
613 break;
615 case LOC_REGPARM_ADDR:
616 fprintf_filtered (outfile, "address parameter register %s",
617 plongest (SYMBOL_VALUE (symbol)));
618 break;
620 case LOC_LOCAL:
621 fprintf_filtered (outfile, "local at offset %s",
622 hex_string (SYMBOL_VALUE (symbol)));
623 break;
625 case LOC_TYPEDEF:
626 break;
628 case LOC_LABEL:
629 fprintf_filtered (outfile, "label at ");
630 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
631 outfile);
632 if (section)
633 fprintf_filtered (outfile, " section %s",
634 bfd_section_name (section->the_bfd_section));
635 break;
637 case LOC_BLOCK:
638 fprintf_filtered (outfile, "block object ");
639 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
640 fprintf_filtered (outfile, ", ");
641 fputs_filtered (paddress (gdbarch,
642 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
643 outfile);
644 fprintf_filtered (outfile, "..");
645 fputs_filtered (paddress (gdbarch,
646 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
647 outfile);
648 if (section)
649 fprintf_filtered (outfile, " section %s",
650 bfd_section_name (section->the_bfd_section));
651 break;
653 case LOC_COMPUTED:
654 fprintf_filtered (outfile, "computed at runtime");
655 break;
657 case LOC_UNRESOLVED:
658 fprintf_filtered (outfile, "unresolved");
659 break;
661 case LOC_OPTIMIZED_OUT:
662 fprintf_filtered (outfile, "optimized out");
663 break;
665 default:
666 fprintf_filtered (outfile, "botched symbol class %x",
667 SYMBOL_CLASS (symbol));
668 break;
671 fprintf_filtered (outfile, "\n");
674 static void
675 maintenance_print_msymbols (const char *args, int from_tty)
677 struct ui_file *outfile = gdb_stdout;
678 char *objfile_arg = NULL;
679 int i, outfile_idx;
681 dont_repeat ();
683 gdb_argv argv (args);
685 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
687 if (strcmp (argv[i], "-objfile") == 0)
689 if (argv[i + 1] == NULL)
690 error (_("Missing objfile name"));
691 objfile_arg = argv[++i];
693 else if (strcmp (argv[i], "--") == 0)
695 /* End of options. */
696 ++i;
697 break;
699 else if (argv[i][0] == '-')
701 /* Future proofing: Don't allow OUTFILE to begin with "-". */
702 error (_("Unknown option: %s"), argv[i]);
704 else
705 break;
707 outfile_idx = i;
709 stdio_file arg_outfile;
711 if (argv != NULL && argv[outfile_idx] != NULL)
713 if (argv[outfile_idx + 1] != NULL)
714 error (_("Junk at end of command"));
715 gdb::unique_xmalloc_ptr<char> outfile_name
716 (tilde_expand (argv[outfile_idx]));
717 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
718 perror_with_name (outfile_name.get ());
719 outfile = &arg_outfile;
722 for (objfile *objfile : current_program_space->objfiles ())
724 QUIT;
725 if (objfile_arg == NULL
726 || compare_filenames_for_search (objfile_name (objfile), objfile_arg))
727 dump_msymbols (objfile, outfile);
731 static void
732 maintenance_print_objfiles (const char *regexp, int from_tty)
734 dont_repeat ();
736 if (regexp)
737 re_comp (regexp);
739 for (struct program_space *pspace : program_spaces)
740 for (objfile *objfile : pspace->objfiles ())
742 QUIT;
743 if (! regexp
744 || re_exec (objfile_name (objfile)))
745 dump_objfile (objfile);
749 /* List all the symbol tables whose names match REGEXP (optional). */
751 static void
752 maintenance_info_symtabs (const char *regexp, int from_tty)
754 dont_repeat ();
756 if (regexp)
757 re_comp (regexp);
759 for (struct program_space *pspace : program_spaces)
760 for (objfile *objfile : pspace->objfiles ())
762 /* We don't want to print anything for this objfile until we
763 actually find a symtab whose name matches. */
764 int printed_objfile_start = 0;
766 for (compunit_symtab *cust : objfile->compunits ())
768 int printed_compunit_symtab_start = 0;
770 for (symtab *symtab : compunit_filetabs (cust))
772 QUIT;
774 if (! regexp
775 || re_exec (symtab_to_filename_for_display (symtab)))
777 if (! printed_objfile_start)
779 printf_filtered ("{ objfile %s ", objfile_name (objfile));
780 wrap_here (" ");
781 printf_filtered ("((struct objfile *) %s)\n",
782 host_address_to_string (objfile));
783 printed_objfile_start = 1;
785 if (! printed_compunit_symtab_start)
787 printf_filtered (" { ((struct compunit_symtab *) %s)\n",
788 host_address_to_string (cust));
789 printf_filtered (" debugformat %s\n",
790 COMPUNIT_DEBUGFORMAT (cust));
791 printf_filtered (" producer %s\n",
792 COMPUNIT_PRODUCER (cust) != NULL
793 ? COMPUNIT_PRODUCER (cust)
794 : "(null)");
795 printf_filtered (" dirname %s\n",
796 COMPUNIT_DIRNAME (cust) != NULL
797 ? COMPUNIT_DIRNAME (cust)
798 : "(null)");
799 printf_filtered (" blockvector"
800 " ((struct blockvector *) %s)\n",
801 host_address_to_string
802 (COMPUNIT_BLOCKVECTOR (cust)));
803 printf_filtered (" user"
804 " ((struct compunit_symtab *) %s)\n",
805 cust->user != nullptr
806 ? host_address_to_string (cust->user)
807 : "(null)");
808 if (cust->includes != nullptr)
810 printf_filtered (" ( includes\n");
811 for (int i = 0; ; ++i)
813 struct compunit_symtab *include
814 = cust->includes[i];
815 if (include == nullptr)
816 break;
817 const char *addr
818 = host_address_to_string (include);
819 printf_filtered (" (%s %s)\n",
820 "(struct compunit_symtab *)",
821 addr);
823 printf_filtered (" )\n");
825 printed_compunit_symtab_start = 1;
828 printf_filtered ("\t{ symtab %s ",
829 symtab_to_filename_for_display (symtab));
830 wrap_here (" ");
831 printf_filtered ("((struct symtab *) %s)\n",
832 host_address_to_string (symtab));
833 printf_filtered ("\t fullname %s\n",
834 symtab->fullname != NULL
835 ? symtab->fullname
836 : "(null)");
837 printf_filtered ("\t "
838 "linetable ((struct linetable *) %s)\n",
839 host_address_to_string (symtab->linetable));
840 printf_filtered ("\t}\n");
844 if (printed_compunit_symtab_start)
845 printf_filtered (" }\n");
848 if (printed_objfile_start)
849 printf_filtered ("}\n");
853 /* Check consistency of symtabs.
854 An example of what this checks for is NULL blockvectors.
855 They can happen if there's a bug during debug info reading.
856 GDB assumes they are always non-NULL.
858 Note: This does not check for psymtab vs symtab consistency.
859 Use "maint check-psymtabs" for that. */
861 static void
862 maintenance_check_symtabs (const char *ignore, int from_tty)
864 for (struct program_space *pspace : program_spaces)
865 for (objfile *objfile : pspace->objfiles ())
867 /* We don't want to print anything for this objfile until we
868 actually find something worth printing. */
869 int printed_objfile_start = 0;
871 for (compunit_symtab *cust : objfile->compunits ())
873 int found_something = 0;
874 struct symtab *symtab = compunit_primary_filetab (cust);
876 QUIT;
878 if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
879 found_something = 1;
880 /* Add more checks here. */
882 if (found_something)
884 if (! printed_objfile_start)
886 printf_filtered ("{ objfile %s ", objfile_name (objfile));
887 wrap_here (" ");
888 printf_filtered ("((struct objfile *) %s)\n",
889 host_address_to_string (objfile));
890 printed_objfile_start = 1;
892 printf_filtered (" { symtab %s\n",
893 symtab_to_filename_for_display (symtab));
894 if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
895 printf_filtered (" NULL blockvector\n");
896 printf_filtered (" }\n");
900 if (printed_objfile_start)
901 printf_filtered ("}\n");
905 /* Expand all symbol tables whose name matches an optional regexp. */
907 static void
908 maintenance_expand_symtabs (const char *args, int from_tty)
910 char *regexp = NULL;
912 /* We use buildargv here so that we handle spaces in the regexp
913 in a way that allows adding more arguments later. */
914 gdb_argv argv (args);
916 if (argv != NULL)
918 if (argv[0] != NULL)
920 regexp = argv[0];
921 if (argv[1] != NULL)
922 error (_("Extra arguments after regexp."));
926 if (regexp)
927 re_comp (regexp);
929 for (struct program_space *pspace : program_spaces)
930 for (objfile *objfile : pspace->objfiles ())
931 objfile->expand_symtabs_matching
932 ([&] (const char *filename, bool basenames)
934 /* KISS: Only apply the regexp to the complete file name. */
935 return (!basenames
936 && (regexp == NULL || re_exec (filename)));
938 NULL,
939 NULL,
940 NULL,
941 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
942 UNDEF_DOMAIN,
943 ALL_DOMAIN);
947 /* Return the nexting depth of a block within other blocks in its symtab. */
949 static int
950 block_depth (const struct block *block)
952 int i = 0;
954 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
956 i++;
958 return i;
962 /* Used by MAINTENANCE_INFO_LINE_TABLES to print the information about a
963 single line table. */
965 static int
966 maintenance_print_one_line_table (struct symtab *symtab, void *data)
968 struct linetable *linetable;
969 struct objfile *objfile;
971 objfile = symtab->compunit_symtab->objfile;
972 printf_filtered (_("objfile: %s ((struct objfile *) %s)\n"),
973 objfile_name (objfile),
974 host_address_to_string (objfile));
975 printf_filtered (_("compunit_symtab: ((struct compunit_symtab *) %s)\n"),
976 host_address_to_string (symtab->compunit_symtab));
977 printf_filtered (_("symtab: %s ((struct symtab *) %s)\n"),
978 symtab_to_fullname (symtab),
979 host_address_to_string (symtab));
980 linetable = SYMTAB_LINETABLE (symtab);
981 printf_filtered (_("linetable: ((struct linetable *) %s):\n"),
982 host_address_to_string (linetable));
984 if (linetable == NULL)
985 printf_filtered (_("No line table.\n"));
986 else if (linetable->nitems <= 0)
987 printf_filtered (_("Line table has no lines.\n"));
988 else
990 /* Leave space for 6 digits of index and line number. After that the
991 tables will just not format as well. */
992 struct ui_out *uiout = current_uiout;
993 ui_out_emit_table table_emitter (uiout, 4, -1, "line-table");
994 uiout->table_header (6, ui_left, "index", _("INDEX"));
995 uiout->table_header (6, ui_left, "line", _("LINE"));
996 uiout->table_header (18, ui_left, "address", _("ADDRESS"));
997 uiout->table_header (1, ui_left, "is-stmt", _("IS-STMT"));
998 uiout->table_body ();
1000 for (int i = 0; i < linetable->nitems; ++i)
1002 struct linetable_entry *item;
1004 item = &linetable->item [i];
1005 ui_out_emit_tuple tuple_emitter (uiout, nullptr);
1006 uiout->field_signed ("index", i);
1007 if (item->line > 0)
1008 uiout->field_signed ("line", item->line);
1009 else
1010 uiout->field_string ("line", _("END"));
1011 uiout->field_core_addr ("address", objfile->arch (),
1012 item->pc);
1013 uiout->field_string ("is-stmt", item->is_stmt ? "Y" : "");
1014 uiout->text ("\n");
1018 return 0;
1021 /* Implement the 'maint info line-table' command. */
1023 static void
1024 maintenance_info_line_tables (const char *regexp, int from_tty)
1026 dont_repeat ();
1028 if (regexp != NULL)
1029 re_comp (regexp);
1031 for (struct program_space *pspace : program_spaces)
1032 for (objfile *objfile : pspace->objfiles ())
1034 for (compunit_symtab *cust : objfile->compunits ())
1036 for (symtab *symtab : compunit_filetabs (cust))
1038 QUIT;
1040 if (regexp == NULL
1041 || re_exec (symtab_to_filename_for_display (symtab)))
1042 maintenance_print_one_line_table (symtab, NULL);
1050 /* Do early runtime initializations. */
1052 void _initialize_symmisc ();
1053 void
1054 _initialize_symmisc ()
1056 add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
1057 Print dump of current symbol definitions.\n\
1058 Usage: mt print symbols [-pc ADDRESS] [--] [OUTFILE]\n\
1059 mt print symbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
1060 Entries in the full symbol table are dumped to file OUTFILE,\n\
1061 or the terminal if OUTFILE is unspecified.\n\
1062 If ADDRESS is provided, dump only the file for that address.\n\
1063 If SOURCE is provided, dump only that file's symbols.\n\
1064 If OBJFILE is provided, dump only that file's minimal symbols."),
1065 &maintenanceprintlist);
1067 add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
1068 Print dump of current minimal symbol definitions.\n\
1069 Usage: mt print msymbols [-objfile OBJFILE] [--] [OUTFILE]\n\
1070 Entries in the minimal symbol table are dumped to file OUTFILE,\n\
1071 or the terminal if OUTFILE is unspecified.\n\
1072 If OBJFILE is provided, dump only that file's minimal symbols."),
1073 &maintenanceprintlist);
1075 add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
1076 _("Print dump of current object file definitions.\n\
1077 With an argument REGEXP, list the object files with matching names."),
1078 &maintenanceprintlist);
1080 add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
1081 List the full symbol tables for all object files.\n\
1082 This does not include information about individual symbols, blocks, or\n\
1083 linetables --- just the symbol table structures themselves.\n\
1084 With an argument REGEXP, list the symbol tables with matching names."),
1085 &maintenanceinfolist);
1087 add_cmd ("line-table", class_maintenance, maintenance_info_line_tables, _("\
1088 List the contents of all line tables, from all symbol tables.\n\
1089 With an argument REGEXP, list just the line tables for the symbol\n\
1090 tables with matching names."),
1091 &maintenanceinfolist);
1093 add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
1094 _("\
1095 Check consistency of currently expanded symtabs."),
1096 &maintenancelist);
1098 add_cmd ("expand-symtabs", class_maintenance, maintenance_expand_symtabs,
1099 _("Expand symbol tables.\n\
1100 With an argument REGEXP, only expand the symbol tables with matching names."),
1101 &maintenancelist);