diff --git a/.vscode/launch.json b/.vscode/launch.json index d71e83dd1f..f86e436e03 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,13 +5,8 @@ "type": "cider-dap", "request": "launch", "name": "Launch Program (Multi Session)", - "debugServer": 8888, - "program": "${file}", - "preLaunchTask": { - "type": "shell", - "command": "${command:cider.startDebugging}" - }, - "stopOnEntry": true, + "program": "${workspaceFolder}/${file}", + // "stopOnEntry": true, }, { "type": "cider-dap", diff --git a/cider-dap/calyxDebug/extension.js b/cider-dap/calyxDebug/extension.js index 2115ebafaf..32022b0b21 100644 --- a/cider-dap/calyxDebug/extension.js +++ b/cider-dap/calyxDebug/extension.js @@ -1,41 +1,84 @@ const vscode = require('vscode'); const cp = require('child_process'); -const { config } = require('process'); - +const net = require('net'); +// Hold the debug adapter instance +let debugAdapter = null; +let programName = null; // Store the program name // Create output channel let outputChannel = vscode.window.createOutputChannel("cider dap"); +function logToPanel(message) { + console.log("inside logPanel"); + outputChannel.appendLine(message); +} + +// Function to get the program name from the user +async function getProgramName() { + const fileName = await vscode.window.showInputBox({ + placeHolder: 'Please enter the name of a futil file in the workspace folder', + value: 'default.futil' + }); + if (fileName) { + if (!fileName.startsWith('/')) { + const path = require('path'); + return path.join(vscode.workspace.workspaceFolders[0].uri.fsPath, fileName); + } + return fileName; + } else { + return null; + } +} + +class CiderDebugAdapterDescriptorFactory { + constructor(adapterPath, workspace, outputChannel) { + logToPanel("inside constructor"); + this.adapter = new CiderDebugAdapter(adapterPath, workspace, outputChannel); + this.adapterPath = adapterPath; + this.workspace = workspace; + this.outputChannel = outputChannel; + } + + createDebugAdapterDescriptor(session) { + // Return a new debug adapter descriptor + logToPanel("creating adapter descriptor"); + return new vscode.DebugAdapterServer(this._startDebugServer(session)); + } + + _startDebugServer(session) { + logToPanel("start of startDebugServer"); + const port = 8888; // This is the default value + + if (!this.adapter.isServerRunning()) { + logToPanel("server is not running"); + this.adapter.start(port); + logToPanel("started dap-server"); + } + + logToPanel("exiting startDebugging"); + return port; + } +} class CiderDebugAdapter { constructor(adapterPath, cwd, outputChannel) { + logToPanel("inside CiderDebugAdapter"); this.adapterPath = adapterPath; this.cwd = cwd; this.outputChannel = outputChannel; this.adapterProcess = null; + logToPanel("at the end of ciderDebugAdapter"); + } + isServerRunning() { + logToPanel("checking if server is running"); + return this.adapterProcess != null && this.adapterProcess.exitCode == null; } - - // Start the debug adapter process - async start(port) { // accept the port parameter - logToPanel('Debugger starting...'); - // Get the program name from the user - const programName = await getProgramName(); - - if (!programName) { - logToPanel('No program selected. Aborting debugging.'); - return; - } - // Verify if the file exists at the provided path - const fs = require('fs'); - if (!fs.existsSync(programName)) { - logToPanel(`File not found: ${programName}`); - return; - } - + start(port) { + logToPanel('begining of start'); // Spawn a new child process for the debug adapter // Include the port as a command line argument - this.adapterProcess = cp.spawn(this.adapterPath, [programName, '--port', port, "--tcp"], { cwd: this.cwd }); + this.adapterProcess = cp.spawn(this.adapterPath, ['--port', port, "--tcp"], { cwd: this.cwd }); // Attach event listener to capture standard output of the adapter process and log it to the output channel this.adapterProcess.stdout.on('data', (data) => { @@ -50,8 +93,6 @@ class CiderDebugAdapter { logToPanel('Debugger started on port ' + port + '!'); } - - // Stop debug adapter process stop() { if (this.adapterProcess) { this.adapterProcess.kill(); @@ -64,60 +105,24 @@ class CiderDebugAdapter { } } - -// Hold the debug adapter instance -let debugAdapter = null; -let programName = null; // Store the program name - - -function logToPanel(message) { - console.log("inside logPanel"); - outputChannel.appendLine(message); -} - - -// Function to get the program name from the user -async function getProgramName() { - const fileName = await vscode.window.showInputBox({ - placeHolder: 'Please enter the name of a futil file in the workspace folder', - value: 'default.futil' - }); - - if (fileName) { - if (!fileName.startsWith('/')) { - const path = require('path'); - return path.join(vscode.workspace.workspaceFolders[0].uri.fsPath, fileName); - } - return fileName; - } else { - return null; - } -} -// Start debugging -async function startDebugging(arg) { - logToPanel("inside startDebugging"); - - if (!debugAdapter) { - const adapterPath = '/home/basantkhalil/calyx2/target/debug/cider-dap'; - const cwd = vscode.workspace.rootPath; - - debugAdapter = new CiderDebugAdapter(adapterPath, cwd, outputChannel); - } - // Prompt for the port - const portInput = await vscode.window.showInputBox({ - placeHolder: 'Please enter the port number', - value: '8888' // This is the default value - }); - - // If the user entered a value, parse it to an integer - const port = portInput ? parseInt(portInput, 10) : 1234; - await debugAdapter.start(port); - logToPanel("exiting startDebugging"); - return; +function activate(context) { + logToPanel('Extension activated!'); + + // Start the debug server explicitly + const factory = new CiderDebugAdapterDescriptorFactory('/home/basantkhalil/calyx2/target/debug/cider-dap', vscode.workspace.rootPath, outputChannel); + context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory('cider-dap', factory)); + logToPanel("after start server"); + + // Update the adapter path with the serverPort and use it for starting the debug adapter + const adapterPath = '/home/basantkhalil/calyx2/target/debug/cider-dap'; + const cwd = vscode.workspace.rootPath; + logToPanel("before startDebugging"); + /* context.subscriptions.push(vscode.commands.registerCommand('cider.startDebugging', startDebugging)); + context.subscriptions.push(vscode.commands.registerCommand('cider.stopDebugging', stopDebugging)); + */ + logToPanel('Hello, your extension is now activated!'); } - -// Stop debugging function stopDebugging() { if (debugAdapter) { debugAdapter.stop(); @@ -126,30 +131,11 @@ function stopDebugging() { } } - -// Activate the extension -function activate(context) { - logToPanel("Extension activated!"); - - let disposableStart = vscode.commands.registerCommand('cider.startDebugging', startDebugging); - context.subscriptions.push(disposableStart); - - let disposableStop = vscode.commands.registerCommand('cider.stopDebugging', stopDebugging); - context.subscriptions.push(disposableStop); - - logToPanel('Hello, your extension is now activated!'); -} - - function deactivate() { logToPanel("deactivate"); } - module.exports = { activate, deactivate -}; - - - +}; \ No newline at end of file diff --git a/cider-dap/src/main.rs b/cider-dap/src/main.rs index 28c8a7c1d4..9b8389713f 100644 --- a/cider-dap/src/main.rs +++ b/cider-dap/src/main.rs @@ -30,9 +30,6 @@ fn read_path(path: &str) -> Result { fn main() -> Result<(), MyAdapterError> { let opts: Opts = argh::from_env(); - let path = opts.file.ok_or(MyAdapterError::MissingFile)?; - let file = File::open(path)?; - let adapter = MyAdapter::new(file); if opts.is_multi_session { eprintln!("running multi-session"); @@ -42,10 +39,13 @@ fn main() -> Result<(), MyAdapterError> { println!("Accepted client on: {}", addr); let read_stream = BufReader::new(stream.try_clone()?); let write_stream = BufWriter::new(stream); - let server = Server::new(read_stream, write_stream); - - run_server(server, adapter)?; + let mut server = Server::new(read_stream, write_stream); + dbg!(server.poll_request()); + // run_server(server, adapter)?; } else { + let path = opts.file.ok_or(MyAdapterError::MissingFile)?; + let file = File::open(path)?; + let adapter = MyAdapter::new(file); eprintln!("running single-session"); let write = BufWriter::new(stdout()); let read = BufReader::new(stdin());