Skip to content

Adding a method to clear the Gimli symbol cache #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ mod symbolize;
pub use crate::types::BytesOrWideString;
mod types;

#[cfg(feature = "std")]
pub use crate::symbolize::clear_symbol_cache;

cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
pub use crate::backtrace::trace;
Expand Down
22 changes: 14 additions & 8 deletions src/symbolize/gimli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::symbolize::ResolveWhat;
use crate::types::BytesOrWideString;
use crate::SymbolName;
use addr2line::gimli;
use core::cell::RefCell;
use core::convert::TryFrom;
use core::mem;
use core::u32;
Expand Down Expand Up @@ -329,7 +328,10 @@ impl Mapping {
}
}

thread_local! {
type Cache = Vec<(PathBuf, Mapping)>;

// unsafe because this is required to be externally synchronized
unsafe fn with_cache(f: impl FnOnce(&mut Cache)) {
// A very small, very simple LRU cache for debug info mappings.
//
// The hit rate should be very high, since the typical stack doesn't cross
Expand All @@ -340,17 +342,21 @@ thread_local! {
// leverage the structures built when constructing `addr2line::Context`s to
// get nice speedups. If we didn't have this cache, that amortization would
// never happen, and symbolicating backtraces would be ssssllllooooowwww.
static MAPPINGS_CACHE: RefCell<Vec<(PathBuf, Mapping)>>
= RefCell::new(Vec::with_capacity(MAPPINGS_CACHE_SIZE));
static mut MAPPINGS_CACHE: Option<Cache> = None;

f(MAPPINGS_CACHE.get_or_insert_with(|| Vec::with_capacity(MAPPINGS_CACHE_SIZE)))
}

fn with_mapping_for_path<F>(path: PathBuf, f: F)
// unsafe because this is required to be externally synchronized
pub unsafe fn clear_symbol_cache() {
with_cache(|cache| cache.clear());
}

unsafe fn with_mapping_for_path<F>(path: PathBuf, f: F)
where
F: FnMut(&Context<'_>),
{
MAPPINGS_CACHE.with(|cache| {
let mut cache = cache.borrow_mut();

with_cache(|cache| {
let idx = cache.iter().position(|&(ref p, _)| p == &path);

// Invariant: after this conditional completes without early returning
Expand Down
27 changes: 27 additions & 0 deletions src/symbolize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,35 @@ cfg_if::cfg_if! {
}
}

/// Attempt to reclaim that cached memory used to symbolicate addresses.
///
/// This method will attempt to release any global data structures that have
/// otherwise been cached globally or in the thread which typically represent
/// parsed DWARF information or similar.
///
/// # Caveats
///
/// While this function is always available it doesn't actually do anything on
/// most implementations. Libraries like dbghelp or libbacktrace do not provide
/// facilities to deallocate state and manage the allocated memory. For now the
/// `gimli-symbolize` feature of this crate is the only feature where this
/// function has any effect.
#[cfg(feature = "std")]
pub fn clear_symbol_cache() {
let _guard = crate::lock::lock();
unsafe {
clear_symbol_cache_imp();
}
}

mod dladdr;

cfg_if::cfg_if! {
if #[cfg(all(windows, target_env = "msvc", feature = "dbghelp"))] {
mod dbghelp;
use self::dbghelp::resolve as resolve_imp;
use self::dbghelp::Symbol as SymbolImp;
unsafe fn clear_symbol_cache_imp() {}
} else if #[cfg(all(
feature = "std",
feature = "gimli-symbolize",
Expand All @@ -453,6 +475,7 @@ cfg_if::cfg_if! {
mod gimli;
use self::gimli::resolve as resolve_imp;
use self::gimli::Symbol as SymbolImp;
use self::gimli::clear_symbol_cache as clear_symbol_cache_imp;
// Note that we only enable coresymbolication on iOS when debug assertions
// are enabled because it's helpful in debug mode but it looks like apps get
// rejected from the app store if they use this API, see #92 for more info
Expand All @@ -462,22 +485,26 @@ cfg_if::cfg_if! {
mod coresymbolication;
use self::coresymbolication::resolve as resolve_imp;
use self::coresymbolication::Symbol as SymbolImp;
unsafe fn clear_symbol_cache_imp() {}
} else if #[cfg(all(feature = "libbacktrace",
any(unix, all(windows, not(target_vendor = "uwp"), target_env = "gnu")),
not(target_os = "fuchsia"),
not(target_os = "emscripten")))] {
mod libbacktrace;
use self::libbacktrace::resolve as resolve_imp;
use self::libbacktrace::Symbol as SymbolImp;
unsafe fn clear_symbol_cache_imp() {}
} else if #[cfg(all(unix,
not(target_os = "emscripten"),
feature = "dladdr"))] {
mod dladdr_resolve;
use self::dladdr_resolve::resolve as resolve_imp;
use self::dladdr_resolve::Symbol as SymbolImp;
unsafe fn clear_symbol_cache_imp() {}
} else {
mod noop;
use self::noop::resolve as resolve_imp;
use self::noop::Symbol as SymbolImp;
unsafe fn clear_symbol_cache_imp() {}
}
}