How do I use catch_backtrace()?

I’m in Julia 0.4.5, and trying to use catch_backtrace() in a catch block, but it just returns an array of Ptr objects and I’ve been unable to find any documentation detailing what to do with these Ptr objects.

So, how do I get the filename, line number and optionally, function name of each frame in the stack trace?

bt = backtrace();

for ip in bt
    for fr in StackTraces.lookup(ip)
        println(fr)
        # Can look at fields like fr.file, fr.func, fr.from_c etc as well
    end
    println()
end
1 Like

Thanks. No way to do it without depending on an external module?

External? StackTraces comes with julia Base. It started as a package though.

I had to Pkg.add it, it wasn’t available with the base 0.4.5 installation.

Yeah. It came in 0.5.

Unless you have a good reason to stay with 0.4.5, you should update to the bugfix only 0.4.7 version or upgrade to the stable release 0.5.0.

0.4.7 is definitely possible. 0.5.0 is not at the moment. We’ve only recently managed to move most of our customers over from the 0.3 line to the 0.4 line and some of them have 8-12 month upgrade cycles.

If you absolutely cannot use the StackTraces module – which is much nicer – there is unexported functionality to get basic frame info in 0.4. See this commit and the lookup function and LineInfo data structure.