From 93a64db49420e062dffec03b8b5db618abf0b8fc Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 12 Apr 2018 15:21:23 -0700 Subject: [PATCH] Misc. hardening improvements. --- ldt.lua | 43 ++++++++++++++++++++++++++++--------------- ldt.moon | 38 ++++++++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/ldt.lua b/ldt.lua index 09c48af..6802bac 100644 --- a/ldt.lua +++ b/ldt.lua @@ -4,6 +4,7 @@ local line_matcher = re.compile('lines<-{| line ("\n" line)* |} line<-{[^\n]*}') local ldb local AUTO = { } local PARENT = { } +local log = io.open('output.log', 'w') local _error = error local _assert = assert local callstack_range @@ -12,18 +13,18 @@ callstack_range = function() for i = 1, 999 do local info = debug.getinfo(i, 'f') if not info then - min = i - 1 + min = i - 0 break end if info.func == ldb.run_debugger then - min = i + 2 + min = i + 0 break end end for i = min, 999 do local info = debug.getinfo(i, 'f') if not info or info.func == ldb.guard then - max = i - 3 + max = i - 0 break end end @@ -515,7 +516,13 @@ colored_repr = function(x, width, depth) end return ret 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 return { s:sub(1, width - 3), @@ -672,7 +679,11 @@ make_lines = function(location, x, width) end)(), width) if getmetatable(x).__tostring then 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 local line = _list_0[_index_0] local wrapped = wrap_text(line, width) @@ -680,7 +691,7 @@ make_lines = function(location, x, width) table.insert(s_lines, { location = location, subline, - Color('yellow') + ok and Color('yellow') or Color('red') }) end end @@ -1005,11 +1016,10 @@ ldb = { for i, line in ipairs(err_msg_lines) do err_msg_lines[i] = (" "):rep(2) .. line 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") end) - pads.err._frame:attrset(Color("red")) - pads.err:refresh() end local stack_sources = { } local stack_locations = { } @@ -1161,6 +1171,7 @@ ldb = { 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() end)) + log:write("Created var pad.\n") pads.vars.on_select = function(self, var_index) if var_index == nil then return @@ -1168,7 +1179,7 @@ ldb = { local value_x = pads.vars.x + pads.vars.width local value_w = SCREEN_W - (value_x) 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) collectgarbage() return collectgarbage() @@ -1195,7 +1206,7 @@ ldb = { return selected_pad:refresh() end end - select_pad(pads.src) + select_pad(pads.stack) while true do for _, p in pairs(pads) do p:refresh() @@ -1344,6 +1355,8 @@ ldb = { select_pad(pads.vars) elseif ('d'):byte() == _exp_0 then select_pad(pads.values) + elseif ('e'):byte() == _exp_0 then + select_pad(pads.err) else selected_pad:keypress(c) end @@ -1353,8 +1366,8 @@ ldb = { guard = function(fn, ...) local handler handler = function(err_msg) - xpcall(ldb.run_debugger, err_hand, err_msg) - return print(debug.traceback(err_msg, 2)) + print(debug.traceback(err_msg, 2)) + return xpcall(ldb.run_debugger, err_hand, err_msg) end return xpcall(fn, handler, ...) end, @@ -1363,15 +1376,15 @@ ldb = { end, hijack = function() error = function(err_msg) - xpcall(ldb.run_debugger, err_hand, err_msg) print(debug.traceback(err_msg, 2)) + xpcall(ldb.run_debugger, err_hand, err_msg) return os.exit(2) end assert = function(condition, err_msg) if not condition then err_msg = err_msg or 'Assertion failed!' - xpcall(ldb.run_debugger, err_hand, err_msg) print(debug.traceback(err_msg, 2)) + xpcall(ldb.run_debugger, err_hand, err_msg) os.exit(2) end return condition diff --git a/ldt.moon b/ldt.moon index d57e7ea..372d9e5 100644 --- a/ldt.moon +++ b/ldt.moon @@ -4,6 +4,9 @@ line_matcher = re.compile('lines<-{| line ("\n" line)* |} line<-{[^\n]*}') local ldb AUTO = {} -- Singleton PARENT = {} -- Singleton +log = io.open('output.log','w') + +-- TODO: add support for upvalues _error = error _assert = assert @@ -14,15 +17,15 @@ callstack_range = -> for i=1,999 do info = debug.getinfo(i, 'f') if not info - min = i-1 + min = i-0 break if info.func == ldb.run_debugger - min = i+2 + min = i+0 break for i=min,999 info = debug.getinfo(i, 'f') if not info or info.func == ldb.guard - max = i-3 + max = i-0 break return min, max @@ -321,7 +324,9 @@ colored_repr = (x, width, depth=2)-> break return ret else - s = tostring(x) + ok,s = pcall(tostring,x) + if not ok + return {"tostring error: "..s, Color("red")} return if #s > width {s\sub(1,width-3), TYPE_COLORS[type(x)], '...', Color('blue')} else @@ -411,10 +416,12 @@ make_lines = (location, x, width)-> lines = make_lines(location, {k,v for k,v in pairs(x)}, width) if getmetatable(x).__tostring 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) 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 table.insert(lines, i, s_lines[i]) return lines @@ -649,9 +656,8 @@ ldb = { err_msg_lines = wrap_text(err_msg, SCREEN_W - 4) for i,line in ipairs(err_msg_lines) err_msg_lines[i] = (" ")\rep(2)..line - pads.err = Pad("Error Message", 0,0,AUTO,SCREEN_W, err_msg_lines, (i)=> Color("red bold")) - pads.err._frame\attrset(Color("red")) - pads.err\refresh! + height = math.min(#err_msg_lines, 7) + pads.err = Pad("(E)rror Message", 0,0,height,SCREEN_W, err_msg_lines, (i)=> Color("red bold")) stack_sources = {} stack_locations = {} @@ -756,13 +762,14 @@ ldb = { --height = math.min(2+#var_names, 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()) + log\write("Created var pad.\n") pads.vars.on_select = (var_index)=> if var_index == nil then return value_x = pads.vars.x+pads.vars.width value_w = SCREEN_W-(value_x) value = stack_env[var_names[var_index]]--values[var_index] - type_str = type(value) + type_str = tostring(type(value)) -- Show single value: pads.values = DataViewer value, "(D)ata [#{type_str}]", var_y,value_x,pads.vars.height,value_w collectgarbage() @@ -789,7 +796,7 @@ ldb = { selected_pad\set_active(true) selected_pad\refresh! - select_pad(pads.src) + select_pad(pads.stack) while true for _,p in pairs(pads) @@ -915,6 +922,9 @@ ldb = { when ('d')\byte! select_pad(pads.values) -- (D)ata + when ('e')\byte! + select_pad(pads.err) -- (E)rror + else selected_pad\keypress(c) @@ -922,8 +932,8 @@ ldb = { guard: (fn, ...)-> handler = (err_msg)-> - xpcall(ldb.run_debugger, err_hand, err_msg) print(debug.traceback(err_msg, 2)) + xpcall(ldb.run_debugger, err_hand, err_msg) return xpcall(fn, handler, ...) breakpoint: -> @@ -932,15 +942,15 @@ ldb = { hijack: -> export error, assert error = (err_msg)-> - xpcall(ldb.run_debugger, err_hand, err_msg) print(debug.traceback(err_msg, 2)) + xpcall(ldb.run_debugger, err_hand, err_msg) os.exit(2) assert = (condition, err_msg)-> if not condition err_msg or= 'Assertion failed!' - xpcall(ldb.run_debugger, err_hand, err_msg) print(debug.traceback(err_msg, 2)) + xpcall(ldb.run_debugger, err_hand, err_msg) os.exit(2) return condition