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