mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-23 19:43:09 +00:00
add debug command call
This commit is contained in:
@@ -13,13 +13,14 @@ local ip = (arg.n == 2 and arg[1] or "127.0.0.1")
|
||||
local port = tonumber(arg[arg.n])
|
||||
|
||||
local COMMAND = {}
|
||||
local COMMANDX = {}
|
||||
|
||||
local function format_table(t)
|
||||
local index = {}
|
||||
for k in pairs(t) do
|
||||
table.insert(index, k)
|
||||
end
|
||||
table.sort(index)
|
||||
table.sort(index, function(a, b) return tostring(a) < tostring(b) end)
|
||||
local result = {}
|
||||
for _,v in ipairs(index) do
|
||||
table.insert(result, string.format("%s:%s",v,tostring(t[v])))
|
||||
@@ -40,7 +41,7 @@ local function dump_list(print, list)
|
||||
for k in pairs(list) do
|
||||
table.insert(index, k)
|
||||
end
|
||||
table.sort(index)
|
||||
table.sort(index, function(a, b) return tostring(a) < tostring(b) end)
|
||||
for _,v in ipairs(index) do
|
||||
dump_line(print, v, list[v])
|
||||
end
|
||||
@@ -61,9 +62,16 @@ local function docmd(cmdline, print, fd)
|
||||
local cmd = COMMAND[command]
|
||||
local ok, list
|
||||
if cmd then
|
||||
ok, list = pcall(cmd, fd, select(2,table.unpack(split)))
|
||||
ok, list = pcall(cmd, table.unpack(split,2))
|
||||
else
|
||||
print("Invalid command, type help for command list")
|
||||
cmd = COMMANDX[command]
|
||||
if cmd then
|
||||
split.fd = fd
|
||||
split[1] = cmdline
|
||||
ok, list = pcall(cmd, split)
|
||||
else
|
||||
print("Invalid command, type help for command list")
|
||||
end
|
||||
end
|
||||
|
||||
if ok then
|
||||
@@ -147,6 +155,7 @@ function COMMAND.help()
|
||||
cmem = "Show C memory info",
|
||||
shrtbl = "Show shared short string table info",
|
||||
ping = "ping address",
|
||||
call = "call address ...",
|
||||
}
|
||||
end
|
||||
|
||||
@@ -154,7 +163,7 @@ function COMMAND.clearcache()
|
||||
codecache.clear()
|
||||
end
|
||||
|
||||
function COMMAND.start(fd, ...)
|
||||
function COMMAND.start(...)
|
||||
local ok, addr = pcall(skynet.newservice, ...)
|
||||
if ok then
|
||||
if addr then
|
||||
@@ -167,7 +176,7 @@ function COMMAND.start(fd, ...)
|
||||
end
|
||||
end
|
||||
|
||||
function COMMAND.log(fd, ...)
|
||||
function COMMAND.log(...)
|
||||
local ok, addr = pcall(skynet.call, ".launcher", "lua", "LOGLAUNCH", "snlua", ...)
|
||||
if ok then
|
||||
if addr then
|
||||
@@ -180,7 +189,7 @@ function COMMAND.log(fd, ...)
|
||||
end
|
||||
end
|
||||
|
||||
function COMMAND.snax(fd, ...)
|
||||
function COMMAND.snax(...)
|
||||
local ok, s = pcall(snax.newservice, ...)
|
||||
if ok then
|
||||
local addr = s.handle
|
||||
@@ -213,7 +222,7 @@ function COMMAND.mem()
|
||||
return skynet.call(".launcher", "lua", "MEM")
|
||||
end
|
||||
|
||||
function COMMAND.kill(fd, address)
|
||||
function COMMAND.kill(address)
|
||||
return skynet.call(".launcher", "lua", "KILL", address)
|
||||
end
|
||||
|
||||
@@ -221,11 +230,11 @@ function COMMAND.gc()
|
||||
return skynet.call(".launcher", "lua", "GC")
|
||||
end
|
||||
|
||||
function COMMAND.exit(fd, address)
|
||||
function COMMAND.exit(address)
|
||||
skynet.send(adjust_address(address), "debug", "EXIT")
|
||||
end
|
||||
|
||||
function COMMAND.inject(fd, address, filename)
|
||||
function COMMAND.inject(address, filename)
|
||||
address = adjust_address(address)
|
||||
local f = io.open(filename, "rb")
|
||||
if not f then
|
||||
@@ -236,23 +245,23 @@ function COMMAND.inject(fd, address, filename)
|
||||
return skynet.call(address, "debug", "RUN", source, filename)
|
||||
end
|
||||
|
||||
function COMMAND.task(fd, address)
|
||||
function COMMAND.task(address)
|
||||
address = adjust_address(address)
|
||||
return skynet.call(address,"debug","TASK")
|
||||
end
|
||||
|
||||
function COMMAND.info(fd, address, ...)
|
||||
function COMMAND.info(address, ...)
|
||||
address = adjust_address(address)
|
||||
return skynet.call(address,"debug","INFO", ...)
|
||||
end
|
||||
|
||||
function COMMAND.debug(fd, address)
|
||||
address = adjust_address(address)
|
||||
function COMMANDX.debug(cmd)
|
||||
local address = adjust_address(cmd[2])
|
||||
local agent = skynet.newservice "debug_agent"
|
||||
local stop
|
||||
skynet.fork(function()
|
||||
repeat
|
||||
local cmdline = socket.readline(fd, "\n")
|
||||
local cmdline = socket.readline(cmd.fd, "\n")
|
||||
cmdline = cmdline and cmdline:gsub("(.*)\r$", "%1")
|
||||
if not cmdline then
|
||||
skynet.send(agent, "lua", "cmd", "cont")
|
||||
@@ -261,21 +270,21 @@ function COMMAND.debug(fd, address)
|
||||
skynet.send(agent, "lua", "cmd", cmdline)
|
||||
until stop or cmdline == "cont"
|
||||
end)
|
||||
skynet.call(agent, "lua", "start", address, fd)
|
||||
skynet.call(agent, "lua", "start", address, cmd.fd)
|
||||
stop = true
|
||||
end
|
||||
|
||||
function COMMAND.logon(fd, address)
|
||||
function COMMAND.logon(address)
|
||||
address = adjust_address(address)
|
||||
core.command("LOGON", skynet.address(address))
|
||||
end
|
||||
|
||||
function COMMAND.logoff(fd, address)
|
||||
function COMMAND.logoff(address)
|
||||
address = adjust_address(address)
|
||||
core.command("LOGOFF", skynet.address(address))
|
||||
end
|
||||
|
||||
function COMMAND.signal(fd, address, sig)
|
||||
function COMMAND.signal(address, sig)
|
||||
address = skynet.address(adjust_address(address))
|
||||
if sig then
|
||||
core.command("SIGNAL", string.format("%s %d",address,sig))
|
||||
@@ -301,10 +310,22 @@ function COMMAND.shrtbl()
|
||||
return { n = n, total = total, longest = longest, space = space }
|
||||
end
|
||||
|
||||
function COMMAND.ping(fd, address)
|
||||
function COMMAND.ping(address)
|
||||
address = adjust_address(address)
|
||||
local ti = skynet.now()
|
||||
skynet.call(address, "debug", "PING")
|
||||
ti = skynet.now() - ti
|
||||
return tostring(ti)
|
||||
end
|
||||
|
||||
function COMMANDX.call(cmd)
|
||||
local address = adjust_address(cmd[2])
|
||||
local cmdline = assert(cmd[1]:match("%S+%s+%S+%s(.+)") , "need arguments")
|
||||
local args_func = assert(load("return " .. cmdline, "debug console", "t", {}), "Invalid arguments")
|
||||
local args = table.pack(pcall(args_func))
|
||||
if not args[1] then
|
||||
error(args[2])
|
||||
end
|
||||
local rets = table.pack(skynet.call(address, "lua", table.unpack(args, 2, args.n)))
|
||||
return rets
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user