Skip to content

Implement DWARF loading for Gimli on OSX #208

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 1 commit into from
Jun 23, 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: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cpp_demangle = { default-features = false, version = "0.2.3", optional = true }
addr2line = { version = "0.9.0", optional = true }
findshlibs = { version = "0.4.1", optional = true }
memmap = { version = "0.7.0", optional = true }
goblin = { version = "0.0.22", optional = true, default-features = false }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.3", optional = true }
Expand Down Expand Up @@ -92,7 +93,7 @@ kernel32 = []
libbacktrace = ["backtrace-sys"]
dladdr = []
coresymbolication = []
gimli-symbolize = ["addr2line", "findshlibs", "memmap" ]
gimli-symbolize = ["addr2line", "findshlibs", "memmap", "goblin"]

#=======================================
# Methods of serialization
Expand Down
4 changes: 2 additions & 2 deletions ci/azure-test-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ steps:
displayName: "Build backtrace"
- bash: cargo test
displayName: "Test backtrace"
- bash: cargo test --features 'gimli-symbolize'
displayName: "Test backtrace (gimli-symbolize)"
- bash: cargo test --no-default-features
displayName: "Test backtrace (-default)"
- bash: cargo test --no-default-features --features 'std'
Expand Down Expand Up @@ -41,8 +43,6 @@ steps:
displayName: "Test backtrace (-default + serialize-rustc + serialize-serde + std)"
- bash: cargo test --no-default-features --features 'cpp_demangle std'
displayName: "Test backtrace (-default + cpp_demangle + std)"
- bash: cargo test --no-default-features --features 'gimli-symbolize std'
displayName: "Test backtrace (-default + gimli-symbolize + std)"
- bash: cargo test --no-default-features --features 'dbghelp std'
displayName: "Test backtrace (-default + dbghelp + std)"
- bash: cargo test --no-default-features --features 'dbghelp std verify-winapi'
Expand Down
122 changes: 99 additions & 23 deletions src/symbolize/gimli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,128 @@ use crate::types::BytesOrWideString;
use crate::SymbolName;
use addr2line;
use addr2line::object::{self, Object};
use core::cell::RefCell;
use core::mem;
use core::u32;
use findshlibs::{self, Segment, SharedLibrary};
use libc::c_void;
use memmap::Mmap;
use std::cell::RefCell;
use std::env;
use std::fs::File;
use std::mem;
use std::path::{Path, PathBuf};
use std::prelude::v1::*;
use std::u32;

const MAPPINGS_CACHE_SIZE: usize = 4;

type Dwarf = addr2line::Context;
type Symbols<'map> = object::SymbolMap<'map>;

struct Mapping {
dwarf: Dwarf,
dwarf: addr2line::Context,
// 'static lifetime is a lie to hack around lack of support for self-referential structs.
symbols: Symbols<'static>,
_map: Mmap,
}

macro_rules! mk {
(Mapping { $map:expr, $object:expr }) => {{
Mapping {
dwarf: addr2line::Context::new(&$object).ok()?,
// Convert to 'static lifetimes since the symbols should
// only borrow `map` and we're preserving `map` below.
//
// TODO: how do we know that `symbol_map` *only* borrows `map`?
symbols: unsafe { mem::transmute::<Symbols, Symbols<'static>>($object.symbol_map()) },
_map: $map,
}
}};
}

fn mmap(path: &Path) -> Option<Mmap> {
let file = File::open(path).ok()?;
// TODO: not completely safe, see https://github.com/danburkert/memmap-rs/issues/25
unsafe { Mmap::map(&file).ok() }

}

impl Mapping {
fn new(path: &PathBuf) -> Option<Mapping> {
let file = File::open(path).ok()?;
// TODO: not completely safe, see https://github.com/danburkert/memmap-rs/issues/25
let map = unsafe { Mmap::map(&file).ok()? };
let (dwarf, symbols) = {
let object = object::ElfFile::parse(&*map).ok()?;
let dwarf = addr2line::Context::new(&object).ok()?;
let symbols = object.symbol_map();
// Convert to 'static lifetimes.
(dwarf, unsafe { mem::transmute(symbols) })
};
Some(Mapping {
dwarf,
symbols,
_map: map,
})
fn new(path: &Path) -> Option<Mapping> {
if cfg!(target_os = "macos") {
Mapping::new_find_dsym(path)
} else {
let map = mmap(path)?;
let object = object::ElfFile::parse(&map).ok()?;
Some(mk!(Mapping { map, object }))
}
}

fn new_find_dsym(path: &Path) -> Option<Mapping> {
// First up we need to load the unique UUID which is stored in the macho
// header of the file we're reading, specified at `path`.
let map = mmap(path)?;
let object = object::MachOFile::parse(&map).ok()?;
let uuid = get_uuid(&object)?;

// Next we need to look for a `*.dSYM` file. For now we just probe the
// containing directory and look around for something that matches
// `*.dSYM`. Once it's found we root through the dwarf resources that it
// contains and try to find a macho file which has a matching UUID as
// the one of our own file. If we find a match that's the dwarf file we
// want to return.
let parent = path.parent()?;
for entry in parent.read_dir().ok()? {
let entry = entry.ok()?;
let filename = match entry.file_name().into_string() {
Ok(name) => name,
Err(_) => continue,
};
if !filename.ends_with(".dSYM") {
continue;
}
let candidates = entry.path().join("Contents/Resources/DWARF");
if let Some(mapping) = load_dsym(&candidates, &uuid) {
return Some(mapping);
}
}

// Looks like nothing matched our UUID, so let's at least return our own
// file. This should have the symbol table for at least some
// symbolication purposes.
return Some(mk!(Mapping { map, object }));

fn load_dsym(dir: &Path, uuid: &[u8]) -> Option<Mapping> {
for entry in dir.read_dir().ok()? {
let entry = entry.ok()?;
let map = mmap(&entry.path())?;
let object = object::MachOFile::parse(&map).ok()?;
let entry_uuid = get_uuid(&object)?;
if &entry_uuid[..] != uuid {
continue;
}
return Some(mk!(Mapping { map, object }));
}

None
}

fn get_uuid(object: &object::MachOFile) -> Option<[u8; 16]> {
use goblin::mach::load_command::CommandVariant;

object
.macho()
.load_commands
.iter()
.filter_map(|cmd| match cmd.command {
CommandVariant::Uuid(u) => Some(u.uuid),
_ => None,
})
.next()
}
}

// Ensure the 'static lifetimes don't leak.
fn rent<F>(&self, mut f: F)
where
F: FnMut(&Dwarf, &Symbols),
F: FnMut(&addr2line::Context, &Symbols),
{
f(&self.dwarf, &self.symbols)
}
Expand All @@ -77,7 +153,7 @@ thread_local! {

fn with_mapping_for_path<F>(path: PathBuf, f: F)
where
F: FnMut(&Dwarf, &Symbols),
F: FnMut(&addr2line::Context, &Symbols),
{
MAPPINGS_CACHE.with(|cache| {
let mut cache = cache.borrow_mut();
Expand Down
11 changes: 8 additions & 3 deletions src/symbolize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,14 @@ cfg_if::cfg_if! {
mod dbghelp;
use self::dbghelp::resolve as resolve_imp;
use self::dbghelp::Symbol as SymbolImp;
} else if #[cfg(all(feature = "std",
feature = "gimli-symbolize",
target_os = "linux"))] {
} else if #[cfg(all(
feature = "std",
feature = "gimli-symbolize",
any(
target_os = "linux",
target_os = "macos",
),
))] {
mod gimli;
use self::gimli::resolve as resolve_imp;
use self::gimli::Symbol as SymbolImp;
Expand Down