Skip to content

Commit

Permalink
it can attach to vscode currently in multi session
Browse files Browse the repository at this point in the history
  • Loading branch information
Basant-khalil committed Aug 3, 2023
1 parent 94d9148 commit 2051b8d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 108 deletions.
9 changes: 2 additions & 7 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
176 changes: 81 additions & 95 deletions cider-dap/calyxDebug/extension.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -50,8 +93,6 @@ class CiderDebugAdapter {
logToPanel('Debugger started on port ' + port + '!');
}


// Stop debug adapter process
stop() {
if (this.adapterProcess) {
this.adapterProcess.kill();
Expand All @@ -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();
Expand All @@ -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
};



};
12 changes: 6 additions & 6 deletions cider-dap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ fn read_path(path: &str) -> Result<PathBuf, String> {

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");
Expand All @@ -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());

Check failure on line 43 in cider-dap/src/main.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

unused `std::result::Result` that must be used
// 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());
Expand Down

0 comments on commit 2051b8d

Please sign in to comment.