Compare commits
2 Commits
abedca2394
...
349cbc8175
| Author | SHA1 | Date | |
|---|---|---|---|
| 349cbc8175 | |||
| 92fd3cabf8 |
120
debugger.lua
120
debugger.lua
@ -2,72 +2,130 @@ local core = require "core"
|
||||
|
||||
---@class runner
|
||||
local runner = {
|
||||
new = function(self, o) end, ---@meta
|
||||
new = function(self, o)
|
||||
o = o or {}
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
return o
|
||||
end,
|
||||
|
||||
caps = {
|
||||
can_pause = false,
|
||||
can_continue = false,
|
||||
can_step_in = false,
|
||||
can_step_over = false,
|
||||
can_step_out = false,
|
||||
can_breakpoints = false,
|
||||
has_stack = false,
|
||||
has_locals = false,
|
||||
can_eval = false,
|
||||
},
|
||||
|
||||
run = function(self, target, name) end, ---@meta
|
||||
wait = function(sefl, time) end, ---@meta
|
||||
pause = function(self) end, --@meta
|
||||
continue = function(self) end, --@meta
|
||||
step_in = function(self) end, --@meta
|
||||
step_over = function(self) end, --@meta
|
||||
step_out = function(self) end, --@meta
|
||||
|
||||
wait = function(self, time) end, ---@meta
|
||||
kill = function(self) end, ---@meta
|
||||
terminate = function(self) end, ---@meta
|
||||
terminate = function(self) end, ---@meta
|
||||
|
||||
-- Callbacks
|
||||
log = function(msg) end, ---@meta
|
||||
error = function(msg) end, ---@meta
|
||||
on_stdout = function(msg) end, ---@meta
|
||||
on_stderr = function(msg) end, ---@meta
|
||||
on_exit = function(exitcode) end, ---@meta
|
||||
on_state = function(state) end, ---@meta
|
||||
on_break = function(file, line, reason) end, --@meta
|
||||
on_stack = function(frames) end, ---@meta
|
||||
on_locals = function(frame, vars) end, ---@meta
|
||||
on_evaluated = function(expr, ok, value) end, ---@meta
|
||||
}
|
||||
|
||||
local debugger = {}
|
||||
local debugwindow = nil ---@type JPDebugView
|
||||
local debugrunner = nil ---@type runner|nil
|
||||
local debugger = {
|
||||
runner = runner, -- Set here as member to let runners extend this base class
|
||||
debugwindow = nil, ---@type JPDebugView
|
||||
debugrunner = nil, ---@type runner|nil
|
||||
state = "idle",
|
||||
}
|
||||
|
||||
function debugger.log(msg)
|
||||
core.log("[jpdebug][debugger] %s", msg)
|
||||
if debugwindow then debugwindow:push("meta", "debugger] "..msg) end
|
||||
if debugger.debugwindow then debugger.debugwindow:push("meta", "debugger] "..msg) end
|
||||
end
|
||||
|
||||
function debugger.error(msg)
|
||||
core.error("[jpdebug][debugger]"..msg)
|
||||
if debugwindow then debugwindow:push("meta", "debugger] ERROR: "..msg) end
|
||||
if debugger.debugwindow then debugger.debugwindow:push("meta", "debugger] ERROR: "..msg) end
|
||||
end
|
||||
|
||||
function debugger.stdout(msg)
|
||||
if debugwindow then debugwindow:push("stdout", msg) end
|
||||
function debugger.on_stdout(msg)
|
||||
if debugger.debugwindow then debugger.debugwindow:push("stdout", msg) end
|
||||
end
|
||||
|
||||
function debugger.stderr(msg)
|
||||
if debugwindow then debugwindow:push("stderr", msg) end
|
||||
function debugger.on_stderr(msg)
|
||||
if debugger.debugwindow then debugger.debugwindow:push("stderr", msg) end
|
||||
end
|
||||
|
||||
function debugger.is_running()
|
||||
return debugrunner~=nil
|
||||
return debugger.debugrunner~=nil
|
||||
end
|
||||
|
||||
function debugger.on_state(state)
|
||||
debugger.log(string.format("state %s -> %s", debugger.state, state))
|
||||
debugger.state = state
|
||||
end
|
||||
|
||||
function debugger.run(target, name, r, view)
|
||||
debugwindow = view
|
||||
debugwindow:clear()
|
||||
if debugger.debugrunner then
|
||||
if debugger.state == "paused" then
|
||||
debugger.debugrunner:continue()
|
||||
else
|
||||
debugger.error("Already an active session")
|
||||
end
|
||||
return
|
||||
end
|
||||
debugger.debugwindow = view
|
||||
debugger.debugwindow:clear()
|
||||
debugger.log(string.format("Running %s", name))
|
||||
|
||||
-- Create new runner object
|
||||
debugrunner = r:new({
|
||||
debugger.debugrunner = r:new({
|
||||
-- Set callbacks
|
||||
log = debugger.log,
|
||||
error = debugger.error,
|
||||
stdout = debugger.stdout,
|
||||
stderr = debugger.stderr,
|
||||
exited = debugger.exited,
|
||||
on_stdout = debugger.on_stdout,
|
||||
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,
|
||||
})
|
||||
-- And run
|
||||
debugrunner:run(target, name)
|
||||
debugger.debugrunner:run(target, name)
|
||||
end
|
||||
|
||||
function debugger.stop()
|
||||
if debugrunner then
|
||||
debugrunner:kill()
|
||||
local exitcode = debugrunner:wait(1000)
|
||||
if debugger.debugrunner then
|
||||
debugger.debugrunner.on_exit = function() end
|
||||
debugger.debugrunner:kill()
|
||||
local exitcode = debugger.debugrunner:wait(1000)
|
||||
-- TODO terminate if needed
|
||||
debugger.log(string.format("... Stoped: %d", exitcode))
|
||||
debugger.log(string.format("... Stoped with exit code %d", exitcode))
|
||||
end
|
||||
debugrunner = nil
|
||||
debugger.debugrunner = nil
|
||||
debugger.on_state('idle')
|
||||
end
|
||||
|
||||
function debugger.exited()
|
||||
if debugrunner then
|
||||
local exitcode = debugrunner:wait(process.WAIT_INFINITE)
|
||||
debugger.log(string.format("exit: %d", exitcode))
|
||||
end
|
||||
debugrunner = nil
|
||||
function debugger.on_exit(exitcode)
|
||||
debugger.log(string.format("exit: %d", exitcode))
|
||||
debugger.debugrunner = nil
|
||||
debugger.on_state('idle')
|
||||
end
|
||||
|
||||
return debugger
|
||||
|
||||
5
init.lua
5
init.lua
@ -233,6 +233,11 @@ if required_toolbar_plugins and ToolbarView then
|
||||
else
|
||||
table.insert(t, {symbol = "D", command = "jpdebug:stop"})
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
@ -1,115 +0,0 @@
|
||||
local core = require "core"
|
||||
local process = require "process"
|
||||
|
||||
-- tiny helpers
|
||||
local function dirname(p) return p:match("^(.*)[/\\]") or "" end
|
||||
local function join(a, b) return (a:sub(-1) == "/" and a or (a .. "/")) .. b end
|
||||
|
||||
-- returns absolute path to the plugin root (…/jpdebug)
|
||||
local function get_plugin_root()
|
||||
-- debug.getinfo(1, "S").source gives "@/full/path/to/this/file.lua"
|
||||
local src = debug.getinfo(1, "S").source
|
||||
if src:sub(1, 1) == "@" then src = src:sub(2) end
|
||||
local here = dirname(src) -- …/jpdebug/runners
|
||||
return dirname(here) -- …/jpdebug
|
||||
end
|
||||
|
||||
-- Main lua process spawner
|
||||
local function spawn_lua_process(entry, lua, cwd, env)
|
||||
local host = "localhost"
|
||||
local port = 8172
|
||||
|
||||
-- Resolve vendor/?.lua so "require('mobdebug')" finds the bundled file
|
||||
local vendor_glob = join(get_plugin_root(), "vendor/?.lua")
|
||||
|
||||
-- Build the inline lua launcher
|
||||
local dbg_call = string.format([[
|
||||
local ok, m = pcall(require, "mobdebug"); if ok then
|
||||
print("Connecting to "..%q..":"..tostring(%d))
|
||||
local connected = m.start(%q, %d)
|
||||
end
|
||||
]], host, port, host, port)
|
||||
local launcher = string.format([[
|
||||
package.path = %q .. ";" .. package.path
|
||||
%s
|
||||
dofile(%q)
|
||||
]], vendor_glob, dbg_call, entry)
|
||||
|
||||
-- Spawn the process
|
||||
local cmd = {}
|
||||
if type(lua) == "table" then
|
||||
for i=1, #lua do table.insert(cmd, lua[i]) end
|
||||
else
|
||||
table.insert(cmd, lua)
|
||||
end
|
||||
table.insert(cmd, "-e")
|
||||
table.insert(cmd, launcher)
|
||||
local proc = process.start(cmd, {
|
||||
cwd = cwd,
|
||||
env = env,
|
||||
stdout = process.REDIRECT_PIPE,
|
||||
stderr = process.REDIRECT_PIPE,
|
||||
})
|
||||
return proc
|
||||
end
|
||||
|
||||
---@class LDB
|
||||
local LDB = {
|
||||
name = "luadebug"
|
||||
}
|
||||
|
||||
---@param target table Target table
|
||||
---@param name string Name of the target to run
|
||||
---@praam debuginfo table Debugging information
|
||||
---@return process|nil
|
||||
function LDB:run(target, name, debuginfo)
|
||||
if target.entry == nil then
|
||||
core.error("[jpdebug][luadebug] target.entry is required")
|
||||
return
|
||||
end
|
||||
local entry = target.entry
|
||||
local lua = target.lua or "lua"
|
||||
local cwd = target.cwd or "."
|
||||
local env = target.env or {}
|
||||
|
||||
-- spawn the main lua process
|
||||
local proc = spawn_lua_process(entry, lua, cwd, env)
|
||||
|
||||
if proc == nil then
|
||||
core.error("[jpdebug][luadebug] Failed to start "..entry)
|
||||
return nil
|
||||
end
|
||||
|
||||
return {
|
||||
luaproc=proc
|
||||
}
|
||||
end
|
||||
|
||||
-- Wait untill it ends, possibly with timeout
|
||||
function LDB:wait(proc, time)
|
||||
return proc.luaproc:wait(time)
|
||||
end
|
||||
|
||||
-- Read the stdout, returns nil if process has stopped
|
||||
function LDB:read_stdout(proc)
|
||||
local sl = proc.luaproc:read_stdout()
|
||||
return sl
|
||||
end
|
||||
|
||||
-- Read the stderr
|
||||
function LDB:read_stderr(proc)
|
||||
local sl = proc.luaproc:read_stderr()
|
||||
return sl
|
||||
end
|
||||
|
||||
-- Kill the process
|
||||
function LDB:kill(proc)
|
||||
proc.luaproc:kill()
|
||||
end
|
||||
|
||||
-- Terminate the process
|
||||
function LDB:terminate(proc)
|
||||
proc.luaproc:terminate()
|
||||
end
|
||||
|
||||
return LDB
|
||||
157
runners/luadebug/init.lua
Normal file
157
runners/luadebug/init.lua
Normal file
@ -0,0 +1,157 @@
|
||||
local core = require "core"
|
||||
local process = require "process"
|
||||
local debugger = require "plugins.jpdebug.debugger"
|
||||
|
||||
-- Enable hot reloading of mdb
|
||||
package.loaded["plugins.jpdebug.runners.luadebug.mdb"] = nil
|
||||
local mdb = require "plugins.jpdebug.runners.luadebug.mdb"
|
||||
|
||||
---@class luadebug: runner
|
||||
local luadebug = debugger.runner:new({
|
||||
name = "luadebug",
|
||||
proc = nil ---@type process|nil
|
||||
})
|
||||
|
||||
-- tiny helpers
|
||||
local function dirname(p) return p:match("^(.*)[/\\]") or "" end
|
||||
local function join(a, b) return (a:sub(-1) == "/" and a or (a .. "/")) .. b end
|
||||
|
||||
-- returns absolute path to the plugin root (…/jpdebug)
|
||||
local function get_plugin_root()
|
||||
-- debug.getinfo(1, "S").source gives "@/full/path/to/this/file.lua"
|
||||
local src = debug.getinfo(1, "S").source
|
||||
if src:sub(1, 1) == "@" then src = src:sub(2) end
|
||||
local here = dirname(src) -- …/jpdebug/runners
|
||||
return dirname(here) -- …/jpdebug
|
||||
end
|
||||
|
||||
-- Main lua process spawner
|
||||
local function spawn_lua_process(entry, lua, cwd, env)
|
||||
local host = "localhost"
|
||||
local port = 8172
|
||||
|
||||
-- Resolve vendor/?.lua so "require('mobdebug')" finds the bundled file
|
||||
local vendor_glob = join(get_plugin_root(), "vendor/?.lua")
|
||||
|
||||
-- Build the inline lua launcher
|
||||
local dbg_call = string.format([[
|
||||
local ok, m = pcall(require, "mobdebug"); if ok then
|
||||
print("Connecting to "..%q..":"..tostring(%d))
|
||||
local connected = m.start(%q, %d)
|
||||
end
|
||||
]], host, port, host, port)
|
||||
local launcher = string.format([[
|
||||
package.path = %q .. ";" .. package.path
|
||||
%s
|
||||
dofile(%q)
|
||||
]], vendor_glob, dbg_call, entry)
|
||||
|
||||
-- Spawn the process
|
||||
local cmd = {}
|
||||
if type(lua) == "table" then
|
||||
for i=1, #lua do table.insert(cmd, lua[i]) end
|
||||
else
|
||||
table.insert(cmd, lua)
|
||||
end
|
||||
table.insert(cmd, "-e")
|
||||
table.insert(cmd, launcher)
|
||||
local proc = process.start(cmd, {
|
||||
cwd = cwd,
|
||||
env = env,
|
||||
stdout = process.REDIRECT_PIPE,
|
||||
stderr = process.REDIRECT_PIPE,
|
||||
})
|
||||
return proc
|
||||
end
|
||||
|
||||
function luadebug:run(target, name)
|
||||
if target.entry == nil then
|
||||
self.error("target.entry is required for "..name)
|
||||
return
|
||||
end
|
||||
local entry = target.entry
|
||||
local lua = target.lua or "lua"
|
||||
local cwd = target.cwd or "."
|
||||
local env = target.env or {}
|
||||
|
||||
-- spwawn the mobdebug process
|
||||
self.mdb = mdb:new({
|
||||
log = self.log,
|
||||
error = self.error,
|
||||
|
||||
on_connected = function()
|
||||
self.on_state('running')
|
||||
self.mdb:setb('test.lua', 6)
|
||||
self.mdb:run()
|
||||
end,
|
||||
on_pause = function(file, line)
|
||||
self.on_state('paused')
|
||||
self.on_break(file, line, "breakpoint")
|
||||
end,
|
||||
|
||||
})
|
||||
self.mdb:spawn(lua, get_plugin_root())
|
||||
|
||||
-- spawn the main lua process
|
||||
local proc = spawn_lua_process(entry, lua, cwd, env)
|
||||
if proc == nil then
|
||||
self.error("Failed to start "..entry)
|
||||
return
|
||||
end
|
||||
self.proc = proc
|
||||
|
||||
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 and serr == nil then
|
||||
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)
|
||||
end
|
||||
|
||||
function luadebug:continue()
|
||||
if not self.proc then return end
|
||||
self.mdb:run()
|
||||
end
|
||||
|
||||
function luadebug:wait(time)
|
||||
if not self.proc then return end
|
||||
return self.proc:wait(time)
|
||||
end
|
||||
|
||||
function luadebug:read_stdout()
|
||||
if not self.proc then return end
|
||||
local sl = self.proc:read_stdout()
|
||||
return sl
|
||||
end
|
||||
|
||||
function luadebug:read_stderr()
|
||||
if not self.proc then return end
|
||||
local sl = self.proc:read_stderr()
|
||||
return sl
|
||||
end
|
||||
|
||||
function luadebug:kill()
|
||||
if self.mdb then self.mdb:kill() end
|
||||
if not self.proc then return end
|
||||
self.proc:kill()
|
||||
end
|
||||
|
||||
function luadebug:terminate()
|
||||
if not self.proc then return end
|
||||
self.proc:terminate()
|
||||
end
|
||||
|
||||
return luadebug
|
||||
|
||||
120
runners/luadebug/mdb.lua
Normal file
120
runners/luadebug/mdb.lua
Normal file
@ -0,0 +1,120 @@
|
||||
local core = require "core"
|
||||
local process = require "process"
|
||||
|
||||
-- tiny helpers
|
||||
local function join(a, b) return (a:sub(-1) == "/" and a or (a .. "/")) .. b end
|
||||
|
||||
---@class mdb
|
||||
local mdb = {
|
||||
proc = nil, ---@type process|nil
|
||||
state = 'idle',
|
||||
}
|
||||
|
||||
---@meta
|
||||
function mdb.log(msg) end
|
||||
---@meta
|
||||
function mdb.error(msg) end
|
||||
---@meta
|
||||
function mdb.on_connected() end
|
||||
---@meta
|
||||
function mdb.on_pause(file, line) end
|
||||
|
||||
function mdb:new(o)
|
||||
o = o or {}
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
return o
|
||||
end
|
||||
|
||||
function mdb:set_state(newstate)
|
||||
self.state = newstate
|
||||
end
|
||||
|
||||
function mdb:parse_line(line)
|
||||
self.log('mdb> '..line)
|
||||
local words = {}
|
||||
for w in line:gmatch("%w+") do table.insert(words, w) end
|
||||
|
||||
if words[1] == 'Paused' then
|
||||
-- Breakpoint hit
|
||||
if #words ~= 7 then return end
|
||||
local file = words[4]
|
||||
local linenr = tonumber(words[7])
|
||||
self:set_state("paused")
|
||||
core.add_thread(function() self.on_pause(file, linenr) end)
|
||||
elseif words[1] == "Type" then
|
||||
-- mobdebug started
|
||||
self:set_state("paused")
|
||||
core.add_thread(function() self.on_connected() end)
|
||||
end
|
||||
end
|
||||
|
||||
function mdb:spawn(lua, root)
|
||||
local vendor_glob = join(root, "vendor/?.lua")
|
||||
|
||||
local cmd = {}
|
||||
if type(lua) == "table" then
|
||||
for i=1, #lua do table.insert(cmd, lua[i]) end
|
||||
else
|
||||
table.insert(cmd, lua)
|
||||
end
|
||||
table.insert(cmd, "-e")
|
||||
table.insert(cmd, string.format([[
|
||||
package.path = %q .. ";" .. package.path
|
||||
require("mobdebug").listen()
|
||||
]], vendor_glob))
|
||||
|
||||
self:set_state('spawning')
|
||||
|
||||
local proc = process.start(cmd, {
|
||||
stdout = process.REDIRECT_PIPE,
|
||||
stderr = process.REDIRECT_PIPE,
|
||||
})
|
||||
self.proc = proc
|
||||
|
||||
self:set_state('connecting')
|
||||
|
||||
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 and serr == nil then
|
||||
-- mdb exited
|
||||
break
|
||||
end
|
||||
if sout and sout~="" then
|
||||
for l in string.gmatch(sout, "([^\n]+)") do
|
||||
self:parse_line(l)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function mdb:cmd(c)
|
||||
-- commands are only possible when mdb is paused
|
||||
if self.state == "paused" then
|
||||
self.log('>>>'..c)
|
||||
self.proc:write(c..'\n')
|
||||
else
|
||||
self.error('mdb not paused')
|
||||
end
|
||||
end
|
||||
|
||||
function mdb:run()
|
||||
self:cmd("run")
|
||||
end
|
||||
|
||||
function mdb:setb(file, line)
|
||||
self:cmd(string.format("setb %s %d", file, line))
|
||||
end
|
||||
|
||||
function mdb:kill()
|
||||
self.proc:kill()
|
||||
self.proc:kill()
|
||||
end
|
||||
|
||||
return mdb
|
||||
|
||||
@ -1,30 +1,12 @@
|
||||
local core = require "core"
|
||||
local process = require "process"
|
||||
local debugger = require "plugins.jpdebug.debugger"
|
||||
|
||||
---@class shell: runner
|
||||
local shell = {
|
||||
local shell = debugger.runner:new({
|
||||
name = "shell",
|
||||
proc = nil ---@type process|nil
|
||||
}
|
||||
|
||||
function shell:new(o)
|
||||
o = o or {}
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
return o
|
||||
end
|
||||
|
||||
---@meta
|
||||
function shell.log(msg) end
|
||||
---@meta
|
||||
function shell.error(msg) end
|
||||
---@meta
|
||||
function shell.stdout(msg) end
|
||||
---@meta
|
||||
function shell.stderr(msg) end
|
||||
---@meta
|
||||
function shell.exited() end
|
||||
|
||||
})
|
||||
|
||||
function shell:run(target, name)
|
||||
local opts = {
|
||||
@ -43,6 +25,8 @@ function shell:run(target, name)
|
||||
self.error(string.format("command not specified for target %s", name))
|
||||
end
|
||||
|
||||
self.on_state('running')
|
||||
|
||||
-- output pump
|
||||
core.add_thread(function()
|
||||
while true do
|
||||
@ -51,11 +35,12 @@ function shell:run(target, name)
|
||||
local sout = self.proc:read_stdout()
|
||||
local serr = self.proc:read_stderr()
|
||||
if sout == nil and serr == nil then
|
||||
self.exited()
|
||||
local exitcode = self.proc:wait(process.WAIT_INFINITE)
|
||||
self.on_exit(exitcode)
|
||||
break
|
||||
end
|
||||
if sout and sout~="" then self.stdout(sout) end
|
||||
if serr and serr~="" then self.stderr(serr) end
|
||||
if sout and sout~="" then self.on_stdout(sout) end
|
||||
if serr and serr~="" then self.on_stderr(serr) end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user