AnyMath 文档

堆栈跟踪

[医]堆垛机 模块提供简单的堆栈跟踪,既可读又易于编程使用。

查看堆栈跟踪

用于获取堆栈跟踪的主要函数是 [医堆垛机]:

6-element Array{Base.StackTraces.StackFrame,1}:
 top-level scope
 eval at boot.jl:317 [inlined]
 eval(::Module, ::Expr) at REPL.jl:5
 eval_user_input(::Any, ::REPL.REPLBackend) at REPL.jl:85
 macro expansion at REPL.jl:116 [inlined]
 (::getfield(REPL, Symbol("##28#29")){REPL.REPLBackend})() at event.jl:92

打电话来 斯塔克特雷斯()返回一个向量 StackTraces。[医堆叠框架]s.为了便于使用,别名 StackTraces。[医堆垛机]可以用来代替 向量{StackFrame}. (例子与 [...] 指示输出可能因代码的运行方式而异。)

julia> example() = stacktrace()
example (generic function with 1 method)

julia> example()
7-element Array{Base.StackTraces.StackFrame,1}:
 example() at REPL[1]:1
 top-level scope
 eval at boot.jl:317 [inlined]
[...]

julia> @noinline child() = stacktrace()
child (generic function with 1 method)

julia> @noinline parent() = child()
parent (generic function with 1 method)

julia> grandparent() = parent()
grandparent (generic function with 1 method)

julia> grandparent()
9-element Array{Base.StackTraces.StackFrame,1}:
 child() at REPL[3]:1
 parent() at REPL[4]:1
 grandparent() at REPL[5]:1
[...]

请注意,在调用时 xref:base/stacktraces.adoc#Base.StackTraces.stacktrace[斯塔克特雷斯() 你通常会看到一个框架 启动时评估。jl. 打电话时 xref:base/stacktraces.adoc#Base.StackTraces.stacktrace[斯塔克特雷斯() 从REPL中,你也会在堆栈中有几个额外的帧。 REPL。jl,通常看起来像这样:

julia> example() = stacktrace()
example (generic function with 1 method)

julia> example()
7-element Array{Base.StackTraces.StackFrame,1}:
 example() at REPL[1]:1
 top-level scope
 eval at boot.jl:317 [inlined]
 eval(::Module, ::Expr) at REPL.jl:5
 eval_user_input(::Any, ::REPL.REPLBackend) at REPL.jl:85
 macro expansion at REPL.jl:116 [inlined]
 (::getfield(REPL, Symbol("##28#29")){REPL.REPLBackend})() at event.jl:92

提取有用信息

每个 StackTraces。[医堆叠框架]包含函数名,文件名,行号,lambda信息,指示帧是否已内联的标志,指示是否为C函数的标志(默认情况下,c函数不会出现在堆栈跟踪中),以及由 后退,后退:

julia> frame = stacktrace()[3]
eval(::Module, ::Expr) at REPL.jl:5

julia> frame.func
:eval

julia> frame.file
Symbol("~/julia/usr/share/julia/stdlib/v0.7/REPL/src/REPL.jl")

julia> frame.line
5

julia> frame.linfo
MethodInstance for eval(::Module, ::Expr)

julia> frame.inlined
false

julia> frame.from_c
false

julia> frame.pointer
0x00007f92d6293171

这使得堆栈跟踪信息以编程方式可用于日志记录、错误处理等。

错误处理

虽然可以轻松访问有关callstack当前状态的信息在许多地方都很有用,但最明显的应用程序是错误处理和调试。

julia> @noinline bad_function() = undeclared_variable
bad_function (generic function with 1 method)

julia> @noinline example() = try
           bad_function()
       catch
           stacktrace()
       end
example (generic function with 1 method)

julia> example()
7-element Array{Base.StackTraces.StackFrame,1}:
 example() at REPL[2]:4
 top-level scope
 eval at boot.jl:317 [inlined]
[...]

您可能会注意到,在上面的示例中,第一个堆栈帧指向第4行,其中 [医堆垛机]被调用,而不是第2行,其中_bad_function_被调用,并且 坏函数帧完全丢失了。 这是可以理解的,因为 [医堆垛机]从_catch_的上下文中调用。 虽然在这个例子中很容易找到错误的实际来源,但在复杂的情况下,跟踪错误的来源变得无关紧要。

这可以通过传递 [医捕手,捕手]到 [医堆垛机]. 而不是返回当前上下文的callstack信息, [医捕手,捕手]返回最近异常上下文的堆栈信息:

julia> @noinline bad_function() = undeclared_variable
bad_function (generic function with 1 method)

julia> @noinline example() = try
           bad_function()
       catch
           stacktrace(catch_backtrace())
       end
example (generic function with 1 method)

julia> example()
8-element Array{Base.StackTraces.StackFrame,1}:
 bad_function() at REPL[1]:1
 example() at REPL[2]:2
[...]

请注意,堆栈跟踪现在指示适当的行号和丢失的帧。

julia> @noinline child() = error("Whoops!")
child (generic function with 1 method)

julia> @noinline parent() = child()
parent (generic function with 1 method)

julia> @noinline function grandparent()
           try
               parent()
           catch err
               println("ERROR: ", err.msg)
               stacktrace(catch_backtrace())
           end
       end
grandparent (generic function with 1 method)

julia> grandparent()
ERROR: Whoops!
10-element Array{Base.StackTraces.StackFrame,1}:
 error at error.jl:33 [inlined]
 child() at REPL[1]:1
 parent() at REPL[2]:1
 grandparent() at REPL[3]:3
[...]

异常堆栈和 当前概念

兼容性

Julia1.1异常堆栈至少需要Julia1.1。

在处理异常时,可能会引发进一步的异常。 检查所有这些异常以确定问题的根本原因可能很有用。 Julia运行时通过在异常发生时将每个异常推送到内部_exception stack_来支持这一点。 当代码退出a 渔获 通常情况下,任何被推送到关联堆栈上的异常 试试 被认为是成功处理,并从堆栈中删除。

当前异常的堆栈可以使用 当前概念功能。 例如,

julia> try
           error("(A) The root cause")
       catch
           try
               error("(B) An exception while handling the exception")
           catch
               for (exc, bt) in current_exceptions()
                   showerror(stdout, exc, bt)
                   println(stdout)
               end
           end
       end
(A) The root cause
Stacktrace:
 [1] error(::String) at error.jl:33
 [2] top-level scope at REPL[7]:2
 [3] eval(::Module, ::Any) at boot.jl:319
 [4] eval_user_input(::Any, ::REPL.REPLBackend) at REPL.jl:85
 [5] macro expansion at REPL.jl:117 [inlined]
 [6] (::getfield(REPL, Symbol("##26#27")){REPL.REPLBackend})() at task.jl:259
(B) An exception while handling the exception
Stacktrace:
 [1] error(::String) at error.jl:33
 [2] top-level scope at REPL[7]:5
 [3] eval(::Module, ::Any) at boot.jl:319
 [4] eval_user_input(::Any, ::REPL.REPLBackend) at REPL.jl:85
 [5] macro expansion at REPL.jl:117 [inlined]
 [6] (::getfield(REPL, Symbol("##26#27")){REPL.REPLBackend})() at task.jl:259

在此示例中,根本原因异常(A)首先出现在堆栈上,后面还有一个异常(B)。 在正常退出两个catch块(即,不引发进一步的异常)后,所有异常都将从堆栈中删除,并且不再可访问。

异常堆栈存储在 任务 发生异常的地方。 当任务失败且未捕获异常时, current_exceptions(任务) 可用于检查该任务的异常堆栈。

与之比较 后退,后退

电话: 后退,后退返回一个向量 联盟{Ptr{Nothing},基地。解释器,然后可以传递到 [医堆垛机]作翻译:

julia> trace = backtrace()
18-element Array{Union{Ptr{Nothing}, Base.InterpreterIP},1}:
 Ptr{Nothing} @0x00007fd8734c6209
 Ptr{Nothing} @0x00007fd87362b342
 Ptr{Nothing} @0x00007fd87362c136
 Ptr{Nothing} @0x00007fd87362c986
 Ptr{Nothing} @0x00007fd87362d089
 Base.InterpreterIP(CodeInfo(:(begin
      Core.SSAValue(0) = backtrace()
      trace = Core.SSAValue(0)
      return Core.SSAValue(0)
  end)), 0x0000000000000000)
 Ptr{Nothing} @0x00007fd87362e4cf
[...]

julia> stacktrace(trace)
6-element Array{Base.StackTraces.StackFrame,1}:
 top-level scope
 eval at boot.jl:317 [inlined]
 eval(::Module, ::Expr) at REPL.jl:5
 eval_user_input(::Any, ::REPL.REPLBackend) at REPL.jl:85
 macro expansion at REPL.jl:116 [inlined]
 (::getfield(REPL, Symbol("##28#29")){REPL.REPLBackend})() at event.jl:92

请注意,返回的向量 后路,后路有18个元素,而向量返回 [医堆垛机]只有6个。 这是因为,默认情况下, [医堆垛机]从堆栈中删除任何较低级别的C函数。 如果你想从C调用中包含堆栈帧,你可以这样做:

julia> stacktrace(trace, true)
21-element Array{Base.StackTraces.StackFrame,1}:
 jl_apply_generic at gf.c:2167
 do_call at interpreter.c:324
 eval_value at interpreter.c:416
 eval_body at interpreter.c:559
 jl_interpret_toplevel_thunk_callback at interpreter.c:798
 top-level scope
 jl_interpret_toplevel_thunk at interpreter.c:807
 jl_toplevel_eval_flex at toplevel.c:856
 jl_toplevel_eval_in at builtins.c:624
 eval at boot.jl:317 [inlined]
 eval(::Module, ::Expr) at REPL.jl:5
 jl_apply_generic at gf.c:2167
 eval_user_input(::Any, ::REPL.REPLBackend) at REPL.jl:85
 jl_apply_generic at gf.c:2167
 macro expansion at REPL.jl:116 [inlined]
 (::getfield(REPL, Symbol("##28#29")){REPL.REPLBackend})() at event.jl:92
 jl_fptr_trampoline at gf.c:1838
 jl_apply_generic at gf.c:2167
 jl_apply at julia.h:1540 [inlined]
 start_task at task.c:268
 ip:0xffffffffffffffff

返回的单个指针 后退,后退可以翻译成 StackTraces。[医堆叠框架 我们把它们递进去 StackTraces。查阅资料:

julia> pointer = backtrace()[1];

julia> frame = StackTraces.lookup(pointer)
1-element Array{Base.StackTraces.StackFrame,1}:
 jl_apply_generic at gf.c:2167

julia> println("The top frame is from $(frame[1].func)!")
The top frame is from jl_apply_generic!