A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
gdbstub: forward interrupt requests to the debuggee. (#14776)
Chris Fallind8c04f2faad3
diff --git a/crates/gdbstub-component/src/lib.rs b/crates/gdbstub-component/src/lib.rsindex bcc929b2bb..de717b5188 100644--- a/crates/gdbstub-component/src/lib.rs+++ b/crates/gdbstub-component/src/lib.rs@@ -164,6 +164,11 @@ impl<'a> Debugger<'a> {// Connection error: break.break 'mainloop;}+ // Ask the debuggee to stop; its `interrupted` event is+ // then reported as the stop (see `handle_event`).+ trace!("Ctrl-C: interrupting debuggee");+ self.debuggee.interrupt();+ self.interrupt = true;stub = inner.interrupt_handled(self, None::<MultiThreadStopReason<u64>>)?;}GdbStubStateMachine::Disconnected(mut inner) => {@@ -193,6 +198,8 @@ impl<'a> Debugger<'a> {}fn update_on_stop(&mut self) {+ // Any stop satisfies a pending interrupt request.+ self.interrupt = false;self.addr_space.update(self.debuggee).unwrap();// Cache all frame handles for the duration of this stop.diff --git a/tests/all/guest_debug/mod.rs b/tests/all/guest_debug/mod.rsindex a7c157f1b5..c51e049126 100644--- a/tests/all/guest_debug/mod.rs+++ b/tests/all/guest_debug/mod.rs@@ -512,3 +512,107 @@ check: handleOk(())}++/// A minimal GDB remote serial protocol client, for tests that need+/// precise control over the packets sent.+struct RspClient {+ tcp: TcpStream,+}++impl RspClient {+ fn connect(port: u16) -> Result<Self> {+ let tcp = TcpStream::connect(("127.0.0.1", port))?;+ tcp.set_read_timeout(Some(Duration::from_secs(30)))?;+ Ok(Self { tcp })+ }++ /// Send a packet without waiting for a reply.+ fn send(&mut self, payload: &str) -> Result<()> {+ let checksum = payload.bytes().fold(0u8, |a, b| a.wrapping_add(b));+ write!(self.tcp, "${payload}#{checksum:02x}")?;+ Ok(())+ }++ /// Send a packet and return the reply packet's payload.+ fn request(&mut self, payload: &str) -> Result<String> {+ self.send(payload)?;+ self.reply(payload)+ }++ /// Read the reply packet to `payload`, returning its payload.+ fn reply(&mut self, payload: &str) -> Result<String> {+ // Skip acks until the reply packet starts.+ let mut byte = [0u8];+ loop {+ std::io::Read::read_exact(&mut self.tcp, &mut byte)?;+ match byte[0] {+ b'+' => continue,+ b'$' => break,+ other => bail!("unexpected byte {other:#x} before reply to {payload}"),+ }+ }+ let mut reply = vec![];+ loop {+ std::io::Read::read_exact(&mut self.tcp, &mut byte)?;+ if byte[0] == b'#' {+ break;+ }+ reply.push(byte[0]);+ }+ let mut checksum = [0u8; 2];+ std::io::Read::read_exact(&mut self.tcp, &mut checksum)?;+ self.tcp.write_all(b"+")?;+ let reply = String::from_utf8(reply)?;+ eprintln!("RSP: {payload} -> {reply}");+ Ok(reply)+ }+}++/// A long loop with no host calls, which only an interrupt can stop early.+const SPIN_WAT: &str = r#"+(module+ (func (export "_start")+ (local $i i32) (local $j i32)+ loop+ (local.set $j (i32.const 0))+ loop+ (local.set $j (i32.add (local.get $j) (i32.const 1)))+ (br_if 0 (i32.lt_u (local.get $j) (i32.const 1000000000)))+ end+ (local.set $i (i32.add (local.get $i) (i32.const 1)))+ (br_if 0 (i32.lt_u (local.get $i) (i32.const 8)))+ end))+"#;++/// Continues `SPIN_WAT` under `wasmtime <subcmd> -g` with `args`, then+/// interrupts it, expecting a stop rather than an exit.+fn interrupt_spin(subcmd: &str, args: &[&str]) -> Result<()> {+ let port = free_port();+ let mut wt = WasmtimeWithGdbstub::spawn(subcmd, port, args, Duration::from_secs(30))?;+ let result = (|| -> Result<()> {+ let mut rsp = RspClient::connect(port)?;+ rsp.request("?")?;+ rsp.send("c")?;+ std::thread::sleep(Duration::from_millis(200));+ rsp.tcp.write_all(&[0x03])?;+ let reply = rsp.reply("interrupt")?;+ assert!(+ reply.starts_with('T') || reply.starts_with('S'),+ "unexpected interrupt reply: {reply}"+ );+ Ok(())+ })();+ wt.child.kill().ok();+ wt.child.wait()?;+ result+}++/// Test that a debugger can interrupt a running program.+#[test]+#[ignore]+fn guest_debug_cli_interrupt() -> Result<()> {+ let dir = tempfile::tempdir()?;+ let wat = dir.path().join("spin.wat");+ std::fs::write(&wat, SPIN_WAT)?;+ interrupt_spin("run", &["-Ccache=n", wat.to_str().unwrap()])+}