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