# How to know which function calls a given function (without using debugger)?

**URL:** <https://discourse.julialang.org/t/how-to-know-which-function-calls-a-given-function-without-using-debugger/10810>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 10, 2018, 8:02am UTC](https://discourse.julialang.org/t/how-to-know-which-function-calls-a-given-function-without-using-debugger/10810 "2018-05-10T08:02:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [May 10, 2018, 8:02am UTC](https://discourse.julialang.org/t/how-to-know-which-function-calls-a-given-function-without-using-debugger/10810/1 "2018-05-10T08:02:29Z")

</div>

I wonder if there is a way in which I can insert a couple lines of code in a function to know which function calls this one, e.g. by printing out some basic information of the caller, like location etc. I’m trying to understand a large and complicated package for which ASTInterpreter2 fails to track down the work flow of the code due to a few bugs.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 10, 2018, 8:45am UTC](https://discourse.julialang.org/t/how-to-know-which-function-calls-a-given-function-without-using-debugger/10810/2 "2018-05-10T08:45:17Z")

</div>

You can use [`stacktrace()`](https://docs.julialang.org/en/latest/base/stacktraces/#Base.StackTraces.stacktrace) for that. The most basic code would look something like this:

```julia
function outer(x)
    y = inner(x)
    return y + x
end

function inner(x)
    # paste the following let block wherever
    let trace = stacktrace()
        name = trace[1].func # name of current function
        length(trace) >= 2 ?
            println("function `$(name)` was called by $(trace[2])") :
            println("function `$(name)` was called at the top level")
    end
    1 + x
end

outer(3)

```

which then prints

```julia
function `inner` was called by outer(::Int64) at untitled:74

```

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [May 10, 2018, 8:58am UTC](https://discourse.julialang.org/t/how-to-know-which-function-calls-a-given-function-without-using-debugger/10810/3 "2018-05-10T08:58:08Z")

</div>

Christ! This is exactly what I’m looking for. @pfitzseb, you’re absolutely my debugging hero!!
