-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from knurling-rs/panic
add defmt::panic!
- Loading branch information
Showing
20 changed files
with
696 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# `panic!` and `assert!` | ||
|
||
The `defmt` crate provides its own version of `panic!`-like and `assert!`-like macros. | ||
The `defmt` version of these macros will log the panic message using `defmt` and then call `core::panic!` (by default). | ||
Because the panic message is formatted using `defmt!` the format string must use the same syntax as the logging macros (e.g. `info!`). | ||
|
||
## `#[defmt::panic_handler]` | ||
|
||
Because `defmt::panic!` invokes `core::panic!` this can result in the panic message being printed twice if your `#[core::panic_handler]` also prints the panic message. | ||
This is the case if you use [`panic-probe`] with the `print-defmt` feature enabled but not an issue if you are using the [`panic-abort`] crate, for example. | ||
|
||
[`panic-probe`]: https://crates.io/crates/panic-probe | ||
[`panic-abort`]: https://crates.io/crates/panic-abort | ||
|
||
To avoid this issue you can use the `#[defmt::panic_handler]` to *override* the panicking behavior of `defmt::panic`-like and `defmt::assert`-like macros. | ||
This attribute must be placed on a function with signature `fn() -> !`. | ||
In this function you'll want to replicate the panicking behavior of the Rust `#[panic_handler]` but leave out the part that prints the panic message. | ||
For example: | ||
|
||
<!-- NOTE(ignore) we can't compile this test because the `panic_handler` defined here collides with the one in `std` --> | ||
|
||
``` rust, ignore | ||
#[panic_handler] // built-in ("core") attribute | ||
fn core_panic(info: &core::panic::PanicInfo) -> ! { | ||
print(info); // e.g. using RTT | ||
reset() | ||
} | ||
#[defmt::panic_handler] // defmt's attribute | ||
fn defmt_panic() -> ! { | ||
// leave out the printing part here | ||
reset() | ||
} | ||
``` | ||
|
||
If you are using the `panic-probe` crate then you should "abort" (call `cortex_m::asm::udf`) from `#[defmt::panic_handler]` to match its behavior. | ||
|
||
NOTE: even if you don't run into the "double panic message printed" issue you may still want to use `#[defmt::panic_handler]` because this way `defmt::panic` and `defmt::assert` will *not* go through the `core::panic` machinery and that *may* reduce code size (we recommend you measure the effect of the change). |
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,3 @@ | ||
0.000000 ERROR panicked at 'assertion failed: `(left == right)`: dev' | ||
left: `41` | ||
right: `43` |
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,3 @@ | ||
0.000000 ERROR panicked at 'assertion failed: `(left == right)`: release' | ||
left: `41` | ||
right: `43` |
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 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use cortex_m_rt::entry; | ||
|
||
use defmt_semihosting as _; // global logger | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let x = 42; | ||
defmt::debug_assert_eq!(x - 1, x + 1, "dev"); | ||
defmt::assert_eq!(x - 1, x + 1, "release"); | ||
defmt::unreachable!(); | ||
} | ||
|
||
// like `panic-semihosting` but doesn't print to stdout (that would corrupt the defmt stream) | ||
#[cfg(target_os = "none")] | ||
#[panic_handler] | ||
fn panic(_: &core::panic::PanicInfo) -> ! { | ||
use cortex_m_semihosting::debug; | ||
|
||
loop { | ||
debug::exit(debug::EXIT_SUCCESS) | ||
} | ||
} |
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,2 @@ | ||
0.000000 ERROR panicked at 'assertion failed: `(left != right)`: dev' | ||
left/right: `42` |
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,2 @@ | ||
0.000000 ERROR panicked at 'assertion failed: `(left != right)`: release' | ||
left/right: `42` |
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 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use cortex_m_rt::entry; | ||
|
||
use defmt_semihosting as _; // global logger | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let x = 42; | ||
defmt::debug_assert_ne!(x, x, "dev"); | ||
defmt::assert_ne!(x, x, "release"); | ||
defmt::unreachable!(); | ||
} | ||
|
||
// like `panic-semihosting` but doesn't print to stdout (that would corrupt the defmt stream) | ||
#[cfg(target_os = "none")] | ||
#[panic_handler] | ||
fn panic(_: &core::panic::PanicInfo) -> ! { | ||
use cortex_m_semihosting::debug; | ||
|
||
loop { | ||
debug::exit(debug::EXIT_SUCCESS) | ||
} | ||
} |
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 @@ | ||
0.000000 ERROR panicked at 'assertion failed: dev' |
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 @@ | ||
0.000000 ERROR panicked at 'assertion failed: release' |
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 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use cortex_m_rt::entry; | ||
|
||
use defmt_semihosting as _; // global logger | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let dev = false; | ||
let release = false; | ||
defmt::debug_assert!(dev); | ||
defmt::assert!(release); | ||
defmt::unreachable!(); | ||
} | ||
|
||
// like `panic-semihosting` but doesn't print to stdout (that would corrupt the defmt stream) | ||
#[cfg(target_os = "none")] | ||
#[panic_handler] | ||
fn panic(_: &core::panic::PanicInfo) -> ! { | ||
use cortex_m_semihosting::debug; | ||
|
||
loop { | ||
debug::exit(debug::EXIT_SUCCESS) | ||
} | ||
} |
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 @@ | ||
0.000000 ERROR panicked at 'The answer is 42' |
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 @@ | ||
0.000000 ERROR panicked at 'The answer is 42' |
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,22 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use cortex_m_rt::entry; | ||
|
||
use defmt_semihosting as _; // global logger | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
defmt::panic!("The answer is {:?}", 42); | ||
} | ||
|
||
// like `panic-semihosting` but doesn't print to stdout (that would corrupt the defmt stream) | ||
#[cfg(target_os = "none")] | ||
#[panic_handler] | ||
fn panic(_: &core::panic::PanicInfo) -> ! { | ||
use cortex_m_semihosting::debug; | ||
|
||
loop { | ||
debug::exit(debug::EXIT_SUCCESS) | ||
} | ||
} |
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
Oops, something went wrong.