Skip to content

Commit cc5d40c

Browse files
committed
Prepare for this crate to go into libstd
This commit is a preparation of this crate to be included as a submodule into the standard library. I'm not 100% sold on this yet but I'm somewhat convinced that this is going to happen this way. This is progress on #328 and a preview of what it might look like to implement this strategy. Currently I don't plan to merge this to the `master` branch unless it's decided to move forward with this integration strategy of the gimli feature of the backtrace crate.
1 parent 00052ca commit cc5d40c

File tree

21 files changed

+149
-82
lines changed

21 files changed

+149
-82
lines changed

.github/workflows/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ jobs:
8282
if: contains(matrix.os, 'ubuntu')
8383
- run: RUSTFLAGS="-C link-arg=-Wl,--compress-debug-sections=zlib-gnu" cargo test --features gimli-symbolize
8484
if: contains(matrix.os, 'ubuntu')
85+
- run: cargo build --manifest-path crates/as-if-std/Cargo.toml
8586

8687
windows_arm64:
8788
name: Windows AArch64

Cargo.toml

+6-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ autotests = true
1515
edition = "2018"
1616

1717
[workspace]
18-
members = ['crates/cpp_smoke_test']
18+
members = ['crates/cpp_smoke_test', 'crates/as-if-std']
1919
exclude = ['crates/without_debuginfo', 'crates/macos_frames_test', 'crates/line-tables-only']
2020

2121
[dependencies]
@@ -32,14 +32,12 @@ rustc-serialize = { version = "0.3", optional = true }
3232
# Optionally demangle C++ frames' symbols in backtraces.
3333
cpp_demangle = { default-features = false, version = "0.2.3", optional = true }
3434

35-
# Internal dependencies when built as a dependency of libstd, do not use.
36-
core = { version = "1.0.0", optional = true, package = 'rustc-std-workspace-core' }
37-
compiler_builtins = { version = '0.1.2', optional = true }
3835

3936
# Optional dependencies enabled through the `gimli-symbolize` feature, do not
4037
# use these features directly.
4138
addr2line = { version = "0.12.0", optional = true, default-features = false }
42-
miniz_oxide = { version = "0.3.7", optional = true }
39+
# miniz_oxide = { version = "0.3.7", optional = true }
40+
miniz_oxide = { git = 'https://github.com/alexcrichton/miniz_oxide', branch = 'no-std', optional = true }
4341
[dependencies.object]
4442
version = "0.20.0"
4543
optional = true
@@ -105,14 +103,6 @@ verify-winapi = [
105103
'winapi/winbase',
106104
'winapi/winnt',
107105
]
108-
rustc-dep-of-std = [
109-
'backtrace-sys/rustc-dep-of-std',
110-
'cfg-if/rustc-dep-of-std',
111-
'core',
112-
'compiler_builtins',
113-
'libc/rustc-dep-of-std',
114-
'rustc-demangle/rustc-dep-of-std',
115-
]
116106

117107
[[example]]
118108
name = "backtrace"
@@ -144,3 +134,6 @@ edition = '2018'
144134
name = "concurrent-panics"
145135
required-features = ["std"]
146136
harness = false
137+
138+
[patch.crates-io]
139+
miniz_oxide = { git = 'https://github.com/alexcrichton/miniz_oxide', branch = 'no-std' }

ci/run.sh

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
set -ex
44

55
cargo test --target $TARGET
6+
cargo build --target $TARGET --manifest-path crates/as-if-std/Cargo.toml

crates/as-if-std/Cargo.toml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "as-if-std"
3+
version = "0.1.0"
4+
authors = ["Alex Crichton <alex@alexcrichton.com>"]
5+
edition = "2018"
6+
publish = false
7+
8+
[lib]
9+
test = false
10+
doc = false
11+
doctest = false
12+
bench = false
13+
14+
[dependencies]
15+
cfg-if = "0.1.10"
16+
rustc-demangle = "0.1.4"
17+
libc = { version = "0.2.45", default-features = false }
18+
addr2line = { version = "0.12.0", default-features = false }
19+
miniz_oxide = { version = "0.3.7" }
20+
21+
[dependencies.object]
22+
version = "0.20.0"
23+
default-features = false
24+
features = ['read_core', 'elf', 'macho', 'pe', 'unaligned']
25+
26+
[features]
27+
default = ['gimli-symbolize']
28+
gimli-symbolize = []

crates/as-if-std/build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("cargo:rustc-cfg=backtrace_in_libstd");
3+
}

crates/as-if-std/src/lib.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// A crate which builds the `backtrace` crate as-if it's included as a
2+
// submodule into the standard library. We try to set this crate up similarly
3+
// to the standard library itself to minimize the likelihood of issues when
4+
// updating the `backtrace` crate.
5+
6+
#![no_std]
7+
8+
extern crate alloc;
9+
10+
// We want to `pub use std::*` in the root but we don't want `std` available in
11+
// the root namespace, so do this in a funky inner module.
12+
mod __internal {
13+
extern crate std;
14+
pub use std::*;
15+
}
16+
17+
pub use __internal::*;
18+
19+
// This is the magical part which we hope works.
20+
#[path = "../../../src/lib.rs"]
21+
mod the_backtrace_crate;

src/backtrace/dbghelp.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
2222
#![allow(bad_style)]
2323

24-
use crate::dbghelp;
25-
use crate::windows::*;
24+
use super::super::{dbghelp, windows::*};
2625
use core::ffi::c_void;
2726
use core::mem;
2827

src/backtrace/libunwind.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//!
2626
//! This is the default unwinding API for all non-Windows platforms currently.
2727
28+
use super::super::Bomb;
2829
use core::ffi::c_void;
2930

3031
pub enum Frame {
@@ -103,7 +104,7 @@ pub unsafe fn trace(mut cb: &mut dyn FnMut(&super::Frame) -> bool) {
103104
inner: Frame::Raw(ctx),
104105
};
105106

106-
let mut bomb = crate::Bomb { enabled: true };
107+
let mut bomb = Bomb { enabled: true };
107108
let keep_going = cb(&cx);
108109
bomb.enabled = false;
109110

src/dbghelp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
2424
#![allow(non_snake_case)]
2525

26-
use crate::windows::*;
26+
use super::windows::*;
2727
use core::mem;
2828
use core::ptr;
2929

src/lib.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,39 @@
4949
feature(sgx_platform)
5050
)]
5151
#![warn(rust_2018_idioms)]
52+
// When we're building as part of libstd, silence all warnings since they're
53+
// irrelevant as this crate is developed out-of-tree.
54+
#![cfg_attr(backtrace_in_libstd, allow(warnings))]
5255

5356
#[cfg(feature = "std")]
5457
#[macro_use]
5558
extern crate std;
5659

57-
pub use crate::backtrace::{trace_unsynchronized, Frame};
60+
// This is only used for gimli right now, so silence warnings elsewhere.
61+
#[cfg_attr(not(target_os = "linux"), allow(unused_extern_crates))]
62+
extern crate alloc;
63+
64+
pub use self::backtrace::{trace_unsynchronized, Frame};
5865
mod backtrace;
5966

60-
pub use crate::symbolize::resolve_frame_unsynchronized;
61-
pub use crate::symbolize::{resolve_unsynchronized, Symbol, SymbolName};
67+
pub use self::symbolize::resolve_frame_unsynchronized;
68+
pub use self::symbolize::{resolve_unsynchronized, Symbol, SymbolName};
6269
mod symbolize;
6370

64-
pub use crate::types::BytesOrWideString;
71+
pub use self::types::BytesOrWideString;
6572
mod types;
6673

6774
#[cfg(feature = "std")]
68-
pub use crate::symbolize::clear_symbol_cache;
75+
pub use self::symbolize::clear_symbol_cache;
6976

7077
mod print;
7178
pub use print::{BacktraceFmt, BacktraceFrameFmt, PrintFmt};
7279

7380
cfg_if::cfg_if! {
7481
if #[cfg(feature = "std")] {
75-
pub use crate::backtrace::trace;
76-
pub use crate::symbolize::{resolve, resolve_frame};
77-
pub use crate::capture::{Backtrace, BacktraceFrame, BacktraceSymbol};
82+
pub use self::backtrace::trace;
83+
pub use self::symbolize::{resolve, resolve_frame};
84+
pub use self::capture::{Backtrace, BacktraceFrame, BacktraceSymbol};
7885
mod capture;
7986
}
8087
}

src/print.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::BytesOrWideString;
1+
#[cfg(feature = "std")]
2+
use super::{BacktraceFrame, BacktraceSymbol};
3+
use super::{BytesOrWideString, Frame, SymbolName};
24
use core::ffi::c_void;
35
use core::fmt;
46

@@ -105,7 +107,7 @@ impl BacktraceFrameFmt<'_, '_, '_> {
105107
/// This function requires the `std` feature of the `backtrace` crate to be
106108
/// enabled, and the `std` feature is enabled by default.
107109
#[cfg(feature = "std")]
108-
pub fn backtrace_frame(&mut self, frame: &crate::BacktraceFrame) -> fmt::Result {
110+
pub fn backtrace_frame(&mut self, frame: &BacktraceFrame) -> fmt::Result {
109111
let symbols = frame.symbols();
110112
for symbol in symbols {
111113
self.backtrace_symbol(frame, symbol)?;
@@ -125,8 +127,8 @@ impl BacktraceFrameFmt<'_, '_, '_> {
125127
#[cfg(feature = "std")]
126128
pub fn backtrace_symbol(
127129
&mut self,
128-
frame: &crate::BacktraceFrame,
129-
symbol: &crate::BacktraceSymbol,
130+
frame: &BacktraceFrame,
131+
symbol: &BacktraceSymbol,
130132
) -> fmt::Result {
131133
self.print_raw(
132134
frame.ip(),
@@ -144,7 +146,7 @@ impl BacktraceFrameFmt<'_, '_, '_> {
144146

145147
/// Prints a raw traced `Frame` and `Symbol`, typically from within the raw
146148
/// callbacks of this crate.
147-
pub fn symbol(&mut self, frame: &crate::Frame, symbol: &crate::Symbol) -> fmt::Result {
149+
pub fn symbol(&mut self, frame: &Frame, symbol: &super::Symbol) -> fmt::Result {
148150
self.print_raw(
149151
frame.ip(),
150152
symbol.name(),
@@ -162,7 +164,7 @@ impl BacktraceFrameFmt<'_, '_, '_> {
162164
pub fn print_raw(
163165
&mut self,
164166
frame_ip: *mut c_void,
165-
symbol_name: Option<crate::SymbolName<'_>>,
167+
symbol_name: Option<SymbolName<'_>>,
166168
filename: Option<BytesOrWideString<'_>>,
167169
lineno: Option<u32>,
168170
) -> fmt::Result {
@@ -182,7 +184,7 @@ impl BacktraceFrameFmt<'_, '_, '_> {
182184
fn print_raw_generic(
183185
&mut self,
184186
mut frame_ip: *mut c_void,
185-
symbol_name: Option<crate::SymbolName<'_>>,
187+
symbol_name: Option<SymbolName<'_>>,
186188
filename: Option<BytesOrWideString<'_>>,
187189
lineno: Option<u32>,
188190
) -> fmt::Result {

src/symbolize/dbghelp.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@
2727
2828
#![allow(bad_style)]
2929

30-
use crate::backtrace::FrameImp as Frame;
31-
use crate::dbghelp;
32-
use crate::symbolize::ResolveWhat;
33-
use crate::types::BytesOrWideString;
34-
use crate::windows::*;
35-
use crate::SymbolName;
30+
use super::super::{backtrace::FrameImp as Frame, dbghelp, windows::*};
31+
use super::{BytesOrWideString, ResolveWhat, SymbolName};
3632
use core::char;
3733
use core::ffi::c_void;
3834
use core::marker;

src/symbolize/gimli.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use self::gimli::read::EndianSlice;
88
use self::gimli::LittleEndian as Endian;
99
use self::mmap::Mmap;
1010
use self::stash::Stash;
11-
use crate::symbolize::ResolveWhat;
12-
use crate::types::BytesOrWideString;
13-
use crate::SymbolName;
11+
use super::BytesOrWideString;
12+
use super::ResolveWhat;
13+
use super::SymbolName;
1414
use addr2line::gimli;
1515
use core::convert::TryInto;
1616
use core::mem;
@@ -21,6 +21,11 @@ use std::fs::File;
2121
use std::path::Path;
2222
use std::prelude::v1::*;
2323

24+
#[cfg(backtrace_in_libstd)]
25+
mod std {
26+
pub use crate::*;
27+
}
28+
2429
#[cfg(windows)]
2530
#[path = "gimli/mmap_windows.rs"]
2631
mod mmap;
@@ -70,8 +75,6 @@ fn cx<'data>(stash: &'data Stash, object: Object<'data>) -> Option<Context<'data
7075

7176
macro_rules! mk {
7277
(Mapping { $map:expr, $inner:expr, $stash:expr }) => {{
73-
use crate::symbolize::gimli::{Context, Mapping, Mmap};
74-
7578
fn assert_lifetimes<'a>(_: &'a Mmap, _: &Context<'a>, _: &'a Stash) {}
7679
assert_lifetimes(&$map, &$inner, &$stash);
7780
Mapping {
@@ -93,8 +96,9 @@ fn mmap(path: &Path) -> Option<Mmap> {
9396
cfg_if::cfg_if! {
9497
if #[cfg(windows)] {
9598
use core::mem::MaybeUninit;
96-
use crate::windows::*;
99+
use super::super::windows::*;
97100
use std::os::windows::prelude::*;
101+
use alloc::vec;
98102

99103
mod coff;
100104
use self::coff::Object;

src/symbolize/gimli/coff.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use super::{Mapping, Path, Stash, Vec};
1+
use super::{Context, Mapping, Mmap, Path, Stash, Vec};
2+
use core::convert::TryFrom;
23
use object::pe::{ImageDosHeader, ImageSymbol};
34
use object::read::pe::{ImageNtHeaders, ImageOptionalHeader, SectionTable};
45
use object::read::StringTable;
@@ -8,7 +9,6 @@ use object::{Bytes, LittleEndian as LE};
89
type Pe = object::pe::ImageNtHeaders32;
910
#[cfg(target_pointer_width = "64")]
1011
type Pe = object::pe::ImageNtHeaders64;
11-
use std::convert::TryFrom;
1212

1313
impl Mapping {
1414
pub fn new(path: &Path) -> Option<Mapping> {

0 commit comments

Comments
 (0)