Skip to content

Commit 88b4c83

Browse files
committed
event refactoring
1 parent 2cfffc3 commit 88b4c83

20 files changed

+642
-330
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ libipt_master = ["libipt-sys/libipt_master"]
1919
[dependencies]
2020
libipt-sys = { version = "0.2.1", git = "https://github.com/sum-catnip/libipt-sys.git" }
2121
bitflags = "2.4.1"
22+
derive_more = { version = "2.0.1", features = ["deref"]}
2223
num_enum = "0.7.1"

src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ pub enum PtErrorCode {
8282
BadCpu = pt_error_code_pte_bad_cpu as i32,
8383
}
8484

85+
impl From<PtErrorCode> for PtError {
86+
fn from(value: PtErrorCode) -> Self {
87+
PtError::from_code(-(value as i32))
88+
}
89+
}
90+
8591
#[derive(Debug, Clone, Copy)]
8692
pub struct PtError {
8793
code: PtErrorCode,

src/event/branch.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
1-
use libipt_sys::pt_event__bindgen_ty_1__bindgen_ty_4;
1+
use crate::error::{PtError, PtErrorCode};
2+
use crate::event::Event;
3+
use derive_more::Deref;
4+
use libipt_sys::pt_event_type_ptev_async_branch;
25

36
/// An asynchronous branch, e.g. interrupt
4-
#[derive(Clone, Copy, Debug)]
5-
pub struct AsyncBranch(pub(super) pt_event__bindgen_ty_1__bindgen_ty_4);
7+
#[derive(Clone, Copy, Debug, Deref)]
8+
#[repr(transparent)]
9+
pub struct AsyncBranch {
10+
pub(super) event: Event,
11+
}
12+
613
impl AsyncBranch {
714
/// The branch source address
815
#[must_use]
9-
pub fn from(&self) -> u64 {
10-
self.0.from
16+
pub const fn from(&self) -> u64 {
17+
unsafe { self.event.0.variant.async_branch.from }
1118
}
1219

1320
/// The branch destination address.
1421
/// This field is not valid if @ip_suppressed is set.
1522
#[must_use]
16-
pub fn to(&self) -> u64 {
17-
self.0.to
23+
pub const fn to(&self) -> u64 {
24+
unsafe { self.event.0.variant.async_branch.to }
25+
}
26+
}
27+
28+
impl TryFrom<Event> for AsyncBranch {
29+
type Error = PtError;
30+
31+
fn try_from(event: Event) -> Result<Self, Self::Error> {
32+
if event.0.type_ == pt_event_type_ptev_async_branch {
33+
Ok(Self { event })
34+
} else {
35+
Err(PtErrorCode::Invalid.into())
36+
}
1837
}
1938
}
2039

2140
#[cfg(test)]
2241
mod test {
23-
use super::super::Payload;
24-
use super::*;
42+
use super::super::EventType;
2543
use crate::event::Event;
2644
use libipt_sys::{pt_event, pt_event_type_ptev_async_branch};
2745
use std::mem;
@@ -30,11 +48,12 @@ mod test {
3048
fn test_branch_async_payload() {
3149
let mut evt: pt_event = unsafe { mem::zeroed() };
3250
evt.type_ = pt_event_type_ptev_async_branch;
33-
evt.variant.async_branch = pt_event__bindgen_ty_1__bindgen_ty_4 { from: 1, to: 2 };
51+
evt.variant.async_branch.from = 1;
52+
evt.variant.async_branch.to = 2;
3453

35-
let payload: Payload = Event(evt).into();
54+
let payload: EventType = Event(evt).into();
3655
match payload {
37-
Payload::AsyncBranch(e) => {
56+
EventType::AsyncBranch(e) => {
3857
assert_eq!(e.from(), 1);
3958
assert_eq!(e.to(), 2);
4059
}

src/event/cbr.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
1-
use libipt_sys::pt_event__bindgen_ty_1__bindgen_ty_18;
1+
use crate::error::{PtError, PtErrorCode};
2+
use crate::event::Event;
3+
use derive_more::Deref;
4+
use libipt_sys::pt_event_type_ptev_cbr;
25

36
/// A core:bus ratio event
4-
#[derive(Clone, Copy, Debug)]
5-
pub struct Cbr(pub(super) pt_event__bindgen_ty_1__bindgen_ty_18);
7+
#[derive(Clone, Copy, Debug, Deref)]
8+
#[repr(transparent)]
9+
pub struct Cbr {
10+
pub(super) event: Event,
11+
}
612
impl Cbr {
713
/// The core:bus ratio.
814
#[must_use]
9-
pub fn ratio(self) -> u16 {
10-
self.0.ratio
15+
pub const fn ratio(&self) -> u16 {
16+
unsafe { self.event.0.variant.cbr.ratio }
17+
}
18+
}
19+
20+
impl TryFrom<Event> for Cbr {
21+
type Error = PtError;
22+
23+
fn try_from(event: Event) -> Result<Self, Self::Error> {
24+
if event.0.type_ == pt_event_type_ptev_cbr {
25+
Ok(Self { event })
26+
} else {
27+
Err(PtErrorCode::Invalid.into())
28+
}
1129
}
1230
}
1331

1432
#[cfg(test)]
1533
mod test {
16-
use super::super::Payload;
17-
use super::*;
34+
use super::super::EventType;
1835
use crate::event::Event;
1936
use libipt_sys::{pt_event, pt_event_type_ptev_cbr};
2037
use std::mem;
@@ -23,11 +40,11 @@ mod test {
2340
fn test_cbr_payload() {
2441
let mut evt: pt_event = unsafe { mem::zeroed() };
2542
evt.type_ = pt_event_type_ptev_cbr;
26-
evt.variant.cbr = pt_event__bindgen_ty_1__bindgen_ty_18 { ratio: 18 };
43+
evt.variant.cbr.ratio = 18;
2744

28-
let payload: Payload = Event(evt).into();
45+
let payload: EventType = Event(evt).into();
2946
match payload {
30-
Payload::Cbr(e) => {
47+
EventType::Cbr(e) => {
3148
assert_eq!(e.ratio(), 18);
3249
}
3350
_ => unreachable!("oof"),

src/event/disabled.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,72 @@
1-
use libipt_sys::{pt_event__bindgen_ty_1__bindgen_ty_2, pt_event__bindgen_ty_1__bindgen_ty_3};
1+
use crate::error::{PtError, PtErrorCode};
2+
use crate::event::Event;
3+
use derive_more::Deref;
4+
use libipt_sys::{pt_event_type_ptev_async_disabled, pt_event_type_ptev_disabled};
25

36
/// Tracing has been disabled
4-
#[derive(Clone, Copy, Debug)]
5-
pub struct Disabled(pub(super) pt_event__bindgen_ty_1__bindgen_ty_2);
7+
#[derive(Clone, Copy, Debug, Deref)]
8+
#[repr(transparent)]
9+
pub struct Disabled {
10+
pub(super) event: Event,
11+
}
612
impl Disabled {
713
/// The destination of the first branch inside a
814
/// filtered area.
915
///
1016
/// This field is not valid if \@ip_suppressed is set.
1117
#[must_use]
12-
pub fn ip(&self) -> u64 {
13-
self.0.ip
18+
pub const fn ip(&self) -> u64 {
19+
unsafe { self.event.0.variant.disabled.ip }
20+
}
21+
}
22+
23+
impl TryFrom<Event> for Disabled {
24+
type Error = PtError;
25+
26+
fn try_from(event: Event) -> Result<Self, Self::Error> {
27+
if event.0.type_ == pt_event_type_ptev_disabled {
28+
Ok(Self { event })
29+
} else {
30+
Err(PtErrorCode::Invalid.into())
31+
}
1432
}
1533
}
1634

1735
/// Tracing has been disabled asynchronously
1836
#[derive(Clone, Copy, Debug)]
19-
pub struct AsyncDisabled(pub(super) pt_event__bindgen_ty_1__bindgen_ty_3);
37+
pub struct AsyncDisabled {
38+
pub(super) event: Event,
39+
}
2040
impl AsyncDisabled {
2141
/// The source address of the asynchronous branch that disabled tracing
2242
#[must_use]
23-
pub fn at(&self) -> u64 {
24-
self.0.at
43+
pub const fn at(&self) -> u64 {
44+
unsafe { self.event.0.variant.async_disabled.at }
2545
}
2646

2747
/// The destination of the first branch inside a filtered area.
2848
/// This field is not valid if @ip_suppressed is set.
2949
#[must_use]
30-
pub fn ip(&self) -> u64 {
31-
self.0.ip
50+
pub const fn ip(&self) -> u64 {
51+
unsafe { self.event.0.variant.async_disabled.ip }
52+
}
53+
}
54+
55+
impl TryFrom<Event> for AsyncDisabled {
56+
type Error = PtError;
57+
58+
fn try_from(event: Event) -> Result<Self, Self::Error> {
59+
if event.0.type_ == pt_event_type_ptev_async_disabled {
60+
Ok(Self { event })
61+
} else {
62+
Err(PtErrorCode::Invalid.into())
63+
}
3264
}
3365
}
3466

3567
#[cfg(test)]
3668
mod test {
37-
use super::super::Payload;
38-
use super::*;
69+
use super::super::EventType;
3970
use crate::event::Event;
4071
use libipt_sys::{pt_event, pt_event_type_ptev_async_disabled, pt_event_type_ptev_disabled};
4172
use std::mem;
@@ -44,11 +75,11 @@ mod test {
4475
fn test_disabled_payload() {
4576
let mut evt: pt_event = unsafe { mem::zeroed() };
4677
evt.type_ = pt_event_type_ptev_disabled;
47-
evt.variant.disabled = pt_event__bindgen_ty_1__bindgen_ty_2 { ip: 11 };
78+
evt.variant.disabled.ip = 11;
4879

49-
let payload: Payload = Event(evt).into();
80+
let payload: EventType = Event(evt).into();
5081
match payload {
51-
Payload::Disabled(e) => {
82+
EventType::Disabled(e) => {
5283
assert_eq!(e.ip(), 11);
5384
}
5485
_ => unreachable!("oof"),
@@ -59,11 +90,12 @@ mod test {
5990
fn test_async_disabled_payload() {
6091
let mut evt: pt_event = unsafe { mem::zeroed() };
6192
evt.type_ = pt_event_type_ptev_async_disabled;
62-
evt.variant.async_disabled = pt_event__bindgen_ty_1__bindgen_ty_3 { at: 1, ip: 11 };
93+
evt.variant.async_disabled.at = 1;
94+
evt.variant.async_disabled.ip = 11;
6395

64-
let payload: Payload = Event(evt).into();
96+
let payload: EventType = Event(evt).into();
6597
match payload {
66-
Payload::AsnycDisabled(e) => {
98+
EventType::AsnycDisabled(e) => {
6799
assert_eq!(e.ip(), 11);
68100
assert_eq!(e.at(), 1);
69101
}

src/event/enabled.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
1-
use libipt_sys::pt_event__bindgen_ty_1__bindgen_ty_1;
1+
use crate::error::{PtError, PtErrorCode};
2+
use crate::event::Event;
3+
use derive_more::Deref;
4+
use libipt_sys::pt_event_type_ptev_enabled;
25

36
/// Tracing has been enabled
4-
#[derive(Clone, Copy, Debug)]
5-
pub struct Enabled(pub(super) pt_event__bindgen_ty_1__bindgen_ty_1);
7+
#[derive(Clone, Copy, Debug, Deref)]
8+
#[repr(transparent)]
9+
pub struct Enabled {
10+
pub(super) event: Event,
11+
}
612
impl Enabled {
713
/// The address at which tracing resumes
814
#[must_use]
9-
pub fn ip(&self) -> u64 {
10-
self.0.ip
15+
pub const fn ip(&self) -> u64 {
16+
unsafe { self.event.0.variant.enabled.ip }
1117
}
1218

1319
/// A flag indicating that tracing resumes from the IP
1420
/// at which tracing had been disabled before.
1521
#[must_use]
1622
pub fn resumed(&self) -> bool {
17-
self.0.resumed() > 0
23+
(unsafe { self.event.0.variant.enabled.resumed() }) > 0
24+
}
25+
}
26+
27+
impl TryFrom<Event> for Enabled {
28+
type Error = PtError;
29+
30+
fn try_from(event: Event) -> Result<Self, Self::Error> {
31+
if event.0.type_ == pt_event_type_ptev_enabled {
32+
Ok(Self { event })
33+
} else {
34+
Err(PtErrorCode::Invalid.into())
35+
}
1836
}
1937
}
2038

2139
#[cfg(test)]
2240
mod test {
23-
use super::super::Payload;
24-
use super::*;
41+
use super::super::EventType;
2542
use crate::event::Event;
2643
use libipt_sys::{pt_event, pt_event_type_ptev_enabled};
2744
use std::mem;
@@ -30,16 +47,14 @@ mod test {
3047
fn test_enabled_payload() {
3148
let mut evt: pt_event = unsafe { mem::zeroed() };
3249
evt.type_ = pt_event_type_ptev_enabled;
33-
evt.variant.enabled = pt_event__bindgen_ty_1__bindgen_ty_1 {
34-
ip: 11,
35-
_bitfield_align_1: [],
36-
_bitfield_1: pt_event__bindgen_ty_1__bindgen_ty_1::new_bitfield_1(1),
37-
__bindgen_padding_0: Default::default(),
38-
};
50+
evt.variant.enabled.ip = 11;
51+
unsafe {
52+
evt.variant.enabled.set_resumed(1);
53+
}
3954

40-
let payload: Payload = Event(evt).into();
55+
let payload: EventType = Event(evt).into();
4156
match payload {
42-
Payload::Enabled(e) => {
57+
EventType::Enabled(e) => {
4358
assert_eq!(e.ip(), 11);
4459
assert!(e.resumed())
4560
}

0 commit comments

Comments
 (0)