diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 1c912b21..4652ce3c 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -324,7 +324,7 @@ function skynet.call(addr, typename, ...) end local session = c.send(addr, p.id , nil , p.pack(...)) if session == nil then - error("call to invalid address " .. tostring(addr)) + error("call to invalid address " .. skynet.address(addr)) end return p.unpack(yield_call(addr, session)) end @@ -335,7 +335,7 @@ function skynet.blockcall(addr, typename , ...) local session = c.send(addr, p.id , nil , p.pack(...)) if session == nil then c.command("UNLOCK") - error("call to invalid address " .. tostring(addr)) + error("call to invalid address " .. skynet.address(addr)) end return p.unpack(yield_call(addr, session)) end @@ -510,7 +510,11 @@ function skynet.query_group(handle) end function skynet.address(addr) - return string.format(":%x",addr) + if type(addr) == "number" then + return string.format(":%x",addr) + else + return tostring(addr) + end end function skynet.harbor(addr) @@ -566,6 +570,7 @@ function dbgcmd.TIMING() else -- turn on timing timing_call = {} + skynet.ret(skynet.pack(timing_call)) end end diff --git a/service/debug_console.lua b/service/debug_console.lua new file mode 100644 index 00000000..fecf7b44 --- /dev/null +++ b/service/debug_console.lua @@ -0,0 +1,80 @@ +local skynet = require "skynet" +local socket = require "socket" + +local port = tonumber(...) + +local function format_table(t) + local index = {} + for k in pairs(t) do + table.insert(index, k) + end + table.sort(index) + local result = {} + for _,v in ipairs(index) do + table.insert(result, string.format("%s:%s",v,tostring(t[v]))) + end + return table.concat(result,"\t") +end + +local function dump_line(print, key, value) + if type(value) == "table" then + print(key, format_table(value)) + else + print(key,tostring(value)) + end +end + +local function dump_list(print, list) + local index = {} + for k in pairs(list) do + table.insert(index, k) + end + table.sort(index) + for _,v in ipairs(index) do + dump_line(print, v, list[v]) + end +end + +local function docmd(cmdline, print) + local split = {} + for i in string.gmatch(cmdline, "%S+") do + table.insert(split,i) + end + local ok, list = pcall(skynet.call,".launcher","lua", table.unpack(split)) + if ok then + if list then + dump_list(print, list) + end + else + print("Error:", list) + end +end + +local function console_main_loop(stdin, print) + socket.lock(stdin) + print("Welcome to skynet console") + while true do + local cmdline = socket.readline(stdin, "\n") + if not cmdline then + break + end + if cmdline ~= "" then + docmd(cmdline, print) + end + end + socket.unlock(stdin) +end + +skynet.start(function() + local listen_socket = socket.listen ("127.0.0.1", port) + print("Start debug console at 127.0.0.1",port) + socket.start(listen_socket , function(id, addr) + local function print(...) + local t = { ... } + socket.write(id, table.concat(t,"\t")) + socket.write(id, "\n") + end + socket.start(id) + skynet.fork(console_main_loop, id , print) + end) +end) diff --git a/service/launcher.lua b/service/launcher.lua index 7cf71bb0..6283b3fa 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -9,12 +9,14 @@ local function handle_to_address(handle) return tonumber("0x" .. string.sub(handle , 2)) end +local NORET = {} + function command.LIST() local list = {} for k,v in pairs(services) do list[skynet.address(k)] = v end - skynet.ret(skynet.pack(list)) + return list end function command.RELOAD(handle) @@ -23,9 +25,9 @@ function command.RELOAD(handle) print(services[handle],cmd) if cmd then skynet.send(handle,"debug","RELOAD",cmd) - skynet.ret(skynet.pack({cmd})) + return {cmd} else - skynet.ret(skynet.pack({"Support only snlua"})) + return {"Support only snlua"} end end @@ -35,23 +37,23 @@ function command.STAT() local stat = skynet.call(k,"debug","STAT") list[skynet.address(k)] = stat end - skynet.ret(skynet.pack(list)) + return list end function command.INFO(handle) handle = handle_to_address(handle) if services[handle] == nil then - skynet.ret(skynet.pack(nil)) + return else local result = skynet.call(handle,"debug","INFO") - skynet.ret(skynet.pack(result)) + return result end end function command.TIMING(handle) handle = handle_to_address(handle) if services[handle] == nil then - skynet.ret(skynet.pack(nil)) + return else local r = skynet.call(handle,"debug","TIMING") local result = {} @@ -60,15 +62,16 @@ function command.TIMING(handle) v.avg = v.ti/v.n result[skynet.address(k)] = v end - skynet.ret(skynet.pack(result)) + return result end end function command.KILL(handle) handle = handle_to_address(handle) skynet.kill(handle) - skynet.ret( skynet.pack({ [handle] = tostring(services[handle]) })) + local ret = { [skynet.address(handle)] = tostring(services[handle]) } services[handle] = nil + return ret end function command.MEM() @@ -77,18 +80,20 @@ function command.MEM() local kb, bytes = skynet.call(k,"debug","MEM") list[skynet.address(k)] = string.format("%d Kb (%s)",kb,v) end - skynet.ret(skynet.pack(list)) + return list end function command.GC() for k,v in pairs(services) do skynet.send(k,"debug","GC") end - command.MEM() + return command.MEM() end function command.REMOVE(handle) services[handle] = nil + -- don't return (skynet.ret) because the handle may exit + return NORET end local instance = {} @@ -124,7 +129,15 @@ end) skynet.dispatch("lua", function(session, address, cmd , ...) cmd = string.upper(cmd) - command[cmd](...) + local f = command[cmd] + if f then + local ret = f(...) + if ret ~= NORET then + skynet.ret(skynet.pack(ret)) + end + else + skynet.ret(skynet.pack {"Unknown command"} ) + end end) skynet.start(function() end) diff --git a/service/main.lua b/service/main.lua index 863591fa..1e935af6 100644 --- a/service/main.lua +++ b/service/main.lua @@ -8,6 +8,7 @@ skynet.start(function() skynet.monitor "simplemonitor" local lualog = skynet.newservice("lualog") local console = skynet.newservice("console") +-- skynet.newservice("debug_console",8000) local watchdog = skynet.newservice("watchdog","8888", max_client, 0) local db = skynet.newservice("simpledb") -- skynet.launch("snlua","testgroup") diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index f017c25c..49cf288b 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -594,10 +594,10 @@ _filter_args(struct skynet_context * context, int type, int *session, void ** da } if (needcopy && *data) { - char * msg = malloc(*sz+1); + char * msg = malloc(*sz+1); memcpy(msg, *data, *sz); msg[*sz] = '\0'; - *data = msg; + *data = msg; } assert((*sz & HANDLE_MASK) == *sz); @@ -630,7 +630,7 @@ skynet_send(struct skynet_context * context, uint32_t source, uint32_t destinati if (skynet_context_push(destination, &smsg)) { free(data); - skynet_error(NULL, "Drop message from %x to %x (type=%d)(size=%d)", source, destination, type, (int)(sz & HANDLE_MASK)); + skynet_error(NULL, "Drop message from %x to %x (type=%d)(size=%d)", source, destination, type&0xff, (int)(sz & HANDLE_MASK)); return -1; } }