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