|
262 | 262 | @testset "Base.StackTraces docstrings" begin |
263 | 263 | @test isempty(Docs.undocumented_names(StackTraces)) |
264 | 264 | end |
| 265 | + |
| 266 | + |
| 267 | +@testset "Dispatch backtraces" begin |
| 268 | + # Check that it's possible to capture a backtrace upon entrance to inference |
| 269 | + # This test ensures that SnoopCompile will continue working |
| 270 | + # See in particular SnoopCompile/SnoopCompileCore/src/snoop_inference.jl |
| 271 | + # and the "diagnostics" devdoc. |
| 272 | + @noinline callee(x::Int) = sin(x) |
| 273 | + caller(x) = invokelatest(callee, x) |
| 274 | + |
| 275 | + @test sin(0) == 0 # force compilation of sin(::Int) |
| 276 | + dispatch_backtraces = [] |
| 277 | + ccall(:jl_set_inference_entrance_backtraces, Cvoid, (Any,), dispatch_backtraces) |
| 278 | + caller(3) |
| 279 | + ccall(:jl_set_inference_entrance_backtraces, Cvoid, (Any,), nothing) |
| 280 | + ln = @__LINE__() - 2 |
| 281 | + fl = Symbol(@__FILE__()) |
| 282 | + @test length(dispatch_backtraces) == 4 # 2 ci-backtrace pairs, stored as 4 separate elements |
| 283 | + mcallee, mcaller = only(methods(callee)), only(methods(caller)) |
| 284 | + # Extract pairs from the flattened array format: ci at odd indices, backtrace at even indices |
| 285 | + pairs = [(dispatch_backtraces[i], dispatch_backtraces[i+1]) for i in 1:2:length(dispatch_backtraces)] |
| 286 | + @test any(pairs) do (ci, trace) |
| 287 | + # trace is a SimpleVector from jl_backtrace_from_here, need to reformat before stacktrace |
| 288 | + bt = Base._reformat_bt(trace[1], trace[2]) |
| 289 | + ci.def.def === mcallee && any(stacktrace(bt)) do sf |
| 290 | + sf.file == fl && sf.line == ln |
| 291 | + end |
| 292 | + end |
| 293 | + @test any(pairs) do (ci, trace) |
| 294 | + # trace is a SimpleVector from jl_backtrace_from_here, need to reformat before stacktrace |
| 295 | + bt = Base._reformat_bt(trace[1], trace[2]) |
| 296 | + ci.def.def === mcaller && any(stacktrace(bt)) do sf |
| 297 | + sf.file == fl && sf.line == ln |
| 298 | + end |
| 299 | + end |
| 300 | +end |
0 commit comments