symmisc.c 30 KB

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