symfile-debug.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /* Debug logging for the symbol file functions for the GNU debugger, GDB.
  2. Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Support, using pieces from other GDB modules.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* Note: Be careful with functions that can throw errors.
  16. We want to see a logging message regardless of whether an error was thrown.
  17. This typically means printing a message before calling the real function
  18. and then if the function returns a result printing a message after it
  19. returns. */
  20. #include "defs.h"
  21. #include "gdbcmd.h"
  22. #include "objfiles.h"
  23. #include "observable.h"
  24. #include "source.h"
  25. #include "symtab.h"
  26. #include "symfile.h"
  27. #include "block.h"
  28. #include "filenames.h"
  29. /* We need to save a pointer to the real symbol functions.
  30. Plus, the debug versions are malloc'd because we have to NULL out the
  31. ones that are NULL in the real copy. */
  32. struct debug_sym_fns_data
  33. {
  34. const struct sym_fns *real_sf = nullptr;
  35. struct sym_fns debug_sf {};
  36. };
  37. /* We need to record a pointer to the real set of functions for each
  38. objfile. */
  39. static const struct objfile_key<debug_sym_fns_data>
  40. symfile_debug_objfile_data_key;
  41. /* If true all calls to the symfile functions are logged. */
  42. static bool debug_symfile = false;
  43. /* Return non-zero if symfile debug logging is installed. */
  44. static int
  45. symfile_debug_installed (struct objfile *objfile)
  46. {
  47. return (objfile->sf != NULL
  48. && symfile_debug_objfile_data_key.get (objfile) != NULL);
  49. }
  50. /* Utility return the name to print for SYMTAB. */
  51. static const char *
  52. debug_symtab_name (struct symtab *symtab)
  53. {
  54. return symtab_to_filename_for_display (symtab);
  55. }
  56. /* See objfiles.h. */
  57. bool
  58. objfile::has_partial_symbols ()
  59. {
  60. bool retval = false;
  61. /* If we have not read psymbols, but we have a function capable of reading
  62. them, then that is an indication that they are in fact available. Without
  63. this function the symbols may have been already read in but they also may
  64. not be present in this objfile. */
  65. for (const auto &iter : qf)
  66. {
  67. if ((flags & OBJF_PSYMTABS_READ) == 0
  68. && iter->can_lazily_read_symbols ())
  69. retval = true;
  70. else
  71. retval = iter->has_symbols (this);
  72. if (retval)
  73. break;
  74. }
  75. if (debug_symfile)
  76. gdb_printf (gdb_stdlog, "qf->has_symbols (%s) = %d\n",
  77. objfile_debug_name (this), retval);
  78. return retval;
  79. }
  80. /* See objfiles.h. */
  81. bool
  82. objfile::has_unexpanded_symtabs ()
  83. {
  84. if (debug_symfile)
  85. gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s)\n",
  86. objfile_debug_name (this));
  87. bool result = false;
  88. for (const auto &iter : qf)
  89. {
  90. if (iter->has_unexpanded_symtabs (this))
  91. {
  92. result = true;
  93. break;
  94. }
  95. }
  96. if (debug_symfile)
  97. gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s) = %d\n",
  98. objfile_debug_name (this), (result ? 1 : 0));
  99. return result;
  100. }
  101. struct symtab *
  102. objfile::find_last_source_symtab ()
  103. {
  104. struct symtab *retval = nullptr;
  105. if (debug_symfile)
  106. gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (%s)\n",
  107. objfile_debug_name (this));
  108. for (const auto &iter : qf)
  109. {
  110. retval = iter->find_last_source_symtab (this);
  111. if (retval != nullptr)
  112. break;
  113. }
  114. if (debug_symfile)
  115. gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (...) = %s\n",
  116. retval ? debug_symtab_name (retval) : "NULL");
  117. return retval;
  118. }
  119. void
  120. objfile::forget_cached_source_info ()
  121. {
  122. if (debug_symfile)
  123. gdb_printf (gdb_stdlog, "qf->forget_cached_source_info (%s)\n",
  124. objfile_debug_name (this));
  125. for (const auto &iter : qf)
  126. iter->forget_cached_source_info (this);
  127. }
  128. bool
  129. objfile::map_symtabs_matching_filename
  130. (const char *name, const char *real_path,
  131. gdb::function_view<bool (symtab *)> callback)
  132. {
  133. if (debug_symfile)
  134. gdb_printf (gdb_stdlog,
  135. "qf->map_symtabs_matching_filename (%s, \"%s\", "
  136. "\"%s\", %s)\n",
  137. objfile_debug_name (this), name,
  138. real_path ? real_path : NULL,
  139. host_address_to_string (&callback));
  140. bool retval = true;
  141. const char *name_basename = lbasename (name);
  142. auto match_one_filename = [&] (const char *filename, bool basenames)
  143. {
  144. if (compare_filenames_for_search (filename, name))
  145. return true;
  146. if (basenames && FILENAME_CMP (name_basename, filename) == 0)
  147. return true;
  148. if (real_path != nullptr && IS_ABSOLUTE_PATH (filename)
  149. && IS_ABSOLUTE_PATH (real_path))
  150. return filename_cmp (filename, real_path) == 0;
  151. return false;
  152. };
  153. compunit_symtab *last_made = this->compunit_symtabs;
  154. auto on_expansion = [&] (compunit_symtab *symtab)
  155. {
  156. /* The callback to iterate_over_some_symtabs returns false to keep
  157. going and true to continue, so we have to invert the result
  158. here, for expand_symtabs_matching. */
  159. bool result = !iterate_over_some_symtabs (name, real_path,
  160. this->compunit_symtabs,
  161. last_made,
  162. callback);
  163. last_made = this->compunit_symtabs;
  164. return result;
  165. };
  166. for (const auto &iter : qf)
  167. {
  168. if (!iter->expand_symtabs_matching (this,
  169. match_one_filename,
  170. nullptr,
  171. nullptr,
  172. on_expansion,
  173. (SEARCH_GLOBAL_BLOCK
  174. | SEARCH_STATIC_BLOCK),
  175. UNDEF_DOMAIN,
  176. ALL_DOMAIN))
  177. {
  178. retval = false;
  179. break;
  180. }
  181. }
  182. if (debug_symfile)
  183. gdb_printf (gdb_stdlog,
  184. "qf->map_symtabs_matching_filename (...) = %d\n",
  185. retval);
  186. /* We must re-invert the return value here to match the caller's
  187. expectations. */
  188. return !retval;
  189. }
  190. struct compunit_symtab *
  191. objfile::lookup_symbol (block_enum kind, const char *name, domain_enum domain)
  192. {
  193. struct compunit_symtab *retval = nullptr;
  194. if (debug_symfile)
  195. gdb_printf (gdb_stdlog,
  196. "qf->lookup_symbol (%s, %d, \"%s\", %s)\n",
  197. objfile_debug_name (this), kind, name,
  198. domain_name (domain));
  199. lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
  200. auto search_one_symtab = [&] (compunit_symtab *stab)
  201. {
  202. struct symbol *sym, *with_opaque = NULL;
  203. const struct blockvector *bv = stab->blockvector ();
  204. const struct block *block = BLOCKVECTOR_BLOCK (bv, kind);
  205. sym = block_find_symbol (block, name, domain,
  206. block_find_non_opaque_type_preferred,
  207. &with_opaque);
  208. /* Some caution must be observed with overloaded functions
  209. and methods, since the index will not contain any overload
  210. information (but NAME might contain it). */
  211. if (sym != NULL
  212. && symbol_matches_search_name (sym, lookup_name))
  213. {
  214. retval = stab;
  215. /* Found it. */
  216. return false;
  217. }
  218. if (with_opaque != NULL
  219. && symbol_matches_search_name (with_opaque, lookup_name))
  220. retval = stab;
  221. /* Keep looking through other psymtabs. */
  222. return true;
  223. };
  224. for (const auto &iter : qf)
  225. {
  226. if (!iter->expand_symtabs_matching (this,
  227. nullptr,
  228. &lookup_name,
  229. nullptr,
  230. search_one_symtab,
  231. kind == GLOBAL_BLOCK
  232. ? SEARCH_GLOBAL_BLOCK
  233. : SEARCH_STATIC_BLOCK,
  234. domain,
  235. ALL_DOMAIN))
  236. break;
  237. }
  238. if (debug_symfile)
  239. gdb_printf (gdb_stdlog, "qf->lookup_symbol (...) = %s\n",
  240. retval
  241. ? debug_symtab_name (retval->primary_filetab ())
  242. : "NULL");
  243. return retval;
  244. }
  245. void
  246. objfile::print_stats (bool print_bcache)
  247. {
  248. if (debug_symfile)
  249. gdb_printf (gdb_stdlog, "qf->print_stats (%s, %d)\n",
  250. objfile_debug_name (this), print_bcache);
  251. for (const auto &iter : qf)
  252. iter->print_stats (this, print_bcache);
  253. }
  254. void
  255. objfile::dump ()
  256. {
  257. if (debug_symfile)
  258. gdb_printf (gdb_stdlog, "qf->dump (%s)\n",
  259. objfile_debug_name (this));
  260. for (const auto &iter : qf)
  261. iter->dump (this);
  262. }
  263. void
  264. objfile::expand_symtabs_for_function (const char *func_name)
  265. {
  266. if (debug_symfile)
  267. gdb_printf (gdb_stdlog,
  268. "qf->expand_symtabs_for_function (%s, \"%s\")\n",
  269. objfile_debug_name (this), func_name);
  270. lookup_name_info base_lookup (func_name, symbol_name_match_type::FULL);
  271. lookup_name_info lookup_name = base_lookup.make_ignore_params ();
  272. for (const auto &iter : qf)
  273. iter->expand_symtabs_matching (this,
  274. nullptr,
  275. &lookup_name,
  276. nullptr,
  277. nullptr,
  278. (SEARCH_GLOBAL_BLOCK
  279. | SEARCH_STATIC_BLOCK),
  280. VAR_DOMAIN,
  281. ALL_DOMAIN);
  282. }
  283. void
  284. objfile::expand_all_symtabs ()
  285. {
  286. if (debug_symfile)
  287. gdb_printf (gdb_stdlog, "qf->expand_all_symtabs (%s)\n",
  288. objfile_debug_name (this));
  289. for (const auto &iter : qf)
  290. iter->expand_all_symtabs (this);
  291. }
  292. void
  293. objfile::expand_symtabs_with_fullname (const char *fullname)
  294. {
  295. if (debug_symfile)
  296. gdb_printf (gdb_stdlog,
  297. "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
  298. objfile_debug_name (this), fullname);
  299. const char *basename = lbasename (fullname);
  300. auto file_matcher = [&] (const char *filename, bool basenames)
  301. {
  302. return filename_cmp (basenames ? basename : fullname, filename) == 0;
  303. };
  304. for (const auto &iter : qf)
  305. iter->expand_symtabs_matching (this,
  306. file_matcher,
  307. nullptr,
  308. nullptr,
  309. nullptr,
  310. (SEARCH_GLOBAL_BLOCK
  311. | SEARCH_STATIC_BLOCK),
  312. UNDEF_DOMAIN,
  313. ALL_DOMAIN);
  314. }
  315. void
  316. objfile::expand_matching_symbols
  317. (const lookup_name_info &name, domain_enum domain,
  318. int global,
  319. symbol_compare_ftype *ordered_compare)
  320. {
  321. if (debug_symfile)
  322. gdb_printf (gdb_stdlog,
  323. "qf->expand_matching_symbols (%s, %s, %d, %s)\n",
  324. objfile_debug_name (this),
  325. domain_name (domain), global,
  326. host_address_to_string (ordered_compare));
  327. for (const auto &iter : qf)
  328. iter->expand_matching_symbols (this, name, domain, global,
  329. ordered_compare);
  330. }
  331. bool
  332. objfile::expand_symtabs_matching
  333. (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
  334. const lookup_name_info *lookup_name,
  335. gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
  336. gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
  337. block_search_flags search_flags,
  338. domain_enum domain,
  339. enum search_domain kind)
  340. {
  341. /* This invariant is documented in quick-functions.h. */
  342. gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
  343. if (debug_symfile)
  344. gdb_printf (gdb_stdlog,
  345. "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
  346. objfile_debug_name (this),
  347. host_address_to_string (&file_matcher),
  348. host_address_to_string (&symbol_matcher),
  349. host_address_to_string (&expansion_notify),
  350. search_domain_name (kind));
  351. for (const auto &iter : qf)
  352. if (!iter->expand_symtabs_matching (this, file_matcher, lookup_name,
  353. symbol_matcher, expansion_notify,
  354. search_flags, domain, kind))
  355. return false;
  356. return true;
  357. }
  358. struct compunit_symtab *
  359. objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol,
  360. CORE_ADDR pc,
  361. struct obj_section *section,
  362. int warn_if_readin)
  363. {
  364. struct compunit_symtab *retval = nullptr;
  365. if (debug_symfile)
  366. gdb_printf (gdb_stdlog,
  367. "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
  368. objfile_debug_name (this),
  369. host_address_to_string (msymbol.minsym),
  370. hex_string (pc),
  371. host_address_to_string (section),
  372. warn_if_readin);
  373. for (const auto &iter : qf)
  374. {
  375. retval = iter->find_pc_sect_compunit_symtab (this, msymbol, pc, section,
  376. warn_if_readin);
  377. if (retval != nullptr)
  378. break;
  379. }
  380. if (debug_symfile)
  381. gdb_printf (gdb_stdlog,
  382. "qf->find_pc_sect_compunit_symtab (...) = %s\n",
  383. retval
  384. ? debug_symtab_name (retval->primary_filetab ())
  385. : "NULL");
  386. return retval;
  387. }
  388. void
  389. objfile::map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun,
  390. bool need_fullname)
  391. {
  392. if (debug_symfile)
  393. gdb_printf (gdb_stdlog,
  394. "qf->map_symbol_filenames (%s, ..., %d)\n",
  395. objfile_debug_name (this),
  396. need_fullname);
  397. for (const auto &iter : qf)
  398. iter->map_symbol_filenames (this, fun, need_fullname);
  399. }
  400. struct compunit_symtab *
  401. objfile::find_compunit_symtab_by_address (CORE_ADDR address)
  402. {
  403. if (debug_symfile)
  404. gdb_printf (gdb_stdlog,
  405. "qf->find_compunit_symtab_by_address (%s, %s)\n",
  406. objfile_debug_name (this),
  407. hex_string (address));
  408. struct compunit_symtab *result = NULL;
  409. for (const auto &iter : qf)
  410. {
  411. result = iter->find_compunit_symtab_by_address (this, address);
  412. if (result != nullptr)
  413. break;
  414. }
  415. if (debug_symfile)
  416. gdb_printf (gdb_stdlog,
  417. "qf->find_compunit_symtab_by_address (...) = %s\n",
  418. result
  419. ? debug_symtab_name (result->primary_filetab ())
  420. : "NULL");
  421. return result;
  422. }
  423. enum language
  424. objfile::lookup_global_symbol_language (const char *name,
  425. domain_enum domain,
  426. bool *symbol_found_p)
  427. {
  428. enum language result = language_unknown;
  429. *symbol_found_p = false;
  430. for (const auto &iter : qf)
  431. {
  432. result = iter->lookup_global_symbol_language (this, name, domain,
  433. symbol_found_p);
  434. if (*symbol_found_p)
  435. break;
  436. }
  437. return result;
  438. }
  439. void
  440. objfile::require_partial_symbols (bool verbose)
  441. {
  442. if ((flags & OBJF_PSYMTABS_READ) == 0)
  443. {
  444. flags |= OBJF_PSYMTABS_READ;
  445. bool printed = false;
  446. for (const auto &iter : qf)
  447. {
  448. if (iter->can_lazily_read_symbols ())
  449. {
  450. if (verbose && !printed)
  451. {
  452. gdb_printf (_("Reading symbols from %s...\n"),
  453. objfile_name (this));
  454. printed = true;
  455. }
  456. iter->read_partial_symbols (this);
  457. }
  458. }
  459. if (printed && !objfile_has_symbols (this))
  460. gdb_printf (_("(No debugging symbols found in %s)\n"),
  461. objfile_name (this));
  462. }
  463. }
  464. /* Debugging version of struct sym_probe_fns. */
  465. static const std::vector<std::unique_ptr<probe>> &
  466. debug_sym_get_probes (struct objfile *objfile)
  467. {
  468. const struct debug_sym_fns_data *debug_data
  469. = symfile_debug_objfile_data_key.get (objfile);
  470. const std::vector<std::unique_ptr<probe>> &retval
  471. = debug_data->real_sf->sym_probe_fns->sym_get_probes (objfile);
  472. gdb_printf (gdb_stdlog,
  473. "probes->sym_get_probes (%s) = %s\n",
  474. objfile_debug_name (objfile),
  475. host_address_to_string (retval.data ()));
  476. return retval;
  477. }
  478. static const struct sym_probe_fns debug_sym_probe_fns =
  479. {
  480. debug_sym_get_probes,
  481. };
  482. /* Debugging version of struct sym_fns. */
  483. static void
  484. debug_sym_new_init (struct objfile *objfile)
  485. {
  486. const struct debug_sym_fns_data *debug_data
  487. = symfile_debug_objfile_data_key.get (objfile);
  488. gdb_printf (gdb_stdlog, "sf->sym_new_init (%s)\n",
  489. objfile_debug_name (objfile));
  490. debug_data->real_sf->sym_new_init (objfile);
  491. }
  492. static void
  493. debug_sym_init (struct objfile *objfile)
  494. {
  495. const struct debug_sym_fns_data *debug_data
  496. = symfile_debug_objfile_data_key.get (objfile);
  497. gdb_printf (gdb_stdlog, "sf->sym_init (%s)\n",
  498. objfile_debug_name (objfile));
  499. debug_data->real_sf->sym_init (objfile);
  500. }
  501. static void
  502. debug_sym_read (struct objfile *objfile, symfile_add_flags symfile_flags)
  503. {
  504. const struct debug_sym_fns_data *debug_data
  505. = symfile_debug_objfile_data_key.get (objfile);
  506. gdb_printf (gdb_stdlog, "sf->sym_read (%s, 0x%x)\n",
  507. objfile_debug_name (objfile), (unsigned) symfile_flags);
  508. debug_data->real_sf->sym_read (objfile, symfile_flags);
  509. }
  510. static void
  511. debug_sym_finish (struct objfile *objfile)
  512. {
  513. const struct debug_sym_fns_data *debug_data
  514. = symfile_debug_objfile_data_key.get (objfile);
  515. gdb_printf (gdb_stdlog, "sf->sym_finish (%s)\n",
  516. objfile_debug_name (objfile));
  517. debug_data->real_sf->sym_finish (objfile);
  518. }
  519. static void
  520. debug_sym_offsets (struct objfile *objfile,
  521. const section_addr_info &info)
  522. {
  523. const struct debug_sym_fns_data *debug_data
  524. = symfile_debug_objfile_data_key.get (objfile);
  525. gdb_printf (gdb_stdlog, "sf->sym_offsets (%s, %s)\n",
  526. objfile_debug_name (objfile),
  527. host_address_to_string (&info));
  528. debug_data->real_sf->sym_offsets (objfile, info);
  529. }
  530. static symfile_segment_data_up
  531. debug_sym_segments (bfd *abfd)
  532. {
  533. /* This API function is annoying, it doesn't take a "this" pointer.
  534. Fortunately it is only used in one place where we (re-)lookup the
  535. sym_fns table to use. Thus we will never be called. */
  536. gdb_assert_not_reached ("debug_sym_segments called");
  537. }
  538. static void
  539. debug_sym_read_linetable (struct objfile *objfile)
  540. {
  541. const struct debug_sym_fns_data *debug_data
  542. = symfile_debug_objfile_data_key.get (objfile);
  543. gdb_printf (gdb_stdlog, "sf->sym_read_linetable (%s)\n",
  544. objfile_debug_name (objfile));
  545. debug_data->real_sf->sym_read_linetable (objfile);
  546. }
  547. static bfd_byte *
  548. debug_sym_relocate (struct objfile *objfile, asection *sectp, bfd_byte *buf)
  549. {
  550. const struct debug_sym_fns_data *debug_data
  551. = symfile_debug_objfile_data_key.get (objfile);
  552. bfd_byte *retval;
  553. retval = debug_data->real_sf->sym_relocate (objfile, sectp, buf);
  554. gdb_printf (gdb_stdlog,
  555. "sf->sym_relocate (%s, %s, %s) = %s\n",
  556. objfile_debug_name (objfile),
  557. host_address_to_string (sectp),
  558. host_address_to_string (buf),
  559. host_address_to_string (retval));
  560. return retval;
  561. }
  562. /* Template of debugging version of struct sym_fns.
  563. A copy is made, with sym_flavour updated, and a pointer to the real table
  564. installed in real_sf, and then a pointer to the copy is installed in the
  565. objfile. */
  566. static const struct sym_fns debug_sym_fns =
  567. {
  568. debug_sym_new_init,
  569. debug_sym_init,
  570. debug_sym_read,
  571. debug_sym_finish,
  572. debug_sym_offsets,
  573. debug_sym_segments,
  574. debug_sym_read_linetable,
  575. debug_sym_relocate,
  576. &debug_sym_probe_fns,
  577. };
  578. /* Install the debugging versions of the symfile functions for OBJFILE.
  579. Do not call this if the debug versions are already installed. */
  580. static void
  581. install_symfile_debug_logging (struct objfile *objfile)
  582. {
  583. const struct sym_fns *real_sf;
  584. struct debug_sym_fns_data *debug_data;
  585. /* The debug versions should not already be installed. */
  586. gdb_assert (!symfile_debug_installed (objfile));
  587. real_sf = objfile->sf;
  588. /* Alas we have to preserve NULL entries in REAL_SF. */
  589. debug_data = new struct debug_sym_fns_data;
  590. #define COPY_SF_PTR(from, to, name, func) \
  591. do { \
  592. if ((from)->name) \
  593. (to)->debug_sf.name = func; \
  594. } while (0)
  595. COPY_SF_PTR (real_sf, debug_data, sym_new_init, debug_sym_new_init);
  596. COPY_SF_PTR (real_sf, debug_data, sym_init, debug_sym_init);
  597. COPY_SF_PTR (real_sf, debug_data, sym_read, debug_sym_read);
  598. COPY_SF_PTR (real_sf, debug_data, sym_finish, debug_sym_finish);
  599. COPY_SF_PTR (real_sf, debug_data, sym_offsets, debug_sym_offsets);
  600. COPY_SF_PTR (real_sf, debug_data, sym_segments, debug_sym_segments);
  601. COPY_SF_PTR (real_sf, debug_data, sym_read_linetable,
  602. debug_sym_read_linetable);
  603. COPY_SF_PTR (real_sf, debug_data, sym_relocate, debug_sym_relocate);
  604. if (real_sf->sym_probe_fns)
  605. debug_data->debug_sf.sym_probe_fns = &debug_sym_probe_fns;
  606. #undef COPY_SF_PTR
  607. debug_data->real_sf = real_sf;
  608. symfile_debug_objfile_data_key.set (objfile, debug_data);
  609. objfile->sf = &debug_data->debug_sf;
  610. }
  611. /* Uninstall the debugging versions of the symfile functions for OBJFILE.
  612. Do not call this if the debug versions are not installed. */
  613. static void
  614. uninstall_symfile_debug_logging (struct objfile *objfile)
  615. {
  616. struct debug_sym_fns_data *debug_data;
  617. /* The debug versions should be currently installed. */
  618. gdb_assert (symfile_debug_installed (objfile));
  619. debug_data = symfile_debug_objfile_data_key.get (objfile);
  620. objfile->sf = debug_data->real_sf;
  621. symfile_debug_objfile_data_key.clear (objfile);
  622. }
  623. /* Call this function to set OBJFILE->SF.
  624. Do not set OBJFILE->SF directly. */
  625. void
  626. objfile_set_sym_fns (struct objfile *objfile, const struct sym_fns *sf)
  627. {
  628. if (symfile_debug_installed (objfile))
  629. {
  630. gdb_assert (debug_symfile);
  631. /* Remove the current one, and reinstall a new one later. */
  632. uninstall_symfile_debug_logging (objfile);
  633. }
  634. /* Assume debug logging is disabled. */
  635. objfile->sf = sf;
  636. /* Turn debug logging on if enabled. */
  637. if (debug_symfile)
  638. install_symfile_debug_logging (objfile);
  639. }
  640. static void
  641. set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c)
  642. {
  643. for (struct program_space *pspace : program_spaces)
  644. for (objfile *objfile : pspace->objfiles ())
  645. {
  646. if (debug_symfile)
  647. {
  648. if (!symfile_debug_installed (objfile))
  649. install_symfile_debug_logging (objfile);
  650. }
  651. else
  652. {
  653. if (symfile_debug_installed (objfile))
  654. uninstall_symfile_debug_logging (objfile);
  655. }
  656. }
  657. }
  658. static void
  659. show_debug_symfile (struct ui_file *file, int from_tty,
  660. struct cmd_list_element *c, const char *value)
  661. {
  662. gdb_printf (file, _("Symfile debugging is %s.\n"), value);
  663. }
  664. void _initialize_symfile_debug ();
  665. void
  666. _initialize_symfile_debug ()
  667. {
  668. add_setshow_boolean_cmd ("symfile", no_class, &debug_symfile, _("\
  669. Set debugging of the symfile functions."), _("\
  670. Show debugging of the symfile functions."), _("\
  671. When enabled, all calls to the symfile functions are logged."),
  672. set_debug_symfile, show_debug_symfile,
  673. &setdebuglist, &showdebuglist);
  674. /* Note: We don't need a new-objfile observer because debug logging
  675. will be installed when objfile init'n calls objfile_set_sym_fns. */
  676. }