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