From eb0605c13d65f68078a62105f738915143997d7e Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Sat, 14 Aug 2021 13:19:38 +0300 Subject: [PATCH] dap: create C example --- .../examples/{dap-basic.rs => dap-go.rs} | 0 helix-dap/examples/dap-lldb.rs | 114 ++++++++++++++++++ 2 files changed, 114 insertions(+) rename helix-dap/examples/{dap-basic.rs => dap-go.rs} (100%) create mode 100644 helix-dap/examples/dap-lldb.rs diff --git a/helix-dap/examples/dap-basic.rs b/helix-dap/examples/dap-go.rs similarity index 100% rename from helix-dap/examples/dap-basic.rs rename to helix-dap/examples/dap-go.rs diff --git a/helix-dap/examples/dap-lldb.rs b/helix-dap/examples/dap-lldb.rs new file mode 100644 index 00000000..2c8a16c0 --- /dev/null +++ b/helix-dap/examples/dap-lldb.rs @@ -0,0 +1,114 @@ +use helix_dap::{Client, Event, OutputEventBody, Result, SourceBreakpoint, StoppedEventBody}; +use serde::{Deserialize, Serialize}; +use serde_json::from_value; +use tokio::sync::mpsc::Receiver; + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct LaunchArguments { + program: String, + console: String, +} + +async fn output(mut output_event: Receiver) { + let body: OutputEventBody = + from_value(output_event.recv().await.unwrap().body.unwrap()).unwrap(); + println!( + "> [{}] {}", + body.category.unwrap_or("unknown".to_owned()), + body.output + ); +} + +#[tokio::main] +pub async fn main() -> Result<()> { + let base_config = fern::Dispatch::new().level(log::LevelFilter::Info); + + let stderr_config = fern::Dispatch::new() + .format(|out, message, record| out.finish(format_args!("[{}] {}", record.level(), message))) + .chain(std::io::stderr()); + + base_config + .chain(stderr_config) + .apply() + .expect("Failed to set up logging"); + + let client = Client::tcp("127.0.0.1:7777".parse::().unwrap(), 0).await; + println!("create: {:?}", client); + let mut client = client?; + + let output_event = client.listen_for_event("output".to_owned()).await; + tokio::spawn(output(output_event)); + + println!("init: {:?}", client.initialize("lldb".to_owned()).await); + println!("caps: {:?}", client.capabilities()); + + let args = LaunchArguments { + program: "/tmp/cdebug/main".to_owned(), + console: "internalConsole".to_owned(), + }; + + println!("launch: {:?}", client.launch(args).await); + + println!( + "breakpoints: {:#?}", + client + .set_breakpoints( + "/tmp/cdebug/main.c".to_owned(), + vec![SourceBreakpoint { + line: 8, + column: Some(2), + condition: None, + hit_condition: None, + log_message: None, + }] + ) + .await + ); + + let mut _in = String::new(); + std::io::stdin() + .read_line(&mut _in) + .expect("Failed to read line"); + + let mut stopped_event = client.listen_for_event("stopped".to_owned()).await; + + println!("configurationDone: {:?}", client.configuration_done().await); + + let stop: StoppedEventBody = + from_value(stopped_event.recv().await.unwrap().body.unwrap()).unwrap(); + println!("stopped: {:?}", stop); + + let threads = client.threads().await?; + println!("threads: {:#?}", threads); + let bt = client + .stack_trace(threads[2].id) + .await + .expect("expected stack trace"); + println!("stack trace: {:#?}", bt); + let scopes = client + .scopes(bt.0[0].id) + .await + .expect("expected scopes for thread"); + println!("scopes: {:#?}", scopes); + println!( + "vars: {:#?}", + client.variables(scopes[1].variables_reference).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(()) +}