add debug command inject

This commit is contained in:
Cloud Wu
2014-08-13 21:02:56 +08:00
parent 809e6c3a89
commit 6bf8dd9a31
5 changed files with 100 additions and 27 deletions

View File

@@ -701,6 +701,6 @@ end
-- Inject internal debug framework
local debug = require "skynet.debug"
debug(skynet)
debug(skynet, dispatch_message)
return skynet

View File

@@ -1,4 +1,8 @@
return function (skynet)
local io = io
local table = table
local debug = debug
return function (skynet, dispatch_func)
local internal_info_func
@@ -43,6 +47,62 @@ function dbgcmd.EXIT()
skynet.exit()
end
local function getupvaluetable(u, func, unique)
i = 1
while true do
local name, value = debug.getupvalue(func, i)
if name == nil then
return
end
local t = type(value)
if t == "table" then
u[name] = value
elseif t == "function" then
if not unique[value] then
unique[value] = true
getupvaluetable(u, value, unique)
end
end
i=i+1
end
end
function dbgcmd.RUN(source, filename)
if filename then
filename = "@" .. filename
else
filename = "=(load)"
end
local output = {}
do
local function print(...)
local value = { ... }
for k,v in ipairs(value) do
value[k] = tostring(v)
end
table.insert(output, table.concat(value, "\t"))
end
local u = {}
local unique = {}
getupvaluetable(u, dispatch_func, unique)
getupvaluetable(u, skynet.register_protocol, unique)
local env = setmetatable( { print = print , _U = u }, { __index = _ENV })
local func, err = load(source, filename, "bt", env)
if not func then
skynet.ret(skynet.pack(err))
return
end
local ok, err = pcall(func)
if not ok then
table.insert(output, err)
end
source, filename = nil
end
collectgarbage "collect"
skynet.ret(skynet.pack(table.concat(output, "\n")))
end
local function _debug_dispatch(session, address, cmd, ...)
local f = dbgcmd[cmd]
assert(f, cmd)