-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
235 lines (197 loc) · 5.96 KB
/
extension.js
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
const vscode = require("vscode");
const { exec } = require("child_process");
const os = require("os");
const path = require("path");
const fs = require("fs");
let breakpointMap = new Map();
// This starts from 1 because in lldb breakpoints start from index 1.
let bpCounter = 1;
function getCommandPath(command) {
const homeDir = os.homedir();
const agaveLedgerToolPath = path.join(
homeDir,
".local",
"share",
"solana",
"install",
"active_release",
"bin",
"agave-ledger-tool"
);
const solanalldbPath = path.join(
homeDir,
".local",
"share",
"solana",
"install",
"active_release",
"bin",
"sdk",
"sbf",
"dependencies",
"platform-tools",
"llvm",
"bin",
"solana-lldb"
);
const customSolanalldbPath = vscode.workspace
.getConfiguration("solanaDebugger")
.get("solanaLldbPath");
if (customSolanalldbPath) {
solanalldbPath = customSolanalldbPath;
}
if (command.includes("agave-ledger-tool")) {
return agaveLedgerToolPath;
} else if (command.includes("solana-lldb")) {
return solanalldbPath;
} else {
vscode.window.showErrorMessage(`Unknown command: ${command}`);
return "";
}
}
function runCommand(command, args = "") {
const commandPath = getCommandPath(command);
if (!commandPath) {
vscode.window.showErrorMessage(`Command path for ${command} not found.`);
return;
}
const terminal = vscode.window.createTerminal(`Run ${command}`);
terminal.show();
terminal.sendText(`${commandPath} ${args}`);
}
function startSolanaDebugger() {
const workspaceFolder = vscode.workspace.workspaceFolders[0].uri.fsPath;
const projectFolderName = path.basename(workspaceFolder);
const depsPath = `${workspaceFolder}/target/debug/deps`;
vscode.window.terminals.forEach((terminal) => {
if (terminal.name === "Solana Debugger") {
terminal.dispose();
}
});
if (!fs.existsSync(depsPath)) {
vscode.window.showInformationMessage(
"Target folder not found. Cargo is installing necessary tools."
);
}
exec(
`cargo test --no-run --lib --package=${projectFolderName}`,
{ cwd: workspaceFolder },
(err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage(`Build error: ${stderr}`);
return;
}
try {
if (!fs.existsSync(depsPath)) {
vscode.window.showErrorMessage(`Executable not found: ${depsPath}`);
return;
}
fs.readdir(depsPath, (readDirErr, files) => {
if (readDirErr) {
vscode.window.showErrorMessage(
`Error reading directory: ${readDirErr}`
);
return;
}
const transformedProjectFolderName = projectFolderName.replace(
/-/g,
"_"
);
const executableFile = files.find(
(file) =>
file.startsWith(`${transformedProjectFolderName}-`) &&
!file.includes(".")
);
const executablePath = `${depsPath}/${executableFile}`;
console.log(projectFolderName);
console.log(`Executable path: ${executablePath}`);
console.log(`Executable file: ${executableFile}`);
bpCounter = 1;
const debuggerCommand = "solana-lldb";
const terminal = vscode.window.createTerminal("Solana Debugger");
terminal.show();
terminal.sendText(debuggerCommand);
setTimeout(() => {
terminal.sendText(`target create ${executablePath}`);
terminal.sendText("process launch -- --nocapture");
}, 500);
vscode.debug.onDidChangeBreakpoints((event) => {
if (event.added.length > 0) {
event.added.forEach((bp) => {
const line = bp.location.range.start.line + 1;
terminal.sendText(
`breakpoint set --file ${bp.location.uri.fsPath} --line ${line}`
);
breakpointMap.set(bp.id, bpCounter);
bpCounter++;
});
}
if (event.removed.length > 0) {
event.removed.forEach((bp) => {
const breakpoint = breakpointMap.get(bp.id);
if (breakpoint) {
terminal.sendText(`breakpoint delete ${breakpoint}`);
breakpointMap.delete(bp.id);
}
});
}
});
});
} catch (e) {
vscode.window.showErrorMessage(`Error: ${e}`);
vscode.window.showErrorMessage(`Stderr Stack: ${stderr}`);
}
}
);
}
function reRunProcessLaunch() {
const terminal = vscode.window.terminals.find(
(t) => t.name === "Solana Debugger"
);
if (terminal) {
terminal.sendText("process launch -- --nocapture");
} else {
vscode.window.showErrorMessage("Solana Debugger terminal not found.");
}
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// This line of code will only be executed once when your extension is activated
console.log("Solana Step Debugger is now active!");
const disposable = vscode.commands.registerCommand(
"extension.runAgaveLedgerTool",
() => {
vscode.window
.showInputBox({ prompt: "Enter agave-ledger-tool subcommand" })
.then((subcommand) => {
if (subcommand) {
runCommand("agave-ledger-tool", subcommand);
} else {
vscode.window.showErrorMessage("No subcommand provided.");
}
});
}
);
context.subscriptions.push(disposable);
const disposable2 = vscode.commands.registerCommand(
"extension.runSolanaLLDB",
() => {
startSolanaDebugger();
}
);
context.subscriptions.push(disposable2);
const disposable3 = vscode.commands.registerCommand(
"extension.reRunProcessLaunch",
() => {
reRunProcessLaunch();
}
);
context.subscriptions.push(disposable3);
}
function deactivate() {}
module.exports = {
activate,
deactivate,
};