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