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