-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger.lua
298 lines (263 loc) · 9.05 KB
/
debugger.lua
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
local term = {}
function term.write(text)
io.write(text)
end
function term.read()
return io.read()
end
local fs = {}
function fs.exists(path)
return io.open(path, "r") ~= nil
end
local bit32 = {}
local Debugger = {}
Debugger.__index = Debugger
function Debugger.new(machine)
local self = setmetatable({}, Debugger)
self.machine = machine
self.breakpoints = {}
self.history = {}
self.historySize = 100
self.tuiMode = false
self.tui = nil
return self
end
function Debugger:help()
term.write([[
Debugger commands:
h, help - Show this help
s, step - Execute next instruction
c, continue - Continue execution until next breakpoint
b <line> - Set breakpoint at line number
rb <line> - Remove breakpoint at line number
lb - List all breakpoints
p <reg> - Print register value
pm <addr> - Print memory at address
r - Print all registers
m - Print memory dump
stack - Print stack dump
fill <addr> <value> <count> - Fill memory with value
copy <src> <dest> <count> - Copy memory from src to dest
q, quit - Quit debugger
tui start - Start TUI mode
tui stop - Stop TUI mode
]])
end
function Debugger:printRegisters()
term.write("\nRegister values:\n")
term.write(string.format("RAX: %-8d RBX: %-8d RCX: %-8d RDX: %-8d\n",
self.machine.RAX, self.machine.RBX, self.machine.RCX, self.machine.RDX))
term.write(string.format("RSI: %-8d RDI: %-8d RBP: %-8d RSP: %-8d\n",
self.machine.RSI, self.machine.RDI, self.machine.RBP, self.machine.RSP))
term.write(string.format("RIP: %-8d FLAGS: %d\n",
self.machine.RIP, self.machine.RFLAGS))
end
function Debugger:printMemory(start, count)
start = tonumber(start) or 0
count = tonumber(count) or 16
term.write("\nMemory dump:\n")
for i = start, start + count - 1 do
if i % 4 == 0 then term.write("\n") end
term.write(string.format("%04d: %-6d ", i, self.machine.memory[i] or 0))
end
term.write("\n")
end
function Debugger:printStack()
term.write("\nStack dump:\n")
local stackBase = self.machine.RSP
local count = 16 -- Show 16 stack items
for i = 0, count-1 do
local addr = stackBase - i - 1
if addr >= 0 then
term.write(string.format("%04d: %-6d\n", addr, self.machine.memory[addr] or 0))
end
end
end
function Debugger:setBreakpoint(line)
local lineNum = tonumber(line)
if lineNum and lineNum > 0 and lineNum <= #self.machine.currentCode then
self.breakpoints[lineNum] = true
term.write("Breakpoint set at line " .. lineNum .. "\n")
else
term.write("Invalid line number\n")
end
end
function Debugger:removeBreakpoint(line)
local lineNum = tonumber(line)
if lineNum and self.breakpoints[lineNum] then
self.breakpoints[lineNum] = nil
term.write("Breakpoint removed from line " .. lineNum .. "\n")
else
term.write("No breakpoint at line " .. (lineNum or "nil") .. "\n")
end
end
function Debugger:listBreakpoints()
term.write("\nBreakpoints:\n")
local found = false
for line, _ in pairs(self.breakpoints) do
term.write("Line " .. line .. "\n")
found = true
end
if not found then
term.write("No breakpoints set\n")
end
end
function Debugger:shouldBreak()
return self.breakpoints[self.machine.RIP + 1]
end
function Debugger:processCommand(cmd, arg)
if not cmd then return true end
-- Trim whitespace and convert to lowercase
cmd = cmd:match("^%s*(.-)%s*$"):lower()
arg = arg and arg:match("^%s*(.-)%s*$")
-- Handle TUI commands first
if cmd == "tui" then
if arg == "start" and not self.tuiMode then
local ok, TUI = pcall(require, "tui")
if not ok then
term.write("Error loading TUI module: " .. tostring(TUI) .. "\n")
term.write("Current package.path: " .. package.path .. "\n")
return true
end
self.tui = TUI.new(self.machine, self)
self.tuiMode = true
self.tui:handleInput()
return true
elseif arg == "ansi" and not self.tuiMode then
-- Enable ANSI mode and start TUI
local ok, TUI = pcall(require, "tui")
if not ok then
term.write("Error loading TUI module: " .. tostring(TUI) .. "\n")
return true
end
_G.ANSI_MODE = true -- Enable ANSI mode globally
self.tui = TUI.new(self.machine, self)
self.tuiMode = true
self.tui:handleInput()
return true
elseif arg == "text" and not self.tuiMode then
-- Ensure text mode and start TUI
local ok, TUI = pcall(require, "tui")
if not ok then
term.write("Error loading TUI module: " .. tostring(TUI) .. "\n")
return true
end
_G.ANSI_MODE = false -- Disable ANSI mode globally
self.tui = TUI.new(self.machine, self)
self.tuiMode = true
self.tui:handleInput()
return true
elseif arg == "stop" and self.tuiMode then
if self.tui then
self.tui:clear()
end
self.tuiMode = false
return true
end
end
if cmd == "h" or cmd == "help" then
self:help()
elseif cmd == "s" or cmd == "step" then
local line = self.machine.currentCode[self.machine.RIP + 1]
term.write("Executing: " .. (line or "end of program") .. "\n")
if not self.machine:executeInstruction(line) then
term.write("Program finished\n")
return false
end
if not self.tuiMode then
self:printRegisters()
end
elseif cmd == "c" or cmd == "continue" then
while self.machine.RIP < #self.machine.currentCode do
if self:shouldBreak() then
term.write("Breakpoint hit at line " .. (self.machine.RIP+1) .. "\n")
break
end
local line = self.machine.currentCode[self.machine.RIP + 1]
if not self.machine:executeInstruction(line) then
term.write("Program finished\n")
break
end
end
if not self.tuiMode then
self:printRegisters()
end
elseif cmd == "b" then
self:setBreakpoint(arg)
elseif cmd == "rb" then
self:removeBreakpoint(arg)
elseif cmd == "lb" then
self:listBreakpoints()
elseif cmd == "p" then
local value = self.machine[arg]
term.write(string.format("%s = %s\n", arg, value or "nil"))
elseif cmd == "pm" then
local addr = tonumber(arg) or 0
self:printMemory(addr, 16)
elseif cmd == "r" then
self:printRegisters()
elseif cmd == "m" then
self:printMemory()
elseif cmd == "stack" then
self:printStack()
elseif cmd == "fill" then
local args = {}
for word in string.gmatch(arg, "%S+") do
table.insert(args, word)
end
if #args >= 3 then
self.machine:fill(args[1], args[2], args[3])
term.write("Memory filled\n")
else
term.write("Usage: fill <addr> <value> <count>\n")
end
elseif cmd == "copy" then
local args = {}
for word in string.gmatch(arg, "%S+") do
table.insert(args, word)
end
if #args >= 3 then
self.machine:copy(args[2], args[1], args[3])
term.write("Memory copied\n")
else
term.write("Usage: copy <src> <dest> <count>\n")
end
elseif cmd == "q" or cmd == "quit" then
term.write("Debugger terminated\n")
return false
else
term.write("Unknown command '" .. cmd .. "'. Type 'h' for help\n")
end
return true
end
function Debugger:start()
-- Auto-start TUI if machine is in debug mode and autoTUI is enabled
if self.machine.debugMode and self.machine.autoTUI then
local ok, TUI = pcall(require, "tui")
if ok then
self.tui = TUI.new(self.machine, self)
self.tuiMode = true
self.tui:handleInput()
return
else
term.write("Warning: Could not load TUI module, falling back to CLI mode\n")
term.write("Error: " .. tostring(TUI) .. "\n")
end
end
term.write("Debugger started. Type 'h' for help.\n")
while true do
term.write("debug> ")
local input = term.read()
if not input then break end
local cmd, arg = input:match("^(%S+)%s*(.*)$")
if cmd then
if self:processCommand(cmd, arg) == false then
break
end
if self.tuiMode and self.tui then
self.tui:handleInput()
end
end
end
end
return Debugger