forked from libbpf/blazesym
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optionally include backtraces in errors
Seeing error and their call chains can be tremendously useful and most of the time that is sufficient to get a good understanding of an issue. However, occasionally errors don't contain enough context and messages are reused on multiple call paths. With this change we enhance our Error type with the capability to capture a backtrace at the time the error was created. This backtrace is then contained in the Debug representation. Note that because capturing a backtrace is a costly operation, backtrace capture is controlled by environment variables in the same way that Rust proper behaves. Signed-off-by: Daniel Müller <[email protected]>
- Loading branch information
Showing
6 changed files
with
161 additions
and
17 deletions.
There are no files selected for viewing
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,40 @@ | ||
//! This test relies on environment variable modification for its | ||
//! workings and, hence, relies on being run in a dedicated process. Do | ||
//! not add additional test cases unless they are guaranteed to not | ||
//! interfere. | ||
|
||
#![allow(clippy::let_and_return, clippy::let_unit_value)] | ||
|
||
use std::env; | ||
use std::io; | ||
|
||
use blazesym::Error; | ||
|
||
|
||
/// Make sure that we can capture backtraces in errors. | ||
/// | ||
/// # Notes | ||
/// This test requires sufficient debug information to be present so | ||
/// that the file name is contained in the backtrace. For that reason we | ||
/// only run it on debug builds (represented by the `debug_assertions` | ||
/// proxy cfg). | ||
#[test] | ||
fn error_backtrace() { | ||
if !cfg!(debug_assertions) { | ||
return | ||
} | ||
|
||
// Ensure that we capture a backtrace. | ||
let () = env::set_var("RUST_LIB_BACKTRACE", "1"); | ||
|
||
let err = io::Error::new(io::ErrorKind::InvalidData, "some invalid data"); | ||
let err = Error::from(err); | ||
let debug = format!("{err:?}"); | ||
|
||
let start_idx = debug.find("Stack backtrace").unwrap(); | ||
let backtrace = &debug[start_idx..]; | ||
assert!( | ||
backtrace.contains("tests/error_backtrace.rs"), | ||
"{backtrace}" | ||
); | ||
} |
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,26 @@ | ||
//! This test relies on environment variable modification for its | ||
//! workings and, hence, relies on being run in a dedicated process. Do | ||
//! not add additional test cases unless they are guaranteed to not | ||
//! interfere. | ||
|
||
#![allow(clippy::let_and_return, clippy::let_unit_value)] | ||
|
||
use std::env; | ||
use std::io; | ||
|
||
use blazesym::Error; | ||
|
||
|
||
/// Make sure that we do not emit backtraces in errors when | ||
/// the `RUST_LIB_BACKTRACE` environment variable is not present. | ||
#[test] | ||
fn error_no_backtrace1() { | ||
let () = env::remove_var("RUST_BACKTRACE"); | ||
let () = env::remove_var("RUST_LIB_BACKTRACE"); | ||
|
||
let err = io::Error::new(io::ErrorKind::InvalidData, "some invalid data"); | ||
let err = Error::from(err); | ||
let debug = format!("{err:?}"); | ||
|
||
assert_eq!(debug.find("Stack backtrace"), None); | ||
} |
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,25 @@ | ||
//! This test relies on environment variable modification for its | ||
//! workings and, hence, relies on being run in a dedicated process. Do | ||
//! not add additional test cases unless they are guaranteed to not | ||
//! interfere. | ||
|
||
#![allow(clippy::let_and_return, clippy::let_unit_value)] | ||
|
||
use std::env; | ||
use std::io; | ||
|
||
use blazesym::Error; | ||
|
||
|
||
/// Make sure that we do not emit backtraces in errors when | ||
/// the `RUST_LIB_BACKTRACE` environment variable is "0". | ||
#[test] | ||
fn error_no_backtrace2() { | ||
let () = env::set_var("RUST_LIB_BACKTRACE", "0"); | ||
|
||
let err = io::Error::new(io::ErrorKind::InvalidData, "some invalid data"); | ||
let err = Error::from(err); | ||
let debug = format!("{err:?}"); | ||
|
||
assert_eq!(debug.find("Stack backtrace"), None); | ||
} |