Skip to content

Commit

Permalink
Cider-dap changes (#1768)
Browse files Browse the repository at this point in the history
* Cleaned up UnhandledCommandError

* Added the SetBreakPoints command to the debugger

* Added SetExceptionBreakpoints

* Cleaned up code and added threads command, debugger now runs

* PR revisions

---------

Co-authored-by: root <root@EliasSP>
  • Loading branch information
eliascxstro and root authored Nov 14, 2023
1 parent c4968ce commit a93c510
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 8 deletions.
83 changes: 81 additions & 2 deletions cider-dap/src/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,91 @@
use dap::types::{Breakpoint, Source, SourceBreakpoint, Thread};
use std::fs::File;
pub struct MyAdapter {
#[allow(dead_code)]
file: File,
// Other fields of the struct
breakpoints: Vec<(Source, i64)>, //This field is a placeholder
break_count: Counter,
threads: Vec<Thread>, //This field is a placeholder
}

impl MyAdapter {
pub fn new(file: File) -> Self {
MyAdapter { file }
MyAdapter {
file,
breakpoints: Vec::new(),
break_count: Counter::new(),
threads: Vec::new(),
}
}

///Set breakpoints for adapter
pub fn set_breakpoint(
&mut self,
path: Source,
source: &Vec<SourceBreakpoint>,
) -> Vec<Breakpoint> {
//Keep all the new breakpoints made
let mut out_vec: Vec<Breakpoint> = vec![];

//Loop over all breakpoints
for source_point in source {
self.breakpoints.push((path.clone(), source_point.line));
//Create new Breakpoint instance
let breakpoint = make_breakpoint(
self.break_count.increment().into(),
true,
Some(path.clone()),
);

out_vec.push(breakpoint);
}

out_vec
}

/// Clone threads
pub fn clone_threads(&self) -> Vec<Thread> {
self.threads.clone()
}
}

/// Simple struct used to keep an index of the breakpoints used.
pub struct Counter {
value: i64,
}

impl Counter {
pub fn new() -> Self {
Counter { value: 0 }
}

/// Increment the counter by 1 and return the OLD value
pub fn increment(&mut self) -> i64 {
let out = self.value;
self.value += 1;
out
}
}

/// Returns a Breakpoint object.
///
/// This function takes in relevant fields in Breakpoint that are used
/// by the adapter. This is subject to change.
pub fn make_breakpoint(
id: Option<i64>,
verified: bool,
source: Option<Source>,
) -> Breakpoint {
Breakpoint {
id,
verified,
message: None,
source,
line: None,
column: None,
end_line: None,
end_column: None,
instruction_reference: None,
offset: None,
}
}
5 changes: 3 additions & 2 deletions cider-dap/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use dap::errors::ServerError;
use dap::requests::Command;

#[allow(dead_code)] // remove this later
#[derive(thiserror::Error, Debug)]
pub enum MyAdapterError {
/// Represents an unhandled command error.
#[error("Unhandled command")]
UnhandledCommandError,
#[error("Unhandled command: {0:?}")]
UnhandledCommandError(Command),

/// Represents an error when unable to parse the file.
#[error("Unable to parse the file: {0}")]
Expand Down
49 changes: 45 additions & 4 deletions cider-dap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ mod adapter;
mod error;

use adapter::MyAdapter;
use dap::responses::{
SetBreakpointsResponse, SetExceptionBreakpointsResponse, ThreadsResponse,
};
use error::MyAdapterError;

use dap::prelude::*;
Expand Down Expand Up @@ -81,7 +84,12 @@ where
server.respond(rsp)?;
server.send_event(Event::Initialized)?;
}
_ => return Err(MyAdapterError::UnhandledCommandError),

unknown_command => {
return Err(MyAdapterError::UnhandledCommandError(
unknown_command.clone(),
));
}
}

// handle the second request (Launch)
Expand Down Expand Up @@ -119,7 +127,7 @@ where

fn run_server<R: Read, W: Write>(
server: &mut Server<R, W>,
_adapter: MyAdapter,
mut adapter: MyAdapter,
) -> AdapterResult<()> {
loop {
// Start looping here
Expand All @@ -132,13 +140,46 @@ fn run_server<R: Read, W: Write>(
let rsp = req.success(ResponseBody::Launch);
server.respond(rsp)?;
}

Command::SetBreakpoints(args) => {
//Add breakpoints
if let Some(breakpoint) = &args.breakpoints {
let out =
adapter.set_breakpoint(args.source.clone(), breakpoint);

//Success
let rsp = req.success(ResponseBody::SetBreakpoints(
SetBreakpointsResponse { breakpoints: (out) },
));
server.respond(rsp)?;
}
}

//TODO: Implement this request fully when adapter becomes functional
Command::SetExceptionBreakpoints(_) => {
let rsp = req.success(ResponseBody::SetExceptionBreakpoints(
SetExceptionBreakpointsResponse {
breakpoints: (None),
},
));
server.respond(rsp)?;
}

//Retrieve a list of all threads
Command::Threads => {
let rsp = req.success(ResponseBody::Threads(ThreadsResponse {
threads: adapter.clone_threads(),
}));
server.respond(rsp)?;
}
// Here, can add a match pattern for a disconnect or exit command
// to break out of the loop and close the server.
// Command::Disconnect(_) => break,
// ...
unknown_command => {
eprintln!("Unknown_command: {:?}", unknown_command);
return Err(MyAdapterError::UnhandledCommandError);
return Err(MyAdapterError::UnhandledCommandError(
unknown_command.clone(),
));
}
}
}
Expand Down

0 comments on commit a93c510

Please sign in to comment.