working lldb example

This commit is contained in:
Dmitry Sharshakov 2021-08-14 14:23:19 +03:00 committed by Blaž Hrastnik
parent ae32159247
commit f92fb966c0
2 changed files with 46 additions and 31 deletions

View File

@ -57,7 +57,7 @@ pub async fn main() -> Result<()> {
"/tmp/cdebug/main.c".to_owned(),
vec![SourceBreakpoint {
line: 6,
column: Some(4),
column: Some(2),
condition: None,
hit_condition: None,
log_message: None,
@ -82,7 +82,7 @@ pub async fn main() -> Result<()> {
let threads = client.threads().await?;
println!("threads: {:#?}", threads);
let bt = client
.stack_trace(threads[2].id)
.stack_trace(threads[0].id)
.await
.expect("expected stack trace");
println!("stack trace: {:#?}", bt);
@ -93,7 +93,7 @@ pub async fn main() -> Result<()> {
println!("scopes: {:#?}", scopes);
println!(
"vars: {:#?}",
client.variables(scopes[1].variables_reference).await
client.variables(scopes[0].variables_reference).await
);
let mut _in = String::new();

View File

@ -12,6 +12,7 @@
use std::{collections::HashMap, process::Stdio};
use tokio::{
io::{AsyncBufRead, AsyncWrite, BufReader, BufWriter},
join,
net::TcpStream,
process::{Child, Command},
sync::{
@ -449,7 +450,12 @@ fn next_request_id(&self) -> u64 {
self.request_counter.fetch_add(1, Ordering::Relaxed)
}
async fn request(&self, command: String, arguments: Option<Value>) -> Result<Response> {
async fn request(
&self,
command: String,
arguments: Option<Value>,
response: bool,
) -> Result<Option<Response>> {
let (callback_rx, mut callback_tx) = channel(1);
let req = Request {
@ -464,10 +470,11 @@ async fn request(&self, command: String, arguments: Option<Value>) -> Result<Res
.send(req)
.expect("Failed to send request to debugger");
callback_tx
.recv()
.await
.expect("Failed to receive response")
if response {
Ok(Some(callback_tx.recv().await.unwrap()?))
} else {
Ok(None)
}
}
pub fn capabilities(&self) -> &DebuggerCapabilities {
@ -494,25 +501,25 @@ pub async fn initialize(&mut self, adapter_id: String) -> Result<()> {
};
let response = self
.request("initialize".to_owned(), to_value(args).ok())
.await?;
.request("initialize".to_owned(), to_value(args).ok(), true)
.await?
.unwrap();
self.capabilities = from_value(response.body.unwrap()).ok();
Ok(())
}
pub async fn disconnect(&mut self) -> Result<()> {
self.request("disconnect".to_owned(), None).await?;
self.request("disconnect".to_owned(), None, true).await?;
Ok(())
}
pub async fn launch(&mut self, args: impl Serialize) -> Result<()> {
let mut initialized = self.listen_for_event("initialized".to_owned()).await;
self.request("launch".to_owned(), to_value(args).ok())
.await?;
initialized.recv().await;
let res = self.request("launch".to_owned(), to_value(args).ok(), true);
let ev = initialized.recv();
join!(res, ev).0?;
Ok(())
}
@ -520,10 +527,9 @@ pub async fn launch(&mut self, args: impl Serialize) -> Result<()> {
pub async fn attach(&mut self, args: impl Serialize) -> Result<()> {
let mut initialized = self.listen_for_event("initialized".to_owned()).await;
self.request("attach".to_owned(), to_value(args).ok())
.await?;
initialized.recv().await;
let res = self.request("attach".to_owned(), to_value(args).ok(), false);
let ev = initialized.recv();
join!(res, ev).0?;
Ok(())
}
@ -549,15 +555,17 @@ pub async fn set_breakpoints(
};
let response = self
.request("setBreakpoints".to_owned(), to_value(args).ok())
.await?;
.request("setBreakpoints".to_owned(), to_value(args).ok(), true)
.await?
.unwrap();
let body: Option<SetBreakpointsResponseBody> = 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?;
self.request("configurationDone".to_owned(), None, true)
.await?;
Ok(())
}
@ -565,8 +573,9 @@ pub async fn continue_thread(&mut self, thread_id: usize) -> Result<Option<bool>
let args = ContinueArguments { thread_id };
let response = self
.request("continue".to_owned(), to_value(args).ok())
.await?;
.request("continue".to_owned(), to_value(args).ok(), true)
.await?
.unwrap();
let body: Option<ContinueResponseBody> = from_value(response.body.unwrap()).ok();
@ -585,8 +594,9 @@ pub async fn stack_trace(
};
let response = self
.request("stackTrace".to_owned(), to_value(args).ok())
.await?;
.request("stackTrace".to_owned(), to_value(args).ok(), true)
.await?
.unwrap();
let body: StackTraceResponseBody = from_value(response.body.unwrap()).unwrap();
@ -594,7 +604,10 @@ pub async fn stack_trace(
}
pub async fn threads(&mut self) -> Result<Vec<Thread>> {
let response = self.request("threads".to_owned(), None).await?;
let response = self
.request("threads".to_owned(), None, true)
.await?
.unwrap();
let body: ThreadsResponseBody = from_value(response.body.unwrap()).unwrap();
@ -605,8 +618,9 @@ pub async fn scopes(&mut self, frame_id: usize) -> Result<Vec<Scope>> {
let args = ScopesArguments { frame_id };
let response = self
.request("scopes".to_owned(), to_value(args).ok())
.await?;
.request("scopes".to_owned(), to_value(args).ok(), true)
.await?
.unwrap();
let body: ScopesResponseBody = from_value(response.body.unwrap()).unwrap();
@ -623,8 +637,9 @@ pub async fn variables(&mut self, variables_reference: usize) -> Result<Vec<Vari
};
let response = self
.request("variables".to_owned(), to_value(args).ok())
.await?;
.request("variables".to_owned(), to_value(args).ok(), true)
.await?
.unwrap();
let body: VariablesResponseBody = from_value(response.body.unwrap()).unwrap();