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