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