1 /* Abstract base class inherited by all process_stratum targets
3 Copyright (C) 2018-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #ifndef PROCESS_STRATUM_TARGET_H
21 #define PROCESS_STRATUM_TARGET_H
25 #include "gdbsupport/intrusive_list.h"
26 #include "gdbsupport/gdb-checked-static-cast.h"
27 #include "gdbthread.h"
29 /* Abstract base class inherited by all process_stratum targets. */
31 class process_stratum_target
: public target_ops
34 ~process_stratum_target () override
= 0;
36 strata
stratum () const final override
{ return process_stratum
; }
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; }
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; }
54 /* This default implementation returns the inferior's address
56 struct address_space
*thread_address_space (ptid_t ptid
) override
;
58 /* This default implementation always returns target_gdbarch (). */
59 struct gdbarch
*thread_architecture (ptid_t ptid
) override
;
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
;
69 /* Default implementation of follow_exec.
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
;
77 /* Default implementation of follow_fork.
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
;
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;
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
);
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
);
101 /* Return true if this target has at least one resumed thread with a pending
103 bool has_resumed_with_pending_wait_status () const
104 { return !m_resumed_with_pending_wait_status
.empty (); }
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
);
111 /* Search function to lookup a (non-exited) thread by 'ptid'. */
112 thread_info
*find_thread (ptid_t ptid
);
114 /* The connection number. Visible in "info connections". */
115 int connection_number
= 0;
117 /* Whether resumed threads must be committed to the target.
119 When true, resumed threads must be committed to the execution
122 When false, the target may leave resumed threads stopped when
123 it's convenient or efficient to do so. When the core requires
124 resumed threads to be committed again, this is set back to true
125 and calls the `commit_resumed` method to allow the target to do
128 To simplify the implementation of targets, the following methods
129 are guaranteed to be called with COMMIT_RESUMED_STATE set to
136 Knowing this, the target doesn't need to implement different
137 behaviors depending on the COMMIT_RESUMED_STATE, and can simply
138 assume that it is false.
140 Targets can take advantage of this to batch resumption requests,
141 for example. In that case, the target doesn't actually resume in
142 its `resume` implementation. Instead, it takes note of the
143 resumption intent in `resume` and defers the actual resumption to
144 `commit_resumed`. For example, the remote target uses this to
145 coalesce multiple resumption requests in a single vCont
147 bool commit_resumed_state
= false;
150 /* List of threads managed by this target which simultaneously are resumed
151 and have a pending wait status.
153 This is done for optimization reasons, it would be possible to walk the
154 inferior thread lists to find these threads. But since this is something
155 we need to do quite frequently in the hot path, maintaining this list
156 avoids walking the thread lists repeatedly. */
157 thread_info_resumed_with_pending_wait_status_list
158 m_resumed_with_pending_wait_status
;
161 /* Downcast TARGET to process_stratum_target. */
163 static inline process_stratum_target
*
164 as_process_stratum_target (target_ops
*target
)
166 gdb_assert (target
->stratum () == process_stratum
);
167 return gdb::checked_static_cast
<process_stratum_target
*> (target
);
170 /* Return a collection of targets that have non-exited inferiors. */
172 extern std::set
<process_stratum_target
*> all_non_exited_process_targets ();
174 /* Switch to the first inferior (and program space) of TARGET, and
175 switch to no thread selected. */
177 extern void switch_to_target_no_thread (process_stratum_target
*target
);
179 #endif /* !defined (PROCESS_STRATUM_TARGET_H) */