firedbg_protocol/
event.rs

1//! Data structures for Debugger Event
2use crate::value::RValue;
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6/// Debugger Event
7pub enum Event {
8    Breakpoint {
9        breakpoint_id: u32,
10        thread_id: u64,
11        frame_id: u64,
12        reason: Reason,
13        locals: Vec<(String, RValue)>,
14    },
15    FunctionCall {
16        breakpoint_id: u32,
17        thread_id: u64,
18        frame_id: u64,
19        stack_pointer: u64,
20        function_name: String,
21        arguments: Vec<(String, RValue)>,
22    },
23    FunctionReturn {
24        breakpoint_id: u32,
25        thread_id: u64,
26        frame_id: u64,
27        function_name: String,
28        return_value: RValue,
29    },
30}
31
32#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
33/// Reason a breakpoint is hit.
34pub enum Reason {
35    #[default]
36    Breakpoint,
37    Panic,
38}
39
40impl Event {
41    /// Print arguments in the form of "name: value, .."
42    ///
43    /// # Panics
44    ///
45    /// Panic if self is not FunctionCall
46    pub fn format_arguments(&self) -> String {
47        use std::fmt::Write;
48
49        match self {
50            Event::FunctionCall { arguments, .. } => {
51                let mut string = String::new();
52                for (i, (name, value)) in arguments.iter().enumerate() {
53                    write!(string, "{}{name}: {value}", if i > 0 { ", " } else { "" }).unwrap();
54                }
55                string
56            }
57            _ => panic!("Not FunctionCall"),
58        }
59    }
60
61    /// Redact the memory address of all values. Intended for testing.
62    pub fn redacted(&mut self) {
63        match self {
64            Event::Breakpoint {
65                breakpoint_id,
66                thread_id,
67                locals,
68                ..
69            } => {
70                *breakpoint_id = u32::MAX;
71                *thread_id = u64::MAX;
72                for (_, ref mut local) in locals.iter_mut() {
73                    local.redact_addr();
74                }
75            }
76            Event::FunctionCall {
77                breakpoint_id,
78                thread_id,
79                stack_pointer,
80                arguments,
81                ..
82            } => {
83                *breakpoint_id = u32::MAX;
84                *thread_id = u64::MAX;
85                *stack_pointer = u64::MAX;
86                for (_, ref mut argument) in arguments.iter_mut() {
87                    argument.redact_addr();
88                }
89            }
90            Event::FunctionReturn {
91                breakpoint_id,
92                thread_id,
93                return_value,
94                ..
95            } => {
96                *breakpoint_id = u32::MAX;
97                *thread_id = u64::MAX;
98                return_value.redact_addr();
99            }
100        }
101    }
102}