Skip to content

Commit 51a59fa

Browse files
committed
Better events Debug
1 parent 88b4c83 commit 51a59fa

20 files changed

+283
-40
lines changed

src/block/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ pub struct Block(pub(super) pt_block);
1919
impl Block {
2020
/// The IP of the first instruction in this block.
2121
#[must_use]
22-
pub fn ip(&self) -> u64 {
22+
pub const fn ip(&self) -> u64 {
2323
self.0.ip
2424
}
2525

2626
/// The IP of the last instruction in this block.
2727
///
2828
/// This can be used for error-detection.
2929
#[must_use]
30-
pub fn end_ip(&self) -> u64 {
30+
pub const fn end_ip(&self) -> u64 {
3131
self.0.end_ip
3232
}
3333

@@ -38,7 +38,7 @@ impl Block {
3838
/// The section was not added via an image section cache or the memory
3939
/// was read via the read memory callback.
4040
#[must_use]
41-
pub fn isid(&self) -> i32 {
41+
pub const fn isid(&self) -> i32 {
4242
self.0.isid
4343
}
4444

@@ -62,7 +62,7 @@ impl Block {
6262

6363
/// The number of instructions in this block.
6464
#[must_use]
65-
pub fn ninsn(&self) -> u16 {
65+
pub const fn ninsn(&self) -> u16 {
6666
self.0.ninsn
6767
}
6868

src/event/branch.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use crate::error::{PtError, PtErrorCode};
22
use crate::event::Event;
33
use derive_more::Deref;
44
use libipt_sys::pt_event_type_ptev_async_branch;
5+
use std::fmt::{Debug, Formatter};
56

67
/// An asynchronous branch, e.g. interrupt
7-
#[derive(Clone, Copy, Debug, Deref)]
8+
#[derive(Clone, Copy, Deref)]
89
#[repr(transparent)]
910
pub struct AsyncBranch {
1011
pub(super) event: Event,
@@ -25,6 +26,14 @@ impl AsyncBranch {
2526
}
2627
}
2728

29+
impl Debug for AsyncBranch {
30+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
31+
write!(f, "AsyncBranch {{")?;
32+
self.fmt_common_fields(f)?;
33+
write!(f, "from: 0x{:x?}, to: 0x{:x?}, }}", self.from(), self.to())
34+
}
35+
}
36+
2837
impl TryFrom<Event> for AsyncBranch {
2938
type Error = PtError;
3039

src/event/cbr.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use crate::error::{PtError, PtErrorCode};
22
use crate::event::Event;
33
use derive_more::Deref;
44
use libipt_sys::pt_event_type_ptev_cbr;
5+
use std::fmt::{Debug, Formatter};
56

67
/// A core:bus ratio event
7-
#[derive(Clone, Copy, Debug, Deref)]
8+
#[derive(Clone, Copy, Deref)]
89
#[repr(transparent)]
910
pub struct Cbr {
1011
pub(super) event: Event,
@@ -17,6 +18,14 @@ impl Cbr {
1718
}
1819
}
1920

21+
impl Debug for Cbr {
22+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23+
write!(f, "Cbr {{")?;
24+
self.fmt_common_fields(f)?;
25+
write!(f, "ratio: {:?}, }}", self.ratio())
26+
}
27+
}
28+
2029
impl TryFrom<Event> for Cbr {
2130
type Error = PtError;
2231

src/event/disabled.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use crate::error::{PtError, PtErrorCode};
22
use crate::event::Event;
33
use derive_more::Deref;
44
use libipt_sys::{pt_event_type_ptev_async_disabled, pt_event_type_ptev_disabled};
5+
use std::fmt::{Debug, Formatter};
56

67
/// Tracing has been disabled
7-
#[derive(Clone, Copy, Debug, Deref)]
8+
#[derive(Clone, Copy, Deref)]
89
#[repr(transparent)]
910
pub struct Disabled {
1011
pub(super) event: Event,
@@ -20,6 +21,14 @@ impl Disabled {
2021
}
2122
}
2223

24+
impl Debug for Disabled {
25+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
26+
write!(f, "Disabled {{")?;
27+
self.fmt_common_fields(f)?;
28+
write!(f, "ip: 0x{:x?}, }}", self.ip())
29+
}
30+
}
31+
2332
impl TryFrom<Event> for Disabled {
2433
type Error = PtError;
2534

@@ -33,10 +42,11 @@ impl TryFrom<Event> for Disabled {
3342
}
3443

3544
/// Tracing has been disabled asynchronously
36-
#[derive(Clone, Copy, Debug)]
45+
#[derive(Clone, Copy, Deref)]
3746
pub struct AsyncDisabled {
3847
pub(super) event: Event,
3948
}
49+
4050
impl AsyncDisabled {
4151
/// The source address of the asynchronous branch that disabled tracing
4252
#[must_use]
@@ -52,6 +62,14 @@ impl AsyncDisabled {
5262
}
5363
}
5464

65+
impl Debug for AsyncDisabled {
66+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
67+
write!(f, "AsyncDisabled {{")?;
68+
self.fmt_common_fields(f)?;
69+
write!(f, "at: {:?}, ip: {:?}, }}", self.at(), self.ip())
70+
}
71+
}
72+
5573
impl TryFrom<Event> for AsyncDisabled {
5674
type Error = PtError;
5775

src/event/enabled.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use crate::error::{PtError, PtErrorCode};
22
use crate::event::Event;
33
use derive_more::Deref;
44
use libipt_sys::pt_event_type_ptev_enabled;
5+
use std::fmt::{Debug, Formatter};
56

67
/// Tracing has been enabled
7-
#[derive(Clone, Copy, Debug, Deref)]
8+
#[derive(Clone, Copy, Deref)]
89
#[repr(transparent)]
910
pub struct Enabled {
1011
pub(super) event: Event,
@@ -24,6 +25,19 @@ impl Enabled {
2425
}
2526
}
2627

28+
impl Debug for Enabled {
29+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30+
write!(f, "Enabled {{")?;
31+
self.fmt_common_fields(f)?;
32+
write!(
33+
f,
34+
"ip: 0x{:x?}, resumed: {:?} }}",
35+
self.ip(),
36+
self.resumed()
37+
)
38+
}
39+
}
40+
2741
impl TryFrom<Event> for Enabled {
2842
type Error = PtError;
2943

src/event/exec_mode.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use libipt_sys::{
1010
};
1111
use num_enum::TryFromPrimitive;
1212
use std::convert::TryFrom;
13+
use std::fmt::{Debug, Formatter};
1314

1415
#[derive(Clone, Copy, TryFromPrimitive, Debug, PartialEq)]
1516
#[repr(u32)]
@@ -21,7 +22,7 @@ pub enum ExecModeType {
2122
}
2223

2324
/// An execution mode change
24-
#[derive(Clone, Copy, Debug, Deref)]
25+
#[derive(Clone, Copy, Deref)]
2526
#[repr(transparent)]
2627
pub struct ExecMode {
2728
pub(super) event: Event,
@@ -35,12 +36,19 @@ impl ExecMode {
3536

3637
/// The execution mode
3738
#[must_use]
38-
#[expect(clippy::missing_panics_doc)]
3939
pub fn mode(&self) -> ExecModeType {
4040
ExecModeType::try_from(unsafe { self.event.0.variant.exec_mode.mode } as u32).unwrap()
4141
}
4242
}
4343

44+
impl Debug for ExecMode {
45+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46+
write!(f, "ExecMode {{")?;
47+
self.fmt_common_fields(f)?;
48+
write!(f, "ip: 0x{:x?}, mode: {:?} }}", self.ip(), self.mode())
49+
}
50+
}
51+
4452
impl TryFrom<Event> for ExecMode {
4553
type Error = PtError;
4654

src/event/exstop.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use crate::error::{PtError, PtErrorCode};
22
use crate::event::Event;
33
use derive_more::Deref;
44
use libipt_sys::pt_event_type_ptev_exstop;
5+
use std::fmt::{Debug, Formatter};
56

67
/// Execution has stopped
7-
#[derive(Clone, Copy, Debug, Deref)]
8+
#[derive(Clone, Copy, Deref)]
89
#[repr(transparent)]
910
pub struct Exstop {
1011
pub(super) event: Event,
@@ -19,6 +20,14 @@ impl Exstop {
1920
}
2021
}
2122

23+
impl Debug for Exstop {
24+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25+
write!(f, "Exstop {{")?;
26+
self.fmt_common_fields(f)?;
27+
write!(f, "ip: 0x{:x?}, }}", self.ip())
28+
}
29+
}
30+
2231
impl TryFrom<Event> for Exstop {
2332
type Error = PtError;
2433

src/event/mnt.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use crate::error::{PtError, PtErrorCode};
22
use crate::event::Event;
33
use derive_more::Deref;
44
use libipt_sys::pt_event_type_ptev_mnt;
5+
use std::fmt::{Debug, Formatter};
56

67
/// A maintenance event.
7-
#[derive(Clone, Copy, Debug, Deref)]
8+
#[derive(Clone, Copy, Deref)]
89
#[repr(transparent)]
910
pub struct Mnt {
1011
pub(super) event: Event,
@@ -17,6 +18,14 @@ impl Mnt {
1718
}
1819
}
1920

21+
impl Debug for Mnt {
22+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23+
write!(f, "Mnt {{")?;
24+
self.fmt_common_fields(f)?;
25+
write!(f, "payload: 0x{:x?}, }}", self.payload())
26+
}
27+
}
28+
2029
impl TryFrom<Event> for Mnt {
2130
type Error = PtError;
2231

src/event/mod.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use libipt_sys::{
77
pt_event_type_ptev_pwre, pt_event_type_ptev_pwrx, pt_event_type_ptev_stop,
88
pt_event_type_ptev_tick, pt_event_type_ptev_tsx, pt_event_type_ptev_vmcs,
99
};
10+
use std::fmt::{Debug, Formatter};
1011

1112
mod enabled;
1213
pub use enabled::*;
@@ -76,9 +77,10 @@ impl From<Event> for EventType {
7677
}
7778
}
7879

79-
#[derive(Debug, Clone, Copy)]
80+
#[derive(Clone, Copy)]
8081
#[repr(transparent)]
8182
pub struct Event(pub(crate) pt_event);
83+
8284
impl Event {
8385
/// A flag indicating that the event IP has been suppressed.
8486
#[must_use]
@@ -92,17 +94,16 @@ impl Event {
9294
self.0.status_update() > 0
9395
}
9496

95-
/// A flag indicating that the event has timing information.
96-
#[must_use]
97-
pub fn has_tsc(&self) -> bool {
98-
self.0.has_tsc() > 0
99-
}
100-
10197
/// The time stamp count of the event.
102-
/// This field is only valid if @has_tsc is set.
98+
///
99+
/// Returns `None` if tsc information is not available
103100
#[must_use]
104-
pub const fn tsc(&self) -> u64 {
105-
self.0.tsc
101+
pub fn tsc(&self) -> Option<u64> {
102+
if self.0.has_tsc() > 0 {
103+
Some(self.0.tsc)
104+
} else {
105+
None
106+
}
106107
}
107108

108109
/// The number of lost mtc packets.
@@ -153,6 +154,24 @@ impl Event {
153154
_ => unreachable!(),
154155
}
155156
}
157+
158+
pub(crate) fn fmt_common_fields(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
159+
write!(
160+
f,
161+
"ip_suppressed : {:?}, status_update: {:?}, tsc: {:?}, lost_mtc: {:?}, lost_cyc: {:?},",
162+
self.ip_suppressed(),
163+
self.status_update(),
164+
self.tsc(),
165+
self.lost_mtc(),
166+
self.lost_cyc(),
167+
)
168+
}
169+
}
170+
171+
impl Debug for Event {
172+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
173+
self.event_type().fmt(f)
174+
}
156175
}
157176

158177
#[cfg(test)]
@@ -177,9 +196,8 @@ mod test {
177196
let evt = Event(evt);
178197
assert!(evt.ip_suppressed());
179198
assert!(!evt.status_update());
180-
assert!(evt.has_tsc());
181199

182-
assert_eq!(evt.tsc(), 1);
200+
assert_eq!(evt.tsc(), Some(1));
183201
assert_eq!(evt.lost_mtc(), 2);
184202
assert_eq!(evt.lost_cyc(), 3);
185203

src/event/mwait.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use crate::error::{PtError, PtErrorCode};
22
use crate::event::Event;
33
use derive_more::Deref;
44
use libipt_sys::pt_event_type_ptev_mwait;
5+
use std::fmt::{Debug, Formatter};
56

67
/// An MWAIT operation completed
7-
#[derive(Clone, Copy, Debug, Deref)]
8+
#[derive(Clone, Copy, Deref)]
89
#[repr(transparent)]
910
pub struct Mwait {
1011
pub(super) event: Event,
@@ -35,6 +36,20 @@ impl Mwait {
3536
}
3637
}
3738

39+
impl Debug for Mwait {
40+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41+
write!(f, "Mwait {{")?;
42+
self.fmt_common_fields(f)?;
43+
write!(
44+
f,
45+
"ip: 0x{:x?}, hints: 0x{:x?}, ext: 0x{:x?}, }}",
46+
self.ip(),
47+
self.hints(),
48+
self.ext()
49+
)
50+
}
51+
}
52+
3853
impl TryFrom<Event> for Mwait {
3954
type Error = PtError;
4055

0 commit comments

Comments
 (0)