ptcov 0.1.0-beta.1

Decoder to compute code coverage from IntelĀ® Processor Trace traces
Documentation
/// Packet Stream Boundary (PSB) packet.
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Psb {}

/// PSB End (PSBEND) packet, terminating a PSB+ block.
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct PsbEnd {}

impl PsbEnd {
    pub(crate) const SIZE: usize = 2;
    pub(crate) const B1: u8 = 0x23;
}

impl Psb {
    pub const SIZE: usize = 16;
    pub(crate) const B0: u8 = 0x02;
    pub(crate) const B1: u8 = 0x82;
    pub(crate) const CONTENT: [u8; Psb::SIZE] = [
        Psb::B0,
        Psb::B1,
        Psb::B0,
        Psb::B1,
        Psb::B0,
        Psb::B1,
        Psb::B0,
        Psb::B1,
        Psb::B0,
        Psb::B1,
        Psb::B0,
        Psb::B1,
        Psb::B0,
        Psb::B1,
        Psb::B0,
        Psb::B1,
    ];
}

/// Returns the index of the first byte __after__ the first `Psb` packet
pub(crate) fn first_psb_position(buffer: &[u8]) -> Option<usize> {
    for (i, window) in buffer.windows(Psb::SIZE).enumerate() {
        if window == Psb::CONTENT {
            return Some(i);
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn first_psb_position_works() {
        let traces = [
            (
                [
                    0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02,
                    0x82, 0x02, 0x82,
                ]
                .as_slice(),
                Some(0),
            ),
            (
                &[
                    0x02, 0x03, 0x28, 0x00, 0x00, 0x99, 0x01, 0xd1, 0xed, 0x4d, 0x32, 0x67, 0xaf,
                    0x7d, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcd, 0x9c, 0x76,
                    0x6b, 0x37, 0x72, 0x5d, 0x00, 0x00, 0x2d, 0xc4, 0x76, 0x4d, 0xda, 0xe6, 0x4b,
                    0x37, 0x0a, 0x4d, 0xf4, 0x55, 0x46, 0x37, 0x06, 0x00, 0x2d, 0x37, 0x5f, 0x2d,
                    0x4f, 0x5f, 0x00, 0x00, 0x2d, 0x62, 0xce, 0x2d, 0x30, 0x61, 0x00, 0x00, 0x2d,
                    0x41, 0x61, 0x4d, 0xa0, 0xab, 0x7b, 0x37, 0xf2, 0x2d, 0xee, 0xab, 0x00, 0x00,
                    0x00, 0x00, 0x4d, 0xc4, 0xf9, 0x42, 0x37, 0x00, 0x00, 0x00, 0x4d, 0x66, 0xe5,
                    0x46, 0x37, 0x00, 0x00, 0x00, 0x4d, 0xdb, 0xa0, 0x48, 0x37, 0x0a, 0x00, 0x00,
                    0x4d, 0x2a, 0xfc, 0x42, 0x37, 0x00, 0x00, 0x00, 0x4d, 0xe5, 0xa0, 0x48, 0x37,
                    0x00, 0x00, 0x00, 0x4d, 0xf0, 0xc3, 0x4c, 0x37, 0x00, 0x00, 0x00, 0x4d, 0xda,
                    0x12, 0x48, 0x37, 0x2d, 0x05, 0xa1, 0x4d, 0xc0, 0xda, 0x7b, 0x37, 0x00, 0x00,
                    0x00, 0x4d, 0x15, 0xa1, 0x48, 0x37, 0x2a, 0x00, 0x00, 0x4d, 0xda, 0xc9, 0x44,
                    0x37, 0x00, 0x00, 0x00, 0x4d, 0x36, 0xa1, 0x48, 0x37, 0x00, 0x00, 0x00, 0x4d,
                    0x4e, 0x61, 0x46, 0x37, 0x2d, 0x07, 0xc5, 0x4d, 0x50, 0x73, 0x52, 0x37, 0x2e,
                    0x00, 0x00, 0x2d, 0xcb, 0x73, 0x00, 0x00, 0x00, 0x00, 0xcd, 0x00, 0x94, 0x38,
                    0x67, 0xaf, 0x7d, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcd,
                    0xdf, 0x73, 0x52, 0x37, 0x72, 0x5d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0xcd, 0xe0, 0x8c, 0x2a, 0x67, 0xaf, 0x7d, 0x00, 0x00, 0x06, 0x01,
                    0x31, 0xfc, 0x8c, 0x04, 0x00, 0xcd, 0xd4, 0x74, 0x52, 0x37, 0x72, 0x5d, 0x00,
                    0x00, 0x4d, 0x10, 0xc5, 0x46, 0x37, 0x2d, 0x97, 0xce, 0x06, 0x2d, 0x76, 0x56,
                    0x04, 0x00, 0x00, 0x00, 0x4d, 0x80, 0xe8, 0x4b, 0x37, 0x2d, 0xad, 0xe8, 0x4d,
                    0xd0, 0x76, 0x6b, 0x37, 0x00, 0x00, 0xcd, 0xb0, 0x4d, 0x32, 0x67, 0xaf, 0x7d,
                    0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x82, 0x02,
                    0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82,
                    0x02, 0x03, 0x28, 0x00, 0x02, 0x23, 0x00, 0x00,
                ],
                Some(296),
            ),
        ];

        for (trace, right) in traces {
            assert_eq!(first_psb_position(trace), right);
        }
    }

    #[test]
    fn first_psb_position_empty_buffer() {
        let trace = &[];
        assert_eq!(first_psb_position(trace), None);
    }
}