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