Skip to content

Commit 46887bb

Browse files
committed
Start improving docs
1 parent bc6d2d7 commit 46887bb

File tree

5 files changed

+37
-40
lines changed

5 files changed

+37
-40
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ _This changelog documents only changes relevant to users, internal changes might
1616
- Decoders/Encoder `::new()` have been replaced by `EncoderDecoderBuilder.build()`
1717
- Decoders/Encoder `.get_config()` have been replaced by `.used_builder()`
1818
- Block/Insn decoders `.image()` now returns `&mut Image` instead of `Result<Image,...>`
19+
- Block `raw()` now returns an `Option::<&[u8]>`
1920
- `Image.copy()` has been replaced by `Image.extend()`
2021
- `Image.add_cached()` now takes a `Rc<SectionCache>` instead of `&mut SectionCache` to ensure that the cache outlives the `Image`.
2122
- Many packet/event types methods now take a `&self` instead of consuming `self`
2223
- Some simple methods are now `const`
24+
- ~~`Class:Error`~~ -> `Class::Unknown`
2325

2426
### Removed
2527

src/block/mod.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use decoder::*;
1212
/// A block of instructions.
1313
///
1414
/// Instructions in this block are executed sequentially but are not necessarily
15-
/// contiguous in memory. Users are expected to follow direct branches.
15+
/// contiguous in memory. Users are expected to follow direct branches.
1616
#[derive(Debug, Clone, Copy)]
1717
#[repr(transparent)]
1818
pub struct Block(pub(super) pt_block);
@@ -31,6 +31,7 @@ impl Block {
3131
self.0.end_ip
3232
}
3333

34+
// TODO: make this more rusty? at least with an Option? &Image?
3435
/// The image section that contains the instructions in this block.
3536
///
3637
/// A value of zero means that the section did not have an identifier.
@@ -50,7 +51,7 @@ impl Block {
5051

5152
/// The instruction class for the last instruction in this block.
5253
///
53-
/// This field may be set to `Class::Error` to indicate that the instruction
54+
/// This field may be set to `Class::Unknown` to indicate that the instruction
5455
/// class is not available. The block decoder may choose to not provide
5556
/// the instruction class in some cases for performance reasons.
5657
#[must_use]
@@ -68,31 +69,29 @@ impl Block {
6869
/// The raw bytes of the last instruction in this block in case the
6970
/// instruction does not fit entirely into this block's section.
7071
///
71-
/// This field is only valid if truncated is set.
72+
/// This field is `Some`only if `truncated()` is true.
7273
#[must_use]
73-
pub fn raw(&self) -> &[u8] {
74-
&self.0.raw[..self.0.size as usize]
74+
pub fn raw(&self) -> Option<&[u8]> {
75+
if self.truncated() {
76+
Some(&self.0.raw[..self.0.size as usize])
77+
} else {
78+
None
79+
}
7580
}
7681

77-
/// A collection of flags giving additional information about the
78-
/// instructions in this block.
79-
///
80-
/// - all instructions in this block were executed speculatively.
82+
/// All instructions in this block were executed speculatively.
8183
#[must_use]
8284
pub fn speculative(&self) -> bool {
8385
self.0.speculative() > 0
8486
}
8587

86-
/// A collection of flags giving additional information about the
87-
/// instructions in this block.
88-
///
89-
/// - the last instruction in this block is truncated.
88+
/// The last instruction in this block is truncated.
9089
///
9190
/// It starts in this block's section but continues in one or more
9291
/// other sections depending on how fragmented the memory image is.
9392
///
94-
/// The raw bytes for the last instruction are provided in \@raw and
95-
/// its size in \@size in this case.
93+
/// The raw bytes for the last instruction are provided in @raw and
94+
/// its size in @size in this case.
9695
#[must_use]
9796
pub fn truncated(&self) -> bool {
9897
self.0.truncated() > 0
@@ -125,9 +124,9 @@ mod test {
125124
assert_eq!(blk.end_ip(), 2);
126125
assert_eq!(blk.isid(), 3);
127126
assert_eq!(blk.mode(), ExecModeType::Bit32);
128-
assert_eq!(blk.class(), Class::Error);
127+
assert_eq!(blk.class(), Class::Unknown);
129128
assert_eq!(blk.ninsn(), 4);
130-
assert_eq!(blk.raw(), &data[..8]);
129+
assert_eq!(blk.raw(), Some(&data[..8]));
131130
assert!(blk.truncated());
132131
assert!(!blk.speculative());
133132
}
@@ -153,9 +152,9 @@ mod test {
153152
assert_eq!(blk.end_ip(), 2);
154153
assert_eq!(blk.isid(), 3);
155154
assert_eq!(blk.mode(), ExecModeType::Bit32);
156-
assert_eq!(blk.class(), Class::Error);
155+
assert_eq!(blk.class(), Class::Unknown);
157156
assert_eq!(blk.ninsn(), 4);
158-
assert!(blk.raw().len() > 0);
157+
assert!(blk.raw().is_none());
159158
assert!(!blk.truncated());
160159
assert!(!blk.speculative());
161160
}

src/insn/class.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#![allow(clippy::unnecessary_cast)]
33

44
use libipt_sys::{
5-
pt_insn_class_ptic_call, pt_insn_class_ptic_cond_jump, pt_insn_class_ptic_error,
6-
pt_insn_class_ptic_far_call, pt_insn_class_ptic_far_jump, pt_insn_class_ptic_far_return,
7-
pt_insn_class_ptic_jump, pt_insn_class_ptic_other, pt_insn_class_ptic_ptwrite,
8-
pt_insn_class_ptic_return,
5+
pt_insn_class_ptic_call, pt_insn_class_ptic_cond_jump, pt_insn_class_ptic_far_call,
6+
pt_insn_class_ptic_far_jump, pt_insn_class_ptic_far_return, pt_insn_class_ptic_jump,
7+
pt_insn_class_ptic_other, pt_insn_class_ptic_ptwrite, pt_insn_class_ptic_return,
8+
pt_insn_class_ptic_unknown,
99
};
1010
use num_enum::TryFromPrimitive;
1111

@@ -21,7 +21,7 @@ pub enum Class {
2121
/// The instruction is a near conditional jump.
2222
CondJump = pt_insn_class_ptic_cond_jump as u32,
2323
/// The instruction could not be classified.
24-
Error = pt_insn_class_ptic_error as u32,
24+
Unknown = pt_insn_class_ptic_unknown as u32,
2525
/// The instruction is a call-like far transfer.
2626
/// E.g. SYSCALL, SYSENTER, or FAR CALL.
2727
FarCall = pt_insn_class_ptic_far_call as u32,

src/lib.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
11
#![warn(clippy::all, clippy::cargo)]
22

3-
/// The pt_config structure defines an Intel Processor Trace (Intel PT) encoder or decoder configuration.
4-
///
5-
/// It is required for allocating a trace packet encoder (see pt_alloc_encoder(3)),
6-
/// a trace packet decoder (see pt_pkt_alloc_decoder(3)),
7-
/// a query decoder (see pt_qry_alloc_decoder(3)),
8-
/// or an instruction flow decoder (see pt_insn_alloc_decoder(3)).
3+
/// Intel Processor Trace (Intel PT) encoder or decoder configuration.
94
pub mod enc_dec_builder;
105

11-
/// The library uses a single error enum for all layers.
6+
/// Unique error enum used across the library.
127
///
13-
/// Not all errors may occur on every layer.
14-
/// Every API function specifies the errors it may return. (not accurate!)
8+
/// Not all errors may occur in every module's function.
9+
/// Every API function specifies the errors it may return (sometimes not exhaustively!).
1510
pub mod error;
1611

17-
/// This layer deals with Intel PT packet encoding and decoding.
18-
///
19-
/// It can further be split into three sub-layers: opcodes, encoding, and decoding.
12+
/// Intel PT packet encoding and decoding.
2013
pub mod packet;
2114

22-
/// The event layer deals with packet combinations that encode higher-level events.
15+
/// Higher-level events often resulting from the combination of different PT packets.
2316
///
2417
/// It is used for reconstructing execution flow for users who need finer-grain control not available via the instruction flow layer
2518
/// or for users who want to integrate execution flow reconstruction with other functionality more tightly than it would be possible otherwise.
2619
pub mod event;
2720

28-
/// The block layer provides a simple API for iterating over blocks of sequential instructions in execution order.
21+
/// Simple API for iterating over blocks of sequential instructions in execution order.
2922
///
3023
/// The instructions in a block are sequential in the sense that no trace is required for reconstructing the instructions.
3124
/// The IP of the first instruction is given in struct `Block` and the IP of other instructions in the block can be determined by decoding and examining the previous instruction.
3225
pub mod block;
3326

34-
/// The instruction flow layer provides a simple API for iterating over instructions in execution order.
27+
/// Simple API for iterating over instructions in execution order.
3528
pub mod insn;
3629

3730
mod version;
3831
pub use version::Version;
3932

33+
/// Module handling the memory images of the binaries beeing traced, used in high level decoding.
4034
pub mod image;
4135

36+
/// Address Space Identifier
4237
pub mod asid;
4338

39+
/// Info about the decoder status
4440
pub mod status;

src/version.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use libipt_sys::{pt_library_version, pt_version};
22
use std::ffi::CStr;
33

4-
/// The library version.
4+
/// The underlying C library (libipt) version.
55
#[derive(Clone, Copy, Debug)]
66
#[repr(transparent)]
77
pub struct Version(pt_version);

0 commit comments

Comments
 (0)