Skip to content

Commit 3bebd59

Browse files
committed
Add attach to process command (#604)
1 parent 6e6440b commit 3bebd59

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed

package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@
111111
},
112112
{
113113
"command": "swift.switchPlatform",
114-
"title": "Select Target Platform",
114+
"title": "Select Target Platform...",
115115
"category": "Swift"
116116
},
117117
{
118118
"command": "swift.selectXcodeDeveloperDir",
119-
"title": "Select Xcode Developer Dir",
119+
"title": "Select Xcode Developer Dir...",
120120
"category": "Swift"
121121
},
122122
{
@@ -148,6 +148,11 @@
148148
"command": "swift.toggleTestCoverage",
149149
"title": "Toggle Display of Test Coverage Results",
150150
"category": "Swift"
151+
},
152+
{
153+
"command": "swift.attachDebugger",
154+
"title": "Attach to Process...",
155+
"category": "Swift"
151156
}
152157
],
153158
"configuration": [
@@ -537,6 +542,10 @@
537542
{
538543
"command": "swift.runPluginTask",
539544
"when": "swift.packageHasPlugins"
545+
},
546+
{
547+
"command": "swift.attachDebugger",
548+
"when": "swift.lldbVSCodeAvailable"
540549
}
541550
],
542551
"editor/context": [
@@ -866,8 +875,7 @@
866875
},
867876
"pid": {
868877
"type": [
869-
"number",
870-
"string"
878+
"number"
871879
],
872880
"description": "System process ID to attach to."
873881
},

src/commands.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import { createSwiftTask, SwiftTaskProvider } from "./SwiftTaskProvider";
2121
import { FolderContext } from "./FolderContext";
2222
import { PackageNode } from "./ui/PackageDependencyProvider";
2323
import { withQuickPick } from "./ui/QuickPick";
24-
import { execSwift } from "./utilities/utilities";
24+
import { execSwift, getErrorDescription } from "./utilities/utilities";
2525
import { Version } from "./utilities/version";
2626
import { DarwinCompatibleTarget, SwiftToolchain } from "./toolchain/toolchain";
2727
import { debugSnippet, runSnippet } from "./SwiftSnippets";
2828
import { debugLaunchConfig, getLaunchConfiguration } from "./debugger/launch";
29+
import { execFile } from "./utilities/utilities";
2930

3031
/**
3132
* References:
@@ -620,6 +621,39 @@ function toggleTestCoverageDisplay(workspaceContext: WorkspaceContext) {
620621
workspaceContext.toggleTestCoverageDisplay();
621622
}
622623

624+
async function attachDebugger(workspaceContext: WorkspaceContext) {
625+
// use LLDB to get list of processes
626+
const lldb = workspaceContext.toolchain.getToolchainExecutable("lldb");
627+
try {
628+
const { stdout } = await execFile(lldb, [
629+
"--batch",
630+
"--no-lldbinit",
631+
"--one-line",
632+
"platform process list --show-args --all-users",
633+
]);
634+
const entries = stdout.split("\n");
635+
const processPickItems = entries.flatMap(line => {
636+
const match = /^(\d+)\s+\d+\s+\S+\s+\S+\s+(.+)$/.exec(line);
637+
if (match) {
638+
return [{ pid: parseInt(match[1]), label: `${match[1]}: ${match[2]}` }];
639+
} else {
640+
return [];
641+
}
642+
});
643+
await withQuickPick("Select Process", processPickItems, async selected => {
644+
const debugConfig: vscode.DebugConfiguration = {
645+
type: "swift-lldb",
646+
request: "attach",
647+
name: "Attach",
648+
pid: selected.pid,
649+
};
650+
await vscode.debug.startDebugging(undefined, debugConfig);
651+
});
652+
} catch (error) {
653+
vscode.window.showErrorMessage(`Failed to run LLDB: ${getErrorDescription(error)}`);
654+
}
655+
}
656+
623657
function updateAfterError(result: boolean, folderContext: FolderContext) {
624658
const triggerResolvedUpdatedEvent = folderContext.hasResolveErrors;
625659
// set has resolve errors flag
@@ -689,6 +723,7 @@ export function register(ctx: WorkspaceContext) {
689723
}),
690724
vscode.commands.registerCommand("swift.selectXcodeDeveloperDir", () =>
691725
selectXcodeDeveloperDir()
692-
)
726+
),
727+
vscode.commands.registerCommand("swift.attachDebugger", () => attachDebugger(ctx))
693728
);
694729
}

src/contextKeys.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ const contextKeys = {
5959
set fileIsSnippet(value: boolean) {
6060
vscode.commands.executeCommand("setContext", "swift.fileIsSnippet", value);
6161
},
62+
63+
/**
64+
* Whether current active file is a Snippet
65+
*/
66+
set lldbVSCodeAvailable(value: boolean) {
67+
vscode.commands.executeCommand("setContext", "swift.lldbVSCodeAvailable", value);
68+
},
6269
};
6370

6471
export default contextKeys;

src/debugger/debugAdapter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ import * as vscode from "vscode";
1616
import * as fs from "fs";
1717
import { WorkspaceContext } from "../WorkspaceContext";
1818
import configuration from "../configuration";
19+
import contextKeys from "../contextKeys";
1920

2021
/**
2122
* Class managing which debug adapter we are using. Will only setup lldb-vscode if it is available.
2223
*/
2324
export class DebugAdapter {
24-
static debugAdapaterExists = false;
25+
private static debugAdapaterExists = false;
2526

2627
/** Debug adapter name */
2728
static get adapterName(): string {
@@ -54,9 +55,11 @@ export class DebugAdapter {
5455
}
5556
workspace.outputChannel.log(`Failed to find ${lldbDebugAdapterPath}`);
5657
this.debugAdapaterExists = false;
58+
contextKeys.lldbVSCodeAvailable = false;
5759
return false;
5860
}
5961
this.debugAdapaterExists = true;
62+
contextKeys.lldbVSCodeAvailable = true;
6063
return true;
6164
}
6265

0 commit comments

Comments
 (0)