Skip to content

Commit 48e900c

Browse files
committed
[lldb-dap] Support vscode launch URLs
This commit adds support for starting debug sessions through special `vscode://llvm-vs-code-extensions.lldb-dap/launch/config?config={launch-config}` URIs. This allows tighter integration with custom scripts. One potential use case is providing similar functionality to `xcdebug`, see #125777 for some discussion on that use case. The functionality was inspired by @vadimcn's CodeLLDB extension, which [provides similar functionality](https://github.com/vadimcn/codelldb/blob/master/MANUAL.md#debugging-externally-launched-code).
1 parent 810150b commit 48e900c

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

lldb/tools/lldb-dap/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ The default hostname being used `localhost`.
174174
}
175175
```
176176

177+
### Launching via `vscode://` URIs
178+
179+
Debugging sessions can also be starting using special URIs.
180+
181+
The `vscode://llvm-vs-code-extensions.lldb-dap/launch/config?config={launch-config}`
182+
URI accepts a [URL-encoded](https://en.wikipedia.org/wiki/Percent-encoding)
183+
JSON launch config.
184+
185+
This is useful, e.g., to integrate with custom scripts which start debugging
186+
sessions. The URIs might be printed to the terminal, potentially using
187+
[OSC-8 hyperlinks](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda),
188+
or passed to `vscode --open-url` or `xdg-open`, although mileage may vary
189+
depending on your specific debugging setup.
190+
177191
### Configuration Settings Reference
178192

179193
For both launch and attach configurations, lldb-dap accepts the following `lldb-dap`
@@ -328,4 +342,4 @@ The source code is part of the [LLVM repository](https://github.com/llvm/llvm-pr
328342
We use Github's [issue tracker](https://github.com/llvm/llvm-project/issues?q=label%3Alldb-dap) and patches can be submitted via [pull requests](https://github.com/llvm/llvm-project/pulls?q=label%3Alldb-dap).
329343
Furthermore, there is a [LLDB category](https://discourse.llvm.org/c/subprojects/lldb/8) on the LLVM discourse forum.
330344

331-
For instructions on how to get started with development on lldb-dap, see the "[Contributing to lldb-dap](https://lldb.llvm.org/resources/lldbdap.html)"
345+
For instructions on how to get started with development on lldb-dap, see the "[Contributing to lldb-dap](https://lldb.llvm.org/resources/lldbdap.html)" guide.

lldb/tools/lldb-dap/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"typescript": "^5.7.3"
3636
},
3737
"activationEvents": [
38-
"onDebug"
38+
"onDebug",
39+
"onUri"
3940
],
4041
"main": "./out/extension",
4142
"scripts": {

lldb/tools/lldb-dap/src-ts/extension.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
isExecutable,
66
} from "./debug-adapter-factory";
77
import { DisposableContext } from "./disposable-context";
8+
import { LaunchUriHandler } from "./uri-launch-handler";
89

910
/**
1011
* This class represents the extension and manages its life cycle. Other extensions
@@ -37,6 +38,10 @@ export class LLDBDapExtension extends DisposableContext {
3738
}
3839
}),
3940
);
41+
42+
this.pushSubscription(
43+
vscode.window.registerUriHandler(new LaunchUriHandler())
44+
);
4045
}
4146
}
4247

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as vscode from "vscode";
2+
3+
export class LaunchUriHandler implements vscode.UriHandler {
4+
async handleUri(uri: vscode.Uri) {
5+
try {
6+
const params = new URLSearchParams(uri.query);
7+
if (uri.path == '/launch/config') {
8+
const configJson = params.get("config");
9+
if (configJson === null) {
10+
throw new Error("Missing `config` URI parameter");
11+
}
12+
// Build the debug config
13+
let debugConfig: vscode.DebugConfiguration = {
14+
type: 'lldb-dap',
15+
request: 'launch',
16+
name: '',
17+
};
18+
Object.assign(debugConfig, JSON.parse(configJson));
19+
debugConfig.name = debugConfig.name || debugConfig.program || "Adhoc Launch";
20+
// Force the type to `lldb-dap`. We don't want to allow launching any other
21+
// Debug Adapters using this URI scheme.
22+
if (debugConfig.type != "lldb-dap") {
23+
throw new Error(`Unsupported debugger type: ${debugConfig.type}`);
24+
}
25+
await vscode.debug.startDebugging(undefined, debugConfig);
26+
} else {
27+
throw new Error(`Unsupported Uri path: ${uri.path}`);
28+
}
29+
} catch (err) {
30+
if (err instanceof Error) {
31+
await vscode.window.showErrorMessage(`Failed to handle lldb-dap URI request: ${err.message}`);
32+
} else {
33+
await vscode.window.showErrorMessage(`Failed to handle lldb-dap URI request`);
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)