Skip to content

Commit

Permalink
enable backtrace for some BSD OSs
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Oct 26, 2020
1 parent 8b8ea53 commit 0686f82
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Expand Up @@ -22,7 +22,6 @@ exclude = ['crates/without_debuginfo', 'crates/macos_frames_test', 'crates/line-
cfg-if = "1.0"
rustc-demangle = "0.1.4"
backtrace-sys = { path = "crates/backtrace-sys", version = "0.1.35", optional = true, default_features = false }
libc = { version = "0.2.45", default-features = false }

# Optionally enable the ability to serialize a `Backtrace`, controlled through
# the `serialize-*` features below.
Expand All @@ -43,6 +42,12 @@ optional = true
default-features = false
features = ['read_core', 'elf', 'macho', 'pe', 'unaligned']

[dependencies.libc]
version = "0.2"
default-features = false
git = "https://github.com/lzutao/rust-libc"
branch = "i78184"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.3", optional = true }

Expand Down
17 changes: 12 additions & 5 deletions src/symbolize/gimli.rs
Expand Up @@ -358,6 +358,10 @@ cfg_if::cfg_if! {
} else if #[cfg(any(
target_os = "linux",
target_os = "fuchsia",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "dragonfly",
))] {
// Other Unix (e.g. Linux) platforms use ELF as an object file format
// and typically implement an API called `dl_iterate_phdr` to load
Expand All @@ -372,28 +376,31 @@ cfg_if::cfg_if! {
fn native_libraries() -> Vec<Library> {
let mut ret = Vec::new();
unsafe {
libc::dl_iterate_phdr(Some(callback), &mut ret as *mut _ as *mut _);
libc::dl_iterate_phdr(Some(callback), ret.as_mut_ptr() as *mut _);
}
return ret;
}

// SAFETY: `info` and `vec` should be valid pointers
unsafe extern "C" fn callback(
info: *mut libc::dl_phdr_info,
_size: libc::size_t,
vec: *mut libc::c_void,
) -> libc::c_int {
let info = &*info;
let libs = &mut *(vec as *mut Vec<Library>);
let name = if (*info).dlpi_name.is_null() || *(*info).dlpi_name == 0{
let is_main_prog = info.dlpi_name.is_null() || info.dlpi_name.read() == 0;
let name = if is_main_prog {
if libs.is_empty() {
mystd::env::current_exe().map(|e| e.into()).unwrap_or_default()
} else {
OsString::new()
}
} else {
let bytes = CStr::from_ptr((*info).dlpi_name).to_bytes();
let bytes = CStr::from_ptr(info.dlpi_name).to_bytes();
OsStr::from_bytes(bytes).to_owned()
};
let headers = core::slice::from_raw_parts((*info).dlpi_phdr, (*info).dlpi_phnum as usize);
let headers = core::slice::from_raw_parts(info.dlpi_phdr, info.dlpi_phnum as usize);
libs.push(Library {
name,
segments: headers
Expand All @@ -403,7 +410,7 @@ cfg_if::cfg_if! {
stated_virtual_memory_address: (*header).p_vaddr as usize,
})
.collect(),
bias: (*info).dlpi_addr as usize,
bias: info.dlpi_addr as usize,
});
0
}
Expand Down

0 comments on commit 0686f82

Please sign in to comment.