mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +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 port = tonumber(arg[arg.n])
|
||||||
|
|
||||||
local COMMAND = {}
|
local COMMAND = {}
|
||||||
|
local COMMANDX = {}
|
||||||
|
|
||||||
local function format_table(t)
|
local function format_table(t)
|
||||||
local index = {}
|
local index = {}
|
||||||
for k in pairs(t) do
|
for k in pairs(t) do
|
||||||
table.insert(index, k)
|
table.insert(index, k)
|
||||||
end
|
end
|
||||||
table.sort(index)
|
table.sort(index, function(a, b) return tostring(a) < tostring(b) end)
|
||||||
local result = {}
|
local result = {}
|
||||||
for _,v in ipairs(index) do
|
for _,v in ipairs(index) do
|
||||||
table.insert(result, string.format("%s:%s",v,tostring(t[v])))
|
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
|
for k in pairs(list) do
|
||||||
table.insert(index, k)
|
table.insert(index, k)
|
||||||
end
|
end
|
||||||
table.sort(index)
|
table.sort(index, function(a, b) return tostring(a) < tostring(b) end)
|
||||||
for _,v in ipairs(index) do
|
for _,v in ipairs(index) do
|
||||||
dump_line(print, v, list[v])
|
dump_line(print, v, list[v])
|
||||||
end
|
end
|
||||||
@@ -61,9 +62,16 @@ local function docmd(cmdline, print, fd)
|
|||||||
local cmd = COMMAND[command]
|
local cmd = COMMAND[command]
|
||||||
local ok, list
|
local ok, list
|
||||||
if cmd then
|
if cmd then
|
||||||
ok, list = pcall(cmd, fd, select(2,table.unpack(split)))
|
ok, list = pcall(cmd, table.unpack(split,2))
|
||||||
else
|
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
|
end
|
||||||
|
|
||||||
if ok then
|
if ok then
|
||||||
@@ -147,6 +155,7 @@ function COMMAND.help()
|
|||||||
cmem = "Show C memory info",
|
cmem = "Show C memory info",
|
||||||
shrtbl = "Show shared short string table info",
|
shrtbl = "Show shared short string table info",
|
||||||
ping = "ping address",
|
ping = "ping address",
|
||||||
|
call = "call address ...",
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -154,7 +163,7 @@ function COMMAND.clearcache()
|
|||||||
codecache.clear()
|
codecache.clear()
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.start(fd, ...)
|
function COMMAND.start(...)
|
||||||
local ok, addr = pcall(skynet.newservice, ...)
|
local ok, addr = pcall(skynet.newservice, ...)
|
||||||
if ok then
|
if ok then
|
||||||
if addr then
|
if addr then
|
||||||
@@ -167,7 +176,7 @@ function COMMAND.start(fd, ...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.log(fd, ...)
|
function COMMAND.log(...)
|
||||||
local ok, addr = pcall(skynet.call, ".launcher", "lua", "LOGLAUNCH", "snlua", ...)
|
local ok, addr = pcall(skynet.call, ".launcher", "lua", "LOGLAUNCH", "snlua", ...)
|
||||||
if ok then
|
if ok then
|
||||||
if addr then
|
if addr then
|
||||||
@@ -180,7 +189,7 @@ function COMMAND.log(fd, ...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.snax(fd, ...)
|
function COMMAND.snax(...)
|
||||||
local ok, s = pcall(snax.newservice, ...)
|
local ok, s = pcall(snax.newservice, ...)
|
||||||
if ok then
|
if ok then
|
||||||
local addr = s.handle
|
local addr = s.handle
|
||||||
@@ -213,7 +222,7 @@ function COMMAND.mem()
|
|||||||
return skynet.call(".launcher", "lua", "MEM")
|
return skynet.call(".launcher", "lua", "MEM")
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.kill(fd, address)
|
function COMMAND.kill(address)
|
||||||
return skynet.call(".launcher", "lua", "KILL", address)
|
return skynet.call(".launcher", "lua", "KILL", address)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -221,11 +230,11 @@ function COMMAND.gc()
|
|||||||
return skynet.call(".launcher", "lua", "GC")
|
return skynet.call(".launcher", "lua", "GC")
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.exit(fd, address)
|
function COMMAND.exit(address)
|
||||||
skynet.send(adjust_address(address), "debug", "EXIT")
|
skynet.send(adjust_address(address), "debug", "EXIT")
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.inject(fd, address, filename)
|
function COMMAND.inject(address, filename)
|
||||||
address = adjust_address(address)
|
address = adjust_address(address)
|
||||||
local f = io.open(filename, "rb")
|
local f = io.open(filename, "rb")
|
||||||
if not f then
|
if not f then
|
||||||
@@ -236,23 +245,23 @@ function COMMAND.inject(fd, address, filename)
|
|||||||
return skynet.call(address, "debug", "RUN", source, filename)
|
return skynet.call(address, "debug", "RUN", source, filename)
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.task(fd, address)
|
function COMMAND.task(address)
|
||||||
address = adjust_address(address)
|
address = adjust_address(address)
|
||||||
return skynet.call(address,"debug","TASK")
|
return skynet.call(address,"debug","TASK")
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.info(fd, address, ...)
|
function COMMAND.info(address, ...)
|
||||||
address = adjust_address(address)
|
address = adjust_address(address)
|
||||||
return skynet.call(address,"debug","INFO", ...)
|
return skynet.call(address,"debug","INFO", ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.debug(fd, address)
|
function COMMANDX.debug(cmd)
|
||||||
address = adjust_address(address)
|
local address = adjust_address(cmd[2])
|
||||||
local agent = skynet.newservice "debug_agent"
|
local agent = skynet.newservice "debug_agent"
|
||||||
local stop
|
local stop
|
||||||
skynet.fork(function()
|
skynet.fork(function()
|
||||||
repeat
|
repeat
|
||||||
local cmdline = socket.readline(fd, "\n")
|
local cmdline = socket.readline(cmd.fd, "\n")
|
||||||
cmdline = cmdline and cmdline:gsub("(.*)\r$", "%1")
|
cmdline = cmdline and cmdline:gsub("(.*)\r$", "%1")
|
||||||
if not cmdline then
|
if not cmdline then
|
||||||
skynet.send(agent, "lua", "cmd", "cont")
|
skynet.send(agent, "lua", "cmd", "cont")
|
||||||
@@ -261,21 +270,21 @@ function COMMAND.debug(fd, address)
|
|||||||
skynet.send(agent, "lua", "cmd", cmdline)
|
skynet.send(agent, "lua", "cmd", cmdline)
|
||||||
until stop or cmdline == "cont"
|
until stop or cmdline == "cont"
|
||||||
end)
|
end)
|
||||||
skynet.call(agent, "lua", "start", address, fd)
|
skynet.call(agent, "lua", "start", address, cmd.fd)
|
||||||
stop = true
|
stop = true
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.logon(fd, address)
|
function COMMAND.logon(address)
|
||||||
address = adjust_address(address)
|
address = adjust_address(address)
|
||||||
core.command("LOGON", skynet.address(address))
|
core.command("LOGON", skynet.address(address))
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.logoff(fd, address)
|
function COMMAND.logoff(address)
|
||||||
address = adjust_address(address)
|
address = adjust_address(address)
|
||||||
core.command("LOGOFF", skynet.address(address))
|
core.command("LOGOFF", skynet.address(address))
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.signal(fd, address, sig)
|
function COMMAND.signal(address, sig)
|
||||||
address = skynet.address(adjust_address(address))
|
address = skynet.address(adjust_address(address))
|
||||||
if sig then
|
if sig then
|
||||||
core.command("SIGNAL", string.format("%s %d",address,sig))
|
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 }
|
return { n = n, total = total, longest = longest, space = space }
|
||||||
end
|
end
|
||||||
|
|
||||||
function COMMAND.ping(fd, address)
|
function COMMAND.ping(address)
|
||||||
address = adjust_address(address)
|
address = adjust_address(address)
|
||||||
local ti = skynet.now()
|
local ti = skynet.now()
|
||||||
skynet.call(address, "debug", "PING")
|
skynet.call(address, "debug", "PING")
|
||||||
ti = skynet.now() - ti
|
ti = skynet.now() - ti
|
||||||
return tostring(ti)
|
return tostring(ti)
|
||||||
end
|
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