Skip to content

Commit

Permalink
feature: watchpoints (part for #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
godzie44 committed May 11, 2024
1 parent 6463d96 commit e2fb4c0
Show file tree
Hide file tree
Showing 40 changed files with 2,771 additions and 284 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ sysinfo = "0.30.0"
indexmap = "2.1.0"
chumsky = { version = "1.0.0-alpha.6", features = ["default", "label"] }
ariadne = "0.4.0"
bit_field = "0.10.1"

[dev-dependencies]
serial_test = "3.0.0"
Expand Down
4 changes: 4 additions & 0 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 16 additions & 15 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
[workspace]
members = [
"pastebin",
"todos",
"tokiotiker",
"calc",
"fizzbuzz",
"hello_world",
"vars",
"mt",
"recursion",
"sleeper",
"signals",
"shlib/calc_lib",
"shlib/calc_bin",
"shlib/printer_lib",
"panic",
"pastebin",
"todos",
"tokiotiker",
"calc",
"fizzbuzz",
"hello_world",
"vars",
"mt",
"recursion",
"sleeper",
"signals",
"shlib/calc_lib",
"shlib/calc_bin",
"shlib/printer_lib",
"panic",
"calculations"
]
resolver = "2"
13 changes: 9 additions & 4 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Examples

A list of applications for debugger testing.
A list of applications for debugger testing.

### hello_world

Expand Down Expand Up @@ -30,7 +30,7 @@ Used for test related to type polymorphism.
### sleeper

Long live application.
Used to test debugger attaching to external processes.
Used to test debugger attaching to external processes.

### recursion

Expand All @@ -39,7 +39,8 @@ Used for test debugger behavior with recursive code.

### Pastebin

Example application from [Rocket](https://github.com/SergioBenitez/Rocket) web framework.
Example application from [Rocket](https://github.com/SergioBenitez/Rocket) web
framework.

### Todos

Expand All @@ -56,4 +57,8 @@ Tick 5 seconds and exit. Useful for tokio oracle testing.
### Panic

Program that just panics.
Initiated by user or system panic (like divide by zero panic).
Initiated by user or system panic (like divide by zero panic).

### Calculations

Program that calculates some values. Useful for watchpoints testing.
12 changes: 12 additions & 0 deletions examples/calculations/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "calculations"
version = "0.0.0"
edition = "2021"
workspace = "./.."
publish = false

[[bin]]
name = "calculations"
path = "src/calculations.rs"

[dependencies]
100 changes: 100 additions & 0 deletions examples/calculations/src/calculations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use std::sync::Mutex;
use std::thread;
use std::time::Duration;

#[allow(clippy::deref_addrof)]
fn calculation_single_value() {
let mut int8 = 1_i8;
int8 += 1;
int8 /= 3;
println!("{int8}");
*&mut int8 = -5;
int8 += 11;
println!("{int8}");
}

fn calculation_four_value() {
let mut a = 1_u64;
let mut b = 2_u64;
let mut c = 3_u64;
let mut d = 4_u64;

a += 5;
b += 1;
c -= 2;
d -= 1;

println!("{}", a + b + c + d);
}

static mut GLOBAL_1: i64 = 1;

fn calculation_global_value() {
unsafe {
GLOBAL_1 -= 1;
GLOBAL_1 += 3;
GLOBAL_1 /= 2;

println!("{GLOBAL_1}");
}
}

static GLOBAL_2: Mutex<u64> = Mutex::new(1);

fn calculation_global_value_2() {
let mut lock = GLOBAL_2.lock().unwrap();
*lock += 1;
thread::sleep(Duration::from_millis(100));
drop(lock);
let mut lock = GLOBAL_2.lock().unwrap();
*lock += 1;
thread::sleep(Duration::from_millis(100));
drop(lock);
}

fn calculation_global_value_mt() {
let t1 = thread::spawn(calculation_global_value_2);
let t2 = thread::spawn(calculation_global_value_2);

t1.join().unwrap();
t2.join().unwrap();

println!("{}", GLOBAL_2.lock().unwrap());
}

fn calculation_local_value_mt() {
let mut a = 1;
a += 1;
thread::scope(|t| {
t.spawn(|| {
a += 5;
});
});

println!("{a}");
}

#[allow(clippy::useless_vec)]
fn calculation_with_complex_types() {
let mut vector = vec![1, 2, 3, 4];
for v in vector.iter_mut() {
*v += 1;
}

struct S {
b: f64,
}
let mut s = S { b: 1_f64 };
s.b *= 2_f64;

println!("v[2] = {}, s.b = {}", vector[2], s.b)
}

pub fn main() {
calculation_single_value();
calculation_four_value();
calculation_global_value();
calculation_global_value_mt();
calculation_local_value_mt();
calculation_with_complex_types();
}
2 changes: 1 addition & 1 deletion src/debugger/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Display for RelocatedAddress {

/// Represent address in object files.
/// This address unique per object file but not per process.
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Debug, Default)]
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Debug, Default, Ord)]
pub struct GlobalAddress(usize);

impl GlobalAddress {
Expand Down
59 changes: 58 additions & 1 deletion src/debugger/breakpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ pub enum BrkptType {
EntryPoint,
/// User defined breakpoint
UserDefined,
/// Watchpoint defined breakpoint, used to stop the program
/// when the watchpoint expression leaves a scope where it is valid.
/// Contains linked watchpoint numbers.
WatchpointCompanion(Vec<u32>),
/// Auxiliary breakpoints, using, for example, in step-over implementation
Temporary,
/// Breakpoint at linker internal function that will always be called when the linker
Expand All @@ -496,6 +500,7 @@ impl Debug for BrkptType {
BrkptType::Temporary => f.write_str("temporary"),
BrkptType::LinkerMapFn => f.write_str("linker-map"),
BrkptType::Transparent(_) => f.write_str("transparent"),
BrkptType::WatchpointCompanion(_) => f.write_str("watchpoint-defined"),
}
}
}
Expand All @@ -518,6 +523,9 @@ impl PartialEq for BrkptType {
BrkptType::Transparent(_) => {
matches!(other, BrkptType::Transparent(_))
}
BrkptType::WatchpointCompanion(num) => {
matches!(other, BrkptType::WatchpointCompanion(other_num) if num == other_num)
}
}
}
}
Expand Down Expand Up @@ -548,6 +556,7 @@ impl Breakpoint {
impl Breakpoint {
const INT3: u64 = 0xCC_u64;

#[inline(always)]
fn new_inner(
addr: RelocatedAddress,
pid: Pid,
Expand All @@ -568,6 +577,7 @@ impl Breakpoint {
}
}

#[inline(always)]
pub fn new(
debug_info_file: impl Into<PathBuf>,
addr: RelocatedAddress,
Expand All @@ -584,6 +594,7 @@ impl Breakpoint {
)
}

#[inline(always)]
pub fn new_entry_point(
debug_info_file: impl Into<PathBuf>,
addr: RelocatedAddress,
Expand All @@ -599,6 +610,7 @@ impl Breakpoint {
)
}

#[inline(always)]
pub fn new_temporary(
debug_info_file: impl Into<PathBuf>,
addr: RelocatedAddress,
Expand All @@ -614,6 +626,7 @@ impl Breakpoint {
)
}

#[inline(always)]
pub fn new_linker_map(addr: RelocatedAddress, pid: Pid) -> Self {
Self::new_inner(
addr,
Expand All @@ -625,6 +638,7 @@ impl Breakpoint {
)
}

#[inline(always)]
pub fn new_transparent(
debug_info_file: impl Into<PathBuf>,
addr: RelocatedAddress,
Expand All @@ -641,6 +655,40 @@ impl Breakpoint {
)
}

#[inline(always)]
pub(super) fn new_watchpoint_companion(
registry: &BreakpointRegistry,
wp_num: u32,
addr: RelocatedAddress,
pid: Pid,
) -> Self {
let (wp_nums, brkpt_num) = if let Some(Breakpoint {
r#type: BrkptType::WatchpointCompanion(ref nums),
number,
..
}) = registry.get_enabled(addr)
{
// reuse if a companion already exists
let mut nums = nums.clone();
nums.push(wp_num);
(nums, *number)
} else {
(
vec![wp_num],
GLOBAL_BP_COUNTER.fetch_add(1, Ordering::Relaxed),
)
};

Self::new_inner(
addr,
pid,
brkpt_num,
None,
BrkptType::WatchpointCompanion(wp_nums),
PathBuf::default(),
)
}

#[inline(always)]
pub fn number(&self) -> u32 {
self.number
Expand All @@ -649,6 +697,7 @@ impl Breakpoint {
/// Return breakpoint place information.
///
/// # Panics
///
/// Panic if a breakpoint is not a user defined.
/// It is the caller's responsibility to check that the type is [`BrkptType::UserDefined`].
pub fn place(&self) -> Option<&PlaceDescriptorOwned> {
Expand All @@ -657,6 +706,7 @@ impl Breakpoint {
BrkptType::EntryPoint
| BrkptType::Temporary
| BrkptType::LinkerMapFn
| BrkptType::WatchpointCompanion(_)
| BrkptType::Transparent(_) => {
panic!("only user defined breakpoint has a place attribute")
}
Expand All @@ -667,6 +717,10 @@ impl Breakpoint {
self.r#type == BrkptType::EntryPoint
}

pub fn is_wp_companion(&self) -> bool {
matches!(self.r#type, BrkptType::WatchpointCompanion(_))
}

#[inline(always)]
pub fn r#type(&self) -> &BrkptType {
&self.r#type
Expand Down Expand Up @@ -1045,7 +1099,10 @@ impl BreakpointRegistry {
brkpt.place,
));
}
BrkptType::Temporary | BrkptType::LinkerMapFn | BrkptType::Transparent(_) => {}
BrkptType::Temporary
| BrkptType::LinkerMapFn
| BrkptType::Transparent(_)
| BrkptType::WatchpointCompanion(_) => {}
}
}
Ok(errors)
Expand Down
Loading

0 comments on commit e2fb4c0

Please sign in to comment.