]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/symmisc.c
Fix AMD64 return value ABI in expression evaluation
[thirdparty/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",
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 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
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 331 fprintf_filtered (outfile, ", %d syms/buckets in ",
b026f593 332 mdict_size (BLOCK_MULTIDICT (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. */
b026f593 350 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (b), miter, sym)
c906108c 351 {
a70b8144 352 try
bf469271
PA
353 {
354 print_symbol (gdbarch, sym, depth + 1, outfile);
355 }
230d2906 356 catch (const gdb_exception_error &ex)
bf469271
PA
357 {
358 exception_fprintf (gdb_stderr, ex,
359 "Error printing symbol:\n");
360 }
c906108c
SS
361 }
362 }
363 fprintf_filtered (outfile, "\n");
364 }
365 else
366 {
6c739336
DE
367 const char *compunit_filename
368 = symtab_to_filename_for_display (COMPUNIT_FILETABS (SYMTAB_COMPUNIT (symtab)));
369
370 fprintf_filtered (outfile,
371 "\nBlockvector same as owning compunit: %s\n\n",
372 compunit_filename);
c906108c
SS
373 }
374}
375
44b164c5 376static void
d04c1a59 377dump_symtab (struct symtab *symtab, struct ui_file *outfile)
44b164c5 378{
44b164c5
JB
379 /* Set the current language to the language of the symtab we're dumping
380 because certain routines used during dump_symtab() use the current
969107c5
EZ
381 language to print an image of the symbol. We'll restore it later.
382 But use only real languages, not placeholders. */
383 if (symtab->language != language_unknown
384 && symtab->language != language_auto)
385 {
9bb9b2f9
TT
386 scoped_restore_current_language save_lang;
387 set_language (symtab->language);
d04c1a59 388 dump_symtab_1 (symtab, outfile);
969107c5
EZ
389 }
390 else
d04c1a59 391 dump_symtab_1 (symtab, outfile);
44b164c5
JB
392}
393
80480540 394static void
e99c83e7 395maintenance_print_symbols (const char *args, int from_tty)
c906108c 396{
34c41c68 397 struct ui_file *outfile = gdb_stdout;
34c41c68
DE
398 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
399 int i, outfile_idx;
c906108c
SS
400
401 dont_repeat ();
402
773a1edc 403 gdb_argv argv (args);
c906108c 404
99e8a4f9 405 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
c906108c 406 {
34c41c68
DE
407 if (strcmp (argv[i], "-pc") == 0)
408 {
409 if (argv[i + 1] == NULL)
410 error (_("Missing pc value"));
411 address_arg = argv[++i];
412 }
413 else if (strcmp (argv[i], "-source") == 0)
414 {
415 if (argv[i + 1] == NULL)
416 error (_("Missing source file"));
417 source_arg = argv[++i];
418 }
419 else if (strcmp (argv[i], "-objfile") == 0)
420 {
421 if (argv[i + 1] == NULL)
422 error (_("Missing objfile name"));
423 objfile_arg = argv[++i];
424 }
425 else if (strcmp (argv[i], "--") == 0)
426 {
427 /* End of options. */
428 ++i;
429 break;
430 }
431 else if (argv[i][0] == '-')
c906108c 432 {
34c41c68
DE
433 /* Future proofing: Don't allow OUTFILE to begin with "-". */
434 error (_("Unknown option: %s"), argv[i]);
c906108c 435 }
34c41c68
DE
436 else
437 break;
c906108c 438 }
34c41c68 439 outfile_idx = i;
c906108c 440
34c41c68
DE
441 if (address_arg != NULL && source_arg != NULL)
442 error (_("Must specify at most one of -pc and -source"));
c5aa993b 443
d7e74731
PA
444 stdio_file arg_outfile;
445
99e8a4f9 446 if (argv != NULL && argv[outfile_idx] != NULL)
34c41c68 447 {
34c41c68
DE
448 if (argv[outfile_idx + 1] != NULL)
449 error (_("Junk at end of command"));
ee0c3293
TT
450 gdb::unique_xmalloc_ptr<char> outfile_name
451 (tilde_expand (argv[outfile_idx]));
452 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
453 perror_with_name (outfile_name.get ());
d7e74731 454 outfile = &arg_outfile;
34c41c68 455 }
c906108c 456
34c41c68 457 if (address_arg != NULL)
27618ce4 458 {
34c41c68
DE
459 CORE_ADDR pc = parse_and_eval_address (address_arg);
460 struct symtab *s = find_pc_line_symtab (pc);
461
462 if (s == NULL)
463 error (_("No symtab for address: %s"), address_arg);
464 dump_symtab (s, outfile);
27618ce4 465 }
34c41c68
DE
466 else
467 {
34c41c68
DE
468 int found = 0;
469
2030c079 470 for (objfile *objfile : current_program_space->objfiles ())
34c41c68
DE
471 {
472 int print_for_objfile = 1;
473
474 if (objfile_arg != NULL)
475 print_for_objfile
476 = compare_filenames_for_search (objfile_name (objfile),
477 objfile_arg);
478 if (!print_for_objfile)
479 continue;
480
b669c953 481 for (compunit_symtab *cu : objfile->compunits ())
34c41c68 482 {
d5da8b3c 483 for (symtab *s : compunit_filetabs (cu))
34c41c68 484 {
d5da8b3c
TT
485 int print_for_source = 0;
486
487 QUIT;
488 if (source_arg != NULL)
489 {
490 print_for_source
491 = compare_filenames_for_search
492 (symtab_to_filename_for_display (s), source_arg);
493 found = 1;
494 }
495 if (source_arg == NULL
496 || print_for_source)
497 dump_symtab (s, outfile);
34c41c68 498 }
34c41c68
DE
499 }
500 }
501
502 if (source_arg != NULL && !found)
503 error (_("No symtab for source file: %s"), source_arg);
504 }
c906108c
SS
505}
506
bf469271 507/* Print symbol SYMBOL on OUTFILE. DEPTH says how far to indent. */
c906108c 508
bf469271
PA
509static void
510print_symbol (struct gdbarch *gdbarch, struct symbol *symbol,
511 int depth, ui_file *outfile)
c906108c 512{
1994afbf
DE
513 struct obj_section *section;
514
515 if (SYMBOL_OBJFILE_OWNED (symbol))
516 section = SYMBOL_OBJ_SECTION (symbol_objfile (symbol), symbol);
517 else
518 section = NULL;
c906108c
SS
519
520 print_spaces (depth, outfile);
176620f1 521 if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
c906108c 522 {
de5ad195 523 fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
5af949e3
UW
524 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
525 outfile);
714835d5 526 if (section)
c906108c 527 fprintf_filtered (outfile, " section %s\n",
714835d5
UW
528 bfd_section_name (section->the_bfd_section->owner,
529 section->the_bfd_section));
c906108c
SS
530 else
531 fprintf_filtered (outfile, "\n");
bf469271 532 return;
c906108c 533 }
bf469271 534
176620f1 535 if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
c906108c 536 {
e86ca25f 537 if (TYPE_NAME (SYMBOL_TYPE (symbol)))
c906108c 538 {
79d43c61
TT
539 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
540 &type_print_raw_options);
c906108c
SS
541 }
542 else
543 {
544 fprintf_filtered (outfile, "%s %s = ",
c5aa993b
JM
545 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
546 ? "enum"
547 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
548 ? "struct" : "union")),
3567439c 549 SYMBOL_LINKAGE_NAME (symbol));
79d43c61
TT
550 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
551 &type_print_raw_options);
c906108c
SS
552 }
553 fprintf_filtered (outfile, ";\n");
554 }
555 else
556 {
557 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
558 fprintf_filtered (outfile, "typedef ");
559 if (SYMBOL_TYPE (symbol))
560 {
561 /* Print details of types, except for enums where it's clutter. */
de5ad195 562 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
c906108c
SS
563 outfile,
564 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
79d43c61
TT
565 depth,
566 &type_print_raw_options);
c906108c
SS
567 fprintf_filtered (outfile, "; ");
568 }
569 else
de5ad195 570 fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
c906108c
SS
571
572 switch (SYMBOL_CLASS (symbol))
573 {
574 case LOC_CONST:
12df843f
JK
575 fprintf_filtered (outfile, "const %s (%s)",
576 plongest (SYMBOL_VALUE (symbol)),
577 hex_string (SYMBOL_VALUE (symbol)));
c906108c
SS
578 break;
579
580 case LOC_CONST_BYTES:
581 {
582 unsigned i;
583 struct type *type = check_typedef (SYMBOL_TYPE (symbol));
433759f7 584
cc1defb1
KS
585 fprintf_filtered (outfile, "const %s hex bytes:",
586 pulongest (TYPE_LENGTH (type)));
c906108c
SS
587 for (i = 0; i < TYPE_LENGTH (type); i++)
588 fprintf_filtered (outfile, " %02x",
c5aa993b 589 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
c906108c
SS
590 }
591 break;
592
593 case LOC_STATIC:
594 fprintf_filtered (outfile, "static at ");
5af949e3
UW
595 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
596 outfile);
714835d5 597 if (section)
c906108c 598 fprintf_filtered (outfile, " section %s",
714835d5
UW
599 bfd_section_name (section->the_bfd_section->owner,
600 section->the_bfd_section));
c906108c
SS
601 break;
602
c906108c 603 case LOC_REGISTER:
2a2d4dc3 604 if (SYMBOL_IS_ARGUMENT (symbol))
12df843f
JK
605 fprintf_filtered (outfile, "parameter register %s",
606 plongest (SYMBOL_VALUE (symbol)));
2a2d4dc3 607 else
12df843f
JK
608 fprintf_filtered (outfile, "register %s",
609 plongest (SYMBOL_VALUE (symbol)));
c906108c
SS
610 break;
611
612 case LOC_ARG:
12df843f
JK
613 fprintf_filtered (outfile, "arg at offset %s",
614 hex_string (SYMBOL_VALUE (symbol)));
c906108c
SS
615 break;
616
c906108c 617 case LOC_REF_ARG:
12df843f
JK
618 fprintf_filtered (outfile, "reference arg at %s",
619 hex_string (SYMBOL_VALUE (symbol)));
c906108c
SS
620 break;
621
c906108c 622 case LOC_REGPARM_ADDR:
12df843f
JK
623 fprintf_filtered (outfile, "address parameter register %s",
624 plongest (SYMBOL_VALUE (symbol)));
c906108c
SS
625 break;
626
627 case LOC_LOCAL:
12df843f
JK
628 fprintf_filtered (outfile, "local at offset %s",
629 hex_string (SYMBOL_VALUE (symbol)));
c906108c
SS
630 break;
631
c906108c
SS
632 case LOC_TYPEDEF:
633 break;
634
635 case LOC_LABEL:
636 fprintf_filtered (outfile, "label at ");
5af949e3
UW
637 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
638 outfile);
714835d5 639 if (section)
c906108c 640 fprintf_filtered (outfile, " section %s",
714835d5
UW
641 bfd_section_name (section->the_bfd_section->owner,
642 section->the_bfd_section));
c906108c
SS
643 break;
644
645 case LOC_BLOCK:
646 fprintf_filtered (outfile, "block object ");
d4f3574e 647 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
c906108c 648 fprintf_filtered (outfile, ", ");
5af949e3
UW
649 fputs_filtered (paddress (gdbarch,
650 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
ed49a04f 651 outfile);
c906108c 652 fprintf_filtered (outfile, "..");
5af949e3
UW
653 fputs_filtered (paddress (gdbarch,
654 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
ed49a04f 655 outfile);
714835d5 656 if (section)
c906108c 657 fprintf_filtered (outfile, " section %s",
714835d5
UW
658 bfd_section_name (section->the_bfd_section->owner,
659 section->the_bfd_section));
c906108c
SS
660 break;
661
4c2df51b 662 case LOC_COMPUTED:
4c2df51b
DJ
663 fprintf_filtered (outfile, "computed at runtime");
664 break;
665
c906108c
SS
666 case LOC_UNRESOLVED:
667 fprintf_filtered (outfile, "unresolved");
668 break;
669
670 case LOC_OPTIMIZED_OUT:
671 fprintf_filtered (outfile, "optimized out");
672 break;
673
c5aa993b 674 default:
c906108c
SS
675 fprintf_filtered (outfile, "botched symbol class %x",
676 SYMBOL_CLASS (symbol));
677 break;
678 }
679 }
680 fprintf_filtered (outfile, "\n");
c906108c
SS
681}
682
80480540 683static void
e99c83e7 684maintenance_print_msymbols (const char *args, int from_tty)
c906108c 685{
34c41c68 686 struct ui_file *outfile = gdb_stdout;
34c41c68 687 char *objfile_arg = NULL;
34c41c68 688 int i, outfile_idx;
07318b29 689
c906108c
SS
690 dont_repeat ();
691
773a1edc 692 gdb_argv argv (args);
c906108c 693
99e8a4f9 694 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
c906108c 695 {
34c41c68 696 if (strcmp (argv[i], "-objfile") == 0)
c906108c 697 {
34c41c68
DE
698 if (argv[i + 1] == NULL)
699 error (_("Missing objfile name"));
700 objfile_arg = argv[++i];
701 }
702 else if (strcmp (argv[i], "--") == 0)
703 {
704 /* End of options. */
705 ++i;
706 break;
c906108c 707 }
34c41c68
DE
708 else if (argv[i][0] == '-')
709 {
710 /* Future proofing: Don't allow OUTFILE to begin with "-". */
711 error (_("Unknown option: %s"), argv[i]);
712 }
713 else
714 break;
c906108c 715 }
34c41c68 716 outfile_idx = i;
c906108c 717
d7e74731
PA
718 stdio_file arg_outfile;
719
99e8a4f9 720 if (argv != NULL && argv[outfile_idx] != NULL)
34c41c68 721 {
34c41c68
DE
722 if (argv[outfile_idx + 1] != NULL)
723 error (_("Junk at end of command"));
ee0c3293
TT
724 gdb::unique_xmalloc_ptr<char> outfile_name
725 (tilde_expand (argv[outfile_idx]));
726 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
727 perror_with_name (outfile_name.get ());
d7e74731 728 outfile = &arg_outfile;
34c41c68 729 }
c5aa993b 730
2030c079 731 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
732 {
733 QUIT;
734 if (objfile_arg == NULL
735 || compare_filenames_for_search (objfile_name (objfile), objfile_arg))
736 dump_msymbols (objfile, outfile);
737 }
c906108c
SS
738}
739
80480540 740static void
e99c83e7 741maintenance_print_objfiles (const char *regexp, int from_tty)
c906108c 742{
6c95b8df 743 struct program_space *pspace;
c906108c
SS
744
745 dont_repeat ();
746
52e260a3
DE
747 if (regexp)
748 re_comp (regexp);
749
6c95b8df 750 ALL_PSPACES (pspace)
2030c079 751 for (objfile *objfile : pspace->objfiles ())
27618ce4
TT
752 {
753 QUIT;
52e260a3 754 if (! regexp
4262abfb 755 || re_exec (objfile_name (objfile)))
52e260a3 756 dump_objfile (objfile);
27618ce4 757 }
c906108c
SS
758}
759
5e7b2f39 760/* List all the symbol tables whose names match REGEXP (optional). */
b5ebcee6 761
80480540 762static void
e99c83e7 763maintenance_info_symtabs (const char *regexp, int from_tty)
44ea7b70 764{
6c95b8df 765 struct program_space *pspace;
44ea7b70 766
db68bbae
DE
767 dont_repeat ();
768
44ea7b70
JB
769 if (regexp)
770 re_comp (regexp);
771
6c95b8df 772 ALL_PSPACES (pspace)
2030c079 773 for (objfile *objfile : pspace->objfiles ())
99d89cde 774 {
99d89cde
TT
775 /* We don't want to print anything for this objfile until we
776 actually find a symtab whose name matches. */
777 int printed_objfile_start = 0;
43f3e411 778
b669c953 779 for (compunit_symtab *cust : objfile->compunits ())
99d89cde
TT
780 {
781 int printed_compunit_symtab_start = 0;
782
5accd1a0 783 for (symtab *symtab : compunit_filetabs (cust))
99d89cde
TT
784 {
785 QUIT;
786
787 if (! regexp
788 || re_exec (symtab_to_filename_for_display (symtab)))
789 {
790 if (! printed_objfile_start)
791 {
792 printf_filtered ("{ objfile %s ", objfile_name (objfile));
793 wrap_here (" ");
794 printf_filtered ("((struct objfile *) %s)\n",
795 host_address_to_string (objfile));
796 printed_objfile_start = 1;
797 }
798 if (! printed_compunit_symtab_start)
799 {
800 printf_filtered (" { ((struct compunit_symtab *) %s)\n",
801 host_address_to_string (cust));
802 printf_filtered (" debugformat %s\n",
803 COMPUNIT_DEBUGFORMAT (cust));
804 printf_filtered (" producer %s\n",
805 COMPUNIT_PRODUCER (cust) != NULL
806 ? COMPUNIT_PRODUCER (cust)
807 : "(null)");
808 printf_filtered (" dirname %s\n",
809 COMPUNIT_DIRNAME (cust) != NULL
810 ? COMPUNIT_DIRNAME (cust)
811 : "(null)");
812 printf_filtered (" blockvector"
813 " ((struct blockvector *) %s)\n",
814 host_address_to_string
43f3e411 815 (COMPUNIT_BLOCKVECTOR (cust)));
99d89cde
TT
816 printed_compunit_symtab_start = 1;
817 }
818
819 printf_filtered ("\t{ symtab %s ",
820 symtab_to_filename_for_display (symtab));
821 wrap_here (" ");
822 printf_filtered ("((struct symtab *) %s)\n",
823 host_address_to_string (symtab));
824 printf_filtered ("\t fullname %s\n",
825 symtab->fullname != NULL
826 ? symtab->fullname
827 : "(null)");
828 printf_filtered ("\t "
829 "linetable ((struct linetable *) %s)\n",
830 host_address_to_string (symtab->linetable));
831 printf_filtered ("\t}\n");
832 }
833 }
834
835 if (printed_compunit_symtab_start)
836 printf_filtered (" }\n");
837 }
44ea7b70 838
99d89cde
TT
839 if (printed_objfile_start)
840 printf_filtered ("}\n");
841 }
44ea7b70 842}
7d0c9981
DE
843
844/* Check consistency of symtabs.
845 An example of what this checks for is NULL blockvectors.
846 They can happen if there's a bug during debug info reading.
847 GDB assumes they are always non-NULL.
848
849 Note: This does not check for psymtab vs symtab consistency.
850 Use "maint check-psymtabs" for that. */
851
852static void
e99c83e7 853maintenance_check_symtabs (const char *ignore, int from_tty)
7d0c9981
DE
854{
855 struct program_space *pspace;
7d0c9981
DE
856
857 ALL_PSPACES (pspace)
2030c079 858 for (objfile *objfile : pspace->objfiles ())
99d89cde 859 {
99d89cde
TT
860 /* We don't want to print anything for this objfile until we
861 actually find something worth printing. */
862 int printed_objfile_start = 0;
7d0c9981 863
b669c953 864 for (compunit_symtab *cust : objfile->compunits ())
99d89cde
TT
865 {
866 int found_something = 0;
867 struct symtab *symtab = compunit_primary_filetab (cust);
868
869 QUIT;
870
871 if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
872 found_something = 1;
873 /* Add more checks here. */
874
875 if (found_something)
876 {
877 if (! printed_objfile_start)
878 {
879 printf_filtered ("{ objfile %s ", objfile_name (objfile));
880 wrap_here (" ");
881 printf_filtered ("((struct objfile *) %s)\n",
882 host_address_to_string (objfile));
883 printed_objfile_start = 1;
884 }
885 printf_filtered (" { symtab %s\n",
886 symtab_to_filename_for_display (symtab));
887 if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
888 printf_filtered (" NULL blockvector\n");
889 printf_filtered (" }\n");
890 }
891 }
7d0c9981 892
99d89cde
TT
893 if (printed_objfile_start)
894 printf_filtered ("}\n");
895 }
7d0c9981
DE
896}
897
7d0c9981
DE
898/* Expand all symbol tables whose name matches an optional regexp. */
899
900static void
e99c83e7 901maintenance_expand_symtabs (const char *args, int from_tty)
7d0c9981
DE
902{
903 struct program_space *pspace;
7d0c9981
DE
904 char *regexp = NULL;
905
906 /* We use buildargv here so that we handle spaces in the regexp
907 in a way that allows adding more arguments later. */
773a1edc 908 gdb_argv argv (args);
7d0c9981
DE
909
910 if (argv != NULL)
911 {
912 if (argv[0] != NULL)
913 {
914 regexp = argv[0];
915 if (argv[1] != NULL)
916 error (_("Extra arguments after regexp."));
917 }
918 }
919
920 if (regexp)
921 re_comp (regexp);
922
923 ALL_PSPACES (pspace)
2030c079 924 for (objfile *objfile : pspace->objfiles ())
99d89cde
TT
925 {
926 if (objfile->sf)
927 {
928 objfile->sf->qf->expand_symtabs_matching
929 (objfile,
930 [&] (const char *filename, bool basenames)
931 {
932 /* KISS: Only apply the regexp to the complete file name. */
933 return (!basenames
934 && (regexp == NULL || re_exec (filename)));
935 },
936 lookup_name_info::match_any (),
937 [] (const char *symname)
938 {
939 /* Since we're not searching on symbols, just return true. */
940 return true;
941 },
942 NULL,
943 ALL_DOMAIN);
944 }
945 }
7d0c9981 946}
c906108c 947\f
c5aa993b 948
c906108c
SS
949/* Return the nexting depth of a block within other blocks in its symtab. */
950
951static int
582942f4 952block_depth (const struct block *block)
c906108c 953{
52f0bd74 954 int i = 0;
433759f7 955
c5aa993b 956 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
c906108c
SS
957 {
958 i++;
959 }
960 return i;
961}
c906108c 962\f
c5aa993b 963
f2403c39
AB
964/* Used by MAINTENANCE_INFO_LINE_TABLES to print the information about a
965 single line table. */
966
967static int
968maintenance_print_one_line_table (struct symtab *symtab, void *data)
969{
970 struct linetable *linetable;
971 struct objfile *objfile;
972
973 objfile = symtab->compunit_symtab->objfile;
974 printf_filtered (_("objfile: %s ((struct objfile *) %s)\n"),
975 objfile_name (objfile),
976 host_address_to_string (objfile));
977 printf_filtered (_("compunit_symtab: ((struct compunit_symtab *) %s)\n"),
978 host_address_to_string (symtab->compunit_symtab));
979 printf_filtered (_("symtab: %s ((struct symtab *) %s)\n"),
980 symtab_to_fullname (symtab),
981 host_address_to_string (symtab));
982 linetable = SYMTAB_LINETABLE (symtab);
983 printf_filtered (_("linetable: ((struct linetable *) %s):\n"),
984 host_address_to_string (linetable));
985
986 if (linetable == NULL)
987 printf_filtered (_("No line table.\n"));
988 else if (linetable->nitems <= 0)
989 printf_filtered (_("Line table has no lines.\n"));
990 else
991 {
992 int i;
993
994 /* Leave space for 6 digits of index and line number. After that the
995 tables will just not format as well. */
996 printf_filtered (_("%-6s %6s %s\n"),
997 _("INDEX"), _("LINE"), _("ADDRESS"));
998
999 for (i = 0; i < linetable->nitems; ++i)
1000 {
1001 struct linetable_entry *item;
f2403c39
AB
1002
1003 item = &linetable->item [i];
1004 printf_filtered (_("%-6d %6d %s\n"), i, item->line,
1005 core_addr_to_string (item->pc));
1006 }
1007 }
1008
1009 return 0;
1010}
1011
1012/* Implement the 'maint info line-table' command. */
1013
1014static void
e99c83e7 1015maintenance_info_line_tables (const char *regexp, int from_tty)
f2403c39
AB
1016{
1017 struct program_space *pspace;
f2403c39
AB
1018
1019 dont_repeat ();
1020
1021 if (regexp != NULL)
1022 re_comp (regexp);
1023
1024 ALL_PSPACES (pspace)
2030c079 1025 for (objfile *objfile : pspace->objfiles ())
99d89cde 1026 {
b669c953 1027 for (compunit_symtab *cust : objfile->compunits ())
99d89cde 1028 {
5accd1a0 1029 for (symtab *symtab : compunit_filetabs (cust))
99d89cde
TT
1030 {
1031 QUIT;
1032
1033 if (regexp == NULL
1034 || re_exec (symtab_to_filename_for_display (symtab)))
1035 maintenance_print_one_line_table (symtab, NULL);
1036 }
1037 }
1038 }
f2403c39
AB
1039}
1040
1041\f
1042
c378eb4e 1043/* Do early runtime initializations. */
b5ebcee6 1044
c906108c 1045void
fba45db2 1046_initialize_symmisc (void)
c906108c 1047{
c5aa993b 1048 std_in = stdin;
c906108c
SS
1049 std_out = stdout;
1050 std_err = stderr;
80480540
YQ
1051
1052 add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
1053Print dump of current symbol definitions.\n\
48c5e7e2
TT
1054Usage: mt print symbols [-pc ADDRESS] [--] [OUTFILE]\n\
1055 mt print symbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
34c41c68
DE
1056Entries in the full symbol table are dumped to file OUTFILE,\n\
1057or the terminal if OUTFILE is unspecified.\n\
1058If ADDRESS is provided, dump only the file for that address.\n\
1059If SOURCE is provided, dump only that file's symbols.\n\
1060If OBJFILE is provided, dump only that file's minimal symbols."),
80480540
YQ
1061 &maintenanceprintlist);
1062
1063 add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
1064Print dump of current minimal symbol definitions.\n\
48c5e7e2 1065Usage: mt print msymbols [-objfile OBJFILE] [--] [OUTFILE]\n\
34c41c68
DE
1066Entries in the minimal symbol table are dumped to file OUTFILE,\n\
1067or the terminal if OUTFILE is unspecified.\n\
1068If OBJFILE is provided, dump only that file's minimal symbols."),
80480540
YQ
1069 &maintenanceprintlist);
1070
1071 add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
52e260a3
DE
1072 _("Print dump of current object file definitions.\n\
1073With an argument REGEXP, list the object files with matching names."),
80480540
YQ
1074 &maintenanceprintlist);
1075
1076 add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
1077List the full symbol tables for all object files.\n\
1078This does not include information about individual symbols, blocks, or\n\
1079linetables --- just the symbol table structures themselves.\n\
db68bbae 1080With an argument REGEXP, list the symbol tables with matching names."),
80480540 1081 &maintenanceinfolist);
7d0c9981 1082
f2403c39
AB
1083 add_cmd ("line-table", class_maintenance, maintenance_info_line_tables, _("\
1084List the contents of all line tables, from all symbol tables.\n\
1085With an argument REGEXP, list just the line tables for the symbol\n\
1086tables with matching names."),
1087 &maintenanceinfolist);
1088
7d0c9981
DE
1089 add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
1090 _("\
1091Check consistency of currently expanded symtabs."),
1092 &maintenancelist);
1093
1094 add_cmd ("expand-symtabs", class_maintenance, maintenance_expand_symtabs,
1095 _("Expand symbol tables.\n\
1096With an argument REGEXP, only expand the symbol tables with matching names."),
1097 &maintenancelist);
c906108c 1098}