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