This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontroller.lua
More file actions
232 lines (224 loc) · 7.72 KB
/
controller.lua
File metadata and controls
232 lines (224 loc) · 7.72 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
--
-- RemDebug 1.0 Beta
-- Copyright Kepler Project 2005 (http://www.keplerproject.org/remdebug)
--
local socket = require"socket"
print("Lua Remote Debugger")
print("Run the program you wish to debug")
local server = socket.bind("*", 8171)
local client = server:accept()
local breakpoints = {}
local watches = {}
client:send("STEP\n")
client:receive()
local breakpoint = client:receive()
local _, _, file, line = string.find(breakpoint, "^202 Paused%s+([%w%p]+)%s+(%d+)$")
if file and line then
print("Paused at file " .. file )
print("Type 'help' for commands")
else
local _, _, size = string.find(breakpoint, "^401 Error in Execution (%d+)$")
if size then
print("Error in remote application: ")
print(client:receive(size))
end
end
local basedir = ""
while true do
io.write("> ")
local line = io.read("*line")
local _, _, command = string.find(line, "^([a-z]+)")
if command == "run" or command == "step" or command == "over" then
client:send(string.upper(command) .. "\n")
client:receive()
local breakpoint = client:receive()
if not breakpoint then
print("Program finished")
os.exit()
end
local _, _, status = string.find(breakpoint, "^(%d+)")
if status == "202" then
local _, _, file, line = string.find(breakpoint, "^202 Paused%s+([%w%p]+)%s+(%d+)$")
if file and line then
print("Paused at file " .. file .. " line " .. line)
end
elseif status == "203" then
local _, _, file, line, watch_idx = string.find(breakpoint, "^203 Paused%s+([%w%p]+)%s+(%d+)%s+(%d+)$")
if file and line and watch_idx then
print("Paused at file " .. file .. " line " .. line .. " (watch expression " .. watch_idx .. ": [" .. watches[watch_idx] .. "])")
end
elseif status == "401" then
local _, _, size = string.find(breakpoint, "^401 Error in Execution (%d+)$")
if size then
print("Error in remote application: ")
print(client:receive(tonumber(size)))
os.exit()
end
else
print("Unknown error")
os.exit()
end
elseif command == "exit" then
client:close()
os.exit()
elseif command == "setb" then
local _, _, _, filename, line = string.find(line, "^([a-z]+)%s+([%w%p]+)%s+(%d+)$")
if filename and line then
filename = basedir .. filename
if not breakpoints[filename] then breakpoints[filename] = {} end
client:send("SETB " .. filename .. " " .. line .. "\n")
if client:receive() == "200 OK" then
breakpoints[filename][line] = true
else
print("Error: breakpoint not inserted")
end
else
print("Invalid command")
end
elseif command == "setw" then
local _, _, exp = string.find(line, "^[a-z]+%s+(.+)$")
if exp then
client:send("SETW " .. exp .. "\n")
local answer = client:receive()
local _, _, watch_idx = string.find(answer, "^200 OK (%d+)$")
if watch_idx then
watches[watch_idx] = exp
print("Inserted watch exp no. " .. watch_idx)
else
print("Error: Watch expression not inserted")
end
else
print("Invalid command")
end
elseif command == "delb" then
local _, _, _, filename, line = string.find(line, "^([a-z]+)%s+([%w%p]+)%s+(%d+)$")
if filename and line then
filename = basedir .. filename
if not breakpoints[filename] then breakpoints[filename] = {} end
client:send("DELB " .. filename .. " " .. line .. "\n")
if client:receive() == "200 OK" then
breakpoints[filename][line] = nil
else
print("Error: breakpoint not removed")
end
else
print("Invalid command")
end
elseif command == "delallb" then
for filename, breaks in pairs(breakpoints) do
for line, _ in pairs(breaks) do
client:send("DELB " .. filename .. " " .. line .. "\n")
if client:receive() == "200 OK" then
breakpoints[filename][line] = nil
else
print("Error: breakpoint at file " .. filename .. " line " .. line .. " not removed")
end
end
end
elseif command == "delw" then
local _, _, index = string.find(line, "^[a-z]+%s+(%d+)$")
if index then
client:send("DELW " .. index .. "\n")
if client:receive() == "200 OK" then
watches[index] = nil
else
print("Error: watch expression not removed")
end
else
print("Invalid command")
end
elseif command == "delallw" then
for index, exp in pairs(watches) do
client:send("DELW " .. index .. "\n")
if client:receive() == "200 OK" then
watches[index] = nil
else
print("Error: watch expression at index " .. index .. " [" .. exp .. "] not removed")
end
end
elseif command == "eval" then
local _, _, exp = string.find(line, "^[a-z]+%s+(.+)$")
if exp then
client:send("EXEC return (" .. exp .. ")\n")
local line = client:receive()
local _, _, status, len = string.find(line, "^(%d+)[a-zA-Z ]+(%d+)$")
if status == "200" then
len = tonumber(len)
local res = client:receive(len)
print(res)
elseif status == "401" then
len = tonumber(len)
local res = client:receive(len)
print("Error in expression:")
print(res)
else
print("Unknown error")
end
else
print("Invalid command")
end
elseif command == "exec" then
local _, _, exp = string.find(line, "^[a-z]+%s+(.+)$")
if exp then
client:send("EXEC " .. exp .. "\n")
local line = client:receive()
local _, _, status, len = string.find(line, "^(%d+)[%s%w]+(%d+)$")
if status == "200" then
len = tonumber(len)
local res = client:receive(len)
print(res)
elseif status == "401" then
len = tonumber(len)
local res = client:receive(len)
print("Error in expression:")
print(res)
else
print("Unknown error")
end
else
print("Invalid command")
end
elseif command == "listb" then
for k, v in pairs(breakpoints) do
io.write(k .. ": ")
for k, v in pairs(v) do
io.write(k .. " ")
end
io.write("\n")
end
elseif command == "listw" then
for i, v in pairs(watches) do
print("Watch exp. " .. i .. ": " .. v)
end
elseif command == "basedir" then
local _, _, dir = string.find(line, "^[a-z]+%s+(.+)$")
if dir then
if not string.find(dir, "/$") then dir = dir .. "/" end
basedir = dir
print("New base directory is " .. basedir)
else
print(basedir)
end
elseif command == "help" then
print("setb <file> <line> -- sets a breakpoint")
print("delb <file> <line> -- removes a breakpoint")
print("delallb -- removes all breakpoints")
print("setw <exp> -- adds a new watch expression")
print("delw <index> -- removes the watch expression at index")
print("delallw -- removes all watch expressions")
print("run -- run until next breakpoint")
print("step -- run until next line, stepping into function calls")
print("over -- run until next line, stepping over function calls")
print("listb -- lists breakpoints")
print("listw -- lists watch expressions")
print("eval <exp> -- evaluates expression on the current context and returns its value")
print("exec <stmt> -- executes statement on the current context")
print("basedir [<path>] -- sets the base path of the remote application, or shows the current one")
print("exit -- exits debugger")
else
local _, _, spaces = string.find(line, "^(%s*)$")
if not spaces then
print("Invalid command")
end
end
end