堆栈跟踪

StackTraces 模块提供了简单的堆栈跟踪,这些堆栈跟踪既可读又易于编程使用。

查看堆栈跟踪

获取堆栈跟踪的主要函数是stacktrace

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

Calling stacktrace() returns a vector of StackTraces.StackFrame s. For ease of use, the alias StackTraces.StackTrace can be used in place of Vector{StackFrame}. (Examples with […] indicate that output may vary depending on how the code is run.)

  1. julia> example() = stacktrace()
  2. example (generic function with 1 method)
  3. julia> example()
  4. 7-element Array{Base.StackTraces.StackFrame,1}:
  5. example() at REPL[1]:1
  6. top-level scope
  7. eval at boot.jl:317 [inlined]
  8. [...]
  9. julia> @noinline child() = stacktrace()
  10. child (generic function with 1 method)
  11. julia> @noinline parent() = child()
  12. parent (generic function with 1 method)
  13. julia> grandparent() = parent()
  14. grandparent (generic function with 1 method)
  15. julia> grandparent()
  16. 9-element Array{Base.StackTraces.StackFrame,1}:
  17. child() at REPL[3]:1
  18. parent() at REPL[4]:1
  19. grandparent() at REPL[5]:1
  20. [...]

Note that when calling stacktrace() you'll typically see a frame with eval at boot.jl. When calling stacktrace() from the REPL you'll also have a few extra frames in the stack from REPL.jl, usually looking something like this:

  1. julia> example() = stacktrace()
  2. example (generic function with 1 method)
  3. julia> example()
  4. 7-element Array{Base.StackTraces.StackFrame,1}:
  5. example() at REPL[1]:1
  6. top-level scope
  7. eval at boot.jl:317 [inlined]
  8. eval(::Module, ::Expr) at REPL.jl:5
  9. eval_user_input(::Any, ::REPL.REPLBackend) at REPL.jl:85
  10. macro expansion at REPL.jl:116 [inlined]
  11. (::getfield(REPL, Symbol("##28#29")){REPL.REPLBackend})() at event.jl:92

Extracting useful information

Each StackTraces.StackFrame contains the function name, file name, line number, lambda info, a flag indicating whether the frame has been inlined, a flag indicating whether it is a C function (by default C functions do not appear in the stack trace), and an integer representation of the pointer returned by backtrace:

  1. julia> frame = stacktrace()[3]
  2. eval(::Module, ::Expr) at REPL.jl:5
  3. julia> frame.func
  4. :eval
  5. julia> frame.file
  6. Symbol("~/julia/usr/share/julia/stdlib/v0.7/REPL/src/REPL.jl")
  7. julia> frame.line
  8. 5
  9. julia> top_frame.linfo
  10. MethodInstance for eval(::Module, ::Expr)
  11. julia> top_frame.inlined
  12. false
  13. julia> top_frame.from_c
  14. false
  1. julia> top_frame.pointer
  2. 0x00007f92d6293171

This makes stack trace information available programmatically for logging, error handling, and more.

Error handling

While having easy access to information about the current state of the callstack can be helpful in many places, the most obvious application is in error handling and debugging.

  1. julia> @noinline bad_function() = undeclared_variable
  2. bad_function (generic function with 1 method)
  3. julia> @noinline example() = try
  4. bad_function()
  5. catch
  6. stacktrace()
  7. end
  8. example (generic function with 1 method)
  9. julia> example()
  10. 7-element Array{Base.StackTraces.StackFrame,1}:
  11. example() at REPL[2]:4
  12. top-level scope
  13. eval at boot.jl:317 [inlined]
  14. [...]

You may notice that in the example above the first stack frame points points at line 4, where stacktrace is called, rather than line 2, where bad_function is called, and badfunction's frame is missing entirely. This is understandable, given that stacktrace is called from the context of the _catch. While in this example it's fairly easy to find the actual source of the error, in complex cases tracking down the source of the error becomes nontrivial.

This can be remedied by passing the result of catch_backtrace to stacktrace. Instead of returning callstack information for the current context, catch_backtrace returns stack information for the context of the most recent exception:

  1. julia> @noinline bad_function() = undeclared_variable
  2. bad_function (generic function with 1 method)
  3. julia> @noinline example() = try
  4. bad_function()
  5. catch
  6. stacktrace(catch_backtrace())
  7. end
  8. example (generic function with 1 method)
  9. julia> example()
  10. 8-element Array{Base.StackTraces.StackFrame,1}:
  11. bad_function() at REPL[1]:1
  12. example() at REPL[2]:2
  13. [...]

Notice that the stack trace now indicates the appropriate line number and the missing frame.

  1. julia> @noinline child() = error("Whoops!")
  2. child (generic function with 1 method)
  3. julia> @noinline parent() = child()
  4. parent (generic function with 1 method)
  5. julia> @noinline function grandparent()
  6. try
  7. parent()
  8. catch err
  9. println("ERROR: ", err.msg)
  10. stacktrace(catch_backtrace())
  11. end
  12. end
  13. grandparent (generic function with 1 method)
  14. julia> grandparent()
  15. ERROR: Whoops!
  16. 10-element Array{Base.StackTraces.StackFrame,1}:
  17. error at error.jl:33 [inlined]
  18. child() at REPL[1]:1
  19. parent() at REPL[2]:1
  20. grandparent() at REPL[3]:3
  21. [...]

Comparison with backtrace

A call to backtrace returns a vector of Union{Ptr{Nothing}, Base.InterpreterIP}, which may then be passed into stacktrace for translation:

  1. julia> trace = backtrace()
  2. 18-element Array{Union{Ptr{Nothing}, Base.InterpreterIP},1}:
  3. Ptr{Nothing} @0x00007fd8734c6209
  4. Ptr{Nothing} @0x00007fd87362b342
  5. Ptr{Nothing} @0x00007fd87362c136
  6. Ptr{Nothing} @0x00007fd87362c986
  7. Ptr{Nothing} @0x00007fd87362d089
  8. Base.InterpreterIP(CodeInfo(:(begin
  9. Core.SSAValue(0) = backtrace()
  10. trace = Core.SSAValue(0)
  11. return Core.SSAValue(0)
  12. end)), 0x0000000000000000)
  13. Ptr{Nothing} @0x00007fd87362e4cf
  14. [...]
  15. julia> stacktrace(trace)
  16. 6-element Array{Base.StackTraces.StackFrame,1}:
  17. top-level scope
  18. eval at boot.jl:317 [inlined]
  19. eval(::Module, ::Expr) at REPL.jl:5
  20. eval_user_input(::Any, ::REPL.REPLBackend) at REPL.jl:85
  21. macro expansion at REPL.jl:116 [inlined]
  22. (::getfield(REPL, Symbol("##28#29")){REPL.REPLBackend})() at event.jl:92

Notice that the vector returned by backtrace had 18 elements, while the vector returned by stacktrace only has 6. This is because, by default, stacktrace removes any lower-level C functions from the stack. If you want to include stack frames from C calls, you can do it like this:

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

Individual pointers returned by backtrace can be translated into StackTraces.StackFrame s by passing them into StackTraces.lookup:

  1. julia> pointer = backtrace()[1];
  2. julia> frame = StackTraces.lookup(pointer)
  3. 1-element Array{Base.StackTraces.StackFrame,1}:
  4. jl_apply_generic at gf.c:2167
  5. julia> println("The top frame is from $(frame[1].func)!")
  6. The top frame is from jl_apply_generic!

原文: https://juliacn.github.io/JuliaZH.jl/latest/manual/stacktraces/