1 /* Abstract base class inherited by all process_stratum targets 2 3 Copyright (C) 2018-2023 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 #ifndef PROCESS_STRATUM_TARGET_H 21 #define PROCESS_STRATUM_TARGET_H 22 23 #include "target.h" 24 #include <set> 25 #include "gdbsupport/intrusive_list.h" 26 #include "gdbsupport/gdb-checked-static-cast.h" 27 #include "gdbthread.h" 28 29 /* Abstract base class inherited by all process_stratum targets. */ 30 31 class process_stratum_target : public target_ops 32 { 33 public: 34 ~process_stratum_target () override = 0; 35 36 strata stratum () const final override { return process_stratum; } 37 38 /* Return a string representation of this target's open connection. 39 This string is used to distinguish different instances of a given 40 target type. For example, when remote debugging, the target is 41 called "remote", but since we may have more than one remote 42 target open, connection_string() returns the connection serial 43 connection name, e.g., "localhost:10001", "192.168.0.1:20000", 44 etc. This string is shown in several places, e.g., in "info 45 connections" and "info inferiors". */ 46 virtual const char *connection_string () { return nullptr; } 47 48 /* We must default these because they must be implemented by any 49 target that can run. */ 50 bool can_async_p () override { return false; } 51 bool supports_non_stop () override { return false; } 52 bool supports_disable_randomization () override { return false; } 53 54 /* This default implementation returns the inferior's address 55 space. */ 56 struct address_space *thread_address_space (ptid_t ptid) override; 57 58 /* This default implementation always returns target_gdbarch (). */ 59 struct gdbarch *thread_architecture (ptid_t ptid) override; 60 61 /* Default implementations for process_stratum targets. Return true 62 if there's a selected inferior, false otherwise. */ 63 bool has_all_memory () override; 64 bool has_memory () override; 65 bool has_stack () override; 66 bool has_registers () override; 67 bool has_execution (inferior *inf) override; 68 69 /* Default implementation of follow_exec. 70 71 If the current inferior and FOLLOW_INF are different (execution continues 72 in a new inferior), push this process target to FOLLOW_INF's target stack 73 and add an initial thread to FOLLOW_INF. */ 74 void follow_exec (inferior *follow_inf, ptid_t ptid, 75 const char *execd_pathname) override; 76 77 /* Default implementation of follow_fork. 78 79 If a child inferior was created by infrun while following the fork 80 (CHILD_INF is non-nullptr), push this target on CHILD_INF's target stack 81 and add an initial thread with ptid CHILD_PTID. */ 82 void follow_fork (inferior *child_inf, ptid_t child_ptid, 83 target_waitkind fork_kind, bool follow_child, 84 bool detach_on_fork) override; 85 86 /* True if any thread is, or may be executing. We need to track 87 this separately because until we fully sync the thread list, we 88 won't know whether the target is fully stopped, even if we see 89 stop events for all known threads, because any of those threads 90 may have spawned new threads we haven't heard of yet. */ 91 bool threads_executing = false; 92 93 /* If THREAD is resumed and has a pending wait status, add it to the 94 target's "resumed with pending wait status" list. */ 95 void maybe_add_resumed_with_pending_wait_status (thread_info *thread); 96 97 /* If THREAD is resumed and has a pending wait status, remove it from the 98 target's "resumed with pending wait status" list. */ 99 void maybe_remove_resumed_with_pending_wait_status (thread_info *thread); 100 101 /* Return true if this target has at least one resumed thread with a pending 102 wait status. */ 103 bool has_resumed_with_pending_wait_status () const 104 { return !m_resumed_with_pending_wait_status.empty (); } 105 106 /* Return a random resumed thread with pending wait status belonging to INF 107 and matching FILTER_PTID. */ 108 thread_info *random_resumed_with_pending_wait_status 109 (inferior *inf, ptid_t filter_ptid); 110 111 /* The connection number. Visible in "info connections". */ 112 int connection_number = 0; 113 114 /* Whether resumed threads must be committed to the target. 115 116 When true, resumed threads must be committed to the execution 117 target. 118 119 When false, the target may leave resumed threads stopped when 120 it's convenient or efficient to do so. When the core requires 121 resumed threads to be committed again, this is set back to true 122 and calls the `commit_resumed` method to allow the target to do 123 so. 124 125 To simplify the implementation of targets, the following methods 126 are guaranteed to be called with COMMIT_RESUMED_STATE set to 127 false: 128 129 - resume 130 - stop 131 - wait 132 133 Knowing this, the target doesn't need to implement different 134 behaviors depending on the COMMIT_RESUMED_STATE, and can simply 135 assume that it is false. 136 137 Targets can take advantage of this to batch resumption requests, 138 for example. In that case, the target doesn't actually resume in 139 its `resume` implementation. Instead, it takes note of the 140 resumption intent in `resume` and defers the actual resumption to 141 `commit_resumed`. For example, the remote target uses this to 142 coalesce multiple resumption requests in a single vCont 143 packet. */ 144 bool commit_resumed_state = false; 145 146 private: 147 /* List of threads managed by this target which simultaneously are resumed 148 and have a pending wait status. 149 150 This is done for optimization reasons, it would be possible to walk the 151 inferior thread lists to find these threads. But since this is something 152 we need to do quite frequently in the hot path, maintaining this list 153 avoids walking the thread lists repeatedly. */ 154 thread_info_resumed_with_pending_wait_status_list 155 m_resumed_with_pending_wait_status; 156 }; 157 158 /* Downcast TARGET to process_stratum_target. */ 159 160 static inline process_stratum_target * 161 as_process_stratum_target (target_ops *target) 162 { 163 gdb_assert (target->stratum () == process_stratum); 164 return gdb::checked_static_cast<process_stratum_target *> (target); 165 } 166 167 /* Return a collection of targets that have non-exited inferiors. */ 168 169 extern std::set<process_stratum_target *> all_non_exited_process_targets (); 170 171 /* Switch to the first inferior (and program space) of TARGET, and 172 switch to no thread selected. */ 173 174 extern void switch_to_target_no_thread (process_stratum_target *target); 175 176 #endif /* !defined (PROCESS_STRATUM_TARGET_H) */ 177