Stack traces
Viewing the stack trace
The main function used to get a stack trace is stacktrace
.
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})() в event.jl:92
When calling a function stacktrace()
returns a vector of the type StackTraces.StackFrame
. For simplicity, instead of Vector{StackFrame}
you can use an alias StackTraces.StackTrace
. (The examples with [...]
indicate that the output may vary depending on how the code is executed.)
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
[...]
Note that when calling the function 'stacktrace() a frame with `eval at boot.jl usually appears. When calling a function
stacktrace()
from the REPL, you will also have several additional stack frames from the REPL.jl
, usually looking something like this:
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})() in the event.jl:92
Extracting useful information
Each type StackTraces.StackFrame
contains the function name, file name, line number, lambda information, a flag indicating whether the frame was embedded, 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 the function backtrace
.
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
Stack trace information can be obtained programmatically for logging, error handling, etc.
Error handling
While easy access to information about the current state of the call stack can be useful in many places, its most obvious application is error handling and debugging.
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]
[...]
You can see that in the example above, the first stack frame points to line 4, where the function is called. 'stacktrace`, and not to line 2, where bad_function is called, and the frame of the bad_function' function is completely missing. This is understandable, given that the function 'stacktrace
is called from the context of hook (catch). Although it is quite easy to find the actual source of the error in this example, in complex cases, tracking the source of the error becomes a non-trivial task.
This can be fixed by passing the result to the function catch_backtrace
functions stacktrace
. Instead of returning information about the call stack for the current context, the function catch_backtrace
returns stack information for the context of the most recent exception.
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
[...]
Note that the stack trace now contains the corresponding row number and the missing frame.
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
[...]
Exception stacks and current_exceptions
Compatibility: Julia 1.1
Exception stacks require a version of Julia at least 1.1. |
While processing one exception, others may occur. It is recommended to check all these exceptions to determine the root cause of the problem. The Julia runtime supports this feature by sending each exception that occurs to the internal exception stack. When the code exits the catch
in the usual manner, all exceptions that were sent to the stack in the associated try
are considered successfully handled and removed from the stack.
The current exception stack can be accessed using the function current_exceptions
. For example,
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})() в 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})() в task.jl:259
In this example, the root cause exception (A) is the first on the stack, followed by another exception (B). After both catch blocks exit correctly (i.e. without an additional exception), all exceptions are removed from the stack and become unavailable.
The exception stack is stored in the task where the exceptions occurred. When a task fails with uncaught exceptions, the current_exceptions(task)
function can be used to check the exception stack for that task.
Comparison with backtrace
Function call backtrace
returns the vector Union{Ptr{Nothing}, Base.Interpretip}
, which can then be passed to the function stacktrace
for conversion:
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})() в event.jl:92
Note that the vector returned by the function has backtrace
, there were 18 elements, and the vector returned by the function stacktrace
, only 6. This is due to the fact that by default the function 'stacktrace` removes all lower-level C language functions from the stack. You can enable stack frames from C calls as follows:
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})() в 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
Individual pointers returned by the function backtrace
, can be converted to the type StackTraces.StackFrame
by passing them to the function StackTraces.lookup
.
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!