Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / symmisc.c
blobf80fc5ab196d19831d84c6515fd0a78f054e7727
1 /* Do various things to symbol tables (other than lookup), for GDB.
3 Copyright (C) 1986-2024 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 "symtab.h"
21 #include "gdbtypes.h"
22 #include "bfd.h"
23 #include "filenames.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "breakpoint.h"
27 #include "command.h"
28 #include "gdbsupport/gdb_obstack.h"
29 #include "language.h"
30 #include "bcache.h"
31 #include "block.h"
32 #include "gdbsupport/gdb_regex.h"
33 #include <sys/stat.h>
34 #include "dictionary.h"
35 #include "typeprint.h"
36 #include "gdbcmd.h"
37 #include "source.h"
38 #include "readline/tilde.h"
39 #include <cli/cli-style.h>
40 #include "gdbsupport/buildargv.h"
42 /* Prototypes for local functions */
44 static int block_depth (const struct block *);
46 static void print_symbol (struct gdbarch *gdbarch, struct symbol *symbol,
47 int depth, ui_file *outfile);
50 void
51 print_objfile_statistics (void)
53 int i, linetables, blockvectors;
55 for (struct program_space *pspace : program_spaces)
56 for (objfile *objfile : pspace->objfiles ())
58 QUIT;
59 gdb_printf (_("Statistics for '%s':\n"), objfile_name (objfile));
60 if (OBJSTAT (objfile, n_stabs) > 0)
61 gdb_printf (_(" Number of \"stab\" symbols read: %d\n"),
62 OBJSTAT (objfile, n_stabs));
63 if (objfile->per_bfd->n_minsyms > 0)
64 gdb_printf (_(" Number of \"minimal\" symbols read: %d\n"),
65 objfile->per_bfd->n_minsyms);
66 if (OBJSTAT (objfile, n_syms) > 0)
67 gdb_printf (_(" Number of \"full\" symbols read: %d\n"),
68 OBJSTAT (objfile, n_syms));
69 if (OBJSTAT (objfile, n_types) > 0)
70 gdb_printf (_(" Number of \"types\" defined: %d\n"),
71 OBJSTAT (objfile, n_types));
73 i = linetables = 0;
74 for (compunit_symtab *cu : objfile->compunits ())
76 for (symtab *s : cu->filetabs ())
78 i++;
79 if (s->linetable () != NULL)
80 linetables++;
83 blockvectors = std::distance (objfile->compunits ().begin (),
84 objfile->compunits ().end ());
85 gdb_printf (_(" Number of symbol tables: %d\n"), i);
86 gdb_printf (_(" Number of symbol tables with line tables: %d\n"),
87 linetables);
88 gdb_printf (_(" Number of symbol tables with blockvectors: %d\n"),
89 blockvectors);
91 objfile->print_stats (false);
93 if (OBJSTAT (objfile, sz_strtab) > 0)
94 gdb_printf (_(" Space used by string tables: %d\n"),
95 OBJSTAT (objfile, sz_strtab));
96 gdb_printf (_(" Total memory used for objfile obstack: %s\n"),
97 pulongest (obstack_memory_used (&objfile
98 ->objfile_obstack)));
99 gdb_printf (_(" Total memory used for BFD obstack: %s\n"),
100 pulongest (obstack_memory_used (&objfile->per_bfd
101 ->storage_obstack)));
103 gdb_printf (_(" Total memory used for string cache: %d\n"),
104 objfile->per_bfd->string_cache.memory_used ());
105 gdb_printf (_("Byte cache statistics for '%s':\n"),
106 objfile_name (objfile));
107 objfile->per_bfd->string_cache.print_statistics ("string cache");
108 objfile->print_stats (true);
112 static void
113 dump_objfile (struct objfile *objfile)
115 gdb_printf ("\nObject file %s: ", objfile_name (objfile));
116 gdb_printf ("Objfile at %s, bfd at %s, %d minsyms\n\n",
117 host_address_to_string (objfile),
118 host_address_to_string (objfile->obfd.get ()),
119 objfile->per_bfd->minimal_symbol_count);
121 objfile->dump ();
123 if (objfile->compunit_symtabs != NULL)
125 gdb_printf ("Symtabs:\n");
126 for (compunit_symtab *cu : objfile->compunits ())
128 for (symtab *symtab : cu->filetabs ())
130 gdb_printf ("%s at %s",
131 symtab_to_filename_for_display (symtab),
132 host_address_to_string (symtab));
133 if (symtab->compunit ()->objfile () != objfile)
134 gdb_printf (", NOT ON CHAIN!");
135 gdb_printf ("\n");
138 gdb_printf ("\n\n");
142 /* Print minimal symbols from this objfile. */
144 static void
145 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
147 struct gdbarch *gdbarch = objfile->arch ();
148 int index;
149 char ms_type;
151 gdb_printf (outfile, "\nObject file %s:\n\n", objfile_name (objfile));
152 if (objfile->per_bfd->minimal_symbol_count == 0)
154 gdb_printf (outfile, "No minimal symbols found.\n");
155 return;
157 index = 0;
158 for (minimal_symbol *msymbol : objfile->msymbols ())
160 struct obj_section *section = msymbol->obj_section (objfile);
162 switch (msymbol->type ())
164 case mst_unknown:
165 ms_type = 'u';
166 break;
167 case mst_text:
168 ms_type = 'T';
169 break;
170 case mst_text_gnu_ifunc:
171 case mst_data_gnu_ifunc:
172 ms_type = 'i';
173 break;
174 case mst_solib_trampoline:
175 ms_type = 'S';
176 break;
177 case mst_data:
178 ms_type = 'D';
179 break;
180 case mst_bss:
181 ms_type = 'B';
182 break;
183 case mst_abs:
184 ms_type = 'A';
185 break;
186 case mst_file_text:
187 ms_type = 't';
188 break;
189 case mst_file_data:
190 ms_type = 'd';
191 break;
192 case mst_file_bss:
193 ms_type = 'b';
194 break;
195 default:
196 ms_type = '?';
197 break;
199 gdb_printf (outfile, "[%2d] %c ", index, ms_type);
201 /* Use the relocated address as shown in the symbol here -- do
202 not try to respect copy relocations. */
203 CORE_ADDR addr = (CORE_ADDR (msymbol->unrelocated_address ())
204 + objfile->section_offsets[msymbol->section_index ()]);
205 gdb_puts (paddress (gdbarch, addr), outfile);
206 gdb_printf (outfile, " %s", msymbol->linkage_name ());
207 if (section)
209 if (section->the_bfd_section != NULL)
210 gdb_printf (outfile, " section %s",
211 bfd_section_name (section->the_bfd_section));
212 else
213 gdb_printf (outfile, " spurious section %ld",
214 (long) (section - objfile->sections_start));
216 if (msymbol->demangled_name () != NULL)
218 gdb_printf (outfile, " %s", msymbol->demangled_name ());
220 if (msymbol->filename)
221 gdb_printf (outfile, " %s", msymbol->filename);
222 gdb_puts ("\n", outfile);
223 index++;
225 if (objfile->per_bfd->minimal_symbol_count != index)
227 warning (_("internal error: minimal symbol count %d != %d"),
228 objfile->per_bfd->minimal_symbol_count, index);
230 gdb_printf (outfile, "\n");
233 static void
234 dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
236 struct objfile *objfile = symtab->compunit ()->objfile ();
237 struct gdbarch *gdbarch = objfile->arch ();
238 const struct linetable *l;
239 int depth;
241 gdb_printf (outfile, "\nSymtab for file %s at %s\n",
242 symtab_to_filename_for_display (symtab),
243 host_address_to_string (symtab));
245 if (symtab->compunit ()->dirname () != NULL)
246 gdb_printf (outfile, "Compilation directory is %s\n",
247 symtab->compunit ()->dirname ());
248 gdb_printf (outfile, "Read from object file %s (%s)\n",
249 objfile_name (objfile),
250 host_address_to_string (objfile));
251 gdb_printf (outfile, "Language: %s\n",
252 language_str (symtab->language ()));
254 /* First print the line table. */
255 l = symtab->linetable ();
256 if (l)
258 gdb_printf (outfile, "\nLine table:\n\n");
259 int len = l->nitems;
260 for (int i = 0; i < len; i++)
262 gdb_printf (outfile, " line %d at ", l->item[i].line);
263 gdb_puts (paddress (gdbarch, l->item[i].pc (objfile)), outfile);
264 if (l->item[i].is_stmt)
265 gdb_printf (outfile, "\t(stmt)");
266 gdb_printf (outfile, "\n");
269 /* Now print the block info, but only for compunit symtabs since we will
270 print lots of duplicate info otherwise. */
271 if (is_main_symtab_of_compunit_symtab (symtab))
273 gdb_printf (outfile, "\nBlockvector:\n\n");
274 const blockvector *bv = symtab->compunit ()->blockvector ();
275 for (int i = 0; i < bv->num_blocks (); i++)
277 const block *b = bv->block (i);
278 depth = block_depth (b) * 2;
279 gdb_printf (outfile, "%*sblock #%03d, object at %s",
280 depth, "", i,
281 host_address_to_string (b));
282 if (b->superblock ())
283 gdb_printf (outfile, " under %s",
284 host_address_to_string (b->superblock ()));
285 /* drow/2002-07-10: We could save the total symbols count
286 even if we're using a hashtable, but nothing else but this message
287 wants it. */
288 gdb_printf (outfile, ", %d symbols in ",
289 mdict_size (b->multidict ()));
290 gdb_puts (paddress (gdbarch, b->start ()), outfile);
291 gdb_printf (outfile, "..");
292 gdb_puts (paddress (gdbarch, b->end ()), outfile);
293 if (b->function ())
295 gdb_printf (outfile, ", function %s",
296 b->function ()->linkage_name ());
297 if (b->function ()->demangled_name () != NULL)
299 gdb_printf (outfile, ", %s",
300 b->function ()->demangled_name ());
303 gdb_printf (outfile, "\n");
304 /* Now print each symbol in this block (in no particular order, if
305 we're using a hashtable). Note that we only want this
306 block, not any blocks from included symtabs. */
307 for (struct symbol *sym : b->multidict_symbols ())
311 print_symbol (gdbarch, sym, depth + 1, outfile);
313 catch (const gdb_exception_error &ex)
315 exception_fprintf (gdb_stderr, ex,
316 "Error printing symbol:\n");
320 gdb_printf (outfile, "\n");
322 else
324 compunit_symtab *compunit = symtab->compunit ();
325 const char *compunit_filename
326 = symtab_to_filename_for_display (compunit->primary_filetab ());
328 gdb_printf (outfile,
329 "\nBlockvector same as owning compunit: %s\n\n",
330 compunit_filename);
333 /* Print info about the user of this compunit_symtab, and the
334 compunit_symtabs included by this one. */
335 if (is_main_symtab_of_compunit_symtab (symtab))
337 struct compunit_symtab *cust = symtab->compunit ();
339 if (cust->user != nullptr)
341 const char *addr
342 = host_address_to_string (cust->user->primary_filetab ());
343 gdb_printf (outfile, "Compunit user: %s\n", addr);
345 if (cust->includes != nullptr)
346 for (int i = 0; ; ++i)
348 struct compunit_symtab *include = cust->includes[i];
349 if (include == nullptr)
350 break;
351 const char *addr
352 = host_address_to_string (include->primary_filetab ());
353 gdb_printf (outfile, "Compunit include: %s\n", addr);
358 static void
359 dump_symtab (struct symtab *symtab, struct ui_file *outfile)
361 /* Set the current language to the language of the symtab we're dumping
362 because certain routines used during dump_symtab() use the current
363 language to print an image of the symbol. We'll restore it later.
364 But use only real languages, not placeholders. */
365 if (symtab->language () != language_unknown)
367 scoped_restore_current_language save_lang;
368 set_language (symtab->language ());
369 dump_symtab_1 (symtab, outfile);
371 else
372 dump_symtab_1 (symtab, outfile);
375 static void
376 maintenance_print_symbols (const char *args, int from_tty)
378 struct ui_file *outfile = gdb_stdout;
379 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
380 int i, outfile_idx;
382 dont_repeat ();
384 gdb_argv argv (args);
386 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
388 if (strcmp (argv[i], "-pc") == 0)
390 if (argv[i + 1] == NULL)
391 error (_("Missing pc value"));
392 address_arg = argv[++i];
394 else if (strcmp (argv[i], "-source") == 0)
396 if (argv[i + 1] == NULL)
397 error (_("Missing source file"));
398 source_arg = argv[++i];
400 else if (strcmp (argv[i], "-objfile") == 0)
402 if (argv[i + 1] == NULL)
403 error (_("Missing objfile name"));
404 objfile_arg = argv[++i];
406 else if (strcmp (argv[i], "--") == 0)
408 /* End of options. */
409 ++i;
410 break;
412 else if (argv[i][0] == '-')
414 /* Future proofing: Don't allow OUTFILE to begin with "-". */
415 error (_("Unknown option: %s"), argv[i]);
417 else
418 break;
420 outfile_idx = i;
422 if (address_arg != NULL && source_arg != NULL)
423 error (_("Must specify at most one of -pc and -source"));
425 stdio_file arg_outfile;
427 if (argv != NULL && argv[outfile_idx] != NULL)
429 if (argv[outfile_idx + 1] != NULL)
430 error (_("Junk at end of command"));
431 gdb::unique_xmalloc_ptr<char> outfile_name
432 (tilde_expand (argv[outfile_idx]));
433 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
434 perror_with_name (outfile_name.get ());
435 outfile = &arg_outfile;
438 if (address_arg != NULL)
440 CORE_ADDR pc = parse_and_eval_address (address_arg);
441 struct symtab *s = find_pc_line_symtab (pc);
443 if (s == NULL)
444 error (_("No symtab for address: %s"), address_arg);
445 dump_symtab (s, outfile);
447 else
449 int found = 0;
451 for (objfile *objfile : current_program_space->objfiles ())
453 int print_for_objfile = 1;
455 if (objfile_arg != NULL)
456 print_for_objfile
457 = compare_filenames_for_search (objfile_name (objfile),
458 objfile_arg);
459 if (!print_for_objfile)
460 continue;
462 for (compunit_symtab *cu : objfile->compunits ())
464 for (symtab *s : cu->filetabs ())
466 int print_for_source = 0;
468 QUIT;
469 if (source_arg != NULL)
471 print_for_source
472 = compare_filenames_for_search
473 (symtab_to_filename_for_display (s), source_arg);
474 found = 1;
476 if (source_arg == NULL
477 || print_for_source)
478 dump_symtab (s, outfile);
483 if (source_arg != NULL && !found)
484 error (_("No symtab for source file: %s"), source_arg);
488 /* Print symbol SYMBOL on OUTFILE. DEPTH says how far to indent. */
490 static void
491 print_symbol (struct gdbarch *gdbarch, struct symbol *symbol,
492 int depth, ui_file *outfile)
494 struct obj_section *section;
496 if (symbol->is_objfile_owned ())
497 section = symbol->obj_section (symbol->objfile ());
498 else
499 section = NULL;
501 print_spaces (depth, outfile);
502 if (symbol->domain () == LABEL_DOMAIN)
504 gdb_printf (outfile, "label %s at ", symbol->print_name ());
505 gdb_puts (paddress (gdbarch, symbol->value_address ()),
506 outfile);
507 if (section)
508 gdb_printf (outfile, " section %s\n",
509 bfd_section_name (section->the_bfd_section));
510 else
511 gdb_printf (outfile, "\n");
512 return;
515 if (symbol->domain () == STRUCT_DOMAIN)
517 if (symbol->type ()->name ())
519 current_language->print_type (symbol->type (), "", outfile, 1, depth,
520 &type_print_raw_options);
522 else
524 gdb_printf (outfile, "%s %s = ",
525 (symbol->type ()->code () == TYPE_CODE_ENUM
526 ? "enum"
527 : (symbol->type ()->code () == TYPE_CODE_STRUCT
528 ? "struct" : "union")),
529 symbol->linkage_name ());
530 current_language->print_type (symbol->type (), "", outfile, 1, depth,
531 &type_print_raw_options);
533 gdb_printf (outfile, ";\n");
535 else
537 if (symbol->aclass () == LOC_TYPEDEF)
538 gdb_printf (outfile, "typedef ");
539 if (symbol->type ())
541 /* Print details of types, except for enums where it's clutter. */
542 current_language->print_type (symbol->type (), symbol->print_name (),
543 outfile,
544 symbol->type ()->code () != TYPE_CODE_ENUM,
545 depth,
546 &type_print_raw_options);
547 gdb_printf (outfile, "; ");
549 else
550 gdb_printf (outfile, "%s ", symbol->print_name ());
552 switch (symbol->aclass ())
554 case LOC_CONST:
555 gdb_printf (outfile, "const %s (%s)",
556 plongest (symbol->value_longest ()),
557 hex_string (symbol->value_longest ()));
558 break;
560 case LOC_CONST_BYTES:
562 unsigned i;
563 struct type *type = check_typedef (symbol->type ());
565 gdb_printf (outfile, "const %s hex bytes:",
566 pulongest (type->length ()));
567 for (i = 0; i < type->length (); i++)
568 gdb_printf (outfile, " %02x",
569 (unsigned) symbol->value_bytes ()[i]);
571 break;
573 case LOC_STATIC:
574 gdb_printf (outfile, "static at ");
575 gdb_puts (paddress (gdbarch, symbol->value_address ()), outfile);
576 if (section)
577 gdb_printf (outfile, " section %s",
578 bfd_section_name (section->the_bfd_section));
579 break;
581 case LOC_REGISTER:
582 if (symbol->is_argument ())
583 gdb_printf (outfile, "parameter register %s",
584 plongest (symbol->value_longest ()));
585 else
586 gdb_printf (outfile, "register %s",
587 plongest (symbol->value_longest ()));
588 break;
590 case LOC_ARG:
591 gdb_printf (outfile, "arg at offset %s",
592 hex_string (symbol->value_longest ()));
593 break;
595 case LOC_REF_ARG:
596 gdb_printf (outfile, "reference arg at %s",
597 hex_string (symbol->value_longest ()));
598 break;
600 case LOC_REGPARM_ADDR:
601 gdb_printf (outfile, "address parameter register %s",
602 plongest (symbol->value_longest ()));
603 break;
605 case LOC_LOCAL:
606 gdb_printf (outfile, "local at offset %s",
607 hex_string (symbol->value_longest ()));
608 break;
610 case LOC_TYPEDEF:
611 break;
613 case LOC_LABEL:
614 gdb_printf (outfile, "label at ");
615 gdb_puts (paddress (gdbarch, symbol->value_address ()), outfile);
616 if (section)
617 gdb_printf (outfile, " section %s",
618 bfd_section_name (section->the_bfd_section));
619 break;
621 case LOC_BLOCK:
622 gdb_printf
623 (outfile, "block object %s, %s..%s",
624 host_address_to_string (symbol->value_block ()),
625 paddress (gdbarch, symbol->value_block()->start ()),
626 paddress (gdbarch, symbol->value_block()->end ()));
627 if (section)
628 gdb_printf (outfile, " section %s",
629 bfd_section_name (section->the_bfd_section));
630 break;
632 case LOC_COMPUTED:
633 gdb_printf (outfile, "computed at runtime");
634 break;
636 case LOC_UNRESOLVED:
637 gdb_printf (outfile, "unresolved");
638 break;
640 case LOC_OPTIMIZED_OUT:
641 gdb_printf (outfile, "optimized out");
642 break;
644 default:
645 gdb_printf (outfile, "botched symbol class %x",
646 symbol->aclass ());
647 break;
650 gdb_printf (outfile, "\n");
653 static void
654 maintenance_print_msymbols (const char *args, int from_tty)
656 struct ui_file *outfile = gdb_stdout;
657 char *objfile_arg = NULL;
658 int i, outfile_idx;
660 dont_repeat ();
662 gdb_argv argv (args);
664 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
666 if (strcmp (argv[i], "-objfile") == 0)
668 if (argv[i + 1] == NULL)
669 error (_("Missing objfile name"));
670 objfile_arg = argv[++i];
672 else if (strcmp (argv[i], "--") == 0)
674 /* End of options. */
675 ++i;
676 break;
678 else if (argv[i][0] == '-')
680 /* Future proofing: Don't allow OUTFILE to begin with "-". */
681 error (_("Unknown option: %s"), argv[i]);
683 else
684 break;
686 outfile_idx = i;
688 stdio_file arg_outfile;
690 if (argv != NULL && argv[outfile_idx] != NULL)
692 if (argv[outfile_idx + 1] != NULL)
693 error (_("Junk at end of command"));
694 gdb::unique_xmalloc_ptr<char> outfile_name
695 (tilde_expand (argv[outfile_idx]));
696 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
697 perror_with_name (outfile_name.get ());
698 outfile = &arg_outfile;
701 for (objfile *objfile : current_program_space->objfiles ())
703 QUIT;
704 if (objfile_arg == NULL
705 || compare_filenames_for_search (objfile_name (objfile), objfile_arg))
706 dump_msymbols (objfile, outfile);
710 static void
711 maintenance_print_objfiles (const char *regexp, int from_tty)
713 dont_repeat ();
715 if (regexp)
716 re_comp (regexp);
718 for (struct program_space *pspace : program_spaces)
719 for (objfile *objfile : pspace->objfiles ())
721 QUIT;
722 if (! regexp
723 || re_exec (objfile_name (objfile)))
724 dump_objfile (objfile);
728 /* List all the symbol tables whose names match REGEXP (optional). */
730 static void
731 maintenance_info_symtabs (const char *regexp, int from_tty)
733 dont_repeat ();
735 if (regexp)
736 re_comp (regexp);
738 for (struct program_space *pspace : program_spaces)
739 for (objfile *objfile : pspace->objfiles ())
741 /* We don't want to print anything for this objfile until we
742 actually find a symtab whose name matches. */
743 int printed_objfile_start = 0;
745 for (compunit_symtab *cust : objfile->compunits ())
747 int printed_compunit_symtab_start = 0;
749 for (symtab *symtab : cust->filetabs ())
751 QUIT;
753 if (! regexp
754 || re_exec (symtab_to_filename_for_display (symtab)))
756 if (! printed_objfile_start)
758 gdb_printf ("{ objfile %s ", objfile_name (objfile));
759 gdb_stdout->wrap_here (2);
760 gdb_printf ("((struct objfile *) %s)\n",
761 host_address_to_string (objfile));
762 printed_objfile_start = 1;
764 if (! printed_compunit_symtab_start)
766 gdb_printf (" { ((struct compunit_symtab *) %s)\n",
767 host_address_to_string (cust));
768 gdb_printf (" debugformat %s\n",
769 cust->debugformat ());
770 gdb_printf (" producer %s\n",
771 (cust->producer () != nullptr
772 ? cust->producer () : "(null)"));
773 gdb_printf (" name %s\n", cust->name);
774 gdb_printf (" dirname %s\n",
775 (cust->dirname () != NULL
776 ? cust->dirname () : "(null)"));
777 gdb_printf (" blockvector"
778 " ((struct blockvector *) %s)\n",
779 host_address_to_string
780 (cust->blockvector ()));
781 gdb_printf (" user"
782 " ((struct compunit_symtab *) %s)\n",
783 cust->user != nullptr
784 ? host_address_to_string (cust->user)
785 : "(null)");
786 if (cust->includes != nullptr)
788 gdb_printf (" ( includes\n");
789 for (int i = 0; ; ++i)
791 struct compunit_symtab *include
792 = cust->includes[i];
793 if (include == nullptr)
794 break;
795 const char *addr
796 = host_address_to_string (include);
797 gdb_printf (" (%s %s)\n",
798 "(struct compunit_symtab *)",
799 addr);
801 gdb_printf (" )\n");
803 printed_compunit_symtab_start = 1;
806 gdb_printf ("\t{ symtab %s ",
807 symtab_to_filename_for_display (symtab));
808 gdb_stdout->wrap_here (4);
809 gdb_printf ("((struct symtab *) %s)\n",
810 host_address_to_string (symtab));
811 gdb_printf ("\t fullname %s\n",
812 symtab->fullname != NULL
813 ? symtab->fullname
814 : "(null)");
815 gdb_printf ("\t "
816 "linetable ((struct linetable *) %s)\n",
817 host_address_to_string
818 (symtab->linetable ()));
819 gdb_printf ("\t}\n");
823 if (printed_compunit_symtab_start)
824 gdb_printf (" }\n");
827 if (printed_objfile_start)
828 gdb_printf ("}\n");
832 /* Check consistency of symtabs.
833 An example of what this checks for is NULL blockvectors.
834 They can happen if there's a bug during debug info reading.
835 GDB assumes they are always non-NULL.
837 Note: This does not check for psymtab vs symtab consistency.
838 Use "maint check-psymtabs" for that. */
840 static void
841 maintenance_check_symtabs (const char *ignore, int from_tty)
843 for (struct program_space *pspace : program_spaces)
844 for (objfile *objfile : pspace->objfiles ())
846 /* We don't want to print anything for this objfile until we
847 actually find something worth printing. */
848 int printed_objfile_start = 0;
850 for (compunit_symtab *cust : objfile->compunits ())
852 int found_something = 0;
853 struct symtab *symtab = cust->primary_filetab ();
855 QUIT;
857 if (cust->blockvector () == NULL)
858 found_something = 1;
859 /* Add more checks here. */
861 if (found_something)
863 if (! printed_objfile_start)
865 gdb_printf ("{ objfile %s ", objfile_name (objfile));
866 gdb_stdout->wrap_here (2);
867 gdb_printf ("((struct objfile *) %s)\n",
868 host_address_to_string (objfile));
869 printed_objfile_start = 1;
871 gdb_printf (" { symtab %s\n",
872 symtab_to_filename_for_display (symtab));
873 if (cust->blockvector () == NULL)
874 gdb_printf (" NULL blockvector\n");
875 gdb_printf (" }\n");
879 if (printed_objfile_start)
880 gdb_printf ("}\n");
884 /* Expand all symbol tables whose name matches an optional regexp. */
886 static void
887 maintenance_expand_symtabs (const char *args, int from_tty)
889 char *regexp = NULL;
891 /* We use buildargv here so that we handle spaces in the regexp
892 in a way that allows adding more arguments later. */
893 gdb_argv argv (args);
895 if (argv != NULL)
897 if (argv[0] != NULL)
899 regexp = argv[0];
900 if (argv[1] != NULL)
901 error (_("Extra arguments after regexp."));
905 if (regexp)
906 re_comp (regexp);
908 for (struct program_space *pspace : program_spaces)
909 for (objfile *objfile : pspace->objfiles ())
910 objfile->expand_symtabs_matching
911 ([&] (const char *filename, bool basenames)
913 /* KISS: Only apply the regexp to the complete file name. */
914 return (!basenames
915 && (regexp == NULL || re_exec (filename)));
917 NULL,
918 NULL,
919 NULL,
920 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
921 SEARCH_ALL_DOMAINS);
925 /* Return the nexting depth of a block within other blocks in its symtab. */
927 static int
928 block_depth (const struct block *block)
930 int i = 0;
932 while ((block = block->superblock ()) != NULL)
934 i++;
936 return i;
940 /* Used by MAINTENANCE_INFO_LINE_TABLES to print the information about a
941 single line table. */
943 static int
944 maintenance_print_one_line_table (struct symtab *symtab, void *data)
946 const struct linetable *linetable;
947 struct objfile *objfile;
949 objfile = symtab->compunit ()->objfile ();
950 gdb_printf (_("objfile: %ps ((struct objfile *) %s)\n"),
951 styled_string (file_name_style.style (),
952 objfile_name (objfile)),
953 host_address_to_string (objfile));
954 gdb_printf (_("compunit_symtab: %s ((struct compunit_symtab *) %s)\n"),
955 symtab->compunit ()->name,
956 host_address_to_string (symtab->compunit ()));
957 gdb_printf (_("symtab: %ps ((struct symtab *) %s)\n"),
958 styled_string (file_name_style.style (),
959 symtab_to_fullname (symtab)),
960 host_address_to_string (symtab));
961 linetable = symtab->linetable ();
962 gdb_printf (_("linetable: ((struct linetable *) %s):\n"),
963 host_address_to_string (linetable));
965 if (linetable == NULL)
966 gdb_printf (_("No line table.\n"));
967 else if (linetable->nitems <= 0)
968 gdb_printf (_("Line table has no lines.\n"));
969 else
971 /* Leave space for 6 digits of index and line number. After that the
972 tables will just not format as well. */
973 struct ui_out *uiout = current_uiout;
974 ui_out_emit_table table_emitter (uiout, 7, -1, "line-table");
975 uiout->table_header (6, ui_left, "index", _("INDEX"));
976 uiout->table_header (6, ui_left, "line", _("LINE"));
977 uiout->table_header (18, ui_left, "rel-address", _("REL-ADDRESS"));
978 uiout->table_header (18, ui_left, "unrel-address", _("UNREL-ADDRESS"));
979 uiout->table_header (7, ui_left, "is-stmt", _("IS-STMT"));
980 uiout->table_header (12, ui_left, "prologue-end", _("PROLOGUE-END"));
981 uiout->table_header (14, ui_left, "epilogue-begin", _("EPILOGUE-BEGIN"));
982 uiout->table_body ();
984 for (int i = 0; i < linetable->nitems; ++i)
986 const linetable_entry *item;
988 item = &linetable->item [i];
989 ui_out_emit_tuple tuple_emitter (uiout, nullptr);
990 uiout->field_signed ("index", i);
991 if (item->line > 0)
992 uiout->field_signed ("line", item->line);
993 else
994 uiout->field_string ("line", _("END"));
995 uiout->field_core_addr ("rel-address", objfile->arch (),
996 item->pc (objfile));
997 uiout->field_core_addr ("unrel-address", objfile->arch (),
998 CORE_ADDR (item->unrelocated_pc ()));
999 uiout->field_string ("is-stmt", item->is_stmt ? "Y" : "");
1000 uiout->field_string ("prologue-end", item->prologue_end ? "Y" : "");
1001 uiout->field_string ("epilogue-begin", item->epilogue_begin ? "Y" : "");
1002 uiout->text ("\n");
1006 return 0;
1009 /* Implement the 'maint info line-table' command. */
1011 static void
1012 maintenance_info_line_tables (const char *regexp, int from_tty)
1014 dont_repeat ();
1016 if (regexp != NULL)
1017 re_comp (regexp);
1019 for (struct program_space *pspace : program_spaces)
1020 for (objfile *objfile : pspace->objfiles ())
1022 for (compunit_symtab *cust : objfile->compunits ())
1024 for (symtab *symtab : cust->filetabs ())
1026 QUIT;
1028 if (regexp == NULL
1029 || re_exec (symtab_to_filename_for_display (symtab)))
1031 maintenance_print_one_line_table (symtab, NULL);
1032 gdb_printf ("\n");
1041 /* Do early runtime initializations. */
1043 void _initialize_symmisc ();
1044 void
1045 _initialize_symmisc ()
1047 add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
1048 Print dump of current symbol definitions.\n\
1049 Usage: mt print symbols [-pc ADDRESS] [--] [OUTFILE]\n\
1050 mt print symbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
1051 Entries in the full symbol table are dumped to file OUTFILE,\n\
1052 or the terminal if OUTFILE is unspecified.\n\
1053 If ADDRESS is provided, dump only the symbols for the file with code at that address.\n\
1054 If SOURCE is provided, dump only that file's symbols.\n\
1055 If OBJFILE is provided, dump only that object file's symbols."),
1056 &maintenanceprintlist);
1058 add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
1059 Print dump of current minimal symbol definitions.\n\
1060 Usage: mt print msymbols [-objfile OBJFILE] [--] [OUTFILE]\n\
1061 Entries in the minimal symbol table are dumped to file OUTFILE,\n\
1062 or the terminal if OUTFILE is unspecified.\n\
1063 If OBJFILE is provided, dump only that file's minimal symbols."),
1064 &maintenanceprintlist);
1066 add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
1067 _("Print dump of current object file definitions.\n\
1068 With an argument REGEXP, list the object files with matching names."),
1069 &maintenanceprintlist);
1071 add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
1072 List the full symbol tables for all object files.\n\
1073 This does not include information about individual symbols, blocks, or\n\
1074 linetables --- just the symbol table structures themselves.\n\
1075 With an argument REGEXP, list the symbol tables with matching names."),
1076 &maintenanceinfolist);
1078 add_cmd ("line-table", class_maintenance, maintenance_info_line_tables, _("\
1079 List the contents of all line tables, from all symbol tables.\n\
1080 With an argument REGEXP, list just the line tables for the symbol\n\
1081 tables with matching names."),
1082 &maintenanceinfolist);
1084 add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
1085 _("\
1086 Check consistency of currently expanded symtabs."),
1087 &maintenancelist);
1089 add_cmd ("expand-symtabs", class_maintenance, maintenance_expand_symtabs,
1090 _("Expand symbol tables.\n\
1091 With an argument REGEXP, only expand the symbol tables with matching names."),
1092 &maintenancelist);