Breakpoint

Enum Breakpoint 

Source
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 EINVAL errors 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_open manpage indicates that the only valid values for bp_len are 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: BreakpointAccess

Bitfield containing the types of accesses we want the breakpoint to trigger on.

§addr: u64

The address of the memory location on which the breakpoint should trigger.

§len: u64

The length of the breakpoint being measured.

There are a limited number of valid values for this field. Basically, the options are 1, 2, 4, and 8. Setting this field to anything else will cause counter creation to fail with an error.

§

Code

Code breakpoint. Triggers when the code at the address is executed.

Fields

§addr: u64

The address that the breakpoint is monitoring.

Implementations§

Source§

impl Breakpoint

Source

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.

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> Breakpoint

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Breakpoint

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Event for Breakpoint

Source§

fn update_attrs(self, attr: &mut perf_event_attr)

Update the perf_event_attr struct so that it will record the requested event. Read more
Source§

fn update_attrs_with_data( self, attr: &mut perf_event_attr, ) -> Option<Arc<dyn EventData>>

Update the perf_event_attr struct so that it will record the requested event. Read more
Source§

impl Hash for Breakpoint

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Breakpoint

Source§

fn eq(&self, other: &Breakpoint) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Breakpoint

Source§

impl Eq for Breakpoint

Source§

impl StructuralPartialEq for Breakpoint

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> EventData for T
where T: Send + Sync,