1 1.1 christos /* Variables that describe the inferior process running under GDB: 2 1.1 christos Where it is, why it stopped, and how to step it. 3 1.1 christos 4 1.11 christos Copyright (C) 1986-2024 Free Software Foundation, Inc. 5 1.1 christos 6 1.1 christos This file is part of GDB. 7 1.1 christos 8 1.1 christos This program is free software; you can redistribute it and/or modify 9 1.1 christos it under the terms of the GNU General Public License as published by 10 1.1 christos the Free Software Foundation; either version 3 of the License, or 11 1.1 christos (at your option) any later version. 12 1.1 christos 13 1.1 christos This program is distributed in the hope that it will be useful, 14 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 15 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 1.1 christos GNU General Public License for more details. 17 1.1 christos 18 1.1 christos You should have received a copy of the GNU General Public License 19 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 20 1.1 christos 21 1.1 christos #if !defined (INFERIOR_H) 22 1.1 christos #define INFERIOR_H 1 23 1.1 christos 24 1.9 christos #include <exception> 25 1.10 christos #include <list> 26 1.9 christos 27 1.1 christos struct target_waitstatus; 28 1.10 christos class frame_info_ptr; 29 1.1 christos struct ui_file; 30 1.1 christos struct type; 31 1.1 christos struct gdbarch; 32 1.1 christos struct regcache; 33 1.1 christos struct ui_out; 34 1.1 christos struct terminal_info; 35 1.1 christos struct target_desc_info; 36 1.8 christos struct inferior; 37 1.8 christos struct thread_info; 38 1.1 christos 39 1.1 christos /* For bpstat. */ 40 1.1 christos #include "breakpoint.h" 41 1.1 christos 42 1.1 christos /* For enum gdb_signal. */ 43 1.1 christos #include "target.h" 44 1.1 christos 45 1.1 christos /* For struct frame_id. */ 46 1.1 christos #include "frame.h" 47 1.1 christos 48 1.8 christos /* For gdb_environ. */ 49 1.9 christos #include "gdbsupport/environ.h" 50 1.8 christos 51 1.1 christos #include "progspace.h" 52 1.1 christos #include "registry.h" 53 1.1 christos 54 1.7 christos #include "symfile-add-flags.h" 55 1.9 christos #include "gdbsupport/refcounted-object.h" 56 1.9 christos #include "gdbsupport/forward-scope-exit.h" 57 1.9 christos #include "gdbsupport/gdb_unique_ptr.h" 58 1.10 christos #include "gdbsupport/intrusive_list.h" 59 1.8 christos 60 1.9 christos #include "gdbsupport/common-inferior.h" 61 1.8 christos #include "gdbthread.h" 62 1.7 christos 63 1.9 christos #include "process-stratum-target.h" 64 1.10 christos #include "displaced-stepping.h" 65 1.10 christos 66 1.10 christos #include <unordered_map> 67 1.9 christos 68 1.1 christos struct infcall_suspend_state; 69 1.1 christos struct infcall_control_state; 70 1.1 christos 71 1.1 christos extern void restore_infcall_suspend_state (struct infcall_suspend_state *); 72 1.1 christos extern void restore_infcall_control_state (struct infcall_control_state *); 73 1.1 christos 74 1.8 christos /* A deleter for infcall_suspend_state that calls 75 1.8 christos restore_infcall_suspend_state. */ 76 1.8 christos struct infcall_suspend_state_deleter 77 1.8 christos { 78 1.8 christos void operator() (struct infcall_suspend_state *state) const 79 1.8 christos { 80 1.9 christos try 81 1.8 christos { 82 1.8 christos restore_infcall_suspend_state (state); 83 1.8 christos } 84 1.9 christos catch (const gdb_exception_error &e) 85 1.8 christos { 86 1.8 christos /* If we are restoring the inferior state due to an exception, 87 1.8 christos some error message will be printed. So, only warn the user 88 1.8 christos when we cannot restore during normal execution. */ 89 1.9 christos bool unwinding; 90 1.9 christos #if __cpp_lib_uncaught_exceptions 91 1.9 christos unwinding = std::uncaught_exceptions () > 0; 92 1.9 christos #else 93 1.9 christos unwinding = std::uncaught_exception (); 94 1.9 christos #endif 95 1.9 christos if (!unwinding) 96 1.9 christos warning (_("Failed to restore inferior state: %s"), e.what ()); 97 1.8 christos } 98 1.8 christos } 99 1.8 christos }; 100 1.8 christos 101 1.8 christos /* A unique_ptr specialization for infcall_suspend_state. */ 102 1.8 christos typedef std::unique_ptr<infcall_suspend_state, infcall_suspend_state_deleter> 103 1.8 christos infcall_suspend_state_up; 104 1.8 christos 105 1.8 christos extern infcall_suspend_state_up save_infcall_suspend_state (); 106 1.8 christos 107 1.8 christos /* A deleter for infcall_control_state that calls 108 1.8 christos restore_infcall_control_state. */ 109 1.8 christos struct infcall_control_state_deleter 110 1.8 christos { 111 1.8 christos void operator() (struct infcall_control_state *state) const 112 1.8 christos { 113 1.8 christos restore_infcall_control_state (state); 114 1.8 christos } 115 1.8 christos }; 116 1.8 christos 117 1.8 christos /* A unique_ptr specialization for infcall_control_state. */ 118 1.8 christos typedef std::unique_ptr<infcall_control_state, infcall_control_state_deleter> 119 1.8 christos infcall_control_state_up; 120 1.8 christos 121 1.8 christos extern infcall_control_state_up save_infcall_control_state (); 122 1.1 christos 123 1.1 christos extern void discard_infcall_suspend_state (struct infcall_suspend_state *); 124 1.1 christos extern void discard_infcall_control_state (struct infcall_control_state *); 125 1.1 christos 126 1.8 christos extern readonly_detached_regcache * 127 1.1 christos get_infcall_suspend_state_regcache (struct infcall_suspend_state *); 128 1.1 christos 129 1.1 christos extern void set_sigint_trap (void); 130 1.1 christos 131 1.1 christos extern void clear_sigint_trap (void); 132 1.1 christos 133 1.1 christos /* Collected pid, tid, etc. of the debugged inferior. When there's 134 1.8 christos no inferior, inferior_ptid.pid () will be 0. */ 135 1.1 christos 136 1.1 christos extern ptid_t inferior_ptid; 137 1.1 christos 138 1.1 christos extern void generic_mourn_inferior (void); 139 1.1 christos 140 1.1 christos extern CORE_ADDR unsigned_pointer_to_address (struct gdbarch *gdbarch, 141 1.1 christos struct type *type, 142 1.1 christos const gdb_byte *buf); 143 1.1 christos extern void unsigned_address_to_pointer (struct gdbarch *gdbarch, 144 1.1 christos struct type *type, gdb_byte *buf, 145 1.1 christos CORE_ADDR addr); 146 1.1 christos extern CORE_ADDR signed_pointer_to_address (struct gdbarch *gdbarch, 147 1.1 christos struct type *type, 148 1.1 christos const gdb_byte *buf); 149 1.1 christos extern void address_to_signed_pointer (struct gdbarch *gdbarch, 150 1.1 christos struct type *type, gdb_byte *buf, 151 1.1 christos CORE_ADDR addr); 152 1.1 christos 153 1.1 christos extern void reopen_exec_file (void); 154 1.1 christos 155 1.1 christos /* From misc files */ 156 1.1 christos 157 1.1 christos extern void default_print_registers_info (struct gdbarch *gdbarch, 158 1.1 christos struct ui_file *file, 159 1.11 christos const frame_info_ptr &frame, 160 1.1 christos int regnum, int all); 161 1.1 christos 162 1.3 christos /* Default implementation of gdbarch_print_float_info. Print 163 1.3 christos the values of all floating point registers. */ 164 1.3 christos 165 1.3 christos extern void default_print_float_info (struct gdbarch *gdbarch, 166 1.3 christos struct ui_file *file, 167 1.11 christos const frame_info_ptr &frame, 168 1.3 christos const char *args); 169 1.3 christos 170 1.10 christos /* Try to determine whether TTY is GDB's input terminal. Returns 171 1.10 christos TRIBOOL_UNKNOWN if we can't tell. */ 172 1.10 christos 173 1.10 christos extern tribool is_gdb_terminal (const char *tty); 174 1.10 christos 175 1.10 christos /* Helper for sharing_input_terminal. Try to determine whether pid 176 1.10 christos PID is using the same TTY for input as GDB is. Returns 177 1.10 christos TRIBOOL_UNKNOWN if we can't tell. */ 178 1.10 christos 179 1.10 christos extern tribool sharing_input_terminal (int pid); 180 1.10 christos 181 1.10 christos /* The type of the function that is called when SIGINT is handled. */ 182 1.10 christos 183 1.10 christos typedef void c_c_handler_ftype (int); 184 1.10 christos 185 1.10 christos /* Install a new SIGINT handler in a host-dependent way. The previous 186 1.10 christos handler is returned. It is fine to pass SIG_IGN for FN, but not 187 1.10 christos SIG_DFL. */ 188 1.10 christos 189 1.10 christos extern c_c_handler_ftype *install_sigint_handler (c_c_handler_ftype *fn); 190 1.10 christos 191 1.3 christos extern void child_terminal_info (struct target_ops *self, const char *, int); 192 1.1 christos 193 1.3 christos extern void child_terminal_ours (struct target_ops *self); 194 1.3 christos 195 1.3 christos extern void child_terminal_ours_for_output (struct target_ops *self); 196 1.1 christos 197 1.3 christos extern void child_terminal_inferior (struct target_ops *self); 198 1.1 christos 199 1.8 christos extern void child_terminal_save_inferior (struct target_ops *self); 200 1.8 christos 201 1.3 christos extern void child_terminal_init (struct target_ops *self); 202 1.1 christos 203 1.8 christos extern void child_pass_ctrlc (struct target_ops *self); 204 1.1 christos 205 1.8 christos extern void child_interrupt (struct target_ops *self); 206 1.7 christos 207 1.8 christos /* From fork-child.c */ 208 1.1 christos 209 1.8 christos /* Helper function to call STARTUP_INFERIOR with PID and NUM_TRAPS. 210 1.8 christos This function already calls set_executing. Return the ptid_t from 211 1.8 christos STARTUP_INFERIOR. */ 212 1.8 christos extern ptid_t gdb_startup_inferior (pid_t pid, int num_traps); 213 1.1 christos 214 1.1 christos /* From infcmd.c */ 215 1.1 christos 216 1.6 christos /* Initial inferior setup. Determines the exec file is not yet known, 217 1.6 christos takes any necessary post-attaching actions, fetches the target 218 1.6 christos description and syncs the shared library list. */ 219 1.6 christos 220 1.6 christos extern void setup_inferior (int from_tty); 221 1.6 christos 222 1.10 christos extern void post_create_inferior (int from_tty); 223 1.1 christos 224 1.8 christos extern void attach_command (const char *, int); 225 1.1 christos 226 1.8 christos extern void registers_info (const char *, int); 227 1.1 christos 228 1.1 christos extern void continue_1 (int all_threads); 229 1.1 christos 230 1.9 christos extern void interrupt_target_1 (bool all_threads); 231 1.1 christos 232 1.8 christos using delete_longjmp_breakpoint_cleanup 233 1.8 christos = FORWARD_SCOPE_EXIT (delete_longjmp_breakpoint); 234 1.1 christos 235 1.8 christos extern void detach_command (const char *, int); 236 1.1 christos 237 1.10 christos extern void notice_new_inferior (struct thread_info *, bool, int); 238 1.10 christos 239 1.10 christos /* Return the value of the result of a function at the end of a 'finish' 240 1.10 christos command/BP. If the result's value cannot be retrieved, return NULL. 241 1.10 christos 242 1.10 christos FUNC_SYMBOL is the symbol of the function being returned from. FUNCTION is 243 1.10 christos a value containing the address of the function. */ 244 1.1 christos 245 1.10 christos extern struct value *get_return_value (struct symbol *func_symbol, 246 1.10 christos struct value *function); 247 1.1 christos 248 1.3 christos /* Prepare for execution command. TARGET is the target that will run 249 1.3 christos the command. BACKGROUND determines whether this is a foreground 250 1.3 christos (synchronous) or background (asynchronous) command. */ 251 1.3 christos 252 1.3 christos extern void prepare_execution_command (struct target_ops *target, 253 1.3 christos int background); 254 1.3 christos 255 1.1 christos /* Nonzero if stopped due to completion of a stack dummy routine. */ 256 1.1 christos 257 1.1 christos extern enum stop_stack_kind stop_stack_dummy; 258 1.1 christos 259 1.1 christos /* Nonzero if program stopped due to a random (unexpected) signal in 260 1.1 christos inferior process. */ 261 1.1 christos 262 1.1 christos extern int stopped_by_random_signal; 263 1.1 christos 264 1.8 christos /* Print notices on inferior events (attach, detach, etc.), set with 265 1.8 christos `set print inferior-events'. */ 266 1.9 christos extern bool print_inferior_events; 267 1.1 christos 268 1.1 christos /* Anything but NO_STOP_QUIETLY means we expect a trap and the caller 269 1.1 christos will handle it themselves. STOP_QUIETLY is used when running in 270 1.1 christos the shell before the child program has been exec'd and when running 271 1.1 christos through shared library loading. STOP_QUIETLY_REMOTE is used when 272 1.1 christos setting up a remote connection; it is like STOP_QUIETLY_NO_SIGSTOP 273 1.1 christos except that there is no need to hide a signal. */ 274 1.1 christos 275 1.3 christos /* STOP_QUIETLY_NO_SIGSTOP is used to handle a tricky situation with attach. 276 1.3 christos When doing an attach, the kernel stops the debuggee with a SIGSTOP. 277 1.3 christos On newer GNU/Linux kernels (>= 2.5.61) the handling of SIGSTOP for 278 1.3 christos a ptraced process has changed. Earlier versions of the kernel 279 1.3 christos would ignore these SIGSTOPs, while now SIGSTOP is treated like any 280 1.3 christos other signal, i.e. it is not muffled. 281 1.3 christos 282 1.1 christos If the gdb user does a 'continue' after the 'attach', gdb passes 283 1.1 christos the global variable stop_signal (which stores the signal from the 284 1.1 christos attach, SIGSTOP) to the ptrace(PTRACE_CONT,...) call. This is 285 1.1 christos problematic, because the kernel doesn't ignore such SIGSTOP 286 1.1 christos now. I.e. it is reported back to gdb, which in turn presents it 287 1.1 christos back to the user. 288 1.3 christos 289 1.1 christos To avoid the problem, we use STOP_QUIETLY_NO_SIGSTOP, which allows 290 1.1 christos gdb to clear the value of stop_signal after the attach, so that it 291 1.1 christos is not passed back down to the kernel. */ 292 1.1 christos 293 1.1 christos enum stop_kind 294 1.1 christos { 295 1.1 christos NO_STOP_QUIETLY = 0, 296 1.1 christos STOP_QUIETLY, 297 1.1 christos STOP_QUIETLY_REMOTE, 298 1.1 christos STOP_QUIETLY_NO_SIGSTOP 299 1.1 christos }; 300 1.1 christos 301 1.1 christos 302 1.1 christos 304 1.1 christos /* Base class for target-specific inferior data. */ 305 1.8 christos 306 1.8 christos struct private_inferior 307 1.8 christos { 308 1.8 christos virtual ~private_inferior () = 0; 309 1.1 christos }; 310 1.1 christos 311 1.1 christos /* Inferior process specific part of `struct infcall_control_state'. 312 1.1 christos 313 1.1 christos Inferior thread counterpart is `struct thread_control_state'. */ 314 1.1 christos 315 1.1 christos struct inferior_control_state 316 1.8 christos { 317 1.8 christos inferior_control_state () 318 1.8 christos : stop_soon (NO_STOP_QUIETLY) 319 1.8 christos { 320 1.8 christos } 321 1.8 christos 322 1.8 christos explicit inferior_control_state (enum stop_kind when) 323 1.8 christos : stop_soon (when) 324 1.8 christos { 325 1.8 christos } 326 1.1 christos 327 1.1 christos /* See the definition of stop_kind above. */ 328 1.1 christos enum stop_kind stop_soon; 329 1.1 christos }; 330 1.11 christos 331 1.11 christos /* Initialize the inferior-related global state. */ 332 1.11 christos extern void initialize_inferiors (); 333 1.8 christos 334 1.8 christos /* Return a pointer to the current inferior. */ 335 1.8 christos extern inferior *current_inferior (); 336 1.8 christos 337 1.8 christos extern void set_current_inferior (inferior *); 338 1.9 christos 339 1.9 christos /* Switch inferior (and program space) to INF, and switch to no thread 340 1.9 christos selected. */ 341 1.9 christos extern void switch_to_inferior_no_thread (inferior *inf); 342 1.11 christos 343 1.11 christos /* Ensure INF is the current inferior. 344 1.11 christos 345 1.11 christos If the current inferior was changed, return an RAII object that will 346 1.11 christos restore the original current context. */ 347 1.11 christos extern std::optional<scoped_restore_current_thread> maybe_switch_inferior 348 1.11 christos (inferior *inf); 349 1.11 christos 350 1.11 christos /* Info about an inferior's target description. There's one of these 351 1.11 christos for each inferior. */ 352 1.11 christos 353 1.11 christos struct target_desc_info 354 1.11 christos { 355 1.11 christos /* Returns true if this target description information has been supplied by 356 1.11 christos the user. */ 357 1.11 christos bool from_user_p () 358 1.11 christos { return !this->filename.empty (); } 359 1.11 christos 360 1.11 christos /* A flag indicating that a description has already been fetched 361 1.11 christos from the target, so it should not be queried again. */ 362 1.11 christos bool fetched = false; 363 1.11 christos 364 1.11 christos /* The description fetched from the target, or NULL if the target 365 1.11 christos did not supply any description. Only valid when 366 1.11 christos FETCHED is set. Only the description initialization 367 1.11 christos code should access this; normally, the description should be 368 1.11 christos accessed through the gdbarch object. */ 369 1.11 christos const struct target_desc *tdesc = nullptr; 370 1.11 christos 371 1.11 christos /* If not empty, the filename to read a target description from, as set by 372 1.11 christos "set tdesc filename ...". 373 1.11 christos 374 1.11 christos If empty, there is not filename specified by the user. */ 375 1.11 christos std::string filename; 376 1.11 christos }; 377 1.1 christos 378 1.1 christos /* GDB represents the state of each program execution with an object 379 1.1 christos called an inferior. An inferior typically corresponds to a process 380 1.1 christos but is more general and applies also to targets that do not have a 381 1.1 christos notion of processes. Each run of an executable creates a new 382 1.1 christos inferior, as does each attachment to an existing process. 383 1.1 christos Inferiors have unique internal identifiers that are different from 384 1.8 christos target process ids. Each inferior may in turn have multiple 385 1.1 christos threads running in it. 386 1.8 christos 387 1.8 christos Inferiors are intrusively refcounted objects. Unlike thread 388 1.8 christos objects, being the user-selected inferior is considered a strong 389 1.8 christos reference and is thus accounted for in the inferior object's 390 1.8 christos refcount (see set_current_inferior). When GDB needs to remember 391 1.8 christos the selected inferior to later restore it, GDB temporarily bumps 392 1.8 christos the inferior object's refcount, to prevent something deleting the 393 1.8 christos inferior object before reverting back (e.g., due to a 394 1.9 christos "remove-inferiors" command (see 395 1.8 christos scoped_restore_current_inferior). All other inferior 396 1.8 christos references are considered weak references. Inferiors are always 397 1.8 christos listed exactly once in the inferior list, so placing an inferior in 398 1.8 christos the inferior list is an implicit, not counted strong reference. */ 399 1.10 christos 400 1.10 christos class inferior : public refcounted_object, 401 1.1 christos public intrusive_list_node<inferior> 402 1.7 christos { 403 1.7 christos public: 404 1.7 christos explicit inferior (int pid); 405 1.7 christos ~inferior (); 406 1.8 christos 407 1.8 christos /* Returns true if we can delete this inferior. */ 408 1.8 christos bool deletable () const { return refcount () == 0; } 409 1.9 christos 410 1.9 christos /* Push T in this inferior's target stack. */ 411 1.9 christos void push_target (struct target_ops *t) 412 1.9 christos { m_target_stack.push (t); } 413 1.10 christos 414 1.10 christos /* An overload that deletes the target on failure. */ 415 1.10 christos void push_target (target_ops_up &&t) 416 1.10 christos { 417 1.10 christos m_target_stack.push (t.get ()); 418 1.10 christos t.release (); 419 1.10 christos } 420 1.9 christos 421 1.10 christos /* Unpush T from this inferior's target stack. */ 422 1.9 christos int unpush_target (struct target_ops *t); 423 1.9 christos 424 1.10 christos /* Returns true if T is pushed in this inferior's target stack. */ 425 1.9 christos bool target_is_pushed (const target_ops *t) const 426 1.9 christos { return m_target_stack.is_pushed (t); } 427 1.9 christos 428 1.9 christos /* Find the target beneath T in this inferior's target stack. */ 429 1.9 christos target_ops *find_target_beneath (const target_ops *t) 430 1.9 christos { return m_target_stack.find_beneath (t); } 431 1.9 christos 432 1.9 christos /* Return the target at the top of this inferior's target stack. */ 433 1.9 christos target_ops *top_target () 434 1.9 christos { return m_target_stack.top (); } 435 1.10 christos 436 1.10 christos /* Unpush all targets except the dummy target from m_target_stack. As 437 1.10 christos targets are removed from m_target_stack their reference count is 438 1.10 christos decremented, which may cause a target to close. */ 439 1.10 christos void pop_all_targets () 440 1.10 christos { pop_all_targets_above (dummy_stratum); } 441 1.10 christos 442 1.10 christos /* Unpush all targets above STRATUM from m_target_stack. As targets are 443 1.10 christos removed from m_target_stack their reference count is decremented, 444 1.10 christos which may cause a target to close. */ 445 1.10 christos void pop_all_targets_above (enum strata stratum); 446 1.10 christos 447 1.10 christos /* Unpush all targets at and above STRATUM from m_target_stack. As 448 1.10 christos targets are removed from m_target_stack their reference count is 449 1.10 christos decremented, which may cause a target to close. */ 450 1.10 christos void pop_all_targets_at_and_above (enum strata stratum); 451 1.9 christos 452 1.9 christos /* Return the target at process_stratum level in this inferior's 453 1.9 christos target stack. */ 454 1.9 christos struct process_stratum_target *process_target () 455 1.9 christos { return (process_stratum_target *) m_target_stack.at (process_stratum); } 456 1.9 christos 457 1.9 christos /* Return the target at STRATUM in this inferior's target stack. */ 458 1.9 christos target_ops *target_at (enum strata stratum) 459 1.9 christos { return m_target_stack.at (stratum); } 460 1.9 christos 461 1.10 christos bool has_execution () 462 1.9 christos { return target_has_execution (this); } 463 1.10 christos 464 1.10 christos /* This inferior's thread list, sorted by creation order. */ 465 1.1 christos intrusive_list<thread_info> thread_list; 466 1.10 christos 467 1.10 christos /* A map of ptid_t to thread_info*, for average O(1) ptid_t lookup. 468 1.11 christos Exited threads do not appear in the map. */ 469 1.8 christos std::unordered_map<ptid_t, thread_info *> ptid_thread_map; 470 1.8 christos 471 1.8 christos /* Returns a range adapter covering the inferior's threads, 472 1.8 christos including exited threads. Used like this: 473 1.8 christos 474 1.8 christos for (thread_info *thr : inf->threads ()) 475 1.8 christos { .... } 476 1.8 christos */ 477 1.10 christos inf_threads_range threads () 478 1.8 christos { return inf_threads_range (this->thread_list.begin ()); } 479 1.8 christos 480 1.8 christos /* Returns a range adapter covering the inferior's non-exited 481 1.8 christos threads. Used like this: 482 1.8 christos 483 1.8 christos for (thread_info *thr : inf->non_exited_threads ()) 484 1.8 christos { .... } 485 1.8 christos */ 486 1.10 christos inf_non_exited_threads_range non_exited_threads () 487 1.8 christos { return inf_non_exited_threads_range (this->thread_list.begin ()); } 488 1.8 christos 489 1.8 christos /* Like inferior::threads(), but returns a range adapter that can be 490 1.8 christos used with range-for, safely. I.e., it is safe to delete the 491 1.8 christos currently-iterated thread, like this: 492 1.8 christos 493 1.8 christos for (thread_info *t : inf->threads_safe ()) 494 1.8 christos if (some_condition ()) 495 1.8 christos delete f; 496 1.8 christos */ 497 1.10 christos inline safe_inf_threads_range threads_safe () 498 1.10 christos { return safe_inf_threads_range (this->thread_list.begin ()); } 499 1.11 christos 500 1.11 christos /* Find (non-exited) thread PTID of this inferior. */ 501 1.11 christos thread_info *find_thread (ptid_t ptid); 502 1.11 christos 503 1.11 christos /* Delete all threads in the thread list, silently. */ 504 1.10 christos void clear_thread_list (); 505 1.10 christos 506 1.10 christos /* Continuations-related methods. A continuation is an std::function 507 1.10 christos to be called to finish the execution of a command when running 508 1.10 christos GDB asynchronously. A continuation is executed after any thread 509 1.10 christos of this inferior stops. Continuations are used by the attach 510 1.10 christos command and the remote target when a new inferior is detected. */ 511 1.10 christos void add_continuation (std::function<void ()> &&cont); 512 1.10 christos void do_all_continuations (); 513 1.10 christos 514 1.10 christos /* Set/get file name for default use for standard in/out in the inferior. 515 1.10 christos 516 1.10 christos On Unix systems, we try to make TERMINAL_NAME the inferior's controlling 517 1.10 christos terminal. 518 1.10 christos 519 1.10 christos If TERMINAL_NAME is the empty string, then the inferior inherits GDB's 520 1.10 christos terminal (or GDBserver's if spawning a remote process). */ 521 1.10 christos void set_tty (std::string terminal_name); 522 1.10 christos const std::string &tty (); 523 1.10 christos 524 1.10 christos /* Set the argument string to use when running this inferior. 525 1.10 christos 526 1.10 christos An empty string can be used to represent "no arguments". */ 527 1.10 christos void set_args (std::string args) 528 1.10 christos { 529 1.10 christos m_args = std::move (args); 530 1.10 christos }; 531 1.11 christos 532 1.11 christos /* Set the argument string from some strings. */ 533 1.11 christos void set_args (gdb::array_view<char * const> args); 534 1.10 christos 535 1.10 christos /* Get the argument string to use when running this inferior. 536 1.10 christos 537 1.10 christos No arguments is represented by an empty string. */ 538 1.10 christos const std::string &args () const 539 1.10 christos { 540 1.10 christos return m_args; 541 1.8 christos } 542 1.10 christos 543 1.10 christos /* Set the inferior current working directory. 544 1.10 christos 545 1.10 christos If CWD is empty, unset the directory. */ 546 1.10 christos void set_cwd (std::string cwd) 547 1.10 christos { 548 1.10 christos m_cwd = std::move (cwd); 549 1.10 christos } 550 1.10 christos 551 1.10 christos /* Get the inferior current working directory. 552 1.10 christos 553 1.10 christos Return an empty string if the current working directory is not 554 1.10 christos specified. */ 555 1.10 christos const std::string &cwd () const 556 1.10 christos { 557 1.10 christos return m_cwd; 558 1.9 christos } 559 1.11 christos 560 1.11 christos /* Set this inferior's arch. */ 561 1.11 christos void set_arch (gdbarch *arch); 562 1.11 christos 563 1.11 christos /* Get this inferior's arch. */ 564 1.11 christos gdbarch *arch () 565 1.11 christos { return m_gdbarch; } 566 1.1 christos 567 1.1 christos /* Convenient handle (GDB inferior id). Unique across all 568 1.7 christos inferiors. */ 569 1.1 christos int num = 0; 570 1.1 christos 571 1.1 christos /* Actual target inferior id, usually, a process id. This matches 572 1.7 christos the ptid_t.pid member of threads of this inferior. */ 573 1.1 christos int pid = 0; 574 1.7 christos /* True if the PID was actually faked by GDB. */ 575 1.1 christos bool fake_pid_p = false; 576 1.6 christos 577 1.7 christos /* The highest thread number this inferior ever had. */ 578 1.6 christos int highest_thread_num = 0; 579 1.1 christos 580 1.1 christos /* State of GDB control of inferior process execution. 581 1.8 christos See `struct inferior_control_state'. */ 582 1.1 christos inferior_control_state control; 583 1.1 christos 584 1.1 christos /* True if this was an auto-created inferior, e.g. created from 585 1.1 christos following a fork; false, if this inferior was manually added by 586 1.1 christos the user, and we should not attempt to prune it 587 1.7 christos automatically. */ 588 1.1 christos bool removable = false; 589 1.1 christos 590 1.11 christos /* The address space bound to this inferior. */ 591 1.1 christos address_space_ref_ptr aspace; 592 1.1 christos 593 1.7 christos /* The program space bound to this inferior. */ 594 1.1 christos struct program_space *pspace = NULL; 595 1.8 christos 596 1.8 christos /* The terminal state as set by the last target_terminal::terminal_* 597 1.8 christos call. */ 598 1.8 christos target_terminal_state terminal_state = target_terminal_state::is_ours; 599 1.1 christos 600 1.1 christos /* Environment to use for running inferior, 601 1.8 christos in format described in environ.h. */ 602 1.1 christos gdb_environ environment; 603 1.7 christos 604 1.7 christos /* True if this child process was attached rather than forked. */ 605 1.1 christos bool attach_flag = false; 606 1.1 christos 607 1.1 christos /* If this inferior is a vfork child, then this is the pointer to 608 1.7 christos its vfork parent, if GDB is still attached to it. */ 609 1.1 christos inferior *vfork_parent = NULL; 610 1.1 christos 611 1.1 christos /* If this process is a vfork parent, this is the pointer to the 612 1.1 christos child. Since a vfork parent is left frozen by the kernel until 613 1.1 christos the child execs or exits, a process can only have one vfork child 614 1.7 christos at a given time. */ 615 1.1 christos inferior *vfork_child = NULL; 616 1.1 christos 617 1.1 christos /* True if this inferior should be detached when it's vfork sibling 618 1.7 christos exits or execs. */ 619 1.1 christos bool pending_detach = false; 620 1.10 christos 621 1.10 christos /* If non-nullptr, points to a thread that called vfork and is now waiting 622 1.10 christos for a vfork child not under our control to be done with the shared memory 623 1.10 christos region, either by exiting or execing. */ 624 1.1 christos thread_info *thread_waiting_for_vfork_done = nullptr; 625 1.1 christos 626 1.8 christos /* True if we're in the process of detaching from this inferior. */ 627 1.1 christos bool detaching = false; 628 1.6 christos 629 1.6 christos /* True if setup_inferior wasn't called for this inferior yet. 630 1.6 christos Until that is done, we must not access inferior memory or 631 1.6 christos registers, as we haven't determined the target 632 1.7 christos architecture/description. */ 633 1.6 christos bool needs_setup = false; 634 1.10 christos 635 1.10 christos /* True if the inferior is starting up (inside startup_inferior), 636 1.10 christos and we're nursing it along (through the shell) until it is ready 637 1.10 christos to execute its first instruction. Until that is done, we must 638 1.10 christos not access inferior memory or registers, as we haven't determined 639 1.10 christos the target architecture/description. */ 640 1.10 christos bool starting_up = false; 641 1.10 christos 642 1.10 christos /* True when we are reading the library list of the inferior during an 643 1.10 christos attach or handling a fork child. */ 644 1.10 christos bool in_initial_library_scan = false; 645 1.11 christos 646 1.8 christos /* Private data used by the process_stratum target. */ 647 1.1 christos std::unique_ptr<private_inferior> priv; 648 1.1 christos 649 1.1 christos /* HAS_EXIT_CODE is true if the inferior exited with an exit code. 650 1.7 christos In this case, the EXIT_CODE field is also valid. */ 651 1.7 christos bool has_exit_code = false; 652 1.1 christos LONGEST exit_code = 0; 653 1.1 christos 654 1.7 christos /* Default flags to pass to the symbol reading functions. These are 655 1.7 christos used whenever a new objfile is created. */ 656 1.1 christos symfile_add_flags symfile_flags = 0; 657 1.1 christos 658 1.1 christos /* Info about an inferior's target description (if it's fetched; the 659 1.11 christos user supplied description's filename, if any; etc.). */ 660 1.1 christos target_desc_info tdesc_info; 661 1.8 christos 662 1.8 christos /* Data related to displaced stepping. */ 663 1.8 christos displaced_step_inferior_state displaced_step_state; 664 1.1 christos 665 1.10 christos /* Per inferior data-pointers required by other GDB modules. */ 666 1.9 christos registry<inferior> registry_fields; 667 1.9 christos 668 1.10 christos private: 669 1.10 christos 670 1.10 christos /* Unpush TARGET and assert that it worked. */ 671 1.10 christos void unpush_target_and_assert (struct target_ops *target); 672 1.9 christos 673 1.9 christos /* The inferior's target stack. */ 674 1.9 christos target_stack m_target_stack; 675 1.9 christos 676 1.10 christos /* The name of terminal device to use for I/O. */ 677 1.1 christos std::string m_terminal; 678 1.10 christos 679 1.10 christos /* The list of continuations. */ 680 1.1 christos std::list<std::function<void ()>> m_continuations; 681 1.10 christos 682 1.10 christos /* The arguments string to use when running. */ 683 1.10 christos std::string m_args; 684 1.10 christos 685 1.10 christos /* The current working directory that will be used when starting 686 1.10 christos this inferior. */ 687 1.11 christos std::string m_cwd; 688 1.11 christos 689 1.11 christos /* The architecture associated with the inferior through the 690 1.11 christos connection to the target. 691 1.11 christos 692 1.11 christos The architecture vector provides some information that is really 693 1.11 christos a property of the inferior, accessed through a particular target: 694 1.11 christos ptrace operations; the layout of certain RSP packets; the 695 1.11 christos solib_ops vector; etc. To differentiate architecture accesses to 696 1.11 christos per-inferior/target properties from 697 1.11 christos per-thread/per-frame/per-objfile properties, accesses to 698 1.11 christos per-inferior/target properties should be made through 699 1.11 christos this gdbarch. */ 700 1.10 christos gdbarch *m_gdbarch = nullptr; 701 1.1 christos }; 702 1.1 christos 703 1.1 christos /* Add an inferior to the inferior list, print a message that a new 704 1.1 christos inferior is found, and return the pointer to the new inferior. 705 1.1 christos Caller may use this pointer to initialize the private inferior 706 1.1 christos data. */ 707 1.1 christos extern struct inferior *add_inferior (int pid); 708 1.1 christos 709 1.1 christos /* Same as add_inferior, but don't print new inferior notifications to 710 1.1 christos the CLI. */ 711 1.1 christos extern struct inferior *add_inferior_silent (int pid); 712 1.6 christos 713 1.1 christos extern void delete_inferior (struct inferior *todel); 714 1.1 christos 715 1.8 christos /* Delete an existing inferior list entry, due to inferior detaching. */ 716 1.1 christos extern void detach_inferior (inferior *inf); 717 1.11 christos 718 1.11 christos /* Notify observers and interpreters that INF has gone away. Reset the INF 719 1.11 christos object back to an default, empty, state. Clear register and frame 720 1.8 christos caches. */ 721 1.1 christos extern void exit_inferior (inferior *inf); 722 1.1 christos 723 1.1 christos extern void inferior_appeared (struct inferior *inf, int pid); 724 1.9 christos 725 1.9 christos /* Search function to lookup an inferior of TARG by target 'pid'. */ 726 1.9 christos extern struct inferior *find_inferior_pid (process_stratum_target *targ, 727 1.9 christos int pid); 728 1.9 christos 729 1.9 christos /* Search function to lookup an inferior of TARG whose pid is equal to 730 1.9 christos 'ptid.pid'. */ 731 1.9 christos extern struct inferior *find_inferior_ptid (process_stratum_target *targ, 732 1.3 christos ptid_t ptid); 733 1.1 christos 734 1.1 christos /* Search function to lookup an inferior by GDB 'num'. */ 735 1.1 christos extern struct inferior *find_inferior_id (int num); 736 1.3 christos 737 1.3 christos /* Find an inferior bound to PSPACE, giving preference to the current 738 1.1 christos inferior. */ 739 1.1 christos extern struct inferior * 740 1.1 christos find_inferior_for_program_space (struct program_space *pspace); 741 1.1 christos 742 1.1 christos /* Returns true if the inferior list is not empty. */ 743 1.1 christos extern int have_inferiors (void); 744 1.9 christos 745 1.9 christos /* Returns the number of live inferiors running on PROC_TARGET (real 746 1.9 christos live processes with execution). */ 747 1.6 christos extern int number_of_live_inferiors (process_stratum_target *proc_target); 748 1.1 christos 749 1.1 christos /* Returns true if there are any live inferiors in the inferior list 750 1.1 christos (not cores, not executables, real live processes). */ 751 1.1 christos extern int have_live_inferiors (void); 752 1.8 christos 753 1.8 christos /* Save/restore the current inferior. */ 754 1.8 christos 755 1.8 christos class scoped_restore_current_inferior 756 1.8 christos { 757 1.8 christos public: 758 1.8 christos scoped_restore_current_inferior () 759 1.8 christos : m_saved_inf (current_inferior ()) 760 1.1 christos {} 761 1.8 christos 762 1.8 christos ~scoped_restore_current_inferior () 763 1.8 christos { set_current_inferior (m_saved_inf); } 764 1.8 christos 765 1.8 christos DISABLE_COPY_AND_ASSIGN (scoped_restore_current_inferior); 766 1.8 christos 767 1.8 christos private: 768 1.8 christos inferior *m_saved_inf; 769 1.1 christos }; 770 1.11 christos 771 1.11 christos /* When reading memory from an inferior, the global inferior_ptid must 772 1.11 christos also be set. This class arranges to save and restore the necessary 773 1.11 christos state for reading or writing memory, but without invalidating the 774 1.11 christos frame cache. */ 775 1.11 christos 776 1.11 christos class scoped_restore_current_inferior_for_memory 777 1.11 christos { 778 1.11 christos public: 779 1.11 christos 780 1.11 christos /* Save the current globals and switch to the given inferior and the 781 1.11 christos inferior's program space. inferior_ptid is set to point to the 782 1.11 christos inferior's process id (and not to any particular thread). */ 783 1.11 christos explicit scoped_restore_current_inferior_for_memory (inferior *inf) 784 1.11 christos : m_save_ptid (&inferior_ptid) 785 1.11 christos { 786 1.11 christos set_current_inferior (inf); 787 1.11 christos set_current_program_space (inf->pspace); 788 1.11 christos inferior_ptid = ptid_t (inf->pid); 789 1.11 christos } 790 1.11 christos 791 1.11 christos DISABLE_COPY_AND_ASSIGN (scoped_restore_current_inferior_for_memory); 792 1.11 christos 793 1.11 christos private: 794 1.11 christos 795 1.11 christos scoped_restore_current_inferior m_save_inferior; 796 1.11 christos scoped_restore_current_program_space m_save_progspace; 797 1.11 christos scoped_restore_tmpl<ptid_t> m_save_ptid; 798 1.11 christos }; 799 1.1 christos 800 1.1 christos 801 1.1 christos /* Traverse all inferiors. */ 802 1.10 christos 803 1.8 christos extern intrusive_list<inferior> inferior_list; 804 1.8 christos 805 1.8 christos /* Pull in the internals of the inferiors ranges and iterators. Must 806 1.8 christos be done after struct inferior is defined. */ 807 1.8 christos #include "inferior-iter.h" 808 1.8 christos 809 1.8 christos /* Return a range that can be used to walk over all inferiors 810 1.8 christos inferiors, with range-for, safely. I.e., it is safe to delete the 811 1.8 christos currently-iterated inferior. When combined with range-for, this 812 1.8 christos allow convenient patterns like this: 813 1.8 christos 814 1.8 christos for (inferior *inf : all_inferiors_safe ()) 815 1.8 christos if (some_condition ()) 816 1.8 christos delete inf; 817 1.1 christos */ 818 1.8 christos 819 1.8 christos inline all_inferiors_safe_range 820 1.8 christos all_inferiors_safe () 821 1.10 christos { 822 1.8 christos return all_inferiors_safe_range (nullptr, inferior_list); 823 1.8 christos } 824 1.8 christos 825 1.8 christos /* Returns a range representing all inferiors, suitable to use with 826 1.7 christos range-for, like this: 827 1.8 christos 828 1.8 christos for (inferior *inf : all_inferiors ()) 829 1.8 christos [...] 830 1.8 christos */ 831 1.8 christos 832 1.9 christos inline all_inferiors_range 833 1.8 christos all_inferiors (process_stratum_target *proc_target = nullptr) 834 1.10 christos { 835 1.8 christos return all_inferiors_range (proc_target, inferior_list); 836 1.7 christos } 837 1.8 christos 838 1.8 christos /* Return a range that can be used to walk over all inferiors with PID 839 1.8 christos not zero, with range-for. */ 840 1.8 christos 841 1.9 christos inline all_non_exited_inferiors_range 842 1.8 christos all_non_exited_inferiors (process_stratum_target *proc_target = nullptr) 843 1.10 christos { 844 1.8 christos return all_non_exited_inferiors_range (proc_target, inferior_list); 845 1.1 christos } 846 1.1 christos 847 1.1 christos /* Prune away automatically added inferiors that aren't required 848 1.1 christos anymore. */ 849 1.1 christos extern void prune_inferiors (void); 850 1.1 christos 851 1.1 christos extern int number_of_inferiors (void); 852 1.1 christos 853 1.1 christos extern struct inferior *add_inferior_with_spaces (void); 854 1.6 christos 855 1.6 christos /* Print the current selected inferior. */ 856 1.6 christos extern void print_selected_inferior (struct ui_out *uiout); 857 1.10 christos 858 1.10 christos /* Switch to inferior NEW_INF, a new inferior, and unless 859 1.10 christos NO_CONNECTION is true, push the process_stratum_target of ORG_INF 860 1.10 christos to NEW_INF. */ 861 1.10 christos 862 1.10 christos extern void switch_to_inferior_and_push_target 863 1.10 christos (inferior *new_inf, bool no_connection, inferior *org_inf); 864 1.11 christos 865 1.11 christos /* Return true if ID is a valid global inferior number. */ 866 1.11 christos 867 1.11 christos inline bool 868 1.11 christos valid_global_inferior_id (int id) 869 1.11 christos { 870 1.11 christos for (inferior *inf : all_inferiors ()) 871 1.11 christos if (inf->num == id) 872 1.11 christos return true; 873 1.11 christos return false; 874 1.11 christos } 875 1.1 christos 876 #endif /* !defined (INFERIOR_H) */ 877