Skip to content

Commit ad781a0

Browse files
committed
Auto merge of #56836 - alexcrichton:std-backtrace-sys, r=Mark-Simulacrum
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.
2 parents f960f37 + 8d50057 commit ad781a0

File tree

9 files changed

+40
-138
lines changed

9 files changed

+40
-138
lines changed

.gitmodules

-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
[submodule "src/tools/lld"]
4242
path = src/tools/lld
4343
url = https://github.com/rust-lang/lld.git
44-
[submodule "src/libbacktrace"]
45-
path = src/libbacktrace
46-
url = https://github.com/rust-lang-nursery/libbacktrace.git
4744
[submodule "src/tools/lldb"]
4845
path = src/tools/lldb
4946
url = https://github.com/rust-lang-nursery/lldb.git

Cargo.lock

+6-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ name = "backtrace"
8585
version = "0.3.11"
8686
source = "registry+https://github.com/rust-lang/crates.io-index"
8787
dependencies = [
88-
"backtrace-sys 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)",
88+
"backtrace-sys 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)",
8989
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
9090
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
9191
"rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -94,11 +94,13 @@ dependencies = [
9494

9595
[[package]]
9696
name = "backtrace-sys"
97-
version = "0.1.24"
97+
version = "0.1.27"
9898
source = "registry+https://github.com/rust-lang/crates.io-index"
9999
dependencies = [
100100
"cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
101+
"compiler_builtins 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
101102
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
103+
"rustc-std-workspace-core 1.0.0",
102104
]
103105

104106
[[package]]
@@ -2887,7 +2889,7 @@ name = "std"
28872889
version = "0.0.0"
28882890
dependencies = [
28892891
"alloc 0.0.0",
2890-
"build_helper 0.1.0",
2892+
"backtrace-sys 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)",
28912893
"cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
28922894
"compiler_builtins 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
28932895
"core 0.0.0",
@@ -3388,7 +3390,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
33883390
"checksum assert_cli 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "98589b0e465a6c510d95fceebd365bb79bedece7f6e18a480897f2015f85ec51"
33893391
"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
33903392
"checksum backtrace 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "18b65ea1161bfb2dd6da6fade5edd4dbd08fba85012123dd333d2fd1b90b2782"
3391-
"checksum backtrace-sys 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)" = "c66d56ac8dabd07f6aacdaf633f4b8262f5b3601a810a0dcddffd5c22c69daa0"
3393+
"checksum backtrace-sys 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)" = "6ea90dd7b012b3d1a2cb6bec16670a0db2c95d4e931e84f4047e0460c1b34c8d"
33923394
"checksum bit-set 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f1efcc46c18245a69c38fcc5cc650f16d3a59d034f3106e9ed63748f695730a"
33933395
"checksum bit-vec 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4440d5cb623bb7390ae27fec0bb6c61111969860f8e3ae198bfa0663645e67cf"
33943396
"checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5"

src/bootstrap/dist.rs

-1
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,6 @@ impl Step for Src {
858858
let std_src_dirs = [
859859
"src/build_helper",
860860
"src/liballoc",
861-
"src/libbacktrace",
862861
"src/libcore",
863862
"src/libpanic_abort",
864863
"src/libpanic_unwind",

src/libbacktrace

-1
This file was deleted.

src/libstd/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ compiler_builtins = { version = "0.1.1" }
2222
profiler_builtins = { path = "../libprofiler_builtins", optional = true }
2323
unwind = { path = "../libunwind" }
2424
rustc-demangle = { version = "0.1.10", features = ['rustc-dep-of-std'] }
25+
backtrace-sys = { version = "0.1.24", features = ["rustc-dep-of-std"], optional = true }
2526

2627
[dev-dependencies]
2728
rand = "0.6.1"
@@ -44,12 +45,11 @@ fortanix-sgx-abi = { version = "0.3.2", features = ['rustc-dep-of-std'] }
4445

4546
[build-dependencies]
4647
cc = "1.0"
47-
build_helper = { path = "../build_helper" }
4848

4949
[features]
5050
default = ["compiler_builtins_c"]
5151

52-
backtrace = []
52+
backtrace = ["backtrace-sys"]
5353
panic-unwind = ["panic_unwind"]
5454
profiler = ["profiler_builtins"]
5555
compiler_builtins_c = ["compiler_builtins/c"]

src/libstd/build.rs

-76
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,12 @@
1010

1111
#![deny(warnings)]
1212

13-
extern crate build_helper;
1413
extern crate cc;
1514

16-
use build_helper::native_lib_boilerplate;
1715
use std::env;
18-
use std::fs::File;
1916

2017
fn main() {
2118
let target = env::var("TARGET").expect("TARGET was not set");
22-
if cfg!(feature = "backtrace") &&
23-
!target.contains("cloudabi") &&
24-
!target.contains("emscripten") &&
25-
!target.contains("msvc") &&
26-
!target.contains("wasm32")
27-
{
28-
let _ = build_libbacktrace(&target);
29-
}
30-
3119
if target.contains("linux") {
3220
if target.contains("android") {
3321
println!("cargo:rustc-link-lib=dl");
@@ -79,67 +67,3 @@ fn main() {
7967
println!("cargo:rustc-link-lib=compiler_rt");
8068
}
8169
}
82-
83-
fn build_libbacktrace(target: &str) -> Result<(), ()> {
84-
let native = native_lib_boilerplate(
85-
"../libbacktrace".as_ref(),
86-
"libbacktrace",
87-
"backtrace",
88-
"",
89-
)?;
90-
91-
let mut build = cc::Build::new();
92-
build
93-
.flag("-fvisibility=hidden")
94-
.include("../libbacktrace")
95-
.include(&native.out_dir)
96-
.out_dir(&native.out_dir)
97-
.warnings(false)
98-
.file("../libbacktrace/alloc.c")
99-
.file("../libbacktrace/backtrace.c")
100-
.file("../libbacktrace/dwarf.c")
101-
.file("../libbacktrace/fileline.c")
102-
.file("../libbacktrace/posix.c")
103-
.file("../libbacktrace/read.c")
104-
.file("../libbacktrace/sort.c")
105-
.file("../libbacktrace/state.c");
106-
107-
let any_debug = env::var("RUSTC_DEBUGINFO").unwrap_or_default() == "true" ||
108-
env::var("RUSTC_DEBUGINFO_LINES").unwrap_or_default() == "true";
109-
build.debug(any_debug);
110-
111-
if target.contains("darwin") {
112-
build.file("../libbacktrace/macho.c");
113-
} else if target.contains("windows") {
114-
build.file("../libbacktrace/pecoff.c");
115-
} else {
116-
build.file("../libbacktrace/elf.c");
117-
118-
let pointer_width = env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap();
119-
if pointer_width == "64" {
120-
build.define("BACKTRACE_ELF_SIZE", "64");
121-
} else {
122-
build.define("BACKTRACE_ELF_SIZE", "32");
123-
}
124-
}
125-
126-
File::create(native.out_dir.join("backtrace-supported.h")).unwrap();
127-
build.define("BACKTRACE_SUPPORTED", "1");
128-
build.define("BACKTRACE_USES_MALLOC", "1");
129-
build.define("BACKTRACE_SUPPORTS_THREADS", "0");
130-
build.define("BACKTRACE_SUPPORTS_DATA", "0");
131-
132-
File::create(native.out_dir.join("config.h")).unwrap();
133-
if !target.contains("apple-ios") &&
134-
!target.contains("solaris") &&
135-
!target.contains("redox") &&
136-
!target.contains("android") &&
137-
!target.contains("haiku") {
138-
build.define("HAVE_DL_ITERATE_PHDR", "1");
139-
}
140-
build.define("_GNU_SOURCE", "1");
141-
build.define("_LARGE_FILES", "1");
142-
143-
build.compile("backtrace");
144-
Ok(())
145-
}

src/libstd/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ extern crate rustc_demangle;
346346
#[allow(unused_extern_crates)]
347347
extern crate unwind;
348348

349+
#[cfg(feature = "backtrace")]
350+
extern crate backtrace_sys;
351+
349352
// During testing, this crate is not actually the "real" std library, but rather
350353
// it links to the real std library, which was compiled from this same source
351354
// code. So any lang items std defines are conditionally excluded (or else they

src/libstd/sys_common/backtrace.rs

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ const MAX_NB_FRAMES: usize = 100;
5353
pub fn print(w: &mut dyn Write, format: PrintFormat) -> io::Result<()> {
5454
static LOCK: Mutex = Mutex::new();
5555

56+
// There are issues currently linking libbacktrace into tests, and in
57+
// general during libstd's own unit tests we're not testing this path. In
58+
// test mode immediately return here to optimize away any references to the
59+
// libbacktrace symbols
60+
if cfg!(test) {
61+
return Ok(())
62+
}
63+
5664
// Use a lock to prevent mixed output in multithreading context.
5765
// Some platforms also requires it, like `SymFromAddr` on Windows.
5866
unsafe {

src/libstd/sys_common/gnu/libbacktrace.rs

+21-51
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
use libc;
12+
use backtrace_sys::{self, backtrace_state};
1213

1314
use ffi::CStr;
1415
use io;
@@ -39,11 +40,13 @@ where F: FnMut(&[u8], u32) -> io::Result<()>
3940
let mut fileline_win: &mut [FileLine] = &mut fileline_buf;
4041
let fileline_addr = &mut fileline_win as *mut &mut [FileLine];
4142
ret = unsafe {
42-
backtrace_pcinfo(state,
43-
frame.exact_position as libc::uintptr_t,
44-
pcinfo_cb,
45-
error_cb,
46-
fileline_addr as *mut libc::c_void)
43+
backtrace_sys::backtrace_pcinfo(
44+
state,
45+
frame.exact_position as libc::uintptr_t,
46+
pcinfo_cb,
47+
error_cb,
48+
fileline_addr as *mut libc::c_void,
49+
)
4750
};
4851
FILELINE_SIZE - fileline_win.len()
4952
};
@@ -76,11 +79,13 @@ pub fn resolve_symname<F>(frame: Frame,
7679
let mut data: *const libc::c_char = ptr::null();
7780
let data_addr = &mut data as *mut *const libc::c_char;
7881
let ret = unsafe {
79-
backtrace_syminfo(state,
80-
frame.symbol_addr as libc::uintptr_t,
81-
syminfo_cb,
82-
error_cb,
83-
data_addr as *mut libc::c_void)
82+
backtrace_sys::backtrace_syminfo(
83+
state,
84+
frame.symbol_addr as libc::uintptr_t,
85+
syminfo_cb,
86+
error_cb,
87+
data_addr as *mut libc::c_void,
88+
)
8489
};
8590
if ret == 0 || data.is_null() {
8691
None
@@ -93,45 +98,6 @@ pub fn resolve_symname<F>(frame: Frame,
9398
callback(symname)
9499
}
95100

96-
////////////////////////////////////////////////////////////////////////
97-
// libbacktrace.h API
98-
////////////////////////////////////////////////////////////////////////
99-
type backtrace_syminfo_callback =
100-
extern "C" fn(data: *mut libc::c_void,
101-
pc: libc::uintptr_t,
102-
symname: *const libc::c_char,
103-
symval: libc::uintptr_t,
104-
symsize: libc::uintptr_t);
105-
type backtrace_full_callback =
106-
extern "C" fn(data: *mut libc::c_void,
107-
pc: libc::uintptr_t,
108-
filename: *const libc::c_char,
109-
lineno: libc::c_int,
110-
function: *const libc::c_char) -> libc::c_int;
111-
type backtrace_error_callback =
112-
extern "C" fn(data: *mut libc::c_void,
113-
msg: *const libc::c_char,
114-
errnum: libc::c_int);
115-
enum backtrace_state {}
116-
117-
extern {
118-
fn backtrace_create_state(filename: *const libc::c_char,
119-
threaded: libc::c_int,
120-
error: backtrace_error_callback,
121-
data: *mut libc::c_void)
122-
-> *mut backtrace_state;
123-
fn backtrace_syminfo(state: *mut backtrace_state,
124-
addr: libc::uintptr_t,
125-
cb: backtrace_syminfo_callback,
126-
error: backtrace_error_callback,
127-
data: *mut libc::c_void) -> libc::c_int;
128-
fn backtrace_pcinfo(state: *mut backtrace_state,
129-
addr: libc::uintptr_t,
130-
cb: backtrace_full_callback,
131-
error: backtrace_error_callback,
132-
data: *mut libc::c_void) -> libc::c_int;
133-
}
134-
135101
////////////////////////////////////////////////////////////////////////
136102
// helper callbacks
137103
////////////////////////////////////////////////////////////////////////
@@ -210,7 +176,11 @@ unsafe fn init_state() -> *mut backtrace_state {
210176
Err(_) => ptr::null(),
211177
};
212178

213-
STATE = backtrace_create_state(filename, 0, error_cb,
214-
ptr::null_mut());
179+
STATE = backtrace_sys::backtrace_create_state(
180+
filename,
181+
0,
182+
error_cb,
183+
ptr::null_mut(),
184+
);
215185
STATE
216186
}

0 commit comments

Comments
 (0)