backtrace-rs icon indicating copy to clipboard operation
backtrace-rs copied to clipboard

gimli/libbacktrace don't handle dynamically loaded libraries

Open alexcrichton opened this issue 5 years ago • 7 comments

Currently iteration over all dynamic objects happens once in libbacktrace/gimli (gimli for sure, libbacktrace is a best approximation). This means that if a backtrace is generated and then afterwards a dynamic object is loaded (e.g. via dlopen) the backtraces through that new dynamic object may be missing symbol information. We'll likely need to add a fallback in gimli where if a symbol doesn't hit the cache of libraries we know about we check again to see if any new libraries have been opened.

alexcrichton avatar Aug 08 '19 14:08 alexcrichton

Could this explain backtraces in dynamically loaded libraries becoming <unknown> from Rust 1.37 onwards? I also saw that https://github.com/rust-lang/rust/pull/57967 was causing a similar issue in >= 1.37 (https://github.com/rust-lang/rust/issues/61416), but that appears to have been fixed.

t-mw avatar Oct 30 '19 08:10 t-mw

I don't believe this is related to gimli because gimli isn't used in libstd. That being said that still sounds like a regression that's likely related to this crate! (just separate)

alexcrichton avatar Oct 30 '19 14:10 alexcrichton

Thanks, I've filed a separate issue at https://github.com/rust-lang/rust/issues/67599.

t-mw avatar Dec 27 '19 10:12 t-mw

Assuming this is still a problem, would calling backtrace::clear_symbol_cache() after every dlopen() be an effective workaround?

itamarst avatar May 10 '22 20:05 itamarst

At least on 0.36:

  1. this is still a problem
  2. calling clear_symbol_cache() doesn't clear cache.libraries, only cache.mappings. So if you load a new library, you won't get any new symbols.

I'll put up a PR for adding the ability to reload the library cache shortly.

@alexcrichton do you have a preference for rolling it into the existing clear_symbol_cache function? or a new function (e.g. reload_native_library_cache)

jmrgibson avatar Jun 07 '22 21:06 jmrgibson

Given that cache.mappings has indexes into cache.libraries, I think I'll make a new function that clears both cache.libraries AND cache.mappings.

jmrgibson avatar Jun 09 '22 20:06 jmrgibson

We'll likely need to add a fallback in gimli where if a symbol doesn't hit the cache of libraries we know about we check again to see if any new libraries have been opened.

I think this is a better solution that requiring users to call a function to clear the cache.

bjorn3 avatar Jun 09 '22 20:06 bjorn3