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