# How to get a stacktrace

**URL:** https://discourse.julialang.org/t/how-to-get-a-stacktrace/9960
**Category:** New to Julia
**Tags:** debug
**Created:** [March 25, 2018, 11:11am UTC](https://discourse.julialang.org/t/how-to-get-a-stacktrace/9960 "2018-03-25T11:11:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![leoll2](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@leoll2](https://discourse.julialang.org/u/leoll2)
#### Post date: [March 25, 2018, 11:11am UTC](https://discourse.julialang.org/t/how-to-get-a-stacktrace/9960/1 "2018-03-25T11:11:17Z")

</div>

My question is simple: When I perform an operation, I need to get a list of all the functions that get called during its execution.

For example:

```julia
function foo()
    ...
    rand(Int32)
end

function bar()
    foo()
end

```

What I want is something like:

```julia
julia> show_stacktrace(bar())
- Call to bar() with no arguments
- Call to foo() with no arguments
- Call to Base.rand(T::Type) with argument 'Int32'
- ... # <- other calls within the Julia libraries

```

I’ve read the [manual page of StackTraces](https://docs.julialang.org/en/stable/manual/stacktraces/) but didn’t understand much. The examples in that page aren’t very clear and look different from what I want.  
Thanks!

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 25, 2018, 11:22am UTC](https://discourse.julialang.org/t/how-to-get-a-stacktrace/9960/2 "2018-03-25T11:22:07Z")

</div>

The stacktrace should be obtained at the innermost point in the stack which you are interested in. Eg if you merely want to print,

```julia
@noinline function foo(x)
    show(STDOUT, MIME"text/plain"(), stacktrace())
    x + 1
end

@noinline bar(x) = foo(x + 2)

baz(x) = bar(x + 3)

baz(0)

```

shows

```julia
9-element Array{StackFrame,1}:
 foo(::Int64) at foo.jl:2                                     
 bar(::Int64) at foo.jl:6                                     
 baz(::Int64) at foo.jl:8                                     
 ....

```

Most of the complications in the example above are about pretty-printing and preventing inlining.

---

<div class="post-metadata">

### Author: ![leoll2](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@leoll2](https://discourse.julialang.org/u/leoll2)
#### Post date: [March 25, 2018, 11:37am UTC](https://discourse.julialang.org/t/how-to-get-a-stacktrace/9960/3 "2018-03-25T11:37:34Z")

</div>

What if the innermost point is inside the Julia source code or an external package or something I can’t modify?

For example, for `sort([1,3,2])` I want a list of all the functions called during the execution of `Base.sort(v::AbstractArray)`.

Also, is it possible to show the arguments passed to them?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 25, 2018, 11:39am UTC](https://discourse.julialang.org/t/how-to-get-a-stacktrace/9960/4 "2018-03-25T11:39:23Z")

</div>

You would need to use a debugger, but that is work in progress at the moment:  
[https://github.com/Keno/ASTInterpreter2.jl](https://github.com/Keno/ASTInterpreter2.jl)  
[https://github.com/Keno/Gallium.jl](https://github.com/Keno/Gallium.jl)

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [March 25, 2018, 8:37pm UTC](https://discourse.julialang.org/t/how-to-get-a-stacktrace/9960/5 "2018-03-25T20:37:56Z")

</div>

> [@leoll2](#):
>
> What if the innermost point is inside the Julia source code or an external package or something I can’t modify?

You can try [TraceCalls.jl](http://nbviewer.jupyter.org/github/cstjean/TraceCalls.jl/blob/master/README.ipynb). It doesn’t work super well with Base functions at the moment; for that you may wait for 0.7 and [Cassette.jl](https://github.com/jrevels/Cassette.jl).

---

<div class="post-metadata">

### Author: ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)
#### Post date: [January 12, 2023, 1:15pm UTC](https://discourse.julialang.org/t/how-to-get-a-stacktrace/9960/6 "2023-01-12T13:15:18Z")

</div>

are there any new approaches here on simply getting a non-interactive, non-intrusive function stack trace of a method call ?
