C++: dlsym casts in gdb/linux-thread-db.c and gdb/gdbserver/thread-db.c
[deliverable/binutils-gdb.git] / gdb / symmisc.c
CommitLineData
c906108c 1/* Do various things to symbol tables (other than lookup), for GDB.
af5f3db6 2
32d0add0 3 Copyright (C) 1986-2015 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#ifndef DEV_TTY
44#define DEV_TTY "/dev/tty"
45#endif
46
47/* Unfortunately for debugging, stderr is usually a macro. This is painful
48 when calling functions that take FILE *'s from the debugger.
49 So we make a variable which has the same value and which is accessible when
50 debugging GDB with itself. Because stdin et al need not be constants,
51 we initialize them in the _initialize_symmisc function at the bottom
52 of the file. */
53FILE *std_in;
54FILE *std_out;
55FILE *std_err;
56
57/* Prototypes for local functions */
58
a14ed312 59static int block_depth (struct block *);
c906108c 60
a14ed312 61void _initialize_symmisc (void);
c906108c 62
c5aa993b
JM
63struct print_symbol_args
64 {
5af949e3 65 struct gdbarch *gdbarch;
c5aa993b
JM
66 struct symbol *symbol;
67 int depth;
d9fcf2fb 68 struct ui_file *outfile;
c5aa993b 69 };
c906108c 70
4efb68b1 71static int print_symbol (void *);
c906108c 72\f
c906108c 73
c906108c 74void
fba45db2 75print_symbol_bcache_statistics (void)
c906108c 76{
6c95b8df 77 struct program_space *pspace;
c906108c
SS
78 struct objfile *objfile;
79
6c95b8df
PA
80 ALL_PSPACES (pspace)
81 ALL_PSPACE_OBJFILES (pspace, objfile)
c5aa993b 82 {
27618ce4 83 QUIT;
4262abfb
JK
84 printf_filtered (_("Byte cache statistics for '%s':\n"),
85 objfile_name (objfile));
710e1a31
SW
86 print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
87 "partial symbol cache");
6532ff36
TT
88 print_bcache_statistics (objfile->per_bfd->macro_cache,
89 "preprocessor macro cache");
706e3705
TT
90 print_bcache_statistics (objfile->per_bfd->filename_cache,
91 "file name cache");
c5aa993b 92 }
c906108c
SS
93}
94
95void
fba45db2 96print_objfile_statistics (void)
c906108c 97{
6c95b8df 98 struct program_space *pspace;
c906108c 99 struct objfile *objfile;
43f3e411 100 struct compunit_symtab *cu;
c4f90d87 101 struct symtab *s;
c4f90d87 102 int i, linetables, blockvectors;
c906108c 103
6c95b8df
PA
104 ALL_PSPACES (pspace)
105 ALL_PSPACE_OBJFILES (pspace, objfile)
c5aa993b 106 {
27618ce4 107 QUIT;
4262abfb 108 printf_filtered (_("Statistics for '%s':\n"), objfile_name (objfile));
c5aa993b 109 if (OBJSTAT (objfile, n_stabs) > 0)
a3f17187 110 printf_filtered (_(" Number of \"stab\" symbols read: %d\n"),
c5aa993b 111 OBJSTAT (objfile, n_stabs));
5f6cac40 112 if (objfile->per_bfd->n_minsyms > 0)
a3f17187 113 printf_filtered (_(" Number of \"minimal\" symbols read: %d\n"),
5f6cac40 114 objfile->per_bfd->n_minsyms);
c5aa993b 115 if (OBJSTAT (objfile, n_psyms) > 0)
a3f17187 116 printf_filtered (_(" Number of \"partial\" symbols read: %d\n"),
c5aa993b
JM
117 OBJSTAT (objfile, n_psyms));
118 if (OBJSTAT (objfile, n_syms) > 0)
a3f17187 119 printf_filtered (_(" Number of \"full\" symbols read: %d\n"),
c5aa993b
JM
120 OBJSTAT (objfile, n_syms));
121 if (OBJSTAT (objfile, n_types) > 0)
a3f17187 122 printf_filtered (_(" Number of \"types\" defined: %d\n"),
c5aa993b 123 OBJSTAT (objfile, n_types));
ccefe4c4
TT
124 if (objfile->sf)
125 objfile->sf->qf->print_stats (objfile);
c4f90d87 126 i = linetables = blockvectors = 0;
43f3e411 127 ALL_OBJFILE_FILETABS (objfile, cu, s)
c4f90d87
JM
128 {
129 i++;
8435453b 130 if (SYMTAB_LINETABLE (s) != NULL)
c4f90d87 131 linetables++;
c4f90d87 132 }
43f3e411
DE
133 ALL_OBJFILE_COMPUNITS (objfile, cu)
134 blockvectors++;
a3f17187 135 printf_filtered (_(" Number of symbol tables: %d\n"), i);
26a84859 136 printf_filtered (_(" Number of symbol tables with line tables: %d\n"),
c4f90d87 137 linetables);
26a84859 138 printf_filtered (_(" Number of symbol tables with blockvectors: %d\n"),
c4f90d87 139 blockvectors);
26a84859 140
c5aa993b 141 if (OBJSTAT (objfile, sz_strtab) > 0)
a3f17187 142 printf_filtered (_(" Space used by a.out string tables: %d\n"),
c5aa993b 143 OBJSTAT (objfile, sz_strtab));
241fd515
AM
144 printf_filtered (_(" Total memory used for objfile obstack: %s\n"),
145 pulongest (obstack_memory_used (&objfile
146 ->objfile_obstack)));
147 printf_filtered (_(" Total memory used for BFD obstack: %s\n"),
148 pulongest (obstack_memory_used (&objfile->per_bfd
149 ->storage_obstack)));
a3f17187 150 printf_filtered (_(" Total memory used for psymbol cache: %d\n"),
710e1a31
SW
151 bcache_memory_used (psymbol_bcache_get_bcache
152 (objfile->psymbol_cache)));
a3f17187 153 printf_filtered (_(" Total memory used for macro cache: %d\n"),
6532ff36 154 bcache_memory_used (objfile->per_bfd->macro_cache));
10abe6bf 155 printf_filtered (_(" Total memory used for file name cache: %d\n"),
706e3705 156 bcache_memory_used (objfile->per_bfd->filename_cache));
c5aa993b 157 }
c906108c
SS
158}
159
c5aa993b 160static void
fba45db2 161dump_objfile (struct objfile *objfile)
c906108c 162{
43f3e411 163 struct compunit_symtab *cust;
c906108c 164 struct symtab *symtab;
c906108c 165
4262abfb 166 printf_filtered ("\nObject file %s: ", objfile_name (objfile));
c906108c 167 printf_filtered ("Objfile at ");
d4f3574e 168 gdb_print_host_address (objfile, gdb_stdout);
c906108c 169 printf_filtered (", bfd at ");
d4f3574e 170 gdb_print_host_address (objfile->obfd, gdb_stdout);
c906108c 171 printf_filtered (", %d minsyms\n\n",
34643a32 172 objfile->per_bfd->minimal_symbol_count);
c906108c 173
ccefe4c4
TT
174 if (objfile->sf)
175 objfile->sf->qf->dump (objfile);
c906108c 176
43f3e411 177 if (objfile->compunit_symtabs != NULL)
c906108c
SS
178 {
179 printf_filtered ("Symtabs:\n");
43f3e411 180 ALL_OBJFILE_FILETABS (objfile, cust, symtab)
c906108c 181 {
05cba821 182 printf_filtered ("%s at ", symtab_to_filename_for_display (symtab));
d4f3574e 183 gdb_print_host_address (symtab, gdb_stdout);
c906108c 184 printf_filtered (", ");
eb822aa6 185 if (SYMTAB_OBJFILE (symtab) != objfile)
c906108c
SS
186 {
187 printf_filtered ("NOT ON CHAIN! ");
188 }
189 wrap_here (" ");
190 }
191 printf_filtered ("\n\n");
192 }
193}
194
195/* Print minimal symbols from this objfile. */
c5aa993b
JM
196
197static void
fba45db2 198dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
c906108c 199{
5af949e3 200 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
201 struct minimal_symbol *msymbol;
202 int index;
203 char ms_type;
c5aa993b 204
4262abfb 205 fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile_name (objfile));
34643a32 206 if (objfile->per_bfd->minimal_symbol_count == 0)
c906108c
SS
207 {
208 fprintf_filtered (outfile, "No minimal symbols found.\n");
209 return;
210 }
3567439c
DJ
211 index = 0;
212 ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
c906108c 213 {
efd66ac6 214 struct obj_section *section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
714835d5 215
712f90be 216 switch (MSYMBOL_TYPE (msymbol))
c906108c 217 {
c5aa993b
JM
218 case mst_unknown:
219 ms_type = 'u';
220 break;
221 case mst_text:
222 ms_type = 'T';
223 break;
0875794a
JK
224 case mst_text_gnu_ifunc:
225 ms_type = 'i';
226 break;
c5aa993b
JM
227 case mst_solib_trampoline:
228 ms_type = 'S';
229 break;
230 case mst_data:
231 ms_type = 'D';
232 break;
233 case mst_bss:
234 ms_type = 'B';
235 break;
236 case mst_abs:
237 ms_type = 'A';
238 break;
239 case mst_file_text:
240 ms_type = 't';
241 break;
242 case mst_file_data:
243 ms_type = 'd';
244 break;
245 case mst_file_bss:
246 ms_type = 'b';
247 break;
248 default:
249 ms_type = '?';
250 break;
c906108c
SS
251 }
252 fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
77e371c0
TT
253 fputs_filtered (paddress (gdbarch, MSYMBOL_VALUE_ADDRESS (objfile,
254 msymbol)),
5af949e3 255 outfile);
efd66ac6 256 fprintf_filtered (outfile, " %s", MSYMBOL_LINKAGE_NAME (msymbol));
714835d5 257 if (section)
8625fc1b
TT
258 {
259 if (section->the_bfd_section != NULL)
260 fprintf_filtered (outfile, " section %s",
261 bfd_section_name (objfile->obfd,
262 section->the_bfd_section));
263 else
264 fprintf_filtered (outfile, " spurious section %ld",
4c8429ef 265 (long) (section - objfile->sections));
8625fc1b 266 }
efd66ac6 267 if (MSYMBOL_DEMANGLED_NAME (msymbol) != NULL)
c906108c 268 {
efd66ac6 269 fprintf_filtered (outfile, " %s", MSYMBOL_DEMANGLED_NAME (msymbol));
c906108c 270 }
c906108c
SS
271 if (msymbol->filename)
272 fprintf_filtered (outfile, " %s", msymbol->filename);
c906108c 273 fputs_filtered ("\n", outfile);
3567439c 274 index++;
c906108c 275 }
34643a32 276 if (objfile->per_bfd->minimal_symbol_count != index)
c906108c 277 {
8a3fe4f8 278 warning (_("internal error: minimal symbol count %d != %d"),
34643a32 279 objfile->per_bfd->minimal_symbol_count, index);
c906108c
SS
280 }
281 fprintf_filtered (outfile, "\n");
282}
283
c5aa993b 284static void
44b164c5
JB
285dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
286 struct ui_file *outfile)
c906108c 287{
5af949e3 288 struct gdbarch *gdbarch = get_objfile_arch (objfile);
de4f826b
DC
289 int i;
290 struct dict_iterator iter;
952a6d41 291 int len;
de4f826b 292 struct linetable *l;
346d1dfe 293 const struct blockvector *bv;
e88c90f2 294 struct symbol *sym;
de4f826b 295 struct block *b;
c906108c
SS
296 int depth;
297
05cba821
JK
298 fprintf_filtered (outfile, "\nSymtab for file %s\n",
299 symtab_to_filename_for_display (symtab));
ee6f8984 300 if (SYMTAB_DIRNAME (symtab) != NULL)
c906108c 301 fprintf_filtered (outfile, "Compilation directory is %s\n",
ee6f8984 302 SYMTAB_DIRNAME (symtab));
4262abfb
JK
303 fprintf_filtered (outfile, "Read from object file %s (",
304 objfile_name (objfile));
d4f3574e 305 gdb_print_host_address (objfile, outfile);
c906108c 306 fprintf_filtered (outfile, ")\n");
3e43a32a
MS
307 fprintf_filtered (outfile, "Language: %s\n",
308 language_str (symtab->language));
c906108c
SS
309
310 /* First print the line table. */
8435453b 311 l = SYMTAB_LINETABLE (symtab);
c906108c
SS
312 if (l)
313 {
314 fprintf_filtered (outfile, "\nLine table:\n\n");
315 len = l->nitems;
316 for (i = 0; i < len; i++)
317 {
318 fprintf_filtered (outfile, " line %d at ", l->item[i].line);
5af949e3 319 fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
c906108c
SS
320 fprintf_filtered (outfile, "\n");
321 }
322 }
43f3e411 323 /* Now print the block info, but only for compunit symtabs since we will
c378eb4e 324 print lots of duplicate info otherwise. */
43f3e411 325 if (symtab == COMPUNIT_FILETABS (SYMTAB_COMPUNIT (symtab)))
c906108c
SS
326 {
327 fprintf_filtered (outfile, "\nBlockvector:\n\n");
439247b6 328 bv = SYMTAB_BLOCKVECTOR (symtab);
c906108c
SS
329 len = BLOCKVECTOR_NBLOCKS (bv);
330 for (i = 0; i < len; i++)
331 {
332 b = BLOCKVECTOR_BLOCK (bv, i);
333 depth = block_depth (b) * 2;
334 print_spaces (depth, outfile);
335 fprintf_filtered (outfile, "block #%03d, object at ", i);
d4f3574e 336 gdb_print_host_address (b, outfile);
c906108c
SS
337 if (BLOCK_SUPERBLOCK (b))
338 {
339 fprintf_filtered (outfile, " under ");
d4f3574e 340 gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
c906108c 341 }
261397f8
DJ
342 /* drow/2002-07-10: We could save the total symbols count
343 even if we're using a hashtable, but nothing else but this message
344 wants it. */
de4f826b
DC
345 fprintf_filtered (outfile, ", %d syms/buckets in ",
346 dict_size (BLOCK_DICT (b)));
5af949e3 347 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
c906108c 348 fprintf_filtered (outfile, "..");
5af949e3 349 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
c906108c
SS
350 if (BLOCK_FUNCTION (b))
351 {
3567439c
DJ
352 fprintf_filtered (outfile, ", function %s",
353 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
c906108c
SS
354 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
355 {
356 fprintf_filtered (outfile, ", %s",
c5aa993b 357 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
c906108c
SS
358 }
359 }
c906108c 360 fprintf_filtered (outfile, "\n");
261397f8 361 /* Now print each symbol in this block (in no particular order, if
8157b174
TT
362 we're using a hashtable). Note that we only want this
363 block, not any blocks from included symtabs. */
364 ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
c906108c
SS
365 {
366 struct print_symbol_args s;
433759f7 367
5af949e3 368 s.gdbarch = gdbarch;
e88c90f2 369 s.symbol = sym;
c906108c
SS
370 s.depth = depth + 1;
371 s.outfile = outfile;
372 catch_errors (print_symbol, &s, "Error printing symbol:\n",
5c3ce3f7 373 RETURN_MASK_ERROR);
c906108c
SS
374 }
375 }
376 fprintf_filtered (outfile, "\n");
377 }
378 else
379 {
380 fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
381 }
382}
383
44b164c5
JB
384static void
385dump_symtab (struct objfile *objfile, struct symtab *symtab,
386 struct ui_file *outfile)
387{
44b164c5
JB
388 /* Set the current language to the language of the symtab we're dumping
389 because certain routines used during dump_symtab() use the current
969107c5
EZ
390 language to print an image of the symbol. We'll restore it later.
391 But use only real languages, not placeholders. */
392 if (symtab->language != language_unknown
393 && symtab->language != language_auto)
394 {
395 enum language saved_lang;
396
397 saved_lang = set_language (symtab->language);
44b164c5 398
969107c5 399 dump_symtab_1 (objfile, symtab, outfile);
44b164c5 400
969107c5
EZ
401 set_language (saved_lang);
402 }
403 else
404 dump_symtab_1 (objfile, symtab, outfile);
44b164c5
JB
405}
406
80480540 407static void
fba45db2 408maintenance_print_symbols (char *args, int from_tty)
c906108c
SS
409{
410 char **argv;
d9fcf2fb 411 struct ui_file *outfile;
c906108c
SS
412 struct cleanup *cleanups;
413 char *symname = NULL;
414 char *filename = DEV_TTY;
415 struct objfile *objfile;
43f3e411 416 struct compunit_symtab *cu;
c906108c
SS
417 struct symtab *s;
418
419 dont_repeat ();
420
421 if (args == NULL)
422 {
3e43a32a
MS
423 error (_("Arguments missing: an output file name "
424 "and an optional symbol file name"));
c906108c 425 }
d1a41061 426 argv = gdb_buildargv (args);
7a292a7a 427 cleanups = make_cleanup_freeargv (argv);
c906108c
SS
428
429 if (argv[0] != NULL)
430 {
431 filename = argv[0];
c378eb4e 432 /* If a second arg is supplied, it is a source file name to match on. */
c906108c
SS
433 if (argv[1] != NULL)
434 {
435 symname = argv[1];
436 }
437 }
438
439 filename = tilde_expand (filename);
b8c9b27d 440 make_cleanup (xfree, filename);
c5aa993b 441
c906108c
SS
442 outfile = gdb_fopen (filename, FOPEN_WT);
443 if (outfile == 0)
444 perror_with_name (filename);
d9fcf2fb 445 make_cleanup_ui_file_delete (outfile);
c906108c 446
43f3e411 447 ALL_FILETABS (objfile, cu, s)
27618ce4
TT
448 {
449 QUIT;
05cba821
JK
450 if (symname == NULL
451 || filename_cmp (symname, symtab_to_filename_for_display (s)) == 0)
27618ce4
TT
452 dump_symtab (objfile, s, outfile);
453 }
c906108c
SS
454 do_cleanups (cleanups);
455}
456
457/* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
458 far to indent. ARGS is really a struct print_symbol_args *, but is
459 declared as char * to get it past catch_errors. Returns 0 for error,
460 1 for success. */
461
462static int
4efb68b1 463print_symbol (void *args)
c906108c 464{
5af949e3 465 struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
c5aa993b
JM
466 struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
467 int depth = ((struct print_symbol_args *) args)->depth;
d9fcf2fb 468 struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
1994afbf
DE
469 struct obj_section *section;
470
471 if (SYMBOL_OBJFILE_OWNED (symbol))
472 section = SYMBOL_OBJ_SECTION (symbol_objfile (symbol), symbol);
473 else
474 section = NULL;
c906108c
SS
475
476 print_spaces (depth, outfile);
176620f1 477 if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
c906108c 478 {
de5ad195 479 fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
5af949e3
UW
480 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
481 outfile);
714835d5 482 if (section)
c906108c 483 fprintf_filtered (outfile, " section %s\n",
714835d5
UW
484 bfd_section_name (section->the_bfd_section->owner,
485 section->the_bfd_section));
c906108c
SS
486 else
487 fprintf_filtered (outfile, "\n");
488 return 1;
489 }
176620f1 490 if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
c906108c
SS
491 {
492 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
493 {
79d43c61
TT
494 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
495 &type_print_raw_options);
c906108c
SS
496 }
497 else
498 {
499 fprintf_filtered (outfile, "%s %s = ",
c5aa993b
JM
500 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
501 ? "enum"
502 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
503 ? "struct" : "union")),
3567439c 504 SYMBOL_LINKAGE_NAME (symbol));
79d43c61
TT
505 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
506 &type_print_raw_options);
c906108c
SS
507 }
508 fprintf_filtered (outfile, ";\n");
509 }
510 else
511 {
512 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
513 fprintf_filtered (outfile, "typedef ");
514 if (SYMBOL_TYPE (symbol))
515 {
516 /* Print details of types, except for enums where it's clutter. */
de5ad195 517 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
c906108c
SS
518 outfile,
519 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
79d43c61
TT
520 depth,
521 &type_print_raw_options);
c906108c
SS
522 fprintf_filtered (outfile, "; ");
523 }
524 else
de5ad195 525 fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
c906108c
SS
526
527 switch (SYMBOL_CLASS (symbol))
528 {
529 case LOC_CONST:
12df843f
JK
530 fprintf_filtered (outfile, "const %s (%s)",
531 plongest (SYMBOL_VALUE (symbol)),
532 hex_string (SYMBOL_VALUE (symbol)));
c906108c
SS
533 break;
534
535 case LOC_CONST_BYTES:
536 {
537 unsigned i;
538 struct type *type = check_typedef (SYMBOL_TYPE (symbol));
433759f7 539
c906108c
SS
540 fprintf_filtered (outfile, "const %u hex bytes:",
541 TYPE_LENGTH (type));
542 for (i = 0; i < TYPE_LENGTH (type); i++)
543 fprintf_filtered (outfile, " %02x",
c5aa993b 544 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
c906108c
SS
545 }
546 break;
547
548 case LOC_STATIC:
549 fprintf_filtered (outfile, "static at ");
5af949e3
UW
550 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
551 outfile);
714835d5 552 if (section)
c906108c 553 fprintf_filtered (outfile, " section %s",
714835d5
UW
554 bfd_section_name (section->the_bfd_section->owner,
555 section->the_bfd_section));
c906108c
SS
556 break;
557
c906108c 558 case LOC_REGISTER:
2a2d4dc3 559 if (SYMBOL_IS_ARGUMENT (symbol))
12df843f
JK
560 fprintf_filtered (outfile, "parameter register %s",
561 plongest (SYMBOL_VALUE (symbol)));
2a2d4dc3 562 else
12df843f
JK
563 fprintf_filtered (outfile, "register %s",
564 plongest (SYMBOL_VALUE (symbol)));
c906108c
SS
565 break;
566
567 case LOC_ARG:
12df843f
JK
568 fprintf_filtered (outfile, "arg at offset %s",
569 hex_string (SYMBOL_VALUE (symbol)));
c906108c
SS
570 break;
571
c906108c 572 case LOC_REF_ARG:
12df843f
JK
573 fprintf_filtered (outfile, "reference arg at %s",
574 hex_string (SYMBOL_VALUE (symbol)));
c906108c
SS
575 break;
576
c906108c 577 case LOC_REGPARM_ADDR:
12df843f
JK
578 fprintf_filtered (outfile, "address parameter register %s",
579 plongest (SYMBOL_VALUE (symbol)));
c906108c
SS
580 break;
581
582 case LOC_LOCAL:
12df843f
JK
583 fprintf_filtered (outfile, "local at offset %s",
584 hex_string (SYMBOL_VALUE (symbol)));
c906108c
SS
585 break;
586
c906108c
SS
587 case LOC_TYPEDEF:
588 break;
589
590 case LOC_LABEL:
591 fprintf_filtered (outfile, "label at ");
5af949e3
UW
592 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
593 outfile);
714835d5 594 if (section)
c906108c 595 fprintf_filtered (outfile, " section %s",
714835d5
UW
596 bfd_section_name (section->the_bfd_section->owner,
597 section->the_bfd_section));
c906108c
SS
598 break;
599
600 case LOC_BLOCK:
601 fprintf_filtered (outfile, "block object ");
d4f3574e 602 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
c906108c 603 fprintf_filtered (outfile, ", ");
5af949e3
UW
604 fputs_filtered (paddress (gdbarch,
605 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
ed49a04f 606 outfile);
c906108c 607 fprintf_filtered (outfile, "..");
5af949e3
UW
608 fputs_filtered (paddress (gdbarch,
609 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
ed49a04f 610 outfile);
714835d5 611 if (section)
c906108c 612 fprintf_filtered (outfile, " section %s",
714835d5
UW
613 bfd_section_name (section->the_bfd_section->owner,
614 section->the_bfd_section));
c906108c
SS
615 break;
616
4c2df51b 617 case LOC_COMPUTED:
4c2df51b
DJ
618 fprintf_filtered (outfile, "computed at runtime");
619 break;
620
c906108c
SS
621 case LOC_UNRESOLVED:
622 fprintf_filtered (outfile, "unresolved");
623 break;
624
625 case LOC_OPTIMIZED_OUT:
626 fprintf_filtered (outfile, "optimized out");
627 break;
628
c5aa993b 629 default:
c906108c
SS
630 fprintf_filtered (outfile, "botched symbol class %x",
631 SYMBOL_CLASS (symbol));
632 break;
633 }
634 }
635 fprintf_filtered (outfile, "\n");
636 return 1;
637}
638
80480540 639static void
fba45db2 640maintenance_print_msymbols (char *args, int from_tty)
c906108c
SS
641{
642 char **argv;
d9fcf2fb 643 struct ui_file *outfile;
c906108c
SS
644 struct cleanup *cleanups;
645 char *filename = DEV_TTY;
646 char *symname = NULL;
6c95b8df 647 struct program_space *pspace;
c906108c
SS
648 struct objfile *objfile;
649
07318b29
CV
650 struct stat sym_st, obj_st;
651
c906108c
SS
652 dont_repeat ();
653
654 if (args == NULL)
655 {
3e43a32a
MS
656 error (_("print-msymbols takes an output file "
657 "name and optional symbol file name"));
c906108c 658 }
d1a41061 659 argv = gdb_buildargv (args);
7a292a7a 660 cleanups = make_cleanup_freeargv (argv);
c906108c
SS
661
662 if (argv[0] != NULL)
663 {
664 filename = argv[0];
c378eb4e 665 /* If a second arg is supplied, it is a source file name to match on. */
c906108c
SS
666 if (argv[1] != NULL)
667 {
f5b95b50 668 symname = gdb_realpath (argv[1]);
07318b29
CV
669 make_cleanup (xfree, symname);
670 if (symname && stat (symname, &sym_st))
671 perror_with_name (symname);
c906108c
SS
672 }
673 }
674
675 filename = tilde_expand (filename);
b8c9b27d 676 make_cleanup (xfree, filename);
c5aa993b 677
c906108c
SS
678 outfile = gdb_fopen (filename, FOPEN_WT);
679 if (outfile == 0)
680 perror_with_name (filename);
d9fcf2fb 681 make_cleanup_ui_file_delete (outfile);
c906108c 682
6c95b8df
PA
683 ALL_PSPACES (pspace)
684 ALL_PSPACE_OBJFILES (pspace, objfile)
27618ce4
TT
685 {
686 QUIT;
4262abfb 687 if (symname == NULL || (!stat (objfile_name (objfile), &obj_st)
3b74cdc3 688 && sym_st.st_dev == obj_st.st_dev
27618ce4
TT
689 && sym_st.st_ino == obj_st.st_ino))
690 dump_msymbols (objfile, outfile);
691 }
c906108c
SS
692 fprintf_filtered (outfile, "\n\n");
693 do_cleanups (cleanups);
694}
695
80480540 696static void
52e260a3 697maintenance_print_objfiles (char *regexp, int from_tty)
c906108c 698{
6c95b8df 699 struct program_space *pspace;
c906108c
SS
700 struct objfile *objfile;
701
702 dont_repeat ();
703
52e260a3
DE
704 if (regexp)
705 re_comp (regexp);
706
6c95b8df
PA
707 ALL_PSPACES (pspace)
708 ALL_PSPACE_OBJFILES (pspace, objfile)
27618ce4
TT
709 {
710 QUIT;
52e260a3 711 if (! regexp
4262abfb 712 || re_exec (objfile_name (objfile)))
52e260a3 713 dump_objfile (objfile);
27618ce4 714 }
c906108c
SS
715}
716
5e7b2f39 717/* List all the symbol tables whose names match REGEXP (optional). */
b5ebcee6 718
80480540 719static void
5e7b2f39 720maintenance_info_symtabs (char *regexp, int from_tty)
44ea7b70 721{
6c95b8df 722 struct program_space *pspace;
44ea7b70
JB
723 struct objfile *objfile;
724
db68bbae
DE
725 dont_repeat ();
726
44ea7b70
JB
727 if (regexp)
728 re_comp (regexp);
729
6c95b8df
PA
730 ALL_PSPACES (pspace)
731 ALL_PSPACE_OBJFILES (pspace, objfile)
44ea7b70 732 {
43f3e411 733 struct compunit_symtab *cust;
44ea7b70 734 struct symtab *symtab;
26a84859 735
44ea7b70
JB
736 /* We don't want to print anything for this objfile until we
737 actually find a symtab whose name matches. */
738 int printed_objfile_start = 0;
739
43f3e411 740 ALL_OBJFILE_COMPUNITS (objfile, cust)
8a498d38 741 {
43f3e411 742 int printed_compunit_symtab_start = 0;
8a498d38 743
43f3e411 744 ALL_COMPUNIT_FILETABS (cust, symtab)
8a498d38 745 {
43f3e411
DE
746 QUIT;
747
748 if (! regexp
749 || re_exec (symtab_to_filename_for_display (symtab)))
8a498d38 750 {
43f3e411
DE
751 if (! printed_objfile_start)
752 {
753 printf_filtered ("{ objfile %s ", objfile_name (objfile));
754 wrap_here (" ");
755 printf_filtered ("((struct objfile *) %s)\n",
756 host_address_to_string (objfile));
757 printed_objfile_start = 1;
758 }
759 if (! printed_compunit_symtab_start)
760 {
761 printf_filtered (" { ((struct compunit_symtab *) %s)\n",
762 host_address_to_string (cust));
763 printf_filtered (" debugformat %s\n",
764 COMPUNIT_DEBUGFORMAT (cust));
765 printf_filtered (" producer %s\n",
766 COMPUNIT_PRODUCER (cust) != NULL
767 ? COMPUNIT_PRODUCER (cust)
768 : "(null)");
769 printf_filtered (" dirname %s\n",
770 COMPUNIT_DIRNAME (cust) != NULL
771 ? COMPUNIT_DIRNAME (cust)
772 : "(null)");
773 printf_filtered (" blockvector"
774 " ((struct blockvector *) %s)\n",
775 host_address_to_string
776 (COMPUNIT_BLOCKVECTOR (cust)));
777 printed_compunit_symtab_start = 1;
778 }
779
780 printf_filtered ("\t{ symtab %s ",
781 symtab_to_filename_for_display (symtab));
782 wrap_here (" ");
783 printf_filtered ("((struct symtab *) %s)\n",
784 host_address_to_string (symtab));
785 printf_filtered ("\t fullname %s\n",
786 symtab->fullname != NULL
787 ? symtab->fullname
788 : "(null)");
789 printf_filtered ("\t "
790 "linetable ((struct linetable *) %s)\n",
791 host_address_to_string (symtab->linetable));
792 printf_filtered ("\t}\n");
8a498d38 793 }
8a498d38 794 }
43f3e411
DE
795
796 if (printed_compunit_symtab_start)
797 printf_filtered (" }\n");
8a498d38 798 }
44ea7b70
JB
799
800 if (printed_objfile_start)
801 printf_filtered ("}\n");
802 }
803}
7d0c9981
DE
804
805/* Check consistency of symtabs.
806 An example of what this checks for is NULL blockvectors.
807 They can happen if there's a bug during debug info reading.
808 GDB assumes they are always non-NULL.
809
810 Note: This does not check for psymtab vs symtab consistency.
811 Use "maint check-psymtabs" for that. */
812
813static void
814maintenance_check_symtabs (char *ignore, int from_tty)
815{
816 struct program_space *pspace;
817 struct objfile *objfile;
818
819 ALL_PSPACES (pspace)
820 ALL_PSPACE_OBJFILES (pspace, objfile)
821 {
43f3e411 822 struct compunit_symtab *cust;
7d0c9981
DE
823
824 /* We don't want to print anything for this objfile until we
825 actually find something worth printing. */
826 int printed_objfile_start = 0;
827
43f3e411 828 ALL_OBJFILE_COMPUNITS (objfile, cust)
7d0c9981
DE
829 {
830 int found_something = 0;
43f3e411 831 struct symtab *symtab = compunit_primary_filetab (cust);
7d0c9981
DE
832
833 QUIT;
834
43f3e411 835 if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
7d0c9981
DE
836 found_something = 1;
837 /* Add more checks here. */
838
839 if (found_something)
840 {
841 if (! printed_objfile_start)
842 {
4262abfb 843 printf_filtered ("{ objfile %s ", objfile_name (objfile));
7d0c9981 844 wrap_here (" ");
26a84859 845 printf_filtered ("((struct objfile *) %s)\n",
7d0c9981
DE
846 host_address_to_string (objfile));
847 printed_objfile_start = 1;
848 }
849 printf_filtered (" { symtab %s\n",
850 symtab_to_filename_for_display (symtab));
43f3e411 851 if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
7d0c9981
DE
852 printf_filtered (" NULL blockvector\n");
853 printf_filtered (" }\n");
854 }
855 }
856
857 if (printed_objfile_start)
858 printf_filtered ("}\n");
859 }
860}
861
862/* Helper function for maintenance_expand_symtabs.
863 This is the name_matcher function for expand_symtabs_matching. */
864
865static int
866maintenance_expand_name_matcher (const char *symname, void *data)
867{
868 /* Since we're not searching on symbols, just return TRUE. */
869 return 1;
870}
871
872/* Helper function for maintenance_expand_symtabs.
873 This is the file_matcher function for expand_symtabs_matching. */
874
875static int
876maintenance_expand_file_matcher (const char *filename, void *data,
877 int basenames)
878{
879 const char *regexp = data;
880
881 QUIT;
882
883 /* KISS: Only apply the regexp to the complete file name. */
884 if (basenames)
885 return 0;
886
887 if (regexp == NULL || re_exec (filename))
888 return 1;
889
890 return 0;
891}
892
893/* Expand all symbol tables whose name matches an optional regexp. */
894
895static void
896maintenance_expand_symtabs (char *args, int from_tty)
897{
898 struct program_space *pspace;
899 struct objfile *objfile;
900 struct cleanup *cleanups;
901 char **argv;
902 char *regexp = NULL;
903
904 /* We use buildargv here so that we handle spaces in the regexp
905 in a way that allows adding more arguments later. */
906 argv = gdb_buildargv (args);
907 cleanups = make_cleanup_freeargv (argv);
908
909 if (argv != NULL)
910 {
911 if (argv[0] != NULL)
912 {
913 regexp = argv[0];
914 if (argv[1] != NULL)
915 error (_("Extra arguments after regexp."));
916 }
917 }
918
919 if (regexp)
920 re_comp (regexp);
921
922 ALL_PSPACES (pspace)
923 ALL_PSPACE_OBJFILES (pspace, objfile)
924 {
925 if (objfile->sf)
926 {
927 objfile->sf->qf->expand_symtabs_matching
928 (objfile, maintenance_expand_file_matcher,
276d885b 929 maintenance_expand_name_matcher, NULL, ALL_DOMAIN, regexp);
7d0c9981
DE
930 }
931 }
4e993a19
TT
932
933 do_cleanups (cleanups);
7d0c9981 934}
c906108c 935\f
c5aa993b 936
c906108c
SS
937/* Return the nexting depth of a block within other blocks in its symtab. */
938
939static int
fba45db2 940block_depth (struct block *block)
c906108c 941{
52f0bd74 942 int i = 0;
433759f7 943
c5aa993b 944 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
c906108c
SS
945 {
946 i++;
947 }
948 return i;
949}
c906108c 950\f
c5aa993b 951
c378eb4e 952/* Do early runtime initializations. */
b5ebcee6 953
c906108c 954void
fba45db2 955_initialize_symmisc (void)
c906108c 956{
c5aa993b 957 std_in = stdin;
c906108c
SS
958 std_out = stdout;
959 std_err = stderr;
80480540
YQ
960
961 add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
962Print dump of current symbol definitions.\n\
963Entries in the full symbol table are dumped to file OUTFILE.\n\
964If a SOURCE file is specified, dump only that file's symbols."),
965 &maintenanceprintlist);
966
967 add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
968Print dump of current minimal symbol definitions.\n\
969Entries in the minimal symbol table are dumped to file OUTFILE.\n\
970If a SOURCE file is specified, dump only that file's minimal symbols."),
971 &maintenanceprintlist);
972
973 add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
52e260a3
DE
974 _("Print dump of current object file definitions.\n\
975With an argument REGEXP, list the object files with matching names."),
80480540
YQ
976 &maintenanceprintlist);
977
978 add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
979List the full symbol tables for all object files.\n\
980This does not include information about individual symbols, blocks, or\n\
981linetables --- just the symbol table structures themselves.\n\
db68bbae 982With an argument REGEXP, list the symbol tables with matching names."),
80480540 983 &maintenanceinfolist);
7d0c9981
DE
984
985 add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
986 _("\
987Check consistency of currently expanded symtabs."),
988 &maintenancelist);
989
990 add_cmd ("expand-symtabs", class_maintenance, maintenance_expand_symtabs,
991 _("Expand symbol tables.\n\
992With an argument REGEXP, only expand the symbol tables with matching names."),
993 &maintenancelist);
c906108c 994}
This page took 1.809994 seconds and 4 git commands to generate.