Skip to main content

iptr_decoder/packet_handler/
packet_counter.rs

1//! Handler for counting total packets
2
3use core::num::NonZero;
4
5use crate::{DecoderContext, HandlePacket, IpReconstructionPattern};
6
7/// A [`HandlePacket`] instance for counting Intel PT packets
8#[derive(Default)]
9pub struct PacketCounter {
10    packet_count: usize,
11}
12
13impl PacketCounter {
14    /// Create a new [`PacketCounter`]
15    #[must_use]
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    /// Get the total packet count
21    #[must_use]
22    pub fn packet_count(&self) -> usize {
23        self.packet_count
24    }
25}
26
27impl HandlePacket for PacketCounter {
28    // Will never fail
29    type Error = core::convert::Infallible;
30
31    fn at_decode_begin(&mut self) -> Result<(), Self::Error> {
32        self.packet_count = 0;
33        Ok(())
34    }
35
36    fn on_short_tnt_packet(
37        &mut self,
38        _context: &DecoderContext,
39        _packet_byte: NonZero<u8>,
40        _highest_bit: u32,
41    ) -> Result<(), Self::Error> {
42        self.packet_count += 1;
43
44        Ok(())
45    }
46
47    fn on_long_tnt_packet(
48        &mut self,
49        _context: &DecoderContext,
50        _packet_bytes: NonZero<u64>,
51        _highest_bit: u32,
52    ) -> Result<(), Self::Error> {
53        self.packet_count += 1;
54
55        Ok(())
56    }
57
58    fn on_tip_packet(
59        &mut self,
60        _context: &DecoderContext,
61        _ip_reconstruction_pattern: IpReconstructionPattern,
62    ) -> Result<(), Self::Error> {
63        self.packet_count += 1;
64
65        Ok(())
66    }
67
68    fn on_tip_pgd_packet(
69        &mut self,
70        _context: &DecoderContext,
71        _ip_reconstruction_pattern: IpReconstructionPattern,
72    ) -> Result<(), Self::Error> {
73        self.packet_count += 1;
74
75        Ok(())
76    }
77
78    fn on_tip_pge_packet(
79        &mut self,
80        _context: &DecoderContext,
81        _ip_reconstruction_pattern: IpReconstructionPattern,
82    ) -> Result<(), Self::Error> {
83        self.packet_count += 1;
84
85        Ok(())
86    }
87
88    fn on_fup_packet(
89        &mut self,
90        _context: &DecoderContext,
91        _ip_reconstruction_pattern: IpReconstructionPattern,
92    ) -> Result<(), Self::Error> {
93        self.packet_count += 1;
94
95        Ok(())
96    }
97
98    fn on_pad_packet(&mut self, _context: &DecoderContext) -> Result<(), Self::Error> {
99        self.packet_count += 1;
100
101        Ok(())
102    }
103
104    fn on_cyc_packet(
105        &mut self,
106        _context: &DecoderContext,
107        _cyc_packet: &[u8],
108    ) -> Result<(), Self::Error> {
109        self.packet_count += 1;
110
111        Ok(())
112    }
113
114    fn on_mode_packet(
115        &mut self,
116        _context: &DecoderContext,
117        _leaf_id: u8,
118        _mode: u8,
119    ) -> Result<(), Self::Error> {
120        self.packet_count += 1;
121
122        Ok(())
123    }
124
125    fn on_mtc_packet(
126        &mut self,
127        _context: &DecoderContext,
128        _ctc_payload: u8,
129    ) -> Result<(), Self::Error> {
130        self.packet_count += 1;
131
132        Ok(())
133    }
134
135    fn on_tsc_packet(
136        &mut self,
137        _context: &DecoderContext,
138        _tsc_value: u64,
139    ) -> Result<(), Self::Error> {
140        self.packet_count += 1;
141
142        Ok(())
143    }
144
145    fn on_cbr_packet(
146        &mut self,
147        _context: &DecoderContext,
148        _core_bus_ratio: u8,
149    ) -> Result<(), Self::Error> {
150        self.packet_count += 1;
151
152        Ok(())
153    }
154
155    fn on_tma_packet(
156        &mut self,
157        _context: &DecoderContext,
158        _ctc: u16,
159        _fast_counter: u8,
160        _fc8: bool,
161    ) -> Result<(), Self::Error> {
162        self.packet_count += 1;
163
164        Ok(())
165    }
166
167    fn on_vmcs_packet(
168        &mut self,
169        _context: &DecoderContext,
170        _vmcs_pointer: u64,
171    ) -> Result<(), Self::Error> {
172        self.packet_count += 1;
173
174        Ok(())
175    }
176
177    fn on_ovf_packet(&mut self, _context: &DecoderContext) -> Result<(), Self::Error> {
178        self.packet_count += 1;
179
180        Ok(())
181    }
182
183    fn on_psb_packet(&mut self, _context: &DecoderContext) -> Result<(), Self::Error> {
184        self.packet_count += 1;
185
186        Ok(())
187    }
188
189    fn on_psbend_packet(&mut self, _context: &DecoderContext) -> Result<(), Self::Error> {
190        self.packet_count += 1;
191
192        Ok(())
193    }
194
195    fn on_trace_stop_packet(&mut self, _context: &DecoderContext) -> Result<(), Self::Error> {
196        self.packet_count += 1;
197
198        Ok(())
199    }
200
201    fn on_pip_packet(
202        &mut self,
203        _context: &DecoderContext,
204        _cr3: u64,
205        _rsvd_nr: bool,
206    ) -> Result<(), Self::Error> {
207        self.packet_count += 1;
208
209        Ok(())
210    }
211
212    fn on_mnt_packet(
213        &mut self,
214        _context: &DecoderContext,
215        _payload: u64,
216    ) -> Result<(), Self::Error> {
217        self.packet_count += 1;
218
219        Ok(())
220    }
221
222    fn on_ptw_packet(
223        &mut self,
224        _context: &DecoderContext,
225        _ip_bit: bool,
226        _payload: crate::PtwPayload,
227    ) -> Result<(), Self::Error> {
228        self.packet_count += 1;
229
230        Ok(())
231    }
232
233    fn on_exstop_packet(
234        &mut self,
235        _context: &DecoderContext,
236        _ip_bit: bool,
237    ) -> Result<(), Self::Error> {
238        self.packet_count += 1;
239
240        Ok(())
241    }
242
243    fn on_mwait_packet(
244        &mut self,
245        _context: &DecoderContext,
246        _mwait_hints: u8,
247        _ext: u8,
248    ) -> Result<(), Self::Error> {
249        self.packet_count += 1;
250
251        Ok(())
252    }
253
254    fn on_pwre_packet(
255        &mut self,
256        _context: &DecoderContext,
257        _hw: bool,
258        _resolved_thread_c_state: u8,
259        _resolved_thread_sub_c_state: u8,
260    ) -> Result<(), Self::Error> {
261        self.packet_count += 1;
262
263        Ok(())
264    }
265
266    fn on_pwrx_packet(
267        &mut self,
268        _context: &DecoderContext,
269        _last_core_c_state: u8,
270        _deepest_core_c_state: u8,
271        _wake_reason: u8,
272    ) -> Result<(), Self::Error> {
273        self.packet_count += 1;
274
275        Ok(())
276    }
277
278    fn on_evd_packet(
279        &mut self,
280        _context: &DecoderContext,
281        _type: u8,
282        _payload: u64,
283    ) -> Result<(), Self::Error> {
284        self.packet_count += 1;
285
286        Ok(())
287    }
288
289    fn on_cfe_packet(
290        &mut self,
291        _context: &DecoderContext,
292        _ip_bit: bool,
293        _type: u8,
294        _vector: u8,
295    ) -> Result<(), Self::Error> {
296        self.packet_count += 1;
297
298        Ok(())
299    }
300
301    fn on_bbp_packet(
302        &mut self,
303        _context: &DecoderContext,
304        _sz_bit: bool,
305        _type: u8,
306    ) -> Result<(), Self::Error> {
307        self.packet_count += 1;
308
309        Ok(())
310    }
311
312    fn on_bep_packet(
313        &mut self,
314        _context: &DecoderContext,
315        _ip_bit: bool,
316    ) -> Result<(), Self::Error> {
317        self.packet_count += 1;
318
319        Ok(())
320    }
321
322    fn on_bip_packet(
323        &mut self,
324        _context: &DecoderContext,
325        _id: u8,
326        _payload: &[u8],
327        _bbp_type: u8,
328    ) -> Result<(), Self::Error> {
329        self.packet_count += 1;
330
331        Ok(())
332    }
333}