progspace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* Program and address space management, for GDB, the GNU debugger.
  2. Copyright (C) 2009-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 "gdbcmd.h"
  16. #include "objfiles.h"
  17. #include "arch-utils.h"
  18. #include "gdbcore.h"
  19. #include "solib.h"
  20. #include "solist.h"
  21. #include "gdbthread.h"
  22. #include "inferior.h"
  23. #include <algorithm>
  24. #include "cli/cli-style.h"
  25. /* The last program space number assigned. */
  26. static int last_program_space_num = 0;
  27. /* The head of the program spaces list. */
  28. std::vector<struct program_space *> program_spaces;
  29. /* Pointer to the current program space. */
  30. struct program_space *current_program_space;
  31. /* The last address space number assigned. */
  32. static int highest_address_space_num;
  33. /* Keep a registry of per-program_space data-pointers required by other GDB
  34. modules. */
  35. DEFINE_REGISTRY (program_space, REGISTRY_ACCESS_FIELD)
  36. /* Keep a registry of per-address_space data-pointers required by other GDB
  37. modules. */
  38. DEFINE_REGISTRY (address_space, REGISTRY_ACCESS_FIELD)
  39. /* Create a new address space object, and add it to the list. */
  40. struct address_space *
  41. new_address_space (void)
  42. {
  43. struct address_space *aspace;
  44. aspace = XCNEW (struct address_space);
  45. aspace->num = ++highest_address_space_num;
  46. address_space_alloc_data (aspace);
  47. return aspace;
  48. }
  49. /* Maybe create a new address space object, and add it to the list, or
  50. return a pointer to an existing address space, in case inferiors
  51. share an address space on this target system. */
  52. struct address_space *
  53. maybe_new_address_space (void)
  54. {
  55. int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ());
  56. if (shared_aspace)
  57. {
  58. /* Just return the first in the list. */
  59. return program_spaces[0]->aspace;
  60. }
  61. return new_address_space ();
  62. }
  63. static void
  64. free_address_space (struct address_space *aspace)
  65. {
  66. address_space_free_data (aspace);
  67. xfree (aspace);
  68. }
  69. int
  70. address_space_num (struct address_space *aspace)
  71. {
  72. return aspace->num;
  73. }
  74. /* Start counting over from scratch. */
  75. static void
  76. init_address_spaces (void)
  77. {
  78. highest_address_space_num = 0;
  79. }
  80. /* Remove a program space from the program spaces list. */
  81. static void
  82. remove_program_space (program_space *pspace)
  83. {
  84. gdb_assert (pspace != NULL);
  85. auto iter = std::find (program_spaces.begin (), program_spaces.end (),
  86. pspace);
  87. gdb_assert (iter != program_spaces.end ());
  88. program_spaces.erase (iter);
  89. }
  90. /* See progspace.h. */
  91. program_space::program_space (address_space *aspace_)
  92. : num (++last_program_space_num),
  93. aspace (aspace_)
  94. {
  95. program_space_alloc_data (this);
  96. program_spaces.push_back (this);
  97. }
  98. /* See progspace.h. */
  99. program_space::~program_space ()
  100. {
  101. gdb_assert (this != current_program_space);
  102. remove_program_space (this);
  103. scoped_restore_current_program_space restore_pspace;
  104. set_current_program_space (this);
  105. breakpoint_program_space_exit (this);
  106. no_shared_libraries (NULL, 0);
  107. free_all_objfiles ();
  108. /* Defer breakpoint re-set because we don't want to create new
  109. locations for this pspace which we're tearing down. */
  110. clear_symtab_users (SYMFILE_DEFER_BP_RESET);
  111. if (!gdbarch_has_shared_address_space (target_gdbarch ()))
  112. free_address_space (this->aspace);
  113. /* Discard any data modules have associated with the PSPACE. */
  114. program_space_free_data (this);
  115. }
  116. /* See progspace.h. */
  117. void
  118. program_space::free_all_objfiles ()
  119. {
  120. /* Any objfile reference would become stale. */
  121. for (struct so_list *so : current_program_space->solibs ())
  122. gdb_assert (so->objfile == NULL);
  123. while (!objfiles_list.empty ())
  124. objfiles_list.front ()->unlink ();
  125. }
  126. /* See progspace.h. */
  127. void
  128. program_space::add_objfile (std::shared_ptr<objfile> &&objfile,
  129. struct objfile *before)
  130. {
  131. if (before == nullptr)
  132. objfiles_list.push_back (std::move (objfile));
  133. else
  134. {
  135. auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
  136. [=] (const std::shared_ptr<::objfile> &objf)
  137. {
  138. return objf.get () == before;
  139. });
  140. gdb_assert (iter != objfiles_list.end ());
  141. objfiles_list.insert (iter, std::move (objfile));
  142. }
  143. }
  144. /* See progspace.h. */
  145. void
  146. program_space::remove_objfile (struct objfile *objfile)
  147. {
  148. /* Removing an objfile from the objfile list invalidates any frame
  149. that was built using frame info found in the objfile. Reinit the
  150. frame cache to get rid of any frame that might otherwise
  151. reference stale info. */
  152. reinit_frame_cache ();
  153. auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
  154. [=] (const std::shared_ptr<::objfile> &objf)
  155. {
  156. return objf.get () == objfile;
  157. });
  158. gdb_assert (iter != objfiles_list.end ());
  159. objfiles_list.erase (iter);
  160. if (objfile == symfile_object_file)
  161. symfile_object_file = NULL;
  162. }
  163. /* See progspace.h. */
  164. void
  165. program_space::exec_close ()
  166. {
  167. if (ebfd != nullptr)
  168. {
  169. /* Removing target sections may close the exec_ops target.
  170. Clear ebfd before doing so to prevent recursion. */
  171. ebfd.reset (nullptr);
  172. ebfd_mtime = 0;
  173. remove_target_sections (&ebfd);
  174. exec_filename.reset (nullptr);
  175. }
  176. }
  177. /* Copies program space SRC to DEST. Copies the main executable file,
  178. and the main symbol file. Returns DEST. */
  179. struct program_space *
  180. clone_program_space (struct program_space *dest, struct program_space *src)
  181. {
  182. scoped_restore_current_program_space restore_pspace;
  183. set_current_program_space (dest);
  184. if (src->exec_filename != NULL)
  185. exec_file_attach (src->exec_filename.get (), 0);
  186. if (src->symfile_object_file != NULL)
  187. symbol_file_add_main (objfile_name (src->symfile_object_file),
  188. SYMFILE_DEFER_BP_RESET);
  189. return dest;
  190. }
  191. /* Sets PSPACE as the current program space. It is the caller's
  192. responsibility to make sure that the currently selected
  193. inferior/thread matches the selected program space. */
  194. void
  195. set_current_program_space (struct program_space *pspace)
  196. {
  197. if (current_program_space == pspace)
  198. return;
  199. gdb_assert (pspace != NULL);
  200. current_program_space = pspace;
  201. /* Different symbols change our view of the frame chain. */
  202. reinit_frame_cache ();
  203. }
  204. /* Returns true iff there's no inferior bound to PSPACE. */
  205. bool
  206. program_space::empty ()
  207. {
  208. return find_inferior_for_program_space (this) == nullptr;
  209. }
  210. /* Prints the list of program spaces and their details on UIOUT. If
  211. REQUESTED is not -1, it's the ID of the pspace that should be
  212. printed. Otherwise, all spaces are printed. */
  213. static void
  214. print_program_space (struct ui_out *uiout, int requested)
  215. {
  216. int count = 0;
  217. /* Compute number of pspaces we will print. */
  218. for (struct program_space *pspace : program_spaces)
  219. {
  220. if (requested != -1 && pspace->num != requested)
  221. continue;
  222. ++count;
  223. }
  224. /* There should always be at least one. */
  225. gdb_assert (count > 0);
  226. ui_out_emit_table table_emitter (uiout, 3, count, "pspaces");
  227. uiout->table_header (1, ui_left, "current", "");
  228. uiout->table_header (4, ui_left, "id", "Id");
  229. uiout->table_header (17, ui_left, "exec", "Executable");
  230. uiout->table_body ();
  231. for (struct program_space *pspace : program_spaces)
  232. {
  233. int printed_header;
  234. if (requested != -1 && requested != pspace->num)
  235. continue;
  236. ui_out_emit_tuple tuple_emitter (uiout, NULL);
  237. if (pspace == current_program_space)
  238. uiout->field_string ("current", "*");
  239. else
  240. uiout->field_skip ("current");
  241. uiout->field_signed ("id", pspace->num);
  242. if (pspace->exec_filename != nullptr)
  243. uiout->field_string ("exec", pspace->exec_filename.get (),
  244. file_name_style.style ());
  245. else
  246. uiout->field_skip ("exec");
  247. /* Print extra info that doesn't really fit in tabular form.
  248. Currently, we print the list of inferiors bound to a pspace.
  249. There can be more than one inferior bound to the same pspace,
  250. e.g., both parent/child inferiors in a vfork, or, on targets
  251. that share pspaces between inferiors. */
  252. printed_header = 0;
  253. /* We're going to switch inferiors. */
  254. scoped_restore_current_thread restore_thread;
  255. for (inferior *inf : all_inferiors ())
  256. if (inf->pspace == pspace)
  257. {
  258. /* Switch to inferior in order to call target methods. */
  259. switch_to_inferior_no_thread (inf);
  260. if (!printed_header)
  261. {
  262. printed_header = 1;
  263. gdb_printf ("\n\tBound inferiors: ID %d (%s)",
  264. inf->num,
  265. target_pid_to_str (ptid_t (inf->pid)).c_str ());
  266. }
  267. else
  268. gdb_printf (", ID %d (%s)",
  269. inf->num,
  270. target_pid_to_str (ptid_t (inf->pid)).c_str ());
  271. }
  272. uiout->text ("\n");
  273. }
  274. }
  275. /* Boolean test for an already-known program space id. */
  276. static int
  277. valid_program_space_id (int num)
  278. {
  279. for (struct program_space *pspace : program_spaces)
  280. if (pspace->num == num)
  281. return 1;
  282. return 0;
  283. }
  284. /* If ARGS is NULL or empty, print information about all program
  285. spaces. Otherwise, ARGS is a text representation of a LONG
  286. indicating which the program space to print information about. */
  287. static void
  288. maintenance_info_program_spaces_command (const char *args, int from_tty)
  289. {
  290. int requested = -1;
  291. if (args && *args)
  292. {
  293. requested = parse_and_eval_long (args);
  294. if (!valid_program_space_id (requested))
  295. error (_("program space ID %d not known."), requested);
  296. }
  297. print_program_space (current_uiout, requested);
  298. }
  299. /* Update all program spaces matching to address spaces. The user may
  300. have created several program spaces, and loaded executables into
  301. them before connecting to the target interface that will create the
  302. inferiors. All that happens before GDB has a chance to know if the
  303. inferiors will share an address space or not. Call this after
  304. having connected to the target interface and having fetched the
  305. target description, to fixup the program/address spaces mappings.
  306. It is assumed that there are no bound inferiors yet, otherwise,
  307. they'd be left with stale referenced to released aspaces. */
  308. void
  309. update_address_spaces (void)
  310. {
  311. int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ());
  312. init_address_spaces ();
  313. if (shared_aspace)
  314. {
  315. struct address_space *aspace = new_address_space ();
  316. free_address_space (current_program_space->aspace);
  317. for (struct program_space *pspace : program_spaces)
  318. pspace->aspace = aspace;
  319. }
  320. else
  321. for (struct program_space *pspace : program_spaces)
  322. {
  323. free_address_space (pspace->aspace);
  324. pspace->aspace = new_address_space ();
  325. }
  326. for (inferior *inf : all_inferiors ())
  327. if (gdbarch_has_global_solist (target_gdbarch ()))
  328. inf->aspace = maybe_new_address_space ();
  329. else
  330. inf->aspace = inf->pspace->aspace;
  331. }
  332. /* See progspace.h. */
  333. void
  334. program_space::clear_solib_cache ()
  335. {
  336. added_solibs.clear ();
  337. deleted_solibs.clear ();
  338. }
  339. void
  340. initialize_progspace (void)
  341. {
  342. add_cmd ("program-spaces", class_maintenance,
  343. maintenance_info_program_spaces_command,
  344. _("Info about currently known program spaces."),
  345. &maintenanceinfolist);
  346. /* There's always one program space. Note that this function isn't
  347. an automatic _initialize_foo function, since other
  348. _initialize_foo routines may need to install their per-pspace
  349. data keys. We can only allocate a progspace when all those
  350. modules have done that. Do this before
  351. initialize_current_architecture, because that accesses the ebfd
  352. of current_program_space. */
  353. current_program_space = new program_space (new_address_space ());
  354. }