]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/symmisc.c
gdb/buildsym: Line record use a record flag
[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;
6cb06a8c 60 gdb_printf (_("Statistics for '%s':\n"), objfile_name (objfile));
94c93c35 61 if (OBJSTAT (objfile, n_stabs) > 0)
6cb06a8c
TT
62 gdb_printf (_(" Number of \"stab\" symbols read: %d\n"),
63 OBJSTAT (objfile, n_stabs));
94c93c35 64 if (objfile->per_bfd->n_minsyms > 0)
6cb06a8c
TT
65 gdb_printf (_(" Number of \"minimal\" symbols read: %d\n"),
66 objfile->per_bfd->n_minsyms);
94c93c35 67 if (OBJSTAT (objfile, n_syms) > 0)
6cb06a8c
TT
68 gdb_printf (_(" Number of \"full\" symbols read: %d\n"),
69 OBJSTAT (objfile, n_syms));
94c93c35 70 if (OBJSTAT (objfile, n_types) > 0)
6cb06a8c
TT
71 gdb_printf (_(" 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 {
102cc235 77 for (symtab *s : cu->filetabs ())
94c93c35
TT
78 {
79 i++;
5b607461 80 if (s->linetable () != NULL)
94c93c35
TT
81 linetables++;
82 }
83 }
84 blockvectors = std::distance (objfile->compunits ().begin (),
85 objfile->compunits ().end ());
6cb06a8c
TT
86 gdb_printf (_(" Number of symbol tables: %d\n"), i);
87 gdb_printf (_(" Number of symbol tables with line tables: %d\n"),
88 linetables);
89 gdb_printf (_(" Number of symbol tables with blockvectors: %d\n"),
90 blockvectors);
94c93c35 91
4829711b
TT
92 objfile->print_stats (false);
93
94c93c35 94 if (OBJSTAT (objfile, sz_strtab) > 0)
6cb06a8c
TT
95 gdb_printf (_(" Space used by string tables: %d\n"),
96 OBJSTAT (objfile, sz_strtab));
97 gdb_printf (_(" Total memory used for objfile obstack: %s\n"),
98 pulongest (obstack_memory_used (&objfile
99 ->objfile_obstack)));
100 gdb_printf (_(" Total memory used for BFD obstack: %s\n"),
101 pulongest (obstack_memory_used (&objfile->per_bfd
102 ->storage_obstack)));
103
104 gdb_printf (_(" Total memory used for string cache: %d\n"),
105 objfile->per_bfd->string_cache.memory_used ());
106 gdb_printf (_("Byte cache statistics for '%s':\n"),
107 objfile_name (objfile));
4829711b
TT
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{
6cb06a8c
TT
116 gdb_printf ("\nObject file %s: ", objfile_name (objfile));
117 gdb_printf ("Objfile at %s, bfd at %s, %d minsyms\n\n",
118 host_address_to_string (objfile),
119 host_address_to_string (objfile->obfd),
120 objfile->per_bfd->minimal_symbol_count);
c906108c 121
4d080b46 122 objfile->dump ();
c906108c 123
43f3e411 124 if (objfile->compunit_symtabs != NULL)
c906108c 125 {
6cb06a8c 126 gdb_printf ("Symtabs:\n");
b669c953 127 for (compunit_symtab *cu : objfile->compunits ())
c906108c 128 {
102cc235 129 for (symtab *symtab : cu->filetabs ())
c906108c 130 {
6cb06a8c
TT
131 gdb_printf ("%s at %s",
132 symtab_to_filename_for_display (symtab),
133 host_address_to_string (symtab));
65209971 134 if (symtab->objfile () != objfile)
6cb06a8c
TT
135 gdb_printf (", NOT ON CHAIN!");
136 gdb_printf ("\n");
c906108c 137 }
c906108c 138 }
6cb06a8c 139 gdb_printf ("\n\n");
c906108c
SS
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
6cb06a8c 152 gdb_printf (outfile, "\nObject file %s:\n\n", objfile_name (objfile));
34643a32 153 if (objfile->per_bfd->minimal_symbol_count == 0)
c906108c 154 {
6cb06a8c 155 gdb_printf (outfile, "No minimal symbols found.\n");
c906108c
SS
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 199 }
6cb06a8c 200 gdb_printf (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 ()]);
0426ad51 206 gdb_puts (paddress (gdbarch, addr), outfile);
6cb06a8c 207 gdb_printf (outfile, " %s", msymbol->linkage_name ());
714835d5 208 if (section)
8625fc1b
TT
209 {
210 if (section->the_bfd_section != NULL)
6cb06a8c
TT
211 gdb_printf (outfile, " section %s",
212 bfd_section_name (section->the_bfd_section));
8625fc1b 213 else
6cb06a8c
TT
214 gdb_printf (outfile, " spurious section %ld",
215 (long) (section - objfile->sections));
8625fc1b 216 }
c9d95fa3 217 if (msymbol->demangled_name () != NULL)
c906108c 218 {
6cb06a8c 219 gdb_printf (outfile, " %s", msymbol->demangled_name ());
c906108c 220 }
c906108c 221 if (msymbol->filename)
6cb06a8c 222 gdb_printf (outfile, " %s", msymbol->filename);
0426ad51 223 gdb_puts ("\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 230 }
6cb06a8c 231 gdb_printf (outfile, "\n");
c906108c
SS
232}
233
c5aa993b 234static void
d04c1a59 235dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
c906108c 236{
65209971 237 struct objfile *objfile = symtab->objfile ();
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
6cb06a8c
TT
248 gdb_printf (outfile, "\nSymtab for file %s at %s\n",
249 symtab_to_filename_for_display (symtab),
250 host_address_to_string (symtab));
7b1eff95 251
3574a7b3 252 if (symtab->dirname () != NULL)
6cb06a8c
TT
253 gdb_printf (outfile, "Compilation directory is %s\n",
254 symtab->dirname ());
255 gdb_printf (outfile, "Read from object file %s (%s)\n",
256 objfile_name (objfile),
257 host_address_to_string (objfile));
258 gdb_printf (outfile, "Language: %s\n",
259 language_str (symtab->language ()));
c906108c
SS
260
261 /* First print the line table. */
5b607461 262 l = symtab->linetable ();
c906108c
SS
263 if (l)
264 {
6cb06a8c 265 gdb_printf (outfile, "\nLine table:\n\n");
c906108c
SS
266 len = l->nitems;
267 for (i = 0; i < len; i++)
268 {
6cb06a8c 269 gdb_printf (outfile, " line %d at ", l->item[i].line);
0426ad51 270 gdb_puts (paddress (gdbarch, l->item[i].pc), outfile);
8c95582d 271 if (l->item[i].is_stmt)
6cb06a8c
TT
272 gdb_printf (outfile, "\t(stmt)");
273 gdb_printf (outfile, "\n");
c906108c
SS
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 279 {
6cb06a8c 280 gdb_printf (outfile, "\nBlockvector:\n\n");
012cfab9 281 bv = symtab->blockvector ();
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;
6cb06a8c
TT
287 gdb_printf (outfile, "%*sblock #%03d, object at %s",
288 depth, "", i,
289 host_address_to_string (b));
c906108c 290 if (BLOCK_SUPERBLOCK (b))
6cb06a8c
TT
291 gdb_printf (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. */
6cb06a8c
TT
296 gdb_printf (outfile, ", %d syms/buckets in ",
297 mdict_size (BLOCK_MULTIDICT (b)));
0426ad51 298 gdb_puts (paddress (gdbarch, BLOCK_START (b)), outfile);
6cb06a8c 299 gdb_printf (outfile, "..");
0426ad51 300 gdb_puts (paddress (gdbarch, BLOCK_END (b)), outfile);
c906108c
SS
301 if (BLOCK_FUNCTION (b))
302 {
6cb06a8c
TT
303 gdb_printf (outfile, ", function %s",
304 BLOCK_FUNCTION (b)->linkage_name ());
987012b8 305 if (BLOCK_FUNCTION (b)->demangled_name () != NULL)
c906108c 306 {
6cb06a8c
TT
307 gdb_printf (outfile, ", %s",
308 BLOCK_FUNCTION (b)->demangled_name ());
c906108c
SS
309 }
310 }
6cb06a8c 311 gdb_printf (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 }
6cb06a8c 328 gdb_printf (outfile, "\n");
c906108c
SS
329 }
330 else
331 {
c6159652 332 compunit_symtab *compunit = symtab->compunit ();
6c739336 333 const char *compunit_filename
510860f2 334 = symtab_to_filename_for_display (compunit->primary_filetab ());
6c739336 335
6cb06a8c
TT
336 gdb_printf (outfile,
337 "\nBlockvector same as owning compunit: %s\n\n",
338 compunit_filename);
c906108c 339 }
7b1eff95
TV
340
341 /* Print info about the user of this compunit_symtab, and the
342 compunit_symtabs included by this one. */
343 if (is_main_symtab_of_compunit_symtab (symtab))
344 {
c6159652 345 struct compunit_symtab *cust = symtab->compunit ();
7b1eff95
TV
346
347 if (cust->user != nullptr)
348 {
349 const char *addr
510860f2 350 = host_address_to_string (cust->user->primary_filetab ());
6cb06a8c 351 gdb_printf (outfile, "Compunit user: %s\n", addr);
7b1eff95
TV
352 }
353 if (cust->includes != nullptr)
354 for (i = 0; ; ++i)
355 {
356 struct compunit_symtab *include = cust->includes[i];
357 if (include == nullptr)
358 break;
359 const char *addr
510860f2 360 = host_address_to_string (include->primary_filetab ());
6cb06a8c 361 gdb_printf (outfile, "Compunit include: %s\n", addr);
7b1eff95
TV
362 }
363 }
c906108c
SS
364}
365
44b164c5 366static void
d04c1a59 367dump_symtab (struct symtab *symtab, struct ui_file *outfile)
44b164c5 368{
44b164c5
JB
369 /* Set the current language to the language of the symtab we're dumping
370 because certain routines used during dump_symtab() use the current
969107c5
EZ
371 language to print an image of the symbol. We'll restore it later.
372 But use only real languages, not placeholders. */
1ee2e9f9
SM
373 if (symtab->language () != language_unknown
374 && symtab->language () != language_auto)
969107c5 375 {
9bb9b2f9 376 scoped_restore_current_language save_lang;
1ee2e9f9 377 set_language (symtab->language ());
d04c1a59 378 dump_symtab_1 (symtab, outfile);
969107c5
EZ
379 }
380 else
d04c1a59 381 dump_symtab_1 (symtab, outfile);
44b164c5
JB
382}
383
80480540 384static void
e99c83e7 385maintenance_print_symbols (const char *args, int from_tty)
c906108c 386{
34c41c68 387 struct ui_file *outfile = gdb_stdout;
34c41c68
DE
388 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
389 int i, outfile_idx;
c906108c
SS
390
391 dont_repeat ();
392
773a1edc 393 gdb_argv argv (args);
c906108c 394
99e8a4f9 395 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
c906108c 396 {
34c41c68
DE
397 if (strcmp (argv[i], "-pc") == 0)
398 {
399 if (argv[i + 1] == NULL)
400 error (_("Missing pc value"));
401 address_arg = argv[++i];
402 }
403 else if (strcmp (argv[i], "-source") == 0)
404 {
405 if (argv[i + 1] == NULL)
406 error (_("Missing source file"));
407 source_arg = argv[++i];
408 }
409 else if (strcmp (argv[i], "-objfile") == 0)
410 {
411 if (argv[i + 1] == NULL)
412 error (_("Missing objfile name"));
413 objfile_arg = argv[++i];
414 }
415 else if (strcmp (argv[i], "--") == 0)
416 {
417 /* End of options. */
418 ++i;
419 break;
420 }
421 else if (argv[i][0] == '-')
c906108c 422 {
34c41c68
DE
423 /* Future proofing: Don't allow OUTFILE to begin with "-". */
424 error (_("Unknown option: %s"), argv[i]);
c906108c 425 }
34c41c68
DE
426 else
427 break;
c906108c 428 }
34c41c68 429 outfile_idx = i;
c906108c 430
34c41c68
DE
431 if (address_arg != NULL && source_arg != NULL)
432 error (_("Must specify at most one of -pc and -source"));
c5aa993b 433
d7e74731
PA
434 stdio_file arg_outfile;
435
99e8a4f9 436 if (argv != NULL && argv[outfile_idx] != NULL)
34c41c68 437 {
34c41c68
DE
438 if (argv[outfile_idx + 1] != NULL)
439 error (_("Junk at end of command"));
ee0c3293
TT
440 gdb::unique_xmalloc_ptr<char> outfile_name
441 (tilde_expand (argv[outfile_idx]));
442 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
443 perror_with_name (outfile_name.get ());
d7e74731 444 outfile = &arg_outfile;
34c41c68 445 }
c906108c 446
34c41c68 447 if (address_arg != NULL)
27618ce4 448 {
34c41c68
DE
449 CORE_ADDR pc = parse_and_eval_address (address_arg);
450 struct symtab *s = find_pc_line_symtab (pc);
451
452 if (s == NULL)
453 error (_("No symtab for address: %s"), address_arg);
454 dump_symtab (s, outfile);
27618ce4 455 }
34c41c68
DE
456 else
457 {
34c41c68
DE
458 int found = 0;
459
2030c079 460 for (objfile *objfile : current_program_space->objfiles ())
34c41c68
DE
461 {
462 int print_for_objfile = 1;
463
464 if (objfile_arg != NULL)
465 print_for_objfile
466 = compare_filenames_for_search (objfile_name (objfile),
467 objfile_arg);
468 if (!print_for_objfile)
469 continue;
470
b669c953 471 for (compunit_symtab *cu : objfile->compunits ())
34c41c68 472 {
102cc235 473 for (symtab *s : cu->filetabs ())
34c41c68 474 {
d5da8b3c
TT
475 int print_for_source = 0;
476
477 QUIT;
478 if (source_arg != NULL)
479 {
480 print_for_source
481 = compare_filenames_for_search
482 (symtab_to_filename_for_display (s), source_arg);
483 found = 1;
484 }
485 if (source_arg == NULL
486 || print_for_source)
487 dump_symtab (s, outfile);
34c41c68 488 }
34c41c68
DE
489 }
490 }
491
492 if (source_arg != NULL && !found)
493 error (_("No symtab for source file: %s"), source_arg);
494 }
c906108c
SS
495}
496
bf469271 497/* Print symbol SYMBOL on OUTFILE. DEPTH says how far to indent. */
c906108c 498
bf469271
PA
499static void
500print_symbol (struct gdbarch *gdbarch, struct symbol *symbol,
501 int depth, ui_file *outfile)
c906108c 502{
1994afbf
DE
503 struct obj_section *section;
504
7b3ecc75 505 if (symbol->is_objfile_owned ())
ebbc3a7d 506 section = symbol->obj_section (symbol_objfile (symbol));
1994afbf
DE
507 else
508 section = NULL;
c906108c 509
d0b1020b 510 print_spaces (depth, outfile);
6c9c307c 511 if (symbol->domain () == LABEL_DOMAIN)
c906108c 512 {
6cb06a8c 513 gdb_printf (outfile, "label %s at ", symbol->print_name ());
0426ad51
TT
514 gdb_puts (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
515 outfile);
714835d5 516 if (section)
6cb06a8c
TT
517 gdb_printf (outfile, " section %s\n",
518 bfd_section_name (section->the_bfd_section));
c906108c 519 else
6cb06a8c 520 gdb_printf (outfile, "\n");
bf469271 521 return;
c906108c 522 }
bf469271 523
6c9c307c 524 if (symbol->domain () == STRUCT_DOMAIN)
c906108c 525 {
5f9c5a63 526 if (symbol->type ()->name ())
c906108c 527 {
13eb081a
TT
528 current_language->print_type (symbol->type (), "", outfile, 1, depth,
529 &type_print_raw_options);
c906108c
SS
530 }
531 else
532 {
6cb06a8c
TT
533 gdb_printf (outfile, "%s %s = ",
534 (symbol->type ()->code () == TYPE_CODE_ENUM
535 ? "enum"
536 : (symbol->type ()->code () == TYPE_CODE_STRUCT
537 ? "struct" : "union")),
538 symbol->linkage_name ());
13eb081a
TT
539 current_language->print_type (symbol->type (), "", outfile, 1, depth,
540 &type_print_raw_options);
c906108c 541 }
6cb06a8c 542 gdb_printf (outfile, ";\n");
c906108c
SS
543 }
544 else
545 {
66d7f48f 546 if (symbol->aclass () == LOC_TYPEDEF)
6cb06a8c 547 gdb_printf (outfile, "typedef ");
5f9c5a63 548 if (symbol->type ())
c906108c
SS
549 {
550 /* Print details of types, except for enums where it's clutter. */
13eb081a
TT
551 current_language->print_type (symbol->type (), symbol->print_name (),
552 outfile,
553 symbol->type ()->code () != TYPE_CODE_ENUM,
554 depth,
555 &type_print_raw_options);
6cb06a8c 556 gdb_printf (outfile, "; ");
c906108c
SS
557 }
558 else
6cb06a8c 559 gdb_printf (outfile, "%s ", symbol->print_name ());
c906108c 560
66d7f48f 561 switch (symbol->aclass ())
c906108c
SS
562 {
563 case LOC_CONST:
6cb06a8c
TT
564 gdb_printf (outfile, "const %s (%s)",
565 plongest (SYMBOL_VALUE (symbol)),
566 hex_string (SYMBOL_VALUE (symbol)));
c906108c
SS
567 break;
568
569 case LOC_CONST_BYTES:
570 {
571 unsigned i;
5f9c5a63 572 struct type *type = check_typedef (symbol->type ());
433759f7 573
6cb06a8c
TT
574 gdb_printf (outfile, "const %s hex bytes:",
575 pulongest (TYPE_LENGTH (type)));
c906108c 576 for (i = 0; i < TYPE_LENGTH (type); i++)
6cb06a8c
TT
577 gdb_printf (outfile, " %02x",
578 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
c906108c
SS
579 }
580 break;
581
582 case LOC_STATIC:
6cb06a8c 583 gdb_printf (outfile, "static at ");
0426ad51
TT
584 gdb_puts (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
585 outfile);
714835d5 586 if (section)
6cb06a8c
TT
587 gdb_printf (outfile, " section %s",
588 bfd_section_name (section->the_bfd_section));
c906108c
SS
589 break;
590
c906108c 591 case LOC_REGISTER:
d9743061 592 if (symbol->is_argument ())
6cb06a8c
TT
593 gdb_printf (outfile, "parameter register %s",
594 plongest (SYMBOL_VALUE (symbol)));
2a2d4dc3 595 else
6cb06a8c
TT
596 gdb_printf (outfile, "register %s",
597 plongest (SYMBOL_VALUE (symbol)));
c906108c
SS
598 break;
599
600 case LOC_ARG:
6cb06a8c
TT
601 gdb_printf (outfile, "arg at offset %s",
602 hex_string (SYMBOL_VALUE (symbol)));
c906108c
SS
603 break;
604
c906108c 605 case LOC_REF_ARG:
6cb06a8c
TT
606 gdb_printf (outfile, "reference arg at %s",
607 hex_string (SYMBOL_VALUE (symbol)));
c906108c
SS
608 break;
609
c906108c 610 case LOC_REGPARM_ADDR:
6cb06a8c
TT
611 gdb_printf (outfile, "address parameter register %s",
612 plongest (SYMBOL_VALUE (symbol)));
c906108c
SS
613 break;
614
615 case LOC_LOCAL:
6cb06a8c
TT
616 gdb_printf (outfile, "local at offset %s",
617 hex_string (SYMBOL_VALUE (symbol)));
c906108c
SS
618 break;
619
c906108c
SS
620 case LOC_TYPEDEF:
621 break;
622
623 case LOC_LABEL:
6cb06a8c 624 gdb_printf (outfile, "label at ");
0426ad51
TT
625 gdb_puts (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
626 outfile);
714835d5 627 if (section)
6cb06a8c
TT
628 gdb_printf (outfile, " section %s",
629 bfd_section_name (section->the_bfd_section));
c906108c
SS
630 break;
631
632 case LOC_BLOCK:
6cb06a8c 633 gdb_printf
64b7cc50
TT
634 (outfile, "block object %s, %s..%s",
635 host_address_to_string (SYMBOL_BLOCK_VALUE (symbol)),
636 paddress (gdbarch, BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
637 paddress (gdbarch, BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))));
714835d5 638 if (section)
6cb06a8c
TT
639 gdb_printf (outfile, " section %s",
640 bfd_section_name (section->the_bfd_section));
c906108c
SS
641 break;
642
4c2df51b 643 case LOC_COMPUTED:
6cb06a8c 644 gdb_printf (outfile, "computed at runtime");
4c2df51b
DJ
645 break;
646
c906108c 647 case LOC_UNRESOLVED:
6cb06a8c 648 gdb_printf (outfile, "unresolved");
c906108c
SS
649 break;
650
651 case LOC_OPTIMIZED_OUT:
6cb06a8c 652 gdb_printf (outfile, "optimized out");
c906108c
SS
653 break;
654
c5aa993b 655 default:
6cb06a8c
TT
656 gdb_printf (outfile, "botched symbol class %x",
657 symbol->aclass ());
c906108c
SS
658 break;
659 }
660 }
6cb06a8c 661 gdb_printf (outfile, "\n");
c906108c
SS
662}
663
80480540 664static void
e99c83e7 665maintenance_print_msymbols (const char *args, int from_tty)
c906108c 666{
34c41c68 667 struct ui_file *outfile = gdb_stdout;
34c41c68 668 char *objfile_arg = NULL;
34c41c68 669 int i, outfile_idx;
07318b29 670
c906108c
SS
671 dont_repeat ();
672
773a1edc 673 gdb_argv argv (args);
c906108c 674
99e8a4f9 675 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
c906108c 676 {
34c41c68 677 if (strcmp (argv[i], "-objfile") == 0)
c906108c 678 {
34c41c68
DE
679 if (argv[i + 1] == NULL)
680 error (_("Missing objfile name"));
681 objfile_arg = argv[++i];
682 }
683 else if (strcmp (argv[i], "--") == 0)
684 {
685 /* End of options. */
686 ++i;
687 break;
c906108c 688 }
34c41c68
DE
689 else if (argv[i][0] == '-')
690 {
691 /* Future proofing: Don't allow OUTFILE to begin with "-". */
692 error (_("Unknown option: %s"), argv[i]);
693 }
694 else
695 break;
c906108c 696 }
34c41c68 697 outfile_idx = i;
c906108c 698
d7e74731
PA
699 stdio_file arg_outfile;
700
99e8a4f9 701 if (argv != NULL && argv[outfile_idx] != NULL)
34c41c68 702 {
34c41c68
DE
703 if (argv[outfile_idx + 1] != NULL)
704 error (_("Junk at end of command"));
ee0c3293
TT
705 gdb::unique_xmalloc_ptr<char> outfile_name
706 (tilde_expand (argv[outfile_idx]));
707 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
708 perror_with_name (outfile_name.get ());
d7e74731 709 outfile = &arg_outfile;
34c41c68 710 }
c5aa993b 711
2030c079 712 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
713 {
714 QUIT;
715 if (objfile_arg == NULL
716 || compare_filenames_for_search (objfile_name (objfile), objfile_arg))
717 dump_msymbols (objfile, outfile);
718 }
c906108c
SS
719}
720
80480540 721static void
e99c83e7 722maintenance_print_objfiles (const char *regexp, int from_tty)
c906108c 723{
c906108c
SS
724 dont_repeat ();
725
52e260a3
DE
726 if (regexp)
727 re_comp (regexp);
728
94c93c35 729 for (struct program_space *pspace : program_spaces)
2030c079 730 for (objfile *objfile : pspace->objfiles ())
27618ce4
TT
731 {
732 QUIT;
52e260a3 733 if (! regexp
4262abfb 734 || re_exec (objfile_name (objfile)))
52e260a3 735 dump_objfile (objfile);
27618ce4 736 }
c906108c
SS
737}
738
5e7b2f39 739/* List all the symbol tables whose names match REGEXP (optional). */
b5ebcee6 740
80480540 741static void
e99c83e7 742maintenance_info_symtabs (const char *regexp, int from_tty)
44ea7b70 743{
db68bbae
DE
744 dont_repeat ();
745
44ea7b70
JB
746 if (regexp)
747 re_comp (regexp);
748
94c93c35 749 for (struct program_space *pspace : program_spaces)
2030c079 750 for (objfile *objfile : pspace->objfiles ())
99d89cde 751 {
99d89cde
TT
752 /* We don't want to print anything for this objfile until we
753 actually find a symtab whose name matches. */
754 int printed_objfile_start = 0;
43f3e411 755
b669c953 756 for (compunit_symtab *cust : objfile->compunits ())
99d89cde
TT
757 {
758 int printed_compunit_symtab_start = 0;
759
102cc235 760 for (symtab *symtab : cust->filetabs ())
99d89cde
TT
761 {
762 QUIT;
763
764 if (! regexp
765 || re_exec (symtab_to_filename_for_display (symtab)))
766 {
767 if (! printed_objfile_start)
768 {
6cb06a8c 769 gdb_printf ("{ objfile %s ", objfile_name (objfile));
1285ce86 770 gdb_stdout->wrap_here (2);
6cb06a8c
TT
771 gdb_printf ("((struct objfile *) %s)\n",
772 host_address_to_string (objfile));
99d89cde
TT
773 printed_objfile_start = 1;
774 }
775 if (! printed_compunit_symtab_start)
776 {
6cb06a8c
TT
777 gdb_printf (" { ((struct compunit_symtab *) %s)\n",
778 host_address_to_string (cust));
779 gdb_printf (" debugformat %s\n",
780 cust->debugformat ());
781 gdb_printf (" producer %s\n",
782 (cust->producer () != nullptr
783 ? cust->producer () : "(null)"));
af7047e8 784 gdb_printf (" name %s\n", cust->name);
6cb06a8c
TT
785 gdb_printf (" dirname %s\n",
786 (cust->dirname () != NULL
787 ? cust->dirname () : "(null)"));
788 gdb_printf (" blockvector"
789 " ((struct blockvector *) %s)\n",
790 host_address_to_string
791 (cust->blockvector ()));
792 gdb_printf (" user"
793 " ((struct compunit_symtab *) %s)\n",
794 cust->user != nullptr
795 ? host_address_to_string (cust->user)
796 : "(null)");
7b1eff95
TV
797 if (cust->includes != nullptr)
798 {
6cb06a8c 799 gdb_printf (" ( includes\n");
7b1eff95
TV
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);
6cb06a8c
TT
808 gdb_printf (" (%s %s)\n",
809 "(struct compunit_symtab *)",
810 addr);
7b1eff95 811 }
6cb06a8c 812 gdb_printf (" )\n");
7b1eff95 813 }
99d89cde
TT
814 printed_compunit_symtab_start = 1;
815 }
816
6cb06a8c
TT
817 gdb_printf ("\t{ symtab %s ",
818 symtab_to_filename_for_display (symtab));
1285ce86 819 gdb_stdout->wrap_here (4);
6cb06a8c
TT
820 gdb_printf ("((struct symtab *) %s)\n",
821 host_address_to_string (symtab));
822 gdb_printf ("\t fullname %s\n",
823 symtab->fullname != NULL
824 ? symtab->fullname
825 : "(null)");
826 gdb_printf ("\t "
827 "linetable ((struct linetable *) %s)\n",
828 host_address_to_string
829 (symtab->linetable ()));
830 gdb_printf ("\t}\n");
99d89cde
TT
831 }
832 }
833
834 if (printed_compunit_symtab_start)
6cb06a8c 835 gdb_printf (" }\n");
99d89cde 836 }
44ea7b70 837
99d89cde 838 if (printed_objfile_start)
6cb06a8c 839 gdb_printf ("}\n");
99d89cde 840 }
44ea7b70 841}
7d0c9981
DE
842
843/* Check consistency of symtabs.
844 An example of what this checks for is NULL blockvectors.
845 They can happen if there's a bug during debug info reading.
846 GDB assumes they are always non-NULL.
847
848 Note: This does not check for psymtab vs symtab consistency.
849 Use "maint check-psymtabs" for that. */
850
851static void
e99c83e7 852maintenance_check_symtabs (const char *ignore, int from_tty)
7d0c9981 853{
94c93c35 854 for (struct program_space *pspace : program_spaces)
2030c079 855 for (objfile *objfile : pspace->objfiles ())
99d89cde 856 {
99d89cde
TT
857 /* We don't want to print anything for this objfile until we
858 actually find something worth printing. */
859 int printed_objfile_start = 0;
7d0c9981 860
b669c953 861 for (compunit_symtab *cust : objfile->compunits ())
99d89cde
TT
862 {
863 int found_something = 0;
0b17a4f7 864 struct symtab *symtab = cust->primary_filetab ();
99d89cde
TT
865
866 QUIT;
867
af39c5c8 868 if (cust->blockvector () == NULL)
99d89cde
TT
869 found_something = 1;
870 /* Add more checks here. */
871
872 if (found_something)
873 {
874 if (! printed_objfile_start)
875 {
6cb06a8c 876 gdb_printf ("{ objfile %s ", objfile_name (objfile));
1285ce86 877 gdb_stdout->wrap_here (2);
6cb06a8c
TT
878 gdb_printf ("((struct objfile *) %s)\n",
879 host_address_to_string (objfile));
99d89cde
TT
880 printed_objfile_start = 1;
881 }
6cb06a8c
TT
882 gdb_printf (" { symtab %s\n",
883 symtab_to_filename_for_display (symtab));
af39c5c8 884 if (cust->blockvector () == NULL)
6cb06a8c
TT
885 gdb_printf (" NULL blockvector\n");
886 gdb_printf (" }\n");
99d89cde
TT
887 }
888 }
7d0c9981 889
99d89cde 890 if (printed_objfile_start)
6cb06a8c 891 gdb_printf ("}\n");
99d89cde 892 }
7d0c9981
DE
893}
894
7d0c9981
DE
895/* Expand all symbol tables whose name matches an optional regexp. */
896
897static void
e99c83e7 898maintenance_expand_symtabs (const char *args, int from_tty)
7d0c9981 899{
7d0c9981
DE
900 char *regexp = NULL;
901
902 /* We use buildargv here so that we handle spaces in the regexp
903 in a way that allows adding more arguments later. */
773a1edc 904 gdb_argv argv (args);
7d0c9981
DE
905
906 if (argv != NULL)
907 {
908 if (argv[0] != NULL)
909 {
910 regexp = argv[0];
911 if (argv[1] != NULL)
912 error (_("Extra arguments after regexp."));
913 }
914 }
915
916 if (regexp)
917 re_comp (regexp);
918
94c93c35 919 for (struct program_space *pspace : program_spaces)
2030c079 920 for (objfile *objfile : pspace->objfiles ())
4d080b46
TT
921 objfile->expand_symtabs_matching
922 ([&] (const char *filename, bool basenames)
923 {
924 /* KISS: Only apply the regexp to the complete file name. */
925 return (!basenames
926 && (regexp == NULL || re_exec (filename)));
927 },
928 NULL,
929 NULL,
930 NULL,
03a8ea51 931 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
3bfa51a7 932 UNDEF_DOMAIN,
4d080b46 933 ALL_DOMAIN);
7d0c9981 934}
c906108c 935\f
c5aa993b 936
c906108c
SS
937/* Return the nexting depth of a block within other blocks in its symtab. */
938
939static int
582942f4 940block_depth (const struct block *block)
c906108c 941{
52f0bd74 942 int i = 0;
433759f7 943
c5aa993b 944 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
c906108c
SS
945 {
946 i++;
947 }
948 return i;
949}
c906108c 950\f
c5aa993b 951
f2403c39
AB
952/* Used by MAINTENANCE_INFO_LINE_TABLES to print the information about a
953 single line table. */
954
955static int
956maintenance_print_one_line_table (struct symtab *symtab, void *data)
957{
958 struct linetable *linetable;
959 struct objfile *objfile;
960
c6159652 961 objfile = symtab->compunit ()->objfile ();
6cb06a8c
TT
962 gdb_printf (_("objfile: %ps ((struct objfile *) %s)\n"),
963 styled_string (file_name_style.style (),
964 objfile_name (objfile)),
965 host_address_to_string (objfile));
966 gdb_printf (_("compunit_symtab: %s ((struct compunit_symtab *) %s)\n"),
967 symtab->compunit ()->name,
968 host_address_to_string (symtab->compunit ()));
969 gdb_printf (_("symtab: %ps ((struct symtab *) %s)\n"),
970 styled_string (file_name_style.style (),
971 symtab_to_fullname (symtab)),
972 host_address_to_string (symtab));
5b607461 973 linetable = symtab->linetable ();
6cb06a8c
TT
974 gdb_printf (_("linetable: ((struct linetable *) %s):\n"),
975 host_address_to_string (linetable));
f2403c39
AB
976
977 if (linetable == NULL)
6cb06a8c 978 gdb_printf (_("No line table.\n"));
f2403c39 979 else if (linetable->nitems <= 0)
6cb06a8c 980 gdb_printf (_("Line table has no lines.\n"));
f2403c39
AB
981 else
982 {
f2403c39
AB
983 /* Leave space for 6 digits of index and line number. After that the
984 tables will just not format as well. */
1773be9e
TT
985 struct ui_out *uiout = current_uiout;
986 ui_out_emit_table table_emitter (uiout, 4, -1, "line-table");
987 uiout->table_header (6, ui_left, "index", _("INDEX"));
988 uiout->table_header (6, ui_left, "line", _("LINE"));
989 uiout->table_header (18, ui_left, "address", _("ADDRESS"));
990 uiout->table_header (1, ui_left, "is-stmt", _("IS-STMT"));
991 uiout->table_body ();
992
993 for (int i = 0; i < linetable->nitems; ++i)
f2403c39
AB
994 {
995 struct linetable_entry *item;
f2403c39
AB
996
997 item = &linetable->item [i];
1773be9e
TT
998 ui_out_emit_tuple tuple_emitter (uiout, nullptr);
999 uiout->field_signed ("index", i);
94a72be7 1000 if (item->line > 0)
1773be9e 1001 uiout->field_signed ("line", item->line);
94a72be7 1002 else
1773be9e 1003 uiout->field_string ("line", _("END"));
08feed99 1004 uiout->field_core_addr ("address", objfile->arch (),
1773be9e
TT
1005 item->pc);
1006 uiout->field_string ("is-stmt", item->is_stmt ? "Y" : "");
1007 uiout->text ("\n");
f2403c39
AB
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 1018{
f2403c39
AB
1019 dont_repeat ();
1020
1021 if (regexp != NULL)
1022 re_comp (regexp);
1023
94c93c35 1024 for (struct program_space *pspace : program_spaces)
2030c079 1025 for (objfile *objfile : pspace->objfiles ())
99d89cde 1026 {
b669c953 1027 for (compunit_symtab *cust : objfile->compunits ())
99d89cde 1028 {
102cc235 1029 for (symtab *symtab : cust->filetabs ())
99d89cde
TT
1030 {
1031 QUIT;
1032
1033 if (regexp == NULL
1034 || re_exec (symtab_to_filename_for_display (symtab)))
92228a33
SM
1035 {
1036 maintenance_print_one_line_table (symtab, NULL);
6cb06a8c 1037 gdb_printf ("\n");
92228a33 1038 }
99d89cde
TT
1039 }
1040 }
1041 }
f2403c39
AB
1042}
1043
1044\f
1045
c378eb4e 1046/* Do early runtime initializations. */
b5ebcee6 1047
6c265988 1048void _initialize_symmisc ();
c906108c 1049void
6c265988 1050_initialize_symmisc ()
c906108c 1051{
80480540
YQ
1052 add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
1053Print dump of current symbol definitions.\n\
48c5e7e2
TT
1054Usage: mt print symbols [-pc ADDRESS] [--] [OUTFILE]\n\
1055 mt print symbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
34c41c68
DE
1056Entries in the full symbol table are dumped to file OUTFILE,\n\
1057or the terminal if OUTFILE is unspecified.\n\
1058If ADDRESS is provided, dump only the file for that address.\n\
1059If SOURCE is provided, dump only that file's symbols.\n\
1060If OBJFILE is provided, dump only that file's minimal symbols."),
80480540
YQ
1061 &maintenanceprintlist);
1062
1063 add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
1064Print dump of current minimal symbol definitions.\n\
48c5e7e2 1065Usage: mt print msymbols [-objfile OBJFILE] [--] [OUTFILE]\n\
34c41c68
DE
1066Entries in the minimal symbol table are dumped to file OUTFILE,\n\
1067or the terminal if OUTFILE is unspecified.\n\
1068If OBJFILE is provided, dump only that file's minimal symbols."),
80480540
YQ
1069 &maintenanceprintlist);
1070
1071 add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
52e260a3
DE
1072 _("Print dump of current object file definitions.\n\
1073With an argument REGEXP, list the object files with matching names."),
80480540
YQ
1074 &maintenanceprintlist);
1075
1076 add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
1077List the full symbol tables for all object files.\n\
1078This does not include information about individual symbols, blocks, or\n\
1079linetables --- just the symbol table structures themselves.\n\
db68bbae 1080With an argument REGEXP, list the symbol tables with matching names."),
80480540 1081 &maintenanceinfolist);
7d0c9981 1082
f2403c39
AB
1083 add_cmd ("line-table", class_maintenance, maintenance_info_line_tables, _("\
1084List the contents of all line tables, from all symbol tables.\n\
1085With an argument REGEXP, list just the line tables for the symbol\n\
1086tables with matching names."),
1087 &maintenanceinfolist);
1088
7d0c9981
DE
1089 add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
1090 _("\
1091Check consistency of currently expanded symtabs."),
1092 &maintenancelist);
1093
1094 add_cmd ("expand-symtabs", class_maintenance, maintenance_expand_symtabs,
1095 _("Expand symbol tables.\n\
1096With an argument REGEXP, only expand the symbol tables with matching names."),
1097 &maintenancelist);
c906108c 1098}