-
Notifications
You must be signed in to change notification settings - Fork 85
/
gdb_server.rs
402 lines (364 loc) · 13.1 KB
/
gdb_server.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use gdbstub::common::Tid;
use gdbstub::target::ext::base::multithread::{
GdbInterrupt, MultiThreadOps, ResumeAction, ThreadStopReason,
};
use gdbstub::{DisconnectReason, GdbStubError};
use gdbstub::state_machine::GdbStubStateMachine;
use gdbstub::target::ext::base::BaseOps;
use gdbstub::target::{Target, TargetResult};
pub struct XousTarget {
pid: Option<xous_kernel::PID>,
}
pub struct XousDebugState<'a> {
pub target: XousTarget,
pub server: GdbStubStateMachine<'a, XousTarget, super::Uart>,
}
pub static mut GDB_STATE: Option<XousDebugState> = None;
pub static mut GDB_BUFFER: [u8; 4096] = [0u8; 4096];
impl XousTarget {
pub fn new() -> XousTarget {
XousTarget {
// pid: Some(crate::services::SystemServices::with_mut(
// |system_services| system_services.current_pid(),
pid: xous_kernel::PID::new(2),
}
}
pub fn pid(&self) -> &Option<xous_kernel::PID> {
&self.pid
}
}
impl Target for XousTarget {
type Arch = gdbstub_arch::riscv::Riscv32;
type Error = &'static str;
fn base_ops(&mut self) -> BaseOps<Self::Arch, Self::Error> {
BaseOps::MultiThread(self)
}
fn breakpoints(&mut self) -> Option<gdbstub::target::ext::breakpoints::BreakpointsOps<Self>> {
Some(self)
}
}
impl MultiThreadOps for XousTarget {
fn resume(
&mut self,
default_resume_action: ResumeAction,
_gdb_interrupt: GdbInterrupt<'_>,
) -> Result<Option<ThreadStopReason<u32>>, Self::Error> {
unsafe { HALTED = false };
match default_resume_action {
ResumeAction::Step | ResumeAction::StepWithSignal(_) => {
return Err("single-stepping not supported")
}
_ => (),
}
crate::services::SystemServices::with_mut(|system_services| {
system_services.continue_process(self.pid.unwrap()).unwrap()
});
Ok(None)
}
fn clear_resume_actions(&mut self) -> Result<(), Self::Error> {
Ok(())
}
fn set_resume_action(&mut self, _tid: Tid, action: ResumeAction) -> Result<(), Self::Error> {
match action {
ResumeAction::Step | ResumeAction::StepWithSignal(_) => {
Err("single-stepping resume action not supported")
}
ResumeAction::Continue | ResumeAction::ContinueWithSignal(_) => Ok(()),
}
}
fn read_registers(
&mut self,
regs: &mut gdbstub_arch::riscv::reg::RiscvCoreRegs<u32>,
tid: Tid,
) -> TargetResult<(), Self> {
crate::services::SystemServices::with(|system_services| {
let current_pid = system_services.current_pid();
// Actiavte the debugging process and iterate through it,
// noting down each active thread.
let debugging_pid = self.pid.unwrap();
system_services
.get_process(debugging_pid)
.unwrap()
.activate()
.unwrap();
let process = crate::arch::process::Process::current();
let thread = process.thread(tid.get());
regs.x[0] = 0;
for (dbg_reg, thr_reg) in regs.x[1..].iter_mut().zip(thread.registers.iter()) {
*dbg_reg = (*thr_reg) as u32;
}
regs.pc = (thread.sepc) as u32;
// Restore the previous PID
system_services
.get_process(current_pid)
.unwrap()
.activate()
.unwrap();
});
Ok(())
}
fn write_registers(
&mut self,
regs: &gdbstub_arch::riscv::reg::RiscvCoreRegs<u32>,
tid: Tid,
) -> TargetResult<(), Self> {
crate::services::SystemServices::with(|system_services| {
let current_pid = system_services.current_pid();
// Actiavte the debugging process and iterate through it,
// noting down each active thread.
let debugging_pid = self.pid.unwrap();
system_services
.get_process(debugging_pid)
.unwrap()
.activate()
.unwrap();
let mut process = crate::arch::process::Process::current();
let thread = process.thread_mut(tid.get());
for (thr_reg, dbg_reg) in thread.registers.iter_mut().zip(regs.x[1..].iter()) {
*thr_reg = (*dbg_reg) as usize;
}
thread.sepc = (regs.pc) as usize;
// Restore the previous PID
system_services
.get_process(current_pid)
.unwrap()
.activate()
.unwrap();
});
Ok(())
}
fn read_addrs(
&mut self,
start_addr: u32,
data: &mut [u8],
_tid: Tid, // same address space for each core
) -> TargetResult<(), Self> {
let mut current_addr = start_addr;
crate::services::SystemServices::with(|system_services| {
let current_pid = system_services.current_pid();
// Actiavte the debugging process and iterate through it,
// noting down each active thread.
let debugging_pid = self.pid.unwrap();
system_services
.get_process(debugging_pid)
.unwrap()
.activate()
.unwrap();
data.iter_mut().for_each(|b| {
*b = crate::arch::mem::peek_memory(current_addr as *mut u8).unwrap_or(0xff);
current_addr += 1;
});
// Restore the previous PID
system_services
.get_process(current_pid)
.unwrap()
.activate()
.unwrap();
});
Ok(())
}
fn write_addrs(
&mut self,
start_addr: u32,
data: &[u8],
_tid: Tid, // same address space for each core
) -> TargetResult<(), Self> {
let mut current_addr = start_addr;
// gprintln!("Writing data to {:08x}", start_addr);
crate::services::SystemServices::with(|system_services| {
let current_pid = system_services.current_pid();
// Actiavte the debugging process and iterate through it,
// noting down each active thread.
let debugging_pid = self.pid.unwrap();
system_services
.get_process(debugging_pid)
.unwrap()
.activate()
.unwrap();
data.iter().for_each(|b| {
if let Err(_e) = crate::arch::mem::poke_memory(current_addr as *mut u8, *b) {
panic!("couldn't poke memory: {:?}", _e);
// gprintln!("Error writing to {:08x}: {:?}", current_addr, e);
}
current_addr += 1;
});
// Restore the previous PID
system_services
.get_process(current_pid)
.unwrap()
.activate()
.unwrap();
});
Ok(())
}
fn list_active_threads(
&mut self,
register_thread: &mut dyn FnMut(Tid),
) -> Result<(), Self::Error> {
crate::services::SystemServices::with(|system_services| {
let current_pid = system_services.current_pid();
let debugging_pid = self.pid.unwrap();
// Actiavte the debugging process and iterate through it,
// noting down each active thread.
system_services
.get_process(debugging_pid)
.unwrap()
.activate()
.unwrap();
crate::arch::process::Process::current().for_each_thread_mut(|tid, _thr| {
println!("Registering thread {}", tid);
register_thread(Tid::new(tid).unwrap());
});
// Restore the previous PID
system_services
.get_process(current_pid)
.unwrap()
.activate()
.unwrap();
});
Ok(())
}
}
impl gdbstub::target::ext::breakpoints::Breakpoints for XousTarget {
fn hw_breakpoint(
&mut self,
) -> Option<gdbstub::target::ext::breakpoints::HwBreakpointOps<Self>> {
Some(self)
}
}
impl gdbstub::target::ext::breakpoints::HwBreakpoint for XousTarget {
fn add_hw_breakpoint(&mut self, _addr: u32, _kind: usize) -> TargetResult<bool, Self> {
Ok(false)
}
fn remove_hw_breakpoint(&mut self, _addr: u32, _kind: usize) -> TargetResult<bool, Self> {
Ok(false)
}
}
pub fn handle(b: u8) -> bool {
if let Some(XousDebugState {
mut target,
server: gdb,
}) = unsafe { GDB_STATE.take() }
{
// gprintln!("Adding char: {}", b as char);
let new_gdb = match gdb {
GdbStubStateMachine::Pump(gdb_state) => match gdb_state.pump(&mut target, b) {
// Remote disconnected -- leave the `GDB_SERVER` as `None`.
Ok((gdb, Some(DisconnectReason::TargetTerminated(_r)))) => {
// gprintln!("Target halted: {}", r);
crate::services::SystemServices::with_mut(|system_services| {
system_services
.suspend_process(target.pid().unwrap())
.unwrap()
});
gdb
}
Ok((_, Some(_disconnect_reason))) => {
cleanup();
match _disconnect_reason {
DisconnectReason::Disconnect => println!("GDB Disconnected"),
DisconnectReason::TargetExited(_) => println!("Target exited"),
DisconnectReason::TargetTerminated(_) => unreachable!(),
DisconnectReason::Kill => println!("GDB sent a kill command"),
}
return true;
}
Err(GdbStubError::TargetError(e)) => {
cleanup();
println!("Target raised a fatal error: {}", e);
return true;
}
Err(e) => {
cleanup();
println!("gdbstub internal error: {}", e);
return true;
}
Ok((gdb, None)) => gdb,
},
GdbStubStateMachine::DeferredStopReason(gdb_state) => {
match gdb_state.deferred_stop_reason(&mut target, ThreadStopReason::DoneStep) {
Ok((gdb, None)) => {
crate::services::SystemServices::with_mut(|system_services| {
system_services
.suspend_process(target.pid().unwrap())
.unwrap()
});
gdb
}
Ok((_, Some(disconnect_reason))) => {
cleanup();
println!("client disconnected: {:?}", disconnect_reason);
return true;
}
Err(e) => {
cleanup();
println!("deferred_stop_reason_error: {:?}", e);
return true;
}
}
}
};
unsafe {
GDB_STATE = Some(XousDebugState {
target,
server: new_gdb,
})
};
true
} else {
false
}
}
pub fn setup() {
match gdbstub::GdbStubBuilder::new(super::Uart {})
.with_packet_buffer(unsafe { &mut GDB_BUFFER })
.build()
{
Ok(gdb) => match gdb.run_state_machine() {
Ok(state) => unsafe {
GDB_STATE = Some(XousDebugState {
target: XousTarget::new(),
server: state,
});
super::DEBUG_OUTPUT = Some(&mut GUART);
HALTED = true;
},
Err(e) => println!("Unable to start GDB state machine: {}", e),
},
Err(e) => println!("Unable to start GDB server: {}", e),
}
}
fn cleanup() {
unsafe { super::DEBUG_OUTPUT = Some(&mut super::UART) };
}
// Support printf() when running under a debugger
pub struct GUart {}
static mut GUART: GUart = GUart {};
static mut HALTED: bool = false;
impl core::fmt::Write for GUart {
fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> {
fn putc(checksum: u8, c: u8) -> u8 {
unsafe { super::UART.putc(c) };
checksum.wrapping_add(c)
}
fn puth(checksum: u8, c: u8) -> u8 {
let hex = b"0123456789abcdef";
let c = c as usize;
let checksum = putc(checksum, hex[(c >> 4) & 0xf]);
putc(checksum, hex[c & 0xf])
}
// Can't write when we're halted
if unsafe { HALTED } {
return Ok(());
}
let mut checksum = 0u8;
putc(0, b'$');
checksum = putc(checksum, b'O');
for c in s.as_bytes() {
checksum = puth(checksum, *c);
}
putc(0, b'#');
puth(0, checksum);
Ok(())
}
}