feat(dap): send Disconnect if Terminated event received (#5532)

Send a `Disconnect` DAP request if the `Terminated` event is received.
According to the specification, if the debugging session was started by
as `launch`, the debuggee should be terminated alongside the session. If
instead the session was started as `attach`, it should not be disposed of.

This default behaviour can be overriden if the `supportTerminateDebuggee`
capability is supported by the adapter, through the `Disconnect` request
`terminateDebuggee` argument, as described in
[the specification][discon-spec].

This also implies saving the starting command for a debug sessions, in
order to decide which behaviour should be used, as well as validating the
capabilities of the adapter, in order to decide what the disconnect should
do.

An additional change made is handling of the `Exited` event, showing a
message if the exit code is different than `0`, for the user to be aware
off the termination failure.

[discon-spec]: https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Disconnect

Closes: #4674

Signed-off-by: Filip Dutescu <filip.dutescu@gmail.com>
pull/5651/head
Filip Dutescu 2 years ago committed by GitHub
parent 31b0c75832
commit e3765ac6d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,4 +1,5 @@
use crate::{ use crate::{
requests::DisconnectArguments,
transport::{Payload, Request, Response, Transport}, transport::{Payload, Request, Response, Transport},
types::*, types::*,
Error, Result, ThreadId, Error, Result, ThreadId,
@ -31,6 +32,7 @@ pub struct Client {
_process: Option<Child>, _process: Option<Child>,
server_tx: UnboundedSender<Payload>, server_tx: UnboundedSender<Payload>,
request_counter: AtomicU64, request_counter: AtomicU64,
connection_type: Option<ConnectionType>,
pub caps: Option<DebuggerCapabilities>, pub caps: Option<DebuggerCapabilities>,
// thread_id -> frames // thread_id -> frames
pub stack_frames: HashMap<ThreadId, Vec<StackFrame>>, pub stack_frames: HashMap<ThreadId, Vec<StackFrame>>,
@ -41,6 +43,12 @@ pub struct Client {
pub quirks: DebuggerQuirks, pub quirks: DebuggerQuirks,
} }
#[derive(Clone, Copy, Debug)]
pub enum ConnectionType {
Launch,
Attach,
}
impl Client { impl Client {
// Spawn a process and communicate with it by either TCP or stdio // Spawn a process and communicate with it by either TCP or stdio
pub async fn process( pub async fn process(
@ -78,7 +86,7 @@ impl Client {
server_tx, server_tx,
request_counter: AtomicU64::new(0), request_counter: AtomicU64::new(0),
caps: None, caps: None,
// connection_type: None,
stack_frames: HashMap::new(), stack_frames: HashMap::new(),
thread_states: HashMap::new(), thread_states: HashMap::new(),
thread_id: None, thread_id: None,
@ -207,6 +215,10 @@ impl Client {
self.id self.id
} }
pub fn connection_type(&self) -> Option<ConnectionType> {
self.connection_type
}
fn next_request_id(&self) -> u64 { fn next_request_id(&self) -> u64 {
self.request_counter.fetch_add(1, Ordering::Relaxed) self.request_counter.fetch_add(1, Ordering::Relaxed)
} }
@ -334,15 +346,21 @@ impl Client {
Ok(()) Ok(())
} }
pub fn disconnect(&self) -> impl Future<Output = Result<Value>> { pub fn disconnect(
self.call::<requests::Disconnect>(()) &mut self,
args: Option<DisconnectArguments>,
) -> impl Future<Output = Result<Value>> {
self.connection_type = None;
self.call::<requests::Disconnect>(args)
} }
pub fn launch(&self, args: serde_json::Value) -> impl Future<Output = Result<Value>> { pub fn launch(&mut self, args: serde_json::Value) -> impl Future<Output = Result<Value>> {
self.connection_type = Some(ConnectionType::Launch);
self.call::<requests::Launch>(args) self.call::<requests::Launch>(args)
} }
pub fn attach(&self, args: serde_json::Value) -> impl Future<Output = Result<Value>> { pub fn attach(&mut self, args: serde_json::Value) -> impl Future<Output = Result<Value>> {
self.connection_type = Some(ConnectionType::Attach);
self.call::<requests::Attach>(args) self.call::<requests::Attach>(args)
} }

@ -2,7 +2,7 @@ mod client;
mod transport; mod transport;
mod types; mod types;
pub use client::Client; pub use client::{Client, ConnectionType};
pub use events::Event; pub use events::Event;
pub use transport::{Payload, Response, Transport}; pub use transport::{Payload, Response, Transport};
pub use types::*; pub use types::*;

@ -391,11 +391,22 @@ pub mod requests {
const COMMAND: &'static str = "attach"; const COMMAND: &'static str = "attach";
} }
#[derive(Debug, Default, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DisconnectArguments {
#[serde(skip_serializing_if = "Option::is_none")]
pub restart: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub terminate_debuggee: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub suspend_debuggee: Option<bool>,
}
#[derive(Debug)] #[derive(Debug)]
pub enum Disconnect {} pub enum Disconnect {}
impl Request for Disconnect { impl Request for Disconnect {
type Arguments = (); type Arguments = Option<DisconnectArguments>;
type Result = (); type Result = ();
const COMMAND: &'static str = "disconnect"; const COMMAND: &'static str = "disconnect";
} }

@ -539,7 +539,7 @@ pub fn dap_variables(cx: &mut Context) {
pub fn dap_terminate(cx: &mut Context) { pub fn dap_terminate(cx: &mut Context) {
let debugger = debugger!(cx.editor); let debugger = debugger!(cx.editor);
let request = debugger.disconnect(); let request = debugger.disconnect(None);
dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| { dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| {
// editor.set_error(format!("Failed to disconnect: {}", e)); // editor.set_error(format!("Failed to disconnect: {}", e));
editor.debugger = None; editor.debugger = None;

@ -1,7 +1,8 @@
use crate::editor::{Action, Breakpoint}; use crate::editor::{Action, Breakpoint};
use crate::{align_view, Align, Editor}; use crate::{align_view, Align, Editor};
use dap::requests::DisconnectArguments;
use helix_core::Selection; use helix_core::Selection;
use helix_dap::{self as dap, Client, Payload, Request, ThreadId}; use helix_dap::{self as dap, Client, ConnectionType, Payload, Request, ThreadId};
use helix_lsp::block_on; use helix_lsp::block_on;
use log::warn; use log::warn;
use std::fmt::Write; use std::fmt::Write;
@ -274,6 +275,66 @@ impl Editor {
self.set_status("Debugged application started"); self.set_status("Debugged application started");
}; // TODO: do we need to handle error? }; // TODO: do we need to handle error?
} }
Event::Terminated(terminated) => {
let restart_args = if let Some(terminated) = terminated {
terminated.restart
} else {
None
};
let disconnect_args = Some(DisconnectArguments {
restart: Some(restart_args.is_some()),
terminate_debuggee: None,
suspend_debuggee: None,
});
if let Err(err) = debugger.disconnect(disconnect_args).await {
self.set_error(format!(
"Cannot disconnect debugger upon terminated event receival {:?}",
err
));
return false;
}
match restart_args {
Some(restart_args) => {
log::info!("Attempting to restart debug session.");
let connection_type = match debugger.connection_type() {
Some(connection_type) => connection_type,
None => {
self.set_error("No starting request found, to be used in restarting the debugging session.");
return false;
}
};
let relaunch_resp = if let ConnectionType::Launch = connection_type {
debugger.launch(restart_args).await
} else {
debugger.attach(restart_args).await
};
if let Err(err) = relaunch_resp {
self.set_error(format!(
"Failed to restart debugging session: {:?}",
err
));
}
}
None => {
self.set_status(
"Terminated debugging session and disconnected debugger.",
);
}
}
}
Event::Exited(resp) => {
let exit_code = resp.exit_code;
if exit_code != 0 {
self.set_error(format!(
"Debuggee failed to exit successfully (exit code: {exit_code})."
));
}
}
ev => { ev => {
log::warn!("Unhandled event {:?}", ev); log::warn!("Unhandled event {:?}", ev);
return false; // return early to skip render return false; // return early to skip render

Loading…
Cancel
Save