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