| 1 | /* Program and address space management, for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 2009-2012 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 | |
| 27 | struct target_ops; |
| 28 | struct bfd; |
| 29 | struct objfile; |
| 30 | struct inferior; |
| 31 | struct exec; |
| 32 | struct address_space; |
| 33 | struct program_space_data; |
| 34 | |
| 35 | /* A program space represents a symbolic view of an address space. |
| 36 | Roughly speaking, it holds all the data associated with a |
| 37 | non-running-yet program (main executable, main symbols), and when |
| 38 | an inferior is running and is bound to it, includes the list of its |
| 39 | mapped in shared libraries. |
| 40 | |
| 41 | In the traditional debugging scenario, there's a 1-1 correspondence |
| 42 | among program spaces, inferiors and address spaces, like so: |
| 43 | |
| 44 | pspace1 (prog1) <--> inf1(pid1) <--> aspace1 |
| 45 | |
| 46 | In the case of debugging more than one traditional unix process or |
| 47 | program, we still have: |
| 48 | |
| 49 | |-----------------+------------+---------| |
| 50 | | pspace1 (prog1) | inf1(pid1) | aspace1 | |
| 51 | |----------------------------------------| |
| 52 | | pspace2 (prog1) | no inf yet | aspace2 | |
| 53 | |-----------------+------------+---------| |
| 54 | | pspace3 (prog2) | inf2(pid2) | aspace3 | |
| 55 | |-----------------+------------+---------| |
| 56 | |
| 57 | In the former example, if inf1 forks (and GDB stays attached to |
| 58 | both processes), the new child will have its own program and |
| 59 | address spaces. Like so: |
| 60 | |
| 61 | |-----------------+------------+---------| |
| 62 | | pspace1 (prog1) | inf1(pid1) | aspace1 | |
| 63 | |-----------------+------------+---------| |
| 64 | | pspace2 (prog1) | inf2(pid2) | aspace2 | |
| 65 | |-----------------+------------+---------| |
| 66 | |
| 67 | However, had inf1 from the latter case vforked instead, it would |
| 68 | share the program and address spaces with its parent, until it |
| 69 | execs or exits, like so: |
| 70 | |
| 71 | |-----------------+------------+---------| |
| 72 | | pspace1 (prog1) | inf1(pid1) | aspace1 | |
| 73 | | | inf2(pid2) | | |
| 74 | |-----------------+------------+---------| |
| 75 | |
| 76 | When the vfork child execs, it is finally given new program and |
| 77 | address spaces. |
| 78 | |
| 79 | |-----------------+------------+---------| |
| 80 | | pspace1 (prog1) | inf1(pid1) | aspace1 | |
| 81 | |-----------------+------------+---------| |
| 82 | | pspace2 (prog1) | inf2(pid2) | aspace2 | |
| 83 | |-----------------+------------+---------| |
| 84 | |
| 85 | There are targets where the OS (if any) doesn't provide memory |
| 86 | management or VM protection, where all inferiors share the same |
| 87 | address space --- e.g. uClinux. GDB models this by having all |
| 88 | inferiors share the same address space, but, giving each its own |
| 89 | program space, like so: |
| 90 | |
| 91 | |-----------------+------------+---------| |
| 92 | | pspace1 (prog1) | inf1(pid1) | | |
| 93 | |-----------------+------------+ | |
| 94 | | pspace2 (prog1) | inf2(pid2) | aspace1 | |
| 95 | |-----------------+------------+ | |
| 96 | | pspace3 (prog2) | inf3(pid3) | | |
| 97 | |-----------------+------------+---------| |
| 98 | |
| 99 | The address space sharing matters for run control and breakpoints |
| 100 | management. E.g., did we just hit a known breakpoint that we need |
| 101 | to step over? Is this breakpoint a duplicate of this other one, or |
| 102 | do I need to insert a trap? |
| 103 | |
| 104 | Then, there are targets where all symbols look the same for all |
| 105 | inferiors, although each has its own address space, as e.g., |
| 106 | Ericsson DICOS. In such case, the model is: |
| 107 | |
| 108 | |---------+------------+---------| |
| 109 | | | inf1(pid1) | aspace1 | |
| 110 | | +------------+---------| |
| 111 | | pspace | inf2(pid2) | aspace2 | |
| 112 | | +------------+---------| |
| 113 | | | inf3(pid3) | aspace3 | |
| 114 | |---------+------------+---------| |
| 115 | |
| 116 | Note however, that the DICOS debug API takes care of making GDB |
| 117 | believe that breakpoints are "global". That is, although each |
| 118 | process does have its own private copy of data symbols (just like a |
| 119 | bunch of forks), to the breakpoints module, all processes share a |
| 120 | single address space, so all breakpoints set at the same address |
| 121 | are duplicates of each other, even breakpoints set in the data |
| 122 | space (e.g., call dummy breakpoints placed on stack). This allows |
| 123 | a simplification in the spaces implementation: we avoid caring for |
| 124 | a many-many links between address and program spaces. Either |
| 125 | there's a single address space bound to the program space |
| 126 | (traditional unix/uClinux), or, in the DICOS case, the address |
| 127 | space bound to the program space is mostly ignored. */ |
| 128 | |
| 129 | /* The program space structure. */ |
| 130 | |
| 131 | struct program_space |
| 132 | { |
| 133 | /* Pointer to next in linked list. */ |
| 134 | struct program_space *next; |
| 135 | |
| 136 | /* Unique ID number. */ |
| 137 | int num; |
| 138 | |
| 139 | /* The main executable loaded into this program space. This is |
| 140 | managed by the exec target. */ |
| 141 | |
| 142 | /* The BFD handle for the main executable. */ |
| 143 | bfd *ebfd; |
| 144 | /* The last-modified time, from when the exec was brought in. */ |
| 145 | long ebfd_mtime; |
| 146 | |
| 147 | /* The address space attached to this program space. More than one |
| 148 | program space may be bound to the same address space. In the |
| 149 | traditional unix-like debugging scenario, this will usually |
| 150 | match the address space bound to the inferior, and is mostly |
| 151 | used by the breakpoints module for address matches. If the |
| 152 | target shares a program space for all inferiors and breakpoints |
| 153 | are global, then this field is ignored (we don't currently |
| 154 | support inferiors sharing a program space if the target doesn't |
| 155 | make breakpoints global). */ |
| 156 | struct address_space *aspace; |
| 157 | |
| 158 | /* True if this program space's section offsets don't yet represent |
| 159 | the final offsets of the "live" address space (that is, the |
| 160 | section addresses still require the relocation offsets to be |
| 161 | applied, and hence we can't trust the section addresses for |
| 162 | anything that pokes at live memory). E.g., for qOffsets |
| 163 | targets, or for PIE executables, until we connect and ask the |
| 164 | target for the final relocation offsets, the symbols we've used |
| 165 | to set breakpoints point at the wrong addresses. */ |
| 166 | int executing_startup; |
| 167 | |
| 168 | /* True if no breakpoints should be inserted in this program |
| 169 | space. */ |
| 170 | int breakpoints_not_allowed; |
| 171 | |
| 172 | /* The object file that the main symbol table was loaded from |
| 173 | (e.g. the argument to the "symbol-file" or "file" command). */ |
| 174 | struct objfile *symfile_object_file; |
| 175 | |
| 176 | /* All known objfiles are kept in a linked list. This points to |
| 177 | the head of this list. */ |
| 178 | struct objfile *objfiles; |
| 179 | |
| 180 | /* The set of target sections matching the sections mapped into |
| 181 | this program space. Managed by both exec_ops and solib.c. */ |
| 182 | struct target_section_table target_sections; |
| 183 | |
| 184 | /* List of shared objects mapped into this space. Managed by |
| 185 | solib.c. */ |
| 186 | struct so_list *so_list; |
| 187 | |
| 188 | /* Number of calls to solib_add. */ |
| 189 | unsigned solib_add_generation; |
| 190 | |
| 191 | /* Per pspace data-pointers required by other GDB modules. */ |
| 192 | void **data; |
| 193 | unsigned num_data; |
| 194 | }; |
| 195 | |
| 196 | /* The object file that the main symbol table was loaded from (e.g. the |
| 197 | argument to the "symbol-file" or "file" command). */ |
| 198 | |
| 199 | #define symfile_objfile current_program_space->symfile_object_file |
| 200 | |
| 201 | /* All known objfiles are kept in a linked list. This points to the |
| 202 | root of this list. */ |
| 203 | #define object_files current_program_space->objfiles |
| 204 | |
| 205 | /* The set of target sections matching the sections mapped into the |
| 206 | current program space. */ |
| 207 | #define current_target_sections (¤t_program_space->target_sections) |
| 208 | |
| 209 | /* The list of all program spaces. There's always at least one. */ |
| 210 | extern struct program_space *program_spaces; |
| 211 | |
| 212 | /* The current program space. This is always non-null. */ |
| 213 | extern struct program_space *current_program_space; |
| 214 | |
| 215 | #define ALL_PSPACES(pspace) \ |
| 216 | for ((pspace) = program_spaces; (pspace) != NULL; (pspace) = (pspace)->next) |
| 217 | |
| 218 | /* Add a new empty program space, and assign ASPACE to it. Returns the |
| 219 | pointer to the new object. */ |
| 220 | extern struct program_space *add_program_space (struct address_space *aspace); |
| 221 | |
| 222 | /* Release PSPACE and removes it from the pspace list. */ |
| 223 | extern void remove_program_space (struct program_space *pspace); |
| 224 | |
| 225 | /* Returns the number of program spaces listed. */ |
| 226 | extern int number_of_program_spaces (void); |
| 227 | |
| 228 | /* Copies program space SRC to DEST. Copies the main executable file, |
| 229 | and the main symbol file. Returns DEST. */ |
| 230 | extern struct program_space *clone_program_space (struct program_space *dest, |
| 231 | struct program_space *src); |
| 232 | |
| 233 | /* Save the current program space so that it may be restored by a later |
| 234 | call to do_cleanups. Returns the struct cleanup pointer needed for |
| 235 | later doing the cleanup. */ |
| 236 | extern struct cleanup *save_current_program_space (void); |
| 237 | |
| 238 | /* Sets PSPACE as the current program space. This is usually used |
| 239 | instead of set_current_space_and_thread when the current |
| 240 | thread/inferior is not important for the operations that follow. |
| 241 | E.g., when accessing the raw symbol tables. If memory access is |
| 242 | required, then you should use switch_to_program_space_and_thread. |
| 243 | Otherwise, it is the caller's responsibility to make sure that the |
| 244 | currently selected inferior/thread matches the selected program |
| 245 | space. */ |
| 246 | extern void set_current_program_space (struct program_space *pspace); |
| 247 | |
| 248 | /* Saves the current thread (may be null), frame and program space in |
| 249 | the current cleanup chain. */ |
| 250 | extern struct cleanup *save_current_space_and_thread (void); |
| 251 | |
| 252 | /* Switches full context to program space PSPACE. Switches to the |
| 253 | first thread found bound to PSPACE. */ |
| 254 | extern void switch_to_program_space_and_thread (struct program_space *pspace); |
| 255 | |
| 256 | /* Create a new address space object, and add it to the list. */ |
| 257 | extern struct address_space *new_address_space (void); |
| 258 | |
| 259 | /* Maybe create a new address space object, and add it to the list, or |
| 260 | return a pointer to an existing address space, in case inferiors |
| 261 | share an address space. */ |
| 262 | extern struct address_space *maybe_new_address_space (void); |
| 263 | |
| 264 | /* Returns the integer address space id of ASPACE. */ |
| 265 | extern int address_space_num (struct address_space *aspace); |
| 266 | |
| 267 | /* Update all program spaces matching to address spaces. The user may |
| 268 | have created several program spaces, and loaded executables into |
| 269 | them before connecting to the target interface that will create the |
| 270 | inferiors. All that happens before GDB has a chance to know if the |
| 271 | inferiors will share an address space or not. Call this after |
| 272 | having connected to the target interface and having fetched the |
| 273 | target description, to fixup the program/address spaces |
| 274 | mappings. */ |
| 275 | extern void update_address_spaces (void); |
| 276 | |
| 277 | /* Prune away automatically added program spaces that aren't required |
| 278 | anymore. */ |
| 279 | extern void prune_program_spaces (void); |
| 280 | |
| 281 | /* Keep a registry of per-pspace data-pointers required by other GDB |
| 282 | modules. */ |
| 283 | |
| 284 | extern const struct program_space_data *register_program_space_data (void); |
| 285 | extern const struct program_space_data *register_program_space_data_with_cleanup |
| 286 | (void (*cleanup) (struct program_space *, void *)); |
| 287 | extern void clear_program_space_data (struct program_space *pspace); |
| 288 | extern void set_program_space_data (struct program_space *pspace, |
| 289 | const struct program_space_data *data, |
| 290 | void *value); |
| 291 | extern void *program_space_data (struct program_space *pspace, |
| 292 | const struct program_space_data *data); |
| 293 | |
| 294 | #endif |