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