From 8d500572fa8f4110033fa3bc5e925831f6bbd18e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 14 Dec 2018 16:47:18 -0800 Subject: [PATCH] std: Use backtrace-sys from crates.io This commit switches the standard library to using the `backtrace-sys` crate from crates.io instead of duplicating the logic here in the Rust repositor with the `backtrace-sys`'s crate's logic. Eventually this will hopefully be a good step towards using the `backtrace` crate directly from crates.io itself, but we're not quite there yet! Hopefully this is a small incremental first step we can take. --- .gitmodules | 3 - Cargo.lock | 10 +-- src/bootstrap/dist.rs | 1 - src/libbacktrace | 1 - src/libstd/Cargo.toml | 4 +- src/libstd/build.rs | 76 ----------------------- src/libstd/lib.rs | 3 + src/libstd/sys_common/backtrace.rs | 8 +++ src/libstd/sys_common/gnu/libbacktrace.rs | 72 +++++++-------------- 9 files changed, 40 insertions(+), 138 deletions(-) delete mode 160000 src/libbacktrace diff --git a/.gitmodules b/.gitmodules index 70164d48a307b..c4763612dbf3c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -41,9 +41,6 @@ [submodule "src/tools/lld"] path = src/tools/lld url = https://github.com/rust-lang/lld.git -[submodule "src/libbacktrace"] - path = src/libbacktrace - url = https://github.com/rust-lang-nursery/libbacktrace.git [submodule "src/tools/lldb"] path = src/tools/lldb url = https://github.com/rust-lang-nursery/lldb.git diff --git a/Cargo.lock b/Cargo.lock index ff289ab163f84..1337318bad74d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,7 +85,7 @@ name = "backtrace" version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "backtrace-sys 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)", + "backtrace-sys 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -94,11 +94,13 @@ dependencies = [ [[package]] name = "backtrace-sys" -version = "0.1.24" +version = "0.1.27" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", + "compiler_builtins 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-std-workspace-core 1.0.0", ] [[package]] @@ -2887,7 +2889,7 @@ name = "std" version = "0.0.0" dependencies = [ "alloc 0.0.0", - "build_helper 0.1.0", + "backtrace-sys 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", "compiler_builtins 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "core 0.0.0", @@ -3388,7 +3390,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum assert_cli 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "98589b0e465a6c510d95fceebd365bb79bedece7f6e18a480897f2015f85ec51" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum backtrace 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "18b65ea1161bfb2dd6da6fade5edd4dbd08fba85012123dd333d2fd1b90b2782" -"checksum backtrace-sys 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)" = "c66d56ac8dabd07f6aacdaf633f4b8262f5b3601a810a0dcddffd5c22c69daa0" +"checksum backtrace-sys 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)" = "6ea90dd7b012b3d1a2cb6bec16670a0db2c95d4e931e84f4047e0460c1b34c8d" "checksum bit-set 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f1efcc46c18245a69c38fcc5cc650f16d3a59d034f3106e9ed63748f695730a" "checksum bit-vec 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4440d5cb623bb7390ae27fec0bb6c61111969860f8e3ae198bfa0663645e67cf" "checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5" diff --git a/src/bootstrap/dist.rs b/src/bootstrap/dist.rs index 55ec4af0382b0..e38a1d295453a 100644 --- a/src/bootstrap/dist.rs +++ b/src/bootstrap/dist.rs @@ -858,7 +858,6 @@ impl Step for Src { let std_src_dirs = [ "src/build_helper", "src/liballoc", - "src/libbacktrace", "src/libcore", "src/libpanic_abort", "src/libpanic_unwind", diff --git a/src/libbacktrace b/src/libbacktrace deleted file mode 160000 index f4d02bbdbf8a2..0000000000000 --- a/src/libbacktrace +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f4d02bbdbf8a2c5a31f0801dfef597a86caad9e3 diff --git a/src/libstd/Cargo.toml b/src/libstd/Cargo.toml index 9e3d9af5ba253..5214f365a6476 100644 --- a/src/libstd/Cargo.toml +++ b/src/libstd/Cargo.toml @@ -22,6 +22,7 @@ compiler_builtins = { version = "0.1.1" } profiler_builtins = { path = "../libprofiler_builtins", optional = true } unwind = { path = "../libunwind" } rustc-demangle = { version = "0.1.10", features = ['rustc-dep-of-std'] } +backtrace-sys = { version = "0.1.24", features = ["rustc-dep-of-std"], optional = true } [dev-dependencies] rand = "0.6.1" @@ -44,12 +45,11 @@ fortanix-sgx-abi = { version = "0.3.2", features = ['rustc-dep-of-std'] } [build-dependencies] cc = "1.0" -build_helper = { path = "../build_helper" } [features] default = ["compiler_builtins_c"] -backtrace = [] +backtrace = ["backtrace-sys"] panic-unwind = ["panic_unwind"] profiler = ["profiler_builtins"] compiler_builtins_c = ["compiler_builtins/c"] diff --git a/src/libstd/build.rs b/src/libstd/build.rs index 7143de55c8831..69bd0e41504fa 100644 --- a/src/libstd/build.rs +++ b/src/libstd/build.rs @@ -10,24 +10,12 @@ #![deny(warnings)] -extern crate build_helper; extern crate cc; -use build_helper::native_lib_boilerplate; use std::env; -use std::fs::File; fn main() { let target = env::var("TARGET").expect("TARGET was not set"); - if cfg!(feature = "backtrace") && - !target.contains("cloudabi") && - !target.contains("emscripten") && - !target.contains("msvc") && - !target.contains("wasm32") - { - let _ = build_libbacktrace(&target); - } - if target.contains("linux") { if target.contains("android") { println!("cargo:rustc-link-lib=dl"); @@ -79,67 +67,3 @@ fn main() { println!("cargo:rustc-link-lib=compiler_rt"); } } - -fn build_libbacktrace(target: &str) -> Result<(), ()> { - let native = native_lib_boilerplate( - "../libbacktrace".as_ref(), - "libbacktrace", - "backtrace", - "", - )?; - - let mut build = cc::Build::new(); - build - .flag("-fvisibility=hidden") - .include("../libbacktrace") - .include(&native.out_dir) - .out_dir(&native.out_dir) - .warnings(false) - .file("../libbacktrace/alloc.c") - .file("../libbacktrace/backtrace.c") - .file("../libbacktrace/dwarf.c") - .file("../libbacktrace/fileline.c") - .file("../libbacktrace/posix.c") - .file("../libbacktrace/read.c") - .file("../libbacktrace/sort.c") - .file("../libbacktrace/state.c"); - - let any_debug = env::var("RUSTC_DEBUGINFO").unwrap_or_default() == "true" || - env::var("RUSTC_DEBUGINFO_LINES").unwrap_or_default() == "true"; - build.debug(any_debug); - - if target.contains("darwin") { - build.file("../libbacktrace/macho.c"); - } else if target.contains("windows") { - build.file("../libbacktrace/pecoff.c"); - } else { - build.file("../libbacktrace/elf.c"); - - let pointer_width = env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap(); - if pointer_width == "64" { - build.define("BACKTRACE_ELF_SIZE", "64"); - } else { - build.define("BACKTRACE_ELF_SIZE", "32"); - } - } - - File::create(native.out_dir.join("backtrace-supported.h")).unwrap(); - build.define("BACKTRACE_SUPPORTED", "1"); - build.define("BACKTRACE_USES_MALLOC", "1"); - build.define("BACKTRACE_SUPPORTS_THREADS", "0"); - build.define("BACKTRACE_SUPPORTS_DATA", "0"); - - File::create(native.out_dir.join("config.h")).unwrap(); - if !target.contains("apple-ios") && - !target.contains("solaris") && - !target.contains("redox") && - !target.contains("android") && - !target.contains("haiku") { - build.define("HAVE_DL_ITERATE_PHDR", "1"); - } - build.define("_GNU_SOURCE", "1"); - build.define("_LARGE_FILES", "1"); - - build.compile("backtrace"); - Ok(()) -} diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 4a693bffddfc1..af5d511c035ee 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -346,6 +346,9 @@ extern crate rustc_demangle; #[allow(unused_extern_crates)] extern crate unwind; +#[cfg(feature = "backtrace")] +extern crate backtrace_sys; + // During testing, this crate is not actually the "real" std library, but rather // it links to the real std library, which was compiled from this same source // code. So any lang items std defines are conditionally excluded (or else they diff --git a/src/libstd/sys_common/backtrace.rs b/src/libstd/sys_common/backtrace.rs index e44113f76f40e..08cd7b05e0755 100644 --- a/src/libstd/sys_common/backtrace.rs +++ b/src/libstd/sys_common/backtrace.rs @@ -53,6 +53,14 @@ const MAX_NB_FRAMES: usize = 100; pub fn print(w: &mut dyn Write, format: PrintFormat) -> io::Result<()> { static LOCK: Mutex = Mutex::new(); + // There are issues currently linking libbacktrace into tests, and in + // general during libstd's own unit tests we're not testing this path. In + // test mode immediately return here to optimize away any references to the + // libbacktrace symbols + if cfg!(test) { + return Ok(()) + } + // Use a lock to prevent mixed output in multithreading context. // Some platforms also requires it, like `SymFromAddr` on Windows. unsafe { diff --git a/src/libstd/sys_common/gnu/libbacktrace.rs b/src/libstd/sys_common/gnu/libbacktrace.rs index c2589d477ee0d..9321242fec80e 100644 --- a/src/libstd/sys_common/gnu/libbacktrace.rs +++ b/src/libstd/sys_common/gnu/libbacktrace.rs @@ -9,6 +9,7 @@ // except according to those terms. use libc; +use backtrace_sys::{self, backtrace_state}; use ffi::CStr; use io; @@ -39,11 +40,13 @@ where F: FnMut(&[u8], u32) -> io::Result<()> let mut fileline_win: &mut [FileLine] = &mut fileline_buf; let fileline_addr = &mut fileline_win as *mut &mut [FileLine]; ret = unsafe { - backtrace_pcinfo(state, - frame.exact_position as libc::uintptr_t, - pcinfo_cb, - error_cb, - fileline_addr as *mut libc::c_void) + backtrace_sys::backtrace_pcinfo( + state, + frame.exact_position as libc::uintptr_t, + pcinfo_cb, + error_cb, + fileline_addr as *mut libc::c_void, + ) }; FILELINE_SIZE - fileline_win.len() }; @@ -76,11 +79,13 @@ pub fn resolve_symname(frame: Frame, let mut data: *const libc::c_char = ptr::null(); let data_addr = &mut data as *mut *const libc::c_char; let ret = unsafe { - backtrace_syminfo(state, - frame.symbol_addr as libc::uintptr_t, - syminfo_cb, - error_cb, - data_addr as *mut libc::c_void) + backtrace_sys::backtrace_syminfo( + state, + frame.symbol_addr as libc::uintptr_t, + syminfo_cb, + error_cb, + data_addr as *mut libc::c_void, + ) }; if ret == 0 || data.is_null() { None @@ -93,45 +98,6 @@ pub fn resolve_symname(frame: Frame, callback(symname) } -//////////////////////////////////////////////////////////////////////// -// libbacktrace.h API -//////////////////////////////////////////////////////////////////////// -type backtrace_syminfo_callback = -extern "C" fn(data: *mut libc::c_void, - pc: libc::uintptr_t, - symname: *const libc::c_char, - symval: libc::uintptr_t, - symsize: libc::uintptr_t); -type backtrace_full_callback = -extern "C" fn(data: *mut libc::c_void, - pc: libc::uintptr_t, - filename: *const libc::c_char, - lineno: libc::c_int, - function: *const libc::c_char) -> libc::c_int; -type backtrace_error_callback = -extern "C" fn(data: *mut libc::c_void, - msg: *const libc::c_char, - errnum: libc::c_int); -enum backtrace_state {} - -extern { - fn backtrace_create_state(filename: *const libc::c_char, - threaded: libc::c_int, - error: backtrace_error_callback, - data: *mut libc::c_void) - -> *mut backtrace_state; - fn backtrace_syminfo(state: *mut backtrace_state, - addr: libc::uintptr_t, - cb: backtrace_syminfo_callback, - error: backtrace_error_callback, - data: *mut libc::c_void) -> libc::c_int; - fn backtrace_pcinfo(state: *mut backtrace_state, - addr: libc::uintptr_t, - cb: backtrace_full_callback, - error: backtrace_error_callback, - data: *mut libc::c_void) -> libc::c_int; -} - //////////////////////////////////////////////////////////////////////// // helper callbacks //////////////////////////////////////////////////////////////////////// @@ -210,7 +176,11 @@ unsafe fn init_state() -> *mut backtrace_state { Err(_) => ptr::null(), }; - STATE = backtrace_create_state(filename, 0, error_cb, - ptr::null_mut()); + STATE = backtrace_sys::backtrace_create_state( + filename, + 0, + error_cb, + ptr::null_mut(), + ); STATE }