Stack backtrace location links


#1

With a fresh Rust install stack backtrace reports source locations that look like temporary files or perhaps memory pointers + some meta info. That’s not very helpful and doesn’t match the Book. What magic do I need to perform to see source code locations that point to actual source files? See copy-pasta below.

While we are at it. Is there a way to filter out runtime frames like “panic”, “backtrace” etc that have no relation to the code being run? Actual code is being sandwiched between bookkeeping that doesn’t help any, so you have to carefully eyeball the entire backtrace to even figure out where your code is.

Thanks

  9: <usize as core::slice::SliceIndex<[T]>>::index
         at /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/libcore/slice/mod.rs:2455
  10: core::slice::<impl core::ops::index::Index<I> for [T]>::index
             at /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/libcore/slice/mod.rs:2323
  11: <alloc::vec::Vec<T> as core::ops::index::Index<I>>::index
             at /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/liballoc/vec.rs:1678
  12: try_rust::main
             at src/main.rs:4

#2

It looks like those are stack frames from the standard library. In particular, probably somewhere around here for the libcore/slice/mod.rs bits. I don’t believe you’ll be able to get a “real” source path because the standard library is distributed in its compiled form (otherwise every project would need to re-compile std all the time).

The leading /rustc/ probably indicate that the stack frames come from a rustc-specific library, with the hex string containing the compiled library’s hash.

Usually if you show backtraces with RUSTC_BACKTRACE=1 it’ll try to filter out the unnecessary frames. Setting RUSTC_BACKTRACE=full shows everything. The filtering is done as a best-effort basis though, so your backtraces may contain some noise.

I made a trivial program on the playground and you can see using full instead of 1 shows one or two more frames.


#3

Would compiling std locally help me get those references? Where would I read how to do that and install the resulting binary so that I can get those links pointing at correct locations? Is compiling STD even feasible on my machine? I hear rust compiler is really really slow - I haven’t tried it on anything but toy examples tbh.


#4

Those paths, while cryptic, aren’t magic – that’s the location where the Rust sources were checked out on the build machine that produced your toolchain. Building a toolchain locally would fix the reports.

However, you have some other options. If you’re using GDB, you can use set substitute-path /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858 /path/to/rustup/sources – replace that last path with the actual path to your sources. On my machine it’s ~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/, and you can get the first part of that by running rustc --print sysroot.

If you’re just reading the paths, remove the first /rustc/<big-scary-hash> and look up the rest of the path in that same location.

I think this behavior is a poor default for the toolchain, but it’s what we have right now.