Skip to content

Commit 5f4c46c

Browse files
Copilotxusheng6
andcommitted
Add comprehensive documentation for TTD code coverage feature
Co-authored-by: xusheng6 <[email protected]>
1 parent 8b0eedb commit 5f4c46c

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

docs/ttd-code-coverage.md

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# TTD Code Coverage Analysis
2+
3+
This document describes the TTD Code Coverage functionality added to the Binary Ninja debugger.
4+
5+
## Overview
6+
7+
The TTD Code Coverage feature analyzes Time Travel Debugging (TTD) traces to determine which instructions were executed during the recorded session. This information is then displayed as visual highlights in the disassembly view, allowing you to quickly see which code paths were taken.
8+
9+
## Features
10+
11+
### 1. Code Coverage Analysis
12+
- **Purpose**: Extract executed instruction addresses from TTD traces
13+
- **Method**: Queries TTD memory access events for execute operations
14+
- **Output**: Set of executed instruction addresses stored in memory
15+
- **Performance**: Single efficient query covering entire executable range
16+
17+
### 2. Visual Highlighting
18+
- **Executed Instructions**: Highlighted with green background (light transparency)
19+
- **Current Instruction**: Blue highlight (existing functionality)
20+
- **Breakpoints**: Red highlight (existing functionality)
21+
- **Combined States**: Magenta for breakpoint + current instruction
22+
23+
### 3. Analysis Dialog
24+
- **Location**: Debugger menu → "TTD Analysis..."
25+
- **Features**:
26+
- Progress tracking with threaded execution
27+
- Result metrics (number of executed instructions)
28+
- Cache management (save/load results)
29+
- Extensible for future analysis types
30+
31+
### 4. Caching System
32+
- **Metadata**: JSON format with analysis information
33+
- **Data**: Binary format for executed instruction addresses
34+
- **Auto-detection**: Shows cache status on dialog startup
35+
- **Manual Control**: Save/load/clear cache operations
36+
37+
## Usage
38+
39+
### Prerequisites
40+
- Binary Ninja with debugger plugin
41+
- TTD-capable debug adapter (e.g., DbgEng TTD adapter)
42+
- Active TTD trace
43+
44+
### Running Code Coverage Analysis
45+
46+
1. **Open the Analysis Dialog**
47+
- Menu: `Debugger → TTD Analysis...`
48+
- Requires active TTD session
49+
50+
2. **Start Analysis**
51+
- Select "Code Coverage" from the list
52+
- Click "Run Analysis"
53+
- Monitor progress bar and status
54+
55+
3. **View Results**
56+
- Executed instructions appear with green highlights
57+
- Check result count in dialog
58+
- Optionally save results to cache
59+
60+
4. **Cache Management**
61+
- **Auto-save**: Enable "Automatically cache results"
62+
- **Manual save**: Click "Save Results" after analysis
63+
- **Load cache**: Click "Load Results" to restore previous analysis
64+
- **Clear cache**: Remove all cached analysis files
65+
66+
## API Reference
67+
68+
### New DebuggerController Methods
69+
70+
```cpp
71+
// Check if specific instruction was executed
72+
bool IsInstructionExecuted(uint64_t address);
73+
74+
// Run complete code coverage analysis
75+
bool RunCodeCoverageAnalysis();
76+
77+
// Get number of executed instructions found
78+
size_t GetExecutedInstructionCount() const;
79+
80+
// Save/load analysis results to/from file
81+
bool SaveCodeCoverageToFile(const std::string& filePath) const;
82+
bool LoadCodeCoverageFromFile(const std::string& filePath);
83+
```
84+
85+
### Usage Example (C++)
86+
87+
```cpp
88+
// Get debugger controller
89+
auto controller = DebuggerController::GetController(binaryView);
90+
if (!controller || !controller->IsTTD()) {
91+
return; // TTD not available
92+
}
93+
94+
// Run analysis
95+
if (controller->RunCodeCoverageAnalysis()) {
96+
size_t executedCount = controller->GetExecutedInstructionCount();
97+
LogInfo("Found {} executed instructions", executedCount);
98+
99+
// Check specific instruction
100+
if (controller->IsInstructionExecuted(0x401000)) {
101+
LogInfo("Instruction at 0x401000 was executed");
102+
}
103+
104+
// Save results
105+
controller->SaveCodeCoverageToFile("/path/to/cache.data");
106+
}
107+
```
108+
109+
## Technical Details
110+
111+
### Data Storage
112+
- **In-memory**: `std::unordered_set<uint64_t>` for O(1) lookup performance
113+
- **Cache format**: Binary file with magic number (0x54544443) and version
114+
- **Metadata**: JSON file with analysis details and statistics
115+
116+
### Analysis Algorithm
117+
1. Enumerate all instruction addresses from all functions
118+
2. Query TTD for execute access events in entire executable range
119+
3. Filter results to only include actual instruction addresses
120+
4. Store executed addresses in unordered set for fast lookup
121+
122+
### Render Layer Integration
123+
- Hooks into existing DebuggerRenderLayer
124+
- Minimal changes to existing highlighting logic
125+
- Green highlight with 64/255 alpha for executed instructions
126+
- Preserves existing breakpoint and PC highlighting
127+
128+
### Thread Safety
129+
- Analysis runs in separate QThread worker
130+
- Mutex protection for result data access
131+
- Progress signals for UI updates
132+
133+
## Cache File Format
134+
135+
### Metadata (.json)
136+
```json
137+
{
138+
"type": 0,
139+
"name": "Code Coverage",
140+
"description": "...",
141+
"resultCount": 1234,
142+
"lastRun": "2023-12-07T10:30:00Z",
143+
"status": 2
144+
}
145+
```
146+
147+
### Data (.data)
148+
```
149+
[4 bytes] Magic: 0x54544443 ("TTDC")
150+
[4 bytes] Version: 1
151+
[8 bytes] Count: number of addresses
152+
[8*Count bytes] Addresses: executed instruction addresses
153+
```
154+
155+
## Troubleshooting
156+
157+
### Common Issues
158+
159+
1. **"TTD not available"**
160+
- Ensure TTD-capable adapter is active
161+
- Check that trace is loaded and accessible
162+
163+
2. **"Analysis failed"**
164+
- Verify TTD trace contains execute events
165+
- Check available memory and disk space
166+
- Review log output for specific errors
167+
168+
3. **No highlights visible**
169+
- Confirm analysis completed successfully
170+
- Check that cache was loaded properly
171+
- Verify you're viewing the correct binary/view
172+
173+
### Performance Considerations
174+
175+
- **Large traces**: Analysis time scales with trace size
176+
- **Memory usage**: ~8 bytes per executed instruction
177+
- **Cache size**: Depends on number of unique executed instructions
178+
- **Disk I/O**: Caching improves subsequent analysis speed
179+
180+
## Future Enhancements
181+
182+
The analysis framework is designed to be extensible:
183+
184+
- **Additional analysis types**: Memory access patterns, branch coverage, etc.
185+
- **Export formats**: Integration with external coverage tools
186+
- **Statistics**: Coverage percentages, hot paths, etc.
187+
- **Filtering**: Analysis of specific time ranges or threads
188+
189+
## Implementation Notes
190+
191+
This feature maintains the existing debugger architecture with minimal changes:
192+
193+
- **5 new API methods** added to DebuggerController
194+
- **Backward compatible** with existing functionality
195+
- **Thread-safe** analysis with progress reporting
196+
- **Extensible design** for future analysis types
197+
- **Robust error handling** and validation
198+
199+
The implementation follows Binary Ninja plugin patterns and integrates cleanly with the existing TTD infrastructure.

0 commit comments

Comments
 (0)