-
Notifications
You must be signed in to change notification settings - Fork 13.4k
std: print a backtrace on stackoverflow #133170
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::ffi::{CStr, c_void}; | ||
use crate::mem::MaybeUninit; | ||
use crate::ptr; | ||
|
||
/// Prints the current backtrace, even in a signal handlers. | ||
/// | ||
/// Since `backtrace` requires locking and memory allocation, it cannot be used | ||
/// from inside a signal handler. Instead, this uses `libunwind` and `dladdr`, | ||
/// even though both of them are not guaranteed to be async-signal-safe, strictly | ||
/// speaking. However, at least LLVM's libunwind (used by macOS) has a [test] for | ||
/// unwinding in signal handlers, and `dladdr` is used by `backtrace_symbols_fd` | ||
/// in glibc, which it [documents] as async-signal-safe. In practice, this hack | ||
/// works well enough on GNU/Linux and macOS (and perhaps some other platforms, | ||
/// but we haven't enabled those yet). | ||
/// | ||
/// [test]: https://github.com/llvm/llvm-project/blob/a6385a3fc8a88f092d07672210a1e773481c2919/libunwind/test/signal_unwind.pass.cpp | ||
/// [documents]: https://www.gnu.org/software/libc/manual/html_node/Backtraces.html#index-backtrace_005fsymbols_005ffd | ||
pub fn print() { | ||
extern "C" fn frame( | ||
ctx: *mut unwind::_Unwind_Context, | ||
arg: *mut c_void, | ||
) -> unwind::_Unwind_Reason_Code { | ||
let count = unsafe { &mut *(arg as *mut usize) }; | ||
let depth = *count; | ||
*count += 1; | ||
if depth >= 128 { | ||
return unwind::_URC_NORMAL_STOP; | ||
} | ||
|
||
let ip = unsafe { unwind::_Unwind_GetIP(ctx) }; | ||
let mut info = MaybeUninit::uninit(); | ||
let r = unsafe { libc::dladdr(ip.cast(), info.as_mut_ptr()) }; | ||
if r != 0 { | ||
let info = unsafe { info.assume_init() }; | ||
if !info.dli_sname.is_null() { | ||
let name = unsafe { CStr::from_ptr(info.dli_sname) }; | ||
if let Ok(name) = name.to_str() { | ||
rtprintpanic!("{depth}: {}\n", rustc_demangle::demangle(name)); | ||
return unwind::_URC_NO_REASON; | ||
} | ||
} | ||
} | ||
|
||
rtprintpanic!("{depth}: {ip:p}\n"); | ||
unwind::_URC_NO_REASON | ||
} | ||
|
||
let mut count = 0usize; | ||
unsafe { unwind::_Unwind_Backtrace(frame, ptr::from_mut(&mut count).cast()) }; | ||
if count >= 128 { | ||
rtprintpanic!("[... some frames omitted ...]\n"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.