Skip to content

Commit 2a6a342

Browse files
authored
Rollup merge of #64444 - RalfJung:no-backtrace, r=alexcrichton
fix building libstd without backtrace feature Fixes #64410 r? @alexcrichton
2 parents 63bc6ae + 49854c4 commit 2a6a342

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
lines changed

src/libstd/Cargo.toml

+11-10
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,11 @@ profiler_builtins = { path = "../libprofiler_builtins", optional = true }
2525
unwind = { path = "../libunwind" }
2626
hashbrown = { version = "0.5.0", features = ['rustc-dep-of-std'] }
2727

28-
[dependencies.backtrace]
28+
[dependencies.backtrace_rs]
29+
package = "backtrace"
2930
version = "0.3.37"
30-
default-features = false # don't use coresymbolication on OSX
31-
features = [
32-
"rustc-dep-of-std", # enable build support for integrating into libstd
33-
"dbghelp", # backtrace/symbolize on MSVC
34-
"libbacktrace", # symbolize on most platforms
35-
"libunwind", # backtrace on most platforms
36-
"dladdr", # symbolize on platforms w/o libbacktrace
37-
]
38-
optional = true
31+
default-features = false # without the libstd `backtrace` feature, stub out everything
32+
features = [ "rustc-dep-of-std" ] # enable build support for integrating into libstd
3933

4034
[dev-dependencies]
4135
rand = "0.7"
@@ -65,6 +59,13 @@ cc = "1.0"
6559
[features]
6660
default = ["std_detect_file_io", "std_detect_dlsym_getauxval"]
6761

62+
backtrace = [
63+
"backtrace_rs/dbghelp", # backtrace/symbolize on MSVC
64+
"backtrace_rs/libbacktrace", # symbolize on most platforms
65+
"backtrace_rs/libunwind", # backtrace on most platforms
66+
"backtrace_rs/dladdr", # symbolize on platforms w/o libbacktrace
67+
]
68+
6869
panic-unwind = ["panic_unwind"]
6970
profiler = ["profiler_builtins"]
7071
compiler-builtins-c = ["alloc/compiler-builtins-c"]

src/libstd/backtrace.rs

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ use crate::sync::atomic::{AtomicUsize, Ordering::SeqCst};
9797
use crate::sync::Mutex;
9898
use crate::sys_common::backtrace::{output_filename, lock};
9999
use crate::vec::Vec;
100+
use backtrace_rs as backtrace;
100101
use backtrace::BytesOrWideString;
101102

102103
/// A captured OS thread stack backtrace.

src/libstd/panicking.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use crate::ptr;
1717
use crate::raw;
1818
use crate::sys::stdio::panic_output;
1919
use crate::sys_common::rwlock::RWLock;
20-
use crate::sys_common::thread_info;
21-
use crate::sys_common::util;
20+
use crate::sys_common::{thread_info, util, backtrace};
2221
use crate::thread;
2322

2423
#[cfg(not(test))]
@@ -157,20 +156,18 @@ pub fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send> {
157156
}
158157

159158
fn default_hook(info: &PanicInfo<'_>) {
160-
#[cfg(feature = "backtrace")]
161-
use crate::sys_common::{backtrace as backtrace_mod};
162-
163159
// If this is a double panic, make sure that we print a backtrace
164160
// for this panic. Otherwise only print it if logging is enabled.
165-
#[cfg(feature = "backtrace")]
166-
let log_backtrace = {
161+
let log_backtrace = if cfg!(feature = "backtrace") {
167162
let panics = update_panic_count(0);
168163

169164
if panics >= 2 {
170-
Some(backtrace::PrintFmt::Full)
165+
Some(backtrace_rs::PrintFmt::Full)
171166
} else {
172-
backtrace_mod::log_enabled()
167+
backtrace::log_enabled()
173168
}
169+
} else {
170+
None
174171
};
175172

176173
// The current implementation always returns `Some`.
@@ -190,14 +187,13 @@ fn default_hook(info: &PanicInfo<'_>) {
190187
let _ = writeln!(err, "thread '{}' panicked at '{}', {}",
191188
name, msg, location);
192189

193-
#[cfg(feature = "backtrace")]
194-
{
190+
if cfg!(feature = "backtrace") {
195191
use crate::sync::atomic::{AtomicBool, Ordering};
196192

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

199195
if let Some(format) = log_backtrace {
200-
let _ = backtrace_mod::print(err, format);
196+
let _ = backtrace::print(err, format);
201197
} else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) {
202198
let _ = writeln!(err, "note: run with `RUST_BACKTRACE=1` \
203199
environment variable to display a backtrace.");

src/libstd/sys_common/backtrace.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ use crate::io;
77
use crate::borrow::Cow;
88
use crate::io::prelude::*;
99
use crate::path::{self, Path, PathBuf};
10-
use crate::sync::atomic::{self, Ordering};
1110
use crate::sys::mutex::Mutex;
1211

13-
use backtrace::{BacktraceFmt, BytesOrWideString, PrintFmt};
12+
use backtrace_rs::{BacktraceFmt, BytesOrWideString, PrintFmt};
1413

1514
/// Max number of frames to print.
1615
const MAX_NB_FRAMES: usize = 100;
@@ -74,14 +73,14 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::
7473
bt_fmt.add_context()?;
7574
let mut idx = 0;
7675
let mut res = Ok(());
77-
backtrace::trace_unsynchronized(|frame| {
76+
backtrace_rs::trace_unsynchronized(|frame| {
7877
if print_fmt == PrintFmt::Short && idx > MAX_NB_FRAMES {
7978
return false;
8079
}
8180

8281
let mut hit = false;
8382
let mut stop = false;
84-
backtrace::resolve_frame_unsynchronized(frame, |symbol| {
83+
backtrace_rs::resolve_frame_unsynchronized(frame, |symbol| {
8584
hit = true;
8685
if print_fmt == PrintFmt::Short {
8786
if let Some(sym) = symbol.name().and_then(|s| s.as_str()) {
@@ -130,6 +129,8 @@ where
130129
// For now logging is turned off by default, and this function checks to see
131130
// whether the magical environment variable is present to see if it's turned on.
132131
pub fn log_enabled() -> Option<PrintFmt> {
132+
use crate::sync::atomic::{self, Ordering};
133+
133134
// Setting environment variables for Fuchsia components isn't a standard
134135
// or easily supported workflow. For now, always display backtraces.
135136
if cfg!(target_os = "fuchsia") {

src/libstd/sys_common/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ macro_rules! rtunwrap {
4141

4242
pub mod alloc;
4343
pub mod at_exit_imp;
44-
#[cfg(feature = "backtrace")]
4544
pub mod backtrace;
4645
pub mod condvar;
4746
pub mod io;

0 commit comments

Comments
 (0)