-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathbreakpoint.lua
More file actions
441 lines (413 loc) · 11.7 KB
/
Copy pathbreakpoint.lua
File metadata and controls
441 lines (413 loc) · 11.7 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
local rdebug = require 'luadebug.visitor'
local fs = require 'backend.worker.filesystem'
local source = require 'backend.worker.source'
local eval = require 'backend.worker.eval'
local ev = require 'backend.event'
local hookmgr = require 'luadebug.hookmgr'
local parser = require 'backend.worker.parser'
local stdout = require 'backend.worker.stdout'
local currentactive = {}
local waitverify = {}
local info = {}
local m = {}
local enable = false
local function updateHook()
if enable then
if next(currentactive) == nil and next(waitverify) == nil then
enable = false
hookmgr.break_open(false)
end
else
if next(currentactive) ~= nil or next(waitverify) ~= nil then
enable = true
hookmgr.break_open(true)
end
end
end
local function hasActiveBreakpoint(bps, activeline)
if activeline then
for line in pairs(bps) do
if activeline[line] then
return true
end
end
end
return false
end
local function bpKey(src)
if src.sourceReference then
return src.sourceReference
end
local path = fs.path_native(fs.path_normalize(src.path))
if src.startline then
return path..":"..src.startline
end
return path
end
local function bpClientKey(src)
if src.sourceReference then
return src.sourceReference
end
return fs.path_native(fs.path_normalize(src.path))
end
local function updateBreakpoint(src, breakpoints)
local bpkey = bpKey(src)
local bps
if breakpoints == nil then
bps = currentactive[bpkey]
if not bps then
return
end
else
bps = {}
for _, bp in ipairs(breakpoints) do
if bp.verified == true then
bps[bp.line] = bp
end
end
end
if next(bps) == nil then
currentactive[bpkey] = nil
for proto in pairs(src.protos) do
hookmgr.break_del(proto)
end
else
currentactive[bpkey] = bps
local lineinfo = src.lineinfo
if lineinfo then
for proto, key in pairs(src.protos) do
if hasActiveBreakpoint(bps, lineinfo[key]) then
hookmgr.break_add(proto)
else
hookmgr.break_del(proto)
end
end
else
for proto in pairs(src.protos) do
hookmgr.break_add(proto)
end
end
end
updateHook()
end
local function NormalizeErrorMessage(what, err)
return ("%s: %s."):format(what, err:gsub("^:%d+: %(EVAL%):%d+: (.*)$", "%1"))
end
local function setBreakPointUnverified(bp, errmsg)
bp.unverified = true
ev.emit('breakpoint', 'changed', {
id = bp.id,
message = errmsg,
verified = false,
})
end
local function valid(bp)
if bp.condition then
local ok, err = eval.verify(bp.condition)
if not ok then
setBreakPointUnverified(bp, NormalizeErrorMessage("Condition Error", err))
return false
end
end
if bp.hitCondition then
local ok, err = eval.verify('0 '..bp.hitCondition)
if not ok then
setBreakPointUnverified(bp, NormalizeErrorMessage("HitCondition Error", err))
return false
end
end
return true
end
local function verifyBreakpoint(breakpoints)
local hits = {}
for _, bp in ipairs(breakpoints) do
if not valid(bp) then
goto continue
end
bp.realLine = bp.line
bp.statHit = hits[bp.realLine] or 0
if bp.logMessage then
local n = 0
bp.statLog = {}
bp.statLog[1] = bp.logMessage:gsub('%b{}', function(str)
n = n + 1
local key = ('{%d}'):format(n)
bp.statLog[key] = str:sub(2, -2)
return key
end)
bp.statLog[1] = bp.statLog[1]..'\n'
end
::continue::
end
end
local function verifyBreakpointByLineInfo(src, breakpoints)
local lineinfo = src.lineinfo
for _, bp in ipairs(breakpoints) do
if bp.unverified ~= nil then
goto continue
end
if lineinfo then
local activeline = lineinfo[bp.line]
if not activeline then
if not src.startline then
setBreakPointUnverified(bp, "The breakpoint didn't hit a valid line.")
end
goto continue
end
bp.line = activeline
end
bp.source = src
bp.verified = true
ev.emit('breakpoint', 'changed', {
id = bp.id,
line = bp.line,
verified = true,
})
::continue::
end
end
-- 降级模式:无 lineinfo 时跳过行号校正,直接标记断点为 verified
local function verifyBreakpointWithoutLineInfo(src, breakpoints)
for _, bp in ipairs(breakpoints) do
if bp.unverified ~= nil then
goto continue
end
bp.source = src
bp.verified = true
ev.emit('breakpoint', 'changed', {
id = bp.id,
line = bp.line,
verified = true,
})
::continue::
end
end
function m.find(src, currentline)
local currentBP = currentactive[bpKey(src)]
if not currentBP then
hookmgr.break_closeline()
return
end
return currentBP[currentline]
end
local function parserInlineLineinfo(src)
local old = parser(src.content)
if not old then
return
end
local new = {}
local diff = src.startline - 1
for k, v in pairs(old) do
if type(k) == "number" then
new[k + diff] = v + diff
else
local newv = {}
for l in pairs(v) do newv[l + diff] = true end
if k == "0-0" then
new[k] = newv
else
local s, e = k:match "^(%d+)-(%d+)$"
s = tonumber(s) + 1
e = tonumber(e) + 1
new[("%d-%d"):format(s, e)] = newv
end
end
end
return new
end
local function calcLineInfo(src, content)
if not src.lineinfo then
if src.content then
src.lineinfo = parserInlineLineinfo(src)
elseif content then
src.lineinfo = parser(content)
elseif src.sourceReference then
src.lineinfo = parser(source.getCode(src.sourceReference))
end
end
return src.lineinfo
end
local function cantVerifyBreakpoints(breakpoints)
for _, bp in ipairs(breakpoints) do
setBreakPointUnverified(bp, "The source file has no line information.")
end
end
function m.set_bp(clientsrc, breakpoints, content)
verifyBreakpoint(breakpoints)
local srcarray = source.c2s(clientsrc)
if srcarray then
local ok = false
for _, src in ipairs(srcarray) do
if calcLineInfo(src, content) then
ok = true
end
end
if ok then
for _, src in ipairs(srcarray) do
verifyBreakpointByLineInfo(src, breakpoints)
updateBreakpoint(src, breakpoints)
end
elseif content == false then
-- 降级路径:前端已上报但 sourceContent 为空
for _, src in ipairs(srcarray) do
verifyBreakpointWithoutLineInfo(src, breakpoints)
updateBreakpoint(src, breakpoints)
end
else
cantVerifyBreakpoints(breakpoints)
end
waitverify[bpClientKey(clientsrc)] = nil
updateHook()
else
waitverify[bpClientKey(clientsrc)] = {
breakpoints = breakpoints,
content = content,
}
updateHook()
end
end
function m.exec(bp)
if bp.condition then
local ok, res = eval.eval(bp.condition)
if not ok or not res then
return false
end
end
bp.statHit = bp.statHit + 1
if bp.hitCondition then
local ok, res = eval.eval(bp.statHit..' '..bp.hitCondition)
if not ok or res ~= true then
return false
end
end
if bp.statLog then
local res = bp.statLog[1]:gsub('{%d+}', function(key)
local info = bp.statLog[key]
if not info then
return key
end
local ok, res = eval.eval(info)
if not ok then
return '{'..info..'}'
end
return tostring(res)
end)
rdebug.getinfo(0, "Sl", info)
stdout(res, info)
return false
end
return true
end
function m.newproto(proto, src, key)
src.protos[proto] = key
local bpkey = bpClientKey(src)
local wv = waitverify[bpkey]
if wv then
if not wv.content then
waitverify[bpkey] = nil
end
if not calcLineInfo(src, wv.content) then
if wv.content == false then
-- 降级路径:前端已上报但 sourceContent 为空
verifyBreakpointWithoutLineInfo(src, wv.breakpoints)
updateBreakpoint(src, wv.breakpoints)
return
end
cantVerifyBreakpoints(wv.breakpoints)
return
end
verifyBreakpointByLineInfo(src, wv.breakpoints)
updateBreakpoint(src, wv.breakpoints)
return
end
updateBreakpoint(src)
end
local funcs = {}
function m.set_funcbp(breakpoints)
funcs = {}
for _, bp in ipairs(breakpoints) do
local ok, err = eval.verify(bp.name)
if not ok then
setBreakPointUnverified(bp, NormalizeErrorMessage("Error", err))
goto continue
end
if not valid(bp) then
goto continue
end
funcs[#funcs + 1] = bp
bp.verified = true
bp.statHit = 0
ev.emit('breakpoint', 'changed', bp)
::continue::
end
hookmgr.funcbp_open(#funcs > 0)
end
function m.hit_bp(src, currentline)
local bp = m.find(src, currentline)
if bp and m.exec(bp) then
return bp
end
end
function m.hit_funcbp(func)
for _, bp in ipairs(funcs) do
local ok, res = eval.eval(bp.name, 1)
if ok and res == func and m.exec(bp) then
return bp
end
end
end
local exceptionFilters = {}
function m.hitExceptionBreakpoint(flags, level, error)
for _, flag in ipairs(flags) do
local bp = exceptionFilters[flag]
if bp then
if not bp.condition then
return bp
end
local ok, res = eval.eval(bp.condition, level, { error = error })
if ok and res then
return bp
end
end
end
end
function m.setExceptionBreakpoints(breakpoints)
exceptionFilters = {}
for _, filter in ipairs(breakpoints) do
if not filter.condition then
exceptionFilters[filter.filterId] = {
id = filter.id,
}
ev.emit('breakpoint', 'changed', {
id = filter.id,
verified = true,
})
goto continue
end
local ok, err = eval.verify(filter.condition)
if not ok then
setBreakPointUnverified(filter, NormalizeErrorMessage("Error", err))
goto continue
end
exceptionFilters[filter.filterId] = {
id = filter.id,
condition = filter.condition,
}
ev.emit('breakpoint', 'changed', {
id = filter.id,
verified = true,
})
::continue::
end
if hookmgr.exception_open then
hookmgr.exception_open(next(exceptionFilters) ~= nil)
end
end
ev.on('terminated', function()
currentactive = {}
waitverify = {}
info = {}
enable = false
hookmgr.break_open(false)
end)
return m