diff --git a/debugger.lua b/debugger.lua index de84fe3..2443569 100644 --- a/debugger.lua +++ b/debugger.lua @@ -79,6 +79,16 @@ function debugger.on_state(state) debugger.state = state end +function debugger.on_break(file, line, reason) + debugger.log(string.format("breakpoint hit: %s:%d", file, line)) + -- Temporary continue at unknown breaks + if file=='?' and line==-1 then + if debugger.debugrunner.caps.can_continue then + debugger.debugrunner:continue() + end + end +end + function debugger.run(target, name, r, view) if debugger.debugrunner then if debugger.state == "paused" then @@ -101,10 +111,7 @@ function debugger.run(target, name, r, view) on_stderr = debugger.on_stderr, on_exit = debugger.on_exit, on_state = debugger.on_state, - - on_break = function(file, line, reason) - debugger.log(string.format("breakpoint hit: %s:%d", file, line)) - end, + on_break = debugger.on_break, }) -- And run debugger.debugrunner:run(target, name) diff --git a/init.lua b/init.lua index efbc1dd..e07c1ef 100644 --- a/init.lua +++ b/init.lua @@ -248,7 +248,7 @@ if required_toolbar_plugins and ToolbarView then if debugger.is_running() then if debugger.debugrunner.caps.can_pause then table.insert(t, {symbol="C",command=""}) end end - + table.insert(t, {symbol = "E", command = "jpdebug:reload-runners"}) self.toolbar_commands = t end diff --git a/runners/luadebug.lua b/runners/luadebug.lua index b6d1450..4d84b40 100644 --- a/runners/luadebug.lua +++ b/runners/luadebug.lua @@ -15,12 +15,29 @@ local function get_plugin_root() return dirname(here) -- …/jpdebug end +-- Check if string starts with a substring +local function starts_with(str, word) + return str:sub(1, #word) == word +end + -- ------------------- Runner API -------------------------------- ---@class luadebug: runner local luadebug = debugger.runner:new({ name = "luadebug", - proc = nil ---@type process|nil + proc = nil, ---@type process|nil + + caps = { + can_pause = false, + can_continue = true, + can_step_in = true, + can_step_over = true, + can_step_out = true, + can_breakpoints = true, + has_stack = true, + has_locals = true, + can_eval = false, + }, }) function luadebug:run(target, name) @@ -48,24 +65,13 @@ function luadebug:run(target, name) self.on_state('connecting') - -- output pump - core.add_thread(function() - while true do - core.redraw = true - coroutine.yield(0.016) -- 60FPS - local sout = self.proc:read_stdout() - local serr = self.proc:read_stderr() - if sout == nil or serr == nil then - -- Make sure to read stderr for the last time - if serr and serr~="" then self.on_stderr(serr) end - local exitcode = self.proc:wait(process.WAIT_INFINITE) - self.on_exit(exitcode) - break - end - if sout and sout~="" then self.on_stdout(sout) end - if serr and serr~="" then self.on_stderr(serr) end - end - end) + -- Start output pump + self:pumps() +end + +function luadebug:continue() + self.mdb:write('run\n') + self.on_state('running') end function luadebug:wait(time) @@ -171,28 +177,57 @@ function luadebug:spawn_mdb(lua) end self.mdb = proc +end - self.mdb:write('run\n') - - -- output pump +-- ---------------- Pumps --------------------------------------- +function luadebug:pumps() core.add_thread(function() while true do core.redraw = true coroutine.yield(0.016) -- 60FPS - local sout = self.mdb:read_stdout() - local serr = self.mdb:read_stderr() - if sout == nil or serr == nil then - -- Make sure to read stderr for the last time + + -- LUA PROGRAM OUTPUT + if true then + local sout = self.proc:read_stdout() + local serr = self.proc:read_stderr() + + if sout == nil or serr == nil then + -- Make sure to read stderr for the last time + if serr and serr~="" then self.on_stderr(serr) end + local exitcode = self.proc:wait(process.WAIT_INFINITE) + self.on_exit(exitcode) + break + end + if sout and sout~="" then self.on_stdout(sout) end if serr and serr~="" then self.on_stderr(serr) end - local exitcode = self.mdb:wait(process.WAIT_INFINITE) - self.on_exit(exitcode) - break end - if sout and sout~="" then self.on_stdout('mdb> '..sout) end - if serr and serr~="" then self.on_stderr('mdb> '..serr) end + + -- MDB OUTPUT + if true then + local sout = self.mdb:read_stdout() + local serr = self.mdb:read_stderr() + + if sout == nil or serr == nil then + -- Make sure to read stderr for the last time + if serr and serr~="" then self.on_stderr(serr) end + local exitcode = self.proc:wait(process.WAIT_INFINITE) + self.on_exit(exitcode) + break + end + if sout and sout~="" then self.on_stdout("mdb> "..sout) end + if serr and serr~="" then self.on_stderr("mdb> "..serr) end + + if sout and sout~="" then + -- Check output + if starts_with(sout, "Paused") then + self.on_state('paused') + self.on_break('?', -1, 'paused') + end + end + end end end) end -return luadebug +return luadebug