pub enum Breakpoint {
Data {
access: BreakpointAccess,
addr: u64,
len: u64,
},
Code {
addr: u64,
},
}Expand description
A hardware breakpoint.
A hardware breakpoint watches a region of memory for accesses. It has three parameters:
- the address that is being watched (
addr) - the number of bytes that breakpoint covers (
len) - which type of memory accesses we care about (
ty)
Note that both number of bytes that can be watched as well as the number of breakpoints that is allowed to be active at any given time is limited.
§Execute Breakpoint
We can use a breakpoint to count the number of times that a function gets called, as long as the compiler does not optimize the function away.
#[inline(never)]
fn do_some_things() {
// ...
}
let fnptr = do_some_things as fn() as usize;
let mut counter = Builder::new(Breakpoint::execute(fnptr as u64)).build()?;
counter.enable()?;
for _ in 0..500 {
do_some_things();
}
counter.disable()?;
assert_eq!(counter.read()?, 500);§Data Breakpoint
We can also use a breakpoint to count the number of times that a memory location is accessed.
let mut data: Vec<u64> = (0..1024).rev().collect();
let breakpoint = Breakpoint::read_write(&data[20] as *const _ as usize as u64, 8);
let mut counter = Builder::new(breakpoint).build()?;
counter.enable()?;
data.sort();
counter.disable()?;
println!("Position 20 accessed {} times", counter.read()?);§Usage Notes
-
Some systems do not support creating read-only or write-only breakpoints. If you are getting
EINVALerrors while trying to build such a counter using a read-write breakpoint might work instead. -
The valid values of len are quite limited. The
perf_event_openmanpage indicates that the only valid values forbp_lenare 1, 2, 4, and 8.
Variants§
Data
Data breakpoint. Triggers when code reads or writes to the memory area as configured by the parameters below.
Fields
access: BreakpointAccessBitfield containing the types of accesses we want the breakpoint to trigger on.
Code
Code breakpoint. Triggers when the code at the address is executed.
Implementations§
Source§impl Breakpoint
impl Breakpoint
Sourcepub const fn execute(addr: u64) -> Self
pub const fn execute(addr: u64) -> Self
Create a code execution breakpoint, that counts the number of times the instruction at the provided address was executed.
Sourcepub const fn read(addr: u64, len: u64) -> Self
pub const fn read(addr: u64, len: u64) -> Self
Create a memory read breakpoint, that counts the number of times we read from the provided memory location.
See the struct field docs for valid values of len.
Sourcepub const fn write(addr: u64, len: u64) -> Self
pub const fn write(addr: u64, len: u64) -> Self
Create a memory write breakpoint, that counts the number of times we write to the provided memory location.
See the struct field docs for valid values of len.
Sourcepub const fn read_write(addr: u64, len: u64) -> Self
pub const fn read_write(addr: u64, len: u64) -> Self
Create a memory access breakpoint, that counts the number of times we either read from or write to the provided memory location.
See the struct field docs for valid values of len.
Trait Implementations§
Source§impl Clone for Breakpoint
impl Clone for Breakpoint
Source§fn clone(&self) -> Breakpoint
fn clone(&self) -> Breakpoint
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Breakpoint
impl Debug for Breakpoint
Source§impl Event for Breakpoint
impl Event for Breakpoint
Source§fn update_attrs(self, attr: &mut perf_event_attr)
fn update_attrs(self, attr: &mut perf_event_attr)
perf_event_attr struct so that it will record the
requested event. Read moreSource§fn update_attrs_with_data(
self,
attr: &mut perf_event_attr,
) -> Option<Arc<dyn EventData>>
fn update_attrs_with_data( self, attr: &mut perf_event_attr, ) -> Option<Arc<dyn EventData>>
perf_event_attr struct so that it will record the
requested event. Read more