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