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