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