Skip to content

Commit 057fcbe

Browse files
Copilotxusheng6
andcommitted
Complete TTD Heap implementation with documentation and examples
Co-authored-by: xusheng6 <[email protected]>
1 parent 9296ae7 commit 057fcbe

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

docs/ttd-python-api.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,36 @@ class TTDCallEvent:
8585
"""
8686
```
8787

88+
### TTDHeapEvent
89+
90+
Represents a heap operation event in a TTD trace.
91+
92+
```python
93+
class TTDHeapEvent:
94+
"""
95+
TTDHeapEvent represents a heap operation event in a TTD trace.
96+
97+
Attributes:
98+
event_type (str): Type of the event (always "Heap" for TTD.Heap objects)
99+
action (str): Heap action that occurred (Alloc, ReAlloc, Free, Create, Protect, Lock, Unlock, Destroy)
100+
thread_id (int): OS thread ID that performed the heap operation
101+
unique_thread_id (int): Unique thread ID across the trace
102+
heap (int): Handle for the Win32 heap
103+
address (int): Address of the allocated object (if applicable)
104+
previous_address (int): Address before reallocation (for ReAlloc operations)
105+
size (int): Size of allocated object (if applicable)
106+
base_address (int): Base address of allocated object (if applicable)
107+
flags (int): Heap API flags (meaning depends on the specific API)
108+
result (int): Result of heap API call (non-zero means success)
109+
reserve_size (int): Amount of memory to reserve (for Create operations)
110+
commit_size (int): Initial committed size (for Create operations)
111+
make_read_only (int): Non-zero indicates request to make heap read-only
112+
parameters (List[str]): List of raw parameters from the heap call
113+
time_start (TTDPosition): TTD position when heap operation started
114+
time_end (TTDPosition): TTD position when heap operation ended
115+
"""
116+
```
117+
88118
## Constants and Access Types
89119

90120
### DebuggerTTDMemoryAccessType Enum
@@ -188,6 +218,22 @@ def get_ttd_calls_for_symbols(
188218
"""
189219
```
190220

221+
### get_ttd_heap_objects()
222+
223+
```python
224+
def get_ttd_heap_objects(self) -> List[TTDHeapEvent]:
225+
"""
226+
Get TTD heap operation events.
227+
228+
This method queries all heap operations that occurred during the TTD trace.
229+
It provides information about heap allocations, deallocations, reallocations,
230+
and other heap management operations.
231+
232+
Returns:
233+
List of TTDHeapEvent objects representing heap operations
234+
"""
235+
```
236+
191237
### get_current_ttd_position()
192238

193239
```python
@@ -264,6 +310,58 @@ for call in call_events:
264310
print(f" Return value: {call.return_value:#x}")
265311
```
266312

313+
### Heap Analysis
314+
315+
```python
316+
# Analyze heap operations during the trace
317+
heap_events = dbg.get_ttd_heap_objects()
318+
319+
print(f"Found {len(heap_events)} heap operations")
320+
321+
# Group by action type
322+
actions = {}
323+
for event in heap_events:
324+
if event.action not in actions:
325+
actions[event.action] = []
326+
actions[event.action].append(event)
327+
328+
# Display summary
329+
for action, events in actions.items():
330+
print(f"{action}: {len(events)} operations")
331+
332+
# Find large allocations
333+
large_allocs = [e for e in heap_events
334+
if e.action == "Alloc" and e.size > 1024*1024] # > 1MB
335+
336+
print(f"Found {len(large_allocs)} large allocations (>1MB)")
337+
for alloc in large_allocs:
338+
print(f" {alloc.size} bytes at {alloc.address:#x} (heap {alloc.heap:#x})")
339+
print(f" Time: {alloc.time_start}")
340+
341+
# Analyze heap usage patterns
342+
heap_stats = {}
343+
for event in heap_events:
344+
heap_handle = event.heap
345+
if heap_handle not in heap_stats:
346+
heap_stats[heap_handle] = {'allocs': 0, 'frees': 0, 'total_allocated': 0}
347+
348+
if event.action == "Alloc":
349+
heap_stats[heap_handle]['allocs'] += 1
350+
heap_stats[heap_handle]['total_allocated'] += event.size
351+
elif event.action == "Free":
352+
heap_stats[heap_handle]['frees'] += 1
353+
354+
print("\nHeap usage statistics:")
355+
for heap_handle, stats in heap_stats.items():
356+
print(f"Heap {heap_handle:#x}:")
357+
print(f" Allocations: {stats['allocs']}")
358+
print(f" Frees: {stats['frees']}")
359+
print(f" Total allocated: {stats['total_allocated']} bytes")
360+
leaked = stats['allocs'] - stats['frees']
361+
if leaked > 0:
362+
print(f" Potential leaks: {leaked} allocations")
363+
```
364+
267365
### TTD Navigation
268366

269367
```python

0 commit comments

Comments
 (0)