-
Notifications
You must be signed in to change notification settings - Fork 83
/
gdb.rs
357 lines (313 loc) · 10.2 KB
/
gdb.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use gdbstub::common::{Signal, Tid};
use gdbstub::conn::Connection;
use gdbstub::stub::state_machine::GdbStubStateMachine;
use gdbstub::stub::{GdbStubBuilder, GdbStubError, MultiThreadStopReason};
use gdbstub::target::Target;
use crate::io::SerialRead;
use crate::platform::precursor::gdbuart::GdbUart;
mod breakpoints;
mod current_active_pid;
mod extended_mode;
mod monitor;
mod multi_thread_base;
mod multi_thread_resume;
mod multi_thread_single_step;
mod single_register_access;
mod target;
#[cfg(target_arch = "riscv32")]
#[path = "gdb/riscv.rs"]
mod cpu;
pub struct XousTarget {
pid: Option<xous_kernel::PID>,
inner: cpu::XousTargetInner,
}
pub struct XousDebugState<'a> {
pub target: XousTarget,
pub server: GdbStubStateMachine<'a, XousTarget, crate::platform::precursor::gdbuart::GdbUart>,
}
static mut GDB_STATE: Option<XousDebugState> = None;
static mut GDB_BUFFER: [u8; 4096] = [0u8; 4096];
trait ProcessPid {
fn pid(&self) -> Option<xous_kernel::PID>;
fn take_pid(&mut self) -> Option<xous_kernel::PID>;
}
impl ProcessPid for XousTarget {
fn pid(&self) -> Option<xous_kernel::PID> {
self.pid
}
fn take_pid(&mut self) -> Option<xous_kernel::PID> {
self.pid.take()
}
}
struct MicroRingBuf<const N: usize> {
buffer: [u8; N],
head: usize,
tail: usize,
}
impl<const N: usize> Default for MicroRingBuf<N> {
fn default() -> Self {
MicroRingBuf {
buffer: [0u8; N],
head: 0,
tail: 0,
}
}
}
impl<const N: usize> MicroRingBuf<N> {
// pub fn capacity(&self) -> usize {
// self.buffer.len()
// }
// pub fn len(&self) -> usize {
// self.head.wrapping_sub(self.tail) % N
// }
pub fn is_full(&self) -> bool {
(self.tail.wrapping_sub(1) % N) == self.head
}
pub fn try_push(&mut self, val: u8) -> Result<(), ()> {
if self.is_full() {
return Err(());
}
self.buffer[self.head] = val;
self.head = (self.head + 1) % N;
Ok(())
}
pub fn try_pop(&mut self) -> Option<u8> {
if self.tail == self.head {
return None;
}
let val = self.buffer[self.tail];
self.tail = (self.tail + 1) % N;
Some(val)
}
}
fn receive_irq(uart: &mut GdbUart) {
let mut buffer = MicroRingBuf::<32>::default();
loop {
// Try to fill up the ring buffer with as many characters
// as can fit. This is to compensate for the fact that we do
// all of this processing in an interrupt context, and the
// hardware UART buffer is only a few characters deep.
while !buffer.is_full() {
if let Some(c) = uart.getc() {
buffer.try_push(c).ok();
} else {
break;
}
}
// If there is a character in the buffer, process it. Otherwise,
// we're done.
let Some(c) = buffer.try_pop() else { break };
process_character(c);
// If the GDB server goes away for some reason, reconstitute it
unsafe {
if GDB_STATE.is_none() {
init();
}
}
}
}
impl XousTarget {
pub fn new() -> XousTarget {
XousTarget {
pid: None,
inner: cpu::XousTargetInner::default(),
}
}
}
fn state_can_accept_characters<'a, T: Target + ProcessPid, C: Connection>(
machine: &GdbStubStateMachine<'a, T, C>,
) -> bool {
match machine {
GdbStubStateMachine::Idle(_) | GdbStubStateMachine::Running(_) => true,
GdbStubStateMachine::CtrlCInterrupt(_) | GdbStubStateMachine::Disconnected(_) => false,
}
}
fn ensure_can_accept_characters_inner<'a, T: Target + ProcessPid, C: Connection>(
machine: GdbStubStateMachine<'a, T, C>,
target: &mut T,
recurse_count: usize,
) -> Option<GdbStubStateMachine<'a, T, C>> {
if recurse_count == 0 {
return None;
}
match machine {
GdbStubStateMachine::Idle(_) | GdbStubStateMachine::Running(_) => Some(machine),
GdbStubStateMachine::CtrlCInterrupt(gdb_stm_inner) => {
if let Some(pid) = target.pid() {
crate::services::SystemServices::with_mut(|system_services| {
if let Err(e) = system_services.pause_process_for_debug(pid) {
println!("Unable to pause process {:?} for debug: {:?}", pid, e);
}
});
}
let Ok(new_server) = gdb_stm_inner.interrupt_handled(target, Some(MultiThreadStopReason::Signal(Signal::SIGINT))) else {
return None
};
ensure_can_accept_characters_inner(new_server, target, recurse_count - 1)
}
GdbStubStateMachine::Disconnected(gdb_stm_inner) => {
if let Some(pid) = target.take_pid() {
crate::services::SystemServices::with_mut(|system_services| {
system_services.resume_process_from_debug(pid).unwrap()
});
}
ensure_can_accept_characters_inner(
gdb_stm_inner.return_to_idle(),
target,
recurse_count - 1,
)
}
}
}
fn ensure_can_accept_characters<'a, T: Target + ProcessPid, C: Connection>(
machine: GdbStubStateMachine<'a, T, C>,
target: &mut T,
) -> Option<GdbStubStateMachine<'a, T, C>> {
ensure_can_accept_characters_inner(machine, target, 4)
}
/// Advance the GDB state.
///
/// Two states accept characters:
///
/// GdbStubStateMachine::Idle
/// GdbStubStateMachine::Running
///
/// Two states exist merely to transition to other states:
///
/// GdbStubStateMachine::CtrlCInterrupt
/// GdbStubStateMachine::Disconnected
fn process_character(byte: u8) {
let XousDebugState { mut target, server } = unsafe {
GDB_STATE.take().unwrap_or_else(|| {
init();
GDB_STATE.take().unwrap()
})
};
if !state_can_accept_characters(&server) {
println!("GDB server was not in a state to accept characters");
return;
}
let new_server = match server {
GdbStubStateMachine::Idle(gdb_stm_inner) => {
let Ok(gdb) = gdb_stm_inner.incoming_data(&mut target, byte).map_err(|e| println!("gdbstub error during idle operation: {:?}", e)) else {
return;
};
gdb
}
GdbStubStateMachine::Running(gdb_stm_inner) => {
// If we're here we were running but have stopped now (either
// because we hit Ctrl+c in gdb and hence got a serial interrupt
// or we hit a breakpoint).
match gdb_stm_inner.incoming_data(&mut target, byte) {
Ok(pumped_stm) => pumped_stm,
Err(GdbStubError::TargetError(e)) => {
println!("Target raised a fatal error: {:?}", e);
return;
}
Err(e) => {
println!("gdbstub error in DeferredStopReason.pump: {:?}", e);
return;
}
}
}
_ => {
println!("GDB is in an unexpected state!");
return;
}
};
// If the user just hit Ctrl-C, then remove the pending interrupt that may or may not exist.
if let GdbStubStateMachine::CtrlCInterrupt(_) = &new_server {
target.unpatch_stepi(Tid::new(1).unwrap()).ok();
}
let Some(server) = ensure_can_accept_characters(new_server, &mut target) else {
println!("Couldn't convert GDB into a state that accepts characters");
return;
};
unsafe { GDB_STATE = Some(XousDebugState { target, server }) };
}
pub fn report_stop(_pid: xous_kernel::PID, tid: xous_kernel::TID, _pc: usize) {
let Some(XousDebugState {
mut target,
server: gdb,
}) = (unsafe { GDB_STATE.take() }) else {
println!("No GDB!");
return;
};
target.unpatch_stepi(Tid::new(tid).unwrap()).ok();
let GdbStubStateMachine::Running(inner) = gdb else {
println!("GDB state machine was in an invalid state");
return;
};
let Ok(new_gdb) = inner.report_stop(
&mut target,
MultiThreadStopReason::SignalWithThread {
signal: Signal::EXC_BREAKPOINT,
tid: Tid::new(tid).unwrap(),
}
) else {
println!("Unable to report stop");
return;
};
unsafe {
GDB_STATE = Some(XousDebugState {
target,
server: new_gdb,
})
};
}
pub fn report_terminated(_pid: xous_kernel::PID) {
let Some(XousDebugState {
mut target,
server: gdb,
}) = (unsafe { GDB_STATE.take() }) else {
println!("No GDB!");
return;
};
let new_gdb = match gdb {
GdbStubStateMachine::Running(inner) => {
println!("Reporting a STOP");
match inner.report_stop(
&mut target,
MultiThreadStopReason::Signal(Signal::EXC_BAD_ACCESS),
) {
Ok(new_gdb) => new_gdb,
Err(e) => {
println!("Unable to report stop: {:?}", e);
return;
}
}
}
GdbStubStateMachine::CtrlCInterrupt(_inner) => {
println!("GDB state was in CtrlCInterrupt, which shouldn't be possible!");
return;
}
GdbStubStateMachine::Disconnected(_inner) => {
println!("GDB state was in Disconnect, which shouldn't be possible!");
return;
}
GdbStubStateMachine::Idle(_inner) => {
println!("GDB state was in Idle, which shouldn't be possible!");
return;
}
};
unsafe {
GDB_STATE = Some(XousDebugState {
target,
server: new_gdb,
})
};
}
pub fn init() {
let mut uart = GdbUart::new(receive_irq).unwrap();
uart.enable();
let mut target = XousTarget::new();
let server = GdbStubBuilder::new(uart)
.with_packet_buffer(unsafe { &mut GDB_BUFFER })
.build()
.expect("unable to build gdb server")
.run_state_machine(&mut target)
.expect("unable to start gdb state machine");
unsafe {
GDB_STATE = Some(XousDebugState { target, server });
}
}