Showing function calls (like in stacktrace)

Is there a way to show the functions called inside a function like in the stacktrace of error messages, even if the function returns successfully?

In other words, I would like to see something like [1]–[6] in the following error message, from the start to the end of a function call:

julia> broadcast(+, [1 2; 3 4], [1 2; 3 4; 5 6])
ERROR: DimensionMismatch("arrays could not be broadcast to a common size")
Stacktrace:
 [1] _bcs1(::Base.OneTo{Int64}, ::Base.OneTo{Int64}) at ./broadcast.jl:70
 [2] _bcs at ./broadcast.jl:63 [inlined]
 [3] broadcast_shape at ./broadcast.jl:57 [inlined] (repeats 2 times)
 [4] broadcast_indices at ./broadcast.jl:53 [inlined]
 [5] broadcast_c at ./broadcast.jl:311 [inlined]
 [6] broadcast(::Function, ::Array{Int64,2}, ::Array{Int64,2}) at ./broadcast.jl:434

I need this capability to debug semantic errors by showing all the functions visited and examine what went wrong. Currently I’m achieving this by successively calling which.

Have you heard of Jupyter Notebook Viewer?

2 Likes

TraceCalls isn’t super good at tracing Base code at the moment. You have to specify the functions manually, and Base tends to use a lot of eval, @generated and @inline, all of which aren’t traceable yet. But stay tuned!

BB = Base.Broadcast
broadcast_funs = (BB.broadcast, BB.broadcast_c, BB.broadcast_indices, BB.broadcast_shape, BB._bcs, BB._bcs1)
@trace broadcast_funs broadcast(+, [1 2; 3 4], [1 2; 3 4])

Like display(stacktrace()) ?

1 Like