Skip to content

Commit

Permalink
try it with multi-session
Browse files Browse the repository at this point in the history
  • Loading branch information
Basant-khalil committed Aug 2, 2023
1 parent b853568 commit 94d9148
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 49 deletions.
12 changes: 7 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
{
"type": "cider-dap",
"request": "launch",
"name": "Launch Program (Single Session)",
"args": [
"${file}"
],
"program": "${command:cider.startDebugging}",
"name": "Launch Program (Multi Session)",
"debugServer": 8888,
"program": "${file}",
"preLaunchTask": {
"type": "shell",
"command": "${command:cider.startDebugging}"
},
"stopOnEntry": true,
},
{
Expand Down
89 changes: 47 additions & 42 deletions cider-dap/calyxDebug/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ const vscode = require('vscode');
const cp = require('child_process');
const { config } = require('process');


// Create output channel
let outputChannel = vscode.window.createOutputChannel("cider dap");

// Class for debug adapter process

class CiderDebugAdapter {
constructor(adapterPath, cwd, outputChannel) {
this.adapterPath = adapterPath;
this.cwd = cwd;
this.outputChannel = outputChannel;
this.adapterProcess = null;
}


// Start the debug adapter process
async start() {
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)) {
Expand All @@ -33,7 +34,8 @@ class CiderDebugAdapter {
}

// Spawn a new child process for the debug adapter
this.adapterProcess = cp.spawn(this.adapterPath, [programName], { cwd: this.cwd });
// Include the port as a command line argument
this.adapterProcess = cp.spawn(this.adapterPath, [programName, '--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 @@ -45,60 +47,35 @@ class CiderDebugAdapter {
logToPanel(data.toString());
});

logToPanel('Debugger started!');
logToPanel('Debugger started on port ' + port + '!');
}


// Stop debug adapter process
stop() {
if (this.adapterProcess) {
// Terminate the adapter process and set it to null
this.adapterProcess.kill();
this.adapterProcess = null;
this.isRunning = false;
logToPanel('Debugger stopped.');
} else {
logToPanel('No running debug adapter to stop.');
}
}
}

// Start debugging
async function startDebugging(arg1) {
logToPanel("inside startDebugging");
console.log(arg1);
if (!debugAdapter) {
// Set the path to the debug adapter and current working directory
const adapterPath = '/home/basantkhalil/calyx2/target/debug/cider-dap';
const cwd = vscode.workspace.rootPath;

// Create an instance of the CiderDebugAdapter
debugAdapter = new CiderDebugAdapter(adapterPath, cwd, outputChannel);

// Start the debug adapter with the selected program
debugAdapter.start();
debugAdapter = null; //will need to change with multi session
}
}

// Stop debugging
function stopDebugging() {
if (debugAdapter) {
// Stop the running debug adapter
debugAdapter.stop();
} else {
logToPanel('No running debug adapter to stop.');
}
}

// 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({
Expand All @@ -107,20 +84,47 @@ async function getProgramName() {
});

if (fileName) {
// If the fileName is a relative path (not starting with "/"),
// prepend the path to the workspace folder
if (!fileName.startsWith('/')) {
// Also, use path.join to properly join paths
const path = require('path');
return path.join(vscode.workspace.workspaceFolders[0].uri.fsPath, fileName);
}
// If the fileName is an absolute path, return it as is
return fileName;
} else {
// Return null if the user canceled the input
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;
}


// Stop debugging
function stopDebugging() {
if (debugAdapter) {
debugAdapter.stop();
} else {
logToPanel('No running debug adapter to stop.');
}
}


// Activate the extension
Expand All @@ -132,9 +136,6 @@ function activate(context) {

let disposableStop = vscode.commands.registerCommand('cider.stopDebugging', stopDebugging);
context.subscriptions.push(disposableStop);
/*
// Dispose the provider when the extension is deactivated
context.subscriptions.push(provider); */

logToPanel('Hello, your extension is now activated!');
}
Expand All @@ -144,7 +145,11 @@ function deactivate() {
logToPanel("deactivate");
}


module.exports = {
activate,
deactivate
};



9 changes: 7 additions & 2 deletions cider-dap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,31 @@ fn main() -> Result<(), MyAdapterError> {
let adapter = MyAdapter::new(file);

if opts.is_multi_session {
eprintln!("running multi-session");
let listener = TcpListener::bind(("127.0.0.1", opts.port))?;
eprintln!("bound on port: {} ", opts.port);
let (stream, addr) = listener.accept()?;
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)?;
} else {
eprintln!("running single-session");
let write = BufWriter::new(stdout());
let read = BufReader::new(stdin());
let server = Server::new(read, write);
run_server(server, adapter)?;
}

eprintln!("exited run_Server");
Ok(())
}

fn run_server<R: Read, W: Write>(
_server: Server<R, W>,
_adapter: MyAdapter,
) -> AdapterResult<()> {
todo!()
println!("inside run_server");
Ok(()) //still need to implement this
}

0 comments on commit 94d9148

Please sign in to comment.