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