-
Notifications
You must be signed in to change notification settings - Fork 1
/
cross_tracter.lua
209 lines (195 loc) · 5.27 KB
/
cross_tracter.lua
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
local skynet = require "skynet"
local skynet_send = skynet.send
local skynet_call = skynet.call
local skynet_dispatch = skynet.dispatch
local function pairsForLocalvalue(fun)
local id = 0
local nextF = function ()
id = id + 1
local ln, lv = debug.getlocal(fun, id)
return ln, lv
end
return nextF
end
local function getUpvalue(func, upvalueName)
local i = 1
while true do
local name, value = debug.getupvalue(func, i)
if not name then break end
if name == upvalueName then
return value
end
i = i + 1
end
return nil
end
local function tracebacks(startIndex)
startIndex= startIndex or 3
local ret = {"\n"}
repeat
local stackInfo = debug.getinfo(startIndex)
if stackInfo then
local funcInfo = debug.getinfo(stackInfo.func)
local currentline = stackInfo.currentline
local flag = " [" .. tostring(funcInfo.what) .. "]"
if currentline < 0 then
flag = flag .. tostring(funcInfo.source) .. ":" .. tostring(funcInfo.linedefined) .. "->" .. tostring(funcInfo.lastlinedefined)
else
flag = flag .. tostring(funcInfo.source) .. ":" .. tostring(currentline)
end
if stackInfo.name then
flag = flag .. " in function:".. stackInfo.name
end
if stackInfo.istailcall then
flag = flag .. "\n"
flag = flag .. " tail call"
end
ret[#ret+1] = flag
end
startIndex = startIndex + 1
until not stackInfo
return table.concat(ret, "\n")
end
local function dispatcher_traceback(session, source, arList, typename, ...)
local arLists = skynet.tracebackStrCache[source] and skynet.tracebackStrCache[source].arLists or {}
arLists[#arLists + 1] = arList
skynet.tracebackStrCache[source] = {
arLists = arLists,
typename = typename,
session = session,
source = source,
argv = {...}
}
end
local function make_ar_list(startIndex)
startIndex = startIndex or 3
local ret = {}
repeat
local stackInfo = debug.getinfo(startIndex)
if stackInfo then
local funcInfo = debug.getinfo(stackInfo.func)
funcInfo.func = nil
ret[#ret + 1] = {
funcInfo = funcInfo,
currentline = stackInfo.currentline,
name = stackInfo.name,
istailcall = stackInfo.istailcall,
}
end
startIndex = startIndex + 1
until not stackInfo
return ret
end
local function print_ar_lists(arlists)
local ret = {}
arlists[#arlists + 1] = make_ar_list()
for i = #arlists, 1, -1 do
local arlist = arlists[i]
for _, ar in ipairs(arlist) do
local funcInfo = ar.funcInfo
local currentline = ar.currentline
local flag = " [" .. tostring(funcInfo.what) .. "]"
if currentline < 0 then
flag = flag .. tostring(funcInfo.source) .. ":" .. tostring(funcInfo.linedefined) .. "->" .. tostring(funcInfo.lastlinedefined)
else
flag = flag .. tostring(funcInfo.source) .. ":" .. tostring(currentline)
end
if ar.name then
flag = flag .. " in function:".. ar.name
end
if ar.istailcall then
flag = flag .. "\n"
flag = flag .. " tail call"
end
ret[#ret+1] = flag
end
end
return table.concat(ret, "\n")
end
return function ()
skynet.PTYPE_TRACEBACK = 14 --协议号
skynet.tracebackStrCache = {}
skynet.register_protocol {
name = "traceback",
id = skynet.PTYPE_TRACEBACK,
pack = skynet.pack,
unpack = skynet.unpack,
dispatch = dispatcher_traceback,
}
skynet.send = function(addr, id, ...)
local protos = getUpvalue(skynet.register_protocol, "proto")
local p = protos[id]
if type(p) == "table" and p.name ~= "traceback" then
if p.name == "snax" or p.name == "lua" then
skynet_send(addr, "traceback", make_ar_list(), p.name)
end
end
return skynet_send(addr, id, ...)
end
skynet.call = function(addr, id, ...)
local protos = getUpvalue(skynet.register_protocol, "proto")
local p = protos[id]
if type(p) == "table" and p.name ~= "traceback" then
if p.name == "snax" or p.name == "lua" then
skynet_send(addr, "traceback", make_ar_list(), p.name)
end
end
return skynet_call(addr, id, ...)
end
skynet.dispatch = function(typeName, func)
local newFunc = function(session, source, cmd, ...)
local __debugInfo___
if skynet.tracebackStrCache[source] and
skynet.tracebackStrCache[source].typename == typeName
then
__debugInfo___ = skynet.tracebackStrCache[source]
skynet.tracebackStrCache[source] = nil
end
func(session, source, cmd, ...)
end
skynet_dispatch(typeName, newFunc)
end
--- 直接打印跨服堆栈
skynet.printCrossTrace = function()
local index = 1
while true do
local key, value = debug.getlocal(4, index)
if key == nil then
break
end
if key == "__debugInfo___" then
skynet.error("\r\n"..print_ar_lists(value.arLists))
return
end
index = index + 1
end
end
--- 直接打印跨服堆栈
skynet.getCrossTrace = function()
local index = 1
while true do
local key, value = debug.getlocal(4, index)
if key == nil then
break
end
if key == "__debugInfo___" then
return print_ar_lists(value.arLists)
end
index = index + 1
end
end
--- 返回跨服的栈帧列表,方便调试用
skynet.getCrossArLists = function()
local index = 1
while true do
local key, value = debug.getlocal(4, index)
if key == nil then
break
end
if key == "__debugInfo___" then
return value.arLists
end
index = index + 1
end
end
end