Blame view

publish/skynet/lualib/skynet/debug.lua 2.62 KB
4d6f285d   zhouhaihai   增加发布功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  local table = table
  local extern_dbgcmd = {}
  
  local function init(skynet, export)
  	local internal_info_func
  
  	function skynet.info_func(func)
  		internal_info_func = func
  	end
  
  	local dbgcmd
  
  	local function init_dbgcmd()
  		dbgcmd = {}
  
  		function dbgcmd.MEM()
  			local kb = collectgarbage "count"
  			skynet.ret(skynet.pack(kb))
  		end
  
  		function dbgcmd.GC()
  
  			collectgarbage "collect"
  		end
  
  		function dbgcmd.STAT()
  			local stat = {}
  			stat.task = skynet.task()
  			stat.mqlen = skynet.stat "mqlen"
  			stat.cpu = skynet.stat "cpu"
  			stat.message = skynet.stat "message"
  			skynet.ret(skynet.pack(stat))
  		end
  
  		function dbgcmd.TASK(session)
  			if session then
  				skynet.ret(skynet.pack(skynet.task(session)))
  			else
  				local task = {}
  				skynet.task(task)
  				skynet.ret(skynet.pack(task))
  			end
  		end
  
  		function dbgcmd.UNIQTASK()
  			skynet.ret(skynet.pack(skynet.uniqtask()))
  		end
  
  		function dbgcmd.INFO(...)
  			if internal_info_func then
  				skynet.ret(skynet.pack(internal_info_func(...)))
  			else
  				skynet.ret(skynet.pack(nil))
  			end
  		end
  
  		function dbgcmd.EXIT()
  			skynet.exit()
  		end
  
  		function dbgcmd.RUN(source, filename, ...)
  			local inject = require "skynet.inject"
  			local args = table.pack(...)
  			local ok, output = inject(skynet, source, filename, args, export.dispatch, skynet.register_protocol)
  			collectgarbage "collect"
  			skynet.ret(skynet.pack(ok, table.concat(output, "\n")))
  		end
  
  		function dbgcmd.TERM(service)
  			skynet.term(service)
  		end
  
  		function dbgcmd.REMOTEDEBUG(...)
  			local remotedebug = require "skynet.remotedebug"
  			remotedebug.start(export, ...)
  		end
  
  		function dbgcmd.SUPPORT(pname)
  			return skynet.ret(skynet.pack(skynet.dispatch(pname) ~= nil))
  		end
  
  		function dbgcmd.PING()
  			return skynet.ret()
  		end
  
  		function dbgcmd.LINK()
  			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
  
  	local function _debug_dispatch(session, address, cmd, ...)
  		dbgcmd = dbgcmd or init_dbgcmd() -- lazy init dbgcmd
  		local f = dbgcmd[cmd] or extern_dbgcmd[cmd]
  		assert(f, cmd)
  		f(...)
  	end
  
  	skynet.register_protocol {
  		name = "debug",
  		id = assert(skynet.PTYPE_DEBUG),
  		pack = assert(skynet.pack),
  		unpack = assert(skynet.unpack),
  		dispatch = _debug_dispatch,
  	}
  end
  
  local function reg_debugcmd(name, fn)
  	extern_dbgcmd[name] = fn
  end
  
  return {
  	init = init,
  	reg_debugcmd = reg_debugcmd,
  }