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