gdb: generalize commit_resume, avoid commit-resuming when threads have pending statuses
[binutils-gdb.git] / gdb / process-stratum-target.h
1 /* Abstract base class inherited by all process_stratum targets
2
3 Copyright (C) 2018-2021 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
26 /* Abstract base class inherited by all process_stratum targets. */
27
28 class process_stratum_target : public target_ops
29 {
30 public:
31 ~process_stratum_target () override = 0;
32
33 strata stratum () const final override { return process_stratum; }
34
35 /* Return a string representation of this target's open connection.
36 This string is used to distinguish different instances of a given
37 target type. For example, when remote debugging, the target is
38 called "remote", but since we may have more than one remote
39 target open, connection_string() returns the connection serial
40 connection name, e.g., "localhost:10001", "192.168.0.1:20000",
41 etc. This string is shown in several places, e.g., in "info
42 connections" and "info inferiors". */
43 virtual const char *connection_string () { return nullptr; }
44
45 /* We must default these because they must be implemented by any
46 target that can run. */
47 bool can_async_p () override { return false; }
48 bool supports_non_stop () override { return false; }
49 bool supports_disable_randomization () override { return false; }
50
51 /* This default implementation returns the inferior's address
52 space. */
53 struct address_space *thread_address_space (ptid_t ptid) override;
54
55 /* This default implementation always returns target_gdbarch (). */
56 struct gdbarch *thread_architecture (ptid_t ptid) override;
57
58 /* Default implementations for process_stratum targets. Return true
59 if there's a selected inferior, false otherwise. */
60 bool has_all_memory () override;
61 bool has_memory () override;
62 bool has_stack () override;
63 bool has_registers () override;
64 bool has_execution (inferior *inf) override;
65
66 /* True if any thread is, or may be executing. We need to track
67 this separately because until we fully sync the thread list, we
68 won't know whether the target is fully stopped, even if we see
69 stop events for all known threads, because any of those threads
70 may have spawned new threads we haven't heard of yet. */
71 bool threads_executing = false;
72
73 /* The connection number. Visible in "info connections". */
74 int connection_number = 0;
75
76 /* Whether resumed threads must be committed to the target.
77
78 When true, resumed threads must be committed to the execution
79 target.
80
81 When false, the target may leave resumed threads stopped when
82 it's convenient or efficient to do so. When the core requires
83 resumed threads to be committed again, this is set back to true
84 and calls the `commit_resumed` method to allow the target to do
85 so.
86
87 To simplify the implementation of targets, the following methods
88 are guaranteed to be called with COMMIT_RESUMED_STATE set to
89 false:
90
91 - resume
92 - stop
93 - wait
94
95 Knowing this, the target doesn't need to implement different
96 behaviors depending on the COMMIT_RESUMED_STATE, and can simply
97 assume that it is false.
98
99 Targets can take advantage of this to batch resumption requests,
100 for example. In that case, the target doesn't actually resume in
101 its `resume` implementation. Instead, it takes note of the
102 resumption intent in `resume` and defers the actual resumption to
103 `commit_resumed`. For example, the remote target uses this to
104 coalesce multiple resumption requests in a single vCont
105 packet. */
106 bool commit_resumed_state = false;
107 };
108
109 /* Downcast TARGET to process_stratum_target. */
110
111 static inline process_stratum_target *
112 as_process_stratum_target (target_ops *target)
113 {
114 gdb_assert (target->stratum () == process_stratum);
115 return static_cast<process_stratum_target *> (target);
116 }
117
118 /* Return a collection of targets that have non-exited inferiors. */
119
120 extern std::set<process_stratum_target *> all_non_exited_process_targets ();
121
122 /* Switch to the first inferior (and program space) of TARGET, and
123 switch to no thread selected. */
124
125 extern void switch_to_target_no_thread (process_stratum_target *target);
126
127 #endif /* !defined (PROCESS_STRATUM_TARGET_H) */