# \`stacktrace()\` giving back wrong files for some of the frames

**URL:** <https://discourse.julialang.org/t/stacktrace-giving-back-wrong-files-for-some-of-the-frames/97075>\
**Category:** General Usage\
**Created:** [April 4, 2023, 5:50pm UTC](https://discourse.julialang.org/t/stacktrace-giving-back-wrong-files-for-some-of-the-frames/97075 "2023-04-04T17:50:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashton314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashton314/32/48752_2.png) [@ashton314](https://discourse.julialang.org/u/ashton314)\
**Post date:** [April 4, 2023, 5:50pm UTC](https://discourse.julialang.org/t/stacktrace-giving-back-wrong-files-for-some-of-the-frames/97075/1 "2023-04-04T17:50:44Z")

</div>

Hi all!

I’m noticing some odd behavior around collecting stack traces. When I call `stacktrace()` I get some frames that look like this:

```julia
injectable_region : /Users/ashton/.julia/dev/FloatTracker/src/Injector.jl:164
should_inject : ./Base.jl:0
run_should_inject : /Users/ashton/.julia/dev/FloatTracker/test/injector_tests.jl:14
macro expansion (inlined) : /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-aarch64-4.0/build/default-macmini-aarch64-4-0/julialang/julia-release-1-dot-8/usr/share/julia/stdlib/v1.8/Test/src/Test.jl:464
…

```

That `should_inject` function on line 2 actually lives inside the same `Injector.jl` module seen on line 1. Moreover, if I add a `println()` before the return, I get this (correct) stack trace:

```julia
injectable_region : /Users/ashton/.julia/dev/FloatTracker/src/Injector.jl:155
should_inject : /Users/ashton/.julia/dev/FloatTracker/src/Injector.jl:128
run_should_inject : /Users/ashton/.julia/dev/FloatTracker/test/injector_tests.jl:14
macro expansion (inlined) : /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-aarch64-4.0/build/default-macmini-aarch64-4-0/julialang/julia-release-1-dot-8/usr/share/julia/stdlib/v1.8/Test/src/Test.jl:464
…

```

Here’s the `should_inject` function:

```julia
function should_inject(i::Injector)::Bool
  i.place_counter += 1

  # Are we replaying a recording?
  if i.replay !== ""
    go = i.place_counter === i.replay_script[i.replay_head].counter && frame_file(drop_ft_frames(stacktrace())[1]) === i.replay_script[i.replay_head].check

    if go
      i.replay_head += 1
    end

    return go
  end

  if i.active && i.ninject > 0 && rand(1:i.odds) == 1
    if i.record !== ""
      # We're recording this
      did_injectp = injectable_region(i, stacktrace())
      if did_injectp
        fh = open(i.record, "a")
        println(fh, "$(i.place_counter), $(frame_file(drop_ft_frames(stacktrace())[1]))")
        close(fh)
      end
      return did_injectp
    else
      println("defer to injectable_region") # THIS IS THE CRITICAL PRINTLN CALL
      return injectable_region(i, stacktrace())
    end
  end

  println("nope way")
  return false
end

```

For my application, I need a way to check whether or not my call originates from inside the standard library. Right now I’m just looking at the paths on the stack frame’s `file` attribute. It’s a bit hacky, but it works well enough for this POC code.

Any help to determine _why_ `stacktrace()` gives me different result when print is/isn’t present would be appreciated—or if there’s some other, less-brittle way to tell if your call stack goes through the standard library, that’d be awesome too.
