-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
core_impl.rs
277 lines (247 loc) · 10.3 KB
/
core_impl.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use core::marker::PhantomData;
use crate::common::{Signal, Tid};
use crate::conn::Connection;
use crate::protocol::commands::Command;
use crate::protocol::{Packet, ResponseWriter, SpecificIdKind};
use crate::stub::GdbStubError as Error;
use crate::target::Target;
use crate::SINGLE_THREAD_TID;
/// Common imports used by >50% of all extensions.
///
/// Do not clutter this prelude with types only used by a few extensions.
mod prelude {
pub(super) use crate::conn::Connection;
pub(super) use crate::internal::BeBytes;
pub(super) use crate::protocol::ResponseWriter;
pub(super) use crate::stub::core_impl::target_result_ext::TargetResultExt;
pub(super) use crate::stub::core_impl::{GdbStubImpl, HandlerStatus};
pub(super) use crate::stub::error::GdbStubError as Error;
pub(super) use crate::target::Target;
}
mod auxv;
mod base;
mod breakpoints;
mod catch_syscalls;
mod exec_file;
mod extended_mode;
mod host_io;
mod lldb_register_info;
mod memory_map;
mod monitor_cmd;
mod resume;
mod reverse_exec;
mod section_offsets;
mod single_register_access;
mod target_xml;
mod thread_extra_info;
mod x_upcase_packet;
pub(crate) use resume::FinishExecStatus;
pub(crate) mod target_result_ext {
use crate::stub::GdbStubError;
use crate::target::TargetError;
/// Extension trait to ease working with `TargetResult` in the GdbStub
/// implementation.
pub(super) trait TargetResultExt<V, T, C> {
/// Encapsulates the boilerplate associated with handling
/// `TargetError`s, such as bailing-out on Fatal errors, or
/// returning response codes.
fn handle_error(self) -> Result<V, GdbStubError<T, C>>;
}
impl<V, T, C> TargetResultExt<V, T, C> for Result<V, TargetError<T>> {
fn handle_error(self) -> Result<V, GdbStubError<T, C>> {
let code = match self {
Ok(v) => return Ok(v),
Err(TargetError::Fatal(e)) => return Err(GdbStubError::TargetError(e)),
// Recoverable errors:
// Error code 121 corresponds to `EREMOTEIO` lol
Err(TargetError::NonFatal) => 121,
Err(TargetError::Errno(code)) => code,
#[cfg(feature = "std")]
Err(TargetError::Io(e)) => e.raw_os_error().unwrap_or(121) as u8,
};
Err(GdbStubError::NonFatalError(code))
}
}
}
/// Describes why the GDB session ended.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DisconnectReason {
/// Target exited with given status code
TargetExited(u8),
/// Target terminated with given signal
TargetTerminated(Signal),
/// GDB issued a disconnect command
Disconnect,
/// GDB issued a kill command
Kill,
}
pub enum State {
Pump,
DeferredStopReason,
CtrlCInterrupt,
Disconnect(DisconnectReason),
}
pub struct GdbStubImpl<T: Target, C: Connection> {
_target: PhantomData<T>,
_connection: PhantomData<C>,
current_mem_tid: Tid,
current_resume_tid: SpecificIdKind,
features: ProtocolFeatures,
}
pub enum HandlerStatus {
Handled,
NeedsOk,
DeferredStopReason,
Disconnect(DisconnectReason),
}
impl<T: Target, C: Connection> GdbStubImpl<T, C> {
pub fn new() -> GdbStubImpl<T, C> {
GdbStubImpl {
_target: PhantomData,
_connection: PhantomData,
// NOTE: `current_mem_tid` and `current_resume_tid` are never queried prior to being set
// by the GDB client (via the 'H' packet), so it's fine to use dummy values here.
//
// The alternative would be to use `Option`, and while this would be more "correct", it
// would introduce a _lot_ of noisy and heavy error handling logic all over the place.
//
// Plus, even if the GDB client is acting strangely and doesn't overwrite these values,
// the target will simply return a non-fatal error, which is totally fine.
current_mem_tid: SINGLE_THREAD_TID,
current_resume_tid: SpecificIdKind::WithId(SINGLE_THREAD_TID),
features: ProtocolFeatures::empty(),
}
}
pub fn handle_packet(
&mut self,
target: &mut T,
conn: &mut C,
packet: Packet<'_>,
) -> Result<State, Error<T::Error, C::Error>> {
match packet {
Packet::Ack => Ok(State::Pump),
Packet::Nack => Err(Error::ClientSentNack),
Packet::Interrupt => {
debug!("<-- interrupt packet");
Ok(State::CtrlCInterrupt)
}
Packet::Command(command) => {
// Acknowledge the command
if !self.features.no_ack_mode() {
conn.write(b'+').map_err(Error::ConnectionWrite)?;
}
let mut res = ResponseWriter::new(conn, target.use_rle());
let disconnect_reason = match self.handle_command(&mut res, target, command) {
Ok(HandlerStatus::Handled) => None,
Ok(HandlerStatus::NeedsOk) => {
res.write_str("OK")?;
None
}
Ok(HandlerStatus::DeferredStopReason) => return Ok(State::DeferredStopReason),
Ok(HandlerStatus::Disconnect(reason)) => Some(reason),
// HACK: handling this "dummy" error is required as part of the
// `TargetResultExt::handle_error()` machinery.
Err(Error::NonFatalError(code)) => {
res.write_str("E")?;
res.write_num(code)?;
None
}
Err(e) => return Err(e),
};
// every response needs to be flushed, _except_ for the response to a kill
// packet, but ONLY when extended mode is NOT implemented.
let is_kill = matches!(disconnect_reason, Some(DisconnectReason::Kill));
if !(target.support_extended_mode().is_none() && is_kill) {
res.flush()?;
}
let state = match disconnect_reason {
Some(reason) => State::Disconnect(reason),
None => State::Pump,
};
Ok(state)
}
}
}
fn handle_command(
&mut self,
res: &mut ResponseWriter<'_, C>,
target: &mut T,
cmd: Command<'_>,
) -> Result<HandlerStatus, Error<T::Error, C::Error>> {
match cmd {
// `handle_X` methods are defined in the `ext` module
Command::Base(cmd) => self.handle_base(res, target, cmd),
Command::TargetXml(cmd) => self.handle_target_xml(res, target, cmd),
Command::Resume(cmd) => self.handle_stop_resume(res, target, cmd),
Command::XUpcasePacket(cmd) => self.handle_x_upcase_packet(res, target, cmd),
Command::SingleRegisterAccess(cmd) => {
self.handle_single_register_access(res, target, cmd)
}
Command::Breakpoints(cmd) => self.handle_breakpoints(res, target, cmd),
Command::CatchSyscalls(cmd) => self.handle_catch_syscalls(res, target, cmd),
Command::ExtendedMode(cmd) => self.handle_extended_mode(res, target, cmd),
Command::MonitorCmd(cmd) => self.handle_monitor_cmd(res, target, cmd),
Command::SectionOffsets(cmd) => self.handle_section_offsets(res, target, cmd),
Command::ReverseCont(cmd) => self.handle_reverse_cont(res, target, cmd),
Command::ReverseStep(cmd) => self.handle_reverse_step(res, target, cmd),
Command::MemoryMap(cmd) => self.handle_memory_map(res, target, cmd),
Command::HostIo(cmd) => self.handle_host_io(res, target, cmd),
Command::ExecFile(cmd) => self.handle_exec_file(res, target, cmd),
Command::Auxv(cmd) => self.handle_auxv(res, target, cmd),
Command::ThreadExtraInfo(cmd) => self.handle_thread_extra_info(res, target, cmd),
Command::LldbRegisterInfo(cmd) => self.handle_lldb_register_info(res, target, cmd),
// in the worst case, the command could not be parsed...
Command::Unknown(cmd) => {
// HACK: if the user accidentally sends a resume command to a
// target without resume support, inform them of their mistake +
// return a dummy stop reason.
if target.base_ops().resume_ops().is_none() && target.use_resume_stub() {
let is_resume_pkt = cmd
.first()
.map(|c| matches!(c, b'c' | b'C' | b's' | b'S'))
.unwrap_or(false);
if is_resume_pkt {
warn!("attempted to resume target without resume support!");
// TODO: omit this message if non-stop mode is active
{
let mut res = ResponseWriter::new(res.as_conn(), target.use_rle());
res.write_str("O")?;
res.write_hex_buf(b"target has not implemented `support_resume()`\n")?;
res.flush()?;
}
res.write_str("S05")?;
}
}
info!("Unknown command: {:?}", core::str::from_utf8(cmd));
Ok(HandlerStatus::Handled)
}
}
}
}
// This bitflag is not part of the protocol - it is an internal implementation
// detail. The alternative would be to use multiple `bool` fields, which wastes
// space in minimal `gdbstub` configurations.
bitflags::bitflags! {
struct ProtocolFeatures: u8 {
const NO_ACK_MODE = 1 << 0;
const MULTIPROCESS = 1 << 1;
}
}
impl ProtocolFeatures {
#[inline(always)]
fn no_ack_mode(&self) -> bool {
self.contains(ProtocolFeatures::NO_ACK_MODE)
}
#[inline(always)]
fn set_no_ack_mode(&mut self, val: bool) {
self.set(ProtocolFeatures::NO_ACK_MODE, val)
}
#[inline(always)]
fn multiprocess(&self) -> bool {
self.contains(ProtocolFeatures::MULTIPROCESS)
}
#[inline(always)]
fn set_multiprocess(&mut self, val: bool) {
self.set(ProtocolFeatures::MULTIPROCESS, val)
}
}