Skip to main content

torsh_jit/debugger/
core.rs

1//! Core types and data structures for JIT debugging
2//!
3//! This module provides fundamental types, enums, and configurations
4//! used throughout the JIT debugging system.
5
6use crate::NodeId;
7use std::collections::HashMap;
8use torsh_core::{DType, Shape};
9
10/// Debugger configuration
11#[derive(Debug, Clone)]
12pub struct DebuggerConfig {
13    pub enable_single_step: bool,
14    pub enable_breakpoints: bool,
15    pub enable_watches: bool,
16    pub enable_memory_view: bool,
17    pub enable_disassembly: bool,
18    pub max_trace_length: usize,
19    pub ui_mode: UiMode,
20}
21
22impl Default for DebuggerConfig {
23    fn default() -> Self {
24        Self {
25            enable_single_step: true,
26            enable_breakpoints: true,
27            enable_watches: true,
28            enable_memory_view: true,
29            enable_disassembly: true,
30            max_trace_length: 10000,
31            ui_mode: UiMode::Interactive,
32        }
33    }
34}
35
36/// UI mode for the debugger
37#[derive(Debug, Clone)]
38pub enum UiMode {
39    Interactive,
40    Batch,
41    Remote,
42}
43
44/// Debug commands
45#[derive(Debug, Clone)]
46pub enum DebugCommand {
47    Step,
48    StepOver,
49    StepInto,
50    StepOut,
51    Continue,
52    SetBreakpoint { location: BreakpointLocation },
53    RemoveBreakpoint { id: BreakpointId },
54    ListBreakpoints,
55    Watch { expression: String },
56    Unwatch { id: WatchId },
57    ListWatches,
58    Inspect { target: InspectionTarget },
59    CallStack,
60    Locals,
61    Memory { address: u64 },
62    Disassemble { location: ExecutionLocation },
63    Help,
64    Quit,
65}
66
67/// Execution locations
68#[derive(Debug, Clone)]
69pub enum ExecutionLocation {
70    GraphNode(NodeId),
71    Instruction {
72        function: String,
73        instruction_index: usize,
74    },
75    Completed,
76}
77
78/// Breakpoint locations
79#[derive(Debug, Clone)]
80pub enum BreakpointLocation {
81    GraphNode(NodeId),
82    Instruction {
83        function: String,
84        instruction: usize,
85    },
86}
87
88/// Inspection targets
89#[derive(Debug, Clone)]
90pub enum InspectionTarget {
91    Variable(String),
92    Node(NodeId),
93    Memory(u64),
94}
95
96/// Debug values that can be inspected
97#[derive(Debug, Clone, PartialEq)]
98pub enum DebugValue {
99    Scalar(f64),
100    Integer(i64),
101    Boolean(bool),
102    Tensor {
103        data: Vec<f32>,
104        shape: Shape,
105        dtype: DType,
106    },
107}
108
109/// Breakpoint representation
110#[derive(Debug, Clone)]
111pub struct Breakpoint {
112    pub id: BreakpointId,
113    pub location: BreakpointLocation,
114    pub condition: Option<String>,
115    pub enabled: bool,
116    pub hit_count: u32,
117}
118
119/// Watch representation
120#[derive(Debug, Clone)]
121pub struct Watch {
122    pub id: WatchId,
123    pub expression: String,
124    pub enabled: bool,
125    pub last_value: Option<DebugValue>,
126}
127
128/// Call frame for call stack
129#[derive(Debug, Clone)]
130pub struct CallFrame {
131    pub function_name: String,
132    pub location: ExecutionLocation,
133    pub return_location: ExecutionLocation,
134    pub local_variables: HashMap<String, DebugValue>,
135}
136
137/// Current debug state
138#[derive(Debug, Clone)]
139pub struct DebugState {
140    pub location: ExecutionLocation,
141    pub call_stack: crate::debugger::state::CallStack,
142    pub variables: HashMap<String, DebugValue>,
143    pub execution_step: usize,
144    pub is_running: bool,
145}
146
147/// Execution step in the trace
148#[derive(Debug, Clone)]
149pub struct ExecutionStep {
150    pub location: ExecutionLocation,
151    pub timestamp: std::time::SystemTime,
152    pub operation: String,
153    pub inputs: Vec<NodeExecutionResult>,
154    pub outputs: Vec<NodeExecutionResult>,
155    pub state_changes: HashMap<String, DebugValue>,
156}
157
158/// Execution state tracking
159#[derive(Debug, Clone)]
160pub struct ExecutionState {
161    pub registers: HashMap<u32, DebugValue>,
162    pub memory: crate::debugger::state::MemoryState,
163    pub flags: HashMap<String, bool>,
164}
165
166impl ExecutionState {
167    pub fn new() -> Self {
168        Self {
169            registers: HashMap::new(),
170            memory: crate::debugger::state::MemoryState::new(),
171            flags: HashMap::new(),
172        }
173    }
174}
175
176/// Node execution result
177#[derive(Debug, Clone)]
178pub struct NodeExecutionResult {
179    pub data: Vec<f32>,
180    pub shape: Shape,
181    pub dtype: DType,
182}
183
184/// Instruction execution result
185#[derive(Debug, Clone)]
186pub enum InstructionExecutionResult {
187    Value(DebugValue),
188    SideEffect,
189    Return,
190    NoOp,
191}
192
193/// Debug statistics
194#[derive(Debug, Clone)]
195pub struct DebugStatistics {
196    pub total_steps: usize,
197    pub total_execution_time: std::time::Duration,
198    pub breakpoints_hit: usize,
199    pub watches_triggered: usize,
200}
201
202impl DebugStatistics {
203    pub fn new() -> Self {
204        Self {
205            total_steps: 0,
206            total_execution_time: std::time::Duration::from_millis(0),
207            breakpoints_hit: 0,
208            watches_triggered: 0,
209        }
210    }
211}
212
213/// Type information for debug values
214#[derive(Debug, Clone)]
215pub struct TypeInfo {
216    pub type_name: String,
217    pub size_bytes: usize,
218    pub alignment: usize,
219}
220
221/// Node metadata for debugging
222#[derive(Debug, Clone)]
223pub struct NodeMetadata {
224    pub operation: String,
225    pub input_count: usize,
226    pub output_shape: Shape,
227    pub dtype: DType,
228}
229
230/// Memory view for debugging
231#[derive(Debug, Clone)]
232pub struct MemoryView {
233    pub start_address: u64,
234    pub content: Vec<u8>,
235    pub size: usize,
236}
237
238/// Disassembly view
239#[derive(Debug, Clone)]
240pub struct DisassemblyView {
241    pub location: ExecutionLocation,
242    pub instructions: Vec<DisassemblyInstruction>,
243}
244
245/// Disassembly instruction
246#[derive(Debug, Clone)]
247pub struct DisassemblyInstruction {
248    pub address: u64,
249    pub opcode: String,
250    pub operands: String,
251    pub comment: Option<String>,
252}
253
254/// Watch update notification
255#[derive(Debug, Clone)]
256pub struct WatchUpdate {
257    pub watch_id: WatchId,
258    pub old_value: Option<DebugValue>,
259    pub new_value: DebugValue,
260}
261
262/// Evaluation result for expressions
263#[derive(Debug, Clone)]
264pub struct EvaluationResult {
265    pub expression: String,
266    pub result: DebugValue,
267    pub success: bool,
268    pub error_message: Option<String>,
269}
270
271/// Inspection result
272#[derive(Debug, Clone)]
273pub enum InspectionResult {
274    Variable {
275        name: String,
276        value: DebugValue,
277        type_info: TypeInfo,
278    },
279    Node {
280        node_id: NodeId,
281        value: DebugValue,
282        metadata: NodeMetadata,
283    },
284    Memory {
285        address: u64,
286        content: Vec<u8>,
287        size: usize,
288    },
289}
290
291/// Result types
292#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
293pub struct BreakpointId(pub u64);
294
295#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
296pub struct WatchId(pub u64);
297
298#[derive(Debug)]
299pub enum DebugCommandResult {
300    Continue,
301    Exit,
302    ExecutionComplete,
303}
304
305#[derive(Debug)]
306pub enum StepResult {
307    Success,
308    Completed,
309}
310
311#[derive(Debug)]
312pub enum ContinueResult {
313    Breakpoint,
314    Completed,
315}
316
317/// Debug session result
318#[derive(Debug)]
319pub struct DebugSessionResult {
320    pub execution_trace: Vec<ExecutionStep>,
321    pub final_state: DebugState,
322    pub command_history: Vec<DebugCommand>,
323    pub statistics: DebugStatistics,
324}