-
Notifications
You must be signed in to change notification settings - Fork 15
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
Log backtrace on sigsegv and sigbus signals #22
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,48 @@ | ||
// | ||
// traps.rs | ||
// | ||
// Copyright (C) 2023 Posit Software, PBC. All rights reserved. | ||
// | ||
// | ||
|
||
// Call this after initialising the `log` package. Instruments SIGBUS, | ||
// SIGSEGV, and SIGILL to generate a backtrace with `info` verbosity | ||
// (lowest level so it's always reported). | ||
// | ||
// This uses `signal()` instead of `sigaction()` for Windows support | ||
// (SIGSEGV is one of the rare supported signals) | ||
// | ||
// Note that Rust also has a SIGSEGV handler to catch stack overflows. In | ||
// this case it displays an informative message and aborts the program (no | ||
// segfaults in Rust!). Ideally we'd save the Rust handler and notify | ||
// it. However the only safe way to notify an old handler on Unixes is to | ||
// use `sigaction()` so that we get the information needed to determine the | ||
// type of handler (old or new school). So we'd need to make a different | ||
// implementation for Windows (which only supports old style) and for Unix, | ||
// and this doesn't seem worth it. | ||
pub fn register_trap_handlers() { | ||
unsafe { | ||
libc::signal(libc::SIGBUS, backtrace_handler as libc::sighandler_t); | ||
libc::signal(libc::SIGSEGV, backtrace_handler as libc::sighandler_t); | ||
libc::signal(libc::SIGILL, backtrace_handler as libc::sighandler_t); | ||
} | ||
} | ||
|
||
extern "C" fn backtrace_handler(signum: libc::c_int) { | ||
// Prevent infloop into the handler | ||
unsafe { | ||
libc::signal(signum, libc::SIG_DFL); | ||
} | ||
|
||
let mut header = format!("\n>>> Backtrace for signal {}", signum); | ||
|
||
if let Some(name) = std::thread::current().name() { | ||
header = format!("{}\n>>> In thread {}", header, name); | ||
} | ||
|
||
// Unlike asynchronous signals, SIGSEGV and SIGBUS are synchronous and | ||
// always delivered to the thread that caused it, so we can just | ||
// capture the current thread's backtrace | ||
let bt = backtrace::Backtrace::new(); | ||
log::info!("{}\n{:?}", header, bt); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to disable this in release builds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would there be a downside to leave it on? On the upside it could be useful for R developers to get a backtrace of their native code on crash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right -- let's leave it on.