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