-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_breakpoint.py
More file actions
208 lines (173 loc) · 6.36 KB
/
simple_breakpoint.py
File metadata and controls
208 lines (173 loc) · 6.36 KB
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
"""
Simple example demonstrating basic debugging workflow
"""
import sys
import os
# Add parent directory to path to import src module
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src.debugger import AIDebugger
import json
def main():
"""
Example: Basic debugging workflow
This example shows how to:
1. Create a debugging session
2. Set breakpoints
3. Run the program
4. Step through code
5. Inspect variables
6. Get call stack
7. Close the session
"""
# Initialize the debugger
debugger = AIDebugger()
# Replace with your program path
program_path = "./my_program"
print("=" * 60)
print("C/C++ AI Debugger - Basic Example")
print("=" * 60)
try:
# Step 1: Create a debugging session
print("\n[1] Creating debug session...")
result = debugger.execute_command(json.dumps({
'action': 'create_session',
'params': {
'program_path': program_path,
'debugger_type': 'auto' # Auto-detect GDB or LLDB
}
}))
response = json.loads(result)
if not response['success']:
print(f"Error: {response['error']}")
return
session_id = response['result']['session_id']
print(f"✓ Session created: {session_id}")
# Step 2: Set a breakpoint at main function
print("\n[2] Setting breakpoint at main()...")
result = debugger.execute_command(json.dumps({
'action': 'set_breakpoint',
'params': {
'session_id': session_id,
'location': 'main'
}
}))
response = json.loads(result)
if response['success']:
print(f"✓ Breakpoint set: {response['result']}")
else:
print(f"✗ Failed to set breakpoint: {response['error']}")
# Step 3: Run the program
print("\n[3] Running program...")
result = debugger.execute_command(json.dumps({
'action': 'run',
'params': {
'session_id': session_id
}
}))
response = json.loads(result)
if response['success']:
print(f"✓ Program running, state: {response['result']['state']}")
else:
print(f"✗ Failed to run: {response['error']}")
# Step 4: Get current location
print("\n[4] Getting current location...")
result = debugger.execute_command(json.dumps({
'action': 'get_current_location',
'params': {
'session_id': session_id
}
}))
response = json.loads(result)
if response['success']:
location = response['result']
print(f"✓ Current location: {location.get('file')}:{location.get('line')}")
print(f" Function: {location.get('function')}")
# Step 5: Get local variables
print("\n[5] Getting local variables...")
result = debugger.execute_command(json.dumps({
'action': 'get_variables',
'params': {
'session_id': session_id,
'scope': 'local'
}
}))
response = json.loads(result)
if response['success']:
variables = response['result']['variables']
print(f"✓ Found {len(variables)} variables:")
for name, var in variables.items():
print(f" - {name} = {var.get('value')} ({var.get('type')})")
# Step 6: Get call stack
print("\n[6] Getting call stack...")
result = debugger.execute_command(json.dumps({
'action': 'get_backtrace',
'params': {
'session_id': session_id
}
}))
response = json.loads(result)
if response['success']:
frames = response['result']['frames']
print(f"✓ Call stack depth: {response['result']['depth']}")
for i, frame in enumerate(frames[:5]): # Show first 5 frames
print(f" #{frame['level']} {frame['func']} at {frame['file']}:{frame['line']}")
# Step 7: Step through code
print("\n[7] Stepping through code...")
for i in range(3):
result = debugger.execute_command(json.dumps({
'action': 'next',
'params': {
'session_id': session_id
}
}))
response = json.loads(result)
if response['success']:
location = response['result']['location']
print(f" Step {i+1}: {location.get('file')}:{location.get('line')}")
# Step 8: Evaluate an expression
print("\n[8] Evaluating expression...")
result = debugger.execute_command(json.dumps({
'action': 'evaluate_expression',
'params': {
'session_id': session_id,
'expression': 'argc' # Example expression
}
}))
response = json.loads(result)
if response['success']:
print(f"✓ Result: {response['result']}")
else:
print(f"✗ Evaluation failed: {response.get('error')}")
# Step 9: Continue execution
print("\n[9] Continuing execution...")
result = debugger.execute_command(json.dumps({
'action': 'continue',
'params': {
'session_id': session_id
}
}))
response = json.loads(result)
print(f"✓ State: {response['result']['state']}")
# Step 10: Close session
print("\n[10] Closing session...")
result = debugger.execute_command(json.dumps({
'action': 'close_session',
'params': {
'session_id': session_id
}
}))
response = json.loads(result)
if response['success']:
print("✓ Session closed")
except Exception as e:
print(f"\n✗ Error: {e}")
import traceback
traceback.print_exc()
finally:
# Cleanup
debugger.cleanup()
print("\n" + "=" * 60)
print("Example completed")
print("=" * 60)
if __name__ == "__main__":
main()