2022-03-22 11:53:44 +08:00
|
|
|
use crate::editor::{Action, Breakpoint};
|
|
|
|
use crate::{align_view, Align, Editor};
|
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>
2023-02-20 12:00:00 +08:00
|
|
|
use dap::requests::DisconnectArguments;
|
2022-03-22 11:53:44 +08:00
|
|
|
use helix_core::Selection;
|
2025-06-23 22:48:05 +08:00
|
|
|
use helix_dap::{
|
|
|
|
self as dap, registry::DebugAdapterId, Client, ConnectionType, Payload, Request, ThreadId,
|
|
|
|
};
|
2022-03-22 11:53:44 +08:00
|
|
|
use helix_lsp::block_on;
|
2025-06-23 22:48:05 +08:00
|
|
|
use log::{error, warn};
|
|
|
|
use serde_json::{json, Value};
|
2022-08-04 13:32:59 +08:00
|
|
|
use std::fmt::Write;
|
2022-03-22 11:53:44 +08:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! debugger {
|
|
|
|
($editor:expr) => {{
|
2025-06-23 22:48:05 +08:00
|
|
|
let Some(debugger) = $editor.debug_adapters.get_active_client_mut() else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
debugger
|
2022-03-22 11:53:44 +08:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
// general utils:
|
|
|
|
pub fn dap_pos_to_pos(doc: &helix_core::Rope, line: usize, column: usize) -> Option<usize> {
|
|
|
|
// 1-indexing to 0 indexing
|
|
|
|
let line = doc.try_line_to_char(line - 1).ok()?;
|
|
|
|
let pos = line + column.saturating_sub(1);
|
|
|
|
// TODO: this is probably utf-16 offsets
|
|
|
|
Some(pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn select_thread_id(editor: &mut Editor, thread_id: ThreadId, force: bool) {
|
|
|
|
let debugger = debugger!(editor);
|
|
|
|
|
|
|
|
if !force && debugger.thread_id.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debugger.thread_id = Some(thread_id);
|
|
|
|
fetch_stack_trace(debugger, thread_id).await;
|
|
|
|
|
2024-08-01 05:39:46 +08:00
|
|
|
let frame = debugger.stack_frames[&thread_id].first().cloned();
|
2022-03-22 11:53:44 +08:00
|
|
|
if let Some(frame) = &frame {
|
|
|
|
jump_to_stack_frame(editor, frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn fetch_stack_trace(debugger: &mut Client, thread_id: ThreadId) {
|
|
|
|
let (frames, _) = match debugger.stack_trace(thread_id).await {
|
|
|
|
Ok(frames) => frames,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
debugger.stack_frames.insert(thread_id, frames);
|
|
|
|
debugger.active_frame = Some(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn jump_to_stack_frame(editor: &mut Editor, frame: &helix_dap::StackFrame) {
|
|
|
|
let path = if let Some(helix_dap::Source {
|
|
|
|
path: Some(ref path),
|
|
|
|
..
|
|
|
|
}) = frame.source
|
|
|
|
{
|
|
|
|
path.clone()
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2022-04-27 07:18:20 +08:00
|
|
|
if let Err(e) = editor.open(&path, Action::Replace) {
|
2022-03-22 11:53:44 +08:00
|
|
|
editor.set_error(format!("Unable to jump to stack frame: {}", e));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (view, doc) = current!(editor);
|
|
|
|
|
|
|
|
let text_end = doc.text().len_chars().saturating_sub(1);
|
|
|
|
let start = dap_pos_to_pos(doc.text(), frame.line, frame.column).unwrap_or(0);
|
|
|
|
let end = frame
|
|
|
|
.end_line
|
|
|
|
.and_then(|end_line| dap_pos_to_pos(doc.text(), end_line, frame.end_column.unwrap_or(0)))
|
|
|
|
.unwrap_or(start);
|
|
|
|
|
|
|
|
let selection = Selection::single(start.min(text_end), end.min(text_end));
|
|
|
|
doc.set_selection(view.id, selection);
|
|
|
|
align_view(doc, view, Align::Center);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn breakpoints_changed(
|
|
|
|
debugger: &mut dap::Client,
|
|
|
|
path: PathBuf,
|
|
|
|
breakpoints: &mut [Breakpoint],
|
|
|
|
) -> Result<(), anyhow::Error> {
|
2022-06-02 01:01:37 +08:00
|
|
|
// TODO: handle capabilities correctly again, by filtering breakpoints when emitting
|
2022-03-22 11:53:44 +08:00
|
|
|
// if breakpoint.condition.is_some()
|
|
|
|
// && !debugger
|
|
|
|
// .caps
|
|
|
|
// .as_ref()
|
|
|
|
// .unwrap()
|
|
|
|
// .supports_conditional_breakpoints
|
|
|
|
// .unwrap_or_default()
|
|
|
|
// {
|
|
|
|
// bail!(
|
|
|
|
// "Can't edit breakpoint: debugger does not support conditional breakpoints"
|
|
|
|
// )
|
|
|
|
// }
|
|
|
|
// if breakpoint.log_message.is_some()
|
|
|
|
// && !debugger
|
|
|
|
// .caps
|
|
|
|
// .as_ref()
|
|
|
|
// .unwrap()
|
|
|
|
// .supports_log_points
|
|
|
|
// .unwrap_or_default()
|
|
|
|
// {
|
|
|
|
// bail!("Can't edit breakpoint: debugger does not support logpoints")
|
|
|
|
// }
|
|
|
|
let source_breakpoints = breakpoints
|
|
|
|
.iter()
|
|
|
|
.map(|breakpoint| helix_dap::SourceBreakpoint {
|
|
|
|
line: breakpoint.line + 1, // convert from 0-indexing to 1-indexing (TODO: could set debugger to 0-indexing on init)
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let request = debugger.set_breakpoints(path, source_breakpoints);
|
|
|
|
match block_on(request) {
|
|
|
|
Ok(Some(dap_breakpoints)) => {
|
|
|
|
for (breakpoint, dap_breakpoint) in breakpoints.iter_mut().zip(dap_breakpoints) {
|
|
|
|
breakpoint.id = dap_breakpoint.id;
|
|
|
|
breakpoint.verified = dap_breakpoint.verified;
|
|
|
|
breakpoint.message = dap_breakpoint.message;
|
|
|
|
// TODO: handle breakpoint.message
|
|
|
|
// TODO: verify source matches
|
|
|
|
breakpoint.line = dap_breakpoint.line.unwrap_or(0).saturating_sub(1); // convert to 0-indexing
|
|
|
|
// TODO: no unwrap
|
|
|
|
breakpoint.column = dap_breakpoint.column;
|
|
|
|
// TODO: verify end_linef/col instruction reference, offset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => anyhow::bail!("Failed to set breakpoints: {}", e),
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Editor {
|
2025-06-23 22:48:05 +08:00
|
|
|
pub async fn handle_debugger_message(
|
|
|
|
&mut self,
|
|
|
|
id: DebugAdapterId,
|
|
|
|
payload: helix_dap::Payload,
|
|
|
|
) -> bool {
|
2022-03-22 11:53:44 +08:00
|
|
|
use helix_dap::{events, Event};
|
|
|
|
|
|
|
|
match payload {
|
2025-01-28 04:27:35 +08:00
|
|
|
Payload::Event(event) => {
|
|
|
|
let event = match Event::parse(&event.event, event.body) {
|
|
|
|
Ok(event) => event,
|
|
|
|
Err(dap::Error::Unhandled) => {
|
|
|
|
log::info!("Discarding unknown DAP event '{}'", event.event);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
log::warn!("Discarding invalid DAP event '{}': {err}", event.event);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match event {
|
|
|
|
Event::Stopped(events::StoppedBody {
|
|
|
|
thread_id,
|
|
|
|
description,
|
|
|
|
text,
|
|
|
|
reason,
|
|
|
|
all_threads_stopped,
|
|
|
|
..
|
|
|
|
}) => {
|
2025-06-23 22:48:05 +08:00
|
|
|
let debugger = match self.debug_adapters.get_client_mut(id) {
|
|
|
|
Some(debugger) => debugger,
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
|
2025-01-28 04:27:35 +08:00
|
|
|
let all_threads_stopped = all_threads_stopped.unwrap_or_default();
|
|
|
|
|
|
|
|
if all_threads_stopped {
|
|
|
|
if let Ok(response) =
|
|
|
|
debugger.request::<dap::requests::Threads>(()).await
|
|
|
|
{
|
|
|
|
for thread in response.threads {
|
|
|
|
fetch_stack_trace(debugger, thread.id).await;
|
|
|
|
}
|
|
|
|
select_thread_id(self, thread_id.unwrap_or_default(), false).await;
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
} else if let Some(thread_id) = thread_id {
|
|
|
|
debugger.thread_states.insert(thread_id, reason.clone()); // TODO: dap uses "type" || "reason" here
|
|
|
|
|
2025-06-23 22:48:05 +08:00
|
|
|
fetch_stack_trace(debugger, thread_id).await;
|
2025-01-28 04:27:35 +08:00
|
|
|
// whichever thread stops is made "current" (if no previously selected thread).
|
|
|
|
select_thread_id(self, thread_id, false).await;
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
|
|
|
|
2025-01-28 04:27:35 +08:00
|
|
|
let scope = match thread_id {
|
|
|
|
Some(id) => format!("Thread {}", id),
|
|
|
|
None => "Target".to_owned(),
|
|
|
|
};
|
2022-03-22 11:53:44 +08:00
|
|
|
|
2025-01-28 04:27:35 +08:00
|
|
|
let mut status = format!("{} stopped because of {}", scope, reason);
|
|
|
|
if let Some(desc) = description {
|
|
|
|
write!(status, " {}", desc).unwrap();
|
|
|
|
}
|
|
|
|
if let Some(text) = text {
|
|
|
|
write!(status, " {}", text).unwrap();
|
|
|
|
}
|
|
|
|
if all_threads_stopped {
|
|
|
|
status.push_str(" (all threads stopped)");
|
|
|
|
}
|
2022-03-22 11:53:44 +08:00
|
|
|
|
2025-01-28 04:27:35 +08:00
|
|
|
self.set_status(status);
|
2025-06-23 22:48:05 +08:00
|
|
|
self.debug_adapters.set_active_client(id);
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
Event::Continued(events::ContinuedBody { thread_id, .. }) => {
|
2025-06-23 22:48:05 +08:00
|
|
|
let debugger = match self.debug_adapters.get_client_mut(id) {
|
|
|
|
Some(debugger) => debugger,
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
|
2025-01-28 04:27:35 +08:00
|
|
|
debugger
|
|
|
|
.thread_states
|
|
|
|
.insert(thread_id, "running".to_owned());
|
|
|
|
if debugger.thread_id == Some(thread_id) {
|
|
|
|
debugger.resume_application();
|
|
|
|
}
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
2025-06-23 22:48:05 +08:00
|
|
|
Event::Thread(thread) => {
|
|
|
|
self.set_status(format!("Thread {}: {}", thread.thread_id, thread.reason));
|
|
|
|
let debugger = match self.debug_adapters.get_client_mut(id) {
|
|
|
|
Some(debugger) => debugger,
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
|
|
|
|
debugger.thread_id = Some(thread.thread_id);
|
|
|
|
// set the stack frame for the thread
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
Event::Breakpoint(events::BreakpointBody { reason, breakpoint }) => {
|
|
|
|
match &reason[..] {
|
|
|
|
"new" => {
|
|
|
|
if let Some(source) = breakpoint.source {
|
|
|
|
self.breakpoints
|
|
|
|
.entry(source.path.unwrap()) // TODO: no unwraps
|
|
|
|
.or_default()
|
|
|
|
.push(Breakpoint {
|
|
|
|
id: breakpoint.id,
|
|
|
|
verified: breakpoint.verified,
|
|
|
|
message: breakpoint.message,
|
|
|
|
line: breakpoint.line.unwrap().saturating_sub(1), // TODO: no unwrap
|
|
|
|
column: breakpoint.column,
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
"changed" => {
|
|
|
|
for breakpoints in self.breakpoints.values_mut() {
|
|
|
|
if let Some(i) =
|
|
|
|
breakpoints.iter().position(|b| b.id == breakpoint.id)
|
|
|
|
{
|
|
|
|
breakpoints[i].verified = breakpoint.verified;
|
|
|
|
breakpoints[i].message = breakpoint
|
|
|
|
.message
|
|
|
|
.clone()
|
|
|
|
.or_else(|| breakpoints[i].message.take());
|
|
|
|
breakpoints[i].line =
|
|
|
|
breakpoint.line.map_or(breakpoints[i].line, |line| {
|
|
|
|
line.saturating_sub(1)
|
|
|
|
});
|
|
|
|
breakpoints[i].column =
|
|
|
|
breakpoint.column.or(breakpoints[i].column);
|
|
|
|
}
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
"removed" => {
|
|
|
|
for breakpoints in self.breakpoints.values_mut() {
|
|
|
|
if let Some(i) =
|
|
|
|
breakpoints.iter().position(|b| b.id == breakpoint.id)
|
|
|
|
{
|
|
|
|
breakpoints.remove(i);
|
|
|
|
}
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
reason => {
|
|
|
|
warn!("Unknown breakpoint event: {}", reason);
|
|
|
|
}
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
Event::Output(events::OutputBody {
|
|
|
|
category, output, ..
|
|
|
|
}) => {
|
|
|
|
let prefix = match category {
|
|
|
|
Some(category) => {
|
|
|
|
if &category == "telemetry" {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
format!("Debug ({}):", category)
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
None => "Debug:".to_owned(),
|
|
|
|
};
|
2022-03-22 11:53:44 +08:00
|
|
|
|
2025-01-28 04:27:35 +08:00
|
|
|
log::info!("{}", output);
|
|
|
|
self.set_status(format!("{} {}", prefix, output));
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
Event::Initialized(_) => {
|
2025-06-23 22:48:05 +08:00
|
|
|
self.set_status("Debugger initialized...");
|
|
|
|
let debugger = match self.debug_adapters.get_client_mut(id) {
|
|
|
|
Some(debugger) => debugger,
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
|
2025-01-28 04:27:35 +08:00
|
|
|
// send existing breakpoints
|
|
|
|
for (path, breakpoints) in &mut self.breakpoints {
|
|
|
|
// TODO: call futures in parallel, await all
|
|
|
|
let _ = breakpoints_changed(debugger, path.clone(), breakpoints);
|
|
|
|
}
|
|
|
|
// TODO: fetch breakpoints (in case we're attaching)
|
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>
2023-02-20 12:00:00 +08:00
|
|
|
|
2025-01-28 04:27:35 +08:00
|
|
|
if debugger.configuration_done().await.is_ok() {
|
|
|
|
self.set_status("Debugged application started");
|
|
|
|
}; // TODO: do we need to handle error?
|
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>
2023-02-20 12:00:00 +08:00
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
Event::Terminated(terminated) => {
|
2025-06-23 22:48:05 +08:00
|
|
|
let debugger = match self.debug_adapters.get_client_mut(id) {
|
|
|
|
Some(debugger) => debugger,
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let restart_arg = if let Some(terminated) = terminated {
|
2025-01-28 04:27:35 +08:00
|
|
|
terminated.restart
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2025-06-23 22:48:05 +08:00
|
|
|
let restart_bool = restart_arg
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|v| v.as_bool())
|
|
|
|
.unwrap_or(false);
|
2025-01-28 04:27:35 +08:00
|
|
|
let disconnect_args = Some(DisconnectArguments {
|
2025-06-23 22:48:05 +08:00
|
|
|
restart: Some(restart_bool),
|
2025-01-28 04:27:35 +08:00
|
|
|
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;
|
|
|
|
}
|
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>
2023-02-20 12:00:00 +08:00
|
|
|
|
2025-06-23 22:48:05 +08:00
|
|
|
match restart_arg {
|
|
|
|
Some(Value::Bool(false)) | None => {
|
|
|
|
self.debug_adapters.remove_client(id);
|
|
|
|
self.debug_adapters.unset_active_client();
|
|
|
|
self.set_status(
|
|
|
|
"Terminated debugging session and disconnected debugger.",
|
|
|
|
);
|
|
|
|
|
|
|
|
// Go through all breakpoints and set verfified to false
|
|
|
|
// this should update the UI to show the breakpoints are no longer connected
|
|
|
|
for breakpoints in self.breakpoints.values_mut() {
|
|
|
|
for breakpoint in breakpoints.iter_mut() {
|
|
|
|
breakpoint.verified = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(val) => {
|
2025-01-28 04:27:35 +08:00
|
|
|
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
|
|
|
|
{
|
2025-06-23 22:48:05 +08:00
|
|
|
debugger.launch(val).await
|
2025-01-28 04:27:35 +08:00
|
|
|
} else {
|
2025-06-23 22:48:05 +08:00
|
|
|
debugger.attach(val).await
|
2025-01-28 04:27:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = relaunch_resp {
|
|
|
|
self.set_error(format!(
|
|
|
|
"Failed to restart debugging session: {:?}",
|
|
|
|
err
|
|
|
|
));
|
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>
2023-02-20 12:00:00 +08:00
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
}
|
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>
2023-02-20 12:00:00 +08:00
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
}
|
|
|
|
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})."
|
|
|
|
));
|
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>
2023-02-20 12:00:00 +08:00
|
|
|
}
|
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
ev => {
|
|
|
|
log::warn!("Unhandled event {:?}", ev);
|
|
|
|
return false; // return early to skip render
|
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>
2023-02-20 12:00:00 +08:00
|
|
|
}
|
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
}
|
2022-03-22 11:53:44 +08:00
|
|
|
Payload::Response(_) => unreachable!(),
|
2025-01-28 04:27:35 +08:00
|
|
|
Payload::Request(request) => {
|
|
|
|
let reply = match Request::parse(&request.command, request.arguments) {
|
|
|
|
Ok(Request::RunInTerminal(arguments)) => {
|
|
|
|
let config = self.config();
|
|
|
|
let Some(config) = config.terminal.as_ref() else {
|
2022-08-22 09:11:04 +08:00
|
|
|
self.set_error("No external terminal defined");
|
|
|
|
return true;
|
2025-01-28 04:27:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let process = match std::process::Command::new(&config.command)
|
|
|
|
.args(&config.args)
|
|
|
|
.arg(arguments.args.join(" "))
|
|
|
|
.spawn()
|
|
|
|
{
|
|
|
|
Ok(process) => process,
|
|
|
|
Err(err) => {
|
|
|
|
self.set_error(format!(
|
|
|
|
"Error starting external terminal: {}",
|
|
|
|
err
|
|
|
|
));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2022-08-22 09:11:04 +08:00
|
|
|
|
2025-01-28 04:27:35 +08:00
|
|
|
Ok(json!(dap::requests::RunInTerminalResponse {
|
|
|
|
process_id: Some(process.id()),
|
|
|
|
shell_process_id: None,
|
|
|
|
}))
|
|
|
|
}
|
2025-06-23 22:48:05 +08:00
|
|
|
Ok(Request::StartDebugging(arguments)) => {
|
|
|
|
let debugger = match self.debug_adapters.get_client_mut(id) {
|
|
|
|
Some(debugger) => debugger,
|
|
|
|
None => {
|
|
|
|
self.set_error("No active debugger found.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Currently we only support starting a child debugger if the parent is using the TCP transport
|
|
|
|
let socket = match debugger.socket {
|
|
|
|
Some(socket) => socket,
|
|
|
|
None => {
|
|
|
|
self.set_error("Child debugger can only be started if the parent debugger is using TCP transport.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let config = match debugger.config.clone() {
|
|
|
|
Some(config) => config,
|
|
|
|
None => {
|
|
|
|
error!("No configuration found for the debugger.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = self.debug_adapters.start_client(Some(socket), &config);
|
|
|
|
|
|
|
|
let client_id = match result {
|
|
|
|
Ok(child) => child,
|
|
|
|
Err(err) => {
|
|
|
|
self.set_error(format!(
|
|
|
|
"Failed to create child debugger: {:?}",
|
|
|
|
err
|
|
|
|
));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let client = match self.debug_adapters.get_client_mut(client_id) {
|
|
|
|
Some(child) => child,
|
|
|
|
None => {
|
|
|
|
self.set_error("Failed to get child debugger.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let relaunch_resp = if let ConnectionType::Launch = arguments.request {
|
|
|
|
client.launch(arguments.configuration).await
|
|
|
|
} else {
|
|
|
|
client.attach(arguments.configuration).await
|
|
|
|
};
|
|
|
|
if let Err(err) = relaunch_resp {
|
|
|
|
self.set_error(format!("Failed to start debugging session: {:?}", err));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(json!({
|
|
|
|
"success": true,
|
|
|
|
}))
|
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
Err(err) => Err(err),
|
|
|
|
};
|
2022-03-22 11:53:44 +08:00
|
|
|
|
2025-06-23 22:48:05 +08:00
|
|
|
if let Some(debugger) = self.debug_adapters.get_client_mut(id) {
|
2025-01-28 04:27:35 +08:00
|
|
|
debugger
|
|
|
|
.reply(request.seq, &request.command, reply)
|
|
|
|
.await
|
|
|
|
.ok();
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
2025-01-28 04:27:35 +08:00
|
|
|
}
|
2022-03-22 11:53:44 +08:00
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|