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