From 3a1556e1fd7ab08adf6d90e0c26e9db67fcee2c6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 8 Jun 2018 11:24:27 +0800 Subject: [PATCH] add new debug command trace --- lualib/skynet.lua | 25 ++++++++++++++++++------- lualib/skynet/debug.lua | 10 ++++++++++ service/debug_console.lua | 17 +++++++++++++++++ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 5208c5f8..8b50d209 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -136,8 +136,8 @@ local function co_create(f) end -- coroutine exit local tag = session_coroutine_tracetag[co] - if tag then - c.trace(tag, "end") + if tag ~= nil then + if tag then c.trace(tag, "end") end session_coroutine_tracetag[co] = nil end local address = session_coroutine_address[co] @@ -271,6 +271,11 @@ skynet.hpc = c.hpc -- high performance counter local traceid = 0 function skynet.trace(info) + skynet.error("TRACE", session_coroutine_tracetag[running_thread]) + if session_coroutine_tracetag[running_thread] == false then + -- force off trace log + return + end traceid = traceid + 1 local tag = string.format(":%08x-%d",skynet.self(), traceid) @@ -584,15 +589,21 @@ local function raw_dispatch_message(prototype, msg, sz, session, source) session_coroutine_id[co] = session session_coroutine_address[co] = source local traceflag = p.trace - local tag = trace_source[source] - if tag then + if traceflag == false then + -- force off trace_source[source] = nil - if traceflag ~= false then + session_coroutine_tracetag[co] = false + else + local tag = trace_source[source] + if tag then + trace_source[source] = nil c.trace(tag, "request") session_coroutine_tracetag[co] = tag + elseif traceflag then + -- set running_thread for trace + running_thread = co + skynet.trace() end - elseif traceflag then - skynet.trace() end suspend(co, coroutine_resume(co, session,source, p.unpack(msg,sz))) else diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index f776c96e..802548ee 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -78,6 +78,16 @@ local function init(skynet, export) skynet.response() -- get response , but not return. raise error when exit end + function dbgcmd.TRACELOG(proto, flag) + if type(proto) ~= "string" then + flag = proto + proto = "lua" + end + skynet.error(string.format("Turn trace log %s for %s", flag, proto)) + skynet.traceproto(proto, flag) + skynet.ret() + end + return dbgcmd end -- function init_dbgcmd diff --git a/service/debug_console.lua b/service/debug_console.lua index 33cc06ee..6ca3a6c6 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -158,6 +158,7 @@ function COMMAND.help() shrtbl = "Show shared short string table info", ping = "ping address", call = "call address ...", + trace = "trace address [proto] [on|off]", } end @@ -343,6 +344,22 @@ function COMMAND.ping(address) return tostring(ti) end +local function toboolean(x) + return x and (x == "true" or x == "on") +end + +function COMMAND.trace(address, proto, flag) + address = adjust_address(address) + if flag == nil then + if proto == "on" or proto == "off" then + proto = toboolean(proto) + end + else + flag = toboolean(flag) + end + skynet.call(address, "debug", "TRACELOG", proto, flag) +end + function COMMANDX.call(cmd) local address = adjust_address(cmd[2]) local cmdline = assert(cmd[1]:match("%S+%s+%S+%s(.+)") , "need arguments")