1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//! Starting and ending a session: spawning or reaching each adapter (debugpy, lldb-dap,
//! delve, vscode-js-debug, a server already listening), where delve builds the program,
//! the fresh session around its transport, and disconnecting.
use super::*;
impl DapSession {
/// Spawn the adapter, send `initialize`, and stash the breakpoints to push
/// when the `initialized` event arrives. `adapter_*` describe how to launch
/// the DAP adapter (e.g. the debug-venv python with `-m debugpy.adapter`);
/// `program`/`interpreter` describe the debuggee.
#[allow(clippy::too_many_arguments)]
pub fn launch(
adapter_program: &str,
adapter_args: &[String],
cwd: &Path,
program: &Path,
interpreter: &Path,
breakpoints: BTreeMap<PathBuf, Vec<SourceBreakpoint>>,
stop_on_entry: bool,
) -> Result<DapSession> {
Self::launch_with(
adapter_program,
adapter_args,
cwd,
launch_request(program, interpreter, stop_on_entry),
breakpoints,
)
}
/// Generic launch: spawn `adapter_program args...`, send `initialize`, then
/// the caller-built `launch_request` (debugpy or lldb-dap shaped). The rest
/// of the protocol (breakpoints, configurationDone, stepping, inspection) is
/// adapter-agnostic, so every adapter shares this path.
pub fn launch_with(
adapter_program: &str,
adapter_args: &[String],
cwd: &Path,
launch_request: Value,
breakpoints: BTreeMap<PathBuf, Vec<SourceBreakpoint>>,
) -> Result<DapSession> {
let transport = DapTransport::spawn(adapter_program, adapter_args, cwd)?;
transport.send(initialize_request(false))?;
let attached = is_attach(&launch_request);
transport.send(launch_request)?;
let mut session = Self::new_with_transport(transport, breakpoints, None);
session.attached = attached;
Ok(session)
}
/// Attach to a DAP server that is already listening at `host:port`
/// (#1417): a `debugpy --listen` process, whose port is its own adapter.
/// No adapter is spawned; `initialize` and `attach_request` go straight
/// to the socket.
pub fn attach_tcp(
host: &str,
port: u16,
attach_request: Value,
breakpoints: BTreeMap<PathBuf, Vec<SourceBreakpoint>>,
) -> Result<DapSession> {
let transport = DapTransport::connect_tcp_once(host, port)?;
transport.send(initialize_request(false))?;
transport.send(attach_request)?;
let mut session = Self::new_with_transport(transport, breakpoints, None);
session.attached = true;
Ok(session)
}
/// Launch a vscode-js-debug session: spawn `node dapDebugServer.js <port>
/// <host>` as a TCP debug server, connect the parent socket, and drive the
/// `pwa-node` launch. The parent will later issue a `startDebugging` reverse
/// request (handled in [`DapSession::poll`]) that opens the child session
/// which actually runs `program` and binds breakpoints.
pub fn launch_js(
node: &str,
server_js: &Path,
cwd: &Path,
program: &Path,
breakpoints: BTreeMap<PathBuf, Vec<SourceBreakpoint>>,
stop_on_entry: bool,
) -> Result<DapSession> {
Self::launch_js_with(
node,
server_js,
cwd,
js_launch_request(program, cwd, stop_on_entry),
breakpoints,
)
}
/// Like [`DapSession::launch_js`] but with a caller-built session-starting
/// request (a launch.json config's `launch` or `attach`), mirroring what
/// [`DapSession::launch_with`] is to the stdio adapters.
pub fn launch_js_with(
node: &str,
server_js: &Path,
cwd: &Path,
start_request: Value,
breakpoints: BTreeMap<PathBuf, Vec<SourceBreakpoint>>,
) -> Result<DapSession> {
let host = "127.0.0.1";
let port = crate::dap::transport::free_port()?;
let args = vec![
server_js.to_string_lossy().into_owned(),
port.to_string(),
host.to_string(),
];
let node_dir = crate::dap::install::node_bin_dir(node);
let transport =
DapTransport::connect_tcp_server(node, &args, cwd, host, port, node_dir.as_deref())?;
transport.send(initialize_request(true))?;
transport.send(start_request)?;
Ok(Self::new_with_transport(
transport,
breakpoints,
Some((host.to_string(), port)),
))
}
/// Launch a delve session (#264): spawn `dlv dap --listen=<host:port>`,
/// connect over TCP, and send `start_request`. delve serves one session
/// per connection, so unlike js-debug there is no child to open later.
pub fn launch_delve(
dlv: &Path,
cwd: &Path,
start_request: Value,
breakpoints: BTreeMap<PathBuf, Vec<SourceBreakpoint>>,
) -> Result<DapSession> {
let build_dir = delve_builds(&start_request)
.then(DebugBuildDir::create)
.and_then(std::io::Result::ok);
let start_request = match &build_dir {
Some(dir) => with_build_output(start_request, dir.path()),
None => start_request,
};
let host = "127.0.0.1";
let port = crate::dap::transport::free_port()?;
let args = vec![String::from("dap"), format!("--listen={host}:{port}")];
let transport =
// delve runs `go build`: the Go croft found, put on its PATH
// when croft's own does not reach it (#2807).
DapTransport::connect_tcp_server(
&dlv.to_string_lossy(),
&args,
cwd,
host,
port,
crate::proc::find::go_dir_off_path().as_deref(),
)?;
transport.send(initialize_request(false))?;
let attached = is_attach(&start_request);
transport.send(with_remote_output(start_request))?;
let mut session = Self::new_with_transport(transport, breakpoints, None);
session.attached = attached;
session.build_dir = build_dir;
Ok(session)
}
/// Attach to a headless delve that already serves DAP at `host:port`
/// (#1418, the Go extension's "Connect to server"): no dlv is started,
/// `initialize` and the `mode: "remote"` attach go straight to the
/// socket, and stopping disconnects with `terminateDebuggee: false`.
/// Delve keeps the remote program running only when it was started
/// with `--accept-multiclient`; a single-client server shuts down with
/// its debuggee on disconnect.
pub fn attach_delve_server(
host: &str,
port: u16,
attach_request: Value,
breakpoints: BTreeMap<PathBuf, Vec<SourceBreakpoint>>,
) -> Result<DapSession> {
let transport = DapTransport::connect_tcp(host, port)?;
transport.send(initialize_request(false))?;
transport.send(attach_request)?;
let mut session = Self::new_with_transport(transport, breakpoints, None);
session.attached = true;
Ok(session)
}
/// Assemble a fresh session around its (parent) `transport`. `js_server` is
/// set only for vscode-js-debug, marking the session multi-session and
/// carrying the address its child connection reuses.
pub(super) fn new_with_transport(
transport: DapTransport,
breakpoints: BTreeMap<PathBuf, Vec<SourceBreakpoint>>,
js_server: Option<(String, u16)>,
) -> DapSession {
DapSession {
transport,
child: None,
js_server,
phase: SessionPhase::Initializing,
launch_error: None,
breakpoints,
stopped_thread: None,
threads: Vec::new(),
current_location: None,
unverified_breakpoints: BTreeMap::new(),
stack_frames: Vec::new(),
scopes: Vec::new(),
open_expensive_scopes: std::collections::BTreeSet::new(),
variables: BTreeMap::new(),
selected_frame: None,
pending_var_refs: std::collections::HashMap::new(),
pending_evals: std::collections::HashMap::new(),
exception_info_seq: None,
step_in_targets_seq: None,
exception_filters: vec![String::from("uncaught")],
pending_raised: None,
known_thread: None,
capabilities: Value::Null,
function_breakpoints: Vec::new(),
run_to: None,
data_breakpoints: Vec::new(),
pending_data_info: std::collections::HashMap::new(),
attached: false,
build_dir: None,
}
}
/// Ask the adapter to disconnect and terminate the debuggee. Sent to both the
/// child (if any) and the parent so a js-debug server tears the whole tree
/// down, not just the target. An attached session leaves its process
/// running: croft did not start it, and delve and lldb-dap both honour
/// `terminateDebuggee` for an attach.
pub fn disconnect(&mut self) {
let req = disconnect_request(self.attached);
if let Some(child) = self.child.as_ref() {
let _ = child.send(req.clone());
}
let _ = self.transport.send(req);
self.phase = SessionPhase::Terminated;
self.exception_info_seq = None;
}
}
/// Whether delve builds the program for this request: a `launch` in
/// `debug`, `test` or `auto` mode (delve's default is `debug`) whose
/// launch.json names no `output`. An `exec`, a core dump or an attach
/// builds nothing (#1206).
pub fn delve_builds(request: &Value) -> bool {
let args = &request["arguments"];
request["command"] == "launch"
&& args.get("output").is_none()
&& matches!(
args["mode"].as_str(),
None | Some("debug" | "test" | "auto")
)
}
/// The launch request with delve's `output` set to a `__debug_bin` in `dir`,
/// so the binary it builds lands outside the workspace. Left in the package
/// folder, it stayed behind whenever croft ended delve before delve could
/// delete it, showing in the Explorer and as untracked in git (#1206).
pub fn with_build_output(mut request: Value, dir: &Path) -> Value {
let bin = dir.join(format!("__debug_bin{}", std::env::consts::EXE_SUFFIX));
if let Some(args) = request.get_mut("arguments").and_then(Value::as_object_mut) {
args.insert("output".into(), json!(bin.to_string_lossy()));
}
request
}