1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! Breakpoint management for JIT debugging
//!
//! This module provides comprehensive breakpoint management capabilities including
//! setting, removing, and checking breakpoints at various execution locations.
use super::core::{Breakpoint, BreakpointId, BreakpointLocation, ExecutionLocation};
use crate::{JitError, JitResult};
use std::collections::HashMap;
/// Breakpoint manager
pub struct BreakpointManager {
breakpoints: HashMap<BreakpointId, Breakpoint>,
next_id: BreakpointId,
}
impl BreakpointManager {
/// Create a new breakpoint manager
pub fn new() -> Self {
Self {
breakpoints: HashMap::new(),
next_id: BreakpointId(0),
}
}
/// Set a breakpoint at the specified location
///
/// # Arguments
/// * `location` - The location where the breakpoint should be set
///
/// # Returns
/// The ID of the newly created breakpoint
///
/// # Examples
/// ```rust
/// use torsh_jit::debugger::{BreakpointManager, BreakpointLocation};
/// use torsh_jit::NodeId;
///
/// let mut manager = BreakpointManager::new();
/// let location = BreakpointLocation::GraphNode(NodeId::new(0));
/// let id = manager.set_breakpoint(location).unwrap();
/// ```
pub fn set_breakpoint(&mut self, location: BreakpointLocation) -> JitResult<BreakpointId> {
let id = self.next_id;
self.next_id = BreakpointId(self.next_id.0 + 1);
let breakpoint = Breakpoint {
id,
location,
condition: None,
enabled: true,
hit_count: 0,
};
self.breakpoints.insert(id, breakpoint);
Ok(id)
}
/// Set a conditional breakpoint at the specified location
///
/// # Arguments
/// * `location` - The location where the breakpoint should be set
/// * `condition` - The condition that must be true for the breakpoint to trigger
///
/// # Returns
/// The ID of the newly created breakpoint
pub fn set_conditional_breakpoint(
&mut self,
location: BreakpointLocation,
condition: String,
) -> JitResult<BreakpointId> {
let id = self.next_id;
self.next_id = BreakpointId(self.next_id.0 + 1);
let breakpoint = Breakpoint {
id,
location,
condition: Some(condition),
enabled: true,
hit_count: 0,
};
self.breakpoints.insert(id, breakpoint);
Ok(id)
}
/// Remove a breakpoint by ID
///
/// # Arguments
/// * `id` - The ID of the breakpoint to remove
///
/// # Returns
/// `Ok(())` if the breakpoint was removed, error if not found
pub fn remove_breakpoint(&mut self, id: BreakpointId) -> JitResult<()> {
if self.breakpoints.remove(&id).is_some() {
Ok(())
} else {
Err(JitError::RuntimeError(format!(
"Breakpoint {} not found",
id.0
)))
}
}
/// Enable a breakpoint
///
/// # Arguments
/// * `id` - The ID of the breakpoint to enable
pub fn enable_breakpoint(&mut self, id: BreakpointId) -> JitResult<()> {
if let Some(breakpoint) = self.breakpoints.get_mut(&id) {
breakpoint.enabled = true;
Ok(())
} else {
Err(JitError::RuntimeError(format!(
"Breakpoint {} not found",
id.0
)))
}
}
/// Disable a breakpoint
///
/// # Arguments
/// * `id` - The ID of the breakpoint to disable
pub fn disable_breakpoint(&mut self, id: BreakpointId) -> JitResult<()> {
if let Some(breakpoint) = self.breakpoints.get_mut(&id) {
breakpoint.enabled = false;
Ok(())
} else {
Err(JitError::RuntimeError(format!(
"Breakpoint {} not found",
id.0
)))
}
}
/// Get a list of all breakpoints
///
/// # Returns
/// A vector of references to all breakpoints
pub fn list_breakpoints(&self) -> Vec<&Breakpoint> {
self.breakpoints.values().collect()
}
/// Get a specific breakpoint by ID
///
/// # Arguments
/// * `id` - The ID of the breakpoint to retrieve
///
/// # Returns
/// An optional reference to the breakpoint
pub fn get_breakpoint(&self, id: BreakpointId) -> Option<&Breakpoint> {
self.breakpoints.get(&id)
}
/// Check if there is a breakpoint at the specified location
///
/// # Arguments
/// * `location` - The execution location to check
///
/// # Returns
/// `true` if there is an enabled breakpoint at the location, `false` otherwise
pub fn is_breakpoint_at(&self, location: &ExecutionLocation) -> bool {
self.breakpoints.values().any(|bp| {
bp.enabled
&& match (&bp.location, location) {
(
BreakpointLocation::GraphNode(bp_node),
ExecutionLocation::GraphNode(loc_node),
) => bp_node == loc_node,
(
BreakpointLocation::Instruction {
function: bp_func,
instruction: bp_inst,
},
ExecutionLocation::Instruction {
function: loc_func,
instruction_index: loc_inst,
},
) => bp_func == loc_func && *bp_inst == *loc_inst,
_ => false,
}
})
}
/// Increment hit count for breakpoints at the specified location
///
/// # Arguments
/// * `location` - The execution location where a hit occurred
///
/// # Returns
/// The number of breakpoints hit at this location
pub fn hit_breakpoints_at(&mut self, location: &ExecutionLocation) -> usize {
let mut hit_count = 0;
for breakpoint in self.breakpoints.values_mut() {
if breakpoint.enabled
&& match (&breakpoint.location, location) {
(
BreakpointLocation::GraphNode(bp_node),
ExecutionLocation::GraphNode(loc_node),
) => bp_node == loc_node,
(
BreakpointLocation::Instruction {
function: bp_func,
instruction: bp_inst,
},
ExecutionLocation::Instruction {
function: loc_func,
instruction_index: loc_inst,
},
) => bp_func == loc_func && *bp_inst == *loc_inst,
_ => false,
}
{
breakpoint.hit_count += 1;
hit_count += 1;
}
}
hit_count
}
/// Clear all breakpoints
pub fn clear_all_breakpoints(&mut self) {
self.breakpoints.clear();
}
/// Get the number of breakpoints
pub fn count(&self) -> usize {
self.breakpoints.len()
}
/// Get the number of enabled breakpoints
pub fn enabled_count(&self) -> usize {
self.breakpoints.values().filter(|bp| bp.enabled).count()
}
/// Get all breakpoints at a specific location
///
/// # Arguments
/// * `location` - The location to search for breakpoints
///
/// # Returns
/// A vector of references to breakpoints at the specified location
pub fn get_breakpoints_at(&self, location: &BreakpointLocation) -> Vec<&Breakpoint> {
self.breakpoints
.values()
.filter(|bp| match (&bp.location, location) {
(
BreakpointLocation::GraphNode(bp_node),
BreakpointLocation::GraphNode(loc_node),
) => bp_node == loc_node,
(
BreakpointLocation::Instruction {
function: bp_func,
instruction: bp_inst,
},
BreakpointLocation::Instruction {
function: loc_func,
instruction: loc_inst,
},
) => bp_func == loc_func && bp_inst == loc_inst,
_ => false,
})
.collect()
}
}
impl Default for BreakpointManager {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::NodeId;
#[test]
fn test_breakpoint_manager_creation() {
let manager = BreakpointManager::new();
assert_eq!(manager.count(), 0);
assert_eq!(manager.enabled_count(), 0);
}
#[test]
fn test_set_and_remove_breakpoint() {
let mut manager = BreakpointManager::new();
let location = BreakpointLocation::GraphNode(NodeId::new(0));
let id = manager
.set_breakpoint(location)
.expect("breakpoint setting should succeed");
assert_eq!(manager.count(), 1);
assert_eq!(manager.enabled_count(), 1);
assert!(manager.remove_breakpoint(id).is_ok());
assert_eq!(manager.count(), 0);
}
#[test]
fn test_conditional_breakpoint() {
let mut manager = BreakpointManager::new();
let location = BreakpointLocation::GraphNode(NodeId::new(0));
let condition = "x > 10".to_string();
let id = manager
.set_conditional_breakpoint(location, condition.clone())
.expect("operation should succeed");
let breakpoint = manager
.get_breakpoint(id)
.expect("breakpoint retrieval should succeed");
assert_eq!(breakpoint.condition, Some(condition));
}
#[test]
fn test_enable_disable_breakpoint() {
let mut manager = BreakpointManager::new();
let location = BreakpointLocation::GraphNode(NodeId::new(0));
let id = manager
.set_breakpoint(location)
.expect("breakpoint setting should succeed");
assert_eq!(manager.enabled_count(), 1);
manager
.disable_breakpoint(id)
.expect("breakpoint disable should succeed");
assert_eq!(manager.enabled_count(), 0);
manager
.enable_breakpoint(id)
.expect("breakpoint enable should succeed");
assert_eq!(manager.enabled_count(), 1);
}
#[test]
fn test_is_breakpoint_at() {
let mut manager = BreakpointManager::new();
let node_id = NodeId::new(0);
let location = BreakpointLocation::GraphNode(node_id);
manager
.set_breakpoint(location)
.expect("breakpoint setting should succeed");
let exec_location = ExecutionLocation::GraphNode(node_id);
assert!(manager.is_breakpoint_at(&exec_location));
let other_exec_location = ExecutionLocation::GraphNode(NodeId::new(1));
assert!(!manager.is_breakpoint_at(&other_exec_location));
}
#[test]
fn test_hit_breakpoints() {
let mut manager = BreakpointManager::new();
let node_id = NodeId::new(0);
let location = BreakpointLocation::GraphNode(node_id);
let id = manager
.set_breakpoint(location)
.expect("breakpoint setting should succeed");
let exec_location = ExecutionLocation::GraphNode(node_id);
let hit_count = manager.hit_breakpoints_at(&exec_location);
assert_eq!(hit_count, 1);
let breakpoint = manager
.get_breakpoint(id)
.expect("breakpoint retrieval should succeed");
assert_eq!(breakpoint.hit_count, 1);
}
#[test]
fn test_clear_all_breakpoints() {
let mut manager = BreakpointManager::new();
manager
.set_breakpoint(BreakpointLocation::GraphNode(NodeId::new(0)))
.expect("operation should succeed");
manager
.set_breakpoint(BreakpointLocation::GraphNode(NodeId::new(1)))
.expect("operation should succeed");
assert_eq!(manager.count(), 2);
manager.clear_all_breakpoints();
assert_eq!(manager.count(), 0);
}
}