Skip to content

Commit 7580534

Browse files
authored
Auto merge of #35051 - japaric:backtrace, r=alexcrichton
rustbuild: make backtraces (RUST_BACKTRACE) optional but keep them enabled by default to maintain the status quo. When disabled shaves ~56KB off every x86_64-unknown-linux-gnu binary. To disable backtraces you have to use a config.toml (see src/bootstrap/config.toml.example for details) when building rustc/std: $ python bootstrap.py --config=config.toml --- r? @alexcrichton cc rust-lang/rfcs#1417
2 parents 2ad98a0 + 774fbdf commit 7580534

File tree

9 files changed

+36
-10
lines changed

9 files changed

+36
-10
lines changed

src/bootstrap/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub struct Config {
7272
// libstd features
7373
pub debug_jemalloc: bool,
7474
pub use_jemalloc: bool,
75+
pub backtrace: bool, // support for RUST_BACKTRACE
7576

7677
// misc
7778
pub channel: String,
@@ -134,6 +135,7 @@ struct Rust {
134135
debuginfo: Option<bool>,
135136
debug_jemalloc: Option<bool>,
136137
use_jemalloc: Option<bool>,
138+
backtrace: Option<bool>,
137139
default_linker: Option<String>,
138140
default_ar: Option<String>,
139141
channel: Option<String>,
@@ -158,6 +160,7 @@ impl Config {
158160
let mut config = Config::default();
159161
config.llvm_optimize = true;
160162
config.use_jemalloc = true;
163+
config.backtrace = true;
161164
config.rust_optimize = true;
162165
config.rust_optimize_tests = true;
163166
config.submodules = true;
@@ -230,6 +233,7 @@ impl Config {
230233
set(&mut config.rust_rpath, rust.rpath);
231234
set(&mut config.debug_jemalloc, rust.debug_jemalloc);
232235
set(&mut config.use_jemalloc, rust.use_jemalloc);
236+
set(&mut config.backtrace, rust.backtrace);
233237
set(&mut config.channel, rust.channel.clone());
234238
config.rustc_default_linker = rust.default_linker.clone();
235239
config.rustc_default_ar = rust.default_ar.clone();

src/bootstrap/config.toml.example

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
# Whether or not jemalloc is built with its debug option set
100100
#debug-jemalloc = false
101101

102+
# Whether or not `panic!`s generate backtraces (RUST_BACKTRACE)
103+
#backtrace = true
104+
102105
# The default linker that will be used by the generated compiler. Note that this
103106
# is not the linker used to link said compiler.
104107
#default-linker = "cc"

src/bootstrap/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,9 @@ impl Build {
652652
if self.config.use_jemalloc {
653653
features.push_str(" jemalloc");
654654
}
655+
if self.config.backtrace {
656+
features.push_str(" backtrace");
657+
}
655658
return features
656659
}
657660

src/libstd/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ build_helper = { path = "../build_helper" }
2727
gcc = "0.3"
2828

2929
[features]
30+
backtrace = []
3031
jemalloc = ["alloc_jemalloc"]
3132
debug-jemalloc = ["alloc_jemalloc/debug"]

src/libstd/build.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ fn main() {
2525

2626
let target = env::var("TARGET").unwrap();
2727
let host = env::var("HOST").unwrap();
28-
if !target.contains("apple") && !target.contains("msvc") && !target.contains("emscripten"){
28+
if cfg!(feature = "backtrace") && !target.contains("apple") && !target.contains("msvc") &&
29+
!target.contains("emscripten") {
2930
build_libbacktrace(&host, &target);
3031
}
3132

src/libstd/panicking.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ use intrinsics;
2828
use mem;
2929
use raw;
3030
use sys_common::rwlock::RWLock;
31-
use sync::atomic::{AtomicBool, Ordering};
3231
use sys::stdio::Stderr;
33-
use sys_common::backtrace;
3432
use sys_common::thread_info;
3533
use sys_common::util;
3634
use thread;
@@ -71,7 +69,6 @@ enum Hook {
7169

7270
static HOOK_LOCK: RWLock = RWLock::new();
7371
static mut HOOK: Hook = Hook::Default;
74-
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
7572

7673
/// Registers a custom panic hook, replacing any that was previously registered.
7774
///
@@ -183,11 +180,17 @@ impl<'a> Location<'a> {
183180
}
184181

185182
fn default_hook(info: &PanicInfo) {
186-
let panics = PANIC_COUNT.with(|c| c.get());
183+
#[cfg(any(not(cargobuild), feature = "backtrace"))]
184+
use sys_common::backtrace;
187185

188186
// If this is a double panic, make sure that we print a backtrace
189187
// for this panic. Otherwise only print it if logging is enabled.
190-
let log_backtrace = panics >= 2 || backtrace::log_enabled();
188+
#[cfg(any(not(cargobuild), feature = "backtrace"))]
189+
let log_backtrace = {
190+
let panics = PANIC_COUNT.with(|c| c.get());
191+
192+
panics >= 2 || backtrace::log_enabled()
193+
};
191194

192195
let file = info.location.file;
193196
let line = info.location.line;
@@ -207,10 +210,17 @@ fn default_hook(info: &PanicInfo) {
207210
let _ = writeln!(err, "thread '{}' panicked at '{}', {}:{}",
208211
name, msg, file, line);
209212

210-
if log_backtrace {
211-
let _ = backtrace::write(err);
212-
} else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) {
213-
let _ = writeln!(err, "note: Run with `RUST_BACKTRACE=1` for a backtrace.");
213+
#[cfg(any(not(cargobuild), feature = "backtrace"))]
214+
{
215+
use sync::atomic::{AtomicBool, Ordering};
216+
217+
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
218+
219+
if log_backtrace {
220+
let _ = backtrace::write(err);
221+
} else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) {
222+
let _ = writeln!(err, "note: Run with `RUST_BACKTRACE=1` for a backtrace.");
223+
}
214224
}
215225
};
216226

src/libstd/sys/common/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ macro_rules! rtassert {
2828

2929
pub mod args;
3030
pub mod at_exit_imp;
31+
#[cfg(any(not(cargobuild), feature = "backtrace"))]
3132
pub mod backtrace;
3233
pub mod condvar;
3334
pub mod io;
@@ -42,6 +43,7 @@ pub mod thread_local;
4243
pub mod util;
4344
pub mod wtf8;
4445

46+
#[cfg(any(not(cargobuild), feature = "backtrace"))]
4547
#[cfg(any(all(unix, not(any(target_os = "macos", target_os = "ios", target_os = "emscripten"))),
4648
all(windows, target_env = "gnu")))]
4749
pub mod gnu;

src/libstd/sys/unix/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use libc;
3030
pub mod weak;
3131

3232
pub mod android;
33+
#[cfg(any(not(cargobuild), feature = "backtrace"))]
3334
pub mod backtrace;
3435
pub mod condvar;
3536
pub mod ext;

src/rustc/std_shim/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ std = { path = "../../libstd" }
4646
[features]
4747
jemalloc = ["std/jemalloc"]
4848
debug-jemalloc = ["std/debug-jemalloc"]
49+
backtrace = ["std/backtrace"]

0 commit comments

Comments
 (0)