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