lua调试钩子导致竞态条件?

我想报告一下我正在写的lua解析器的一些调试信息。我正在使用调试钩子来进行跟踪,但似乎存在某种形式的竞态条件。

下面是我的测试代码:

enters = 0
enters2 = 0
calls = 0
tailcalls = 0
returns = 0
lines = 0
other = 0
exits = 0

local function analyze(arg)
    enters = enters + 1
    enters2 = enters2 + 1

    if arg == "call" then
        calls = calls + 1
    elseif arg == "tail call" then
        tailcalls = tailcalls + 1
    elseif arg == "return" then
        returns = returns + 1
    elseif arg == "line" then
        lines = lines + 1
    else
        other = other + 1
    end

    exits = exits + 1
end

debug.sethook(analyze, "crl")

-- 主要代码

print("enters = ", enters)
print("enters2 = ", enters2)
print("calls = ", calls)
print("tailcalls = ", tailcalls)
print("returns = ", returns)
print("lines = ", lines)
print("other = ", other)
print("exits = ", exits)

print("sum = ", calls + tailcalls + returns + lines + other)

这是结果:

enters =        429988
enters2 =       429991
calls =         97433
tailcalls =     7199
returns =       97436
lines =         227931
other =         0
exits =         430009
sum =   430012

为什么这些都对不上呢?我在Ubuntu 20.04上运行lua 5.4.2,没有定制c库,也没有进一步的调试库操作。

原文链接 https://stackoverflow.com/questions/71009670

点赞
stackoverflow用户3825996
stackoverflow用户3825996

我发现了问题...

当打印结果时调用 print 函数也会触发钩子,但只会影响尚未打印的结果。

2022-02-06 17:46:16