diff --git a/CHANGELOG.md b/CHANGELOG.md index d0a15e22..2cd8c3a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - [#859]: `defmt`: Satisfy clippy - [#858]: `defmt`: Implement "passthrough" trait impls for *2Format wrappers - [#857]: Add an octal display hint (`:o`) +- [#856]: `defmt`: Add a `Format` impl for `PanicInfo` and related types. - [#855]: `defmt-print`: Now uses tokio to make tcp and stdin reads async (in preparation for a `watch elf` flag) - [#852]: `CI`: Update mdbook to v0.4.40 - [#848]: `decoder`: add optional one-line format @@ -22,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). [#859]: https://github.com/knurling-rs/defmt/pull/859 [#858]: https://github.com/knurling-rs/defmt/pull/858 [#857]: https://github.com/knurling-rs/defmt/pull/857 +[#856]: https://github.com/knurling-rs/defmt/pull/856 [#855]: https://github.com/knurling-rs/defmt/pull/855 [#852]: https://github.com/knurling-rs/defmt/pull/852 [#848]: https://github.com/knurling-rs/defmt/pull/848 diff --git a/defmt/src/impls/core_/mod.rs b/defmt/src/impls/core_/mod.rs index c9b3c770..b005ca57 100644 --- a/defmt/src/impls/core_/mod.rs +++ b/defmt/src/impls/core_/mod.rs @@ -12,6 +12,7 @@ mod cell; mod net; mod num; mod ops; +mod panic; mod ptr; mod slice; diff --git a/defmt/src/impls/core_/panic.rs b/defmt/src/impls/core_/panic.rs new file mode 100644 index 00000000..78a56d74 --- /dev/null +++ b/defmt/src/impls/core_/panic.rs @@ -0,0 +1,27 @@ +use core::panic; + +use super::*; + +impl<'a> Format for panic::PanicInfo<'a> { + fn format(&self, f: Formatter) { + if let Some(location) = self.location() { + crate::write!(f, "panicked at {}", location); + } else { + crate::write!(f, "panicked"); + } + // TODO: consider supporting self.message() once stabilized, or add a crate feature for + // conditional support + } +} + +impl<'a> Format for panic::Location<'a> { + fn format(&self, f: Formatter) { + crate::write!( + f, + "{=str}:{=u32}:{=u32}", + self.file(), + self.line(), + self.column() + ); + } +} diff --git a/firmware/qemu/src/bin/panic_info.out b/firmware/qemu/src/bin/panic_info.out new file mode 100644 index 00000000..4f76222f --- /dev/null +++ b/firmware/qemu/src/bin/panic_info.out @@ -0,0 +1 @@ +INFO PanicInfo: panicked at qemu/src/bin/panic_info.rs:14:5 diff --git a/firmware/qemu/src/bin/panic_info.rs b/firmware/qemu/src/bin/panic_info.rs new file mode 100644 index 00000000..f260cd99 --- /dev/null +++ b/firmware/qemu/src/bin/panic_info.rs @@ -0,0 +1,23 @@ +#![no_std] +#![no_main] + +use core::panic; +use cortex_m_semihosting::debug; + +use defmt_semihosting as _; // global logger + +#[cortex_m_rt::entry] +fn main() -> ! { + // Note: this test is a bit brittle in that the line/column number of the following panic is + // included in the test snapshot. Hence, be mindful to update the snapshot if you want to + // add any additional code to this file above the following line! + panic!("aaah!") +} + +#[panic_handler] +fn panic(panic_info: &panic::PanicInfo) -> ! { + defmt::info!("PanicInfo: {=?}", panic_info); + loop { + debug::exit(debug::EXIT_SUCCESS) + } +} diff --git a/xtask/src/snapshot.rs b/xtask/src/snapshot.rs index f19e4181..bfcf950d 100644 --- a/xtask/src/snapshot.rs +++ b/xtask/src/snapshot.rs @@ -26,6 +26,7 @@ pub(crate) fn all_snapshot_tests() -> Vec<&'static str> { "hints_inner", "dbg", "net", + "panic_info", ]; const NIGHTLY_SNAPSHOT_TESTS: &[&str] = &["alloc"];