Skip to content

Commit

Permalink
Auto merge of #35051 - japaric:backtrace, r=alexcrichton
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors authored Jul 30, 2016
2 parents 2ad98a0 + 774fbdf commit 7580534
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub struct Config {
// libstd features
pub debug_jemalloc: bool,
pub use_jemalloc: bool,
pub backtrace: bool, // support for RUST_BACKTRACE

// misc
pub channel: String,
Expand Down Expand Up @@ -134,6 +135,7 @@ struct Rust {
debuginfo: Option<bool>,
debug_jemalloc: Option<bool>,
use_jemalloc: Option<bool>,
backtrace: Option<bool>,
default_linker: Option<String>,
default_ar: Option<String>,
channel: Option<String>,
Expand All @@ -158,6 +160,7 @@ impl Config {
let mut config = Config::default();
config.llvm_optimize = true;
config.use_jemalloc = true;
config.backtrace = true;
config.rust_optimize = true;
config.rust_optimize_tests = true;
config.submodules = true;
Expand Down Expand Up @@ -230,6 +233,7 @@ impl Config {
set(&mut config.rust_rpath, rust.rpath);
set(&mut config.debug_jemalloc, rust.debug_jemalloc);
set(&mut config.use_jemalloc, rust.use_jemalloc);
set(&mut config.backtrace, rust.backtrace);
set(&mut config.channel, rust.channel.clone());
config.rustc_default_linker = rust.default_linker.clone();
config.rustc_default_ar = rust.default_ar.clone();
Expand Down
3 changes: 3 additions & 0 deletions src/bootstrap/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
# Whether or not jemalloc is built with its debug option set
#debug-jemalloc = false

# Whether or not `panic!`s generate backtraces (RUST_BACKTRACE)
#backtrace = true

# The default linker that will be used by the generated compiler. Note that this
# is not the linker used to link said compiler.
#default-linker = "cc"
Expand Down
3 changes: 3 additions & 0 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,9 @@ impl Build {
if self.config.use_jemalloc {
features.push_str(" jemalloc");
}
if self.config.backtrace {
features.push_str(" backtrace");
}
return features
}

Expand Down
1 change: 1 addition & 0 deletions src/libstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ build_helper = { path = "../build_helper" }
gcc = "0.3"

[features]
backtrace = []
jemalloc = ["alloc_jemalloc"]
debug-jemalloc = ["alloc_jemalloc/debug"]
3 changes: 2 additions & 1 deletion src/libstd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ fn main() {

let target = env::var("TARGET").unwrap();
let host = env::var("HOST").unwrap();
if !target.contains("apple") && !target.contains("msvc") && !target.contains("emscripten"){
if cfg!(feature = "backtrace") && !target.contains("apple") && !target.contains("msvc") &&
!target.contains("emscripten") {
build_libbacktrace(&host, &target);
}

Expand Down
28 changes: 19 additions & 9 deletions src/libstd/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ use intrinsics;
use mem;
use raw;
use sys_common::rwlock::RWLock;
use sync::atomic::{AtomicBool, Ordering};
use sys::stdio::Stderr;
use sys_common::backtrace;
use sys_common::thread_info;
use sys_common::util;
use thread;
Expand Down Expand Up @@ -71,7 +69,6 @@ enum Hook {

static HOOK_LOCK: RWLock = RWLock::new();
static mut HOOK: Hook = Hook::Default;
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);

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

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

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

panics >= 2 || backtrace::log_enabled()
};

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

if log_backtrace {
let _ = backtrace::write(err);
} else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) {
let _ = writeln!(err, "note: Run with `RUST_BACKTRACE=1` for a backtrace.");
#[cfg(any(not(cargobuild), feature = "backtrace"))]
{
use sync::atomic::{AtomicBool, Ordering};

static FIRST_PANIC: AtomicBool = AtomicBool::new(true);

if log_backtrace {
let _ = backtrace::write(err);
} else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) {
let _ = writeln!(err, "note: Run with `RUST_BACKTRACE=1` for a backtrace.");
}
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ macro_rules! rtassert {

pub mod args;
pub mod at_exit_imp;
#[cfg(any(not(cargobuild), feature = "backtrace"))]
pub mod backtrace;
pub mod condvar;
pub mod io;
Expand All @@ -42,6 +43,7 @@ pub mod thread_local;
pub mod util;
pub mod wtf8;

#[cfg(any(not(cargobuild), feature = "backtrace"))]
#[cfg(any(all(unix, not(any(target_os = "macos", target_os = "ios", target_os = "emscripten"))),
all(windows, target_env = "gnu")))]
pub mod gnu;
Expand Down
1 change: 1 addition & 0 deletions src/libstd/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use libc;
pub mod weak;

pub mod android;
#[cfg(any(not(cargobuild), feature = "backtrace"))]
pub mod backtrace;
pub mod condvar;
pub mod ext;
Expand Down
1 change: 1 addition & 0 deletions src/rustc/std_shim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ std = { path = "../../libstd" }
[features]
jemalloc = ["std/jemalloc"]
debug-jemalloc = ["std/debug-jemalloc"]
backtrace = ["std/backtrace"]

0 comments on commit 7580534

Please sign in to comment.