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