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