Skip to content

Commit

Permalink
add gdb step capabilities
Browse files Browse the repository at this point in the history
Signed-off-by: Doru Blânzeanu <[email protected]>
  • Loading branch information
dblnz committed Dec 17, 2024
1 parent 7514103 commit d1878d2
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/hyperlight_host/src/hypervisor/gdb/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use gdbstub::stub::{BaseStopReason, SingleThreadStopReason};
use gdbstub::target::ext::base::singlethread::{
SingleThreadBase,
SingleThreadResume, SingleThreadResumeOps,
SingleThreadSingleStep, SingleThreadSingleStepOps,
};
use gdbstub::target::ext::base::BaseOps;
use gdbstub::target::ext::breakpoints::{
Expand Down Expand Up @@ -101,6 +102,8 @@ pub struct HyperlightKvmSandboxTarget {

/// vCPU paused state
paused: bool,
/// vCPU stepping state
single_step: bool,

/// Array of addresses for HW breakpoints
hw_breakpoints: Vec<u64>,
Expand All @@ -125,6 +128,7 @@ impl HyperlightKvmSandboxTarget {
entrypoint,

paused: false,
single_step: false,

hw_breakpoints: vec![],

Expand All @@ -146,6 +150,13 @@ impl HyperlightKvmSandboxTarget {

/// Get the reason the vCPU has stopped
pub fn get_stop_reason(&self) -> Result<Option<BaseStopReason<(), u64>>, GdbTargetError> {
if self.single_step {
return Ok(Some(SingleThreadStopReason::SignalWithThread {
tid: (),
signal: Signal::SIGTRAP,
}));
}

let ip = self.get_instruction_pointer()?;

if self.hw_breakpoints.contains(&ip) {
Expand All @@ -159,6 +170,15 @@ impl HyperlightKvmSandboxTarget {
Ok(None)
}

fn set_single_step(&mut self, enable: bool) -> Result<(), GdbTargetError> {
self.single_step = enable;

self.debug
.set_breakpoints(&self.vcpu_fd.lock().unwrap(), &self.hw_breakpoints, enable)?;

Ok(())
}

/// This method provides a way to set a breakpoint at the entrypoint
/// it does not keep this breakpoint set after the vcpu already stopped at the address
pub fn set_entrypoint_bp(&mut self) -> Result<bool, GdbTargetError> {
Expand Down Expand Up @@ -472,6 +492,20 @@ impl HwBreakpoint for HyperlightKvmSandboxTarget {
impl SingleThreadResume for HyperlightKvmSandboxTarget {
fn resume(&mut self, _signal: Option<Signal>) -> Result<(), Self::Error> {
log::debug!("Resume");
self.set_single_step(false)?;
self.resume_vcpu()
}
fn support_single_step(&mut self) -> Option<SingleThreadSingleStepOps<Self>> {
Some(self)
}
}

impl SingleThreadSingleStep for HyperlightKvmSandboxTarget {
fn step(&mut self, signal: Option<Signal>) -> Result<(), Self::Error> {
assert!(signal.is_none());

log::debug!("Step");
self.set_single_step(true)?;
self.resume_vcpu()
}
}

0 comments on commit d1878d2

Please sign in to comment.