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 the current inferior's
60 struct gdbarch
*thread_architecture (ptid_t ptid
) override
;
62 /* Default implementations for process_stratum targets. Return true
63 if there's a selected inferior, false otherwise. */
64 bool has_all_memory () override
;
65 bool has_memory () override
;
66 bool has_stack () override
;
67 bool has_registers () override
;
68 bool has_execution (inferior
*inf
) override
;
70 /* Default implementation of follow_exec.
72 If the current inferior and FOLLOW_INF are different (execution continues
73 in a new inferior), push this process target to FOLLOW_INF's target stack
74 and add an initial thread to FOLLOW_INF. */
75 void follow_exec (inferior
*follow_inf
, ptid_t ptid
,
76 const char *execd_pathname
) override
;
78 /* Default implementation of follow_fork.
80 If a child inferior was created by infrun while following the fork
81 (CHILD_INF is non-nullptr), push this target on CHILD_INF's target stack
82 and add an initial thread with ptid CHILD_PTID. */
83 void follow_fork (inferior
*child_inf
, ptid_t child_ptid
,
84 target_waitkind fork_kind
, bool follow_child
,
85 bool detach_on_fork
) override
;
87 /* True if any thread is, or may be executing. We need to track
88 this separately because until we fully sync the thread list, we
89 won't know whether the target is fully stopped, even if we see
90 stop events for all known threads, because any of those threads
91 may have spawned new threads we haven't heard of yet. */
92 bool threads_executing
= false;
94 /* If THREAD is resumed and has a pending wait status, add it to the
95 target's "resumed with pending wait status" list. */
96 void maybe_add_resumed_with_pending_wait_status (thread_info
*thread
);
98 /* If THREAD is resumed and has a pending wait status, remove it from the
99 target's "resumed with pending wait status" list. */
100 void maybe_remove_resumed_with_pending_wait_status (thread_info
*thread
);
102 /* Return true if this target has at least one resumed thread with a pending
104 bool has_resumed_with_pending_wait_status () const
105 { return !m_resumed_with_pending_wait_status
.empty (); }
107 /* Return a random resumed thread with pending wait status belonging to INF
108 and matching FILTER_PTID. */
109 thread_info
*random_resumed_with_pending_wait_status
110 (inferior
*inf
, ptid_t filter_ptid
);
112 /* Search function to lookup a (non-exited) thread by 'ptid'. */
113 thread_info
*find_thread (ptid_t ptid
);
115 /* The connection number. Visible in "info connections". */
116 int connection_number
= 0;
118 /* Whether resumed threads must be committed to the target.
120 When true, resumed threads must be committed to the execution
123 When false, the target may leave resumed threads stopped when
124 it's convenient or efficient to do so. When the core requires
125 resumed threads to be committed again, this is set back to true
126 and calls the `commit_resumed` method to allow the target to do
129 To simplify the implementation of targets, the following methods
130 are guaranteed to be called with COMMIT_RESUMED_STATE set to
137 Knowing this, the target doesn't need to implement different
138 behaviors depending on the COMMIT_RESUMED_STATE, and can simply
139 assume that it is false.
141 Targets can take advantage of this to batch resumption requests,
142 for example. In that case, the target doesn't actually resume in
143 its `resume` implementation. Instead, it takes note of the
144 resumption intent in `resume` and defers the actual resumption to
145 `commit_resumed`. For example, the remote target uses this to
146 coalesce multiple resumption requests in a single vCont
148 bool commit_resumed_state
= false;
151 /* List of threads managed by this target which simultaneously are resumed
152 and have a pending wait status.
154 This is done for optimization reasons, it would be possible to walk the
155 inferior thread lists to find these threads. But since this is something
156 we need to do quite frequently in the hot path, maintaining this list
157 avoids walking the thread lists repeatedly. */
158 thread_info_resumed_with_pending_wait_status_list
159 m_resumed_with_pending_wait_status
;
162 /* Downcast TARGET to process_stratum_target. */
164 static inline process_stratum_target
*
165 as_process_stratum_target (target_ops
*target
)
167 gdb_assert (target
->stratum () == process_stratum
);
168 return gdb::checked_static_cast
<process_stratum_target
*> (target
);
171 /* Return a collection of targets that have non-exited inferiors. */
173 extern std::set
<process_stratum_target
*> all_non_exited_process_targets ();
175 /* Switch to the first inferior (and program space) of TARGET, and
176 switch to no thread selected. */
178 extern void switch_to_target_no_thread (process_stratum_target
*target
);
180 #endif /* !defined (PROCESS_STRATUM_TARGET_H) */