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