Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Update existing breakpoint instead of overwriting #874

Merged
merged 3 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/agent/debugger/src/breakpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ impl ResolvedBreakpoint {
self.kind
}

pub fn update(&mut self, id: BreakpointId, kind: BreakpointType) {
self.id = id;
self.kind = kind;
}

#[allow(unused)]
pub fn is_enabled(&self) -> bool {
!self.is_disabled()
Expand All @@ -119,10 +124,8 @@ impl ResolvedBreakpoint {
pub(crate) fn disable(&mut self, process_handle: HANDLE) -> Result<()> {
self.disabled = self.disabled.saturating_add(1);

if self.is_disabled() {
if let Some(original_byte) = self.original_byte.take() {
write_instruction_byte(process_handle, self.address, original_byte)?;
}
if let Some(original_byte) = self.original_byte.take() {
write_instruction_byte(process_handle, self.address, original_byte)?;
}

Ok(())
Expand All @@ -131,9 +134,10 @@ impl ResolvedBreakpoint {
pub fn enable(&mut self, process_handle: HANDLE) -> Result<()> {
self.disabled = self.disabled.saturating_sub(1);

let new_original_byte = process::read_memory(process_handle, self.address as _)?;
self.original_byte = Some(new_original_byte);
write_instruction_byte(process_handle, self.address, 0xcc)?;
if self.original_byte.is_none() {
self.original_byte = Some(process::read_memory(process_handle, self.address as _)?);
write_instruction_byte(process_handle, self.address, 0xcc)?;
}

Ok(())
}
Expand Down
18 changes: 15 additions & 3 deletions src/agent/debugger/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,21 @@ impl Module {
address: u64,
process_handle: HANDLE,
) -> Result<()> {
let mut breakpoint = ResolvedBreakpoint::new(id, kind, address);
breakpoint.enable(process_handle)?;
self.breakpoints.insert(address, breakpoint);
if let Some(breakpoint) = self.breakpoints.get_mut(address) {
// Update the existing breakpoint with a (probably new) id and (probably not new) kind.
breakpoint.update(id, kind);
if breakpoint.is_disabled() {
// Existing breakpoint was disabled, but the caller wants a new
// breakpoint that by luck or otherwise, was at the same address
// as an existing breakpoint, so we go ahead and enable.
breakpoint.enable(process_handle)?;
}
} else {
let mut breakpoint = ResolvedBreakpoint::new(id, kind, address);
breakpoint.enable(process_handle)?;
self.breakpoints.insert(address, breakpoint);
}

Ok(())
}

Expand Down