# Finding the module of a callsite in the stacktrace

**URL:** <https://discourse.julialang.org/t/finding-the-module-of-a-callsite-in-the-stacktrace/65191>\
**Category:** General Usage\
**Tags:** question, base\
**Created:** [July 23, 2021, 4:42pm UTC](https://discourse.julialang.org/t/finding-the-module-of-a-callsite-in-the-stacktrace/65191 "2021-07-23T16:42:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)\
**Post date:** [July 23, 2021, 4:42pm UTC](https://discourse.julialang.org/t/finding-the-module-of-a-callsite-in-the-stacktrace/65191/1 "2021-07-23T16:42:58Z")

</div>

I want to get an item returned from `stacktrace()` such that it is the first StackFrame for which `getmodule(x) == m` where `m` is some known module. This works:

```julia
getmodule(x) = nothing
getmodule(x::Base.StackTraces.StackFrame) = getmodule(x.linfo)
getmodule(x::Core.MethodInstance) = getmodule(x.def)
getmodule(x::Core.Method) = x.module
getmodule(x::Core.Module) = x

```

And I use it in my code like so

```julia
trace = stacktrace()
callindex = m === nothing ? trace[2] :
     findfirst(x -> getmodule(x) === m, trace)
callsite = isnothing(callindex) ? trace[2] : trace[callindex]

```

From what I can tell, `linfo` is a [documented property](https://docs.julialang.org/en/v1/manual/stacktraces/) of stacktrace items, and `MethodInstance` is documented under julia internals such that the above should work. But I’m guessing anything under internals is subject to change in future versions (right?)

Is there a more reliable way to do this that would have more guarantees to work with new Julia releases? [CodeTracking](https://github.com/timholy/CodeTracking.jl/blob/beb25bc995f2feb912e81f3c1d2936e12ffbb35e/src/CodeTracking.jl#L82-L92) has some related tooling, but it does not extract the module.
