Skip to content

Commit

Permalink
Complete launch/attach before responding to configurationDone.
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimcn committed Mar 11, 2023
1 parent 3369860 commit 6649c20
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
1 change: 0 additions & 1 deletion adapter/crates/adapter-protocol/src/codelldb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ pub enum RequestArguments {
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "command", content = "body")]
pub enum ResponseBody {
Async,
initialize(Capabilities),
cancel,
launch,
Expand Down
18 changes: 15 additions & 3 deletions adapter/src/debug_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ use futures::prelude::*;
use lldb::*;
use serde_json;
use tokio::io::AsyncReadExt;
use tokio::sync::mpsc;
use tokio::sync::{broadcast, mpsc};

pub struct DebugSession {
self_ref: MustInitialize<Shared<DebugSession>>,
dap_session: DAPSession,
event_listener: SBListener,
python: Option<Box<PythonInterface>>,
current_cancellation: cancellation::Receiver, // Cancellation associated with request currently being processed
configuration_done_sender: broadcast::Sender<()>,

console_pipe: std::fs::File,

Expand Down Expand Up @@ -129,6 +130,7 @@ impl DebugSession {
event_listener: SBListener::new_with_name("DebugSession"),
python: python,
current_cancellation: cancellation::dummy(),
configuration_done_sender: broadcast::channel(1).0,

console_pipe: con_writer,

Expand Down Expand Up @@ -256,7 +258,7 @@ impl DebugSession {
fn cancellation_filter(
dap_session: &DAPSession,
) -> mpsc::Receiver<(u32, RequestArguments, cancellation::Receiver)> {
use tokio::sync::broadcast::error::RecvError;
use broadcast::error::RecvError;

let mut raw_requests_stream = dap_session.subscribe_requests().unwrap();
let (requests_sender, requests_receiver) =
Expand Down Expand Up @@ -606,7 +608,17 @@ impl DebugSession {
}

fn handle_configuration_done(&mut self) -> Result<(), Error> {
Ok(())
// Signal to complete pending launch/attach tasks.
log_errors!(self.configuration_done_sender.send(()));
let self_ref = self.self_ref.clone();
let fut = async move {
// Wait for them to finish before responding.
while self_ref.map(|s| s.configuration_done_sender.receiver_count()).await > 0 {
tokio::task::yield_now().await
}
Ok(ResponseBody::configurationDone)
};
Err(AsyncResponse(Box::new(fut)).into())
}

fn handle_threads(&mut self) -> Result<ThreadsResponseBody, Error> {
Expand Down
24 changes: 7 additions & 17 deletions adapter/src/debug_session/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,17 @@ impl super::DebugSession {
self.send_event(EventBody::initialized);

let term_fut = self.create_terminal(&args);
let config_done_fut = self.wait_for_configuration_done();
let mut config_done_recv = self.configuration_done_sender.subscribe();
let self_ref = self.self_ref.clone();
let fut = async move {
drop(tokio::join!(term_fut, config_done_fut));
term_fut.await;
log_errors!(config_done_recv.recv().await);
self_ref.map(|s| s.complete_launch(args)).await
};
Err(AsyncResponse(Box::new(fut)).into())
}
}

fn wait_for_configuration_done(&self) -> impl Future<Output = Result<(), Error>> {
let result = self.dap_session.subscribe_requests();
async move {
let mut receiver = result?;
while let Ok((_seq, request)) = receiver.recv().await {
if let RequestArguments::configurationDone(_) = request {
return Ok(());
}
}
bail!("Did not receive configurationDone");
}
}

fn complete_launch(&mut self, args: LaunchRequestArguments) -> Result<ResponseBody, Error> {
let mut launch_info = self.target.launch_info();

Expand Down Expand Up @@ -177,9 +165,10 @@ impl super::DebugSession {
self.disassembly = Initialized(disassembly::AddressSpace::new(&self.target));
self.send_event(EventBody::initialized);

let mut config_done_recv = self.configuration_done_sender.subscribe();
let self_ref = self.self_ref.clone();
let fut = async move {
self_ref.map(|s| s.wait_for_configuration_done()).await.await?;
log_errors!(config_done_recv.recv().await);
self_ref.map(|s| s.complete_custom_launch(args)).await
};
Err(AsyncResponse(Box::new(fut)).into())
Expand Down Expand Up @@ -247,9 +236,10 @@ impl super::DebugSession {
self.disassembly = Initialized(disassembly::AddressSpace::new(&self.target));
self.send_event(EventBody::initialized);

let mut config_done_recv = self.configuration_done_sender.subscribe();
let self_ref = self.self_ref.clone();
let fut = async move {
self_ref.map(|s| s.wait_for_configuration_done()).await.await?;
log_errors!(config_done_recv.recv().await);
self_ref.map(|s| s.complete_attach(args, attach_info)).await
};
Err(AsyncResponse(Box::new(fut)).into())
Expand Down
7 changes: 7 additions & 0 deletions debuggee/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
"program": "${dbgconfig:targetDir}/debuggee",
"args": ["threads_long"]
},
{
"name": "debuggee launch inf_loop",
"type": "lldb",
"request": "launch",
"program": "${dbgconfig:targetDir}/debuggee",
"args": ["inf_loop"]
},
{
"name": "debuggee launch crash",
"type": "lldb",
Expand Down
9 changes: 6 additions & 3 deletions debuggee/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"lldb.launch.initCommands": [
"settings set target.x86-disassembly-flavor intel",
"command script import ${workspaceRoot}/../tools/debugvis.py",
"script from debugvis import *",
// "command script import ${workspaceRoot}/../tools/debugvis.py",
// "script from debugvis import *",
],
"lldb.dbgconfig": {
"targetDir": "${workspaceFolder}/../build/debuggee"
//"targetDir": "${workspaceFolder}/../build/debuggee-gnu"
//"targetDir": "${workspaceFolder}/../build/debuggee-msvc"
},
"lldb.displayFormat": "auto",
"lldb.showDisassembly": "auto",
"lldb.dereferencePointers": true,
"lldb.consoleMode": "evaluate",
"lldb.launch.debugServer": 4711,
"lldb.verboseLogging": true,
}
14 changes: 14 additions & 0 deletions debuggee/debuggee.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"folders": [
{
"path": "."
}
],
"settings": {
"lldb.launch.debugServer": 4711,
"lldb.rpcServer": {
"path": "${workspaceFolder}/socket",
"token": "secret"
}
}
}

0 comments on commit 6649c20

Please sign in to comment.