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