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