Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Frames Iterator for the Backtrace Type #78299

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
372ba21
Expose BacktraceFrame as public
seanchen1991 Oct 23, 2020
9b8e4fc
Add empty `frames` method to Backtrace type
seanchen1991 Oct 23, 2020
e07b25d
Fill in `frames` function
seanchen1991 Nov 3, 2020
277a2a4
Add `Frames` struct
seanchen1991 Nov 9, 2020
b4e21e6
Add `as_ref` method for `Frames` type
seanchen1991 Nov 9, 2020
43f2774
Remove `Backtrace::frames` method
seanchen1991 Nov 9, 2020
7331efe
Remove unnecessary newlines
seanchen1991 Nov 9, 2020
b43bcb6
Merge branch 'master' of github.com:rust-lang/rust
seanchen1991 Nov 12, 2020
6df53a1
Add `frames` method that doesn't borrow from lock
seanchen1991 Nov 12, 2020
37f4f13
Add private clone methods to Frame and BacktraceFrame
seanchen1991 Dec 2, 2020
747bb91
Add additional unstable feature flags
seanchen1991 Dec 2, 2020
c5d5912
Fix a type in Frames::clone
seanchen1991 Dec 2, 2020
30c5494
Add tracking issue
seanchen1991 Dec 3, 2020
4e6d2ef
Fix ownership issues
seanchen1991 Dec 3, 2020
e7df885
Add doc comments to `Frames` and `BacktraceFrame`
seanchen1991 Dec 3, 2020
61b198f
Derive Debug on `Frames`, `BacktraceFrame`, and `RawFrame`
seanchen1991 Dec 3, 2020
b4175b1
Tie `Frames` to `Backtrace` via PhantomData
seanchen1991 Dec 4, 2020
b30f662
Add `generate_fake_backtrace` fn to backtrace/tests.rs
seanchen1991 Dec 17, 2020
c624d22
Add test for empty frames iterator
seanchen1991 Dec 17, 2020
de98734
Impl `Iterator` for Frames
seanchen1991 Dec 17, 2020
da2e4a9
Fix IntoIter type
seanchen1991 Dec 17, 2020
0199300
Get empty iterator test passing
seanchen1991 Dec 18, 2020
48e6a38
Add test to check frames iterator count
seanchen1991 Dec 18, 2020
ff81eb1
Remove iterator impl on Backtrace Frame
seanchen1991 Jan 5, 2021
43b9783
Merge upstream changes
seanchen1991 Jan 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion library/std/src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ use crate::sync::atomic::{AtomicUsize, Ordering::SeqCst};
use crate::sync::Once;
use crate::sys_common::backtrace::{lock, output_filename};
use crate::vec::Vec;
use crate::marker::PhantomData;

/// A captured OS thread stack backtrace.
///
Expand Down Expand Up @@ -147,24 +148,39 @@ fn _assert_send_sync() {
_assert::<Backtrace>();
}

struct BacktraceFrame {
/// A single frame of a backtrace.
#[derive(Debug)]
#[unstable(feature = "backtrace_frames", issue = "79676")]
pub struct BacktraceFrame {
frame: RawFrame,
symbols: Vec<BacktraceSymbol>,
}

/// An iterator over the frames of a backtrace, created
/// by the [`frames`] method on [`Backtrace`].
#[derive(Debug)]
#[unstable(feature = "backtrace_frames", issue = "79676")]
pub struct Frames<'a> {
inner: Vec<BacktraceFrame>,
_backtrace: PhantomData<&'a Backtrace>
}

#[derive(Debug, Clone)]
enum RawFrame {
Actual(backtrace_rs::Frame),
#[cfg(test)]
Fake,
}

#[derive(Clone)]
struct BacktraceSymbol {
name: Option<Vec<u8>>,
filename: Option<BytesOrWide>,
lineno: Option<u32>,
colno: Option<u32>,
}

#[derive(Clone)]
enum BytesOrWide {
Bytes(Vec<u8>),
Wide(Vec<u16>),
Expand Down Expand Up @@ -353,6 +369,25 @@ impl Backtrace {
}
}

impl<'a> Backtrace {
/// Returns an iterator over the backtrace frames.
#[unstable(feature = "backtrace_frames", issue = "79676")]
pub fn frames(&self) -> Frames<'a> {
if let Inner::Captured(captured) = &self.inner {
let frames = &captured.lock().unwrap().frames;
Frames {
inner: frames.iter().map(|frame| frame.clone()).collect::<Vec<BacktraceFrame>>(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
inner: frames.iter().map(|frame| frame.clone()).collect::<Vec<BacktraceFrame>>(),
// NOTE: Frames are cloned to avoid returning references to them under the lock
// This could be avoided using a `SyncLazy` to initialize an immutable set of frames
inner: frames.iter().map(BacktraceFrame::clone).collect(),

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So with this change, what's the way to access the type inside of the SyncLazy? Before with the Mutex we had

if let Inner::Captured(captured) = &self.inner {
  let frames = &captured.lock().unwrap().frames;

What would we need to change the let frames line into to access the underlying frames?

_backtrace: PhantomData
}
} else {
Frames {
inner: vec![],
_backtrace: PhantomData
}
}
}
}

impl fmt::Display for Backtrace {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let capture = match &self.inner {
Expand Down Expand Up @@ -476,3 +511,24 @@ impl RawFrame {
}
}
}

#[unstable(feature = "backtrace_frames", issue = "79676")]
impl<'a> AsRef<[BacktraceFrame]> for Frames<'a> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be incompatible with lazy resolving of backtraces. Only an Iterator would be compatible.

fn as_ref(&self) -> &[BacktraceFrame] {
&self.inner
}
}


#[unstable(feature = "backtrace_frames", issue = "79676")]
impl BacktraceFrame {
// Private clone method so that we don't expose a
// public BacktraceFrame.clone() by deriving Clone
fn clone(&self) -> Self {
BacktraceFrame {
frame: self.frame.clone(),
symbols: self.symbols.clone(),
}
}
}

7 changes: 6 additions & 1 deletion library/std/src/backtrace/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ fn test_debug() {
},
],
})),
};
}
}

#[test]
fn test_debug() {
let backtrace = generate_fake_backtrace();

#[rustfmt::skip]
let expected = "Backtrace [\
Expand Down
2 changes: 1 addition & 1 deletion library/stdarch
Submodule stdarch updated 57 files
+7 −10 .github/workflows/main.yml
+8 −21 CONTRIBUTING.md
+0 −2 ci/run.sh
+1 −1 crates/assert-instr-macro/build.rs
+5 −1 crates/assert-instr-macro/src/lib.rs
+0 −558 crates/core_arch/avx512bw.md
+560 −564 crates/core_arch/avx512f.md
+15 −43 crates/core_arch/src/aarch64/crypto.rs
+3 −0 crates/core_arch/src/aarch64/mod.rs
+0 −88 crates/core_arch/src/aarch64/neon/generated.rs
+70 −1,030 crates/core_arch/src/aarch64/neon/mod.rs
+0 −5 crates/core_arch/src/arm/mod.rs
+0 −700 crates/core_arch/src/arm/neon/generated.rs
+0 −208 crates/core_arch/src/arm/neon/load_tests.rs
+1,393 −5,325 crates/core_arch/src/arm/neon/mod.rs
+0 −89 crates/core_arch/src/arm/neon/shift_and_insert_tests.rs
+1 −1 crates/core_arch/src/core_arch_docs.md
+1 −1 crates/core_arch/src/lib.rs
+0 −1 crates/core_arch/src/mod.rs
+0 −393 crates/core_arch/src/simd.rs
+9 −13 crates/core_arch/src/x86/avx.rs
+72 −72 crates/core_arch/src/x86/avx2.rs
+0 −760 crates/core_arch/src/x86/avx512bitalg.rs
+0 −13,881 crates/core_arch/src/x86/avx512bw.rs
+0 −1,170 crates/core_arch/src/x86/avx512cd.rs
+13,355 −29,511 crates/core_arch/src/x86/avx512f.rs
+0 −1,567 crates/core_arch/src/x86/avx512gfni.rs
+1 −1 crates/core_arch/src/x86/avx512ifma.rs
+0 −332 crates/core_arch/src/x86/avx512vaes.rs
+0 −266 crates/core_arch/src/x86/avx512vpclmulqdq.rs
+0 −541 crates/core_arch/src/x86/avx512vpopcntdq.rs
+1 −1 crates/core_arch/src/x86/fma.rs
+2 −2 crates/core_arch/src/x86/fxsr.rs
+0 −844 crates/core_arch/src/x86/macros.rs
+1 −76 crates/core_arch/src/x86/mod.rs
+2 −2 crates/core_arch/src/x86/rtm.rs
+9 −15 crates/core_arch/src/x86/sse.rs
+4 −16 crates/core_arch/src/x86/sse2.rs
+2 −2 crates/core_arch/src/x86/sse41.rs
+35 −25 crates/core_arch/src/x86/test.rs
+11 −10 crates/core_arch/src/x86/xsave.rs
+192 −1,668 crates/core_arch/src/x86_64/avx512f.rs
+2 −2 crates/core_arch/src/x86_64/fxsr.rs
+6 −6 crates/core_arch/src/x86_64/xsave.rs
+1 −1 crates/core_arch/tests/cpu-detection.rs
+1 −1 crates/std_detect/tests/cpu-detection.rs
+1 −1 crates/std_detect/tests/macro_trailing_commas.rs
+27 −76 crates/stdarch-gen/neon.spec
+4 −3 crates/stdarch-gen/src/main.rs
+57 −60 crates/stdarch-test/src/disassembly.rs
+2 −6 crates/stdarch-test/src/lib.rs
+3 −7 crates/stdarch-verify/src/lib.rs
+22 −59 crates/stdarch-verify/tests/arm.rs
+20 −17 crates/stdarch-verify/tests/mips.rs
+12 −89 crates/stdarch-verify/tests/x86-intel.rs
+1 −303 crates/stdarch-verify/x86-intel.xml
+2 −2 examples/hex.rs
2 changes: 1 addition & 1 deletion src/doc/book
Submodule book updated 218 files
2 changes: 1 addition & 1 deletion src/doc/embedded-book
2 changes: 1 addition & 1 deletion src/doc/nomicon
2 changes: 1 addition & 1 deletion src/llvm-project
Submodule llvm-project updated 195 files
2 changes: 1 addition & 1 deletion src/tools/cargo
Submodule cargo updated 184 files
2 changes: 1 addition & 1 deletion src/tools/miri
Submodule miri updated 121 files
2 changes: 1 addition & 1 deletion src/tools/rustfmt
Submodule rustfmt updated 74 files
+1 −5 .github/workflows/integration.yml
+1 −5 .github/workflows/linux.yml
+1 −5 .github/workflows/mac.yml
+2 −2 .github/workflows/upload-assets.yml
+38 −5 .github/workflows/windows.yml
+0 −144 CHANGELOG.md
+92 −112 Cargo.lock
+10 −10 Cargo.toml
+0 −50 Configurations.md
+94 −94 src/attr.rs
+11 −19 src/closures.rs
+1 −1 src/comment.rs
+0 −3 src/config/mod.rs
+0 −12 src/config/options.rs
+29 −29 src/expr.rs
+5 −15 src/formatting.rs
+52 −120 src/items.rs
+12 −16 src/lib.rs
+6 −4 src/macros.rs
+1 −1 src/modules/visitor.rs
+22 −83 src/reorder.rs
+0 −1 src/rewrite.rs
+1 −1 src/skip.rs
+1 −9 src/syntux/parser.rs
+73 −192 src/types.rs
+1 −9 src/utils.rs
+5 −21 src/visitor.rs
+0 −17 tests/source/configs/group_imports/StdExternalCrate-merge_imports.rs
+0 −6 tests/source/configs/group_imports/StdExternalCrate-nested.rs
+0 −17 tests/source/configs/group_imports/StdExternalCrate-no_reorder.rs
+0 −15 tests/source/configs/group_imports/StdExternalCrate.rs
+0 −10 tests/source/expr.rs
+0 −14 tests/source/extern.rs
+0 −11 tests/source/issue-2781.rs
+0 −85 tests/source/issue-4120.rs
+0 −21 tests/source/issue-4243.rs
+0 −16 tests/source/issue-4244.rs
+0 −26 tests/source/issue-4245.rs
+0 −4 tests/source/issue-4382.rs
+0 −20 tests/source/issue-4577.rs
+0 −27 tests/source/issue_4475.rs
+0 −8 tests/source/issue_4528.rs
+0 −19 tests/source/issue_4584.rs
+0 −7 tests/source/negative-impl.rs
+0 −11 tests/source/structs.rs
+0 −6 tests/source/type.rs
+0 −16 tests/target/configs/group_imports/StdExternalCrate-merge_imports.rs
+0 −7 tests/target/configs/group_imports/StdExternalCrate-nested.rs
+0 −15 tests/target/configs/group_imports/StdExternalCrate-no_reorder.rs
+0 −13 tests/target/configs/group_imports/StdExternalCrate.rs
+0 −22 tests/target/configs/where_single_line/true-with-brace-style.rs
+0 −10 tests/target/expr.rs
+0 −6 tests/target/extern.rs
+0 −11 tests/target/issue-2781.rs
+0 −7 tests/target/issue-4029.rs
+0 −8 tests/target/issue-4115.rs
+0 −85 tests/target/issue-4120.rs
+0 −18 tests/target/issue-4152.rs
+0 −28 tests/target/issue-4243.rs
+0 −20 tests/target/issue-4244.rs
+0 −34 tests/target/issue-4245.rs
+0 −5 tests/target/issue-4313.rs
+0 −10 tests/target/issue-4382.rs
+0 −15 tests/target/issue-4577.rs
+0 −6 tests/target/issue_4467.rs
+0 −29 tests/target/issue_4475.rs
+0 −6 tests/target/issue_4522.rs
+0 −8 tests/target/issue_4528.rs
+0 −5 tests/target/issue_4545.rs
+0 −32 tests/target/issue_4584.rs
+0 −22 tests/target/macro_rules_semi.rs
+0 −14 tests/target/negative-impl.rs
+0 −14 tests/target/structs.rs
+0 −4 tests/target/type.rs