Stack Traces

    The primary function used to obtain a stack trace is :

    Calling stacktrace() returns a vector of 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 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

    Each 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:

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

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

    You may notice that in the example above the first stack frame points at line 4, where is called, rather than line 2, where bad_function is called, and bad_function‘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 to stacktrace. Instead of returning callstack information for the current context, 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.

    Julia 1.1

    Exception stacks requires at least Julia 1.1.

    The stack of current exceptions can be accessed using the experimental Base.catch_stack function. For example,

    1. julia> try
    2. error("(A) The root cause")
    3. catch
    4. try
    5. error("(B) An exception while handling the exception")
    6. catch
    7. for (exc, bt) in Base.catch_stack()
    8. showerror(stdout, exc, bt)
    9. println(stdout)
    10. end
    11. end
    12. end
    13. (A) The root cause
    14. Stacktrace:
    15. [2] top-level scope at REPL[7]:2
    16. [4] eval_user_input(::Any, ::REPL.REPLBackend) at REPL.jl:85
    17. [5] macro expansion at REPL.jl:117 [inlined]
    18. [6] (::getfield(REPL, Symbol("##26#27")){REPL.REPLBackend})() at task.jl:259
    19. (B) An exception while handling the exception
    20. Stacktrace:
    21. [1] error(::String) at error.jl:33
    22. [2] top-level scope at REPL[7]:5
    23. [3] eval(::Module, ::Any) at boot.jl:319
    24. [4] eval_user_input(::Any, ::REPL.REPLBackend) at REPL.jl:85
    25. [5] macro expansion at REPL.jl:117 [inlined]
    26. [6] (::getfield(REPL, Symbol("##26#27")){REPL.REPLBackend})() at task.jl:259

    In this example the root cause exception (A) is first on the stack, with a further exception (B) following it. After exiting both catch blocks normally (i.e., without throwing a further exception) all exceptions are removed from the stack and are no longer accessible.

    The exception stack is stored on the Task where the exceptions occurred. When a task fails with uncaught exceptions, catch_stack(task) may be used to inspect the exception stack for that task.

    A call to 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 had 18 elements, while the vector returned by stacktrace only has 6. This is because, by default, 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> 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)!")