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