Misc. hardening improvements.

This commit is contained in:
Bruce Hill 2018-04-12 15:21:23 -07:00
parent 05782d52ca
commit 93a64db494
2 changed files with 52 additions and 29 deletions

43
ldt.lua
View File

@ -4,6 +4,7 @@ local line_matcher = re.compile('lines<-{| line ("\n" line)* |} line<-{[^\n]*}')
local ldb local ldb
local AUTO = { } local AUTO = { }
local PARENT = { } local PARENT = { }
local log = io.open('output.log', 'w')
local _error = error local _error = error
local _assert = assert local _assert = assert
local callstack_range local callstack_range
@ -12,18 +13,18 @@ callstack_range = function()
for i = 1, 999 do for i = 1, 999 do
local info = debug.getinfo(i, 'f') local info = debug.getinfo(i, 'f')
if not info then if not info then
min = i - 1 min = i - 0
break break
end end
if info.func == ldb.run_debugger then if info.func == ldb.run_debugger then
min = i + 2 min = i + 0
break break
end end
end end
for i = min, 999 do for i = min, 999 do
local info = debug.getinfo(i, 'f') local info = debug.getinfo(i, 'f')
if not info or info.func == ldb.guard then if not info or info.func == ldb.guard then
max = i - 3 max = i - 0
break break
end end
end end
@ -515,7 +516,13 @@ colored_repr = function(x, width, depth)
end end
return ret return ret
else else
local s = tostring(x) local ok, s = pcall(tostring, x)
if not ok then
return {
"tostring error: " .. s,
Color("red")
}
end
if #s > width then if #s > width then
return { return {
s:sub(1, width - 3), s:sub(1, width - 3),
@ -672,7 +679,11 @@ make_lines = function(location, x, width)
end)(), width) end)(), width)
if getmetatable(x).__tostring then if getmetatable(x).__tostring then
local s_lines = { } local s_lines = { }
local _list_0 = line_matcher:match(tostring(x)) local ok, s = pcall(tostring, x)
if not ok then
s = "tostring error: " .. s
end
local _list_0 = line_matcher:match(s)
for _index_0 = 1, #_list_0 do for _index_0 = 1, #_list_0 do
local line = _list_0[_index_0] local line = _list_0[_index_0]
local wrapped = wrap_text(line, width) local wrapped = wrap_text(line, width)
@ -680,7 +691,7 @@ make_lines = function(location, x, width)
table.insert(s_lines, { table.insert(s_lines, {
location = location, location = location,
subline, subline,
Color('yellow') ok and Color('yellow') or Color('red')
}) })
end end
end end
@ -1005,11 +1016,10 @@ ldb = {
for i, line in ipairs(err_msg_lines) do for i, line in ipairs(err_msg_lines) do
err_msg_lines[i] = (" "):rep(2) .. line err_msg_lines[i] = (" "):rep(2) .. line
end end
pads.err = Pad("Error Message", 0, 0, AUTO, SCREEN_W, err_msg_lines, function(self, i) local height = math.min(#err_msg_lines, 7)
pads.err = Pad("(E)rror Message", 0, 0, height, SCREEN_W, err_msg_lines, function(self, i)
return Color("red bold") return Color("red bold")
end) end)
pads.err._frame:attrset(Color("red"))
pads.err:refresh()
end end
local stack_sources = { } local stack_sources = { }
local stack_locations = { } local stack_locations = { }
@ -1161,6 +1171,7 @@ ldb = {
pads.vars = Pad("(V)ars", var_y, var_x, height, AUTO, var_names, (function(self, i) pads.vars = Pad("(V)ars", var_y, var_x, height, AUTO, var_names, (function(self, i)
return i == self.selected and Color('reverse') or Color() return i == self.selected and Color('reverse') or Color()
end)) end))
log:write("Created var pad.\n")
pads.vars.on_select = function(self, var_index) pads.vars.on_select = function(self, var_index)
if var_index == nil then if var_index == nil then
return return
@ -1168,7 +1179,7 @@ ldb = {
local value_x = pads.vars.x + pads.vars.width local value_x = pads.vars.x + pads.vars.width
local value_w = SCREEN_W - (value_x) local value_w = SCREEN_W - (value_x)
local value = stack_env[var_names[var_index]] local value = stack_env[var_names[var_index]]
local type_str = type(value) local type_str = tostring(type(value))
pads.values = DataViewer(value, "(D)ata [" .. tostring(type_str) .. "]", var_y, value_x, pads.vars.height, value_w) pads.values = DataViewer(value, "(D)ata [" .. tostring(type_str) .. "]", var_y, value_x, pads.vars.height, value_w)
collectgarbage() collectgarbage()
return collectgarbage() return collectgarbage()
@ -1195,7 +1206,7 @@ ldb = {
return selected_pad:refresh() return selected_pad:refresh()
end end
end end
select_pad(pads.src) select_pad(pads.stack)
while true do while true do
for _, p in pairs(pads) do for _, p in pairs(pads) do
p:refresh() p:refresh()
@ -1344,6 +1355,8 @@ ldb = {
select_pad(pads.vars) select_pad(pads.vars)
elseif ('d'):byte() == _exp_0 then elseif ('d'):byte() == _exp_0 then
select_pad(pads.values) select_pad(pads.values)
elseif ('e'):byte() == _exp_0 then
select_pad(pads.err)
else else
selected_pad:keypress(c) selected_pad:keypress(c)
end end
@ -1353,8 +1366,8 @@ ldb = {
guard = function(fn, ...) guard = function(fn, ...)
local handler local handler
handler = function(err_msg) handler = function(err_msg)
xpcall(ldb.run_debugger, err_hand, err_msg) print(debug.traceback(err_msg, 2))
return print(debug.traceback(err_msg, 2)) return xpcall(ldb.run_debugger, err_hand, err_msg)
end end
return xpcall(fn, handler, ...) return xpcall(fn, handler, ...)
end, end,
@ -1363,15 +1376,15 @@ ldb = {
end, end,
hijack = function() hijack = function()
error = function(err_msg) error = function(err_msg)
xpcall(ldb.run_debugger, err_hand, err_msg)
print(debug.traceback(err_msg, 2)) print(debug.traceback(err_msg, 2))
xpcall(ldb.run_debugger, err_hand, err_msg)
return os.exit(2) return os.exit(2)
end end
assert = function(condition, err_msg) assert = function(condition, err_msg)
if not condition then if not condition then
err_msg = err_msg or 'Assertion failed!' err_msg = err_msg or 'Assertion failed!'
xpcall(ldb.run_debugger, err_hand, err_msg)
print(debug.traceback(err_msg, 2)) print(debug.traceback(err_msg, 2))
xpcall(ldb.run_debugger, err_hand, err_msg)
os.exit(2) os.exit(2)
end end
return condition return condition

View File

@ -4,6 +4,9 @@ line_matcher = re.compile('lines<-{| line ("\n" line)* |} line<-{[^\n]*}')
local ldb local ldb
AUTO = {} -- Singleton AUTO = {} -- Singleton
PARENT = {} -- Singleton PARENT = {} -- Singleton
log = io.open('output.log','w')
-- TODO: add support for upvalues
_error = error _error = error
_assert = assert _assert = assert
@ -14,15 +17,15 @@ callstack_range = ->
for i=1,999 do for i=1,999 do
info = debug.getinfo(i, 'f') info = debug.getinfo(i, 'f')
if not info if not info
min = i-1 min = i-0
break break
if info.func == ldb.run_debugger if info.func == ldb.run_debugger
min = i+2 min = i+0
break break
for i=min,999 for i=min,999
info = debug.getinfo(i, 'f') info = debug.getinfo(i, 'f')
if not info or info.func == ldb.guard if not info or info.func == ldb.guard
max = i-3 max = i-0
break break
return min, max return min, max
@ -321,7 +324,9 @@ colored_repr = (x, width, depth=2)->
break break
return ret return ret
else else
s = tostring(x) ok,s = pcall(tostring,x)
if not ok
return {"tostring error: "..s, Color("red")}
return if #s > width return if #s > width
{s\sub(1,width-3), TYPE_COLORS[type(x)], '...', Color('blue')} {s\sub(1,width-3), TYPE_COLORS[type(x)], '...', Color('blue')}
else else
@ -411,10 +416,12 @@ make_lines = (location, x, width)->
lines = make_lines(location, {k,v for k,v in pairs(x)}, width) lines = make_lines(location, {k,v for k,v in pairs(x)}, width)
if getmetatable(x).__tostring if getmetatable(x).__tostring
s_lines = {} s_lines = {}
for line in *line_matcher\match(tostring(x)) ok, s = pcall(tostring, x)
if not ok then s = "tostring error: "..s
for line in *line_matcher\match(s)
wrapped = wrap_text(line, width) wrapped = wrap_text(line, width)
for i,subline in ipairs(wrapped) for i,subline in ipairs(wrapped)
table.insert(s_lines, {:location, subline, Color('yellow')}) table.insert(s_lines, {:location, subline, ok and Color('yellow') or Color('red')})
for i=1,#s_lines for i=1,#s_lines
table.insert(lines, i, s_lines[i]) table.insert(lines, i, s_lines[i])
return lines return lines
@ -649,9 +656,8 @@ ldb = {
err_msg_lines = wrap_text(err_msg, SCREEN_W - 4) err_msg_lines = wrap_text(err_msg, SCREEN_W - 4)
for i,line in ipairs(err_msg_lines) for i,line in ipairs(err_msg_lines)
err_msg_lines[i] = (" ")\rep(2)..line err_msg_lines[i] = (" ")\rep(2)..line
pads.err = Pad("Error Message", 0,0,AUTO,SCREEN_W, err_msg_lines, (i)=> Color("red bold")) height = math.min(#err_msg_lines, 7)
pads.err._frame\attrset(Color("red")) pads.err = Pad("(E)rror Message", 0,0,height,SCREEN_W, err_msg_lines, (i)=> Color("red bold"))
pads.err\refresh!
stack_sources = {} stack_sources = {}
stack_locations = {} stack_locations = {}
@ -756,13 +762,14 @@ ldb = {
--height = math.min(2+#var_names, SCREEN_H-pads.err.height-pads.stack.height) --height = math.min(2+#var_names, SCREEN_H-pads.err.height-pads.stack.height)
height = SCREEN_H-(pads.err.height+pads.stack.height) height = SCREEN_H-(pads.err.height+pads.stack.height)
pads.vars = Pad "(V)ars", var_y,var_x,height,AUTO,var_names, ((i)=> i == @selected and Color('reverse') or Color()) pads.vars = Pad "(V)ars", var_y,var_x,height,AUTO,var_names, ((i)=> i == @selected and Color('reverse') or Color())
log\write("Created var pad.\n")
pads.vars.on_select = (var_index)=> pads.vars.on_select = (var_index)=>
if var_index == nil then return if var_index == nil then return
value_x = pads.vars.x+pads.vars.width value_x = pads.vars.x+pads.vars.width
value_w = SCREEN_W-(value_x) value_w = SCREEN_W-(value_x)
value = stack_env[var_names[var_index]]--values[var_index] value = stack_env[var_names[var_index]]--values[var_index]
type_str = type(value) type_str = tostring(type(value))
-- Show single value: -- Show single value:
pads.values = DataViewer value, "(D)ata [#{type_str}]", var_y,value_x,pads.vars.height,value_w pads.values = DataViewer value, "(D)ata [#{type_str}]", var_y,value_x,pads.vars.height,value_w
collectgarbage() collectgarbage()
@ -789,7 +796,7 @@ ldb = {
selected_pad\set_active(true) selected_pad\set_active(true)
selected_pad\refresh! selected_pad\refresh!
select_pad(pads.src) select_pad(pads.stack)
while true while true
for _,p in pairs(pads) for _,p in pairs(pads)
@ -915,6 +922,9 @@ ldb = {
when ('d')\byte! when ('d')\byte!
select_pad(pads.values) -- (D)ata select_pad(pads.values) -- (D)ata
when ('e')\byte!
select_pad(pads.err) -- (E)rror
else else
selected_pad\keypress(c) selected_pad\keypress(c)
@ -922,8 +932,8 @@ ldb = {
guard: (fn, ...)-> guard: (fn, ...)->
handler = (err_msg)-> handler = (err_msg)->
xpcall(ldb.run_debugger, err_hand, err_msg)
print(debug.traceback(err_msg, 2)) print(debug.traceback(err_msg, 2))
xpcall(ldb.run_debugger, err_hand, err_msg)
return xpcall(fn, handler, ...) return xpcall(fn, handler, ...)
breakpoint: -> breakpoint: ->
@ -932,15 +942,15 @@ ldb = {
hijack: -> hijack: ->
export error, assert export error, assert
error = (err_msg)-> error = (err_msg)->
xpcall(ldb.run_debugger, err_hand, err_msg)
print(debug.traceback(err_msg, 2)) print(debug.traceback(err_msg, 2))
xpcall(ldb.run_debugger, err_hand, err_msg)
os.exit(2) os.exit(2)
assert = (condition, err_msg)-> assert = (condition, err_msg)->
if not condition if not condition
err_msg or= 'Assertion failed!' err_msg or= 'Assertion failed!'
xpcall(ldb.run_debugger, err_hand, err_msg)
print(debug.traceback(err_msg, 2)) print(debug.traceback(err_msg, 2))
xpcall(ldb.run_debugger, err_hand, err_msg)
os.exit(2) os.exit(2)
return condition return condition