use std::io::{BufReader, Read, Write};
use std::process::{Child, Command, Stdio};
use std::sync::Mutex;
use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::mpsc::{Receiver, Sender};
use anyhow::{Context, Result};
use serde_json::Value;
const CONTENT_LENGTH: &str = "Content-Length";
const HEADER_SEP: &[u8] = b"\r\n\r\n";
pub fn encode(message: &Value) -> Vec<u8> {
let body = serde_json::to_vec(message).unwrap_or_default();
let mut out = format!("{CONTENT_LENGTH}: {}\r\n\r\n", body.len()).into_bytes();
out.extend_from_slice(&body);
out
}
#[derive(Default)]
pub struct FrameDecoder {
buf: Vec<u8>,
}
impl FrameDecoder {
pub fn new() -> Self {
Self { buf: Vec::new() }
}
pub fn feed(&mut self, bytes: &[u8]) {
self.buf.extend_from_slice(bytes);
}
pub fn next_message(&mut self) -> Option<Value> {
let sep = find_subslice(&self.buf, HEADER_SEP)?;
let header = std::str::from_utf8(&self.buf[..sep]).ok()?;
let len = content_length(header)?;
let body_start = sep + HEADER_SEP.len();
let body_end = body_start + len;
if self.buf.len() < body_end {
return None; }
let value = serde_json::from_slice(&self.buf[body_start..body_end]).ok();
self.buf.drain(..body_end);
value
}
}
fn content_length(header: &str) -> Option<usize> {
header.lines().find_map(|line| {
let (key, val) = line.split_once(':')?;
if key.trim().eq_ignore_ascii_case(CONTENT_LENGTH) {
val.trim().parse().ok()
} else {
None
}
})
}
fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
if needle.is_empty() || haystack.len() < needle.len() {
return None;
}
(0..=haystack.len() - needle.len()).find(|&i| &haystack[i..i + needle.len()] == needle)
}
pub struct DapTransport {
child: Option<Child>,
writer: Mutex<Box<dyn Write + Send>>,
seq: AtomicI64,
pub incoming: Receiver<Value>,
}
impl DapTransport {
pub fn spawn(program: &str, args: &[String], cwd: &std::path::Path) -> Result<DapTransport> {
use std::os::unix::process::CommandExt;
let mut cmd = Command::new(program);
cmd.args(args)
.current_dir(cwd)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
unsafe {
cmd.pre_exec(|| {
libc::setsid();
Ok(())
});
}
let mut child = cmd
.spawn()
.with_context(|| format!("spawning debug adapter `{program}`"))?;
let stdin = child.stdin.take().context("adapter stdin missing")?;
let stdout = child.stdout.take().context("adapter stdout missing")?;
if let Some(stderr) = child.stderr.take() {
std::thread::Builder::new()
.name("dap-stderr".into())
.spawn(move || {
use std::io::BufRead;
for line in std::io::BufReader::new(stderr)
.lines()
.map_while(Result::ok)
{
crate::output::push(
crate::output::CHANNEL_DAP,
crate::output::OutputLevel::Info,
&line,
);
}
})
.ok();
}
let (tx, rx): (Sender<Value>, Receiver<Value>) = std::sync::mpsc::channel();
std::thread::Builder::new()
.name("dap-reader".into())
.spawn(move || reader_loop(stdout, tx))
.context("spawning dap-reader thread")?;
Ok(DapTransport {
child: Some(child),
writer: Mutex::new(Box::new(stdin)),
seq: AtomicI64::new(0),
incoming: rx,
})
}
pub fn connect_tcp_server(
program: &str,
args: &[String],
cwd: &std::path::Path,
host: &str,
port: u16,
path_prepend: Option<&std::path::Path>,
) -> Result<DapTransport> {
use std::os::unix::process::CommandExt;
let mut cmd = Command::new(program);
cmd.args(args)
.current_dir(cwd)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
if let Some(dir) = path_prepend {
let existing = std::env::var("PATH").unwrap_or_default();
cmd.env("PATH", format!("{}:{existing}", dir.display()));
}
unsafe {
cmd.pre_exec(|| {
libc::setsid();
Ok(())
});
}
let child = cmd
.spawn()
.with_context(|| format!("spawning debug server `{program}`"))?;
let stream = connect_with_retry(host, port)?;
Self::from_stream(stream, Some(child))
}
pub fn connect_tcp(host: &str, port: u16) -> Result<DapTransport> {
let stream = connect_with_retry(host, port)?;
Self::from_stream(stream, None)
}
fn from_stream(stream: std::net::TcpStream, child: Option<Child>) -> Result<DapTransport> {
let reader = stream.try_clone().context("cloning DAP socket for reads")?;
let writer = stream;
let (tx, rx): (Sender<Value>, Receiver<Value>) = std::sync::mpsc::channel();
std::thread::Builder::new()
.name("dap-reader".into())
.spawn(move || reader_loop(reader, tx))
.context("spawning dap-reader thread")?;
Ok(DapTransport {
child,
writer: Mutex::new(Box::new(writer)),
seq: AtomicI64::new(0),
incoming: rx,
})
}
pub fn next_seq(&self) -> i64 {
self.seq.fetch_add(1, Ordering::SeqCst) + 1
}
pub fn send(&self, mut message: Value) -> Result<i64> {
let seq = self.next_seq();
if let Some(obj) = message.as_object_mut() {
obj.insert("seq".into(), Value::from(seq));
}
let bytes = encode(&message);
super::log::log(&format!(
"send seq={seq} {}",
message
.get("command")
.and_then(Value::as_str)
.unwrap_or("?")
));
let mut writer = self.writer.lock().expect("dap writer mutex poisoned");
writer.write_all(&bytes).context("writing to adapter")?;
writer.flush().context("flushing adapter")?;
Ok(seq)
}
pub fn kill(&mut self) {
if let Some(child) = self.child.as_mut() {
let pid = child.id() as i32;
unsafe {
libc::killpg(pid, libc::SIGKILL);
}
let _ = child.kill();
let _ = child.wait();
}
}
}
fn connect_with_retry(host: &str, port: u16) -> Result<std::net::TcpStream> {
use std::net::{TcpStream, ToSocketAddrs};
let addr = (host, port)
.to_socket_addrs()
.with_context(|| format!("resolving {host}:{port}"))?
.next()
.with_context(|| format!("no address for {host}:{port}"))?;
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
let mut last_err = None;
while std::time::Instant::now() < deadline {
match TcpStream::connect_timeout(&addr, std::time::Duration::from_millis(500)) {
Ok(stream) => {
let _ = stream.set_nodelay(true);
return Ok(stream);
}
Err(e) => {
last_err = Some(e);
std::thread::sleep(std::time::Duration::from_millis(50));
}
}
}
Err(anyhow::anyhow!(
"could not connect to debug server at {host}:{port}: {}",
last_err
.map(|e| e.to_string())
.unwrap_or_else(|| String::from("timed out"))
))
}
pub fn free_port() -> Result<u16> {
let listener =
std::net::TcpListener::bind("127.0.0.1:0").context("binding to find free port")?;
let port = listener.local_addr().context("reading bound port")?.port();
Ok(port)
}
impl Drop for DapTransport {
fn drop(&mut self) {
self.kill();
}
}
fn reader_loop<R: Read>(source: R, tx: Sender<Value>) {
let mut reader = BufReader::new(source);
let mut decoder = FrameDecoder::new();
let mut chunk = [0u8; 8192];
loop {
match reader.read(&mut chunk) {
Ok(0) | Err(_) => break,
Ok(n) => {
decoder.feed(&chunk[..n]);
while let Some(msg) = decoder.next_message() {
super::log::log(&format!(
"recv type={} key={}",
msg.get("type").and_then(Value::as_str).unwrap_or("?"),
msg.get("event")
.or_else(|| msg.get("command"))
.and_then(Value::as_str)
.unwrap_or("?")
));
if tx.send(msg).is_err() {
return;
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn encode_prepends_content_length_header() {
let bytes = encode(&json!({"seq": 1, "type": "request"}));
let text = String::from_utf8(bytes).unwrap();
assert!(text.starts_with("Content-Length: "));
assert!(text.contains("\r\n\r\n"));
let (header, body) = text.split_once("\r\n\r\n").unwrap();
let declared: usize = header
.strip_prefix("Content-Length: ")
.unwrap()
.trim()
.parse()
.unwrap();
assert_eq!(declared, body.len());
}
#[test]
fn decodes_a_single_message() {
let mut d = FrameDecoder::new();
d.feed(&encode(&json!({"type": "event", "event": "stopped"})));
let msg = d.next_message().unwrap();
assert_eq!(msg["event"], "stopped");
assert!(d.next_message().is_none());
}
#[test]
fn decodes_two_messages_in_one_feed() {
let mut d = FrameDecoder::new();
let mut bytes = encode(&json!({"seq": 1}));
bytes.extend(encode(&json!({"seq": 2})));
d.feed(&bytes);
assert_eq!(d.next_message().unwrap()["seq"], 1);
assert_eq!(d.next_message().unwrap()["seq"], 2);
assert!(d.next_message().is_none());
}
#[test]
fn reassembles_a_message_split_across_feeds() {
let bytes = encode(&json!({"command": "initialize", "type": "request"}));
let split = bytes.len() / 2;
let mut d = FrameDecoder::new();
d.feed(&bytes[..split]);
assert!(d.next_message().is_none(), "incomplete: nothing yet");
d.feed(&bytes[split..]);
assert_eq!(d.next_message().unwrap()["command"], "initialize");
}
#[test]
fn content_length_header_is_case_insensitive() {
assert_eq!(content_length("content-length: 42"), Some(42));
assert_eq!(content_length("Content-Length: 7\r\nX: y"), Some(7));
assert_eq!(content_length("X-Other: 1"), None);
}
#[test]
fn spawned_adapter_is_detached_into_its_own_session() {
let cwd = std::env::temp_dir();
let t =
DapTransport::spawn("sleep", &["3".to_string()], &cwd).expect("spawn stand-in adapter");
let child_pid = t.child.as_ref().expect("stdio adapter owns a child").id() as libc::pid_t;
let child_sid = unsafe { libc::getsid(child_pid) };
let our_sid = unsafe { libc::getsid(0) };
assert!(child_sid > 0, "getsid(child) failed: {child_sid}");
assert_ne!(
child_sid, our_sid,
"adapter must be in its own session, detached from croft's tty"
);
}
}