From 0f6e81b85be6e7e8f08446f3cd373da38dd67360 Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Thu, 12 Aug 2021 16:35:15 +0300 Subject: Initial debug adapter protocol implementation --- helix-dap/Cargo.toml | 20 +++ helix-dap/examples/dap-basic.rs | 51 ++++++ helix-dap/src/client.rs | 352 ++++++++++++++++++++++++++++++++++++++++ helix-dap/src/lib.rs | 21 +++ helix-dap/src/transport.rs | 282 ++++++++++++++++++++++++++++++++ 5 files changed, 726 insertions(+) create mode 100644 helix-dap/Cargo.toml create mode 100644 helix-dap/examples/dap-basic.rs create mode 100644 helix-dap/src/client.rs create mode 100644 helix-dap/src/lib.rs create mode 100644 helix-dap/src/transport.rs (limited to 'helix-dap') diff --git a/helix-dap/Cargo.toml b/helix-dap/Cargo.toml new file mode 100644 index 00000000..6adaaedd --- /dev/null +++ b/helix-dap/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "helix-dap" +version = "0.3.0" +authors = ["Blaž Hrastnik "] +edition = "2018" +license = "MPL-2.0" +description = "DAP client implementation for Helix project" +categories = ["editor"] +repository = "https://github.com/helix-editor/helix" +homepage = "https://helix-editor.com" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0" +log = "0.4" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +thiserror = "1.0" +tokio = { version = "1.9", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot"] } diff --git a/helix-dap/examples/dap-basic.rs b/helix-dap/examples/dap-basic.rs new file mode 100644 index 00000000..45522516 --- /dev/null +++ b/helix-dap/examples/dap-basic.rs @@ -0,0 +1,51 @@ +use helix_dap::{Client, Result, SourceBreakpoint}; + +#[tokio::main] +pub async fn main() -> Result<()> { + let mut client = Client::start("nc", vec!["127.0.0.1", "7777"], 0)?; + + println!("init: {:?}", client.initialize().await); + println!("caps: {:?}", client.capabilities()); + println!( + "launch: {:?}", + client.launch("/tmp/godebug/main".to_owned()).await + ); + + println!( + "breakpoints: {:?}", + client + .set_breakpoints( + "/tmp/godebug/main.go".to_owned(), + vec![SourceBreakpoint { + line: 6, + column: Some(2), + }] + ) + .await + ); + + let mut _in = String::new(); + std::io::stdin() + .read_line(&mut _in) + .expect("Failed to read line"); + + println!("configurationDone: {:?}", client.configuration_done().await); + println!("stopped: {:?}", client.wait_for_stopped().await); + println!("stack trace: {:?}", client.stack_trace(1).await); + + let mut _in = String::new(); + std::io::stdin() + .read_line(&mut _in) + .expect("Failed to read line"); + + println!("continued: {:?}", client.continue_thread(0).await); + + let mut _in = String::new(); + std::io::stdin() + .read_line(&mut _in) + .expect("Failed to read line"); + + println!("disconnect: {:?}", client.disconnect().await); + + Ok(()) +} diff --git a/helix-dap/src/client.rs b/helix-dap/src/client.rs new file mode 100644 index 00000000..9f269a53 --- /dev/null +++ b/helix-dap/src/client.rs @@ -0,0 +1,352 @@ +use crate::{ + transport::{Event, Payload, Request, Response, Transport}, + Result, +}; +use serde::{Deserialize, Serialize}; +use serde_json::{from_value, to_value, Value}; +use std::process::Stdio; +use std::sync::atomic::{AtomicU64, Ordering}; +use tokio::{ + io::{BufReader, BufWriter}, + process::{Child, Command}, + sync::mpsc::{channel, UnboundedReceiver, UnboundedSender}, +}; + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct DebuggerCapabilities { + supports_configuration_done_request: bool, + supports_function_breakpoints: bool, + supports_conditional_breakpoints: bool, + supports_exception_info_request: bool, + support_terminate_debuggee: bool, + supports_delayed_stack_trace_loading: bool, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct InitializeArguments { + client_id: String, + client_name: String, + adapter_id: String, + locale: String, + #[serde(rename = "linesStartAt1")] + lines_start_at_one: bool, + #[serde(rename = "columnsStartAt1")] + columns_start_at_one: bool, + path_format: String, + supports_variable_type: bool, + supports_variable_paging: bool, + supports_run_in_terminal_request: bool, + supports_memory_references: bool, + supports_progress_reporting: bool, + supports_invalidated_event: bool, +} + +// TODO: split out +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct LaunchArguments { + mode: String, + program: String, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Source { + path: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct SourceBreakpoint { + pub line: usize, + pub column: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct SetBreakpointsArguments { + source: Source, + breakpoints: Option>, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Breakpoint { + pub id: Option, + pub verified: bool, + pub message: Option, + pub source: Option, + pub line: Option, + pub column: Option, + pub end_line: Option, + pub end_column: Option, + pub instruction_reference: Option, + pub offset: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct SetBreakpointsResponseBody { + breakpoints: Option>, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct ContinueArguments { + thread_id: usize, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct ContinueResponseBody { + all_threads_continued: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct StackFrameFormat { + parameters: Option, + parameter_types: Option, + parameter_names: Option, + parameter_values: Option, + line: Option, + module: Option, + include_all: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct StackTraceArguments { + thread_id: usize, + start_frame: Option, + levels: Option, + format: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct StackFrame { + id: usize, + name: String, + source: Option, + line: usize, + column: usize, + end_line: Option, + end_column: Option, + can_restart: Option, + instruction_pointer_reference: Option, + // module_id + presentation_hint: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct StackTraceResponseBody { + total_frames: Option, + stack_frames: Vec, +} + +#[derive(Debug)] +pub struct Client { + id: usize, + _process: Child, + server_tx: UnboundedSender, + server_rx: UnboundedReceiver, + request_counter: AtomicU64, + capabilities: Option, +} + +impl Client { + pub fn start(cmd: &str, args: Vec<&str>, id: usize) -> Result { + let process = Command::new(cmd) + .args(args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + // make sure the process is reaped on drop + .kill_on_drop(true) + .spawn(); + + let mut process = process?; + + // TODO: do we need bufreader/writer here? or do we use async wrappers on unblock? + let writer = BufWriter::new(process.stdin.take().expect("Failed to open stdin")); + let reader = BufReader::new(process.stdout.take().expect("Failed to open stdout")); + + let (server_rx, server_tx) = Transport::start(reader, writer, id); + + let client = Self { + id, + _process: process, + server_tx, + server_rx, + request_counter: AtomicU64::new(0), + capabilities: None, + }; + + // TODO: async client.initialize() + // maybe use an arc flag + + Ok(client) + } + + pub fn id(&self) -> usize { + self.id + } + + fn next_request_id(&self) -> u64 { + self.request_counter.fetch_add(1, Ordering::Relaxed) + } + + async fn request(&self, command: String, arguments: Option) -> Result { + let (callback_rx, mut callback_tx) = channel(1); + + let req = Request { + back_ch: Some(callback_rx), + seq: self.next_request_id(), + msg_type: "request".to_owned(), + command, + arguments, + }; + + self.server_tx + .send(req) + .expect("Failed to send request to debugger"); + + callback_tx + .recv() + .await + .expect("Failed to receive response") + } + + pub fn capabilities(&self) -> &DebuggerCapabilities { + self.capabilities + .as_ref() + .expect("language server not yet initialized!") + } + + pub async fn initialize(&mut self) -> Result<()> { + let args = InitializeArguments { + client_id: "hx".to_owned(), + client_name: "helix".to_owned(), + adapter_id: "go".to_owned(), + locale: "en-us".to_owned(), + lines_start_at_one: true, + columns_start_at_one: true, + path_format: "path".to_owned(), + supports_variable_type: false, + supports_variable_paging: false, + supports_run_in_terminal_request: false, + supports_memory_references: false, + supports_progress_reporting: true, + supports_invalidated_event: true, + }; + + let response = self + .request("initialize".to_owned(), to_value(args).ok()) + .await?; + self.capabilities = from_value(response.body.unwrap()).ok(); + + Ok(()) + } + + pub async fn disconnect(&mut self) -> Result<()> { + self.request("disconnect".to_owned(), None).await?; + Ok(()) + } + + pub async fn launch(&mut self, executable: String) -> Result<()> { + let args = LaunchArguments { + mode: "exec".to_owned(), + program: executable, + }; + + self.request("launch".to_owned(), to_value(args).ok()) + .await?; + + match self + .server_rx + .recv() + .await + .expect("Expected initialized event") + { + Payload::Event(Event { event, .. }) => { + if event == "initialized".to_owned() { + Ok(()) + } else { + unreachable!() + } + } + _ => unreachable!(), + } + } + + pub async fn set_breakpoints( + &mut self, + file: String, + breakpoints: Vec, + ) -> Result>> { + let args = SetBreakpointsArguments { + source: Source { path: Some(file) }, + breakpoints: Some(breakpoints), + }; + + let response = self + .request("setBreakpoints".to_owned(), to_value(args).ok()) + .await?; + let body: Option = from_value(response.body.unwrap()).ok(); + + Ok(body.map(|b| b.breakpoints).unwrap()) + } + + pub async fn configuration_done(&mut self) -> Result<()> { + self.request("configurationDone".to_owned(), None).await?; + Ok(()) + } + + pub async fn wait_for_stopped(&mut self) -> Result<()> { + match self.server_rx.recv().await.expect("Expected stopped event") { + Payload::Event(Event { event, .. }) => { + if event == "stopped".to_owned() { + Ok(()) + } else { + unreachable!() + } + } + _ => unreachable!(), + } + } + + pub async fn continue_thread(&mut self, thread_id: usize) -> Result> { + let args = ContinueArguments { thread_id }; + + let response = self + .request("continue".to_owned(), to_value(args).ok()) + .await?; + + let body: Option = from_value(response.body.unwrap()).ok(); + + Ok(body.map(|b| b.all_threads_continued).unwrap()) + } + + pub async fn stack_trace( + &mut self, + thread_id: usize, + ) -> Result<(Vec, Option)> { + let args = StackTraceArguments { + thread_id, + start_frame: None, + levels: None, + format: None, + }; + + let response = self + .request("stackTrace".to_owned(), to_value(args).ok()) + .await?; + + let body: StackTraceResponseBody = from_value(response.body.unwrap()).unwrap(); + + Ok((body.stack_frames, body.total_frames)) + } +} diff --git a/helix-dap/src/lib.rs b/helix-dap/src/lib.rs new file mode 100644 index 00000000..1e545fd8 --- /dev/null +++ b/helix-dap/src/lib.rs @@ -0,0 +1,21 @@ +mod client; +mod transport; + +pub use client::{Breakpoint, Client, SourceBreakpoint}; +pub use transport::{Event, Payload, Request, Response, Transport}; + +use thiserror::Error; +#[derive(Error, Debug)] +pub enum Error { + #[error("failed to parse: {0}")] + Parse(#[from] serde_json::Error), + #[error("IO Error: {0}")] + IO(#[from] std::io::Error), + #[error("request timed out")] + Timeout, + #[error("server closed the stream")] + StreamClosed, + #[error(transparent)] + Other(#[from] anyhow::Error), +} +pub type Result = core::result::Result; diff --git a/helix-dap/src/transport.rs b/helix-dap/src/transport.rs new file mode 100644 index 00000000..1c004cfe --- /dev/null +++ b/helix-dap/src/transport.rs @@ -0,0 +1,282 @@ +use crate::{Error, Result}; +use anyhow::Context; +use log::error; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::collections::HashMap; +use std::sync::Arc; +use tokio::{ + io::{AsyncBufRead, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}, + process::{ChildStdin, ChildStdout}, + sync::{ + mpsc::{unbounded_channel, Sender, UnboundedReceiver, UnboundedSender}, + Mutex, + }, +}; + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct Request { + #[serde(skip)] + pub back_ch: Option>>, + pub seq: u64, + #[serde(rename = "type")] + pub msg_type: String, + pub command: String, + pub arguments: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +pub struct Response { + pub seq: u64, + #[serde(rename = "type")] + pub msg_type: String, + pub request_seq: u64, + pub success: bool, + pub command: String, + pub message: Option, + pub body: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +pub struct Event { + pub seq: u64, + #[serde(rename = "type")] + pub msg_type: String, + pub event: String, + pub body: Option, +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +#[serde(untagged)] +pub enum Payload { + // type = "event" + Event(Event), + // type = "response" + Response(Response), + // type = "request" + Request(Request), +} + +#[derive(Debug)] +pub struct Transport { + id: usize, + pending_requests: Mutex>>>, +} + +impl Transport { + pub fn start( + server_stdout: BufReader, + server_stdin: BufWriter, + id: usize, + ) -> (UnboundedReceiver, UnboundedSender) { + let (client_tx, rx) = unbounded_channel(); + let (tx, client_rx) = unbounded_channel(); + + let transport = Self { + id, + pending_requests: Mutex::new(HashMap::default()), + }; + + let transport = Arc::new(transport); + + tokio::spawn(Self::recv(transport.clone(), server_stdout, client_tx)); + tokio::spawn(Self::send(transport, server_stdin, client_rx)); + + (rx, tx) + } + + async fn recv_server_message( + reader: &mut (impl AsyncBufRead + Unpin + Send), + buffer: &mut String, + ) -> Result { + let mut content_length = None; + loop { + buffer.truncate(0); + reader.read_line(buffer).await?; + let header = buffer.trim(); + + if header.is_empty() { + break; + } + + let mut parts = header.split(": "); + + match (parts.next(), parts.next(), parts.next()) { + (Some("Content-Length"), Some(value), None) => { + content_length = Some(value.parse().context("invalid content length")?); + } + (Some(_), Some(_), None) => {} + _ => { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "Failed to parse header", + ) + .into()); + } + } + } + + let content_length = content_length.context("missing content length")?; + + //TODO: reuse vector + let mut content = vec![0; content_length]; + reader.read_exact(&mut content).await?; + let msg = std::str::from_utf8(&content).context("invalid utf8 from server")?; + + // TODO: `info!` here + println!("<- DAP {}", msg); + + // try parsing as output (server response) or call (server request) + let output: serde_json::Result = serde_json::from_str(msg); + + Ok(output?) + } + + async fn send_payload_to_server( + &self, + server_stdin: &mut BufWriter, + req: Request, + ) -> Result<()> { + let json = serde_json::to_string(&req)?; + match req.back_ch { + Some(back) => { + self.pending_requests.lock().await.insert(req.seq, back); + () + } + None => {} + } + self.send_string_to_server(server_stdin, json).await + } + + async fn send_string_to_server( + &self, + server_stdin: &mut BufWriter, + request: String, + ) -> Result<()> { + // TODO: `info!` here + println!("-> DAP {}", request); + + // send the headers + server_stdin + .write_all(format!("Content-Length: {}\r\n\r\n", request.len()).as_bytes()) + .await?; + + // send the body + server_stdin.write_all(request.as_bytes()).await?; + + server_stdin.flush().await?; + + Ok(()) + } + + async fn process_server_message( + &self, + client_tx: &UnboundedSender, + msg: Payload, + ) -> Result<()> { + let (id, result) = match msg { + Payload::Response(Response { + success: true, + seq, + request_seq, + .. + }) => { + // TODO: `info!` here + println!("<- DAP success ({}, in response to {})", seq, request_seq); + if let Payload::Response(val) = msg { + (request_seq, Ok(val)) + } else { + unreachable!(); + } + } + Payload::Response(Response { + success: false, + message, + body, + request_seq, + command, + .. + }) => { + // TODO: `error!` here + println!( + "<- DAP error {:?} ({:?}) for command #{} {}", + message, body, request_seq, command + ); + ( + request_seq, + Err(Error::Other(anyhow::format_err!("{:?}", body))), + ) + } + Payload::Request(Request { + ref command, + ref seq, + .. + }) => { + // TODO: `info!` here + println!("<- DAP request {} #{}", command, seq); + client_tx.send(msg).expect("Failed to send"); + return Ok(()); + } + Payload::Event(Event { + ref event, ref seq, .. + }) => { + // TODO: `info!` here + println!("<- DAP event {} #{}", event, seq); + client_tx.send(msg).expect("Failed to send"); + return Ok(()); + } + }; + + let tx = self + .pending_requests + .lock() + .await + .remove(&id) + .expect("pending_request with id not found!"); + + match tx.send(result).await { + Ok(_) => (), + Err(_) => error!( + "Tried sending response into a closed channel (id={:?}), original request likely timed out", + id + ), + }; + + Ok(()) + } + + async fn recv( + transport: Arc, + mut server_stdout: BufReader, + client_tx: UnboundedSender, + ) { + let mut recv_buffer = String::new(); + loop { + match Self::recv_server_message(&mut server_stdout, &mut recv_buffer).await { + Ok(msg) => { + transport + .process_server_message(&client_tx, msg) + .await + .unwrap(); + } + Err(err) => { + error!("err: <- {:?}", err); + break; + } + } + } + } + + async fn send( + transport: Arc, + mut server_stdin: BufWriter, + mut client_rx: UnboundedReceiver, + ) { + while let Some(req) = client_rx.recv().await { + transport + .send_payload_to_server(&mut server_stdin, req) + .await + .unwrap() + } + } +} -- cgit v1.2.3-70-g09d2