progspace.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. #ifndef PROGSPACE_H
  15. #define PROGSPACE_H
  16. #include "target.h"
  17. #include "gdb_bfd.h"
  18. #include "gdbsupport/gdb_vecs.h"
  19. #include "registry.h"
  20. #include "solist.h"
  21. #include "gdbsupport/next-iterator.h"
  22. #include "gdbsupport/safe-iterator.h"
  23. #include <list>
  24. #include <vector>
  25. struct target_ops;
  26. struct bfd;
  27. struct objfile;
  28. struct inferior;
  29. struct exec;
  30. struct address_space;
  31. struct program_space_data;
  32. struct address_space_data;
  33. struct so_list;
  34. typedef std::list<std::shared_ptr<objfile>> objfile_list;
  35. /* An iterator that wraps an iterator over std::shared_ptr<objfile>,
  36. and dereferences the returned object. This is useful for iterating
  37. over a list of shared pointers and returning raw pointers -- which
  38. helped avoid touching a lot of code when changing how objfiles are
  39. managed. */
  40. class unwrapping_objfile_iterator
  41. {
  42. public:
  43. typedef unwrapping_objfile_iterator self_type;
  44. typedef typename ::objfile *value_type;
  45. typedef typename ::objfile &reference;
  46. typedef typename ::objfile **pointer;
  47. typedef typename objfile_list::iterator::iterator_category iterator_category;
  48. typedef typename objfile_list::iterator::difference_type difference_type;
  49. unwrapping_objfile_iterator (objfile_list::iterator iter)
  50. : m_iter (std::move (iter))
  51. {
  52. }
  53. objfile *operator* () const
  54. {
  55. return m_iter->get ();
  56. }
  57. unwrapping_objfile_iterator operator++ ()
  58. {
  59. ++m_iter;
  60. return *this;
  61. }
  62. bool operator!= (const unwrapping_objfile_iterator &other) const
  63. {
  64. return m_iter != other.m_iter;
  65. }
  66. private:
  67. /* The underlying iterator. */
  68. objfile_list::iterator m_iter;
  69. };
  70. /* A range that returns unwrapping_objfile_iterators. */
  71. using unwrapping_objfile_range = iterator_range<unwrapping_objfile_iterator>;
  72. /* A program space represents a symbolic view of an address space.
  73. Roughly speaking, it holds all the data associated with a
  74. non-running-yet program (main executable, main symbols), and when
  75. an inferior is running and is bound to it, includes the list of its
  76. mapped in shared libraries.
  77. In the traditional debugging scenario, there's a 1-1 correspondence
  78. among program spaces, inferiors and address spaces, like so:
  79. pspace1 (prog1) <--> inf1(pid1) <--> aspace1
  80. In the case of debugging more than one traditional unix process or
  81. program, we still have:
  82. |-----------------+------------+---------|
  83. | pspace1 (prog1) | inf1(pid1) | aspace1 |
  84. |----------------------------------------|
  85. | pspace2 (prog1) | no inf yet | aspace2 |
  86. |-----------------+------------+---------|
  87. | pspace3 (prog2) | inf2(pid2) | aspace3 |
  88. |-----------------+------------+---------|
  89. In the former example, if inf1 forks (and GDB stays attached to
  90. both processes), the new child will have its own program and
  91. address spaces. Like so:
  92. |-----------------+------------+---------|
  93. | pspace1 (prog1) | inf1(pid1) | aspace1 |
  94. |-----------------+------------+---------|
  95. | pspace2 (prog1) | inf2(pid2) | aspace2 |
  96. |-----------------+------------+---------|
  97. However, had inf1 from the latter case vforked instead, it would
  98. share the program and address spaces with its parent, until it
  99. execs or exits, like so:
  100. |-----------------+------------+---------|
  101. | pspace1 (prog1) | inf1(pid1) | aspace1 |
  102. | | inf2(pid2) | |
  103. |-----------------+------------+---------|
  104. When the vfork child execs, it is finally given new program and
  105. address spaces.
  106. |-----------------+------------+---------|
  107. | pspace1 (prog1) | inf1(pid1) | aspace1 |
  108. |-----------------+------------+---------|
  109. | pspace2 (prog1) | inf2(pid2) | aspace2 |
  110. |-----------------+------------+---------|
  111. There are targets where the OS (if any) doesn't provide memory
  112. management or VM protection, where all inferiors share the same
  113. address space --- e.g. uClinux. GDB models this by having all
  114. inferiors share the same address space, but, giving each its own
  115. program space, like so:
  116. |-----------------+------------+---------|
  117. | pspace1 (prog1) | inf1(pid1) | |
  118. |-----------------+------------+ |
  119. | pspace2 (prog1) | inf2(pid2) | aspace1 |
  120. |-----------------+------------+ |
  121. | pspace3 (prog2) | inf3(pid3) | |
  122. |-----------------+------------+---------|
  123. The address space sharing matters for run control and breakpoints
  124. management. E.g., did we just hit a known breakpoint that we need
  125. to step over? Is this breakpoint a duplicate of this other one, or
  126. do I need to insert a trap?
  127. Then, there are targets where all symbols look the same for all
  128. inferiors, although each has its own address space, as e.g.,
  129. Ericsson DICOS. In such case, the model is:
  130. |---------+------------+---------|
  131. | | inf1(pid1) | aspace1 |
  132. | +------------+---------|
  133. | pspace | inf2(pid2) | aspace2 |
  134. | +------------+---------|
  135. | | inf3(pid3) | aspace3 |
  136. |---------+------------+---------|
  137. Note however, that the DICOS debug API takes care of making GDB
  138. believe that breakpoints are "global". That is, although each
  139. process does have its own private copy of data symbols (just like a
  140. bunch of forks), to the breakpoints module, all processes share a
  141. single address space, so all breakpoints set at the same address
  142. are duplicates of each other, even breakpoints set in the data
  143. space (e.g., call dummy breakpoints placed on stack). This allows
  144. a simplification in the spaces implementation: we avoid caring for
  145. a many-many links between address and program spaces. Either
  146. there's a single address space bound to the program space
  147. (traditional unix/uClinux), or, in the DICOS case, the address
  148. space bound to the program space is mostly ignored. */
  149. /* The program space structure. */
  150. struct program_space
  151. {
  152. /* Constructs a new empty program space, binds it to ASPACE, and
  153. adds it to the program space list. */
  154. explicit program_space (address_space *aspace);
  155. /* Releases a program space, and all its contents (shared libraries,
  156. objfiles, and any other references to the program space in other
  157. modules). It is an internal error to call this when the program
  158. space is the current program space, since there should always be
  159. a program space. */
  160. ~program_space ();
  161. using objfiles_range = unwrapping_objfile_range;
  162. /* Return an iterable object that can be used to iterate over all
  163. objfiles. The basic use is in a foreach, like:
  164. for (objfile *objf : pspace->objfiles ()) { ... } */
  165. objfiles_range objfiles ()
  166. {
  167. return objfiles_range
  168. (unwrapping_objfile_iterator (objfiles_list.begin ()),
  169. unwrapping_objfile_iterator (objfiles_list.end ()));
  170. }
  171. using objfiles_safe_range = basic_safe_range<objfiles_range>;
  172. /* An iterable object that can be used to iterate over all objfiles.
  173. The basic use is in a foreach, like:
  174. for (objfile *objf : pspace->objfiles_safe ()) { ... }
  175. This variant uses a basic_safe_iterator so that objfiles can be
  176. deleted during iteration. */
  177. objfiles_safe_range objfiles_safe ()
  178. {
  179. return objfiles_safe_range
  180. (objfiles_range
  181. (unwrapping_objfile_iterator (objfiles_list.begin ()),
  182. unwrapping_objfile_iterator (objfiles_list.end ())));
  183. }
  184. /* Add OBJFILE to the list of objfiles, putting it just before
  185. BEFORE. If BEFORE is nullptr, it will go at the end of the
  186. list. */
  187. void add_objfile (std::shared_ptr<objfile> &&objfile,
  188. struct objfile *before);
  189. /* Remove OBJFILE from the list of objfiles. */
  190. void remove_objfile (struct objfile *objfile);
  191. /* Return true if there is more than one object file loaded; false
  192. otherwise. */
  193. bool multi_objfile_p () const
  194. {
  195. return objfiles_list.size () > 1;
  196. }
  197. /* Free all the objfiles associated with this program space. */
  198. void free_all_objfiles ();
  199. /* Return a range adapter for iterating over all the solibs in this
  200. program space. Use it like:
  201. for (so_list *so : pspace->solibs ()) { ... } */
  202. so_list_range solibs () const
  203. { return so_list_range (this->so_list); }
  204. /* Close and clear exec_bfd. If we end up with no target sections
  205. to read memory from, this unpushes the exec_ops target. */
  206. void exec_close ();
  207. /* Return the exec BFD for this program space. */
  208. bfd *exec_bfd () const
  209. {
  210. return ebfd.get ();
  211. }
  212. /* Set the exec BFD for this program space to ABFD. */
  213. void set_exec_bfd (gdb_bfd_ref_ptr &&abfd)
  214. {
  215. ebfd = std::move (abfd);
  216. }
  217. /* Reset saved solib data at the start of an solib event. This lets
  218. us properly collect the data when calling solib_add, so it can then
  219. later be printed. */
  220. void clear_solib_cache ();
  221. /* Returns true iff there's no inferior bound to this program
  222. space. */
  223. bool empty ();
  224. /* Remove all target sections owned by OWNER. */
  225. void remove_target_sections (void *owner);
  226. /* Add the sections array defined by SECTIONS to the
  227. current set of target sections. */
  228. void add_target_sections (void *owner,
  229. const target_section_table &sections);
  230. /* Add the sections of OBJFILE to the current set of target
  231. sections. They are given OBJFILE as the "owner". */
  232. void add_target_sections (struct objfile *objfile);
  233. /* Clear all target sections from M_TARGET_SECTIONS table. */
  234. void clear_target_sections ()
  235. {
  236. m_target_sections.clear ();
  237. }
  238. /* Return a reference to the M_TARGET_SECTIONS table. */
  239. target_section_table &target_sections ()
  240. {
  241. return m_target_sections;
  242. }
  243. /* Unique ID number. */
  244. int num = 0;
  245. /* The main executable loaded into this program space. This is
  246. managed by the exec target. */
  247. /* The BFD handle for the main executable. */
  248. gdb_bfd_ref_ptr ebfd;
  249. /* The last-modified time, from when the exec was brought in. */
  250. long ebfd_mtime = 0;
  251. /* Similar to bfd_get_filename (exec_bfd) but in original form given
  252. by user, without symbolic links and pathname resolved. It is not
  253. NULL iff EBFD is not NULL. */
  254. gdb::unique_xmalloc_ptr<char> exec_filename;
  255. /* Binary file diddling handle for the core file. */
  256. gdb_bfd_ref_ptr cbfd;
  257. /* The address space attached to this program space. More than one
  258. program space may be bound to the same address space. In the
  259. traditional unix-like debugging scenario, this will usually
  260. match the address space bound to the inferior, and is mostly
  261. used by the breakpoints module for address matches. If the
  262. target shares a program space for all inferiors and breakpoints
  263. are global, then this field is ignored (we don't currently
  264. support inferiors sharing a program space if the target doesn't
  265. make breakpoints global). */
  266. struct address_space *aspace = NULL;
  267. /* True if this program space's section offsets don't yet represent
  268. the final offsets of the "live" address space (that is, the
  269. section addresses still require the relocation offsets to be
  270. applied, and hence we can't trust the section addresses for
  271. anything that pokes at live memory). E.g., for qOffsets
  272. targets, or for PIE executables, until we connect and ask the
  273. target for the final relocation offsets, the symbols we've used
  274. to set breakpoints point at the wrong addresses. */
  275. int executing_startup = 0;
  276. /* True if no breakpoints should be inserted in this program
  277. space. */
  278. int breakpoints_not_allowed = 0;
  279. /* The object file that the main symbol table was loaded from
  280. (e.g. the argument to the "symbol-file" or "file" command). */
  281. struct objfile *symfile_object_file = NULL;
  282. /* All known objfiles are kept in a linked list. */
  283. std::list<std::shared_ptr<objfile>> objfiles_list;
  284. /* List of shared objects mapped into this space. Managed by
  285. solib.c. */
  286. struct so_list *so_list = NULL;
  287. /* Number of calls to solib_add. */
  288. unsigned int solib_add_generation = 0;
  289. /* When an solib is added, it is also added to this vector. This
  290. is so we can properly report solib changes to the user. */
  291. std::vector<struct so_list *> added_solibs;
  292. /* When an solib is removed, its name is added to this vector.
  293. This is so we can properly report solib changes to the user. */
  294. std::vector<std::string> deleted_solibs;
  295. /* Per pspace data-pointers required by other GDB modules. */
  296. REGISTRY_FIELDS {};
  297. private:
  298. /* The set of target sections matching the sections mapped into
  299. this program space. Managed by both exec_ops and solib.c. */
  300. target_section_table m_target_sections;
  301. };
  302. /* An address space. It is used for comparing if
  303. pspaces/inferior/threads see the same address space and for
  304. associating caches to each address space. */
  305. struct address_space
  306. {
  307. int num;
  308. /* Per aspace data-pointers required by other GDB modules. */
  309. REGISTRY_FIELDS;
  310. };
  311. /* The list of all program spaces. There's always at least one. */
  312. extern std::vector<struct program_space *>program_spaces;
  313. /* The current program space. This is always non-null. */
  314. extern struct program_space *current_program_space;
  315. /* Copies program space SRC to DEST. Copies the main executable file,
  316. and the main symbol file. Returns DEST. */
  317. extern struct program_space *clone_program_space (struct program_space *dest,
  318. struct program_space *src);
  319. /* Sets PSPACE as the current program space. This is usually used
  320. instead of set_current_space_and_thread when the current
  321. thread/inferior is not important for the operations that follow.
  322. E.g., when accessing the raw symbol tables. If memory access is
  323. required, then you should use switch_to_program_space_and_thread.
  324. Otherwise, it is the caller's responsibility to make sure that the
  325. currently selected inferior/thread matches the selected program
  326. space. */
  327. extern void set_current_program_space (struct program_space *pspace);
  328. /* Save/restore the current program space. */
  329. class scoped_restore_current_program_space
  330. {
  331. public:
  332. scoped_restore_current_program_space ()
  333. : m_saved_pspace (current_program_space)
  334. {}
  335. ~scoped_restore_current_program_space ()
  336. { set_current_program_space (m_saved_pspace); }
  337. DISABLE_COPY_AND_ASSIGN (scoped_restore_current_program_space);
  338. private:
  339. program_space *m_saved_pspace;
  340. };
  341. /* Create a new address space object, and add it to the list. */
  342. extern struct address_space *new_address_space (void);
  343. /* Maybe create a new address space object, and add it to the list, or
  344. return a pointer to an existing address space, in case inferiors
  345. share an address space. */
  346. extern struct address_space *maybe_new_address_space (void);
  347. /* Returns the integer address space id of ASPACE. */
  348. extern int address_space_num (struct address_space *aspace);
  349. /* Update all program spaces matching to address spaces. The user may
  350. have created several program spaces, and loaded executables into
  351. them before connecting to the target interface that will create the
  352. inferiors. All that happens before GDB has a chance to know if the
  353. inferiors will share an address space or not. Call this after
  354. having connected to the target interface and having fetched the
  355. target description, to fixup the program/address spaces
  356. mappings. */
  357. extern void update_address_spaces (void);
  358. /* Keep a registry of per-pspace data-pointers required by other GDB
  359. modules. */
  360. DECLARE_REGISTRY (program_space);
  361. /* Keep a registry of per-aspace data-pointers required by other GDB
  362. modules. */
  363. DECLARE_REGISTRY (address_space);
  364. #endif